forked from losinggeneration/pego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_test.go
44 lines (38 loc) · 771 Bytes
/
match_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package pego
import (
"fmt"
"testing"
)
func TestSimpleMatch(t *testing.T) {
pat := Grm("S", map[string]*Pattern{
"S": Ref("A").Clist(),
"A": Seq(
NegSet("()").Rep(0, -1),
Seq(
Ref("B"),
NegSet("()").Rep(0, -1),
).Rep(0, -1)).Csimple(),
"B": Seq(
"(", Ref("A"), ")"),
})
fmt.Println("Compiled pattern:")
fmt.Println(pat)
tests := []string{
"x", "(x)", "a(b(c)d(e)f)g", ")",
}
for _, s := range tests {
fmt.Printf("\n\n=== MATCHING %q ===\n", s)
fmt.Println("Trace:")
r, err, pos := Match(pat, s)
if r != nil {
fmt.Printf("Return value: %v\n", r)
}
if err != nil {
t.Errorf("Error: %#v\n", err)
}
fmt.Printf("End position: %d\n", pos)
if pos != len(s) {
t.Error("Failed to match whole input")
}
}
}