This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
/
Compiler-Regex.pir
996 lines (789 loc) · 26.5 KB
/
Compiler-Regex.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
# $Id$
=head1 NAME
PAST::Compiler-Regex - Compiler for PAST::Regex nodes
=head1 DESCRIPTION
PAST::Compiler-Regex implements the transformations to convert
PAST::Regex nodes into POST. It's still a part of PAST::Compiler;
we've separated out the regex-specific transformations here for
better code management and debugging.
=head2 Compiler methods
=head3 C<PAST::Regex>
=over 4
=item as_post(PAST::Regex node)
Return the POST representation of the regex AST rooted by C<node>.
=cut
.include 'cclass.pasm'
.include 'src/Regex/constants.pir'
.namespace ['PAST';'Compiler']
.sub 'as_post' :method :multi(_, ['PAST';'Regex'])
.param pmc node
.param pmc options :slurpy :named
.local pmc ops
ops = self.'post_new'('Ops', 'node'=>node)
.local pmc reghash
reghash = new ['Hash']
.lex '$*REG', reghash
.local pmc regexname
$P0 = get_global '@?BLOCK'
$P1 = $P0[0]
$S0 = $P1.'name'()
regexname = box $S0
.lex '$*REGEXNAME', regexname
.local string prefix, rname, rtype
prefix = self.'unique'('rx')
concat prefix, '_'
$P0 = split ' ', 'tgt string pos int off int eos int rep int cur pmc'
$P1 = iter $P0
iter_loop:
unless $P1 goto iter_done
rname = shift $P1
rtype = shift $P1
$S1 = concat prefix, rname
reghash[rname] = $S1
$S2 = concat '.local ', rtype
ops.'push_pirop'($S2, $S1)
goto iter_loop
iter_done:
.local pmc startlabel, donelabel, faillabel
$S0 = concat prefix, 'start'
startlabel = self.'post_new'('Label', 'result'=>$S0)
$S0 = concat prefix, 'done'
donelabel = self.'post_new'('Label', 'result'=>$S0)
$S0 = concat prefix, 'fail'
faillabel = self.'post_new'('Label', 'result'=>$S0)
reghash['fail'] = faillabel
# If capnames is available, it's a hash where each key is the
# name of a potential subcapture and the value is greater than 1
# if it's to be an array. This builds a list of arrayed subcaptures
# for use by "!cursor_caparray" below.
.local pmc capnames, capnames_it, caparray
capnames = node.'capnames'()
caparray = box 0
unless capnames goto capnames_done
capnames_it = iter capnames
caparray = new ['ResizablePMCArray']
capnames_loop:
unless capnames_it goto capnames_done
$S0 = shift capnames_it
$I0 = capnames[$S0]
unless $I0 > 1 goto capnames_loop
$S0 = self.'escape'($S0)
push caparray, $S0
goto capnames_loop
capnames_done:
.local string cur, rep, pos, tgt, off, eos
(cur, rep, pos, tgt, off, eos) = self.'!rxregs'('cur rep pos tgt off eos')
$S0 = concat '(', cur
concat $S0, ', '
concat $S0, pos
concat $S0, ', '
concat $S0, tgt
concat $S0, ', $I10)'
ops.'push_pirop'('callmethod', '"!cursor_start"', 'self', 'result'=>$S0)
unless caparray goto caparray_skip
self.'!cursorop'(ops, '!cursor_caparray', 0, caparray :flat)
caparray_skip:
ops.'push_pirop'('.lex', 'unicode:"$\x{a2}"', cur)
ops.'push_pirop'('length', eos, tgt, 'result'=>eos)
# On Parrot, indexing into variable-width encoded strings
# (such as utf8) becomes much more expensive as we move
# farther away from the beginning of the string (via calls
# to utf8_skip_forward). For regexes that are starting a match
# at a position other than the beginning of the string (e.g.,
# a subrule call), we can save a lot of useless scanning work
# in utf8_skip_forward by removing the first C<off = from-1>
# characters from the target and then performing all indexed
# operations on the resulting target relative to C<off>.
ops.'push_pirop'('set', off, 0)
ops.'push_pirop'('lt', '$I10', 2, startlabel)
ops.'push_pirop'('sub', off, '$I10', 1, 'result'=>off)
ops.'push_pirop'('substr', tgt, tgt, off, 'result'=>tgt)
ops.'push'(startlabel)
$P0 = self.'post_regex'(node)
ops.'push'($P0)
ops.'push'(faillabel)
self.'!cursorop'(ops, '!mark_fail', 4, rep, pos, '$I10', '$P10', 0)
ops.'push_pirop'('lt', pos, CURSOR_FAIL, donelabel)
ops.'push_pirop'('eq', pos, CURSOR_FAIL, faillabel)
ops.'push_pirop'('jump', '$I10')
ops.'push'(donelabel)
self.'!cursorop'(ops, '!cursor_fail', 0)
ops.'push_pirop'('return', cur)
.return (ops)
.end
=item !cursorop(ops, func, retelems, arg :slurpy)
Helper function to push POST nodes onto C<ops> that perform C<func>
on the regex's current cursor. By default this ends up being a method
call on the cursor, but some values of C<func> can result in inlined
code to perform the equivalent operation without using the method call.
The C<retelems> argument is the number of elements in C<arg> that
represent return values from the function; any remaining elements in arg
are passed to the function as input arguments.
=cut
.sub '!cursorop' :method
.param pmc ops
.param string func
.param int retelems
.param pmc args :slurpy
if retelems < 1 goto result_done
.local pmc retargs
retargs = new ['ResizableStringArray']
$I0 = retelems
retargs_loop:
unless $I0 > 0 goto retargs_done
$S0 = shift args
push retargs, $S0
dec $I0
goto retargs_loop
retargs_done:
.local string result
result = join ', ', retargs
result = concat '(', result
concat result, ')'
result_done:
.local pmc cur
cur = self.'!rxregs'('cur')
$S0 = self.'escape'(func)
$P0 = ops.'push_pirop'('callmethod', $S0, cur, args :flat)
if retelems < 1 goto done
$P0.'result'(result)
done:
.return (ops)
.end
=item !rxregs(keystr)
Helper function -- looks up the current regex register table
in the dynamic scope and returns a slice based on the keys
given in C<keystr>.
=cut
.sub '!rxregs' :method
.param string keystr
.local pmc keys, reghash, vals
keys = split ' ', keystr
reghash = find_dynamic_lex '$*REG'
vals = new ['ResizablePMCArray']
keys_loop:
unless keys goto keys_done
$S0 = shift keys
$P0 = reghash[$S0]
push vals, $P0
goto keys_loop
keys_done:
.return (vals :flat)
.end
=item post_regex(PAST::Regex node)
Return the POST representation of the regex component given by C<node>.
Normally this is handled by redispatching to a method corresponding to
the node's "pasttype" and "backtrack" attributes. If no "pasttype" is
given, then "concat" is assumed.
=cut
.sub 'post_regex' :method :multi(_, ['PAST';'Regex'])
.param pmc node
.param string cur :optional
.param int have_cur :opt_flag
.local string pasttype
pasttype = node.'pasttype'()
if pasttype goto have_pasttype
pasttype = 'concat'
have_pasttype:
$P0 = find_method self, pasttype
$P1 = self.$P0(node)
unless have_cur goto done
$S0 = $P1.'result'()
if $S0 == cur goto done
$P1 = self.'coerce'($P1, cur)
done:
.return ($P1)
.end
.sub 'post_regex' :method :multi(_, _)
.param pmc node
.param string cur :optional
.param int have_cur :opt_flag
$P0 = self.'as_post'(node)
unless have_cur goto done
$P0 = self.'coerce'($P0, cur)
done:
.return ($P0)
.end
=item alt(PAST::Regex node)
=cut
.sub 'alt' :method :multi(_, ['PAST';'Regex'])
.param pmc node
.local pmc cur, pos
(cur, pos) = self.'!rxregs'('cur pos')
.local string name
name = self.'unique'('alt')
concat name, '_'
.local pmc ops, iter
ops = self.'post_new'('Ops', 'node'=>node, 'result'=>cur)
iter = node.'iterator'()
unless iter goto done
.local int acount
.local pmc alabel, endlabel
acount = 0
$S0 = acount
$S0 = concat name, $S0
alabel = self.'post_new'('Label', 'result'=>$S0)
$S0 = concat name, 'end'
endlabel = self.'post_new'('Label', 'result'=>$S0)
iter_loop:
ops.'push'(alabel)
.local pmc apast, apost
apast = shift iter
apost = self.'post_regex'(apast, cur)
unless iter goto iter_done
inc acount
$S0 = acount
$S0 = concat name, $S0
alabel = self.'post_new'('Label', 'result'=>$S0)
ops.'push_pirop'('set_addr', '$I10', alabel)
self.'!cursorop'(ops, '!mark_push', 0, 0, pos, '$I10')
ops.'push'(apost)
ops.'push_pirop'('goto', endlabel)
goto iter_loop
iter_done:
ops.'push'(apost)
ops.'push'(endlabel)
done:
.return (ops)
.end
=item alt_longest(PAST::Regex node)
Same as 'alt' above, but use declarative/LTM semantics.
(Currently we cheat and just use 'alt' above.)
=cut
.sub 'alt_longest' :method
.param pmc node
.tailcall self.'alt'(node)
.end
=item anchor(PAST::Regex node)
Match various anchor points, including ^, ^^, $, $$.
=cut
.sub 'anchor' :method :multi(_, ['PAST';'Regex'])
.param pmc node
.local pmc cur, tgt, pos, off, eos, fail, ops
(cur, tgt, pos, off, eos, fail) = self.'!rxregs'('cur tgt pos off eos fail')
ops = self.'post_new'('Ops', 'node'=>node, 'result'=>cur)
.local string subtype
subtype = node.'subtype'()
ops.'push_pirop'('inline', subtype, 'inline'=>' # rxanchor %0')
if subtype == 'null' goto done
if subtype == 'fail' goto anchor_fail
if subtype == 'bos' goto anchor_bos
if subtype == 'eos' goto anchor_eos
if subtype == 'lwb' goto anchor_lwb
if subtype == 'rwb' goto anchor_rwb
.local pmc donelabel
$S0 = self.'unique'('rxanchor')
concat $S0, '_done'
donelabel = self.'post_new'('Label', 'result'=>$S0)
if subtype == 'bol' goto anchor_bol
if subtype == 'eol' goto anchor_eol
self.'panic'('Unrecognized subtype "', subtype, '" in PAST::Regex anchor node')
anchor_fail:
ops.'push_pirop'('goto', fail)
goto done
anchor_bos:
ops.'push_pirop'('ne', pos, 0, fail)
goto done
anchor_eos:
ops.'push_pirop'('ne', pos, eos, fail)
goto done
anchor_bol:
ops.'push_pirop'('eq', pos, 0, donelabel)
ops.'push_pirop'('ge', pos, eos, fail)
ops.'push_pirop'('sub', '$I10', pos, off)
ops.'push_pirop'('dec', '$I10')
ops.'push_pirop'('is_cclass', '$I11', .CCLASS_NEWLINE, tgt, '$I10')
ops.'push_pirop'('unless', '$I11', fail)
ops.'push'(donelabel)
goto done
anchor_eol:
ops.'push_pirop'('sub', '$I10', pos, off)
ops.'push_pirop'('is_cclass', '$I11', .CCLASS_NEWLINE, tgt, '$I10')
ops.'push_pirop'('if', '$I11', donelabel)
ops.'push_pirop'('ne', pos, eos, fail)
ops.'push_pirop'('eq', pos, 0, donelabel)
ops.'push_pirop'('dec', '$I10')
ops.'push_pirop'('is_cclass', '$I11', .CCLASS_NEWLINE, tgt, '$I10')
ops.'push_pirop'('if', '$I11', fail)
ops.'push'(donelabel)
goto done
anchor_lwb:
ops.'push_pirop'('ge', pos, eos, fail)
ops.'push_pirop'('sub', '$I10', pos, off)
ops.'push_pirop'('is_cclass', '$I11', .CCLASS_WORD, tgt, '$I10')
ops.'push_pirop'('unless', '$I11', fail)
ops.'push_pirop'('dec', '$I10')
ops.'push_pirop'('is_cclass', '$I11', .CCLASS_WORD, tgt, '$I10')
ops.'push_pirop'('if', '$I11', fail)
goto done
anchor_rwb:
ops.'push_pirop'('le', pos, 0, fail)
ops.'push_pirop'('sub', '$I10', pos, off)
ops.'push_pirop'('is_cclass', '$I11', .CCLASS_WORD, tgt, '$I10')
ops.'push_pirop'('if', '$I11', fail)
ops.'push_pirop'('dec', '$I10')
ops.'push_pirop'('is_cclass', '$I11', .CCLASS_WORD, tgt, '$I10')
ops.'push_pirop'('unless', '$I11', fail)
goto done
done:
.return (ops)
.end
=item charclass(PAST::Regex node)
Match something in a character class, such as \w, \d, \s, dot, etc.
=cut
.sub 'charclass' :method
.param pmc node
.local pmc cur, tgt, pos, off, eos, fail, ops
(cur, tgt, pos, off, eos, fail) = self.'!rxregs'('cur tgt pos off eos fail')
ops = self.'post_new'('Ops', 'node'=>node, 'result'=>cur)
.local string subtype
subtype = node.'subtype'()
ops.'push_pirop'('inline', subtype, 'inline'=>' # rx charclass %0')
ops.'push_pirop'('ge', pos, eos, fail)
if subtype == '.' goto charclass_done
.local pmc cctest
$S0 = downcase subtype
$I0 = iseq subtype, $S0
cctest = self.'??!!'($I0, 'unless', 'if')
if $S0 == 'd' goto cclass_digit
if $S0 == 's' goto cclass_space
if $S0 == 'w' goto cclass_word
if $S0 == 'n' goto cclass_newline
if $S0 == 'nl' goto cclass_newline
self.'panic'('Unrecognized subtype "', subtype, '" in PAST::Regex charclass node')
cclass_digit:
.local int cclass
cclass = .CCLASS_NUMERIC
goto cclass_done
cclass_space:
cclass = .CCLASS_WHITESPACE
goto cclass_done
cclass_word:
cclass = .CCLASS_WORD
goto cclass_done
cclass_newline:
cclass = .CCLASS_NEWLINE
cclass_done:
ops.'push_pirop'('sub', '$I10', pos, off)
ops.'push_pirop'('is_cclass', '$I11', cclass, tgt, '$I10')
ops.'push_pirop'(cctest, '$I11', fail)
unless $S0 == 'nl' goto charclass_done
# handle logical newline here
ops.'push_pirop'('substr', '$S10', tgt, '$I10', 2)
ops.'push_pirop'('iseq', '$I11', '$S10', '"\r\n"')
ops.'push_pirop'('add', pos, '$I11')
charclass_done:
ops.'push_pirop'('inc', pos)
.return (ops)
.end
=item concat(PAST::Regex node)
Handle a concatenation of regexes.
=cut
.sub 'concat' :method :multi(_, ['PAST';'Regex'])
.param pmc node
.local pmc cur, ops, iter
(cur) = self.'!rxregs'('cur')
ops = self.'post_new'('Ops', 'node'=>node, 'result'=>cur)
iter = node.'iterator'()
iter_loop:
unless iter goto iter_done
.local pmc cpast, cpost
cpast = shift iter
cpost = self.'post_regex'(cpast, cur)
ops.'push'(cpost)
goto iter_loop
iter_done:
.return (ops)
.end
=item cut(PAST::Regex node)
Generate POST for the cut-group and cut-rule operators.
=cut
.sub 'cut' :method :multi(_, ['PAST';'Regex'])
.param pmc node
.local pmc cur, fail, ops
(cur, fail) = self.'!rxregs'('cur fail')
ops = self.'post_new'('Ops', 'node'=>node, 'result'=>cur)
ops.'push_pirop'('set_addr', '$I10', fail)
self.'!cursorop'(ops, '!mark_commit', 0, '$I10')
.return (ops)
.end
=item enumcharlist(PAST::Regex node)
Generate POST for matching a character from an enumerated
character list.
=cut
.sub 'enumcharlist' :method :multi(_, ['PAST';'Regex'])
.param pmc node
.local pmc cur, tgt, pos, off, eos, fail, ops
(cur, tgt, pos, off, eos, fail) = self.'!rxregs'('cur tgt pos off eos fail')
ops = self.'post_new'('Ops', 'node'=>node, 'result'=>cur)
.local string charlist
charlist = node[0]
charlist = self.'escape'(charlist)
.local pmc negate, testop
negate = node.'negate'()
testop = self.'??!!'(negate, 'ge', 'lt')
.local string subtype
.local int zerowidth
subtype = node.'subtype'()
zerowidth = iseq subtype, 'zerowidth'
ops.'push_pirop'('inline', negate, subtype, 'inline'=>' # rx enumcharlist negate=%0 %1')
ops.'push_pirop'('ge', pos, eos, fail)
ops.'push_pirop'('sub', '$I10', pos, off)
ops.'push_pirop'('substr', '$S10', tgt, '$I10', 1)
ops.'push_pirop'('index', '$I11', charlist, '$S10')
ops.'push_pirop'(testop, '$I11', 0, fail)
if zerowidth goto skip_zero_2
ops.'push_pirop'('inc', pos)
skip_zero_2:
.return (ops)
.end
=item literal(PAST::Regex node)
Generate POST for matching a literal string provided as the
second child of this node.
=cut
.sub 'literal' :method :multi(_,['PAST';'Regex'])
.param pmc node
.local pmc cur, pos, eos, tgt, fail, off
(cur, pos, eos, tgt, fail, off) = self.'!rxregs'('cur pos eos tgt fail off')
.local pmc ops, cpast, cpost, lpast, lpost
ops = self.'post_new'('Ops', 'node'=>node, 'result'=>cur)
.local string subtype
.local int ignorecase
subtype = node.'subtype'()
ignorecase = iseq subtype, 'ignorecase'
# literal to be matched is our first child
.local int litconst
lpast = node[0]
litconst = isa lpast, ['String']
unless litconst goto lpast_done
unless ignorecase goto lpast_done
$S0 = lpast
$S0 = downcase $S0
lpast = box $S0
lpast_done:
lpost = self.'as_post'(lpast, 'rtype'=>'~')
$S0 = lpost.'result'()
ops.'push_pirop'('inline', subtype, $S0, 'inline'=>' # rx literal %0 %1')
ops.'push'(lpost)
# compute constant literal length at compile time
.local string litlen
$I0 = isa lpast, ['String']
if $I0 goto literal_string
litlen = '$I10'
ops.'push_pirop'('length', '$I10', lpost)
goto have_litlen
literal_string:
$S0 = lpast
$I0 = length $S0
litlen = $I0
if $I0 > 0 goto have_litlen
.return (cpost)
have_litlen:
# fail if there aren't enough characters left in string
ops.'push_pirop'('add', '$I11', pos, litlen)
ops.'push_pirop'('gt', '$I11', eos, fail)
# compute string to be matched and fail if mismatch
ops.'push_pirop'('sub', '$I11', pos, off)
ops.'push_pirop'('substr', '$S10', tgt, '$I11', litlen)
unless ignorecase goto literal_test
ops.'push_pirop'('downcase', '$S10', '$S10')
literal_test:
ops.'push_pirop'('ne', '$S10', lpost, fail)
# increase position by literal length and move on
ops.'push_pirop'('add', pos, litlen)
.return (ops)
.end
=item pass(PAST::Regex node)
=cut
.sub 'pass' :method :multi(_,['PAST';'Regex'])
.param pmc node
.local pmc cur, pos, ops
(cur, pos) = self.'!rxregs'('cur pos')
ops = self.'post_new'('Ops', 'node'=>node, 'result'=>cur)
.local string regexname
$P0 = find_dynamic_lex '$*REGEXNAME'
regexname = self.'escape'($P0)
ops.'push_pirop'('inline', 'inline'=>' # rx pass')
self.'!cursorop'(ops, '!cursor_pass', 0, pos, regexname)
ops.'push_pirop'('return', cur)
.return (ops)
.end
=item reduce
=cut
.sub 'reduce' :method :multi(_,['PAST';'Regex'])
.param pmc node
.local pmc cur, pos, ops
(cur, pos) = self.'!rxregs'('cur pos')
ops = self.'post_new'('Ops', 'node'=>node, 'result'=>cur)
.local pmc cpost, posargs, namedargs
(cpost, posargs, namedargs) = self.'post_children'(node, 'signature'=>'v:')
.local string regexname, key
$P0 = find_dynamic_lex '$*REGEXNAME'
regexname = self.'escape'($P0)
key = posargs[0]
ops.'push_pirop'('inline', regexname, key, 'inline'=>' # rx reduce name=%0 key=%1')
ops.'push'(cpost)
self.'!cursorop'(ops, '!cursor_pos', 0, pos)
self.'!cursorop'(ops, '!reduce', 0, regexname, posargs :flat, namedargs :flat)
.return (ops)
.end
=item quant(PAST::Regex node)
=cut
.sub 'quant' :method :multi(_,['PAST';'Regex'])
.param pmc node
.local string backtrack
backtrack = node.'backtrack'()
if backtrack goto have_backtrack
backtrack = 'g'
have_backtrack:
.local int min, max
min = node.'min'()
$P0 = node.'max'()
max = $P0
$I0 = defined $P0
if $I0 goto have_max
max = -1 # -1 represents Inf
have_max:
.local pmc cur, pos, rep, fail
(cur, pos, rep, fail) = self.'!rxregs'('cur pos rep fail')
.local string qname
.local pmc ops, q1label, q2label, btreg, cpost
$S0 = concat 'rxquant', backtrack
qname = self.'unique'($S0)
ops = self.'post_new'('Ops', 'node'=>node)
$S0 = concat qname, '_loop'
q1label = self.'post_new'('Label', 'result'=>$S0)
$S0 = concat qname, '_done'
q2label = self.'post_new'('Label', 'result'=>$S0)
cpost = self.'concat'(node)
.local pmc seppast, seppost
null seppost
seppast = node.'sep'()
unless seppast goto have_seppost
seppost = self.'post_regex'(seppast)
have_seppost:
$S0 = max
.local int needrep
$I0 = isgt min, 1
$I1 = isgt max, 1
needrep = or $I0, $I1
unless max < 0 goto have_s0
$S0 = '*'
have_s0:
ops.'push_pirop'('inline', qname, min, $S0, 'inline'=>' # rx %0 ** %1..%2')
if backtrack == 'f' goto frugal
greedy:
btreg = self.'uniquereg'('I')
ops.'push_pirop'('set_addr', btreg, q2label)
.local int needmark
.local string peekcut
needmark = needrep
peekcut = '!mark_peek'
if backtrack != 'r' goto greedy_1
needmark = 1
peekcut = '!mark_commit'
greedy_1:
if min == 0 goto greedy_2
unless needmark goto greedy_loop
self.'!cursorop'(ops, '!mark_push', 0, 0, CURSOR_FAIL, btreg)
goto greedy_loop
greedy_2:
self.'!cursorop'(ops, '!mark_push', 0, 0, pos, btreg)
greedy_loop:
ops.'push'(q1label)
ops.'push'(cpost)
unless needmark goto greedy_3
self.'!cursorop'(ops, peekcut, 1, rep, btreg)
unless needrep goto greedy_3
ops.'push_pirop'('inc', rep)
greedy_3:
unless max > 1 goto greedy_4
ops.'push_pirop'('ge', rep, max, q2label)
greedy_4:
unless max != 1 goto greedy_5
self.'!cursorop'(ops, '!mark_push', 0, rep, pos, btreg)
if null seppost goto greedy_4a
ops.'push'(seppost)
greedy_4a:
ops.'push_pirop'('goto', q1label)
greedy_5:
ops.'push'(q2label)
unless min > 1 goto greedy_6
ops.'push_pirop'('lt', rep, min, fail)
greedy_6:
.return (ops)
frugal:
.local pmc ireg
ireg = self.'uniquereg'('I')
if min == 0 goto frugal_1
unless needrep goto frugal_0
ops.'push_pirop'('set', rep, 0)
frugal_0:
if null seppost goto frugal_2
.local pmc seplabel
$S0 = concat qname, '_sep'
seplabel = self.'post_new'('Label', 'result'=>$S0)
ops.'push_pirop'('goto', seplabel)
goto frugal_2
frugal_1:
ops.'push_pirop'('set_addr', '$I10', q1label)
self.'!cursorop'(ops, '!mark_push', 0, 0, pos, '$I10')
ops.'push_pirop'('goto', q2label)
frugal_2:
ops.'push'(q1label)
if null seppost goto frugal_2a
ops.'push'(seppost)
ops.'push'(seplabel)
frugal_2a:
unless needrep goto frugal_3
ops.'push_pirop'('set', ireg, rep)
frugal_3:
ops.'push'(cpost)
unless needrep goto frugal_4
ops.'push_pirop'('add', rep, ireg, 1)
frugal_4:
unless min > 1 goto frugal_5
ops.'push_pirop'('lt', rep, min, q1label)
frugal_5:
unless max > 1 goto frugal_6
ops.'push_pirop'('ge', rep, max, q2label)
frugal_6:
unless max != 1 goto frugal_7
ops.'push_pirop'('set_addr', '$I10', q1label)
self.'!cursorop'(ops, '!mark_push', 0, ireg, pos, '$I10')
frugal_7:
ops.'push'(q2label)
.return (ops)
.end
=item scan(POST::Regex)
Code for initial regex scan.
=cut
.sub 'scan' :method :multi(_, ['PAST';'Regex'])
.param pmc node
.local pmc cur, pos, eos, ops
(cur, pos, eos) = self.'!rxregs'('cur pos eos')
ops = self.'post_new'('Ops', 'node'=>node, 'result'=>cur)
.local pmc looplabel, donelabel
$S0 = self.'unique'('rxscan')
$S1 = concat $S0, '_loop'
looplabel = self.'post_new'('Label', 'result'=>$S1)
$S1 = concat $S0, '_done'
donelabel = self.'post_new'('Label', 'result'=>$S1)
ops.'push_pirop'('ge', pos, 0, donelabel)
ops.'push'(looplabel)
self.'!cursorop'(ops, 'from', 1, '$P10')
ops.'push_pirop'('inc', '$P10')
ops.'push_pirop'('set', pos, '$P10')
ops.'push_pirop'('ge', pos, eos, donelabel)
ops.'push_pirop'('set_addr', '$I10', looplabel)
self.'!cursorop'(ops, '!mark_push', 0, 0, pos, '$I10')
ops.'push'(donelabel)
.return (ops)
.end
=item subcapture(PAST::Regex node)
Perform a subcapture (capture of a portion of a regex).
=cut
.sub 'subcapture' :method :multi(_, ['PAST';'Regex'])
.param pmc node
.local pmc cur, pos, tgt, fail
(cur, pos, tgt, fail) = self.'!rxregs'('cur pos tgt fail')
.local pmc ops, cpast, cpost
ops = self.'post_new'('Ops', 'node'=>node, 'result'=>cur)
cpast = node[0]
cpost = self.'post_regex'(cpast)
.local pmc name
$P0 = node.'name'()
name = self.'as_post'($P0, 'rtype'=>'*')
.local string rxname
rxname = self.'unique'('rxcap_')
.local pmc caplabel, donelabel
$S0 = concat rxname, '_fail'
caplabel = self.'post_new'('Label', 'result'=>$S0)
$S0 = concat rxname, '_done'
donelabel = self.'post_new'('Label', 'result'=>$S0)
ops.'push_pirop'('inline', name, 'inline'=>' # rx subcapture %0')
ops.'push_pirop'('set_addr', '$I10', caplabel)
self.'!cursorop'(ops, '!mark_push', 0, 0, pos, '$I10')
ops.'push'(cpost)
ops.'push_pirop'('set_addr', '$I10', caplabel)
self.'!cursorop'(ops, '!mark_peek', 2, '$I12', '$I11', '$I10')
self.'!cursorop'(ops, '!cursor_pos', 0, '$I11')
self.'!cursorop'(ops, '!cursor_start', 1, '$P10')
ops.'push_pirop'('callmethod', '"!cursor_pass"', '$P10', pos, '""')
ops.'push'(name)
self.'!cursorop'(ops, '!mark_push', 0, 0, CURSOR_FAIL, 0, '$P10')
ops.'push_pirop'('callmethod', '"!cursor_names"', '$P10', name)
ops.'push_pirop'('goto', donelabel)
ops.'push'(caplabel)
ops.'push_pirop'('goto', fail)
ops.'push'(donelabel)
.return (ops)
.end
=item subrule(PAST::Regex node)
Perform a subrule call.
=cut
.sub 'subrule' :method :multi(_, ['PAST';'Regex'])
.param pmc node
.local pmc cur, pos, fail, ops
(cur, pos, fail) = self.'!rxregs'('cur pos fail')
ops = self.'post_new'('Ops', 'node'=>node, 'result'=>cur)
.local pmc name
$P0 = node.'name'()
name = self.'as_post'($P0, 'rtype'=>'*')
.local pmc cpost, posargs, namedargs, subpost
(cpost, posargs, namedargs) = self.'post_children'(node, 'signature'=>'v:')
subpost = shift posargs
.local pmc negate
.local string testop
negate = node.'negate'()
testop = self.'??!!'(negate, 'if', 'unless')
.local pmc subtype
subtype = node.'subtype'()
ops.'push_pirop'('inline', subpost, subtype, negate, 'inline'=>" # rx subrule %0 subtype=%1 negate=%2")
self.'!cursorop'(ops, '!cursor_pos', 0, pos)
ops.'push'(cpost)
ops.'push_pirop'('callmethod', subpost, cur, posargs :flat, namedargs :flat, 'result'=>'$P10')
ops.'push_pirop'(testop, '$P10', fail)
if subtype == 'zerowidth' goto done
ops.'push_pirop'('callmethod', '"pos"', '$P10', 'result'=>pos)
self.'!cursorop'(ops, '!mark_push', 0, 0, CURSOR_FAIL, 0, '$P10')
if subtype == 'method' goto done
ops.'push'(name)
ops.'push_pirop'('callmethod', '"!cursor_names"', '$P10', name)
done:
.return (ops)
.end
=item post_new(type, args :slurpy, options :slurpy :named)
Helper method to create a new POST node of C<type>.
=cut
.sub 'post_new' :method
.param string type
.param pmc args :slurpy
.param pmc options :slurpy :named
$P0 = get_hll_global ['POST'], type
.tailcall $P0.'new'(args :flat, options :flat :named)
.end
=item ??!!(test, trueval, falseval)
Helper method to perform ternary operation -- returns C<trueval>
if C<test> is true, C<falseval> otherwise.
=cut
.sub '??!!' :method
.param pmc test
.param pmc trueval
.param pmc falseval
if test goto true
.return (falseval)
true:
.return (trueval)
.end
=back
=head1 AUTHOR
Patrick Michaud <pmichaud@pobox.com> is the author and maintainer.
=head1 COPYRIGHT
Copyright (C) 2009, Patrick R. Michaud.
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: