Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[dotnet] implement regex subrule compilation
[P6Objects] implement named subrule "before"
add a test.
  • Loading branch information
diakopter committed Dec 6, 2010
1 parent ce8a2fe commit 55673ae
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
16 changes: 16 additions & 0 deletions common/NQP/P6Objects.pm
Expand Up @@ -130,6 +130,12 @@ class Regex::Cursor {
self.CREATE();
}

method Bool() {
nqp::repr_defined($!match)
?? ?$!match
!! 0
}

method CREATE() {
nqp::instance_of(self)
}
Expand Down Expand Up @@ -491,6 +497,16 @@ class Regex::Cursor {
$!debug := nqp::repr_defined($arg) ?? $arg !! $TRUE;
1
}

method before($regex?) {
my @ret := self.cursor_start;
my $cur := @ret[0];
my $pos := @ret[1];

$cur.cursor_pass($pos, 'before')
if nqp::repr_defined($regex) && $regex($cur);
$cur;
}
}

class Regex::Regex {
Expand Down
18 changes: 15 additions & 3 deletions dotnet/compiler/PAST2DNSTCompiler.pm
Expand Up @@ -1625,7 +1625,6 @@ our multi sub dnst_regex(PAST::Regex $r) {
#my $subdnst := $posargs.shift;

my $negate := $r.negate;
my $testop := $negate ?? 'if' !! 'unless';

my $subtype := $r.subtype;
my $backtrack := $r.backtrack;
Expand All @@ -1634,12 +1633,25 @@ our multi sub dnst_regex(PAST::Regex $r) {

$stmts.push($cdnst);

my $call := dnst_for(PAST::Op.new(
:pasttype('callmethod'), :name('Bool'),
cursorop($name, $posargs)
));

# the logic here *appears* inverted because of the if_then.
$call := dnst_for(PAST::Op.new(
:pasttype('call'), :name('&prefix:<!>'),
$call
)) unless $negate;

$stmts.push(if_then(:bool(0),
unbox('int', cursorop($name, $posargs)),
unbox('int', $call),
$*re_fail
));

if $subtype ne 'zerowidth' {
if $subtype eq 'zerowidth' {

} else {

}
}
Expand Down
2 changes: 2 additions & 0 deletions t/nqp/45-smartmatch.t
Expand Up @@ -28,3 +28,5 @@ ok(("abcd" ~~ /[[a|ab]|abc]d/) eq 'abcd', 'deep backtracking works');
ok(("bbbbac" ~~ /[b+ b a $] | bbbbac/) eq 'bbbbac', 'greedy quantifier works');

ok(("bbbbac" ~~ /[b+? b ac]/) eq 'bbbbac', 'frugal quantifier works');

ok(("ab" ~~ /<?before ab> a/) eq 'a', 'subrule "before" works');

0 comments on commit 55673ae

Please sign in to comment.