Compare commits
12 Commits
ff03ee922d
..
v0.3.5
| Author | SHA1 | Date | |
|---|---|---|---|
| f8f6ff9e11 | |||
| 05d504346e | |||
| 93c58cdc26 | |||
| 9425ba0f7c | |||
| 5a7b19bb06 | |||
| 6c189fd87d | |||
| 1d62857e14 | |||
| f7e13e0978 | |||
| f67d8a2443 | |||
| d83b128c30 | |||
| 76e6146e9e | |||
| f79e2bee9e |
+17
-3
@@ -1,17 +1,31 @@
|
||||
// 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)
|
||||
|
||||
// Run runs a state machine until the state function either
|
||||
// returns nil or an error
|
||||
func Run(fn StateFn) error {
|
||||
var err error
|
||||
for fn != nil {
|
||||
var err error
|
||||
|
||||
for fn != nil && err == nil {
|
||||
fn, err = fn()
|
||||
switch {
|
||||
case errors.Is(err, io.EOF):
|
||||
// EOF
|
||||
return nil
|
||||
case err != nil:
|
||||
// failed
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
// ended
|
||||
return nil
|
||||
}
|
||||
|
||||
+11
-13
@@ -41,28 +41,26 @@ func (p *Position) Step() {
|
||||
p.Column++
|
||||
}
|
||||
|
||||
// Next returns a new Position one rune forward
|
||||
// on the line
|
||||
func (p Position) Next() Position {
|
||||
// StepN moves the column N places forward
|
||||
func (p *Position) StepN(n int) {
|
||||
if p.Line == 0 {
|
||||
p.Reset()
|
||||
}
|
||||
|
||||
return Position{
|
||||
Line: p.Line,
|
||||
Column: p.Column + 1,
|
||||
switch {
|
||||
case n > 0:
|
||||
p.Column += n
|
||||
default:
|
||||
panic(fmt.Errorf("invalid %v increment", n))
|
||||
}
|
||||
}
|
||||
|
||||
// NextLine returns a new Position at the begining of the next
|
||||
// line.
|
||||
func (p Position) NextLine() Position {
|
||||
// StepLine moves position to the start of the next line
|
||||
func (p *Position) StepLine() {
|
||||
if p.Line == 0 {
|
||||
p.Reset()
|
||||
}
|
||||
|
||||
return Position{
|
||||
Line: p.Line + 1,
|
||||
Column: 1,
|
||||
}
|
||||
p.Line++
|
||||
p.Column = 1
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user