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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
package spec
import (
"strings"
"testing"
)
func TestParse(t *testing.T) {
production := func(lhs string, alts ...*AlternativeNode) *ProductionNode {
return &ProductionNode{
LHS: lhs,
RHS: alts,
}
}
withModifier := func(prod *ProductionNode, mod *ModifierNode) *ProductionNode {
prod.Modifier = mod
return prod
}
modifier := func(name string, param string) *ModifierNode {
return &ModifierNode{
Name: name,
Parameter: param,
}
}
alternative := func(elems ...*ElementNode) *AlternativeNode {
return &AlternativeNode{
Elements: elems,
}
}
withAction := func(alt *AlternativeNode, act *ActionNode) *AlternativeNode {
alt.Action = act
return alt
}
action := func(name string, param string) *ActionNode {
return &ActionNode{
Name: name,
Parameter: param,
}
}
id := func(id string) *ElementNode {
return &ElementNode{
ID: id,
}
}
pattern := func(p string) *ElementNode {
return &ElementNode{
Pattern: p,
}
}
tests := []struct {
caption string
src string
ast *RootNode
synErr *SyntaxError
}{
{
caption: "single production is a valid grammar",
src: `a: "a";`,
ast: &RootNode{
Productions: []*ProductionNode{
production("a", alternative(pattern("a"))),
},
},
},
{
caption: "multiple productions are a valid grammar",
src: `
e: e "\+|-" t | t;
t: t "\*|/" f | f;
f: "\(" e ")" | id;
id: "[A-Za-z_][0-9A-Za-z_]*";
`,
ast: &RootNode{
Productions: []*ProductionNode{
production("e",
alternative(id("e"), pattern(`\+|-`), id("t")),
alternative(id("t")),
),
production("t",
alternative(id("t"), pattern(`\*|/`), id("f")),
alternative(id("f")),
),
production("f",
alternative(pattern(`\(`), id("e"), pattern(`)`)),
alternative(id("id")),
),
production("id",
alternative(pattern(`[A-Za-z_][0-9A-Za-z_]*`)),
),
},
},
},
{
caption: "productions can contain the empty alternative",
src: `
a: "foo" | ;
b: | "bar";
c: ;
`,
ast: &RootNode{
Productions: []*ProductionNode{
production("a",
alternative(pattern(`foo`)),
alternative(),
),
production("b",
alternative(),
alternative(pattern(`bar`)),
),
production("c",
alternative(),
),
},
},
},
{
caption: "when a source contains an unknown token, the parser raises a syntax error",
src: `a: !;`,
synErr: synErrInvalidToken,
},
{
caption: "a grammar must have at least one production",
src: ``,
synErr: synErrNoProduction,
},
{
caption: "a production must have its name as the first element",
src: `: "a";`,
synErr: synErrNoProductionName,
},
{
caption: "':' must precede an alternative",
src: `a "a";`,
synErr: synErrNoColon,
},
{
caption: "';' must follow a production",
src: `a: "a"`,
synErr: synErrNoSemicolon,
},
{
caption: "';' can only appear at the end of a production",
src: `;`,
synErr: synErrNoProductionName,
},
{
caption: "a grammar can contain production modifiers and semantic actions",
src: `
mode_tran_seq
: mode_tran_seq mode_tran
| mode_tran
;
mode_tran
: push_m1
| push_m2
| pop_m1
| pop_m2
;
push_m1: "->" # push m1;
@mode m1
push_m2: "-->" # push m2;
@mode m1
pop_m1 : "<-" # pop;
@mode m2
pop_m2: "<--" # pop;
`,
ast: &RootNode{
Productions: []*ProductionNode{
production("mode_tran_seq",
alternative(id("mode_tran_seq"), id("mode_tran")),
alternative(id("mode_tran")),
),
production("mode_tran",
alternative(id("push_m1")),
alternative(id("push_m2")),
alternative(id("pop_m1")),
alternative(id("pop_m2")),
),
production("push_m1",
withAction(
alternative(pattern(`->`)),
action("push", "m1"),
),
),
withModifier(
production("push_m2",
withAction(
alternative(pattern(`-->`)),
action("push", "m2"),
),
),
modifier("mode", "m1"),
),
withModifier(
production("pop_m1",
withAction(
alternative(pattern(`<-`)),
action("pop", ""),
),
),
modifier("mode", "m1"),
),
withModifier(
production("pop_m2",
withAction(
alternative(pattern(`<--`)),
action("pop", ""),
),
),
modifier("mode", "m2"),
),
},
},
},
}
for _, tt := range tests {
t.Run(tt.caption, func(t *testing.T) {
ast, err := Parse(strings.NewReader(tt.src))
if tt.synErr != nil {
if tt.synErr != err {
t.Fatalf("unexpected error; want: %v, got: %v", tt.synErr, err)
}
if ast != nil {
t.Fatalf("AST must be nil")
}
} else {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if ast == nil {
t.Fatalf("AST must be non-nil")
}
testRootNode(t, ast, tt.ast)
}
})
}
}
func testRootNode(t *testing.T, root, expected *RootNode) {
t.Helper()
if len(root.Productions) != len(expected.Productions) {
t.Fatalf("unexpected length of productions; want: %v, got: %v", len(expected.Productions), len(root.Productions))
}
for i, prod := range root.Productions {
testProductionNode(t, prod, expected.Productions[i])
}
}
func testProductionNode(t *testing.T, prod, expected *ProductionNode) {
t.Helper()
if expected.Modifier == nil && prod.Modifier != nil {
t.Fatalf("unexpected modifier; want: nil, got: %+v", prod.Modifier)
}
if expected.Modifier != nil {
if prod.Modifier == nil {
t.Fatalf("a modifier is not set; want: %+v, got: nil", expected.Modifier)
}
if expected.Modifier.Name != prod.Modifier.Name || expected.Modifier.Parameter != prod.Modifier.Parameter {
t.Fatalf("unexpected modifier; want: %+v, got: %+v", expected.Modifier, prod.Modifier)
}
}
if prod.LHS != expected.LHS {
t.Fatalf("unexpected LHS; want: %v, got: %v", expected.LHS, prod.LHS)
}
if len(prod.RHS) != len(expected.RHS) {
t.Fatalf("unexpected length of an RHS; want: %v, got: %v", len(expected.RHS), len(prod.RHS))
}
for i, alt := range prod.RHS {
testAlternativeNode(t, alt, expected.RHS[i])
}
}
func testAlternativeNode(t *testing.T, alt, expected *AlternativeNode) {
t.Helper()
if len(alt.Elements) != len(expected.Elements) {
t.Fatalf("unexpected length of elements; want: %v, got: %v", len(expected.Elements), len(alt.Elements))
}
for i, elem := range alt.Elements {
testElementNode(t, elem, expected.Elements[i])
}
if expected.Action == nil && alt.Action != nil {
t.Fatalf("unexpected action; want: nil, got: %+v", alt.Action)
}
if expected.Action != nil {
if alt.Action == nil {
t.Fatalf("an action is not set; want: %+v, got: nil", expected.Action)
}
if expected.Action.Name != alt.Action.Name || expected.Action.Parameter != alt.Action.Parameter {
t.Fatalf("unexpected action; want: %+v, got: %+v", expected.Action, alt.Action)
}
}
}
func testElementNode(t *testing.T, elem, expected *ElementNode) {
t.Helper()
if elem.Pattern != expected.Pattern {
t.Fatalf("unexpected pattern; want: %v, got: %v", expected.Pattern, elem.Pattern)
}
}
|