forked from alecthomas/participle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookahead.go
265 lines (229 loc) · 5.48 KB
/
lookahead.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
package participle
import (
"fmt"
"hash/fnv"
"reflect"
"sort"
"github.com/alecthomas/participle/lexer"
)
type lroot int
type lookahead struct {
root int
tokens []lexer.Token
}
func (l lookahead) String() string {
return fmt.Sprintf("lookahead{root: %d, token: %#v}", l.root, l.tokens)
}
func (l *lookahead) hash() uint64 {
w := fnv.New64a()
for _, t := range l.tokens {
fmt.Fprintf(w, "%d:%s\n", t.Type, t.Value)
}
return w.Sum64()
}
func buildLookahead(nodes ...node) (table []lookahead, err error) {
l := &lookaheadWalker{limit: 16, seen: map[node]int{}}
for root, node := range nodes {
if node != nil {
l.push(root, node, nil)
}
}
depth := 0
for ; depth < 16; depth++ {
ambiguous := l.ambiguous()
if len(ambiguous) == 0 {
return l.collect(), nil
}
stepped := false
for _, group := range ambiguous {
for _, c := range group {
// fmt.Printf("root=%d, depth=%d: %T %#v\n", c.root, c.depth, c.branch, c.token)
if l.step(c.branch, c) {
stepped = true
}
}
// fmt.Println()
}
if !stepped {
break
}
}
// TODO: We should never fail to build lookahead.
return nil, fmt.Errorf("possible left recursion: could not disambiguate after %d tokens of lookahead", depth)
}
type lookaheadCursor struct {
branch node // Branch leaf was stepped from.
lookahead
}
type lookaheadWalker struct {
seen map[node]int
limit int
cursors []*lookaheadCursor
}
func (l *lookaheadWalker) collect() []lookahead {
out := []lookahead{}
for _, cursor := range l.cursors {
out = append(out, cursor.lookahead)
}
sort.Slice(out, func(i, j int) bool {
n := len(out[i].tokens)
m := len(out[j].tokens)
if n > m {
return true
}
return (n == m && len(out[i].tokens[n-1].Value) > len(out[j].tokens[m-1].Value)) || out[i].root < out[j].root
})
return out
}
// Find cursors that are still ambiguous.
func (l *lookaheadWalker) ambiguous() [][]*lookaheadCursor {
grouped := map[uint64][]*lookaheadCursor{}
for _, cursor := range l.cursors {
key := cursor.hash()
grouped[key] = append(grouped[key], cursor)
}
out := [][]*lookaheadCursor{}
for _, group := range grouped {
if len(group) > 1 {
out = append(out, group)
}
}
return out
}
func (l *lookaheadWalker) push(root int, node node, tokens []lexer.Token) {
cursor := &lookaheadCursor{
branch: node,
lookahead: lookahead{
root: root,
tokens: append([]lexer.Token{}, tokens...),
},
}
l.cursors = append(l.cursors, cursor)
l.step(node, cursor)
}
func (l *lookaheadWalker) remove(cursor *lookaheadCursor) {
for i, c := range l.cursors {
if cursor == c {
l.cursors = append(l.cursors[:i], l.cursors[i+1:]...)
}
}
}
// Returns true if a step occurred or false if the cursor has already terminated.
func (l *lookaheadWalker) step(node node, cursor *lookaheadCursor) bool {
l.seen[node]++
if cursor.branch == nil || l.seen[node] > 32 {
return false
}
switch n := node.(type) {
case *disjunction:
for _, c := range n.nodes {
l.push(cursor.root, c, cursor.tokens)
}
l.remove(cursor)
case *sequence:
if n != nil {
l.step(n.node, cursor)
cursor.branch = n.next
}
case *capture:
l.step(n.node, cursor)
case *strct:
l.step(n.expr, cursor)
case *optional:
l.push(cursor.root, n.node, cursor.tokens)
if n.next != nil {
l.push(cursor.root, n.next, cursor.tokens)
}
l.remove(cursor)
case *repetition:
l.push(cursor.root, n.node, cursor.tokens)
if n.next != nil {
l.push(cursor.root, n.next, cursor.tokens)
}
l.remove(cursor)
case *parseable:
case *literal:
cursor.tokens = append(cursor.tokens, lexer.Token{Type: n.t, Value: n.s})
cursor.branch = nil
return true
case *reference:
cursor.tokens = append(cursor.tokens, lexer.Token{Type: n.typ})
cursor.branch = nil
default:
panic(fmt.Sprintf("unsupported node type %T", n))
}
return true
}
func applyLookahead(m node, seen map[node]bool) {
if seen[m] {
return
}
seen[m] = true
switch n := m.(type) {
case *disjunction:
lookahead, err := buildLookahead(n.nodes...)
if err == nil {
n.lookahead = lookahead
} else {
panic(Error(err.Error() + ": " + n.String()))
}
for _, c := range n.nodes {
applyLookahead(c, seen)
}
case *sequence:
for c := n; c != nil; c = c.next {
applyLookahead(c.node, seen)
}
case *literal:
case *capture:
applyLookahead(n.node, seen)
case *reference:
case *strct:
applyLookahead(n.expr, seen)
case *optional:
lookahead, err := buildLookahead(n.node, n.next)
if err == nil {
n.lookahead = lookahead
} else {
panic(Error(err.Error() + ": " + n.String()))
}
applyLookahead(n.node, seen)
if n.next != nil {
applyLookahead(n.next, seen)
}
case *repetition:
lookahead, err := buildLookahead(n.node, n.next)
if err == nil {
n.lookahead = lookahead
} else {
panic(Error(err.Error() + ": " + n.String()))
}
applyLookahead(n.node, seen)
if n.next != nil {
applyLookahead(n.next, seen)
}
case *parseable:
default:
panic(fmt.Sprintf("unsupported node type %T", m))
}
}
type lookaheadTable []lookahead
// Select node to use.
//
// Will return -2 if lookahead table is missing, -1 for no match, or index of selected node.
func (l lookaheadTable) Select(lex lexer.PeekingLexer, parent reflect.Value) (selected int) {
if l == nil {
return -2
}
next:
for _, look := range l {
for depth, lt := range look.tokens {
t := lex.Peek(depth)
if !((lt.Value == "" || lt.Value == t.Value) && (lt.Type == lexer.EOF || lt.Type == t.Type)) {
continue next
}
}
return look.root
}
return -1
}