Skip to content

Commit

Permalink
Add /.../ form of regexes and matching.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmichaud committed Dec 5, 2009
1 parent df1c1da commit 5a0677a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/NQP/Actions.pm
Expand Up @@ -695,6 +695,29 @@ method quote:sym<Q:PIR>($/) {
:node($/) );
}

method quote:sym</ />($/, $key?) {
if $key eq 'open' {
Q:PIR {
null $P0
set_hll_global ['Regex';'P6Regex';'Actions'], '$REGEXNAME', $P0
};
@BLOCK[0].symbol('$¢', :scope('lexical'));
@BLOCK[0].symbol('$/', :scope('lexical'));
return 0;
}
my $regex :=
Regex::P6Regex::Actions::buildsub($<p6regex>.ast, @BLOCK.shift);
my $past :=
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;
make $past;
}

method quote_escape:sym<$>($/) { make $<variable>.ast; }
method quote_escape:sym<{ }>($/) {
make PAST::Op.new(
Expand Down
7 changes: 7 additions & 0 deletions src/NQP/Grammar.pm
Expand Up @@ -383,6 +383,13 @@ token quote:sym<q> { 'q' <![(]> <.ws> <quote_EXPR: ':q'> }
token quote:sym<qq> { 'qq' <![(]> <.ws> <quote_EXPR: ':qq'> }
token quote:sym<Q> { 'Q' <![(]> <.ws> <quote_EXPR> }
token quote:sym<Q:PIR> { 'Q:PIR' <.ws> <quote_EXPR> }
token quote:sym</ /> {
'/'
<.newpad>
{*} #= open
<p6regex=.LANG('Regex','nibbler')>
'/'
}

token quote_escape:sym<$> { <?[$]> <?quotemod_check('s')> <variable> }
token quote_escape:sym<{ }> { <?[{]> <?quotemod_check('c')> <block> }
Expand Down
8 changes: 7 additions & 1 deletion t/nqp/45-smartmatch.t
Expand Up @@ -3,7 +3,7 @@
P6metaclass.register('Integer');
P6metaclass.register('ResizablePMCArray');

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

Expand All @@ -20,3 +20,9 @@ ok( $match eq 'cde', "match has correct string value" );
$match := 'abcdef' ~~ regex abc { c(.)e };
ok( !$match, "'regex' form doesn't do :c-like scanning" );

$match := 'abcdef' ~~ / c(.)e /;
ok( $match, "simple smart match, scanning form" );
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 5a0677a

Please sign in to comment.