-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombining.go
555 lines (481 loc) · 10.4 KB
/
combining.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
package peg
import (
"fmt"
"strings"
)
// Underlying types implemented Pattern interface.
type (
patternSequence struct {
pats []Pattern
}
patternAlternative struct {
pats []Pattern
}
patternSkip struct {
n int
}
patternAnyRuneUntil struct {
without bool
pat Pattern
}
patternQualifierAtLeast struct {
n int
pat Pattern
}
patternQualifierOptional struct {
pat Pattern
}
patternQualifierRange struct {
m, n int
pat Pattern
}
)
// Seq tries to match patterns in given sequence, the Seq itself only matched
// when all of the patterns is successfully matched, the text is consumed in
// order. It dismatches if any dismatched pattern is encountered.
func Seq(sequence ...Pattern) Pattern {
switch len(sequence) {
case 0:
return True
case 1:
return sequence[0]
default:
return &patternSequence{sequence}
}
}
// Alt searches the first matched pattern in the given choices, theAlt itself
// only matches when any pattern is successfully matched, the then Alt consumes
// the searched pattern's number of bytes matched. It dismatches if all the
// choices is dismatched.
//
// It is recommended to place pattern that match more text in a prior order.
// For example, Alt(Seq(Q1(R('0', '9')), T("."), Q1(R('0', '9'))),
// Q1(R('0', '9'))) could match both "0.0" and "0", while Alt(Q1(R('0', '9')),
// Seq(Q1(R('0', '9')), T("."), Q1(R('0', '9')))) could only match "0".
func Alt(choices ...Pattern) Pattern {
switch len(choices) {
case 0:
return False
case 1:
return choices[0]
default:
return &patternAlternative{choices}
}
}
// Skip matches exactly n runes.
func Skip(n int) Pattern {
if n < 0 {
n = 0
}
if n == 0 {
return True
}
return &patternSkip{n}
}
// Until matches any rune until the start of text piece
// searched by given pattern.
func Until(pat Pattern) Pattern {
return &patternAnyRuneUntil{without: true, pat: pat}
}
// UntilB matches any rune until the end of text piece
// searched by given pattern.
func UntilB(pat Pattern) Pattern {
return &patternAnyRuneUntil{without: false, pat: pat}
}
// Q0 matches any of the given choices repeated zero or more times.
func Q0(choices ...Pattern) Pattern {
var pat Pattern
switch len(choices) {
case 0:
// Alt() <=> False
// Q0(False) <=> True
return True
case 1:
pat = choices[0]
default:
pat = Alt(choices...)
}
return &patternQualifierAtLeast{n: 0, pat: pat}
}
// Q1 matches any of the given choices repeated one or more times.
func Q1(choices ...Pattern) Pattern {
var pat Pattern
switch len(choices) {
case 0:
// Alt() <=> False
// Q1(False) <=> False
return False
case 1:
pat = choices[0]
default:
pat = Alt(choices...)
}
return &patternQualifierAtLeast{n: 1, pat: pat}
}
// Qn matches any of the given choices repeated at least n times.
//
// The paramenter n is required to be non-negative,
// or the negative value would be treated as zero.
func Qn(n int, choices ...Pattern) Pattern {
if n < 0 {
n = 0
}
var pat Pattern
switch len(choices) {
case 0:
// Alt() <=> False
// Qn(n = 0, False) <=> True
// Qn(n > 0, False) <=> False
if n == 0 {
return True
}
return False
case 1:
pat = choices[0]
default:
pat = Alt(choices...)
}
return &patternQualifierAtLeast{n: n, pat: pat}
}
// Q01 matches any of the given choices zero to one time.
func Q01(choices ...Pattern) Pattern {
var pat Pattern
switch len(choices) {
case 0:
// Alt() <=> False
// Q01(False) <=> True
return True
case 1:
pat = choices[0]
default:
pat = Alt(choices...)
}
return &patternQualifierOptional{pat}
}
// Q0n matches any of the given choices repeated at most n times.
//
// The paramenter n is required to be non-negative,
// or the negative value would be treated as zero.
func Q0n(n int, choices ...Pattern) Pattern {
if n < 0 {
n = 0
}
var pat Pattern
switch len(choices) {
case 0:
// Alt() <=> False
// Q0n(n, False) <=> True
return True
case 1:
pat = choices[0]
default:
pat = Alt(choices...)
}
switch n {
case 0:
return True
case 1:
return &patternQualifierOptional{pat}
default:
return &patternQualifierRange{m: 0, n: n, pat: pat}
}
}
// Qnn matches any of the given choices repeated exactly n times.
//
// The paramenter n is required to be non-negative,
// or the negative value would be treated as zero.
func Qnn(n int, choices ...Pattern) Pattern {
if n < 0 {
n = 0
}
var pat Pattern
switch len(choices) {
case 0:
// Alt() <=> False
// Qnn(n = 0, False) <=> True
// Qnn(n > 0, False) <=> False
if n == 0 {
return True
}
return False
case 1:
pat = choices[0]
default:
pat = Alt(choices...)
}
switch n {
case 0:
return True
case 1:
return pat
default:
return &patternQualifierRange{m: n, n: n, pat: pat}
}
}
// Qmn matches any of the given choices repeated from m to n times.
//
// The paramenter m and n are required to be non-negative,
// or the negative value would be treated as zero.
func Qmn(m, n int, choices ...Pattern) Pattern {
if n < 0 {
n = 0
}
if m < 0 {
m = 0
}
if m > n {
m, n = n, m
}
var pat Pattern
switch len(choices) {
case 0:
// Alt() <=> False
// Qmn(m = 0, n, False) <=> True
// Qmn(m > 0, n, False) <=> False
if m == 0 {
return True
}
return False
case 1:
pat = choices[0]
default:
pat = Alt(choices...)
}
if n == 0 {
return True
}
if m == 0 && n == 1 {
return &patternQualifierOptional{pat}
}
return &patternQualifierRange{m: m, n: n, pat: pat}
}
// J0 matches zero or more items separated by sep.
func J0(item, sep Pattern) Pattern {
return Jn(0, item, sep)
}
// J1 matches one or more items separated by sep.
func J1(item, sep Pattern) Pattern {
return Jn(1, item, sep)
}
// Jn matches at least n items separated by sep.
func Jn(n int, item, sep Pattern) Pattern {
if n < 0 {
n = 0
}
if n == 0 {
return Alt(
Seq(item, Q0(Seq(sep, item))),
True)
}
return Seq(item, Qn(n-1, Seq(sep, item)))
}
// J0n matches at most n items separated by sep.
func J0n(n int, item, sep Pattern) Pattern {
if n < 0 {
n = 0
}
return Jmn(0, n, item, sep)
}
// Jnn matches exactly n items separated by sep.
func Jnn(n int, item, sep Pattern) Pattern {
if n < 0 {
n = 0
}
switch n {
case 0:
return True
case 1:
return item
default:
return Seq(item, Qnn(n-1, Seq(sep, item)))
}
}
// Jmn matches m to n items separated by sep.
func Jmn(m, n int, item, sep Pattern) Pattern {
if m < 0 {
m = 0
}
if n < 0 {
n = 0
}
if m > n {
m, n = n, m
}
switch {
case n == 0:
return True
case m == 0:
return Alt(
Seq(item, Qmn(0, n-1, Seq(sep, item))),
True)
default:
return Seq(item, Qmn(m-1, n-1, Seq(sep, item)))
}
}
// Matches if all the sub-patterns match in order.
func (pat *patternSequence) match(ctx *context) error {
for ctx.locals.i < len(pat.pats) {
if !ctx.justReturned() {
return ctx.call(pat.pats[ctx.locals.i])
}
ret := ctx.ret
if !ret.ok {
return ctx.predicates(false)
}
ctx.consume(ret.n)
ctx.locals.i++
}
return ctx.commit()
}
// Matches if any sub-pattern matches, searches in order.
func (pat *patternAlternative) match(ctx *context) error {
for ctx.locals.i < len(pat.pats) {
if !ctx.justReturned() {
// optimize for the last choice
if ctx.locals.i == len(pat.pats)-1 {
return ctx.execute(pat.pats[ctx.locals.i])
}
return ctx.call(pat.pats[ctx.locals.i])
}
ret := ctx.ret
if ret.ok {
ctx.consume(ret.n)
return ctx.commit()
}
ctx.locals.i++
}
return ctx.predicates(false)
}
// Matches exactly n runes.
func (pat *patternSkip) match(ctx *context) error {
for i := 0; i < pat.n; i++ {
_, n := ctx.nextRune()
ctx.consume(n)
// no enough runes
if n == 0 {
return ctx.predicates(false)
}
}
return ctx.commit()
}
// Matches until pattern.
func (pat *patternAnyRuneUntil) match(ctx *context) error {
for {
if ctx.reachedRepeatLimit(ctx.locals.i) {
return errorReachedRepeatLimit
}
if !ctx.justReturned() {
return ctx.call(pat.pat)
}
ret := ctx.ret
if ret.ok {
// pattern searched
if !pat.without {
ctx.consume(ret.n)
}
return ctx.commit()
}
_, n := ctx.nextRune()
ctx.consume(n)
ctx.locals.i++
// search failed
if n == 0 {
return ctx.predicates(false)
}
}
}
// Matches at least n times.
func (pat *patternQualifierAtLeast) match(ctx *context) error {
for {
if ctx.reachedRepeatLimit(ctx.locals.i) {
return errorReachedRepeatLimit
}
if !ctx.justReturned() {
return ctx.call(pat.pat)
}
ret := ctx.ret
if !ret.ok {
if ctx.locals.i < pat.n {
return ctx.predicates(false)
}
return ctx.commit()
}
ctx.consume(ret.n)
ctx.locals.i++
}
}
// Matches zero or one times.
func (pat *patternQualifierOptional) match(ctx *context) error {
if !ctx.justReturned() {
return ctx.call(pat.pat)
}
ret := ctx.ret
if !ret.ok {
return ctx.predicates(true)
}
ctx.consume(ret.n)
return ctx.commit()
}
// Matches m to n times.
func (pat *patternQualifierRange) match(ctx *context) error {
for ctx.locals.i < pat.n {
if ctx.reachedRepeatLimit(ctx.locals.i) {
return errorReachedRepeatLimit
}
if !ctx.justReturned() {
return ctx.call(pat.pat)
}
ret := ctx.ret
if !ret.ok {
if ctx.locals.i < pat.m {
return ctx.predicates(false)
}
return ctx.commit()
}
ctx.consume(ret.n)
ctx.locals.i++
}
return ctx.commit()
}
func (pat *patternSequence) String() string {
strs := make([]string, len(pat.pats))
for i, pat := range pat.pats {
strs[i] = fmt.Sprint(pat)
}
return fmt.Sprintf("(%s)", strings.Join(strs, " "))
}
func (pat *patternAlternative) String() string {
strs := make([]string, len(pat.pats))
for i, pat := range pat.pats {
strs[i] = fmt.Sprint(pat)
}
return fmt.Sprintf("(%s)", strings.Join(strs, " | "))
}
func (pat *patternSkip) String() string {
return fmt.Sprintf("#dot <%d>", pat.n)
}
func (pat *patternAnyRuneUntil) String() string {
if pat.without {
return fmt.Sprintf("until(%s)", pat.pat)
}
return fmt.Sprintf("until_end_of(%s)", pat.pat)
}
func (pat *patternQualifierAtLeast) String() string {
switch pat.n {
case 0:
return fmt.Sprintf("%s *", pat.pat)
case 1:
return fmt.Sprintf("%s +", pat.pat)
default:
return fmt.Sprintf("%s <%d..>", pat.pat, pat.n)
}
}
func (pat *patternQualifierOptional) String() string {
return fmt.Sprintf("[ %s ]", pat.pat)
}
func (pat *patternQualifierRange) String() string {
if pat.m == pat.n {
return fmt.Sprintf("%s <%d>", pat.pat, pat.m)
}
return fmt.Sprintf("%s <%d..%d>", pat.pat, pat.m, pat.n)
}