Skip to content

Commit e32ad16

Browse files
committed
finish s/NQPCursor/NQPMatch/ tranformation
NQPMatch is now both a cursor and a match object. (The name 'NQPCursor' still exists but is an alias to NQPMatch now.) A new method is provided to clone match information for when we need a new NQPMatch identity for operator reductions. (The rakudo compiler also uses this for its old 'AS_MATCH' method.) The )> regex assertion is now passed on the cstack as '$!pos' rather than as '$!to', since at that level NQPMatch is more like a cursor than a match. Now passes all nqp tests again.
1 parent 2a31c20 commit e32ad16

File tree

5 files changed

+52
-41
lines changed

5 files changed

+52
-41
lines changed

src/HLL/Grammar.nqp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ An operator precedence parser.
368368

369369
method EXPR(str $preclim = '', int :$noinfix = 0) {
370370
my $here := self.'!cursor_start_cur'();
371-
my int $pos := nqp::getattr_i($here, NQPCursor, '$!from');
371+
my int $pos := nqp::getattr_i($here, NQPMatch, '$!from');
372372
my str $termishrx := 'termish';
373373
my @opstack;
374374
my @termstack;
@@ -388,10 +388,10 @@ An operator precedence parser.
388388
my int $term_done;
389389

390390
while 1 {
391-
nqp::bindattr_i($here, NQPCursor, '$!pos', $pos);
391+
nqp::bindattr_i($here, NQPMatch, '$!pos', $pos);
392392
$termcur := $here."$termishrx"();
393-
$pos := nqp::getattr_i($termcur, NQPCursor, '$!pos');
394-
nqp::bindattr_i($here, NQPCursor, '$!pos', $pos);
393+
$pos := nqp::getattr_i($termcur, NQPMatch, '$!pos');
394+
nqp::bindattr_i($here, NQPMatch, '$!pos', $pos);
395395
if $pos < 0 {
396396
$here.panic('Missing required term after infix') if @opstack;
397397
return $here;
@@ -443,18 +443,18 @@ An operator precedence parser.
443443
while $more_infix {
444444
# Now see if we can fetch an infix operator
445445
# First, we need ws to match.
446-
nqp::bindattr_i($here, NQPCursor, '$!pos', $pos);
446+
nqp::bindattr_i($here, NQPMatch, '$!pos', $pos);
447447
$wscur := $here.ws();
448-
$pos := nqp::getattr_i($wscur, NQPCursor, '$!pos');
448+
$pos := nqp::getattr_i($wscur, NQPMatch, '$!pos');
449449
if $pos < 0 {
450450
$term_done := 1;
451451
last;
452452
}
453453

454454
# Next, try the infix itself.
455-
nqp::bindattr_i($here, NQPCursor, '$!pos', $pos);
455+
nqp::bindattr_i($here, NQPMatch, '$!pos', $pos);
456456
$infixcur := $here.infixish();
457-
$pos := nqp::getattr_i($infixcur, NQPCursor, '$!pos');
457+
$pos := nqp::getattr_i($infixcur, NQPMatch, '$!pos');
458458
if $pos < 0 {
459459
$term_done := 1;
460460
last;
@@ -510,20 +510,19 @@ An operator precedence parser.
510510
}
511511

512512
nqp::push(@opstack, $infix); # The Shift
513-
nqp::bindattr_i($here, NQPCursor, '$!pos', $pos);
513+
nqp::bindattr_i($here, NQPMatch, '$!pos', $pos);
514514
$wscur := $here.ws();
515-
$pos := nqp::getattr_i($wscur, NQPCursor, '$!pos');
516-
nqp::bindattr_i($here, NQPCursor, '$!pos', $pos);
515+
$pos := nqp::getattr_i($wscur, NQPMatch, '$!pos');
516+
nqp::bindattr_i($here, NQPMatch, '$!pos', $pos);
517517
return $here if $pos < 0;
518518
}
519519

520520
self.EXPR_reduce(@termstack, @opstack) while @opstack;
521-
$pos := nqp::getattr_i($here, NQPCursor, '$!pos');
522-
$here := nqp::pop(@termstack);
523-
$here.'!cursor_pass'($pos);
524-
nqp::bindattr_i($here, NQPCursor, '$!pos', $pos);
525-
$here.'!reduce'('EXPR');
526-
$here;
521+
522+
self.'!clone_match_at'(
523+
nqp::pop(@termstack),
524+
nqp::getattr_i($here, NQPMatch, '$!pos')
525+
).'!reduce'('EXPR')
527526
}
528527

529528
method EXPR_reduce(@termstack, @opstack) {
@@ -584,7 +583,7 @@ An operator precedence parser.
584583

585584
method MARKER(str $markname) {
586585
my %markhash := nqp::getattr(
587-
nqp::getattr(self, NQPCursor, '$!shared'),
586+
nqp::getattr(self, NQPMatch, '$!shared'),
588587
ParseShared, '%!marks');
589588
my $cur := nqp::atkey(%markhash, $markname);
590589
if nqp::isnull($cur) {
@@ -593,18 +592,18 @@ An operator precedence parser.
593592
nqp::bindkey(%markhash, $markname, $cur);
594593
}
595594
else {
596-
nqp::bindattr_i($cur, NQPCursor, '$!from', self.from);
595+
nqp::bindattr_i($cur, NQPMatch, '$!from', self.from);
597596
$cur."!cursor_pos"(self.pos());
598597
$cur
599598
}
600599
}
601600

602601
method MARKED(str $markname) {
603602
my %markhash := nqp::getattr(
604-
nqp::getattr(self, NQPCursor, '$!shared'),
603+
nqp::getattr(self, NQPMatch, '$!shared'),
605604
ParseShared, '%!marks');
606605
my $cur := nqp::atkey(%markhash, $markname);
607-
unless nqp::istype($cur, NQPCursor) && $cur.pos() == self.pos() {
606+
unless nqp::istype($cur, NQPMatch) && $cur.pos() == self.pos() {
608607
$cur := self.'!cursor_start_fail'();
609608
}
610609
$cur

src/NQP/Actions.nqp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ class NQP::Actions is HLL::Actions {
658658
}
659659
}
660660
elsif nqp::can($how, 'set_default_parent') {
661-
my $default := $*PKGDECL eq 'grammar' ?? ['NQPCursor'] !! ['NQPMu'];
661+
my $default := $*PKGDECL eq 'grammar' ?? ['NQPMatch'] !! ['NQPMu'];
662662
$*W.pkg_add_parent_or_role($package, "set_default_parent",
663663
$*W.find_sym($default));
664664
}
@@ -1932,7 +1932,7 @@ class NQP::RegexActions is QRegex::P6Regex::Actions {
19321932
method set_cursor_type($qast) {
19331933
my $cursor_type := nqp::null();
19341934
try {
1935-
$cursor_type := $*W.find_sym(['NQPCursor']);
1935+
$cursor_type := $*W.find_sym(['NQPMatch']);
19361936
};
19371937
$qast.cursor_type($cursor_type) unless nqp::isnull($cursor_type);
19381938
}

src/QRegex/Cursor.nqp

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ my class Braid is export {
6565
}
6666
}
6767

68-
role NQPCursorRole is export {
68+
role NQPMatchRole is export {
6969
has $!shared;
7070
has int $!from;
7171
has int $!pos;
@@ -610,6 +610,18 @@ role NQPCursorRole is export {
610610
$!regexsub($new);
611611
}
612612

613+
method !clone_match_at($term, int $pos) {
614+
my $new := self.'!cursor_start_cur'();
615+
$new.'!cursor_pass'($pos);
616+
nqp::bindattr_i($new, NQPMatch, '$!pos', $pos);
617+
nqp::bindattr_i($new, NQPMatch, '$!from', nqp::getattr($term, NQPMatch, '$!from' ));
618+
nqp::bindattr( $new, NQPMatch, '$!made', nqp::getattr($term, NQPMatch, '$!made' ));
619+
nqp::bindattr( $new, NQPCapture, '@!array', nqp::getattr($term, NQPCapture, '@!array'));
620+
nqp::bindattr( $new, NQPCapture, '%!hash', nqp::getattr($term, NQPCapture, '%!hash' ));
621+
nqp::bindattr( $new, NQPMatch, '$!match', nqp::getattr($term, NQPMatch, '$!match'));
622+
$new;
623+
}
624+
613625
method !reduce(str $name) {
614626
my $actions := self.actions;
615627
nqp::findmethod($actions, $name)($actions, self.MATCH)
@@ -1100,24 +1112,24 @@ class NQPOldMatch {
11001112
method Bool() { 1 }
11011113
}
11021114

1103-
class NQPCursor is NQPCapture does NQPCursorRole {
1115+
class NQPMatch is NQPCapture does NQPMatchRole {
11041116
my @EMPTY_LIST := [];
11051117
my $NO_CAPS := nqp::hash();
11061118
method MATCH() {
1107-
my $match := nqp::getattr(self, NQPCursor, '$!match');
1119+
my $match := nqp::getattr(self, NQPMatch, '$!match');
11081120
if nqp::isnull($match) || (!nqp::istype($match, NQPOldMatch) && !nqp::ishash($match)) {
11091121
# Set up basic state of (old) Match.
11101122
my $list;
11111123
my $hash := nqp::hash();
11121124
$match := nqp::create(NQPOldMatch);
1113-
nqp::bindattr(self, NQPCursor, '$!match',
1114-
nqp::getattr_i(self, NQPCursor, '$!pos') >= nqp::getattr_i(self, NQPCursor, '$!from')
1125+
nqp::bindattr(self, NQPMatch, '$!match',
1126+
nqp::getattr_i(self, NQPMatch, '$!pos') >= nqp::getattr_i(self, NQPMatch, '$!from')
11151127
?? $match
11161128
!! nqp::null());
11171129

11181130
# For captures with lists, initialize the lists.
11191131
my %caplist := $NO_CAPS;
1120-
my $rxsub := nqp::getattr(self, NQPCursor, '$!regexsub');
1132+
my $rxsub := nqp::getattr(self, NQPMatch, '$!regexsub');
11211133
my str $onlyname := '';
11221134
my int $namecount := 0;
11231135
if !nqp::isnull($rxsub) && nqp::defined($rxsub) {
@@ -1141,7 +1153,7 @@ class NQPCursor is NQPCapture does NQPCursorRole {
11411153
}
11421154

11431155
# Walk the Cursor stack and populate the Cursor.
1144-
my $cs := nqp::getattr(self, NQPCursor, '$!cstack');
1156+
my $cs := nqp::getattr(self, NQPMatch, '$!cstack');
11451157
if nqp::isnull($cs) || !nqp::istrue($cs) {}
11461158
elsif $namecount == 1 && $onlyname ne '' && !nqp::eqat($onlyname, '$!', 0) {
11471159
# If there's only one destination, avoid repeated hash lookups
@@ -1173,8 +1185,8 @@ class NQPCursor is NQPCapture does NQPCursorRole {
11731185
my str $name := nqp::getattr_s($subcur, $?CLASS, '$!name');
11741186
if !nqp::isnull_s($name) && nqp::defined($name) && $name ne '' {
11751187
my $submatch := $subcur.MATCH();
1176-
if nqp::ord($name) == 36 && ($name eq '$!from' || $name eq '$!to') {
1177-
nqp::bindattr_i($match, NQPOldMatch, $name, $submatch.from);
1188+
if nqp::ord($name) == 36 && ($name eq '$!from' || $name eq '$!pos') {
1189+
nqp::bindattr_i(self, NQPMatch, $name, $submatch.from);
11781190
}
11791191
elsif nqp::index($name, '=') < 0 {
11801192
my int $needs_list := %caplist{$name} >= 2;
@@ -1239,9 +1251,9 @@ class NQPCursor is NQPCapture does NQPCursorRole {
12391251

12401252
# Once we've produced the captures, and if we know we're finished and
12411253
# will never be backtracked into, we can release cstack and regexsub.
1242-
unless nqp::defined(nqp::getattr(self, NQPCursor, '$!bstack')) {
1243-
nqp::bindattr(self, NQPCursor, '$!cstack', nqp::null());
1244-
nqp::bindattr(self, NQPCursor, '$!regexsub', nqp::null());
1254+
unless nqp::defined(nqp::getattr(self, NQPMatch, '$!bstack')) {
1255+
nqp::bindattr(self, NQPMatch, '$!cstack', nqp::null());
1256+
nqp::bindattr(self, NQPMatch, '$!regexsub', nqp::null());
12451257
}
12461258
}
12471259
self
@@ -1322,15 +1334,15 @@ class NQPCursor is NQPCapture does NQPCursorRole {
13221334
}
13231335
}
13241336

1325-
my constant NQPMatch := NQPCursor;
1337+
my constant NQPCursor := NQPMatch;
13261338

13271339
class NQPRegexMethod {
13281340
has $!code;
13291341
method new($code) {
13301342
self.bless(:code($code));
13311343
}
13321344
multi method ACCEPTS(NQPRegexMethod:D $self: $target) {
1333-
NQPCursor.parse($target, :rule(self))
1345+
NQPMatch.parse($target, :rule(self))
13341346
}
13351347
method name() {
13361348
nqp::getcodename($!code)
@@ -1343,7 +1355,7 @@ nqp::setinvokespec(NQPRegexMethod, NQPRegexMethod, '$!code', nqp::null);
13431355

13441356
class NQPRegex is NQPRegexMethod {
13451357
multi method ACCEPTS(NQPRegex:D $self: $target) {
1346-
NQPCursor.parse($target, :rule(self), :c(0))
1358+
NQPMatch.parse($target, :rule(self), :c(0))
13471359
}
13481360
}
13491361
nqp::setinvokespec(NQPRegex, NQPRegexMethod, '$!code', nqp::null);

src/QRegex/P5Regex/Actions.nqp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ class QRegex::P5Regex::Actions is HLL::Actions {
432432
}
433433
%capnames{''} := $count;
434434
nqp::deletekey(%capnames, '$!from');
435-
nqp::deletekey(%capnames, '$!to');
435+
nqp::deletekey(%capnames, '$!pos');
436436
%capnames;
437437
}
438438

@@ -504,7 +504,7 @@ class QRegex::P5Regex::Actions is HLL::Actions {
504504

505505
method metachar:sym<to>($/) {
506506
make QAST::Regex.new( :rxtype<subrule>, :subtype<capture>,
507-
:backtrack<r>, :name<$!to>, :node($/),
507+
:backtrack<r>, :name<$!pos>, :node($/),
508508
QAST::NodeList.new(
509509
QAST::SVal.new( :value('!LITERAL') ),
510510
QAST::SVal.new( :value('') ) ) );

src/QRegex/P6Regex/Actions.nqp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ class QRegex::P6Regex::Actions is HLL::Actions {
273273

274274
method metachar:sym<to>($/) {
275275
make QAST::Regex.new( :rxtype<subrule>, :subtype<capture>,
276-
:backtrack<r>, :name<$!to>, :node($/),
276+
:backtrack<r>, :name<$!pos>, :node($/),
277277
QAST::NodeList.new(
278278
QAST::SVal.new( :value('!LITERAL') ),
279279
QAST::SVal.new( :value('') ) ) );

0 commit comments

Comments
 (0)