Remove Parser.Range and subsliceOffset
Range() existed to recover byte offsets from Highlight subslices. Now that ParserError carries an explicit Offset field, Range() is unnecessary. Remove it along with the private subsliceOffset helper in ast.go. Tests now use perr.Offset directly and construct Range literals for Shape() calls. Co-authored-by: Thomas Pelletier <thomas@pelletier.dev>
This commit is contained in:
@@ -90,18 +90,6 @@ type Range struct {
|
|||||||
Length uint32
|
Length uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
func subsliceOffset(data []byte, subslice []byte) int {
|
|
||||||
if len(subslice) == 0 {
|
|
||||||
return len(data)
|
|
||||||
}
|
|
||||||
for i := range data {
|
|
||||||
if &data[i] == &subslice[0] {
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
panic("subslice is not within data")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Next returns a pointer to the next node, or nil if there is no next node.
|
// Next returns a pointer to the next node, or nil if there is no next node.
|
||||||
func (n *Node) Next() *Node {
|
func (n *Node) Next() *Node {
|
||||||
if n.next < 0 {
|
if n.next < 0 {
|
||||||
|
|||||||
@@ -70,18 +70,6 @@ func (p *Parser) offsetOf(b []byte) int {
|
|||||||
return len(p.data) - len(b)
|
return len(p.data) - len(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Range returns a range description that corresponds to a given slice of the
|
|
||||||
// input. If the argument is not a subslice of the parser input, this function
|
|
||||||
// panics.
|
|
||||||
//
|
|
||||||
// Prefer using ParserError.Offset directly for error position information.
|
|
||||||
func (p *Parser) Range(b []byte) Range {
|
|
||||||
return Range{
|
|
||||||
Offset: uint32(subsliceOffset(p.data, b)), //nolint:gosec // TOML documents are small
|
|
||||||
Length: uint32(len(b)), //nolint:gosec // TOML documents are small
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// rangeOfToken computes the Range of a token given the remaining bytes after the token.
|
// rangeOfToken computes the Range of a token given the remaining bytes after the token.
|
||||||
// This is used when the token was extracted from the beginning of some position,
|
// This is used when the token was extracted from the beginning of some position,
|
||||||
// and 'rest' is what remains after the token.
|
// and 'rest' is what remains after the token.
|
||||||
|
|||||||
@@ -674,7 +674,7 @@ key3 = "value3"
|
|||||||
assert.Equal(t, []string{"key1", "key2", "key3"}, keys)
|
assert.Equal(t, []string{"key1", "key2", "key3"}, keys)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRangeOffsetAfterComment(t *testing.T) {
|
func TestErrorOffsetAfterComment(t *testing.T) {
|
||||||
input := []byte("# comment\n= \"value\"")
|
input := []byte("# comment\n= \"value\"")
|
||||||
|
|
||||||
p := Parser{}
|
p := Parser{}
|
||||||
@@ -689,12 +689,11 @@ func TestRangeOffsetAfterComment(t *testing.T) {
|
|||||||
if !errors.As(err, &perr) {
|
if !errors.As(err, &perr) {
|
||||||
t.Fatalf("expected ParserError, got %T", err)
|
t.Fatalf("expected ParserError, got %T", err)
|
||||||
}
|
}
|
||||||
r := p.Range(perr.Highlight)
|
|
||||||
shape := p.Shape(r)
|
|
||||||
|
|
||||||
if r.Offset != 10 {
|
if perr.Offset != 10 {
|
||||||
t.Errorf("Range offset: got %d, want 10", r.Offset)
|
t.Errorf("offset: got %d, want 10", perr.Offset)
|
||||||
}
|
}
|
||||||
|
shape := p.Shape(Range{Offset: uint32(perr.Offset), Length: uint32(len(perr.Highlight))})
|
||||||
if shape.Start.Line != 2 || shape.Start.Column != 1 {
|
if shape.Start.Line != 2 || shape.Start.Column != 1 {
|
||||||
t.Errorf("position: got %d:%d, want 2:1", shape.Start.Line, shape.Start.Column)
|
t.Errorf("position: got %d:%d, want 2:1", shape.Start.Line, shape.Start.Column)
|
||||||
}
|
}
|
||||||
@@ -753,8 +752,7 @@ func TestErrorHighlightPositions(t *testing.T) {
|
|||||||
if !errors.As(err, &perr) {
|
if !errors.As(err, &perr) {
|
||||||
t.Fatalf("expected ParserError, got %T", err)
|
t.Fatalf("expected ParserError, got %T", err)
|
||||||
}
|
}
|
||||||
r := p.Range(perr.Highlight)
|
shape := p.Shape(Range{Offset: uint32(perr.Offset), Length: uint32(len(perr.Highlight))})
|
||||||
shape := p.Shape(r)
|
|
||||||
|
|
||||||
if shape.Start.Line != e.wantLine {
|
if shape.Start.Line != e.wantLine {
|
||||||
t.Errorf("line: got %d, want %d", shape.Start.Line, e.wantLine)
|
t.Errorf("line: got %d, want %d", shape.Start.Line, e.wantLine)
|
||||||
|
|||||||
Reference in New Issue
Block a user