lexer: introduce Lexer wrapper for io.RuneScanner

Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
2023-08-28 23:58:43 +00:00
parent 48b75ec3c3
commit 0120cf0cc6
+29
View File
@@ -3,6 +3,35 @@ package lexer
import "io"
// Lexer adds Accept and AcceptAll support to a
// [io.RuneScanner]
type Lexer struct {
io.RuneScanner
}
// Accept consumes a rune from the source if it meets the condition.
// it returns true if the condition was met and false if it wasn't.
func (p Lexer) Accept(cond func(rune) bool) bool {
return Accept(p, cond)
}
// AcceptAll consumes runes from the source as long as they meet the
// condition. it returns true if the condition was met for at least one rune,
// and false if it wasn't.
func (p Lexer) AcceptAll(cond func(rune) bool) bool {
return AcceptAll(p, cond)
}
// NewLexer extends a [io.RuneScanner] with Accept()/AcceptAll()
// functionality
func NewLexer(src io.RuneScanner) *Lexer {
if src == nil {
return nil
}
return &Lexer{src}
}
// StateFn is a State Function of the parser
type StateFn func() (StateFn, error)