go-parsekit/examples/example_helloManyStateParse...

148 lines
4.1 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.
//
// This implementation uses a state-based Parser for it, and it does not
// implement any custom parser/combinator TokenHandler functions. Note that
// things are much easier to implement using custom TokenHandlers (see the
// helloParserCombinator example for this). Doing this fully parser-based
// implementation is mainly for your learning pleasure.
//
// One big difference between the parser/combinator-based example and this one,
// is that this parser reports errors much more fine-grained. This might or
// might not be useful for your specific use case. If you need error reporting
// like this, then also take a look at the helloSingleState example, which does
// the same thing as this version, only more concise.
package examples
import (
"fmt"
"strings"
"git.makaay.nl/mauricem/go-parsekit"
)
func Example_helloWorldUsingParser1() {
for i, input := range []string{
"Hello, world!",
"HELLO ,Johnny!",
"hello , Bob123!",
"hello Pizza!",
"",
" ",
"hello",
"hello,",
"hello , ",
"hello , Droopy",
"hello , Droopy!",
"hello , \t \t Droopy \t !",
"Oh no!",
"hello,!",
} {
name, err := (&helloparser1{}).Parse(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, name)
}
}
// Output:
// [0] Input: "Hello, world!" Output: world
// [1] Input: "HELLO ,Johnny!" Output: Johnny
// [2] Input: "hello , Bob123!" Output: Bob123
// [3] Input: "hello Pizza!" Error: unexpected character 'P' (expected comma)
// [4] Input: "" Error: unexpected end of file (expected hello)
// [5] Input: " " Error: unexpected character ' ' (expected hello)
// [6] Input: "hello" Error: unexpected end of file (expected comma)
// [7] Input: "hello," Error: unexpected end of file (expected name)
// [8] Input: "hello , " Error: unexpected end of file (expected name)
// [9] Input: "hello , Droopy" Error: unexpected end of file (expected exclamation)
// [10] Input: "hello , Droopy!" Output: Droopy
// [11] Input: "hello , \t \t Droopy \t !" Output: Droopy
// [12] Input: "Oh no!" Error: unexpected character 'O' (expected hello)
// [13] Input: "hello,!" Error: unexpected character '!' (expected name)
}
// ---------------------------------------------------------------------------
// Implementation of the parser
// ---------------------------------------------------------------------------
type helloparser1 struct {
greetee string
}
func (h *helloparser1) Parse(input string) (string, *parsekit.Error) {
parser := parsekit.NewParser(h.start)
err := parser.Execute(input)
return h.greetee, err
}
func (h *helloparser1) start(p *parsekit.ParseAPI) {
a := parsekit.A
p.Expects("hello")
if p.On(a.StrNoCase("hello")).Skip() {
p.Handle(h.comma)
}
}
func (h *helloparser1) comma(p *parsekit.ParseAPI) {
a := parsekit.A
p.Expects("comma")
switch {
case p.On(a.Whitespace).Skip():
p.Handle(h.comma)
case p.On(a.Comma).Skip():
p.Handle(h.startName)
}
}
func (h *helloparser1) startName(p *parsekit.ParseAPI) {
c, a := parsekit.C, parsekit.A
p.Expects("name")
switch {
case p.On(a.Whitespace).Skip():
p.Handle(h.startName)
case p.On(c.Not(a.Excl)).Stay():
p.Handle(h.name)
}
}
func (h *helloparser1) name(p *parsekit.ParseAPI) {
c, a := parsekit.C, parsekit.A
p.Expects("name")
switch {
case p.On(c.Not(a.Excl)).Accept():
h.greetee += p.Result().String()
p.Handle(h.name)
default:
p.Handle(h.exclamation)
}
}
func (h *helloparser1) exclamation(p *parsekit.ParseAPI) {
a := parsekit.A
p.Expects("exclamation")
if p.On(a.Excl).Accept() {
p.Handle(h.end)
}
}
// Here we could have used p.ExpectEndOfFile() as well, but a slightly
// different route was taken to implement a more friendly 'end of greeting'
// error message.
func (h *helloparser1) end(p *parsekit.ParseAPI) {
var a = parsekit.A
if !p.On(a.EndOfFile).Stay() {
p.Expects("end of greeting")
p.UnexpectedInput()
return
}
h.greetee = strings.TrimSpace(h.greetee)
if h.greetee == "" {
p.Error("The name cannot be empty")
} else {
p.Stop()
}
}