-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
184 lines (148 loc) · 2.96 KB
/
parser_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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package bnf_test
import (
"fmt"
"os"
"strings"
"testing"
"github.com/ofabricio/bnf"
)
func Example_pos() {
INP := `One Two Three`
BNF := `
root = 'One' WS 'Two' WS 'Three'
`
b := bnf.Compile(BNF)
v := bnf.Parse(b, INP)
for _, v := range v.Next {
fmt.Println(v.Text, v.Pos)
}
// Output:
// One 0
// Two 4
// Three 8
}
func ExampleFlatten() {
INP := `abcd`
BNF := `
root = GROUP( 'a' GROUP( 'b' GROUP( 'c' GROUP( 'd' ) ) ) )
`
b := bnf.Compile(BNF)
v := bnf.Parse(b, INP)
fmt.Println("AST:")
bnf.Print(v)
fmt.Println("FLATTEN 0:")
bnf.Print(bnf.AST{Type: "Flatten", Next: bnf.Flatten(v, 0)})
fmt.Println("FLATTEN 1:")
bnf.Print(bnf.AST{Type: "Flatten", Next: bnf.Flatten(v, 1)})
fmt.Println("FLATTEN 2:")
bnf.Print(bnf.AST{Type: "Flatten", Next: bnf.Flatten(v, 2)})
fmt.Println("FLATTEN 3:")
bnf.Print(bnf.AST{Type: "Flatten", Next: bnf.Flatten(v, 3)})
// Output:
// AST:
// [Group]
// [Ident] a
// [Group]
// [Ident] b
// [Group]
// [Ident] c
// [Group]
// [Ident] d
// FLATTEN 0:
// [Flatten]
// [Ident] a
// [Ident] b
// [Ident] c
// [Ident] d
// FLATTEN 1:
// [Flatten]
// [Ident] a
// [Ident] b
// [Group]
// [Ident] c
// [Group]
// [Ident] d
// FLATTEN 2:
// [Flatten]
// [Ident] a
// [Ident] b
// [Ident] c
// [Group]
// [Ident] d
// FLATTEN 3:
// [Flatten]
// [Ident] a
// [Ident] b
// [Ident] c
// [Ident] d
}
func ExampleParse_testDataBNF() {
// This tests guarantees that the testdata parsing is correct.
INP := `
[Test]
One
[Give]
Two
[When]
Three
[Then]
Four
[Test]
Five
[Give]
Six
[When]
Seven
[Then]
Eight
`
b := bnf.Compile(testDataBNF)
v := bnf.Parse(b, INP)
bnf.Print(v)
// Output:
// [Group]
// [Group]
// [Ident] One
// [Ident] Two
// [Ident] Three
// [Ident] Four
// [Group]
// [Ident] Five
// [Ident] Six
// [Ident] Seven
// [Ident] Eight
}
func TestParser(t *testing.T) {
INP, err := os.ReadFile("testdata/parser.tests")
if err != nil {
t.Fatal(err)
}
b := bnf.Compile(testDataBNF)
v := bnf.Parse(b, string(INP))
if len(v.Next) == 0 {
t.Fatal("No tests found")
}
var out strings.Builder
out.Grow(512)
for _, n := range v.Next {
desc := n.Next[0].Text
give := n.Next[1].Text
when := n.Next[2].Text
then := n.Next[3].Text
b := bnf.Compile(when)
v := bnf.Parse(b, give)
bnf.Write(&out, v)
got := strings.TrimSpace(out.String())
exp := strings.TrimSpace(then)
if got != exp {
t.Errorf("\n[Test]\n%s\n[Give]\n%s\n[When]\n%s\n[Got]\n%s\n[Exp]\n%s\n", desc, give, when, got, exp)
}
out.Reset()
}
}
var testDataBNF = `
root = GROUP(section section section section)+
section = ws tag '\n'ri MATCH( ANYNOT(ws (tag | EOF))+ )
tag = '[Test]'i | '[Give]'i | '[When]'i | '[Then]'i
ws = '\s*'ri
`