50 lines
1.8 KiB
Go
50 lines
1.8 KiB
Go
package parsekit
|
|
|
|
// Tokenizer is the top-level struct that holds the configuration for
|
|
// a parser that is based solely on a TokenHandler function.
|
|
// The Tokenizer can be instantiated using the parsekit.NewTokenizer()
|
|
// method.
|
|
type Tokenizer struct {
|
|
parser *Parser
|
|
result *TokenResult
|
|
}
|
|
|
|
// TokenHandler is the function type that is involved in turning a low level
|
|
// stream of UTF8 runes into lexical tokens. Its purpose is to check if input
|
|
// data matches some kind of pattern and to report back the results.
|
|
//
|
|
// A TokenHandler function gets a TokenAPI as its input and returns a boolean to
|
|
// indicate whether or not it found a match on the input. The TokenAPI is used
|
|
// for retrieving input data to match against and for reporting back results.
|
|
type TokenHandler func(t *TokenAPI) bool
|
|
|
|
// NewTokenizer instantiates a new Tokenizer.
|
|
//
|
|
// This is a simple wrapper around a TokenHandler function. It can be used to
|
|
// match an input string against that TokenHandler function and retrieve the
|
|
// results in a straight forward way.
|
|
//
|
|
// The 'expects' parameter is used for creating an error message in case parsed
|
|
// input does not match the TokenHandler.
|
|
func NewTokenizer(tokenHandler TokenHandler, expects string) *Tokenizer {
|
|
tokenizer := &Tokenizer{}
|
|
tokenizer.parser = NewParser(func(p *ParseAPI) {
|
|
if p.On(tokenHandler).Accept() {
|
|
tokenizer.result = p.Result()
|
|
p.Stop()
|
|
} else {
|
|
p.Expects(expects)
|
|
p.UnexpectedInput()
|
|
}
|
|
})
|
|
return tokenizer
|
|
}
|
|
|
|
// Execute feeds the input to the wrapped TokenHandler function.
|
|
// It returns the TokenHandler's TokenResult. When an error occurred
|
|
// during parsing, the error will be set, nil otherwise.
|
|
func (t *Tokenizer) Execute(input string) (*TokenResult, *Error) {
|
|
err := t.parser.Execute(input)
|
|
return t.result, err
|
|
}
|