Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
Add ability to smartmatch regex/token/rule (although anchoring is
Browse files Browse the repository at this point in the history
currently incorrect).
  • Loading branch information
pmichaud committed Dec 3, 2009
1 parent e6cb831 commit fe021de
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions build/Makefile.in
Expand Up @@ -44,6 +44,7 @@ REGEX_SOURCES = \
src/Regex/Cursor-builtins.pir \
src/Regex/Cursor-protoregex-peek.pir \
src/Regex/Match.pir \
src/Regex/Regex.pir \
src/Regex/Dumper.pir \
src/PAST/Regex.pir \
src/PAST/Compiler-Regex.pir \
Expand Down
13 changes: 11 additions & 2 deletions src/NQP/Actions.pm
Expand Up @@ -56,6 +56,7 @@ method statementlist($/) {
if $<statement> {
for $<statement> {
my $ast := $_.ast;
$ast := $ast<sink> if pir::defined($ast<sink>);
if $ast.isa(PAST::Block) && !$ast.blocktype {
$ast := block_immediate($ast);
}
Expand Down Expand Up @@ -521,9 +522,17 @@ method regex_declarator($/, $key?) {
);
}
else {
my $regex :=
Regex::P6Regex::Actions::buildsub($<p6regex>.ast, @BLOCK.shift);
$regex.name($name);
$past :=
Regex::P6Regex::Actions::buildsub($<p6regex>.ast, @BLOCK.shift);
$past.name($name);
PAST::Op.new(
:pasttype<callmethod>, :name<new>,
PAST::Var.new( :name('Regex'), :namespace(['Regex']), :scope<package> ),
$regex
);
# In sink context, we don't need the Regex::Regex object.
$past<sink> := $regex;
@MODIFIERS.shift;
}
make $past;
Expand Down
1 change: 1 addition & 0 deletions src/Regex.pir
Expand Up @@ -14,6 +14,7 @@ This file brings together the various Regex modules needed for Regex.pbc .
.include 'src/Regex/Cursor-builtins.pir'
.include 'src/Regex/Cursor-protoregex-peek.pir'
.include 'src/Regex/Match.pir'
.include 'src/Regex/Regex.pir'
.include 'src/Regex/Dumper.pir'

.include 'src/PAST/Regex.pir'
Expand Down
9 changes: 8 additions & 1 deletion t/nqp/45-smartmatch.t
Expand Up @@ -3,10 +3,17 @@
P6metaclass.register('Integer');
P6metaclass.register('ResizablePMCArray');

plan(4);
plan(8);
ok(3 ~~ Integer, "smartmatch of Integer works");
ok(!(4.5 ~~ Integer), "negative smartmatch of Integer works");

ok(<a b c> ~~ ResizablePMCArray, "smartmatch of RPA works");
ok(!(3 ~~ ResizablePMCArray), "negative smartmatch of RPA works");

my $match := 'abcdef' ~~ regex abc { c(.)e };

ok( $match, "simple smart match" );
ok( $match.from == 2, "match has correct .from" );
ok( $match.to == 5, "match has correct .to");
ok( $match eq 'cde', "match has correct string value" );

0 comments on commit fe021de

Please sign in to comment.