Skip to content

Commit

Permalink
feat: add Position.Add()
Browse files Browse the repository at this point in the history
Useful for sub-parsers.
  • Loading branch information
alecthomas committed Sep 9, 2023
1 parent da27a09 commit df2ad66
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 0 additions & 1 deletion bin/.jq-1.7.pkg

This file was deleted.

1 change: 0 additions & 1 deletion bin/jq

This file was deleted.

14 changes: 14 additions & 0 deletions lexer/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ func (p *Position) Advance(span string) {
}
}

// Add returns a new Position that is the sum of this position and "pos".
//
// This is useful when parsing values from a parent grammar.
func (p Position) Add(pos Position) Position {
p.Line += pos.Line - 1
if pos.Line > 1 {
p.Column = pos.Column
} else {
p.Column += pos.Column - 1
}
p.Offset += pos.Offset
return p
}

func (p Position) GoString() string {
return fmt.Sprintf("Position{Filename: %q, Offset: %d, Line: %d, Column: %d}",
p.Filename, p.Offset, p.Line, p.Column)
Expand Down

0 comments on commit df2ad66

Please sign in to comment.