/
rule.go
648 lines (548 loc) · 18 KB
/
rule.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
// 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 "fmt"
// Grammar is a PEG grammar.
type Grammar struct {
// Prelude is custom code added to the beginning of the generated output.
Prelude Text
// Rules are the rules of the grammar.
Rules []Rule
// CheckedRules are the rules successfully checked by the Check pass.
// It contains all non-template rules and all expanded templates.
CheckedRules []*Rule
}
// A Rule defines a production in a PEG grammar.
type Rule struct {
Name
// ErrorName, if non-nil, indicates that this is a named rule.
// Errors beneath a named rule are collapsed,
// reporting the error position as the start of the rule's parse
// with the "want" message set to ErrorName.
//
// If nil, the rule is unnamed and does not collapse errors.
ErrorName Text
// Expr is the PEG expression matched by the rule.
Expr Expr
// N is the rule's unique integer within its containing Grammar.
// It is a small integer that may be used as an array index.
N int
// typ is the type of the rule in the action pass.
// typ is nil before the checkLeft pass add non-nil after.
typ *string
// epsilon indicates whether the rule can match the empty string.
epsilon bool
// Labels is the set of all label names in the rule's expression.
Labels []*LabelExpr
}
func (r *Rule) Begin() Loc { return r.Name.Begin() }
func (r *Rule) End() Loc { return r.Expr.End() }
func (r Rule) Type() string { return *r.typ }
// A Name is the name of a rule template.
type Name struct {
// Name is the name of the template.
Name Text
// Args are the arguments or parameters of the template.
Args []Text
}
func (n Name) Begin() Loc { return n.Name.Begin() }
func (n Name) End() Loc {
if len(n.Args) == 0 {
return n.Name.End()
}
return n.Args[len(n.Args)-1].End()
}
// Text is a string of text located along with its location in the input.
type Text interface {
Located
// String is the text string.
String() string
}
// Loc identifies a location in a file by its line and column numbers.
type Loc struct {
// File is the name of the input file.
File string
// Line is line number of the location.
// The first line of input is line number 1.
Line int
// Col is the Loc's rune offset into the line.
// Col 0 is before the first rune on the line.
Col int
}
// Less returns whether the receiver is earlier in the input than the argument.
func (l Loc) Less(j Loc) bool {
if l.Line == j.Line {
return l.Col < j.Col
}
return l.Line < j.Line
}
// PrettyPrint implements the pretty.PrettyPrinter interface,
// returning a simpler, one-line string form of the Loc.
func (l Loc) PrettyPrint() string { return fmt.Sprintf("Loc{%d, %d}", l.Line, l.Col) }
// Begin returns the Loc.
func (l Loc) Begin() Loc { return l }
// End returns the Loc.
func (l Loc) End() Loc { return l }
// Expr is PEG expression that matches a sequence of input runes.
type Expr interface {
Located
String() string
// fullString returns the fully parenthesized string representation.
fullString() string
// Walk calls a function for each expression in the tree.
// Walk stops early if the function returns false.
Walk(func(Expr) bool) bool
// substitute returns a clone of the expression
// with all occurrences of identifiers that are keys of sub
// substituted with the corresponding value.
// substitute must not be called after Check,
// because it does not update bookkeeping fields
// that are set by the Check pass.
substitute(sub map[string]string) Expr
// Type returns the type of the expression in the Action Tree.
// This is the Go type associated with the expression.
Type() string
// epsilon returns whether the rule can match the empty string.
epsilon() bool
// CanFail returns whether the node can ever fail to parse.
// Nodes like * or ?, for example, can never fail.
// Parents of never-fail nodes needn't emit a failure branch,
// as it will never be called.
CanFail() bool
// checkLeft checks for left-recursion and sets rule types.
checkLeft(rules map[string]*Rule, p path, errs *Errors)
// check checks for undefined identifiers,
// linking defined identifiers to rules;
// and checks for type mismatches.
check(ctx ctx, valueUsed bool, errs *Errors)
}
// A Choice is an ordered choice between expressions.
type Choice struct{ Exprs []Expr }
func (e *Choice) Begin() Loc { return e.Exprs[0].Begin() }
func (e *Choice) End() Loc { return e.Exprs[len(e.Exprs)-1].End() }
func (e *Choice) Walk(f func(Expr) bool) bool {
if !f(e) {
return false
}
for _, kid := range e.Exprs {
if !kid.Walk(f) {
return false
}
}
return true
}
func (e *Choice) substitute(sub map[string]string) Expr {
substitute := *e
substitute.Exprs = make([]Expr, len(e.Exprs))
for i, kid := range e.Exprs {
substitute.Exprs[i] = kid.substitute(sub)
}
return &substitute
}
// Type returns the type of a choice expression,
// which is the type of it's first branch.
// All other branches must have the same type;
// this is verified during the Check pass.
func (e *Choice) Type() string { return e.Exprs[0].Type() }
func (e *Choice) epsilon() bool {
for _, e := range e.Exprs {
if e.epsilon() {
return true
}
}
return false
}
func (e *Choice) CanFail() bool {
// A choice node can only fail if all of its branches can fail.
// If there is a non-failing branch, it will always return accept.
for _, s := range e.Exprs {
if !s.CanFail() {
return false
}
}
return true
}
// An Action is an action expression:
// a subexpression and code to run if matched.
type Action struct {
Expr Expr
// Code is the Go code to execute if the subexpression is matched.
// The Begin and End locations of Code includes the { } delimiters,
// but the string does not.
//
// TODO: specify the environment under which the code is run.
Code Text
// ReturnType is the go type of the value returned by the action.
ReturnType string
// Labels are the labels that are in scope of this action.
Labels []*LabelExpr
}
func (e *Action) Begin() Loc { return e.Expr.Begin() }
func (e *Action) End() Loc { return e.Code.End() }
func (e *Action) Type() string { return e.ReturnType }
func (e *Action) epsilon() bool { return e.Expr.epsilon() }
func (e *Action) CanFail() bool { return e.Expr.CanFail() }
func (e *Action) Walk(f func(Expr) bool) bool {
return f(e) && e.Expr.Walk(f)
}
func (e *Action) substitute(sub map[string]string) Expr {
substitute := *e
substitute.Expr = e.Expr.substitute(sub)
substitute.Labels = nil
return &substitute
}
// A Sequence is a sequence of expressions.
type Sequence struct{ Exprs []Expr }
func (e *Sequence) Begin() Loc { return e.Exprs[0].Begin() }
func (e *Sequence) End() Loc { return e.Exprs[len(e.Exprs)-1].End() }
func (e *Sequence) Walk(f func(Expr) bool) bool {
if !f(e) {
return false
}
for _, kid := range e.Exprs {
if !kid.Walk(f) {
return false
}
}
return true
}
func (e *Sequence) substitute(sub map[string]string) Expr {
substitute := *e
substitute.Exprs = make([]Expr, len(e.Exprs))
for i, kid := range e.Exprs {
substitute.Exprs[i] = kid.substitute(sub)
}
return &substitute
}
// Type returns the type of a sequence expression,
// which is based on the type of its first sub-expression.
// All other other sub-expressions must have the same type;
// this is verified during the Check pass.
//
// If the first sub-expression is a string,
// the type of the entire sequence is a string.
// The value is the concatenation of all sub-expressions.
//
// Otherwise, the type is a slice of the first sub-expression type.
// The value is the slice of all sub-expression values.
func (e *Sequence) Type() string {
t := e.Exprs[0].Type()
switch t {
case "":
return ""
case "string":
return "string"
default:
return "[]" + t
}
}
func (e *Sequence) epsilon() bool {
for _, e := range e.Exprs {
if !e.epsilon() {
return false
}
}
return true
}
func (e *Sequence) CanFail() bool {
for _, s := range e.Exprs {
if s.CanFail() {
return true
}
}
return false
}
// A LabelExpr is a labeled subexpression.
// The label can be used in actions to refer to the result of the subexperssion.
type LabelExpr struct {
// Label is the text of the label, not including the :.
Label Text
Expr Expr
// N is a small integer assigned to this label
// that is unique within the containing Rule.
// It is a small integer that may be used as an array index.
N int
}
func (e *LabelExpr) Begin() Loc { return e.Label.Begin() }
func (e *LabelExpr) End() Loc { return e.Expr.End() }
func (e *LabelExpr) Type() string { return e.Expr.Type() }
func (e *LabelExpr) epsilon() bool { return e.Expr.epsilon() }
func (e *LabelExpr) CanFail() bool { return e.Expr.CanFail() }
func (e *LabelExpr) Walk(f func(Expr) bool) bool {
return f(e) && e.Expr.Walk(f)
}
func (e *LabelExpr) substitute(sub map[string]string) Expr {
substitute := *e
substitute.Expr = e.Expr.substitute(sub)
return &substitute
}
// A PredExpr is a non-consuming predicate expression:
// If it succeeds (or fails, in the case of Neg),
// return success and consume no input.
// If it fails (or succeeds, in the case of Neg),
// return failure and consume no input.
// Predicate expressions allow a powerful form of lookahead.
type PredExpr struct {
Expr Expr
// Neg indicates that the result of the predicate is negated.
Neg bool
// Loc is the location of the operator, & or !.
Loc Loc
}
func (e *PredExpr) Begin() Loc { return e.Loc }
func (e *PredExpr) End() Loc { return e.Expr.End() }
// Type returns the type of the predicate expression,
// which is a string; the value is always the empty string.
func (e *PredExpr) Type() string { return "string" }
func (e *PredExpr) epsilon() bool { return true }
func (e *PredExpr) CanFail() bool { return e.Expr.CanFail() }
func (e *PredExpr) Walk(f func(Expr) bool) bool {
return f(e) && e.Expr.Walk(f)
}
func (e *PredExpr) substitute(sub map[string]string) Expr {
substitute := *e
substitute.Expr = e.Expr.substitute(sub)
return &substitute
}
// A RepExpr is a repetition expression, sepecifying whether the sub-expression
// should be matched any number of times (*) or one or more times (+),
type RepExpr struct {
// Op is one of * or +.
Op rune
Expr Expr
// Loc is the location of the operator, * or +.
Loc Loc
}
func (e *RepExpr) Begin() Loc { return e.Expr.Begin() }
func (e *RepExpr) End() Loc { return e.Loc }
// Type returns the type of the repetition expression,
// which is based on the type of its sub-expression.
//
// If the sub-expression type is string,
// the repetition expression type is a string.
// The value is the concatenation of all matches,
// or the empty string if nothing matches.
//
// Otherwise, the type is a slice of the sub-expression type.
// The value contains an element for each match
// of the sub-expression.
func (e *RepExpr) Type() string {
switch t := e.Expr.Type(); t {
case "":
return ""
case "string":
return t
default:
return "[]" + t
}
}
func (e *RepExpr) epsilon() bool { return e.Op == '*' }
func (e *RepExpr) CanFail() bool { return e.Op == '+' && e.Expr.CanFail() }
func (e *RepExpr) Walk(f func(Expr) bool) bool {
return f(e) && e.Expr.Walk(f)
}
func (e *RepExpr) substitute(sub map[string]string) Expr {
substitute := *e
substitute.Expr = e.Expr.substitute(sub)
return &substitute
}
// An OptExpr is an optional expression, which may or may not be matched.
type OptExpr struct {
Expr Expr
// Loc is the location of the ?.
Loc Loc
}
func (e *OptExpr) Begin() Loc { return e.Expr.Begin() }
func (e *OptExpr) End() Loc { return e.Loc }
// Type returns the type of the optional expression,
// which is based on the type of its sub-expression.
//
// If the sub-expression type is string,
// the optional expression type is a string.
// The value is the value of the sub-expression if it matched,
// or the empty string if it did not match.
//
// Otherwise, the type is a pointer to the type of the sub-expression.
// The value is a pointer to the sub-expression's value if it matched,
// or a nil pointer if it did not match.
func (e *OptExpr) Type() string {
switch t := e.Expr.Type(); {
case t == "":
return ""
case t == "string":
return t
default:
return "*" + e.Expr.Type()
}
}
func (e *OptExpr) epsilon() bool { return true }
func (e *OptExpr) CanFail() bool { return false }
func (e *OptExpr) Walk(f func(Expr) bool) bool {
return f(e) && e.Expr.Walk(f)
}
func (e *OptExpr) substitute(sub map[string]string) Expr {
substitute := *e
substitute.Expr = e.Expr.substitute(sub)
return &substitute
}
// An Ident is an identifier referring to the name of anothe rule,
// indicating to match that rule's expression.
type Ident struct {
Name
// rule is the rule referred to by this identifier.
// It is set during check.
rule *Rule
}
func (e *Ident) Begin() Loc { return e.Name.Begin() }
func (e *Ident) End() Loc { return e.Name.End() }
func (e *Ident) CanFail() bool { return true }
func (e *Ident) Walk(f func(Expr) bool) bool { return f(e) }
// Type returns the type of the identifier expression,
// which is the type of its corresponding rule.
func (e *Ident) Type() string {
if e.rule == nil {
return ""
}
return e.rule.Type()
}
func (e *Ident) epsilon() bool {
if e.rule == nil {
return false
}
return e.rule.epsilon
}
func (e *Ident) substitute(sub map[string]string) Expr {
substitute := *e
if s, ok := sub[e.Name.String()]; ok {
substitute.Name = Name{
Name: text{
str: s,
begin: e.Name.Begin(),
end: e.Name.End(),
},
}
}
substitute.Args = make([]Text, len(e.Args))
for i, a := range e.Args {
if s, ok := sub[a.String()]; !ok {
substitute.Args[i] = e.Args[i]
} else {
substitute.Args[i] = text{
str: s,
begin: a.Begin(),
end: a.End(),
}
}
}
return &substitute
}
// A SubExpr simply wraps an expression.
// It holds no extra information beyond tracking parentheses.
// It's purpose is to allow easily re-inserting the parentheses
// when stringifying an expression, whithout the need
// to compute precedence inversion for each subexpression.
type SubExpr struct {
Expr
// Open is the location of the open parenthesis.
// Close is the location of the close parenthesis.
Open, Close Loc
}
func (e *SubExpr) Begin() Loc { return e.Open }
func (e *SubExpr) End() Loc { return e.Close }
func (e *SubExpr) Type() string { return e.Expr.Type() }
func (e *SubExpr) epsilon() bool { return e.Expr.epsilon() }
func (e *SubExpr) CanFail() bool { return e.Expr.CanFail() }
func (e *SubExpr) Walk(f func(Expr) bool) bool {
return f(e) && e.Expr.Walk(f)
}
func (e *SubExpr) substitute(sub map[string]string) Expr {
substitute := *e
substitute.Expr = e.Expr.substitute(sub)
return &substitute
}
// A PredCode is a predicate code expression,
// allowing predication using a Go boolean expression.
//
// TODO: Specify the conditions under which the expression is evaluated.
type PredCode struct {
// Code is a Go boolean expression.
// The Begin and End locations of Code includes the { } delimiters,
// but the string does not.
Code Text
// Neg indicates that the result of the predicate is negated.
Neg bool
// Loc is the location of the operator, & or !.
Loc Loc
// Labels are the labels that are in scope of this action.
Labels []*LabelExpr
}
func (e *PredCode) Begin() Loc { return e.Loc }
func (e *PredCode) End() Loc { return e.Code.End() }
// Type returns the type of the predicate code expression,
// which is a string; the value is always the empty string.
func (e *PredCode) Type() string { return "string" }
func (e *PredCode) epsilon() bool { return true }
func (e *PredCode) CanFail() bool { return true }
func (e *PredCode) Walk(f func(Expr) bool) bool { return f(e) }
func (e *PredCode) substitute(sub map[string]string) Expr {
substitute := *e
substitute.Labels = nil
return &substitute
}
// A Literal matches a literal text string.
type Literal struct {
// Text is the text to match.
// The Begin and End locations of Text includes the ' or " delimiters,
// but the string does not.
Text Text
}
func (e *Literal) Begin() Loc { return e.Text.Begin() }
func (e *Literal) End() Loc { return e.Text.End() }
func (e *Literal) Type() string { return "string" }
func (e *Literal) epsilon() bool { return false }
func (e *Literal) CanFail() bool { return true }
func (e *Literal) Walk(f func(Expr) bool) bool { return f(e) }
func (e *Literal) substitute(sub map[string]string) Expr {
substitute := *e
return &substitute
}
// A CharClass matches a single rune from a set of acceptable
// (or unacceptable if Neg) runes.
type CharClass struct {
// Spans are rune spans accepted (or rejected) by the character class.
// The 0th rune is always ≤ the 1st.
// Single rune matches are a span of both the same rune.
Spans [][2]rune
// Neg indicates that the input must not match any in the set.
Neg bool
// Open and Close are the Loc of [ and ] respectively.
Open, Close Loc
}
func (e *CharClass) Begin() Loc { return e.Open }
func (e *CharClass) End() Loc { return e.Close }
func (e *CharClass) Type() string { return "string" }
func (e *CharClass) epsilon() bool { return false }
func (e *CharClass) CanFail() bool { return true }
func (e *CharClass) Walk(f func(Expr) bool) bool { return f(e) }
func (e *CharClass) substitute(sub map[string]string) Expr {
substitute := *e
return &substitute
}
// Any matches any rune.
type Any struct {
// Loc is the location of the . symbol.
Loc Loc
}
func (e *Any) Begin() Loc { return e.Loc }
func (e *Any) End() Loc { return Loc{Line: e.Loc.Line, Col: e.Loc.Col + 1} }
func (e *Any) Type() string { return "string" }
func (e *Any) epsilon() bool { return false }
func (e *Any) CanFail() bool { return true }
func (e *Any) Walk(f func(Expr) bool) bool { return f(e) }
func (e *Any) substitute(sub map[string]string) Expr {
substitute := *e
return &substitute
}