123 lines
2.7 KiB
Go
123 lines
2.7 KiB
Go
package parsekit_test
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.makaay.nl/mauricem/go-parsekit"
|
|
)
|
|
|
|
func ExampleItemType() {
|
|
// Make use of positive values. Ideally, define your ItemTypes using
|
|
// iota for easy automatic value management like this:
|
|
const (
|
|
ItemWord parsekit.ItemType = iota
|
|
ItemNumber
|
|
ItemBlob
|
|
// ...
|
|
)
|
|
}
|
|
|
|
func ExampleItem() {
|
|
// Easy access to the parsekit definitions.
|
|
c := parsekit.C
|
|
|
|
// You define your own item types for your specific parser.
|
|
const QuestionItem = parsekit.ItemType(42)
|
|
|
|
// A ParseHandler function can use the defined item type by means of
|
|
// the p.Emit* methods on parsekit.P.
|
|
// When errors occur, or the end of the file is reached, then the built-in
|
|
// types parsekit.ItemEOF and parsekit.ItemError will be emitted by parsekit.
|
|
stateHandler := func(p *parsekit.ParseAPI) {
|
|
if p.On(c.Str("question")).Accept() {
|
|
p.EmitLiteral(QuestionItem)
|
|
}
|
|
p.ExpectEndOfFile()
|
|
}
|
|
|
|
// Successful match
|
|
item, _, ok := parsekit.NewParser(stateHandler).Parse("question").Next()
|
|
fmt.Println(ok, item.Type == QuestionItem, item.Value)
|
|
|
|
// End of file reached
|
|
item, _, ok = parsekit.NewParser(stateHandler).Parse("").Next()
|
|
fmt.Println(ok, item.Type == parsekit.ItemEOF)
|
|
|
|
// An error occurred
|
|
item, err, ok := parsekit.NewParser(stateHandler).Parse("answer").Next()
|
|
fmt.Println(ok, item.Type == parsekit.ItemError, err)
|
|
|
|
// Output:
|
|
// true true question
|
|
// false true
|
|
// false true unexpected character 'a' (expected end of file)
|
|
}
|
|
|
|
func ExampleError() {
|
|
err := &parsekit.Error{
|
|
Message: "it broke down",
|
|
Line: 10,
|
|
Column: 42,
|
|
}
|
|
|
|
fmt.Println(err.Error())
|
|
fmt.Printf("%s\n", err)
|
|
fmt.Println(err.ErrorFull())
|
|
// Output:
|
|
// it broke down
|
|
// it broke down
|
|
// it broke down after line 10, column 42
|
|
}
|
|
|
|
func ExampleError_Error() {
|
|
err := &parsekit.Error{
|
|
Message: "it broke down",
|
|
Line: 10,
|
|
Column: 42,
|
|
}
|
|
|
|
fmt.Println(err.Error())
|
|
fmt.Printf("%s\n", err)
|
|
// Output:
|
|
// it broke down
|
|
// it broke down
|
|
}
|
|
|
|
func ExampleError_ErrorFull() {
|
|
err := &parsekit.Error{
|
|
Message: "it broke down",
|
|
Line: 10,
|
|
Column: 42,
|
|
}
|
|
|
|
fmt.Println(err.ErrorFull())
|
|
// Output:
|
|
// it broke down after line 10, column 42
|
|
}
|
|
|
|
func ExampleMatchAnyRune() {
|
|
// Easy access to the parsekit definitions.
|
|
a := parsekit.A
|
|
|
|
stateHandler := func(p *parsekit.ParseAPI) {
|
|
p.Expects("Any valid rune")
|
|
if p.On(a.AnyRune).Accept() {
|
|
p.EmitLiteral(TestItem)
|
|
p.RouteRepeat()
|
|
}
|
|
}
|
|
parser := parsekit.NewParser(stateHandler)
|
|
run := parser.Parse("¡Any / valid / character will dö!")
|
|
|
|
for i := 0; i < 5; i++ {
|
|
match, _, _ := run.Next()
|
|
fmt.Printf("Match = %q\n", match.Value)
|
|
}
|
|
// Output:
|
|
// Match = "¡"
|
|
// Match = "A"
|
|
// Match = "n"
|
|
// Match = "y"
|
|
// Match = " "
|
|
}
|