-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.go
69 lines (58 loc) · 1.24 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
package peg
import (
"errors"
"fmt"
)
var checkers = [...]func(*Tree) []error{
func(tree *Tree) (errs []error) {
rs := map[string]struct{}{}
// Dup rule
for _, r := range tree.RuleList {
if _, ok := rs[r.Name]; !ok {
rs[r.Name] = struct{}{}
} else {
errs = append(errs, errors.New(
fmt.Sprintf("Dup rule %q", r.Name),
))
}
}
// Rule undefined
stack := []*ChoiceExpr{}
checkCE := func(ce *ChoiceExpr) {
for _, ae := range ce.ActionExprs {
for _, le := range ae.SeqExpr.LabeledExprs {
pe := le.PrefixedExpr.SuffixedExpr.PrimaryExpr.PrimaryExpr
switch pe.(type) {
case string:
rn := pe.(string)
if _, ok := rs[rn]; !ok {
errs = append(errs, errors.New(
fmt.Sprintf("Rule %q undefined", rn),
))
}
case *ChoiceExpr:
stack = append(stack, pe.(*ChoiceExpr))
}
}
}
}
for _, r := range tree.RuleList {
stack = append(stack, r.ChoiceExpr)
}
for len(stack) > 0 {
checkCE(stack[0])
stack = stack[1:]
}
return
},
}
func Check(tree *Tree) []error {
errs := []error{}
for _, checker := range checkers {
_errs := checker(tree)
if _errs != nil && len(_errs) > 0 {
errs = append(errs, _errs...)
}
}
return errs
}