Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ast: move idl.Position to the ast package #504

Merged
merged 2 commits into from
Jun 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion idl/position.go → ast/position.go
Original file line number Diff line number Diff line change
@@ -18,9 +18,18 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package idl
package ast

// Position represents a position in the parsed document.
type Position struct {
Line int
}

// Pos attempts to return the position of a Node in the parsed document.
// For most use cases, prefer to use idl.Info to access positional information.
func Pos(n Node) (Position, bool) {
if nl, ok := n.(nodeWithLine); ok {
return Position{Line: nl.lineNumber()}, true
}
return Position{}, false
}
6 changes: 3 additions & 3 deletions idl/config_test.go
Original file line number Diff line number Diff line change
@@ -39,8 +39,8 @@ func TestParse(t *testing.T) {
func TestInfoPos(t *testing.T) {
c := &Config{Info: &Info{}}
prog, err := c.Parse([]byte(`const string a = 'a';`))
if assert.NoError(t, err, "%v", err) {
assert.Equal(t, Position{Line: 0}, c.Info.Pos(prog))
assert.Equal(t, Position{Line: 1}, c.Info.Pos(prog.Definitions[0]))
if assert.NoError(t, err) {
assert.Equal(t, ast.Position{Line: 0}, c.Info.Pos(prog))
assert.Equal(t, ast.Position{Line: 1}, c.Info.Pos(prog.Definitions[0]))
}
}
5 changes: 3 additions & 2 deletions idl/error.go
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ import (
"bytes"
"fmt"

"go.uber.org/thriftrw/ast"
"go.uber.org/thriftrw/idl/internal"
)

@@ -33,7 +34,7 @@ type ParseError struct{ Errors []Error }

// Error holds an error and the position that caused it.
type Error struct {
Pos Position
Pos ast.Position
Err error
}

@@ -44,7 +45,7 @@ func newParseError(errors []internal.ParseError) error {
errs := make([]Error, len(errors))
for i, err := range errors {
errs[i] = Error{
Pos: Position{Line: err.Pos.Line},
Pos: ast.Position{Line: err.Pos.Line},
Err: err.Err,
}
}
10 changes: 5 additions & 5 deletions idl/info.go
Original file line number Diff line number Diff line change
@@ -30,11 +30,11 @@ type Info struct {
nodePositions internal.NodePositions
}

// Pos returns a node's position in the parsed document.
func (i *Info) Pos(n ast.Node) Position {
if line := ast.LineNumber(n); line != 0 {
return Position{Line: line}
// Pos returns a Node's position in the parsed document.
func (i *Info) Pos(n ast.Node) ast.Position {
if pos, ok := ast.Pos(n); ok {
return pos
}
pos := i.nodePositions[n]
return Position{Line: pos.Line}
return ast.Position{Line: pos.Line}
}
8 changes: 4 additions & 4 deletions idl/info_test.go
Original file line number Diff line number Diff line change
@@ -32,20 +32,20 @@ func TestPos(t *testing.T) {
tests := []struct {
node ast.Node
pos *internal.Position
want Position
want ast.Position
}{
{
node: &ast.Struct{Line: 10},
want: Position{Line: 10},
want: ast.Position{Line: 10},
},
{
node: ast.ConstantString("s"),
want: Position{Line: 0},
want: ast.Position{Line: 0},
},
{
node: ast.ConstantString("s"),
pos: &internal.Position{Line: 1},
want: Position{Line: 1},
want: ast.Position{Line: 1},
},
}