4 Commits

Author SHA1 Message Date
amery 322e383012 lexer: introduce Position.Next()/Position.NextLine() factories
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 16:28:47 +00:00
amery 7ca5f7be25 lexer: ErrUnacceptableRune
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 16:28:47 +00:00
amery 585dbba0b1 Merge branch 'pr-amery-lexer-position' into next-amery 2023-08-29 16:25:53 +00:00
amery 9425ba0f7c lexer: introduce a Position (Line, Column) handler
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-29 16:22:59 +00:00
2 changed files with 30 additions and 0 deletions
+6
View File
@@ -1,6 +1,7 @@
package lexer
import (
"errors"
"fmt"
"strings"
)
@@ -9,6 +10,11 @@ var (
_ error = (*Error)(nil)
)
var (
// ErrUnacceptableRune indicates the read rune
ErrUnacceptableRune = errors.New("rune not acceptable in context")
)
// Error represents a generic parsing error
type Error struct {
Filename string
+24
View File
@@ -41,6 +41,30 @@ func (p *Position) Step() {
p.Column++
}
// StepN moves the column N places forward
func (p *Position) StepN(n int) {
if p.Line == 0 {
p.Reset()
}
switch {
case n > 0:
p.Column += n
default:
panic(fmt.Errorf("invalid %v increment", n))
}
}
// StepLine moves position to the start of the next line
func (p *Position) StepLine() {
if p.Line == 0 {
p.Reset()
}
p.Line++
p.Column = 1
}
// Next returns a new Position one rune forward
// on the line
func (p Position) Next() Position {