Skip to content

Commit

Permalink
ast: implement String() for ast.Position
Browse files Browse the repository at this point in the history
  • Loading branch information
jparise committed Jun 28, 2021
1 parent 8c499ae commit 3a05fd2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions ast/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@

package ast

import "strconv"

// Position represents a position in the parsed document.
// Line and column numbers are 1-based.
type Position struct {
Line int
Column int
}

func (p Position) String() string {
s := strconv.Itoa(p.Line)
if c := p.Column; c > 0 {
s += ":" + strconv.Itoa(c)
}
return s
}

// 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) {
Expand Down
2 changes: 1 addition & 1 deletion idl/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (pe *ParseError) Error() string {
var buffer bytes.Buffer
buffer.WriteString("parse error\n")
for _, pe := range pe.Errors {
buffer.WriteString(fmt.Sprintf(" line %d:%d: %s\n", pe.Pos.Line, pe.Pos.Column, pe.Err))
buffer.WriteString(fmt.Sprintf(" line %s: %s\n", pe.Pos, pe.Err))
}
return buffer.String()
}

0 comments on commit 3a05fd2

Please sign in to comment.