Skip to content

Commit

Permalink
Implement lookbehind.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnthn committed Oct 7, 2012
1 parent 5591588 commit 3442c18
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
46 changes: 30 additions & 16 deletions src/QRegex/P5Regex/Actions.nqp
Expand Up @@ -190,6 +190,20 @@ class QRegex::P5Regex::Actions is HLL::Actions {
QAST::SVal.new( :value(~$<number> - 1) ) ) );
}
}

method p5assertion:sym«<»($/) {
if $<nibbler> {
make QAST::Regex.new(
:rxtype<subrule>, :subtype<zerowidth>, :negate($<neg> eq '!'), :node($/),
QAST::Node.new(
QAST::SVal.new( :value('after') ),
qbuildsub(self.flip_ast($<nibbler>.ast), :anon(1), :addself(1))
));
}
else {
make 0;
}
}

method p5assertion:sym<=>($/) {
if $<nibbler> {
Expand Down Expand Up @@ -362,6 +376,22 @@ class QRegex::P5Regex::Actions is HLL::Actions {
nqp::deletekey(%capnames, '$!to');
%capnames;
}

method flip_ast($qast) {
return $qast unless nqp::istype($qast, QAST::Regex);
if $qast.rxtype eq 'literal' {
$qast[0] := $qast[0].reverse();
}
elsif $qast.rxtype eq 'concat' {
my @tmp;
while +@($qast) { @tmp.push(@($qast).shift) }
while @tmp { @($qast).push(self.flip_ast(@tmp.pop)) }
}
else {
for @($qast) { self.flip_ast($_) }
}
$qast
}


# XXX Below here copied from p6regex; needs review
Expand Down Expand Up @@ -549,20 +579,4 @@ class QRegex::P5Regex::Actions is HLL::Actions {
else { $ast.name($name); }
$ast.subtype('capture');
}

method flip_ast($qast) {
return $qast unless nqp::istype($qast, QAST::Regex);
if $qast.rxtype eq 'literal' {
$qast[0] := $qast[0].reverse();
}
elsif $qast.rxtype eq 'concat' {
my @tmp;
while +@($qast) { @tmp.push(@($qast).shift) }
while @tmp { @($qast).push(self.flip_ast(@tmp.pop)) }
}
else {
for @($qast) { self.flip_ast($_) }
}
$qast
}
}
1 change: 1 addition & 0 deletions src/QRegex/P5Regex/Grammar.nqp
Expand Up @@ -88,6 +88,7 @@ grammar QRegex::P5Regex::Grammar is HLL::Grammar {

proto token p5assertion { <...> }

token p5assertion:sym«<» { <sym> $<neg>=['='|'!'] [ <?before ')'> | <nibbler> ] }
token p5assertion:sym<=> { <sym> [ <?before ')'> | <nibbler> ] }
token p5assertion:sym<!> { <sym> [ <?before ')'> | <nibbler> ] }

Expand Down

0 comments on commit 3442c18

Please sign in to comment.