Skip to content

Commit

Permalink
Implement nonconstant strings in regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Oct 16, 2010
1 parent 3a957b5 commit b4b25d0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/Niecza/Actions.pm
Expand Up @@ -629,13 +629,23 @@ sub metachar__S_Single_Single { my ($cl, $M) = @_;

sub metachar__S_Double_Double { my ($cl, $M) = @_;
if (! $M->{quote}{_ast}->isa('Op::StringLiteral')) {
$M->sorry("Interpolating strings in regexes NYI");
$M->{_ast} = RxOp::VarString->new(value => $M->{quote}{_ast});
return;
}
$M->{_ast} = RxOp::String->new(text => $M->{quote}{_ast}->text,
igcase => $::RX{i}, igmark => $::RX{a});
}

sub metachar__S_var { my ($cl, $M) = @_;
if ($M->{quantified_atom}) {
$M->sorry("Explicit regex bindings NYI");
$M->{_ast} = RxOp::Sequence->new;
return;
}
$M->{_ast} = RxOp::VarString->new(value =>
$cl->do_variable_reference($M, $M->{variable}{_ast}));
}

sub rxcapturize { my ($cl, $name, $rxop) = @_;
if (!$rxop->isa('RxOp::Subrule')) {
# <before>, etc. Not yet really handled XXX
Expand Down
20 changes: 20 additions & 0 deletions src/RxOp.pm
Expand Up @@ -59,6 +59,26 @@ use CgOp;
no Moose;
}

{
package RxOp::VarString;
use Moose;
extends 'RxOp';

has value => (isa => 'Op', is => 'ro', required => 1);
sub opzyg { $_[0]->value }

sub code {
my ($self, $body) = @_;
CgOp::rxbprim('Exact', CgOp::unbox('str', CgOp::fetch(
$self->value->cgop($body))));
}

sub lad { ['Imp'] }

__PACKAGE__->meta->make_immutable;
no Moose;
}

{
package RxOp::Quantifier;
use Moose;
Expand Down
16 changes: 16 additions & 0 deletions test2.pl
Expand Up @@ -5,6 +5,22 @@
ok !('xy' ~~ /x <{ False }> y/), '<{False}> blocks a match';
ok 'xy' ~~ /x <{ True }> y/, '<{True}> does not affect it';

{
my $b = "oo";
is ("foox" ~~ /f$b/), "foo", '$x matches contents in a regex';

our role Stop4717[$a] {
token foo { $a }
}

grammar X {
token TOP { [ <foo> | foo ]: x }
}

ok (X but OUR::Stop4717["foobar"]).parse("foobarx"),
"LTM works through parameterized role variables";
}

#is $?FILE, 'test.pl', '$?FILE works';
#is $?ORIG.substr(0,5), '# vim', '$?ORIG works';

Expand Down
8 changes: 5 additions & 3 deletions v6/TODO
@@ -1,5 +1,3 @@
~
<?{ }>
Cursor.add_categorical
Cursor.alpha
Cursor.canonicalize_name
Expand All @@ -19,6 +17,8 @@ Cursor.mixin
Cursor.O
Cursor.suppose
Cursor.trim_heredoc
Cursor.top_goal
Highwater stuff
EXPR will need a re-write with tests.
$<foo> = 1
func(|($key => $value))
Expand All @@ -32,10 +32,11 @@ self in regexes
$<sym>
temp $*FOO
something with $*HIGHWATER, $*HIGHEXPECT, $*GOAL
token { :my $var = expr; $var }
token { $param-role-var }

DONE:
~
<?{ }>
circumfix:<[ ]>
hash literals
() being Nil
Expand All @@ -60,6 +61,7 @@ push(@array, $thing)
shift(@array)
&sort
substr($str,$from,$len)
token { :my $var = expr; $var }

AVERTED:

Expand Down

0 comments on commit b4b25d0

Please sign in to comment.