Browse Source

lexer: introduce StateFn and the basic state machine loop

v2: make the error break more explicit

Signed-off-by: Alejandro Mery <amery@jpi.io>
pull/5/head
Alejandro Mery 1 year ago
parent
commit
1d62857e14
  1. 20
      lexer/lexer.go

20
lexer/lexer.go

@ -1,2 +1,22 @@
// Package lexer provides basic helpers to implement parsers
package lexer
// 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 {
for fn != nil {
var err error
fn, err = fn()
if err != nil {
// failed
return err
}
}
// ended
return nil
}

Loading…
Cancel
Save