lexer: create Lexer factories for io.Reader, []byte and string

Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
2023-08-29 00:09:28 +00:00
parent 172ffbe4f8
commit e92b117908
+24 -1
View File
@@ -1,7 +1,11 @@
// Package lexer provides basic helpers to implement parsers
package lexer
import "io"
import (
"io"
"asciigoat.org/core/runes"
)
// Lexer adds Accept and AcceptAll support to a
// [io.RuneScanner]
@@ -32,6 +36,25 @@ func NewLexer(src io.RuneScanner) *Lexer {
return &Lexer{src}
}
// NewLexerReader creates a Lexer from an [io.Reader]
func NewLexerReader(r io.Reader) *Lexer {
if r == nil {
return nil
}
return &Lexer{runes.NewReader(r)}
}
// NewLexerBytes creates a Lexer from raw bytes
func NewLexerBytes(b []byte) *Lexer {
return &Lexer{runes.NewReaderBytes(b)}
}
// NewLexerString creates a Lexer from a data string
func NewLexerString(s string) *Lexer {
return &Lexer{runes.NewReaderString(s)}
}
// StateFn is a State Function of the parser
type StateFn func() (StateFn, error)