rparsec 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/rparsec.rb +2 -2
- data/rparsec/context.rb +81 -66
- data/rparsec/error.rb +22 -19
- data/rparsec/expressions.rb +179 -136
- data/rparsec/functors.rb +270 -249
- data/rparsec/id_monad.rb +13 -11
- data/rparsec/keywords.rb +101 -89
- data/rparsec/locator.rb +36 -32
- data/rparsec/misc.rb +109 -102
- data/rparsec/monad.rb +57 -51
- data/rparsec/operators.rb +116 -109
- data/rparsec/parser.rb +892 -794
- data/rparsec/parser_monad.rb +19 -16
- data/rparsec/parsers.rb +614 -603
- data/rparsec/token.rb +23 -20
- data/test/src/simple_monad_test.rb +21 -21
- metadata +49 -41
data/rparsec/parser_monad.rb
CHANGED
@@ -1,17 +1,20 @@
|
|
1
|
-
# require 'rparsec/parser_impls'
|
2
|
-
class ParserMonad
|
3
|
-
def fail msg
|
4
|
-
FailureParser.new(msg)
|
5
|
-
end
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
1
|
+
# require 'rparsec/parser_impls'
|
2
|
+
class ParserMonad
|
3
|
+
def fail msg
|
4
|
+
FailureParser.new(msg)
|
5
|
+
end
|
6
|
+
|
7
|
+
def value v
|
8
|
+
return Nil if v.nil?
|
9
|
+
ValueParser.new(v);
|
10
|
+
end
|
11
|
+
|
12
|
+
def bind(v, &proc)
|
13
|
+
return v unless proc
|
14
|
+
BoundParser.new(v, proc);
|
15
|
+
end
|
16
|
+
|
17
|
+
def mplus(p1, p2)
|
18
|
+
PlusParser.new([p1,p2]);
|
19
|
+
end
|
17
20
|
end
|
data/rparsec/parsers.rb
CHANGED
@@ -1,604 +1,615 @@
|
|
1
|
-
require 'rparsec/parser'
|
2
|
-
|
3
|
-
class FailureParser < Parser
|
4
|
-
init :msg
|
5
|
-
def _parse ctxt
|
6
|
-
return ctxt.failure(@msg)
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
class ValueParser < Parser
|
11
|
-
init :value
|
12
|
-
def _parse ctxt
|
13
|
-
ctxt.retn @value
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
class LazyParser < Parser
|
18
|
-
init :block
|
19
|
-
def _parse ctxt
|
20
|
-
@block.call._parse ctxt
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def add_error(err, e)
|
25
|
-
return e if err.nil?
|
26
|
-
return err if e.nil?
|
27
|
-
cmp = compare_error(err, e)
|
28
|
-
return err if cmp > 0
|
29
|
-
return e if cmp < 0
|
30
|
-
err
|
31
|
-
# merge_error(err, e)
|
32
|
-
end
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
return 1 if e1.index
|
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
|
-
end
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
end
|
106
|
-
def
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
end
|
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
|
-
ctxt
|
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
|
-
end
|
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
|
-
end
|
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
|
-
end
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
ctxt.
|
313
|
-
|
314
|
-
end
|
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
|
-
new_ctxt = ParseContext.new(
|
423
|
-
return true if _run_parser parser, ctxt, new_ctxt
|
424
|
-
ctxt.index =
|
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
|
-
end
|
451
|
-
end
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
end
|
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
|
-
end
|
491
|
-
return
|
492
|
-
end
|
493
|
-
end
|
494
|
-
|
495
|
-
class
|
496
|
-
init :parser, :
|
497
|
-
def _parse ctxt
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
end
|
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
|
-
end
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
return
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
class
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
1
|
+
require 'rparsec/parser'
|
2
|
+
|
3
|
+
class FailureParser < Parser
|
4
|
+
init :msg
|
5
|
+
def _parse ctxt
|
6
|
+
return ctxt.failure(@msg)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class ValueParser < Parser
|
11
|
+
init :value
|
12
|
+
def _parse ctxt
|
13
|
+
ctxt.retn @value
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class LazyParser < Parser
|
18
|
+
init :block
|
19
|
+
def _parse ctxt
|
20
|
+
@block.call._parse ctxt
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_error(err, e)
|
25
|
+
return e if err.nil?
|
26
|
+
return err if e.nil?
|
27
|
+
cmp = compare_error(err, e)
|
28
|
+
return err if cmp > 0
|
29
|
+
return e if cmp < 0
|
30
|
+
err
|
31
|
+
# merge_error(err, e)
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_first_element(err)
|
35
|
+
while err.kind_of?(Array)
|
36
|
+
err = err[0]
|
37
|
+
end
|
38
|
+
err
|
39
|
+
end
|
40
|
+
|
41
|
+
def compare_error(e1, e2)
|
42
|
+
e1, e2 = get_first_element(e1), get_first_element(e2)
|
43
|
+
return -1 if e1.index < e2.index
|
44
|
+
return 1 if e1.index > e2.index
|
45
|
+
0
|
46
|
+
end
|
47
|
+
|
48
|
+
###############################################
|
49
|
+
#def merge_error(e1, e2)
|
50
|
+
# return e1 << e2 if e1.kind_of?(Array)
|
51
|
+
# [e1,e2]
|
52
|
+
#end
|
53
|
+
###############################################
|
54
|
+
class ThrowParser < Parser
|
55
|
+
init :symbol
|
56
|
+
def _parse ctxt
|
57
|
+
throw @symbol
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
class CatchParser < Parser
|
62
|
+
init :symbol, :parser
|
63
|
+
def _parse ctxt
|
64
|
+
interrupted = true
|
65
|
+
ok = false
|
66
|
+
catch @symbol do
|
67
|
+
ok = @parser._parse(ctxt)
|
68
|
+
interrupted = false
|
69
|
+
end
|
70
|
+
return ctxt.retn(@symbol) if interrupted
|
71
|
+
ok
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
class PeekParser < Parser
|
76
|
+
init :parser
|
77
|
+
def _parse ctxt
|
78
|
+
ind = ctxt.index
|
79
|
+
return false unless @parser._parse ctxt
|
80
|
+
ctxt.index = ind
|
81
|
+
return true
|
82
|
+
end
|
83
|
+
def peek
|
84
|
+
self
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
class AtomParser < Parser
|
89
|
+
init :parser
|
90
|
+
def _parse ctxt
|
91
|
+
ind = ctxt.index
|
92
|
+
return true if @parser._parse ctxt
|
93
|
+
ctxt.index = ind
|
94
|
+
return false
|
95
|
+
end
|
96
|
+
def atomize
|
97
|
+
self
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
class LookAheadSensitiveParser < Parser
|
102
|
+
def initialize(la=1)
|
103
|
+
super()
|
104
|
+
@lookahead = la
|
105
|
+
end
|
106
|
+
def visible(ctxt, n)
|
107
|
+
ctxt.index - n < @lookahead
|
108
|
+
end
|
109
|
+
def lookahead(n)
|
110
|
+
raise ArgumentError, "lookahead number #{n} should be positive" unless n>0
|
111
|
+
return self if n == @lookahead
|
112
|
+
withLookahead(n)
|
113
|
+
end
|
114
|
+
def not(msg="#{self} unexpected")
|
115
|
+
NotParser.new(self, msg, @lookahead)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
class NotParser < LookAheadSensitiveParser
|
120
|
+
def initialize(parser, msg, la=1)
|
121
|
+
super(la)
|
122
|
+
@parser, @msg, @name = parser, msg, "~#{parser.name}"
|
123
|
+
end
|
124
|
+
def _parse ctxt
|
125
|
+
ind = ctxt.index
|
126
|
+
if @parser._parse ctxt
|
127
|
+
ctxt.index = ind
|
128
|
+
return ctxt.expecting(@msg)
|
129
|
+
end
|
130
|
+
return ctxt.retn(nil) if visible(ctxt, ind)
|
131
|
+
return false
|
132
|
+
end
|
133
|
+
def withLookahead(n)
|
134
|
+
NotParser.new(@parser, @msg, n)
|
135
|
+
end
|
136
|
+
def not()
|
137
|
+
@parser
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
class ExpectParser < Parser
|
142
|
+
def initialize(parser, msg)
|
143
|
+
super()
|
144
|
+
@parser, @msg, @name = parser, msg, msg
|
145
|
+
end
|
146
|
+
def _parse ctxt
|
147
|
+
ind = ctxt.index
|
148
|
+
return true if @parser._parse ctxt
|
149
|
+
return false unless ind == ctxt.index
|
150
|
+
ctxt.expecting(@msg)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
class PlusParser < LookAheadSensitiveParser
|
155
|
+
def initialize(alts, la=1)
|
156
|
+
super(la)
|
157
|
+
@alts = alts
|
158
|
+
end
|
159
|
+
def _parse ctxt
|
160
|
+
ind, result, err = ctxt.index, ctxt.result, ctxt.error
|
161
|
+
for p in @alts
|
162
|
+
ctxt.reset_error
|
163
|
+
ctxt.index, ctxt.result = ind, result
|
164
|
+
return true if p._parse(ctxt)
|
165
|
+
return false unless visible(ctxt, ind)
|
166
|
+
err = add_error(err, ctxt.error)
|
167
|
+
end
|
168
|
+
ctxt.error = err
|
169
|
+
return false
|
170
|
+
end
|
171
|
+
def withLookahead(n)
|
172
|
+
PlusParser.new(@alts, n)
|
173
|
+
end
|
174
|
+
def plus other
|
175
|
+
PlusParser.new(@alts.dup << other, @lookahead).setName(name)
|
176
|
+
end
|
177
|
+
def_sig :plus, Parser
|
178
|
+
end
|
179
|
+
|
180
|
+
|
181
|
+
class AltParser < LookAheadSensitiveParser
|
182
|
+
def initialize(alts, la = 1)
|
183
|
+
super(la)
|
184
|
+
@alts, @lookahead = alts, la
|
185
|
+
end
|
186
|
+
def _parse ctxt
|
187
|
+
ind, result, err = ctxt.index, ctxt.result, ctxt.error
|
188
|
+
err_ind, err_pos = -1, -1
|
189
|
+
for p in @alts
|
190
|
+
ctxt.reset_error
|
191
|
+
ctxt.index, ctxt.result = ind, result
|
192
|
+
return true if p._parse(ctxt)
|
193
|
+
if ctxt.error.index > err_pos
|
194
|
+
err, err_ind, err_pos = ctxt.error, ctxt.index, ctxt.error.index
|
195
|
+
end
|
196
|
+
end
|
197
|
+
ctxt.index, ctxt.error = err_ind, err
|
198
|
+
return false
|
199
|
+
end
|
200
|
+
def withLookahead(n)
|
201
|
+
AltParser.new(@alts, n)
|
202
|
+
end
|
203
|
+
def | other
|
204
|
+
AltParser.new(@alts.dup << autobox_parser(other)).setName(name)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
|
209
|
+
class BestParser < Parser
|
210
|
+
init :alts, :longer
|
211
|
+
def _parse ctxt
|
212
|
+
best_result, best_ind = nil, -1
|
213
|
+
err_ind, err_pos = -1, -1
|
214
|
+
ind, result, err = ctxt.index, ctxt.result, ctxt.error
|
215
|
+
for p in @alts
|
216
|
+
ctxt.reset_error
|
217
|
+
ctxt.index, ctxt.result = ind, result
|
218
|
+
if p._parse(ctxt)
|
219
|
+
err, now_ind = nil, ctxt.index
|
220
|
+
if best_ind==-1 || now_ind != best_ind && @longer == (now_ind>best_ind)
|
221
|
+
best_result, best_ind = ctxt.result, now_ind
|
222
|
+
end
|
223
|
+
elsif best_ind < 0 # no good match found yet.
|
224
|
+
if ctxt.error.index > err_pos
|
225
|
+
err_ind, err_pos = ctxt.index, ctxt.error.index
|
226
|
+
end
|
227
|
+
err = add_error(err, ctxt.error)
|
228
|
+
end
|
229
|
+
end
|
230
|
+
if best_ind >= 0
|
231
|
+
ctxt.index = best_ind
|
232
|
+
return ctxt.retn(best_result)
|
233
|
+
else
|
234
|
+
ctxt.error, ctxt.index = err, err_ind
|
235
|
+
return false
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
class BoundParser < Parser
|
241
|
+
init :parser, :proc
|
242
|
+
def _parse ctxt
|
243
|
+
return false unless @parser._parse(ctxt)
|
244
|
+
@proc.call(ctxt.result)._parse ctxt
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
class BoundnParser < Parser
|
249
|
+
init :parser, :proc
|
250
|
+
def _parse ctxt
|
251
|
+
return false unless @parser._parse(ctxt)
|
252
|
+
@proc.call(*ctxt.result)._parse ctxt
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
class MapParser < Parser
|
257
|
+
init :parser, :proc
|
258
|
+
def _parse ctxt
|
259
|
+
return false unless @parser._parse(ctxt)
|
260
|
+
ctxt.result = @proc.call(ctxt.result)
|
261
|
+
true
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
class MapnParser < Parser
|
266
|
+
init :parser, :proc
|
267
|
+
def _parse ctxt
|
268
|
+
return false unless @parser._parse(ctxt)
|
269
|
+
ctxt.result = @proc.call(*ctxt.result)
|
270
|
+
true
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
class SequenceParser < Parser
|
275
|
+
init :parsers, :proc
|
276
|
+
def _parse ctxt
|
277
|
+
if @proc.nil?
|
278
|
+
for p in @parsers
|
279
|
+
return false unless p._parse(ctxt)
|
280
|
+
end
|
281
|
+
else
|
282
|
+
results = []
|
283
|
+
for p in @parsers
|
284
|
+
return false unless p._parse(ctxt)
|
285
|
+
results << ctxt.result
|
286
|
+
end
|
287
|
+
ctxt.retn(@proc.call(*results))
|
288
|
+
end
|
289
|
+
return true
|
290
|
+
end
|
291
|
+
def seq(other, &block)
|
292
|
+
# TypeChecker.check_arg_type Parser, other, :seq
|
293
|
+
SequenceParser.new(@parsers.dup << other, &block)
|
294
|
+
end
|
295
|
+
def_sig :seq, Parser
|
296
|
+
end
|
297
|
+
|
298
|
+
class FollowedParser < Parser
|
299
|
+
init :p1, :p2
|
300
|
+
def _parse ctxt
|
301
|
+
return false unless @p1._parse ctxt
|
302
|
+
result = ctxt.result
|
303
|
+
return false unless @p2._parse ctxt
|
304
|
+
ctxt.retn(result)
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
class SatisfiesParser < Parser
|
309
|
+
init :pred, :expected
|
310
|
+
def _parse ctxt
|
311
|
+
elem = nil
|
312
|
+
if ctxt.eof || !@pred.call(elem=ctxt.current)
|
313
|
+
return ctxt.expecting(@expected)
|
314
|
+
end
|
315
|
+
ctxt.next
|
316
|
+
ctxt.retn elem
|
317
|
+
end
|
318
|
+
end
|
319
|
+
|
320
|
+
class AnyParser < Parser
|
321
|
+
def _parse ctxt
|
322
|
+
return ctxt.expecting if ctxt.eof
|
323
|
+
result = ctxt.current
|
324
|
+
ctxt.next
|
325
|
+
ctxt.retn result
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|
329
|
+
class EofParser < Parser
|
330
|
+
init :msg
|
331
|
+
def _parse ctxt
|
332
|
+
return true if ctxt.eof
|
333
|
+
return ctxt.expecting(@msg)
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
class RegexpParser < Parser
|
338
|
+
init :ptn, :msg
|
339
|
+
def _parse ctxt
|
340
|
+
scanner = ctxt.scanner
|
341
|
+
result = scanner.check @ptn
|
342
|
+
if result.nil?
|
343
|
+
ctxt.expecting(@msg)
|
344
|
+
else
|
345
|
+
ctxt.advance(scanner.matched_size)
|
346
|
+
ctxt.retn(result)
|
347
|
+
end
|
348
|
+
end
|
349
|
+
end
|
350
|
+
|
351
|
+
class AreParser < Parser
|
352
|
+
init :vals, :msg
|
353
|
+
def _parse ctxt
|
354
|
+
if @vals.length > ctxt.available
|
355
|
+
return ctxt.expecting(@msg)
|
356
|
+
end
|
357
|
+
cur = 0
|
358
|
+
for cur in (0...@vals.length)
|
359
|
+
if @vals[cur] != ctxt.peek(cur)
|
360
|
+
return ctxt.expecting(@msg)
|
361
|
+
end
|
362
|
+
end
|
363
|
+
ctxt.advance(@vals.length)
|
364
|
+
ctxt.retn @vals
|
365
|
+
end
|
366
|
+
end
|
367
|
+
|
368
|
+
def downcase c
|
369
|
+
case when c >= ?A && c <=?Z then c + (?a - ?A) else c end
|
370
|
+
end
|
371
|
+
|
372
|
+
class StringCaseInsensitiveParser < Parser
|
373
|
+
init :str, :msg
|
374
|
+
def _parse ctxt
|
375
|
+
if @str.length > ctxt.available
|
376
|
+
return ctxt.expecting(@msg)
|
377
|
+
end
|
378
|
+
cur = 0
|
379
|
+
for cur in (0...@str.length)
|
380
|
+
if downcase(@str[cur]) != downcase(ctxt.peek(cur))
|
381
|
+
return ctxt.expecting(@msg)
|
382
|
+
end
|
383
|
+
end
|
384
|
+
result = ctxt.src[ctxt.index, @str.length]
|
385
|
+
ctxt.advance(@str.length)
|
386
|
+
ctxt.retn result
|
387
|
+
end
|
388
|
+
end
|
389
|
+
|
390
|
+
class FragmentParser < Parser
|
391
|
+
init :parser
|
392
|
+
def _parse ctxt
|
393
|
+
ind = ctxt.index
|
394
|
+
return false unless @parser._parse ctxt
|
395
|
+
ctxt.retn(ctxt.src[ind, ctxt.index-ind])
|
396
|
+
end
|
397
|
+
end
|
398
|
+
|
399
|
+
class TokenParser < Parser
|
400
|
+
init :symbol, :parser
|
401
|
+
def _parse ctxt
|
402
|
+
ind = ctxt.index
|
403
|
+
return false unless @parser._parse ctxt
|
404
|
+
raw = ctxt.result
|
405
|
+
raw = ctxt.src[ind, ctxt.index-ind] unless raw.kind_of? String
|
406
|
+
ctxt.retn(Token.new(@symbol, raw, ind))
|
407
|
+
end
|
408
|
+
end
|
409
|
+
|
410
|
+
class NestedParser < Parser
|
411
|
+
init :parser1, :parser2
|
412
|
+
def _parse ctxt
|
413
|
+
ind = ctxt.index
|
414
|
+
return false unless @parser1._parse ctxt
|
415
|
+
_run_nested(ind, ctxt, ctxt.result, @parser2)
|
416
|
+
end
|
417
|
+
private
|
418
|
+
def _run_nested(start, ctxt, src, parser)
|
419
|
+
ctxt.error = nil
|
420
|
+
new_ctxt = nil
|
421
|
+
if src.kind_of? String
|
422
|
+
new_ctxt = ParseContext.new(src)
|
423
|
+
return true if _run_parser parser, ctxt, new_ctxt
|
424
|
+
ctxt.index = start + new_ctxt.index
|
425
|
+
elsif src.kind_of? Array
|
426
|
+
new_ctxt = ParseContext.new(src)
|
427
|
+
return true if _run_parser parser, ctxt, new_ctxt
|
428
|
+
ctxt.index = start + _get_index(new_ctxt) unless new_ctxt.eof
|
429
|
+
else
|
430
|
+
new_ctxt = ParseContext.new([src])
|
431
|
+
return true if _run_parser parser, ctxt, new_ctxt
|
432
|
+
ctxt.index = ind unless new_ctxt.eof
|
433
|
+
end
|
434
|
+
ctxt.error.index = ctxt.index
|
435
|
+
false
|
436
|
+
end
|
437
|
+
def _get_index ctxt
|
438
|
+
cur = ctxt.current
|
439
|
+
return cur.index if cur.respond_to? :index
|
440
|
+
ctxt.index
|
441
|
+
end
|
442
|
+
def _run_parser parser, old_ctxt, new_ctxt
|
443
|
+
if parser._parse new_ctxt
|
444
|
+
old_ctxt.result = new_ctxt.result
|
445
|
+
true
|
446
|
+
else
|
447
|
+
old_ctxt.error = new_ctxt.error
|
448
|
+
false
|
449
|
+
end
|
450
|
+
end
|
451
|
+
end
|
452
|
+
|
453
|
+
class WatchParser < Parser
|
454
|
+
init :proc
|
455
|
+
def _parse ctxt
|
456
|
+
@proc.call(ctxt.result)
|
457
|
+
true
|
458
|
+
end
|
459
|
+
end
|
460
|
+
|
461
|
+
class WatchnParser < Parser
|
462
|
+
init :proc
|
463
|
+
def _parse ctxt
|
464
|
+
@proc.call(*ctxt.result)
|
465
|
+
true
|
466
|
+
end
|
467
|
+
end
|
468
|
+
|
469
|
+
class MapCurrentParser < Parser
|
470
|
+
init :proc
|
471
|
+
def _parse ctxt
|
472
|
+
ctxt.result = @proc.call(ctxt.result)
|
473
|
+
true
|
474
|
+
end
|
475
|
+
end
|
476
|
+
|
477
|
+
class MapnCurrentParser < Parser
|
478
|
+
init :proc
|
479
|
+
def _parse ctxt
|
480
|
+
ctxt.result = @proc.call(*ctxt.result)
|
481
|
+
true
|
482
|
+
end
|
483
|
+
end
|
484
|
+
|
485
|
+
class Repeat_Parser < Parser
|
486
|
+
init :parser, :times
|
487
|
+
def _parse ctxt
|
488
|
+
for i in (0...@times)
|
489
|
+
return false unless @parser._parse ctxt
|
490
|
+
end
|
491
|
+
return true
|
492
|
+
end
|
493
|
+
end
|
494
|
+
|
495
|
+
class RepeatParser < Parser
|
496
|
+
init :parser, :times
|
497
|
+
def _parse ctxt
|
498
|
+
result = []
|
499
|
+
for i in (0...@times)
|
500
|
+
return false unless @parser._parse ctxt
|
501
|
+
result << ctxt.result
|
502
|
+
end
|
503
|
+
return ctxt.retn(result)
|
504
|
+
end
|
505
|
+
end
|
506
|
+
|
507
|
+
class Many_Parser < Parser
|
508
|
+
init :parser, :least
|
509
|
+
def _parse ctxt
|
510
|
+
for i in (0...@least)
|
511
|
+
return false unless @parser._parse ctxt
|
512
|
+
end
|
513
|
+
while(true)
|
514
|
+
ind = ctxt.index
|
515
|
+
if @parser._parse ctxt
|
516
|
+
return true if ind==ctxt.index # infinite loop
|
517
|
+
next
|
518
|
+
end
|
519
|
+
return ind==ctxt.index
|
520
|
+
end
|
521
|
+
end
|
522
|
+
end
|
523
|
+
|
524
|
+
class ManyParser < Parser
|
525
|
+
init :parser, :least
|
526
|
+
def _parse ctxt
|
527
|
+
result = []
|
528
|
+
for i in (0...@least)
|
529
|
+
return false unless @parser._parse ctxt
|
530
|
+
result << ctxt.result
|
531
|
+
end
|
532
|
+
while(true)
|
533
|
+
ind = ctxt.index
|
534
|
+
if @parser._parse ctxt
|
535
|
+
result << ctxt.result
|
536
|
+
return ctxt.retn(result) if ind==ctxt.index # infinite loop
|
537
|
+
next
|
538
|
+
end
|
539
|
+
if ind==ctxt.index
|
540
|
+
return ctxt.retn(result)
|
541
|
+
else
|
542
|
+
return false
|
543
|
+
end
|
544
|
+
end
|
545
|
+
end
|
546
|
+
end
|
547
|
+
|
548
|
+
class Some_Parser < Parser
|
549
|
+
init :parser, :least, :max
|
550
|
+
def _parse ctxt
|
551
|
+
for i in (0...@least)
|
552
|
+
return false unless @parser._parse ctxt
|
553
|
+
end
|
554
|
+
for i in (@least...@max)
|
555
|
+
ind = ctxt.index
|
556
|
+
if @parser._parse ctxt
|
557
|
+
return true if ind==ctxt.index # infinite loop
|
558
|
+
next
|
559
|
+
end
|
560
|
+
return ind==ctxt.index
|
561
|
+
end
|
562
|
+
return true
|
563
|
+
end
|
564
|
+
end
|
565
|
+
|
566
|
+
class SomeParser < Parser
|
567
|
+
init :parser, :least, :max
|
568
|
+
def _parse ctxt
|
569
|
+
result = []
|
570
|
+
for i in (0...@least)
|
571
|
+
return false unless @parser._parse ctxt
|
572
|
+
result << ctxt.result
|
573
|
+
end
|
574
|
+
for i in (@least...@max)
|
575
|
+
ind = ctxt.index
|
576
|
+
if @parser._parse ctxt
|
577
|
+
result << ctxt.result
|
578
|
+
return ctxt.retn(result) if ind==ctxt.index # infinite loop
|
579
|
+
next
|
580
|
+
end
|
581
|
+
if ind==ctxt.index
|
582
|
+
return ctxt.retn(result)
|
583
|
+
else
|
584
|
+
return false
|
585
|
+
end
|
586
|
+
end
|
587
|
+
return ctxt.retn(result)
|
588
|
+
end
|
589
|
+
end
|
590
|
+
|
591
|
+
class OneParser < Parser
|
592
|
+
def _parse ctxt
|
593
|
+
true
|
594
|
+
end
|
595
|
+
end
|
596
|
+
|
597
|
+
class ZeroParser < Parser
|
598
|
+
def _parse ctxt
|
599
|
+
return ctxt.failure
|
600
|
+
end
|
601
|
+
end
|
602
|
+
|
603
|
+
class GetIndexParser < Parser
|
604
|
+
def _parse ctxt
|
605
|
+
ctxt.retn(ctxt.index)
|
606
|
+
end
|
607
|
+
end
|
608
|
+
class SetIndexParser < Parser
|
609
|
+
init :index
|
610
|
+
def _parse ctxt
|
611
|
+
ctxt.index = @index
|
612
|
+
end
|
613
|
+
end
|
614
|
+
|
604
615
|
Nil = ValueParser.new(nil)
|