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 *TokenHandlerResult } // 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. func NewTokenizer(tokenHandler TokenHandler) *Tokenizer { tokenizer := &Tokenizer{} tokenizer.parser = NewParser(func(p *ParseAPI) { if p.On(tokenHandler).Accept() { tokenizer.result = p.Result() p.Stop() } else { p.UnexpectedInput("") } }) return tokenizer } // Execute feeds the input to the wrapped TokenHandler function. // It returns the TokenHandler's TokenHandlerResult. When an error occurred // during parsing, the error will be set, nil otherwise. func (t *Tokenizer) Execute(input string) (*TokenHandlerResult, *Error) { err := t.parser.Execute(input) return t.result, err }