From 15938090f6e3c1da944712a95b8bed370eeed4c4 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 28 Aug 2023 22:14:45 +0000 Subject: [PATCH] lexer: introduce StateFn and the basic state machine loop Signed-off-by: Alejandro Mery --- lexer/lexer.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lexer/lexer.go b/lexer/lexer.go index 33b17b6..26885ba 100644 --- a/lexer/lexer.go +++ b/lexer/lexer.go @@ -1,2 +1,17 @@ // 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 { + var err error + + for fn != nil && err == nil { + fn, err = fn() + } + + return err +}