go-parsekit/examples/example_helloParserCombinat...

66 lines
2.4 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 TokenHandler 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"
)
func Example_helloWorldUsingTokenizer() {
parser := createHelloTokenizer()
for i, input := range []string{
"Hello, world!",
"HELLO ,Johnny!",
"hello , Bob123!",
"hello Pizza!",
"Oh no!",
"Hello, world",
"Hello,!",
} {
output, err := parser.Execute(input)
if err != nil {
fmt.Printf("[%d] Input: %q Error: %s\n", i, input, err.Full())
} 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: unexpected character 'O' (expected a friendly greeting) at start of file
// [5] Input: "Hello, world" Error: unexpected character 'H' (expected a friendly greeting) at start of file
// [6] Input: "Hello,!" Error: unexpected character 'H' (expected a friendly greeting) at start of file
}
// ---------------------------------------------------------------------------
// Implementation of the parser
// ---------------------------------------------------------------------------
func createHelloTokenizer() *parsekit.Tokenizer {
// Easy access to parsekit definition.
c, a, m := parsekit.C, parsekit.A, parsekit.M
// Using the parser/combinator support of parsekit, we create a TokenHandler function
// that does all the work. The 'greeting' TokenHandler matches the whole input and
// drops all but the name from it.
hello := a.StrNoCase("hello")
comma := c.Seq(c.Opt(a.Whitespace), a.Comma, c.Opt(a.Whitespace))
separator := c.Any(comma, a.Whitespace)
name := c.OneOrMore(c.Not(a.Excl))
greeting := c.Seq(m.Drop(hello), m.Drop(separator), name, m.Drop(a.Excl), a.EndOfFile)
// Create a Tokenizer that wraps the 'greeting' TokenHandler and allows
// us to match some input against that handler.
return parsekit.NewTokenizer(greeting, "a friendly greeting")
}