3 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
3 changed files with 33 additions and 10 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
+1 -10
View File
@@ -1,11 +1,6 @@
// Package lexer provides basic helpers to implement parsers
package lexer
import (
"errors"
"io"
)
// StateFn is a State Function of the parser
type StateFn func() (StateFn, error)
@@ -16,11 +11,7 @@ func Run(fn StateFn) error {
var err error
fn, err = fn()
switch {
case errors.Is(err, io.EOF):
// EOF
return nil
case err != nil:
if err != nil {
// failed
return err
}
+26
View File
@@ -64,3 +64,29 @@ func (p *Position) StepLine() {
p.Line++
p.Column = 1
}
// Next returns a new Position one rune forward
// on the line
func (p Position) Next() Position {
if p.Line == 0 {
p.Reset()
}
return Position{
Line: p.Line,
Column: p.Column + 1,
}
}
// NextLine returns a new Position at the begining of the next
// line.
func (p Position) NextLine() Position {
if p.Line == 0 {
p.Reset()
}
return Position{
Line: p.Line + 1,
Column: 1,
}
}