-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
301 lines (247 loc) · 6.18 KB
/
parser.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
package main
import (
"fmt"
"strings"
)
type Attribute struct {
Name string
Value string
token string
export bool
}
type ParseMatch struct {
Text string
Nonterminal string
Rule Rule
Attributes []Attribute
Submatches []ParseMatch
Hypotheses []string
HypothesisCount uint
}
const TerminalSeparators = " ,.;»"
func parseTerminal(pattern string) (string, string) {
var term string
found, unescape := false, false
for i := 0; i < len(pattern); i++ {
escaped := i > 0 && pattern[i-1] == '\\'
if escaped {
unescape = true
}
if pattern[i] == '{' && !escaped {
term, pattern = pattern[:i], pattern[i:]
found = true
break
}
}
if !found {
term, pattern = pattern, ""
}
if unescape {
term = strings.Replace(term, "\\{", "{", -1)
}
return term, pattern
}
func parseNonterminal(pattern string) (string, []Attribute, string) {
attrs := []Attribute{}
if len(pattern) == 0 || pattern[0] != '{' {
return "", attrs, pattern
}
var body string
if i := strings.Index(pattern, "}"); i != -1 {
body, pattern = pattern[1:i], pattern[i+1:]
} else {
body, pattern = pattern, ""
}
var nterm string
for i, a := range strings.Split(body, " ") {
if len(a) == 0 {
continue
}
var attr Attribute
split := strings.Split(a, "=")
if len(split[0]) == 0 {
continue
}
rvalue := len(split) > 1 && len(split[1]) > 0
if split[0][0] == '!' {
attr.Name = split[0][1:]
attr.export = true
} else if i > 0 || rvalue {
attr.Name = split[0]
} else {
nterm = split[0]
continue
}
if rvalue {
if split[1][0] == '@' {
attr.token = split[1][1:]
} else {
attr.Value = split[1]
}
}
attrs = append(attrs, attr)
}
return nterm, attrs, pattern
}
func (match *ParseMatch) clone() ParseMatch {
nmatch := *match
nmatch.Attributes = make([]Attribute, len(match.Attributes))
copy(nmatch.Attributes, match.Attributes)
nmatch.Submatches = make([]ParseMatch, len(match.Submatches))
copy(nmatch.Submatches, match.Submatches)
nmatch.Hypotheses = make([]string, len(match.Hypotheses))
copy(nmatch.Hypotheses, match.Hypotheses)
return nmatch
}
func (match *ParseMatch) findAttr(name string) (Attribute, bool) {
for _, a := range match.Attributes {
if a.Name == name {
return a, true
}
}
return Attribute{}, false
}
func (match *ParseMatch) checkAttrValue(attr Attribute, hypo *string) bool {
attr2, ok := match.findAttr(attr.Name)
if !ok || len(attr2.Value) == 0 || attr2.Value == attr.Value {
return true
}
*hypo = fmt.Sprintf("%s = %s", attr.Name, attr2.Value)
return false
}
func (match *ParseMatch) checkAttrToken(attr Attribute, hypo *string) bool {
if len(attr.Value) == 0 {
return true
}
for i := 0; i < len(match.Attributes); i++ {
a := &match.Attributes[i]
if a.Name == attr.Name && a.token == attr.token {
if attr.export {
a.export = true
}
if len(a.Value) > 0 {
if a.Value == attr.Value {
return true
}
} else {
a.Value = attr.Value
return true
}
*hypo = fmt.Sprintf("%s = %s", attr.Name, a.Value)
return false
}
}
match.Attributes = append(match.Attributes, attr)
return true
}
func (match *ParseMatch) cleanExport() {
attrs := []Attribute{}
for _, a := range match.Attributes {
if len(a.token) == 0 || a.export {
a.token = ""
attrs = append(attrs, a)
}
}
match.Attributes = attrs
}
func (match *ParseMatch) checkSubmatch(attrs []Attribute,
submatch ParseMatch, hypothesesLimit uint) bool {
match.HypothesisCount += submatch.HypothesisCount
if match.HypothesisCount > hypothesesLimit {
return false
}
var hypo string
for _, a := range attrs {
attr, ok := submatch.findAttr(a.Name)
if !ok {
attr.Name = a.Name
}
if len(a.token) > 0 {
a.Value = attr.Value
}
if (len(a.Value) > 0 && !submatch.checkAttrValue(a, &hypo)) ||
(len(a.token) > 0 && !match.checkAttrToken(a, &hypo)) {
match.HypothesisCount++
if match.HypothesisCount > hypothesesLimit {
return false
}
hypo = fmt.Sprintf(
"%d: %s", len(match.Submatches)+1, hypo)
match.Hypotheses = append(match.Hypotheses, hypo)
}
if a.export && len(a.token) == 0 {
match.Attributes = append(match.Attributes, attr)
}
}
match.Text += submatch.Text
match.Submatches = append(match.Submatches, submatch)
return true
}
func parsePatternPart(text, pattern string, match ParseMatch,
hypothesesLimit uint, output chan []ParseMatch) {
term, pattern := parseTerminal(pattern)
if !strings.HasPrefix(text, term) {
output <- nil
return
}
match.Text += term
text = text[len(term):]
if len(pattern) == 0 {
match.cleanExport()
output <- []ParseMatch{match}
return
}
pred, attrs, pattern := parseNonterminal(pattern)
output2 := make(chan []ParseMatch)
count := 0
for _, m := range Parse(text, pred, hypothesesLimit) {
match2 := match.clone()
if match2.checkSubmatch(attrs, m, hypothesesLimit) {
go parsePatternPart(text[len(m.Text):],
pattern, match2, hypothesesLimit, output2)
count++
}
}
matches := []ParseMatch{}
for i := 0; i < count; i++ {
matches = append(matches, <-output2...)
}
output <- matches
}
func parsePattern(text string, match ParseMatch,
hypothesesLimit uint) []ParseMatch {
pattern := match.Rule.Pattern
if strings.HasPrefix(pattern, "@") {
_, match.Attributes, pattern = parseNonterminal(pattern[1:])
}
output := make(chan []ParseMatch)
go parsePatternPart(text, pattern, match, hypothesesLimit, output)
return <-output
}
func Parse(text, nonterminal string, hypothesesLimit uint) []ParseMatch {
matches, ok := FindInCache(text, nonterminal, hypothesesLimit)
if ok {
return matches
}
if len(nonterminal) == 0 {
var pref, sep string
if i := strings.IndexAny(text, TerminalSeparators); i != -1 {
pref, sep = text[:i], text[i:i+1]
} else {
pref, sep = text, ""
}
for _, m := range FindTerminals(pref, sep) {
if strings.HasPrefix(text, m.Text) {
matches = append(matches, m)
}
}
} else {
for _, r := range FindNonterminalRules(nonterminal) {
match := ParseMatch{Nonterminal: nonterminal, Rule: r}
matches = append(matches,
parsePattern(text, match, hypothesesLimit)...)
}
}
AddToCache(text, nonterminal, hypothesesLimit, matches)
return matches
}