Browse Source

lexer: introduce Lexer wrapper for io.RuneScanner

Signed-off-by: Alejandro Mery <[email protected]>
Alejandro Mery 2 years ago
parent
commit
0120cf0cc6
  1. 29
      lexer/lexer.go

29
lexer/lexer.go

@ -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)

Loading…
Cancel
Save