Skip to content

Commit

Permalink
Implement first cut of s/// syntax. Uses .subst under the hood, so it…
Browse files Browse the repository at this point in the history
…'s as capable/incapable as .subst.
  • Loading branch information
jnthn committed Mar 16, 2010
1 parent f676705 commit 8b09f69
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions build/Makefile.in
Expand Up @@ -195,6 +195,7 @@ CORE_SOURCES = \
src/core/Regex.pm \
src/core/Junction.pm \
src/core/Grammar.pm \
src/core/Substitution.pm \
src/core/system.pm \
src/cheats/match-bool.pm \
src/cheats/setup-io.pm \
Expand Down
24 changes: 24 additions & 0 deletions src/Perl6/Actions.pm
Expand Up @@ -2132,6 +2132,30 @@ method quote:sym<m>($/) {
make create_code_object($past, 'Regex', 0, '');
}

method quote:sym<s>($/) {
# Build the regex.
my $regex_ast := Regex::P6Regex::Actions::buildsub($<p6regex>.ast);
my $regex := create_code_object($regex_ast, 'Regex', 0, '');

# Quote needs to be closure-i-fied.
my $closure_ast := PAST::Block.new(
PAST::Stmts.new(),
PAST::Stmts.new(
$<quote_EXPR>.ast
)
);
my $closure := create_code_object($closure_ast, 'Block', 0, '');

# Make a Substitution.
$regex.named('matcher');
$closure.named('replacer');
make PAST::Op.new(
:pasttype('callmethod'), :name('new'),
PAST::Var.new( :name('Substitution'), :scope('package') ),
$regex, $closure
);
}

method quote_escape:sym<$>($/) {
#make $<variable>.ast;
# my $a = 3; say "$a".WHAT # Gives Int, not Str with the above. Force
Expand Down
8 changes: 7 additions & 1 deletion src/Perl6/Grammar.pm
Expand Up @@ -989,7 +989,13 @@ token quote:sym<m> {
| '{'<p6regex=.LANG('Regex','nibbler')>'}'
]
}

token quote:sym<s> {
<sym> >>
'/'
<p6regex=.LANG('Regex','nibbler')>
<?[/]>
<quote_EXPR: ':qq'>
}

token quote_escape:sym<$> { <?[$]> <?quotemod_check('s')> <variable> }
token quote_escape:sym<{ }> { <?[{]> <?quotemod_check('c')> <block> }
Expand Down
8 changes: 8 additions & 0 deletions src/core/Substitution.pm
@@ -0,0 +1,8 @@
class Substitution {
has $!matcher;
has $!replacer;

method ACCEPTS($topic is rw) {
$topic = $topic.subst($!matcher, $!replacer);
}
}
6 changes: 6 additions & 0 deletions src/core/operators.pm
Expand Up @@ -11,6 +11,12 @@ our multi infix:<~~>(Mu $topic, Regex $matcher) {
};
}
class Substitution { ... }
our multi infix:<~~>(Mu $topic is rw, Substitution $matcher) {
$matcher.ACCEPTS($topic)
}


our multi infix:<!~~>(Mu $topic, Mu $matcher) {
$matcher.REJECTS($topic)
}
Expand Down

0 comments on commit 8b09f69

Please sign in to comment.