3 Commits

Author SHA1 Message Date
amery f8f6ff9e11 Merge pull request 'lexer: Reader.Accept(), AcceptAll(), and StateFn' (#5)
Reviewed-on: #5
2023-08-30 19:02:15 +02:00
amery 05d504346e lexer: extend Run() to treat io.EOF as non-error termination
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-30 01:50:12 +01:00
amery 93c58cdc26 Merge pull request 'lexer: introduce a Position (Line, Column) handler' (#8)
Reviewed-on: #8
2023-08-29 22:14:57 +02:00
3 changed files with 10 additions and 33 deletions
-6
View File
@@ -1,7 +1,6 @@
package lexer
import (
"errors"
"fmt"
"strings"
)
@@ -10,11 +9,6 @@ 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
+10 -1
View File
@@ -1,6 +1,11 @@
// 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)
@@ -11,7 +16,11 @@ func Run(fn StateFn) error {
var err error
fn, err = fn()
if err != nil {
switch {
case errors.Is(err, io.EOF):
// EOF
return nil
case err != nil:
// failed
return err
}
-26
View File
@@ -64,29 +64,3 @@ 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,
}
}