This repository was archived by the owner on Mar 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex_parser.go
More file actions
345 lines (313 loc) · 9.92 KB
/
complex_parser.go
File metadata and controls
345 lines (313 loc) · 9.92 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package gparselib
import (
"fmt"
"math"
)
// ParseMulti uses a subparser multiple times.
// The minimum times the subparser has to match and the maximum times the
// subparser can match have to be configured.
func ParseMulti(
pd *ParseData, ctx interface{},
pluginSubparser SubparserOp, pluginSemantics SemanticsOp,
cfgMin, cfgMax int,
) (*ParseData, interface{}) {
orgPos := pd.Source.pos
relPos := 0
subresults := make([]*ParseResult, 0, min(cfgMax, 128))
for i := 0; i < cfgMax && pd.Result == nil; i++ {
pd, ctx = pluginSubparser(pd, ctx)
if !pd.Result.HasError() {
subresults = append(subresults, pd.Result)
pd.Result = nil
}
}
var lastFeedback []*FeedbackItem
if pd.Result != nil {
lastFeedback = pd.Result.Feedback
}
relPos = pd.Source.pos - orgPos
pd.Source.pos = orgPos
if len(subresults) >= cfgMin {
pd.Result = nil
createMatchedResult(pd, relPos)
saveAllValuesFeedback(pd, subresults)
pd.Result.Feedback = addPotentialProblems(pd.Result.Feedback, lastFeedback)
} else {
subresult := pd.Result
pd.Result = nil
createUnmatchedResult(
pd, relPos,
fmt.Sprintf(
"At least %d matches expected but got only %d",
cfgMin,
len(subresults),
),
nil,
)
pd.Result.Feedback = append(pd.Result.Feedback, subresult.Feedback...)
}
pd.SubResults = subresults
return handleSemantics(pluginSemantics, pd, ctx)
}
// NewParseMultiPlugin creates a plugin sporting a parser calling a subparser
// multiple times.
func NewParseMultiPlugin(
pluginSubparser SubparserOp, pluginSemantics SemanticsOp,
cfgMin, cfgMax int,
) SubparserOp {
return func(pd *ParseData, ctx interface{}) (*ParseData, interface{}) {
return ParseMulti(pd, ctx, pluginSubparser, pluginSemantics, cfgMin, cfgMax)
}
}
// ParseMulti0 uses its subparser at least one time without upper bound.
// But the result is still positive even if the subparser didn't match a single
// time.
func ParseMulti0(
pd *ParseData, ctx interface{},
pluginSubparser SubparserOp, pluginSemantics SemanticsOp,
) (*ParseData, interface{}) {
return ParseMulti(pd, ctx, pluginSubparser, pluginSemantics, 0, math.MaxInt32)
}
// NewParseMulti0Plugin creates a plugin sporting a parser calling a subparser
// multiple times.
func NewParseMulti0Plugin(pluginSubparser SubparserOp, pluginSemantics SemanticsOp) SubparserOp {
return func(pd *ParseData, ctx interface{}) (*ParseData, interface{}) {
return ParseMulti0(pd, ctx, pluginSubparser, pluginSemantics)
}
}
// ParseMulti1 uses its subparser at least one time without upper bound.
// The result is positive as long as the subparser matches at least one time.
func ParseMulti1(
pd *ParseData, ctx interface{},
pluginSubparser SubparserOp, pluginSemantics SemanticsOp,
) (*ParseData, interface{}) {
return ParseMulti(pd, ctx, pluginSubparser, pluginSemantics, 1, math.MaxInt32)
}
// NewParseMulti1Plugin creates a plugin sporting a parser calling a subparser
// at least one time.
func NewParseMulti1Plugin(pluginSubparser SubparserOp, pluginSemantics SemanticsOp) SubparserOp {
return func(pd *ParseData, ctx interface{}) (*ParseData, interface{}) {
return ParseMulti1(pd, ctx, pluginSubparser, pluginSemantics)
}
}
// ParseOptional uses its subparser exaclty one time.
// But the result is still positive even if the subparser didn't match.
func ParseOptional(
pd *ParseData, ctx interface{},
pluginSubparser SubparserOp, pluginSemantics SemanticsOp,
) (*ParseData, interface{}) {
orgPos := pd.Source.pos
pd, ctx = pluginSubparser(pd, ctx)
// if error: reset to ignore but save valuable feedback
if pd.Result.HasError() {
fb := pd.Result.Feedback
pd.Result.ErrPos = -1
pd.Result.Feedback = addPotentialProblems(make([]*FeedbackItem, 0, len(fb)), fb)
pd.Source.pos = orgPos
}
return handleSemantics(pluginSemantics, pd, ctx)
}
// NewParseOptionalPlugin creates a plugin sporting a parser calling a subparser
// once and ignoring errors in it.
func NewParseOptionalPlugin(pluginSubparser SubparserOp, pluginSemantics SemanticsOp) SubparserOp {
return func(pd *ParseData, ctx interface{}) (*ParseData, interface{}) {
return ParseOptional(pd, ctx, pluginSubparser, pluginSemantics)
}
}
// ParseAll calls multiple subparsers and all have to match for a successful result.
func ParseAll(
pd *ParseData, ctx interface{},
pluginSubparsers []SubparserOp, pluginSemantics SemanticsOp,
) (*ParseData, interface{}) {
orgPos := pd.Source.pos
subresults := make([]*ParseResult, len(pluginSubparsers))
for i, subparser := range pluginSubparsers {
pd, ctx = subparser(pd, ctx)
if pd.Result.HasError() {
pd.Source.pos = orgPos
pd.Result.Pos = orgPos // make result 'our result'
saveAllFeedback(pd, subresults)
return pd, ctx
}
subresults[i] = pd.Result
pd.Result = nil
}
relPos := pd.Source.pos - orgPos
pd.Source.pos = orgPos
createMatchedResult(pd, relPos)
saveAllValuesFeedback(pd, subresults)
pd.SubResults = subresults
return handleSemantics(pluginSemantics, pd, ctx)
}
// NewParseAllPlugin creates a plugin sporting a parser calling all subparsers.
func NewParseAllPlugin(
pluginSubparsers []SubparserOp, pluginSemantics SemanticsOp,
) SubparserOp {
return func(pd *ParseData, ctx interface{}) (*ParseData, interface{}) {
return ParseAll(pd, ctx, pluginSubparsers, pluginSemantics)
}
}
// ParseAny calls multiple subparsers until one matches.
// The result is only unsuccessful if no subparser matches.
func ParseAny(
pd *ParseData, ctx interface{},
pluginSubparsers []SubparserOp, pluginSemantics SemanticsOp,
) (*ParseData, interface{}) {
orgPos := pd.Source.pos
subresults := make([]*ParseResult, 0, len(pluginSubparsers))
lastPos := 0
for _, subparser := range pluginSubparsers {
pd, ctx = subparser(pd, ctx)
if !pd.Result.HasError() {
return handleSemantics(pluginSemantics, pd, ctx)
}
lastPos = max(lastPos, pd.Result.Pos)
pd.Source.pos = orgPos
subresults = append(subresults, pd.Result)
pd.Result = nil
}
relPos := lastPos - orgPos
pd.Source.pos = orgPos
pd.Result = nil
createUnmatchedResult(
pd, relPos,
fmt.Sprintf(
"Any subparser should match. But all %d subparsers failed",
len(pluginSubparsers),
),
nil,
)
saveBestFeedback(pd, subresults)
return pd, ctx
}
// NewParseAnyPlugin creates a plugin sporting a parser calling any successful
// subparser.
func NewParseAnyPlugin(
pluginSubparsers []SubparserOp, pluginSemantics SemanticsOp,
) SubparserOp {
return func(pd *ParseData, ctx interface{}) (*ParseData, interface{}) {
return ParseAny(pd, ctx, pluginSubparsers, pluginSemantics)
}
}
// ParseBest calls all subparsers and chooses the one with the longest match.
// The result is only unsuccessful if no subparser matches.
func ParseBest(
pd *ParseData, ctx interface{},
pluginSubparsers []SubparserOp, pluginSemantics SemanticsOp,
) (*ParseData, interface{}) {
orgPos := pd.Source.pos
allFeedback := make([]*FeedbackItem, 0, len(pluginSubparsers))
lastPos := 0
bestNewSourcePos := 0
var bestResult *ParseResult
for _, subparser := range pluginSubparsers {
pd, ctx = subparser(pd, ctx)
if !pd.Result.HasError() {
if pd.Source.pos > bestNewSourcePos {
bestNewSourcePos = pd.Source.pos
bestResult = pd.Result
}
}
lastPos = max(lastPos, pd.Result.Pos)
pd.Source.pos = orgPos
allFeedback = append(allFeedback, pd.Result.Feedback...)
pd.Result = nil
}
if bestResult != nil {
pd.Source.pos = bestNewSourcePos
pd.Result = bestResult
return handleSemantics(pluginSemantics, pd, ctx)
}
relPos := lastPos - orgPos
pd.Source.pos = orgPos
pd.Result = nil
createUnmatchedResult(
pd, relPos,
fmt.Sprintf(
"Best subparser should match. But all %d subparsers failed",
len(pluginSubparsers),
),
nil,
)
pd.Result.Feedback = append(pd.Result.Feedback, allFeedback...)
return pd, ctx
}
// NewParseBestPlugin creates a plugin sporting a parser calling the best
// successful subparser.
func NewParseBestPlugin(
pluginSubparsers []SubparserOp, pluginSemantics SemanticsOp,
) SubparserOp {
return func(pd *ParseData, ctx interface{}) (*ParseData, interface{}) {
return ParseBest(pd, ctx, pluginSubparsers, pluginSemantics)
}
}
//
// -----------------------------------------------------------------------------------
// Utility Functions:
//
func saveAllValuesFeedback(pd *ParseData, tmpSubresults []*ParseResult) {
s := make([]interface{}, len(tmpSubresults))
for i, subres := range tmpSubresults {
s[i] = subres.Value
pd.Result.Feedback = append(pd.Result.Feedback, subres.Feedback...)
}
pd.Result.Value = s
}
func saveAllFeedback(pd *ParseData, tmpSubresults []*ParseResult) {
for _, subres := range tmpSubresults {
if subres != nil {
pd.Result.Feedback = append(pd.Result.Feedback, subres.Feedback...)
}
}
}
func saveBestFeedback(pd *ParseData, tmpSubresults []*ParseResult) {
maxFBPos := -1
bestFeedback := []*FeedbackItem{}
for _, subres := range tmpSubresults {
fbPos := maxFeedbackPos(subres.Feedback)
if fbPos > maxFBPos {
maxFBPos = fbPos
bestFeedback = subres.Feedback
} else if fbPos == maxFBPos {
bestFeedback = append(bestFeedback, subres.Feedback...)
}
}
pd.Result.Feedback = append(pd.Result.Feedback, bestFeedback...)
}
func maxFeedbackPos(feedback []*FeedbackItem) int {
maxPos := -1
for _, fb := range feedback {
if fb.Pos > maxPos {
maxPos = fb.Pos
}
}
return maxPos
}
func addPotentialProblems(feedback []*FeedbackItem, potentialFeedback []*FeedbackItem) []*FeedbackItem {
if potentialFeedback == nil {
return feedback
}
for _, pf := range potentialFeedback {
if pf.Kind != FeedbackPotentialProblem {
if pf.Kind == FeedbackError {
pe := pf.Msg.(*parseError)
msg := pe.myErr
if pe.baseErr != nil {
msg += ": " + pe.baseErr.Error()
}
pm := &parseMessage{
where: pe.where,
msg: msg,
}
pf.Msg = pm
pf.Kind = FeedbackPotentialProblem
} else if pf.Kind == FeedbackWarning {
pm := pf.Msg.(*parseMessage)
pm.msg = "potential problem: " + pm.msg
pf.Msg = pm
pf.Kind = FeedbackPotentialProblem
}
}
}
return append(feedback, potentialFeedback...)
}