Compare commits
3 Commits
ef0e093aaa
...
4d9b7dfa55
| Author | SHA1 | Date | |
|---|---|---|---|
| 4d9b7dfa55 | |||
| 385a4319d1 | |||
| 05d504346e |
+10
-1
@@ -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
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user