/
Perl6Regex.pir
1899 lines (1617 loc) · 48.3 KB
/
Perl6Regex.pir
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
556
557
558
559
560
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2006-2009, Parrot Foundation.
=head1 DESCRIPTION
Perl6Regex - compiler and parser for Perl 6 regex
=over 4
=item C<compile_perl6regex(PMC source, PMC adverbs :slurpy :named)>
Return the result of compiling C<source> according to Perl 6
regex syntax and the associated C<adverbs>. Normally this
function is obtained using C<compreg 'PGE::Perl6Regex'> instead
of calling it directly.
Returns the compiled regular expression. If a C<target>
named parameter is supplied, then it will return the parse tree
(target='parse'), the expression tree (target='exp'),
or the resulting PIR code (target='PIR').
=cut
.namespace [ 'PGE';'Perl6Regex' ]
.sub 'compile_perl6regex'
.param pmc source
.param pmc args :slurpy
.param pmc adverbs :slurpy :named
unless null adverbs goto set_adverbs
adverbs = new 'Hash'
set_adverbs:
$I0 = exists adverbs['grammar']
if $I0 goto with_grammar
unless args goto adverb_grammar_1
$S0 = shift args
adverbs['grammar'] = $S0
goto with_grammar
adverb_grammar_1:
adverbs['grammar'] = 'PGE::Grammar'
with_grammar:
$I0 = exists adverbs['name']
if $I0 goto with_name
unless args goto with_name
$S0 = shift args
adverbs['name'] = $S0
with_name:
$I0 = exists adverbs['lang']
if $I0 goto with_lang
adverbs['lang'] = 'PIR'
with_lang:
$I0 = exists adverbs['ignorecase']
if $I0 goto with_ignorecase
$I0 = adverbs['i']
adverbs['ignorecase'] = $I0
with_ignorecase:
$I0 = exists adverbs['sigspace']
if $I0 goto with_sigspace
$I0 = exists adverbs['s']
if $I0 goto with_s
$I0 = exists adverbs['words']
if $I0 goto with_words
$I0 = adverbs['w']
adverbs['sigspace'] = $I0
goto with_sigspace
with_s:
$I0 = adverbs['s']
adverbs['sigspace'] = $I0
goto with_sigspace
with_words:
$I0 = adverbs['words']
adverbs['sigspace'] = $I0
with_sigspace:
.local string target
target = adverbs['target']
target = downcase target
## If we're passed the results of a previous parse, use it.
.local pmc match, exp
$I0 = isa source, ['PGE';'Match']
if $I0 == 0 goto parse
$P0 = source['expr']
if null $P0 goto parse
$I0 = isa $P0, ['PGE';'Exp']
if $I0 == 0 goto parse
match = source
goto analyze
parse:
## Let's parse the source as a regex
$P0 = get_global 'regex'
match = $P0(source, adverbs :flat :named)
if source == '' goto err_null
if target != 'parse' goto check
.return (match)
check:
unless match goto check_1
$S0 = source
$S1 = match
if $S0 == $S1 goto analyze
check_1:
null $P0
.return ($P0)
analyze:
.local pmc pad
exp = match['expr']
pad = clone adverbs
$P0 = new 'Hash'
pad['lexscope'] = $P0
exp = exp.'perl6exp'(pad)
if null exp goto err_null
.tailcall exp.'compile'(adverbs :flat :named)
err_null:
$I0 = match.'from'()
'parse_error'(match, $I0, 'Null pattern illegal')
.end
=item C<regex(PMC mob, PMC adverbs :slurpy :named)>
Parses a regex according to Perl 6 regex syntax, and returns
the corresponding parse tree.
=cut
.sub 'regex'
.param pmc mob
.param pmc adverbs :slurpy :named
.local string stop, tighter
.local pmc stopstack, optable, match
stopstack = get_global '@!stopstack'
optable = get_global '$optable'
stop = adverbs['stop']
tighter = adverbs['tighter']
push stopstack, stop
match = optable.'parse'(mob, 'stop'=>stop, 'tighter'=>tighter)
$S0 = pop stopstack
.return (match)
.end
=item C<p6escapes>
Parse and calculate various Perl 6 string escapes, such as \n, \r,
\x, \o, and \c. For the latter escapes, also handle the bracketed
forms and other special forms.
Note that this function is used directly by PCT::Grammar and Rakudo,
and someday may be refactored to a different location.
=cut
.sub 'trim'
.param string s
.local int rpos, lpos
rpos = length s
lpos = find_not_cclass .CCLASS_WHITESPACE, s, 0, rpos
rtrim_loop:
unless rpos > lpos goto rtrim_done
dec rpos
$I0 = is_cclass .CCLASS_WHITESPACE, s, rpos
if $I0 goto rtrim_loop
rtrim_done:
inc rpos
$I0 = rpos - lpos
$S0 = substr s, lpos, $I0
.return ($S0)
.end
.sub 'p6escapes'
.param pmc mob
.param pmc adverbs :slurpy :named
.local string target, backchar, literal
.local int pos, lastpos
$P0 = get_hll_global ['PGE'], '$!MATCH'
(mob, pos, target) = $P0.'new'(mob, adverbs :flat :named)
lastpos = length target
if pos >= lastpos goto fail
$S0 = substr target, pos, 1
inc pos
if $S0 != "\\" goto fail
if pos >= lastpos goto fail
backchar = substr target, pos, 1
inc pos
backchar = downcase backchar
$I0 = index "\\0abefnrtxco", backchar
if $I0 < 0 goto fail
if $I0 >= 9 goto scan_xco
literal = substr "\\\0\a\b\e\f\n\r\t", $I0, 1
goto succeed
scan_xco:
## Handle \x, \c, and \o escapes. Start by converting
## backchar into the appropriate radix, then loop through
## the characters that follow to compute the decimal value
## of codepoints, and concatenate the codepoints into a
## literal.
.local int base, decnum, isbracketed
base = index ' o c x', backchar
literal = ''
$S0 = substr target, pos, 1
isbracketed = iseq $S0, '['
pos += isbracketed
## Handle the case of \cC (control escape).
if base != 10 goto scan_xco_char
if isbracketed goto scan_xco_char
$I0 = is_cclass .CCLASS_NUMERIC, $S0, 0
if $I0 goto scan_xco_char
## xor the 64 bit
$I0 = ord $S0
bxor $I0, 64
literal = chr $I0
inc pos
goto succeed
scan_xco_char:
decnum = 0
# inside brackets, skip leading ws
unless isbracketed goto scan_xco_char_ws
pos = find_not_cclass .CCLASS_WHITESPACE, target, pos, lastpos
scan_xco_char_ws:
if base != 10 goto scan_xco_char_digits
unless isbracketed goto scan_xco_char_digits
$I0 = is_cclass .CCLASS_NUMERIC, target, pos
if $I0 goto scan_xco_char_digits
## look up character by name
.local int namepos
namepos = index target, ']', pos
if namepos < 0 goto err_missing_bracket
$I0 = index target, ',', pos
if $I0 < 0 goto have_namepos
if namepos < $I0 goto have_namepos
namepos = $I0
have_namepos:
$I0 = namepos - pos
$S0 = substr target, pos, $I0
$S0 = 'trim'($S0)
decnum = find_codepoint $S0
if decnum < 0 goto err_unicode_name
pos = namepos
goto scan_xco_char_end
scan_xco_char_digits:
$S0 = substr target, pos, 1
$I0 = index "0123456789abcdef0123456789ABCDEF", $S0
if $I0 < 0 goto scan_xco_char_end
$I0 %= 16
if $I0 >= base goto scan_xco_char_end
decnum *= base
decnum += $I0
inc pos
goto scan_xco_char_digits
scan_xco_char_end:
$S1 = chr decnum
literal = concat literal, $S1
unless isbracketed goto scan_xco_end
pos = find_not_cclass .CCLASS_WHITESPACE, target, pos, lastpos
$S0 = substr target, pos, 1
if $S0 == ']' goto scan_xco_end
if $S0 == '' goto err_missing_bracket
if $S0 != ',' goto err_digit
inc pos
goto scan_xco_char
scan_xco_end:
pos += isbracketed
succeed:
mob.'!make'(literal)
mob.'to'(pos)
fail:
.return (mob)
err_unicode_name:
$S0 = concat "Unrecognized character name ", $S0
'parse_error'(mob, pos, $S0)
err_missing_bracket:
'parse_error'(mob, pos, "Missing close bracket for \\x[...], \\o[...], or \\c[...]")
err_digit:
'parse_error'(mob, pos, "Invalid digit in \\x[...], \\o[...], or \\c[...]")
.end
=item C<onload()>
Initializes the Perl6Regex parser and other data structures
needed for compiling regexes.
=cut
.include 'cclass.pasm'
.namespace [ 'PGE';'Perl6Regex' ]
.sub '__onload' :load
.local pmc p6meta
p6meta = new 'P6metaclass'
p6meta.'new_class'('PGE::Exp::WS', 'parent'=>'PGE::Exp::Subrule')
p6meta.'new_class'('PGE::Exp::Alias', 'parent'=>'PGE::Exp')
.local pmc optable
optable = new ['PGE';'OPTable']
set_global '$optable', optable
$P0 = get_global 'parse_term'
optable.'newtok'('term:', 'precedence'=>'=', 'nows'=>1, 'parsed'=>$P0)
$P0 = get_global 'parse_term_ws'
optable.'newtok'('term:#', 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
$P0 = get_global 'parse_term_backslash'
optable.'newtok'("term:\\", 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
optable.'newtok'('term:^', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
optable.'newtok'('term:^^', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
optable.'newtok'('term:$$', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
optable.'newtok'('term:\b', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
optable.'newtok'('term:\B', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
optable.'newtok'('term:<<', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
optable.'newtok'('term:>>', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
optable.'newtok'('term:<?>', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
optable.'newtok'('term:<!>', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
optable.'newtok'(unicode:"term:\xab", 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
optable.'newtok'(unicode:"term:\xbb", 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Anchor')
optable.'newtok'('term:.', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
optable.'newtok'('term:\d', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
optable.'newtok'('term:\D', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
optable.'newtok'('term:\s', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
optable.'newtok'('term:\S', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
optable.'newtok'('term:\w', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
optable.'newtok'('term:\W', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
optable.'newtok'('term:\N', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CCShortcut')
optable.'newtok'('term:\n', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Newline')
$P0 = get_global 'parse_dollar'
optable.'newtok'('term:$', 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
$P0 = get_global 'parse_subrule'
optable.'newtok'('term:<', 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
optable.'newtok'('term:<?', 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
optable.'newtok'('term:<!', 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
optable.'newtok'('term:<.', 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
$P0 = get_global 'parse_enumcharclass'
optable.'newtok'('term:<[', 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
optable.'newtok'('term:<+', 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
optable.'newtok'('term:<-', 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
optable.'newtok'('term:<![', 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
$P0 = get_global 'parse_quoted_literal'
optable.'newtok'("term:'", 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
$P0 = get_global 'parse_goal'
optable.'newtok'('term:~', 'equiv'=>'term:', 'parsed'=>$P0)
optable.'newtok'('term:::', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Cut')
optable.'newtok'('term::::', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Cut')
optable.'newtok'('term:<cut>', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Cut')
optable.'newtok'('term:<commit>', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Cut')
$P0 = get_global 'parse_closure'
optable.'newtok'("term:{{", 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
optable.'newtok'("term:<?{{", 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
optable.'newtok'("term:<!{{", 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
$P0 = get_global 'parse_action'
optable.'newtok'("term:{*}", 'equiv'=>'term:', 'nows'=>1, 'parsed'=>$P0)
optable.'newtok'('circumfix:[ ]', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::Group')
optable.'newtok'('circumfix:( )', 'equiv'=>'term:', 'nows'=>1, 'match'=>'PGE::Exp::CGroup')
$P0 = get_global 'parse_quant'
optable.'newtok'('postfix:*', 'looser'=>'term:', 'parsed'=>$P0)
optable.'newtok'('postfix:+', 'equiv'=>'postfix:*', 'parsed'=>$P0)
optable.'newtok'('postfix:?', 'equiv'=>'postfix:*', 'parsed'=>$P0)
optable.'newtok'('postfix::', 'equiv'=>'postfix:*', 'parsed'=>$P0)
optable.'newtok'('postfix:**', 'equiv'=>'postfix:*', 'parsed'=>$P0)
$P0 = get_global 'parse_quant_error'
optable.'newtok'('term:*', 'equiv'=>'term:', 'parsed'=>$P0)
optable.'newtok'('term:+', 'equiv'=>'term:', 'parsed'=>$P0)
optable.'newtok'('term:?', 'equiv'=>'term:', 'parsed'=>$P0)
optable.'newtok'('infix:', 'looser'=>'postfix:*', 'assoc'=>'list', 'nows'=>1, 'match'=>'PGE::Exp::Concat')
optable.'newtok'('infix:&', 'looser'=>'infix:', 'nows'=>1, 'match'=>'PGE::Exp::Conj')
optable.'newtok'('infix:|', 'looser'=>'infix:&', 'nows'=>1, 'match'=>'PGE::Exp::Alt')
optable.'newtok'('prefix:|', 'equiv'=>'infix:|', 'nows'=>1, 'match'=>'PGE::Exp::Alt')
optable.'newtok'('infix:||', 'equiv'=>'infix:|', 'nows'=>1, 'match'=>'PGE::Exp::Alt')
optable.'newtok'('prefix:||', 'equiv'=>'infix:|', 'nows'=>1, 'match'=>'PGE::Exp::Alt')
optable.'newtok'('infix::=', 'tighter'=>'infix:', 'assoc'=>'right', 'match'=>'PGE::Exp::Alias')
optable.'newtok'('infix:=', 'tighter'=>'infix:', 'assoc'=>'right', 'match'=>'PGE::Exp::Alias')
$P0 = get_global 'parse_modifier'
optable.'newtok'('prefix::', 'looser'=>'infix:|', 'parsed'=>$P0)
optable.'newtok'('close:}', 'precedence'=>'<', 'nows'=>1)
.local pmc esclist
esclist = new 'Hash'
set_global '%esclist', esclist
esclist['e'] = "\e"
esclist['f'] = "\f"
esclist['r'] = "\r"
esclist['t'] = "\t"
esclist['v'] = unicode:"\x0a\x0b\x0c\x0d\x85\u2028\u2029"
esclist['h'] = unicode:"\x09\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000"
esclist['n'] = unicode:"\x0a\x0d\x0c\x85\u2028\u2029"
# See http://www.unicode.org/Public/UNIDATA/PropList.txt for above
# Create and store closure preprocessors in %closure_pp
$P0 = new 'Hash'
set_hll_global ['PGE';'Perl6Regex'], '%closure_pp', $P0
$P1 = get_hll_global ['PGE';'Perl6Regex'], 'PIR_closure'
$P0["PIR"] = $P1
# Create an array for holding stop tokens
$P0 = new 'ResizablePMCArray'
set_hll_global ['PGE';'Perl6Regex'], '@!stopstack', $P0
$P0 = get_global 'compile_perl6regex'
compreg 'PGE::Perl6Regex', $P0
.return ()
.end
=item C<parse_term(PMC mob [, PMC adverbs :slurpy :named])>
Parses literal strings and whitespace.
Return a failed match if the stoptoken is found.
=cut
.sub 'parse_term'
.param pmc mob
.param pmc adverbs :slurpy :named
.local string target
.local int pos, lastpos
$P0 = getattribute mob, '$.target'
target = $P0
$P0 = getattribute mob, '$.pos'
pos = $P0
lastpos = length target
.local string stop
$P0 = get_hll_global ['PGE';'Perl6Regex'], '@!stopstack'
stop = $P0[-1]
$I0 = is_cclass .CCLASS_WHITESPACE, target, pos
if $I0 goto term_ws
$I0 = length stop
if $I0 == 0 goto not_stop
$S0 = substr target, pos, $I0
if $S0 == stop goto end_noterm
not_stop:
## find length of word character sequence
.local int litlen
$I0 = find_not_cclass .CCLASS_WORD, target, pos, lastpos
litlen = $I0 - pos
## if we didn't find any, return no term
if litlen == 0 goto end_noterm
## for multi-char unquoted literals, leave the last character
## in case it's quantified (it gets processed as a subsequent term)
if litlen < 2 goto term_literal
dec litlen
term_literal:
$S0 = substr target, pos, litlen
pos += litlen
mob = mob.'new'(mob, 'grammar'=>'PGE::Exp::Literal')
mob.'!make'($S0)
mob.'to'(pos)
.return (mob)
term_ws:
.tailcall 'parse_term_ws'(mob)
end_noterm:
$S0 = substr target, pos, 1
if $S0 == ':' goto err_cut
(mob) = mob.'new'(mob, 'grammar'=>'PGE::Exp::Literal')
.return (mob)
err_cut:
'parse_error'(mob, pos, 'Quantifier follows nothing in regex')
.return (mob)
.end
=item C<parse_term_backslash(mob [, adverbs :slurpy :named])>
Parses terms beginning with backslash.
=cut
.sub 'parse_term_backslash'
.param pmc mob
.param pmc adverbs :slurpy :named
.local string target
.local int pos, lastpos, isnegated
$P0 = getattribute mob, '$.target'
target = $P0
$P0 = getattribute mob, '$.pos'
pos = $P0
lastpos = length target
isnegated = 0
.local string backchar, charlist
## get whatever follows the backslash
backchar = substr target, pos, 1
charlist = backchar
inc pos
## if it's not a word character, it's a quoted metachar
$I0 = is_cclass .CCLASS_WORD, backchar, 0
unless $I0 goto term_literal
## if it's a word character, it may be negated
isnegated = is_cclass .CCLASS_UPPERCASE, backchar, 0
## $S0 = downcase charlist
$I0 = ord backchar
$S0 = chr $I0
backchar = downcase $S0
## if it's \x, \c, or \o, parse as string escape
$I0 = index 'xco', backchar
if $I0 < 0 goto meta_esclist
meta_xco:
$I0 = pos - 2
$P0 = 'p6escapes'(mob, 'pos' => $I0)
unless $P0 goto err_xcoparse
pos = $P0.'to'()
charlist = $P0.'ast'()
unless isnegated goto term_literal
$I0 = length charlist
if $I0 > 1 goto err_negated_brackets
goto term_charlist
meta_esclist:
$P0 = get_global '%esclist'
$I0 = exists $P0[backchar]
unless $I0 goto err_reserved_metachar
charlist = $P0[backchar]
if isnegated goto term_charlist
$I0 = length charlist
if $I0 > 1 goto term_charlist
term_literal:
mob = mob.'new'(mob, 'grammar'=>'PGE::Exp::Literal')
mob.'!make'(charlist)
mob.'to'(pos)
.return (mob)
term_charlist:
mob = mob.'new'(mob, 'grammar'=>'PGE::Exp::EnumCharList')
mob.'!make'(charlist)
mob['isnegated'] = isnegated
mob.'to'(pos)
.return (mob)
err_xcoparse:
parse_error(mob, pos, 'Unable to parse \x, \c, or \o value')
err_negated_brackets:
pos = mob.'from'()
parse_error(mob, pos, 'Cannot use comma in \\X[...] or \\O[...]')
err_reserved_metachar:
parse_error(mob, pos, 'Alphanumeric metacharacters are reserved')
.end
=item C<parse_term_ws(PMC mob)>
Parses a whitespace term.
=cut
.sub 'parse_term_ws'
.param pmc mob
.param pmc adverbs :slurpy :named
.local string target
.local int pos, lastpos
(mob, pos, target) = mob.'new'(mob, 'grammar'=>'PGE::Exp::WS')
lastpos = length target
term_ws_loop:
## scan for the next non-whitespace character
pos = find_not_cclass .CCLASS_WHITESPACE, target, pos, lastpos
$S0 = substr target, pos, 1
if $S0 != '#' goto end
## we have a #-comment, determine its closing delimiter
inc pos
.local string closedelim
closedelim = "\n"
$S0 = substr target, pos, 1
$I0 = index '<[{(', $S0
if $I0 < 0 goto term_ws_loop_1
closedelim = substr '>]})', $I0, 1
term_ws_loop_1:
$I0 = index target, closedelim, pos
pos = $I0 + 1
if pos > 0 goto term_ws_loop
pos = lastpos
end:
mob.'to'(pos)
.return (mob)
.end
=item C<parse_quant(PMC mob)>
Parses a quantifier, such as *, +, ?, :, and all of their wondrous
combinations.
=cut
.sub 'parse_quant'
.param pmc mob
.param pmc adverbs :slurpy :named
.local string target
.local pmc key
.local int pos, lastpos
key = mob['KEY']
(mob, pos, target) = mob.'new'(mob, 'grammar'=>'PGE::Exp::Quant')
lastpos = length target
.local int min, max, suffixpos, sepws
.local string suffix
min = 1
max = 1
sepws = is_cclass .CCLASS_WHITESPACE, target, pos
suffixpos = find_not_cclass .CCLASS_WHITESPACE, target, pos, lastpos
if key == '**' goto quant_suffix
if key == ':' goto quant_cut
if key == '+' goto quant_max
## quantifier is '?' or '*'
min = 0
quant_max:
if key == '?' goto quant_suffix
## quantifier is '+' or '*'
max = PGE_INF
goto quant_suffix
quant_cut:
# The postfix:<:> operator may bring us here when it's really a
# term:<::> term. So, we check for that here and fail this match
# if we really have a cut term.
if key != ':' goto quant_suffix
$S0 = substr target, pos, 1
if $S0 == ':' goto end
mob['backtrack'] = PGE_BACKTRACK_NONE
quant_suffix:
suffix = substr target, suffixpos, 2
if suffix == ':?' goto quant_eager
if suffix == ':!' goto quant_greedy
quant_suffix_1:
suffix = substr target, suffixpos, 1
if suffix == '?' goto quant_eager
if suffix == '!' goto quant_greedy
if suffix != ':' goto quant
quant_none:
mob['backtrack'] = PGE_BACKTRACK_NONE
goto quant_skip_suffix
quant_eager:
mob['backtrack'] = PGE_BACKTRACK_EAGER
goto quant_skip_suffix
quant_greedy:
mob['backtrack'] = PGE_BACKTRACK_GREEDY
quant_skip_suffix:
$I0 = length suffix
pos = suffixpos + $I0
quant:
if key != '**' goto quant_set
quant_closure:
$I0 = is_cclass .CCLASS_WHITESPACE, target, pos
sepws |= $I0
pos = find_not_cclass .CCLASS_WHITESPACE, target, pos, lastpos
.local int isconst
isconst = is_cclass .CCLASS_NUMERIC, target, pos
if isconst goto brace_skip
$S0 = substr target, pos, 1
if $S0 != "{" goto parse_repetition_controller
inc pos
brace_skip:
$I1 = find_not_cclass .CCLASS_NUMERIC, target, pos, lastpos
if $I1 <= pos goto err_closure
$S0 = substr target, pos
min = $S0
max = $S0
pos = $I1
$S0 = substr target, pos, 2
if $S0 != '..' goto quant_closure_end
pos += 2
max = PGE_INF
$S0 = substr target, pos, 1
if $S0 != '*' goto quant_range_end
inc pos
goto quant_closure_end
quant_range_end:
$I1 = find_not_cclass .CCLASS_NUMERIC, target, pos, lastpos
if $I1 <= pos goto err_closure
$S0 = substr target, pos
max = $S0
pos = $I1
quant_closure_end:
if isconst goto brace_skip2
$S0 = substr target, pos, 1
if $S0 != "}" goto err_closure
inc pos
brace_skip2:
suffixpos = find_not_cclass .CCLASS_WHITESPACE, target, pos, lastpos
quant_set:
mob['min'] = min
mob['max'] = max
mob.'to'(pos)
end:
.return (mob)
parse_repetition_controller:
.local pmc regex, repetition_controller
mob.'to'(pos)
regex = get_global 'regex'
#parse everything down to concatenation precedence
repetition_controller = regex(mob, 'tighter'=>'infix:')
unless repetition_controller goto err_repetition_controller
#update pos to after the matched
pos = repetition_controller.'to'()
repetition_controller = repetition_controller['expr']
# if there's surrounding ws, then add WS nodes
unless sepws goto sepws_done
$P0 = mob.'new'(mob, 'grammar'=>'PGE::Exp::Concat')
$P0.'to'(pos)
$P1 = mob.'new'(mob, 'grammar'=>'PGE::Exp::WS')
$P1.'to'(pos)
push $P0, $P1
push $P0, repetition_controller
$P1 = mob.'new'(mob, 'grammar'=>'PGE::Exp::WS')
$P1.'to'(pos)
push $P0, $P1
repetition_controller = $P0
sepws_done:
#save the matched in the mob as sep
mob['sep'] = repetition_controller
#force the match to be 1..Inf
mob['min'] = 1
mob['max'] = PGE_INF
#move position to after the matched
mob.'to'(pos)
.return (mob)
err_repetition_controller:
'parse_error'(mob, pos, "Error in repetition controller")
err_closure:
'parse_error'(mob, pos, "Error in closure quantifier")
.end
=item C<parse_quant_error(mob)>
Throw an exception for quantifiers in term position.
=cut
.sub 'parse_quant_error'
.param pmc mob
.param pmc adverbs :slurpy :named
.local int pos
pos = mob.'to'()
'parse_error'(mob, pos, "Quantifier follows nothing in regex")
.end
=item C<parse_dollar(PMC mob)>
Parse things that begin with a dollar sign, such as scalars,
anchors, and match subscripts.
=cut
.sub "parse_dollar"
.param pmc mob
.param pmc adverbs :slurpy :named
.local string target
.local int pos, lastpos
.local string cname
$P0 = getattribute mob, '$.target'
target = $P0
$P0 = getattribute mob, '$.pos'
pos = $P0
lastpos = length target
$S0 = substr target, pos, 1
if $S0 == '<' goto name
$I0 = find_not_cclass .CCLASS_NUMERIC, target, pos, lastpos
if $I0 > pos goto numeric
$I0 = find_not_cclass .CCLASS_WORD, target, pos, lastpos
if $I0 > pos goto scalar
eos_anchor:
mob = mob.'new'(mob, 'grammar'=>'PGE::Exp::Anchor')
mob.'to'(pos)
.return (mob)
scalar:
mob = mob.'new'(mob, 'grammar'=>'PGE::Exp::Scalar')
dec pos
$I1 = $I0 - pos
cname = substr target, pos, $I1
cname = concat '"', cname
cname = concat cname, '"'
mob["cname"] = cname
mob.'to'($I0)
.return (mob)
numeric:
mob = mob.'new'(mob, 'grammar'=>'PGE::Exp::Scalar')
$I1 = $I0 - pos
cname = substr target, pos, $I1
mob["cname"] = cname
mob.'to'($I0)
.return (mob)
name:
inc pos
mob = mob.'new'(mob, 'grammar'=>'PGE::Exp::Scalar')
$I0 = index target, ">", pos
if $I0 < pos goto err_close
name_1:
$I1 = $I0 - pos
cname = substr target, pos, $I1
cname = escape cname
cname = concat '"', cname
cname = concat cname, '"'
mob["cname"] = cname
pos = $I0 + 1
mob.'to'(pos)
.return (mob)
err_close:
parse_error(mob, pos, "Missing close '>' in scalar")
.return (mob)
.end
=item C<parse_subname(STR target, INT pos)>
Scan C<target> starting at C<pos> looking for a subrule name
(following Perl 6's identifier syntax). Returns any subrule
name found, and the ending position of the name.
=cut
.sub 'parse_subname'
.param string target
.param int pos
.local int startpos, targetlen
targetlen = length target
startpos = pos
$I0 = pos
loop:
$I1 = find_not_cclass .CCLASS_WORD, target, $I0, targetlen
if $I1 == $I0 goto end
pos = $I1
$S0 = substr target, pos, 2
if $S0 != '::' goto end
$I0 = pos + 2
goto loop
end:
$I0 = pos - startpos
$S0 = substr target, startpos, $I0
.return ($S0, pos)
.end
=item C<parse_subrule(PMC mob)>
Parses a subrule token.
=cut
.sub 'parse_subrule'
.param pmc mob
.param pmc adverbs :slurpy :named
.local string target
.local pmc mobsave
.local int pos, lastpos
.local string key
key = mob['KEY']
mobsave = mob
(mob, pos, target) = mob.'new'(mob, 'grammar'=>'PGE::Exp::Subrule')
lastpos = length target
## default to non-capturing rule
.local int iscapture
iscapture = 0
## see what type of subrule this is
if key == '<.' goto scan_subname
if key == '<?' goto zerowidth
if key == '<!' goto negated
## capturing subrule, get its name/alias
iscapture = 1
.local string subname, cname
(subname, pos) = 'parse_subname'(target, pos)
cname = subname
$S0 = substr target, pos, 1
unless $S0 == '=' goto subrule_arg
## aliased subrule, skip the '=' and get the real name
inc pos
goto scan_subname
negated:
mob['isnegated'] = 1
zerowidth:
mob['iszerowidth'] = 1
scan_subname:
(subname, pos) = 'parse_subname'(target, pos)
subrule_arg:
mob['subname'] = subname
$S0 = substr target, pos, 1
if $S0 == ':' goto subrule_text_arg
if $S0 != ' ' goto subrule_end
subrule_pattern_arg:
inc pos
mob.'to'(pos)
.local pmc regex
regex = get_global 'regex'
$P1 = regex(mob, 'stop'=>'>')
unless $P1 goto end
$S0 = $P1
mob['arg'] = $S0
pos = $P1.'to'()
mob.'to'(-1)
goto subrule_end
subrule_text_arg:
$I0 = pos + 1
pos = find_not_cclass .CCLASS_WHITESPACE, target, $I0, lastpos
if pos == $I0 goto end
if pos >= lastpos goto end
.local string textarg, closedelim
textarg = ''
closedelim = '>'
$S0 = substr target, pos, 1
if $S0 == '"' goto subrule_text_quote
if $S0 != "'" goto subrule_text_loop
subrule_text_quote:
closedelim = $S0
inc pos
subrule_text_loop:
if pos >= lastpos goto end
$S0 = substr target, pos, 1
if $S0 == closedelim goto subrule_text_end
if $S0 != "\\" goto subrule_text_add
inc pos
$S0 = substr target, pos, 1
if $S0 == closedelim goto subrule_text_add
if $S0 == "\\" goto subrule_text_add
textarg .= "\\"
subrule_text_add:
textarg .= $S0
inc pos
goto subrule_text_loop
subrule_text_end:
mob['arg'] = textarg
if closedelim == '>' goto subrule_end
inc pos
pos = find_not_cclass .CCLASS_WHITESPACE, target, pos, lastpos
subrule_end:
$S0 = substr target, pos, 1
if $S0 != '>' goto end
inc pos
mob.'to'(pos)
mob['iscapture'] = iscapture
unless iscapture goto end
$S0 = escape cname
$S0 = concat '"', $S0