/
Regex-s0.pir
3885 lines (3095 loc) · 91.8 KB
/
Regex-s0.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
# $Id$
=head1 NAME
Regex - Regex library
=head1 DESCRIPTION
This file brings together the various Regex modules needed for Regex.pbc .
=cut
### .include 'src/Regex/Cursor.pir'
# Copyright (C) 2009, The Perl Foundation.
# $Id$
=head1 NAME
Regex::Cursor - Regex Cursor nodes
=head1 DESCRIPTION
This file implements the Regex::Cursor class, used for managing regular
expression control flow. Regex::Cursor is also a base class for
grammars.
=cut
.include 'cclass.pasm'
### .include 'src/Regex/constants.pir'
.const int CURSOR_FAIL = -1
.const int CURSOR_FAIL_GROUP = -2
.const int CURSOR_FAIL_RULE = -3
.const int CURSOR_FAIL_MATCH = -4
.const int CURSOR_TYPE_SCAN = 1
.const int CURSOR_TYPE_PEEK = 2
.namespace ['Regex';'Cursor']
.sub '' :anon :load :init
load_bytecode 'P6object.pbc'
.local pmc p6meta
p6meta = new 'P6metaclass'
$P0 = p6meta.'new_class'('Regex::Cursor', 'attr'=>'$!target $!from $!pos $!match $!names $!debug @!bstack @!cstack @!caparray &!regex')
$P0 = box 0
set_global '$!generation', $P0
$P0 = new ['Boolean']
assign $P0, 0
set_global '$!FALSE', $P0
$P0 = new ['Boolean']
assign $P0, 1
set_global '$!TRUE', $P0
.return ()
.end
=head2 Methods
=over 4
=item new_match()
A method that creates an empty Match object, by default of type
C<Regex::Match>. This method can be overridden for generating HLL-specific
Match objects.
=cut
.sub 'new_match' :method
.local pmc match
match = new ['Regex';'Match']
.return (match)
.end
=item new_array()
A method that creates an empty array object, by default of type
C<ResizablePMCArray>. This method can be overridden for generating HLL-specific
arrays for usage within Match objects.
=cut
.sub 'new_array' :method
.local pmc arr
arr = new ['ResizablePMCArray']
.return (arr)
.end
=item MATCH()
Return this cursor's current Match object, generating a new one
for the Cursor if one hasn't been created yet.
=cut
.sub 'MATCH' :method
.local pmc match
match = getattribute self, '$!match'
if null match goto match_make
$P0 = get_global '$!TRUE'
$I0 = issame match, $P0
unless $I0 goto match_done
# First, create a Match object and bind it
match_make:
match = self.'new_match'()
setattribute self, '$!match', match
setattribute match, '$!cursor', self
.local pmc target, from, to
target = getattribute self, '$!target'
setattribute match, '$!target', target
from = getattribute self, '$!from'
setattribute match, '$!from', from
to = getattribute self, '$!pos'
setattribute match, '$!to', to
# Create any arrayed subcaptures.
.local pmc caparray, caparray_it, caphash
caparray = getattribute self, '@!caparray'
if null caparray goto caparray_done
caparray_it = iter caparray
caphash = new ['Hash']
caparray_loop:
unless caparray_it goto caparray_done
.local string subname
.local pmc arr
.local int keyint
subname = shift caparray_it
arr = self.'new_array'()
caphash[subname] = arr
keyint = is_cclass .CCLASS_NUMERIC, subname, 0
if keyint goto caparray_int
match[subname] = arr
goto caparray_loop
caparray_int:
$I0 = subname
match[$I0] = arr
goto caparray_loop
caparray_done:
# If it's not a successful match, or if there are
# no saved subcursors, we're done.
if to < from goto match_done
.local pmc cstack, cstack_it
cstack = getattribute self, '@!cstack'
if null cstack goto cstack_done
unless cstack goto cstack_done
cstack_it = iter cstack
cstack_loop:
unless cstack_it goto cstack_done
.local pmc subcur, submatch, names
subcur = shift cstack_it
$I0 = isa subcur, ['Regex';'Cursor']
unless $I0 goto cstack_loop
# If the subcursor isn't bound with a name, skip it
names = getattribute subcur, '$!names'
if null names goto cstack_loop
submatch = subcur.'MATCH'()
# See if we have multiple binds
.local pmc names_it
subname = names
names_it = get_global '$!FALSE'
$I0 = index subname, '='
if $I0 < 0 goto cstack_subname
names_it = split '=', subname
cstack_subname_loop:
subname = shift names_it
cstack_subname:
keyint = is_cclass .CCLASS_NUMERIC, subname, 0
if null caparray goto cstack_bind
$I0 = exists caphash[subname]
unless $I0 goto cstack_bind
if keyint goto cstack_array_int
$P0 = match[subname]
push $P0, submatch
goto cstack_bind_done
cstack_array_int:
$I0 = subname
$P0 = match[$I0]
push $P0, submatch
goto cstack_bind_done
cstack_bind:
if keyint goto cstack_bind_int
match[subname] = submatch
goto cstack_bind_done
cstack_bind_int:
$I0 = subname
match[$I0] = submatch
cstack_bind_done:
if names_it goto cstack_subname_loop
goto cstack_loop
cstack_done:
match_done:
.return (match)
.end
=item parse(target [, 'rule'=>regex])
Parse C<target> in the current grammar starting with C<regex>.
If C<regex> is omitted, then use the C<TOP> rule for the grammar.
=cut
.sub 'parse' :method
.param pmc target
.param pmc regex :named('rule') :optional
.param int has_regex :opt_flag
.param pmc actions :named('actions') :optional
.param int rxtrace :named('rxtrace') :optional
.param pmc options :slurpy :named
if has_regex goto have_regex
regex = box 'TOP'
have_regex:
$I0 = isa regex, ['String']
unless $I0 goto regex_done
$S0 = regex
regex = find_method self, $S0
regex_done:
.lex '$*ACTIONS', actions
.local pmc cur, match
cur = self.'!cursor_init'(target, options :flat :named)
unless rxtrace goto rxtrace_done
cur.'DEBUG'()
rxtrace_done:
cur = cur.regex()
match = cur.'MATCH'()
.return (match)
.end
=item next()
Return the next match from a successful Cursor.
=cut
.sub 'next' :method
.local pmc cur, match
cur = self.'!cursor_next'()
match = cur.'MATCH'()
.return (match)
.end
=item pos()
Return the cursor's current position.
=cut
.sub 'pos' :method
$P0 = getattribute self, '$!pos'
.return ($P0)
.end
=item from()
Return the cursor's from position.
=cut
.sub 'from' :method
$P0 = getattribute self, '$!from'
.return ($P0)
.end
=back
=head2 Private methods
=over 4
=item !cursor_init(target)
Create a new cursor for matching C<target>.
=cut
.sub '!cursor_init' :method
.param string target
.param int pos :named('p') :optional
.param int has_pos :opt_flag
.param int cont :named('c') :optional
.param int has_cont :opt_flag
.local pmc parrotclass, cur
$P0 = self.'HOW'()
parrotclass = getattribute $P0, 'parrotclass'
cur = new parrotclass
$P0 = box target
setattribute cur, '$!target', $P0
if has_cont goto cursor_cont
$P0 = box pos
setattribute cur, '$!from', $P0
$P0 = box pos
setattribute cur, '$!pos', $P0
goto cursor_done
cursor_cont:
$P0 = box CURSOR_FAIL
setattribute cur, '$!from', $P0
$P0 = box cont
setattribute cur, '$!pos', $P0
cursor_done:
.return (cur)
.end
=item !cursor_start([lang])
Create and initialize a new cursor from C<self>. If C<lang> is
provided, then the new cursor has the same type as lang.
=cut
.sub '!cursor_start' :method
.param pmc lang :optional
.param int has_lang :opt_flag
if has_lang goto have_lang
lang = self
have_lang:
.local pmc parrotclass, cur
$P0 = lang.'HOW'()
parrotclass = getattribute $P0, 'parrotclass'
cur = new parrotclass
.local pmc regex
regex = getattribute self, '&!regex'
unless null regex goto cursor_restart
.local pmc from, target, debug
from = getattribute self, '$!pos'
setattribute cur, '$!from', from
setattribute cur, '$!pos', from
target = getattribute self, '$!target'
setattribute cur, '$!target', target
debug = getattribute self, '$!debug'
setattribute cur, '$!debug', debug
.return (cur, from, target, 0)
cursor_restart:
.local pmc pos, cstack, bstack
from = getattribute self, '$!from'
target = getattribute self, '$!target'
debug = getattribute self, '$!debug'
cstack = getattribute self, '@!cstack'
bstack = getattribute self, '@!bstack'
pos = box CURSOR_FAIL
setattribute cur, '$!from', from
setattribute cur, '$!pos', pos
setattribute cur, '$!target', target
setattribute cur, '$!debug', debug
if null cstack goto cstack_done
cstack = clone cstack
setattribute cur, '@!cstack', cstack
cstack_done:
if null bstack goto bstack_done
bstack = clone bstack
setattribute cur, '@!bstack', bstack
bstack_done:
.return (cur, from, target, 1)
.end
=item !cursor_fail(pos)
Permanently fail this cursor.
=cut
.sub '!cursor_fail' :method
.local pmc pos
pos = box CURSOR_FAIL_RULE
setattribute self, '$!pos', pos
null $P0
setattribute self, '$!match', $P0
setattribute self, '@!bstack', $P0
setattribute self, '@!cstack', $P0
.end
=item !cursor_pass(pos, name)
Set the Cursor as passing at C<pos>; calling any reduction action
C<name> associated with the cursor. This method simply sets
C<$!match> to a boolean true value to indicate the regex was
successful; the C<MATCH> method above replaces this boolean true
with a "real" Match object when requested.
=cut
.sub '!cursor_pass' :method
.param pmc pos
.param string name
setattribute self, '$!pos', pos
.local pmc match
match = get_global '$!TRUE'
setattribute self, '$!match', match
unless name goto done
self.'!reduce'(name)
done:
.return (self)
.end
=item !cursor_backtrack()
Configure this cursor for backtracking via C<!cursor_next>.
=cut
.sub '!cursor_backtrack' :method
$P0 = getinterp
$P1 = $P0['sub';1]
setattribute self, '&!regex', $P1
.end
=item !cursor_next()
Continue a regex match from where the current cursor left off.
=cut
.sub '!cursor_next' :method
.local pmc regex, cur
regex = getattribute self, '&!regex'
if null regex goto fail
cur = self.regex()
.return (cur)
fail:
cur = self.'!cursor_start'()
cur.'!cursor_fail'()
.return (cur)
.end
=item !cursor_caparray(caparray :slurpy)
Set the list of subcaptures that produce arrays to C<caparray>.
=cut
.sub '!cursor_caparray' :method
.param pmc caparray :slurpy
setattribute self, '@!caparray', caparray
.end
=item !cursor_names(names)
Set the Cursor's name (for binding) to C<names>.
=cut
.sub '!cursor_names' :method
.param pmc names
setattribute self, '$!names', names
.end
=item !cursor_pos(pos)
Set the cursor's position to C<pos>.
=cut
.sub '!cursor_pos' :method
.param pmc pos
setattribute self, '$!pos', pos
.end
=item !cursor_debug(args :slurpy)
Log a debug message.
=cut
.sub '!cursor_debug' :method
.param string tag
.param pmc args :slurpy
$P0 = getattribute self, '$!debug'
if null $P0 goto done
unless $P0 goto done
.local pmc fmt, from, pos, orig, line
fmt = new ['ResizablePMCArray']
from = getattribute self, '$!from'
orig = getattribute self, '$!target'
$P0 = get_hll_global ['HLL'], 'Compiler'
line = $P0.'lineof'(orig, from, 'cache'=>1)
$P0 = getinterp
$P1 = $P0.'stdhandle'(2)
$N0 = time
push fmt, $N0
push fmt, from
push fmt, line
push fmt, tag
$S0 = sprintf "%.6f %d/%d %-8s ", fmt
print $P1, $S0
$S0 = join '', args
print $P1, $S0
print $P1, "\n"
done:
.return (self)
.end
=item !mark_push(rep, pos, mark)
Push a new backtracking point onto the cursor with the given
C<rep>, C<pos>, and backtracking C<mark>. (The C<mark> is typically
the address of a label to branch to when backtracking occurs.)
=cut
.sub '!mark_push' :method
.param int rep
.param int pos
.param int mark
.param pmc subcur :optional
.param int has_subcur :opt_flag
# cptr contains the desired number of elements in the cstack
.local int cptr
cptr = 0
# Initialize bstack if needed, and set cptr to be the cstack
# size requested by the top frame.
.local pmc bstack
bstack = getattribute self, '@!bstack'
if null bstack goto bstack_new
unless bstack goto bstack_done
$I0 = elements bstack
dec $I0
cptr = bstack[$I0]
goto bstack_done
bstack_new:
bstack = new ['ResizableIntegerArray']
setattribute self, '@!bstack', bstack
bstack_done:
# If a new subcursor is being pushed, then save it in cstack
# and change cptr to include the new subcursor. Also clear
# any existing match object, as we may have just changed the
# match state.
unless has_subcur goto subcur_done
null $P0
setattribute self, '$!match', $P0
.local pmc cstack
cstack = getattribute self, '@!cstack'
unless null cstack goto have_cstack
cstack = new ['ResizablePMCArray']
setattribute self, '@!cstack', cstack
have_cstack:
cstack[cptr] = subcur
inc cptr
subcur_done:
# Save our mark frame information.
push bstack, mark
push bstack, pos
push bstack, rep
push bstack, cptr
.end
=item !mark_peek(mark)
Return information about the latest frame for C<mark>.
If C<mark> is zero, return information about the latest frame.
=cut
.sub '!mark_peek' :method
.param int tomark
.local pmc bstack
bstack = getattribute self, '@!bstack'
if null bstack goto no_mark
unless bstack goto no_mark
.local int bptr
bptr = elements bstack
bptr_loop:
bptr = bptr - 4
if bptr < 0 goto no_mark
.local int rep, pos, mark, cptr
mark = bstack[bptr]
unless tomark goto bptr_done
unless mark == tomark goto bptr_loop
bptr_done:
$I0 = bptr + 1
pos = bstack[$I0]
inc $I0
rep = bstack[$I0]
inc $I0
cptr = bstack[$I0]
.return (rep, pos, mark, bptr, bstack, cptr)
no_mark:
.return (0, CURSOR_FAIL_GROUP, 0, 0, bstack, 0)
.end
=item !mark_fail(tomark)
Remove the most recent C<mark> and backtrack the cursor to the
point given by that mark. If C<mark> is zero, then
backtracks the most recent mark. Returns the backtracked
values of repetition count, cursor position, and mark (address).
=cut
.sub '!mark_fail' :method
.param int mark
# Get the frame information for C<mark>.
.local int rep, pos, mark, bptr, cptr
.local pmc bstack
(rep, pos, mark, bptr, bstack, cptr) = self.'!mark_peek'(mark)
# clear any existing Match object
null $P0
setattribute self, '$!match', $P0
.local pmc subcur
null subcur
# If there's no bstack, there's nothing else to do.
if null bstack goto done
# If there's a subcursor associated with this mark, return it.
unless cptr > 0 goto cstack_done
.local pmc cstack
cstack = getattribute self, '@!cstack'
dec cptr
subcur = cstack[cptr]
# Set the cstack to the size requested by the soon-to-be-top mark frame.
unless bptr > 0 goto cstack_zero
$I0 = bptr - 1
$I0 = bstack[$I0]
assign cstack, $I0
goto cstack_done
cstack_zero:
assign cstack, 0
cstack_done:
# Pop the current mark frame and all above it.
assign bstack, bptr
done:
.return (rep, pos, mark, subcur)
.end
=item !mark_commit(mark)
Like C<!mark_fail> above this backtracks the cursor to C<mark>
(releasing any intermediate marks), but preserves the current
capture states.
=cut
.sub '!mark_commit' :method
.param int mark
# find mark
.local int rep, pos, mark, bptr, cptr
.local pmc bstack
(rep, pos, mark, bptr, bstack) = self.'!mark_peek'(mark)
# get current cstack size into cptr
if null bstack goto done
unless bstack goto done
$I0 = elements bstack
dec $I0
cptr = bstack[$I0]
# Pop the mark frame and everything above it.
assign bstack, bptr
# If we don't need to hold any cstack information, we're done.
unless cptr > 0 goto done
# If the top frame is an auto-fail frame, (re)use it to hold
# our needed cptr, otherwise create a new auto-fail frame to do it.
unless bptr > 0 goto cstack_push
$I0 = bptr - 3 # pos is at top-3
$I1 = bstack[$I0]
unless $I1 < 0 goto cstack_push
$I0 = bptr - 1 # cptr is at top-1
bstack[$I0] = cptr
goto done
cstack_push:
push bstack, 0 # mark
push bstack, CURSOR_FAIL # pos
push bstack, 0 # rep
push bstack, cptr # cptr
done:
.return (rep, pos, mark)
.end
=item !reduce(name [, key] [, match])
Perform any action associated with the current regex match.
=cut
.sub '!reduce' :method
.param string name
.param string key :optional
.param int has_key :opt_flag
.param pmc match :optional
.param int has_match :opt_flag
.local pmc actions
actions = find_dynamic_lex '$*ACTIONS'
if null actions goto actions_done
$I0 = can actions, name
unless $I0 goto actions_done
if has_match goto match_done
match = self.'MATCH'()
match_done:
if has_key goto actions_key
actions.name(match)
goto actions_done
actions_key:
.tailcall actions.name(match, key)
actions_done:
.return ()
.end
=item !BACKREF(name)
Match the backreference given by C<name>.
=cut
.sub '!BACKREF' :method
.param string name
.local pmc cur
.local int pos, eos
.local string tgt
(cur, pos, tgt) = self.'!cursor_start'()
# search the cursor cstack for the latest occurrence of C<name>
.local pmc cstack
cstack = getattribute self, '@!cstack'
if null cstack goto pass
.local int cstack_it
cstack_it = elements cstack
cstack_loop:
dec cstack_it
unless cstack_it >= 0 goto pass
.local pmc subcur
subcur = cstack[cstack_it]
$P0 = getattribute subcur, '$!names'
if null $P0 goto cstack_loop
$S0 = $P0
if name != $S0 goto cstack_loop
# we found a matching subcursor, get the literal it matched
cstack_done:
.local int litlen
.local string litstr
$I1 = subcur.'pos'()
$I0 = subcur.'from'()
litlen = $I1 - $I0
litstr = substr tgt, $I0, litlen
# now test the literal against our target
$S0 = substr tgt, pos, litlen
unless $S0 == litstr goto fail
pos += litlen
pass:
cur.'!cursor_pass'(pos, '')
fail:
.return (cur)
.end
=item !INTERPOLATE(var [, convert])
Perform regex interpolation on C<var>. If C<var> is a
regex (sub), it is used directly, otherwise it is used
for a string literal match. If C<var> is an array,
then all of the elements of C<var> are considered,
and the longest match is returned.
=cut
.sub '!INTERPOLATE' :method
.param pmc var
.local pmc cur
.local int pos, eos
.local string tgt
$I0 = does var, 'array'
if $I0 goto var_array
var_scalar:
$I0 = does var, 'invokable'
if $I0 goto var_sub
var_string:
(cur, pos, tgt) = self.'!cursor_start'()
eos = length tgt
$S0 = var
$I0 = length $S0
$I1 = pos + $I0
if $I1 > eos goto string_fail
$S1 = substr tgt, pos, $I0
if $S0 != $S1 goto string_fail
pos += $I0
string_pass:
cur.'!cursor_pass'(pos, '')
string_fail:
.return (cur)
var_sub:
cur = var(self)
.return (cur)
var_array:
(cur, pos, tgt) = self.'!cursor_start'()
eos = length tgt
.local pmc var_it, elem
.local int maxlen
var_it = iter var
maxlen = -1
array_loop:
unless var_it goto array_done
elem = shift var_it
$I0 = does elem, 'invokable'
if $I0 goto array_sub
array_string:
$S0 = elem
$I0 = length $S0
if $I0 <= maxlen goto array_loop
$I1 = pos + $I0
if $I1 > eos goto array_loop
$S1 = substr tgt, pos, $I0
if $S0 != $S1 goto array_loop
maxlen = $I0
goto array_loop
array_sub:
$P0 = elem(self)
unless $P0 goto array_loop
$I0 = $P0.'pos'()
$I0 -= pos
if $I0 <= maxlen goto array_loop
maxlen = $I0
goto array_loop
array_done:
if maxlen < 0 goto array_fail
$I0 = pos + maxlen
cur.'!cursor_pass'($I0, '')
array_fail:
.return (cur)
.end
=item !INTERPOLATE_REGEX(var)
Same as C<!INTERPOLATE> above, except that any non-regex values
are first compiled to regexes prior to being matched.
=cut
.sub '!INTERPOLATE_REGEX' :method
.param pmc var
$I0 = does var, 'invokable'
if $I0 goto done
.local pmc p6regex
p6regex = compreg 'Regex::P6Regex'
$I0 = does var, 'array'
if $I0 goto var_array
var = p6regex.'compile'(var)
goto done
var_array:
.local pmc var_it, elem
var_it = iter var
var = new ['ResizablePMCArray']
var_loop:
unless var_it goto done
elem = shift var_it
$I0 = does elem, 'invokable'
if $I0 goto var_next
elem = p6regex.'compile'(elem)
var_next:
push var, elem
goto var_loop
done:
.tailcall self.'!INTERPOLATE'(var)
.end
=back
=head2 Vtable functions
=over 4
=item get_bool
=cut
.sub '' :vtable('get_bool') :method
.local pmc match
match = getattribute self, '$!match'
if null match goto false
$I0 = istrue match
.return ($I0)
false:
.return (0)
.end
=back
=head1 AUTHORS
Patrick Michaud <pmichaud@pobox.com> is the author and maintainer.
=cut
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir:
### .include 'src/Regex/Cursor-builtins.pir'
# Copyright (C) 2009, The Perl Foundation.
# $Id$
=head1 NAME
Regex::Cursor-builtins - builtin regexes for Cursor objects
=cut
.include 'cclass.pasm'
.namespace ['Regex';'Cursor']
.sub 'before' :method
.param pmc regex :optional
.local pmc cur
.local int pos
(cur, pos) = self.'!cursor_start'()
if null regex goto fail
$P0 = cur.regex()
unless $P0 goto fail
cur.'!cursor_pass'(pos, 'before')
fail:
.return (cur)
.end
.sub 'ident' :method
.local pmc cur
.local int pos, eos
.local string tgt
(cur, pos, tgt) = self.'!cursor_start'()
eos = length tgt
$S0 = substr tgt, pos, 1
if $S0 == '_' goto ident_1
$I0 = is_cclass .CCLASS_ALPHABETIC, tgt, pos
unless $I0 goto fail
ident_1:
pos = find_not_cclass .CCLASS_WORD, tgt, pos, eos
cur.'!cursor_pass'(pos, 'ident')
fail:
.return (cur)
.end
.sub 'wb' :method