1
1
# Some things that all cursors involved in a given parse share.
2
2
my class ParseShared {
3
3
has $ ! orig ;
4
+ has str $ ! target ;
4
5
}
5
6
6
7
role NQPCursorRole is export {
7
8
has $ ! shared ;
8
- has str $ ! target ;
9
9
has int $ ! from ;
10
10
has int $ ! pos ;
11
11
has $ ! match ;
@@ -16,7 +16,7 @@ role NQPCursorRole is export {
16
16
has $ ! restart ;
17
17
18
18
method orig () { nqp ::getattr($ ! shared , ParseShared, ' $!orig' ) }
19
- method target () { $ ! target }
19
+ method target () { nqp ::getattr_s( $ ! shared , ParseShared, ' $! target' ) }
20
20
method from () { $ ! from }
21
21
method pos () { $ ! pos }
22
22
@@ -70,16 +70,15 @@ role NQPCursorRole is export {
70
70
$ caps ;
71
71
}
72
72
73
- method ! cursor_init ($ orig , : $ p = 0 , : $ c , : $ target , : $ shared ) {
73
+ method ! cursor_init ($ orig , : $ p = 0 , : $ c , : $ shared ) {
74
74
my $ new := self . CREATE();
75
75
unless $ shared {
76
76
$ shared := nqp ::create(ParseShared);
77
77
nqp ::bindattr($ shared , ParseShared, ' $!orig' , $ orig );
78
+ nqp ::bindattr_s($ shared , ParseShared, ' $!target' ,
79
+ pir::trans_encoding__Ssi($ orig , pir::find_encoding__Is(' ucs4' )));
78
80
}
79
81
nqp ::bindattr($ new , $ ? CLASS , ' $!shared' , $ shared );
80
- nqp ::bindattr_s($ new , $ ? CLASS , ' $!target' , $ target
81
- ?? $ target
82
- !! pir::trans_encoding__Ssi($ orig , pir::find_encoding__Is(' ucs4' )));
83
82
if nqp :: defined ($ c ) {
84
83
nqp ::bindattr_i($ new , $ ? CLASS , ' $!from' , -1 );
85
84
nqp ::bindattr_i($ new , $ ? CLASS , ' $!pos' , $ c );
@@ -101,16 +100,16 @@ role NQPCursorRole is export {
101
100
nqp ::bindattr($ new , $ ? CLASS , ' $!cstack' , nqp :: clone ($ ! cstack )) if $ ! cstack ;
102
101
pir::return__0PsiPPi(
103
102
$ new ,
104
- nqp ::bindattr_s( $ new , $ ? CLASS , ' $!target' , $ ! target ),
103
+ nqp ::getattr_s( $ ! shared , ParseShared , ' $!target' ),
105
104
nqp ::bindattr_i($ new , $ ? CLASS , ' $!from' , $ ! from ),
106
105
$ ? CLASS ,
107
106
nqp ::bindattr($ new , $ ? CLASS , ' $!bstack' , nqp :: clone ($ ! bstack )),
108
107
1 );
109
108
}
110
109
nqp ::bindattr_i($ new , $ ? CLASS , ' $!pos' , -3 );
111
110
pir::return__0PsiPPi(
112
- $ new ,
113
- nqp ::bindattr_s( $ new , $ ? CLASS , ' $!target' , $ ! target ),
111
+ $ new ,
112
+ nqp ::getattr_s( $ ! shared , ParseShared , ' $!target' ),
114
113
nqp ::bindattr_i($ new , $ ? CLASS , ' $!from' , $ ! pos ),
115
114
$ ? CLASS ,
116
115
nqp ::bindattr($ new , $ ? CLASS , ' $!bstack' , pir::new__Ps(' ResizableIntegerArray' )),
@@ -121,7 +120,6 @@ role NQPCursorRole is export {
121
120
method ! cursor_start_subcapture ($ from ) {
122
121
my $ new := nqp ::create(self );
123
122
nqp ::bindattr($ new , $ ? CLASS , ' $!shared' , $ ! shared );
124
- nqp ::bindattr_s($ new , $ ? CLASS , ' $!target' , $ ! target );
125
123
nqp ::bindattr_i($ new , $ ? CLASS , ' $!from' , $ from );
126
124
nqp ::bindattr_i($ new , $ ? CLASS , ' $!pos' , -3 );
127
125
$ new ;
@@ -181,7 +179,6 @@ role NQPCursorRole is export {
181
179
return self . " !cursor_next" () if % opts <ex >;
182
180
my $ new := self . CREATE();
183
181
nqp ::bindattr($ new , $ ? CLASS , ' $!shared' , $ ! shared );
184
- nqp ::bindattr_s($ new , $ ? CLASS , ' $!target' , $ ! target );
185
182
nqp ::bindattr_i($ new , $ ? CLASS , ' $!from' , -1 );
186
183
nqp ::bindattr_i($ new , $ ? CLASS , ' $!pos' ,
187
184
(% opts <ov > || $ ! from >= $ ! pos ) ?? $ ! from + 1 !! $ ! pos );
@@ -206,7 +203,7 @@ role NQPCursorRole is export {
206
203
method ! protoregex ($ name ) {
207
204
# Obtain and run NFA.
208
205
my $ nfa := self . HOW . cache(self , $ name , { self . ' !protoregex_nfa' ($ name ) });
209
- my @ fates := $ nfa . run ($ ! target , $ ! pos );
206
+ my @ fates := $ nfa . run (nqp ::getattr_s( $ ! shared , ParseShared, ' $! target' ) , $ ! pos );
210
207
211
208
# Visit rules in fate order.
212
209
my @ rxfate := $ nfa . states[0 ];
@@ -253,7 +250,7 @@ role NQPCursorRole is export {
253
250
254
251
method ! alt (int $ pos , str $ name , @ labels = []) {
255
252
my $ nfa := self . HOW . cache(self , $ name , { self . ' !alt_nfa' ($ ! regexsub , $ name ) });
256
- $ nfa . run_alt($ ! target , $ pos , $ ! bstack , $ ! cstack , @ labels );
253
+ $ nfa . run_alt(nqp ::getattr_s( $ ! shared , ParseShared, ' $! target' ) , $ pos , $ ! bstack , $ ! cstack , @ labels );
257
254
}
258
255
259
256
method ! alt_nfa ($ regex , str $ name ) {
@@ -302,20 +299,22 @@ role NQPCursorRole is export {
302
299
if $ n >= 0 {
303
300
my $ subcur := $ ! cstack [$ n ];
304
301
my int $ litlen := $ subcur . pos - $ subcur . from ;
302
+ my str $ target := nqp ::getattr_s($ ! shared , ParseShared, ' $!target' );
305
303
$ cur . " !cursor_pass" ($ ! pos + $ litlen , ' ' )
306
- if nqp :: substr ($ ! target , $ ! pos , $ litlen )
307
- eq nqp :: substr ($ ! target , $ subcur . from , $ litlen );
304
+ if nqp :: substr ($ target , $ ! pos , $ litlen )
305
+ eq nqp :: substr ($ target , $ subcur . from , $ litlen );
308
306
}
309
307
$ cur ;
310
308
}
311
309
312
310
method ! LITERAL (str $ str , int $ i = 0 ) {
313
311
my $ cur := self . " !cursor_start" ();
314
312
my int $ litlen := nqp :: chars ($ str );
313
+ my str $ target := nqp ::getattr_s($ ! shared , ParseShared, ' $!target' );
315
314
$ cur . " !cursor_pass" ($ ! pos + $ litlen )
316
315
if $ litlen < 1
317
- || ($ i ?? nqp :: lc (nqp :: substr ($ ! target , $ ! pos , $ litlen )) eq nqp :: lc ($ str )
318
- !! nqp :: substr ($ ! target , $ ! pos , $ litlen ) eq $ str );
316
+ || ($ i ?? nqp :: lc (nqp :: substr ($ target , $ ! pos , $ litlen )) eq nqp :: lc ($ str )
317
+ !! nqp :: substr ($ target , $ ! pos , $ litlen ) eq $ str );
319
318
$ cur ;
320
319
}
321
320
@@ -338,9 +337,12 @@ role NQPCursorRole is export {
338
337
# compile.
339
338
method after ($ regex ) {
340
339
my $ cur := self . " !cursor_start" ();
341
- nqp ::bindattr_s($ cur , $ ? CLASS , ' $!target' , $ ! target . reverse ());
342
- nqp ::bindattr_i($ cur , $ ? CLASS , ' $!from' , nqp :: chars ($ ! target ) - $ ! pos );
343
- nqp ::bindattr_i($ cur , $ ? CLASS , ' $!pos' , nqp :: chars ($ ! target ) - $ ! pos );
340
+ my str $ target := nqp ::getattr_s($ ! shared , ParseShared, ' $!target' );
341
+ my $ shared := pir::repr_clone__PP($ ! shared );
342
+ nqp ::bindattr_s($ shared , ParseShared, ' $!target' , $ target . reverse ());
343
+ nqp ::bindattr($ cur , $ ? CLASS , ' $!shared' , $ shared );
344
+ nqp ::bindattr_i($ cur , $ ? CLASS , ' $!from' , nqp :: chars ($ target ) - $ ! pos );
345
+ nqp ::bindattr_i($ cur , $ ? CLASS , ' $!pos' , nqp :: chars ($ target ) - $ ! pos );
344
346
nqp ::getattr_i($ regex ($ cur ), $ ? CLASS , ' $!pos' ) >= 0 ??
345
347
$ cur . " !cursor_pass" ($ ! pos , ' after' ) !!
346
348
nqp ::bindattr_i($ cur , $ ? CLASS , ' $!pos' , -3 );
@@ -349,131 +351,145 @@ role NQPCursorRole is export {
349
351
350
352
method ws () {
351
353
# skip over any whitespace, fail if between two word chars
354
+ my str $ target := nqp ::getattr_s($ ! shared , ParseShared, ' $!target' );
352
355
my $ cur := self . " !cursor_start" ();
353
- $ ! pos >= nqp :: chars ($ ! target )
356
+ $ ! pos >= nqp :: chars ($ target )
354
357
?? $ cur . " !cursor_pass" ($ ! pos , ' ws' )
355
358
!! ($ ! pos < 1
356
- || ! nqp ::iscclass(pir::const::CCLASS_WORD, $ ! target , $ ! pos )
357
- || ! nqp ::iscclass(pir::const::CCLASS_WORD, $ ! target , $ ! pos -1)
359
+ || ! nqp ::iscclass(pir::const::CCLASS_WORD, $ target , $ ! pos )
360
+ || ! nqp ::iscclass(pir::const::CCLASS_WORD, $ target , $ ! pos -1)
358
361
) && $ cur . " !cursor_pass" (
359
362
nqp ::findnotcclass(
360
- pir::const::CCLASS_WHITESPACE, $ ! target , $ ! pos , nqp :: chars ($ ! target )),
363
+ pir::const::CCLASS_WHITESPACE, $ target , $ ! pos , nqp :: chars ($ target )),
361
364
' ws' );
362
365
$ cur ;
363
366
}
364
367
365
368
method ww () {
366
369
my $ cur := self . " !cursor_start" ();
370
+ my str $ target := nqp ::getattr_s($ ! shared , ParseShared, ' $!target' );
367
371
$ cur . " !cursor_pass" ($ ! pos , " ww" )
368
372
if $ ! pos > 0
369
- && $ ! pos != nqp :: chars ($ ! target )
370
- && nqp ::iscclass(pir::const::CCLASS_WORD, $ ! target , $ ! pos )
371
- && nqp ::iscclass(pir::const::CCLASS_WORD, $ ! target , $ ! pos -1);
373
+ && $ ! pos != nqp :: chars ($ target )
374
+ && nqp ::iscclass(pir::const::CCLASS_WORD, $ target , $ ! pos )
375
+ && nqp ::iscclass(pir::const::CCLASS_WORD, $ target , $ ! pos -1);
372
376
$ cur ;
373
377
}
374
378
375
379
method wb () {
376
380
my $ cur := self . " !cursor_start" ();
381
+ my str $ target := nqp ::getattr_s($ ! shared , ParseShared, ' $!target' );
377
382
$ cur . " !cursor_pass" ($ ! pos , " wb" )
378
- if ($ ! pos == 0 && nqp ::iscclass(pir::const::CCLASS_WORD, $ ! target , $ ! pos ))
379
- || ($ ! pos == nqp :: chars ($ ! target )
380
- && nqp ::iscclass(pir::const::CCLASS_WORD, $ ! target , $ ! pos -1))
381
- || nqp ::iscclass(pir::const::CCLASS_WORD, $ ! target , $ ! pos -1)
382
- != nqp ::iscclass(pir::const::CCLASS_WORD, $ ! target , $ ! pos );
383
+ if ($ ! pos == 0 && nqp ::iscclass(pir::const::CCLASS_WORD, $ target , $ ! pos ))
384
+ || ($ ! pos == nqp :: chars ($ target )
385
+ && nqp ::iscclass(pir::const::CCLASS_WORD, $ target , $ ! pos -1))
386
+ || nqp ::iscclass(pir::const::CCLASS_WORD, $ target , $ ! pos -1)
387
+ != nqp ::iscclass(pir::const::CCLASS_WORD, $ target , $ ! pos );
383
388
$ cur ;
384
389
}
385
390
386
391
method ident () {
387
392
my $ cur := self . " !cursor_start" ();
393
+ my str $ target := nqp ::getattr_s($ ! shared , ParseShared, ' $!target' );
388
394
$ cur . " !cursor_pass" (
389
395
nqp ::findnotcclass(
390
396
pir::const::CCLASS_WORD,
391
- $ ! target , $ ! pos , nqp :: chars ($ ! target )))
392
- if $ ! pos < nqp :: chars ($ ! target ) &&
393
- (nqp :: ord ($ ! target , $ ! pos ) == 95
394
- || nqp ::iscclass(pir::const::CCLASS_ALPHABETIC, $ ! target , $ ! pos ));
397
+ $ target , $ ! pos , nqp :: chars ($ target )))
398
+ if $ ! pos < nqp :: chars ($ target ) &&
399
+ (nqp :: ord ($ target , $ ! pos ) == 95
400
+ || nqp ::iscclass(pir::const::CCLASS_ALPHABETIC, $ target , $ ! pos ));
395
401
$ cur ;
396
402
}
397
403
398
404
method alpha () {
399
405
my $ cur := self . " !cursor_start" ();
406
+ my str $ target := nqp ::getattr_s($ ! shared , ParseShared, ' $!target' );
400
407
$ cur . " !cursor_pass" ($ ! pos + 1, ' alpha' )
401
- if $ ! pos < nqp :: chars ($ ! target )
402
- && (nqp ::iscclass(pir::const::CCLASS_ALPHABETIC, $ ! target , $ ! pos )
403
- || nqp :: ord ($ ! target , $ ! pos ) == 95 );
408
+ if $ ! pos < nqp :: chars ($ target )
409
+ && (nqp ::iscclass(pir::const::CCLASS_ALPHABETIC, $ target , $ ! pos )
410
+ || nqp :: ord ($ target , $ ! pos ) == 95 );
404
411
$ cur ;
405
412
}
406
413
407
414
method alnum () {
408
415
my $ cur := self . " !cursor_start" ();
416
+ my str $ target := nqp ::getattr_s($ ! shared , ParseShared, ' $!target' );
409
417
$ cur . " !cursor_pass" ($ ! pos + 1, ' alnum' )
410
- if $ ! pos < nqp :: chars ($ ! target )
411
- && (nqp ::iscclass(pir::const::CCLASS_ALPHANUMERIC, $ ! target , $ ! pos )
412
- || nqp :: ord ($ ! target , $ ! pos ) == 95 );
418
+ if $ ! pos < nqp :: chars ($ target )
419
+ && (nqp ::iscclass(pir::const::CCLASS_ALPHANUMERIC, $ target , $ ! pos )
420
+ || nqp :: ord ($ target , $ ! pos ) == 95 );
413
421
$ cur ;
414
422
}
415
423
416
424
method upper () {
417
425
my $ cur := self . " !cursor_start" ();
426
+ my str $ target := nqp ::getattr_s($ ! shared , ParseShared, ' $!target' );
418
427
$ cur . " !cursor_pass" ($ ! pos + 1, ' upper' )
419
- if $ ! pos < nqp :: chars ($ ! target )
420
- && nqp ::iscclass(pir::const::CCLASS_UPPERCASE, $ ! target , $ ! pos );
428
+ if $ ! pos < nqp :: chars ($ target )
429
+ && nqp ::iscclass(pir::const::CCLASS_UPPERCASE, $ target , $ ! pos );
421
430
$ cur ;
422
431
}
423
432
424
433
method lower () {
425
434
my $ cur := self . " !cursor_start" ();
435
+ my str $ target := nqp ::getattr_s($ ! shared , ParseShared, ' $!target' );
426
436
$ cur . " !cursor_pass" ($ ! pos + 1, ' lower' )
427
- if $ ! pos < nqp :: chars ($ ! target )
428
- && nqp ::iscclass(pir::const::CCLASS_LOWERCASE, $ ! target , $ ! pos );
437
+ if $ ! pos < nqp :: chars ($ target )
438
+ && nqp ::iscclass(pir::const::CCLASS_LOWERCASE, $ target , $ ! pos );
429
439
$ cur ;
430
440
}
431
441
432
442
method digit () {
433
443
my $ cur := self . " !cursor_start" ();
444
+ my str $ target := nqp ::getattr_s($ ! shared , ParseShared, ' $!target' );
434
445
$ cur . " !cursor_pass" ($ ! pos + 1, ' digit' )
435
- if $ ! pos < nqp :: chars ($ ! target )
436
- && nqp ::iscclass(pir::const::CCLASS_NUMERIC, $ ! target , $ ! pos );
446
+ if $ ! pos < nqp :: chars ($ target )
447
+ && nqp ::iscclass(pir::const::CCLASS_NUMERIC, $ target , $ ! pos );
437
448
$ cur ;
438
449
}
439
450
440
451
method xdigit () {
441
452
my $ cur := self . " !cursor_start" ();
453
+ my str $ target := nqp ::getattr_s($ ! shared , ParseShared, ' $!target' );
442
454
$ cur . " !cursor_pass" ($ ! pos + 1, ' xdigit' )
443
- if $ ! pos < nqp :: chars ($ ! target )
444
- && nqp ::iscclass(pir::const::CCLASS_HEXADECIMAL, $ ! target , $ ! pos );
455
+ if $ ! pos < nqp :: chars ($ target )
456
+ && nqp ::iscclass(pir::const::CCLASS_HEXADECIMAL, $ target , $ ! pos );
445
457
$ cur ;
446
458
}
447
459
448
460
method space () {
449
461
my $ cur := self . " !cursor_start" ();
462
+ my str $ target := nqp ::getattr_s($ ! shared , ParseShared, ' $!target' );
450
463
$ cur . " !cursor_pass" ($ ! pos + 1, ' space' )
451
- if $ ! pos < nqp :: chars ($ ! target )
452
- && nqp ::iscclass(pir::const::CCLASS_WHITESPACE, $ ! target , $ ! pos );
464
+ if $ ! pos < nqp :: chars ($ target )
465
+ && nqp ::iscclass(pir::const::CCLASS_WHITESPACE, $ target , $ ! pos );
453
466
$ cur ;
454
467
}
455
468
456
469
method blank () {
457
470
my $ cur := self . " !cursor_start" ();
471
+ my str $ target := nqp ::getattr_s($ ! shared , ParseShared, ' $!target' );
458
472
$ cur . " !cursor_pass" ($ ! pos + 1, ' blank' )
459
- if $ ! pos < nqp :: chars ($ ! target )
460
- && nqp ::iscclass(pir::const::CCLASS_BLANK, $ ! target , $ ! pos );
473
+ if $ ! pos < nqp :: chars ($ target )
474
+ && nqp ::iscclass(pir::const::CCLASS_BLANK, $ target , $ ! pos );
461
475
$ cur ;
462
476
}
463
477
464
478
method cntrl () {
465
479
my $ cur := self . " !cursor_start" ();
480
+ my str $ target := nqp ::getattr_s($ ! shared , ParseShared, ' $!target' );
466
481
$ cur . " !cursor_pass" ($ ! pos + 1, ' cntrl' )
467
- if $ ! pos < nqp :: chars ($ ! target )
468
- && nqp ::iscclass(pir::const::CCLASS_CONTROL, $ ! target , $ ! pos );
482
+ if $ ! pos < nqp :: chars ($ target )
483
+ && nqp ::iscclass(pir::const::CCLASS_CONTROL, $ target , $ ! pos );
469
484
$ cur ;
470
485
}
471
486
472
487
method punct () {
473
488
my $ cur := self . " !cursor_start" ();
489
+ my str $ target := nqp ::getattr_s($ ! shared , ParseShared, ' $!target' );
474
490
$ cur . " !cursor_pass" ($ ! pos + 1, ' punct' )
475
- if $ ! pos < nqp :: chars ($ ! target )
476
- && nqp ::iscclass(pir::const::CCLASS_PUNCTUATION, $ ! target , $ ! pos );
491
+ if $ ! pos < nqp :: chars ($ target )
492
+ && nqp ::iscclass(pir::const::CCLASS_PUNCTUATION, $ target , $ ! pos );
477
493
$ cur ;
478
494
}
479
495
0 commit comments