-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.go
334 lines (292 loc) · 7.92 KB
/
check.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2017 The Peggy Authors
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd.
package main
import (
"sort"
)
// Check does semantic analysis of the rules,
// setting bookkeeping needed to later generate the parser,
// returning any errors encountered in order of their begin location.
func Check(grammar *Grammar) error {
var errs Errors
rules := expandTemplates(grammar.Rules, &errs)
ruleMap := make(map[string]*Rule, len(rules))
for i, r := range rules {
r.N = i
name := r.Name.String()
if other := ruleMap[name]; other != nil {
errs.add(r, "rule %s redefined", name)
}
ruleMap[name] = r
}
var p path
for _, r := range rules {
r.checkLeft(ruleMap, p, &errs)
}
for _, r := range rules {
check(r, ruleMap, &errs)
}
if err := errs.ret(); err != nil {
return err
}
grammar.CheckedRules = rules
return nil
}
func expandTemplates(ruleDefs []Rule, errs *Errors) []*Rule {
var expanded, todo []*Rule
tmplNames := make(map[string]*Rule)
for i := range ruleDefs {
r := &ruleDefs[i]
if len(r.Name.Args) > 0 {
seenParams := make(map[string]bool)
for _, param := range r.Name.Args {
n := param.String()
if seenParams[n] {
errs.add(param, "parameter %s redefined", n)
}
seenParams[n] = true
}
tmplNames[r.Name.Name.String()] = r
} else {
expanded = append(expanded, r)
todo = append(todo, r)
}
}
seen := make(map[string]bool)
for i := 0; i < len(todo); i++ {
for _, invok := range invokedTemplates(todo[i]) {
if seen[invok.Name.String()] {
continue
}
seen[invok.Name.String()] = true
tmpl := tmplNames[invok.Name.Name.String()]
if tmpl == nil {
continue // undefined template, error reported elsewhere
}
exp := expand1(tmpl, invok, errs)
if exp == nil {
continue // error expanding, error reported elsewhere
}
todo = append(todo, exp)
expanded = append(expanded, exp)
}
}
return expanded
}
func expand1(tmpl *Rule, invok *Ident, errs *Errors) *Rule {
if len(invok.Args) != len(tmpl.Args) {
errs.add(invok, "template %s argument count mismatch: got %d, expected %d",
tmpl.Name, len(invok.Args), len(tmpl.Args))
return nil
}
copy := *tmpl
sub := make(map[string]string, len(tmpl.Args))
for i, arg := range invok.Args {
sub[tmpl.Args[i].String()] = arg.String()
}
copy.Args = invok.Args
copy.Expr = tmpl.Expr.substitute(sub)
return ©
}
func invokedTemplates(r *Rule) []*Ident {
var tmpls []*Ident
r.Expr.Walk(func(e Expr) bool {
if id, ok := e.(*Ident); ok {
if len(id.Args) > 0 {
tmpls = append(tmpls, id)
}
}
return true
})
return tmpls
}
type path struct {
stack []*Rule
seen map[*Rule]bool
}
func (p *path) push(r *Rule) bool {
if p.seen == nil {
p.seen = make(map[*Rule]bool)
}
if p.seen[r] {
return false
}
p.stack = append(p.stack, r)
p.seen[r] = true
return true
}
func (p *path) pop() {
p.stack = p.stack[:len(p.stack)]
}
func (p *path) cycle(r *Rule) []*Rule {
for i := len(p.stack) - 1; i >= 0; i-- {
if p.stack[i] == r {
return append(p.stack[i:], r)
}
}
panic("no cycle")
}
func cycleString(rules []*Rule) string {
var s string
for _, r := range rules {
if s != "" {
s += ", "
}
s += r.Name.String()
}
return s
}
func (r *Rule) checkLeft(rules map[string]*Rule, p path, errs *Errors) {
if r.typ != nil {
return
}
if !p.push(r) {
cycle := p.cycle(r)
errs.add(cycle[0], "left-recursion: %s", cycleString(cycle))
for _, r := range cycle {
r.typ = new(string)
}
return
}
r.Expr.checkLeft(rules, p, errs)
t := r.Expr.Type()
r.typ = &t
r.epsilon = r.Expr.epsilon()
p.pop()
}
func (e *Choice) checkLeft(rules map[string]*Rule, p path, errs *Errors) {
for _, sub := range e.Exprs {
sub.checkLeft(rules, p, errs)
}
}
func (e *Action) checkLeft(rules map[string]*Rule, p path, errs *Errors) {
e.Expr.checkLeft(rules, p, errs)
}
func (e *Sequence) checkLeft(rules map[string]*Rule, p path, errs *Errors) {
for _, sub := range e.Exprs {
sub.checkLeft(rules, p, errs)
if !sub.epsilon() {
break
}
}
}
func (e *LabelExpr) checkLeft(rules map[string]*Rule, p path, errs *Errors) {
e.Expr.checkLeft(rules, p, errs)
}
func (e *PredExpr) checkLeft(rules map[string]*Rule, p path, errs *Errors) {
e.Expr.checkLeft(rules, p, errs)
}
func (e *RepExpr) checkLeft(rules map[string]*Rule, p path, errs *Errors) {
e.Expr.checkLeft(rules, p, errs)
}
func (e *OptExpr) checkLeft(rules map[string]*Rule, p path, errs *Errors) {
e.Expr.checkLeft(rules, p, errs)
}
func (e *Ident) checkLeft(rules map[string]*Rule, p path, errs *Errors) {
if e.rule = rules[e.Name.String()]; e.rule != nil {
e.rule.checkLeft(rules, p, errs)
}
}
func (e *SubExpr) checkLeft(rules map[string]*Rule, p path, errs *Errors) {
e.Expr.checkLeft(rules, p, errs)
}
func (e *PredCode) checkLeft(rules map[string]*Rule, p path, errs *Errors) {}
func (e *Literal) checkLeft(rules map[string]*Rule, p path, errs *Errors) {}
func (e *CharClass) checkLeft(rules map[string]*Rule, p path, errs *Errors) {}
func (e *Any) checkLeft(rules map[string]*Rule, p path, errs *Errors) {}
type ctx struct {
rules map[string]*Rule
allLabels *[]*LabelExpr
curLabels map[string]*LabelExpr
}
func check(rule *Rule, rules map[string]*Rule, errs *Errors) {
ctx := ctx{
rules: rules,
allLabels: &rule.Labels,
curLabels: make(map[string]*LabelExpr),
}
rule.Expr.check(ctx, true, errs)
sort.Slice(rule.Labels, func(i, j int) bool {
return rule.Labels[i].N < rule.Labels[j].N
})
}
func (e *Choice) check(ctx ctx, valueUsed bool, errs *Errors) {
for _, sub := range e.Exprs {
subCtx := ctx
subCtx.curLabels = make(map[string]*LabelExpr)
for n, l := range ctx.curLabels {
subCtx.curLabels[n] = l
}
sub.check(subCtx, valueUsed, errs)
}
t := e.Exprs[0].Type()
for _, sub := range e.Exprs {
if got := sub.Type(); *genActions && valueUsed && got != t && got != "" && t != "" {
errs.add(sub, "type mismatch: got %s, expected %s", got, t)
}
}
}
func (e *Action) check(ctx ctx, valueUsed bool, errs *Errors) {
e.Expr.check(ctx, false, errs)
for _, l := range ctx.curLabels {
e.Labels = append(e.Labels, l)
}
sort.Slice(e.Labels, func(i, j int) bool {
return e.Labels[i].Label.String() < e.Labels[j].Label.String()
})
}
// BUG: figure out what to do about sequence types.
func (e *Sequence) check(ctx ctx, valueUsed bool, errs *Errors) {
for _, sub := range e.Exprs {
sub.check(ctx, valueUsed, errs)
}
t := e.Exprs[0].Type()
for _, sub := range e.Exprs {
if got := sub.Type(); *genActions && valueUsed && got != t && got != "" && t != "" {
errs.add(sub, "type mismatch: got %s, expected %s", got, t)
}
}
}
func (e *LabelExpr) check(ctx ctx, valueUsed bool, errs *Errors) {
e.Expr.check(ctx, true, errs)
if _, ok := ctx.curLabels[e.Label.String()]; ok {
errs.add(e.Label, "label %s redefined", e.Label.String())
}
e.N = len(*ctx.allLabels)
*ctx.allLabels = append(*ctx.allLabels, e)
ctx.curLabels[e.Label.String()] = e
}
func (e *PredExpr) check(ctx ctx, valueUsed bool, errs *Errors) {
e.Expr.check(ctx, false, errs)
}
func (e *RepExpr) check(ctx ctx, valueUsed bool, errs *Errors) {
e.Expr.check(ctx, valueUsed, errs)
}
func (e *OptExpr) check(ctx ctx, valueUsed bool, errs *Errors) {
e.Expr.check(ctx, valueUsed, errs)
}
func (e *SubExpr) check(ctx ctx, valueUsed bool, errs *Errors) {
e.Expr.check(ctx, valueUsed, errs)
}
func (e *Ident) check(ctx ctx, _ bool, errs *Errors) {
r, ok := ctx.rules[e.Name.String()]
if !ok {
errs.add(e, "rule %s undefined", e.Name.String())
} else {
e.rule = r
}
}
func (e *PredCode) check(ctx ctx, _ bool, _ *Errors) {
for _, l := range ctx.curLabels {
e.Labels = append(e.Labels, l)
}
sort.Slice(e.Labels, func(i, j int) bool {
return e.Labels[i].Label.String() < e.Labels[j].Label.String()
})
}
func (e *Literal) check(ctx, bool, *Errors) {}
func (e *CharClass) check(ctx, bool, *Errors) {}
func (e *Any) check(ctx, bool, *Errors) {}