go-parsekit/examples/example_helloParserCombinat...

70 lines
2.5 KiB
Go

// In this example, a parser is created that is able to parse input that looks
// like "Hello, <name>!", and that extracts the name from it.
//
// The implementation uses only parser/combinator Handler functions and does
// not implement a full-fledged state-based Parser for it. If you want to see the
// same kind of functionality, implementated using a Parser, take a look at the
// other hello examples.
package examples
import (
"fmt"
"git.makaay.nl/mauricem/go-parsekit/tokenize"
)
func Example_helloWorldUsingTokenizer() {
tokenizer := createHelloTokenizer()
for i, input := range []string{
"Hello, world!",
"HELLO ,Johnny!",
"hello , Bob123!",
"hello Pizza!",
"Oh no!",
"Hello, world",
"Hello,!",
} {
output, err := tokenizer(input)
if err != nil {
fmt.Printf("[%d] Input: %q Error: %s\n", i, input, err)
} else {
fmt.Printf("[%d] Input: %q Output: %s\n", i, input, output)
}
}
// Output:
// [0] Input: "Hello, world!" Output: world
// [1] Input: "HELLO ,Johnny!" Output: Johnny
// [2] Input: "hello , Bob123!" Output: Bob123
// [3] Input: "hello Pizza!" Output: Pizza
// [4] Input: "Oh no!" Error: mismatch at start of file
// [5] Input: "Hello, world" Error: mismatch at start of file
// [6] Input: "Hello,!" Error: mismatch at start of file
}
// ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
// Implementation of the parser
// ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
func createHelloTokenizer() tokenize.Func {
// Easy access to parsekit definition.
c, a, m := tokenize.C, tokenize.A, tokenize.M
// Using the parser/combinator support of parsekit, we create a Handler function
// that does all the work. The 'greeting' Handler matches the whole input and
// drops all but the name from it.
hello := a.StrNoCase("hello")
comma := c.Seq(c.Optional(a.Blanks), a.Comma, c.Optional(a.Blanks))
separator := c.Any(comma, a.Blanks)
name := c.OneOrMore(c.Not(a.Excl))
greeting := m.Drop(hello).
Then(m.Drop(separator)).
Then(name).
Then(m.Drop(a.Excl)).
Then(a.EndOfFile)
// Create a Tokenizer that wraps the 'greeting' Handler and allows
// us to match some input against that handler.
return tokenize.New(greeting)
}