lexer: introduce Lexer wrapper for io.RuneScanner
Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
@@ -3,6 +3,35 @@ package lexer
|
|||||||
|
|
||||||
import "io"
|
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
|
// StateFn is a State Function of the parser
|
||||||
type StateFn func() (StateFn, error)
|
type StateFn func() (StateFn, error)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user