-
Notifications
You must be signed in to change notification settings - Fork 0
/
choice.go
181 lines (152 loc) · 3.47 KB
/
choice.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
package treerack
type choiceParser struct {
name string
id int
commit CommitType
options []parser
generalizations []int
}
type choiceBuilder struct {
name string
id int
commit CommitType
options []builder
generalizations []int
}
func (p *choiceParser) nodeName() string { return p.name }
func (p *choiceParser) nodeID() int { return p.id }
func (p *choiceParser) commitType() CommitType { return p.commit }
func (p *choiceParser) parse(c *context) {
if c.fromResults(p) {
return
}
if c.results.pending(c.offset, p.id) {
c.fail(c.offset)
return
}
c.results.markPending(c.offset, p.id)
var (
match bool
optionIndex int
foundMatch bool
failingParser parser
)
from := c.offset
to := c.offset
initialFailOffset := c.failOffset
initialFailingParser := c.failingParser
failOffset := initialFailOffset
for {
foundMatch = false
optionIndex = 0
for optionIndex < len(p.options) {
p.options[optionIndex].parse(c)
optionIndex++
if !c.matchLast {
if c.failOffset > failOffset {
failOffset = c.failOffset
failingParser = c.failingParser
}
}
if !c.matchLast || match && c.offset <= to {
c.offset = from
continue
}
match = true
foundMatch = true
to = c.offset
c.offset = from
c.results.setMatch(from, p.id, to)
}
if !foundMatch {
break
}
}
if match {
if p.commit&NoKeyword != 0 && c.isKeyword(from, to) {
if c.failingParser == nil &&
p.commit&userDefined != 0 &&
p.commit&Whitespace == 0 &&
p.commit&FailPass == 0 {
c.failingParser = p
}
c.fail(from)
c.results.unmarkPending(from, p.id)
return
}
if failOffset > to {
c.failOffset = failOffset
c.failingParser = failingParser
} else if to > initialFailOffset {
c.failOffset = -1
c.failingParser = nil
} else {
c.failOffset = initialFailOffset
c.failingParser = initialFailingParser
}
c.success(to)
c.results.unmarkPending(from, p.id)
return
}
if failOffset > initialFailOffset {
c.failOffset = failOffset
c.failingParser = failingParser
if c.failingParser == nil &&
p.commitType()&userDefined != 0 &&
p.commitType()&Whitespace == 0 &&
p.commitType()&FailPass == 0 {
c.failingParser = p
}
}
c.results.setNoMatch(from, p.id)
c.fail(from)
c.results.unmarkPending(from, p.id)
}
func (b *choiceBuilder) nodeName() string { return b.name }
func (b *choiceBuilder) nodeID() int { return b.id }
func (b *choiceBuilder) build(c *context) ([]*Node, bool) {
to, ok := c.results.longestMatch(c.offset, b.id)
if !ok {
return nil, false
}
from := c.offset
parsed := to > from
if parsed {
c.results.dropMatchTo(c.offset, b.id, to)
for _, g := range b.generalizations {
c.results.dropMatchTo(c.offset, g, to)
}
} else {
if c.results.pending(c.offset, b.id) {
return nil, false
}
c.results.markPending(c.offset, b.id)
for _, g := range b.generalizations {
c.results.markPending(c.offset, g)
}
}
var option builder
for _, o := range b.options {
if c.results.hasMatchTo(c.offset, o.nodeID(), to) {
option = o
break
}
}
n, _ := option.build(c)
if !parsed {
c.results.unmarkPending(from, b.id)
for _, g := range b.generalizations {
c.results.unmarkPending(from, g)
}
}
if b.commit&Alias != 0 {
return n, true
}
return []*Node{{
Name: b.name,
From: from,
To: to,
Nodes: n,
tokens: c.tokens,
}}, true
}