package tokenize_test import ( "fmt" "git.makaay.nl/mauricem/go-parsekit/tokenize" ) func ExampleAPI_Fork() { // This custom Handler checks for input 'a', 'b' or 'c'. abcHandler := func(t *tokenize.API) bool { a := tokenize.A for _, r := range []rune{'a', 'b', 'c'} { child := t.Fork() // fork, so we won't change parent t if a.Rune(r)(child) { child.Merge() // accept results into parent t return true // and report a successful match } } // If we get here, then no match was found. Return false to communicate // this to the caller. return false } // Note: a custom Handler is normally not what you need. // You can make use of the parser/combinator tooling to make the // implementation a lot simpler and to take care of forking at // the appropriate places. The handler from above can be replaced with: simpler := tokenize.A.RuneRange('a', 'c') result, err := tokenize.New(abcHandler)("another test") fmt.Println(result, err) result, err = tokenize.New(simpler)("curious") fmt.Println(result, err) result, err = tokenize.New(abcHandler)("bang on!") fmt.Println(result, err) result, err = tokenize.New(abcHandler)("not a match") fmt.Println(result, err) // Output: // a // c // b // mismatch at start of file } func ExampleAPI_Merge() { tokenHandler := func(t *tokenize.API) bool { child1 := t.Fork() child1.NextRune() // reads 'H' child1.Accept() child1.NextRune() // reads 'i' child1.Accept() child2 := child1.Fork() child2.NextRune() // reads ' ' child2.Accept() child2.NextRune() // reads 'd' child2.Accept() child1.Merge() // We merge child1, which has read 'H' and 'i' only. return true } result, _ := tokenize.New(tokenHandler)("Hi mister X!") fmt.Println(result) // Output: // Hi }