35 lines
965 B
Go
35 lines
965 B
Go
// In this example, we show that any type can be extended into a parser,
|
|
// filling that type with data from the ParseHandler methods.
|
|
//
|
|
// Here, we create a custom type 'letterCollection', which is an alias
|
|
// for []string. We add a ParseHandler method directly to that type
|
|
// and let the parsing code fill the slice with strings during parsing.
|
|
|
|
package parsekit_test
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.makaay.nl/mauricem/go-parsekit"
|
|
)
|
|
|
|
type letterCollection []string
|
|
|
|
func (l *letterCollection) parseStart(p *parsekit.ParseAPI) {
|
|
for p.On(parsekit.C.MinMax(1, 3, parsekit.A.AnyRune)).Accept() {
|
|
*l = append(*l, p.BufLiteral())
|
|
p.BufClear()
|
|
}
|
|
p.ExpectEndOfFile()
|
|
}
|
|
|
|
func Example_usingSliceAsParserState() {
|
|
letters := &letterCollection{}
|
|
parser := parsekit.NewParser(letters.parseStart)
|
|
err := parser.Execute("¡Any will dö!")
|
|
|
|
fmt.Printf("Matches = %q, Error = %s\n", *letters, err)
|
|
// Output:
|
|
// Matches = ["¡An" "y w" "ill" " dö" "!"], Error = <nil>
|
|
}
|