forked from alecthomas/participle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebnf.go
139 lines (122 loc) · 2.62 KB
/
ebnf.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
package participle
import (
"fmt"
"strings"
)
// String returns the EBNF for the grammar.
//
// Productions are always upper cased. Lexer tokens are always lower case.
func (p *Parser) String() string {
return ebnf(p.root)
}
type ebnfp struct {
name string
out string
}
func ebnf(n node) string {
outp := []*ebnfp{}
switch n.(type) {
case *strct:
buildEBNF(true, n, map[node]bool{}, nil, &outp)
out := []string{}
for _, p := range outp {
out = append(out, fmt.Sprintf("%s = %s .", p.name, p.out))
}
return strings.Join(out, "\n")
default:
out := &ebnfp{}
buildEBNF(true, n, map[node]bool{}, out, &outp)
return out.out
}
}
func buildEBNF(root bool, n node, seen map[node]bool, p *ebnfp, outp *[]*ebnfp) {
switch n := n.(type) {
case *disjunction:
if !root {
p.out += "("
}
for i, next := range n.nodes {
if i > 0 {
p.out += " | "
}
buildEBNF(false, next, seen, p, outp)
}
if !root {
p.out += ")"
}
return
case *strct:
name := strings.ToUpper(n.typ.Name()[:1]) + n.typ.Name()[1:]
if p != nil {
p.out += name
}
if seen[n] {
return
}
seen[n] = true
p = &ebnfp{name: name}
*outp = append(*outp, p)
buildEBNF(true, n.expr, seen, p, outp)
return
case *sequence:
group := n.next != nil && !root
if group {
p.out += "("
}
for n != nil {
buildEBNF(false, n.node, seen, p, outp)
n = n.next
if n != nil {
p.out += " "
}
}
if group {
p.out += ")"
}
return
case *parseable:
p.out += n.t.Name()
case *capture:
buildEBNF(false, n.node, seen, p, outp)
case *reference:
p.out += "<" + strings.ToLower(n.identifier) + ">"
case *optional:
buildEBNF(false, n.node, seen, p, outp)
p.out += "?"
case *repetition:
buildEBNF(false, n.node, seen, p, outp)
p.out += "*"
case *negation:
p.out += "!"
buildEBNF(false, n.node, seen, p, outp)
return
case *literal:
p.out += fmt.Sprintf("%q", n.s)
case *group:
if child, ok := n.expr.(*group); ok && child.mode == groupMatchOnce {
buildEBNF(false, child.expr, seen, p, outp)
} else if child, ok := n.expr.(*capture); ok {
if grandchild, ok := child.node.(*group); ok && grandchild.mode == groupMatchOnce {
buildEBNF(false, grandchild.expr, seen, p, outp)
} else {
buildEBNF(false, n.expr, seen, p, outp)
}
} else {
buildEBNF(false, n.expr, seen, p, outp)
}
switch n.mode {
case groupMatchNonEmpty:
p.out += "!"
case groupMatchZeroOrOne:
p.out += "?"
case groupMatchZeroOrMore:
p.out += "*"
case groupMatchOneOrMore:
p.out += "+"
case groupMatchOnce:
}
return
default:
panic(fmt.Sprintf("unsupported node type %T", n))
}
}