Skip to content

Commit 3b98f66

Browse files
committed
Move $!target into ParseShared.
This means we're now saving a pointer per Cursor.
1 parent 4e91efd commit 3b98f66

File tree

2 files changed

+75
-59
lines changed

2 files changed

+75
-59
lines changed

src/HLL/Grammar.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ An operator precedence parser.
801801
}
802802

803803
method LANG($lang, $regex, *@args) {
804-
my $lang_cursor := %*LANG{$lang}.'!cursor_init'(self.orig(), :p(self.pos()), :target(self.target()), :shared(self.'!shared'()));
804+
my $lang_cursor := %*LANG{$lang}.'!cursor_init'(self.orig(), :p(self.pos()), :shared(self.'!shared'()));
805805
if self.HOW.traced(self) {
806806
$lang_cursor.HOW.trace-on($lang_cursor, self.HOW.trace_depth(self));
807807
}

src/QRegex/Cursor.nqp

Lines changed: 74 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Some things that all cursors involved in a given parse share.
22
my class ParseShared {
33
has $!orig;
4+
has str $!target;
45
}
56

67
role NQPCursorRole is export {
78
has $!shared;
8-
has str $!target;
99
has int $!from;
1010
has int $!pos;
1111
has $!match;
@@ -16,7 +16,7 @@ role NQPCursorRole is export {
1616
has $!restart;
1717

1818
method orig() { nqp::getattr($!shared, ParseShared, '$!orig') }
19-
method target() { $!target }
19+
method target() { nqp::getattr_s($!shared, ParseShared, '$!target') }
2020
method from() { $!from }
2121
method pos() { $!pos }
2222

@@ -70,16 +70,15 @@ role NQPCursorRole is export {
7070
$caps;
7171
}
7272

73-
method !cursor_init($orig, :$p = 0, :$c, :$target, :$shared) {
73+
method !cursor_init($orig, :$p = 0, :$c, :$shared) {
7474
my $new := self.CREATE();
7575
unless $shared {
7676
$shared := nqp::create(ParseShared);
7777
nqp::bindattr($shared, ParseShared, '$!orig', $orig);
78+
nqp::bindattr_s($shared, ParseShared, '$!target',
79+
pir::trans_encoding__Ssi($orig, pir::find_encoding__Is('ucs4')));
7880
}
7981
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')));
8382
if nqp::defined($c) {
8483
nqp::bindattr_i($new, $?CLASS, '$!from', -1);
8584
nqp::bindattr_i($new, $?CLASS, '$!pos', $c);
@@ -101,16 +100,16 @@ role NQPCursorRole is export {
101100
nqp::bindattr($new, $?CLASS, '$!cstack', nqp::clone($!cstack)) if $!cstack;
102101
pir::return__0PsiPPi(
103102
$new,
104-
nqp::bindattr_s($new, $?CLASS, '$!target', $!target),
103+
nqp::getattr_s($!shared, ParseShared, '$!target'),
105104
nqp::bindattr_i($new, $?CLASS, '$!from', $!from),
106105
$?CLASS,
107106
nqp::bindattr($new, $?CLASS, '$!bstack', nqp::clone($!bstack)),
108107
1);
109108
}
110109
nqp::bindattr_i($new, $?CLASS, '$!pos', -3);
111110
pir::return__0PsiPPi(
112-
$new,
113-
nqp::bindattr_s($new, $?CLASS, '$!target', $!target),
111+
$new,
112+
nqp::getattr_s($!shared, ParseShared, '$!target'),
114113
nqp::bindattr_i($new, $?CLASS, '$!from', $!pos),
115114
$?CLASS,
116115
nqp::bindattr($new, $?CLASS, '$!bstack', pir::new__Ps('ResizableIntegerArray')),
@@ -121,7 +120,6 @@ role NQPCursorRole is export {
121120
method !cursor_start_subcapture($from) {
122121
my $new := nqp::create(self);
123122
nqp::bindattr($new, $?CLASS, '$!shared', $!shared);
124-
nqp::bindattr_s($new, $?CLASS, '$!target', $!target);
125123
nqp::bindattr_i($new, $?CLASS, '$!from', $from);
126124
nqp::bindattr_i($new, $?CLASS, '$!pos', -3);
127125
$new;
@@ -181,7 +179,6 @@ role NQPCursorRole is export {
181179
return self."!cursor_next"() if %opts<ex>;
182180
my $new := self.CREATE();
183181
nqp::bindattr($new, $?CLASS, '$!shared', $!shared);
184-
nqp::bindattr_s($new, $?CLASS, '$!target', $!target);
185182
nqp::bindattr_i($new, $?CLASS, '$!from', -1);
186183
nqp::bindattr_i($new, $?CLASS, '$!pos',
187184
(%opts<ov> || $!from >= $!pos) ?? $!from+1 !! $!pos);
@@ -206,7 +203,7 @@ role NQPCursorRole is export {
206203
method !protoregex($name) {
207204
# Obtain and run NFA.
208205
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);
210207

211208
# Visit rules in fate order.
212209
my @rxfate := $nfa.states[0];
@@ -253,7 +250,7 @@ role NQPCursorRole is export {
253250

254251
method !alt(int $pos, str $name, @labels = []) {
255252
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);
257254
}
258255

259256
method !alt_nfa($regex, str $name) {
@@ -302,20 +299,22 @@ role NQPCursorRole is export {
302299
if $n >= 0 {
303300
my $subcur := $!cstack[$n];
304301
my int $litlen := $subcur.pos - $subcur.from;
302+
my str $target := nqp::getattr_s($!shared, ParseShared, '$!target');
305303
$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);
308306
}
309307
$cur;
310308
}
311309

312310
method !LITERAL(str $str, int $i = 0) {
313311
my $cur := self."!cursor_start"();
314312
my int $litlen := nqp::chars($str);
313+
my str $target := nqp::getattr_s($!shared, ParseShared, '$!target');
315314
$cur."!cursor_pass"($!pos + $litlen)
316315
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);
319318
$cur;
320319
}
321320

@@ -338,9 +337,12 @@ role NQPCursorRole is export {
338337
# compile.
339338
method after($regex) {
340339
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);
344346
nqp::getattr_i($regex($cur), $?CLASS, '$!pos') >= 0 ??
345347
$cur."!cursor_pass"($!pos, 'after') !!
346348
nqp::bindattr_i($cur, $?CLASS, '$!pos', -3);
@@ -349,131 +351,145 @@ role NQPCursorRole is export {
349351

350352
method ws() {
351353
# skip over any whitespace, fail if between two word chars
354+
my str $target := nqp::getattr_s($!shared, ParseShared, '$!target');
352355
my $cur := self."!cursor_start"();
353-
$!pos >= nqp::chars($!target)
356+
$!pos >= nqp::chars($target)
354357
?? $cur."!cursor_pass"($!pos, 'ws')
355358
!! ($!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)
358361
) && $cur."!cursor_pass"(
359362
nqp::findnotcclass(
360-
pir::const::CCLASS_WHITESPACE, $!target, $!pos, nqp::chars($!target)),
363+
pir::const::CCLASS_WHITESPACE, $target, $!pos, nqp::chars($target)),
361364
'ws');
362365
$cur;
363366
}
364367

365368
method ww() {
366369
my $cur := self."!cursor_start"();
370+
my str $target := nqp::getattr_s($!shared, ParseShared, '$!target');
367371
$cur."!cursor_pass"($!pos, "ww")
368372
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);
372376
$cur;
373377
}
374378

375379
method wb() {
376380
my $cur := self."!cursor_start"();
381+
my str $target := nqp::getattr_s($!shared, ParseShared, '$!target');
377382
$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);
383388
$cur;
384389
}
385390

386391
method ident() {
387392
my $cur := self."!cursor_start"();
393+
my str $target := nqp::getattr_s($!shared, ParseShared, '$!target');
388394
$cur."!cursor_pass"(
389395
nqp::findnotcclass(
390396
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));
395401
$cur;
396402
}
397403

398404
method alpha() {
399405
my $cur := self."!cursor_start"();
406+
my str $target := nqp::getattr_s($!shared, ParseShared, '$!target');
400407
$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);
404411
$cur;
405412
}
406413

407414
method alnum() {
408415
my $cur := self."!cursor_start"();
416+
my str $target := nqp::getattr_s($!shared, ParseShared, '$!target');
409417
$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);
413421
$cur;
414422
}
415423

416424
method upper() {
417425
my $cur := self."!cursor_start"();
426+
my str $target := nqp::getattr_s($!shared, ParseShared, '$!target');
418427
$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);
421430
$cur;
422431
}
423432

424433
method lower() {
425434
my $cur := self."!cursor_start"();
435+
my str $target := nqp::getattr_s($!shared, ParseShared, '$!target');
426436
$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);
429439
$cur;
430440
}
431441

432442
method digit() {
433443
my $cur := self."!cursor_start"();
444+
my str $target := nqp::getattr_s($!shared, ParseShared, '$!target');
434445
$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);
437448
$cur;
438449
}
439450

440451
method xdigit() {
441452
my $cur := self."!cursor_start"();
453+
my str $target := nqp::getattr_s($!shared, ParseShared, '$!target');
442454
$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);
445457
$cur;
446458
}
447459

448460
method space() {
449461
my $cur := self."!cursor_start"();
462+
my str $target := nqp::getattr_s($!shared, ParseShared, '$!target');
450463
$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);
453466
$cur;
454467
}
455468

456469
method blank() {
457470
my $cur := self."!cursor_start"();
471+
my str $target := nqp::getattr_s($!shared, ParseShared, '$!target');
458472
$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);
461475
$cur;
462476
}
463477

464478
method cntrl() {
465479
my $cur := self."!cursor_start"();
480+
my str $target := nqp::getattr_s($!shared, ParseShared, '$!target');
466481
$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);
469484
$cur;
470485
}
471486

472487
method punct() {
473488
my $cur := self."!cursor_start"();
489+
my str $target := nqp::getattr_s($!shared, ParseShared, '$!target');
474490
$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);
477493
$cur;
478494
}
479495

0 commit comments

Comments
 (0)