Perl6-Pugs
view release on metacpan or search on metacpan
perl5/Pugs-Compiler-Rule/lib/Pugs/Runtime/Regex.pm view on Meta::CPAN
Pugs::Runtime::Match->new( {
bool => \0,
str => \$str,
from => \( 0 + ( $args->{p} || 0 ) ),
to => \( 0 + ( $args->{p} || 0 ) ),
named => {},
match => [],
} ) } unless $1 == 1;
return sub {
my ( $str, $grammar, $args ) = @_;
Pugs::Runtime::Match->new( {
bool => \1,
str => \$str,
from => \( 0 + ( $args->{p} || 0 ) ),
to => \( 0 + ( $args->{p} || 0 ) ),
named => {},
match => [],
} ) };
}
# subrule
#print "compile: ",$h->{$key}, "\n";
# XXX - compile to Token or to Regex ? (v6.pm needs Token)
my $r = Pugs::Compiler::Token->compile( $h->{$key} );
$h->{$key} = $r;
return sub { $r->match( @_ ) };
# return sub { warn "uncompiled subrule: $h->{$key} - not implemented " };
}
# see commit #9783 for an alternate implementation
sub hash {
my %hash = %{shift()};
#print "HASH: @{[ %hash ]}\n";
my @keys = sort {length $b <=> length $a } keys %hash;
#print "hash keys [ @keys ]\n";
for ( @keys ) {
my $h = preprocess_hash( \%hash, $_ );
my $key = $_;
$_ =
concat( [
constant( $key ),
sub {
# print "hash param: ",Dumper(\@_);
# TODO - add $<KEY> to $_[7]
$_[3] = $h->( $_[0], $_[4], $_[7], $_[1] );
# print "result: ",Dumper($_[3]);
}
] );
}
return alternation( \@keys );
}
# not a 'rule node'
# gets a variable from the user's pad
# this is used by the <$var> rule
sub get_variable {
my $name = shift;
local $@;
my($idx, $pad) = 0;
while(eval { require PadWalker; $pad = PadWalker::peek_my($idx) }) {
$idx++, next
unless exists $pad->{$name};
#print "NAME $name $pad->{$name}\n";
return ${ $pad->{$name} } if $name =~ /^\$/;
return $pad->{$name}; # arrayref/hashref
}
die "Couldn't find '$name' in surrounding lexical scope.";
}
1;
__END__
=for About
Original file: pX/Common/iterator_engine.pl - fglock
TODO
- There are no tests yet for <before>, hashes, end_of_string
- It needs a 'direction' flag, in order to implement <after>.
- Quantified matches could use less stack space.
- Simplify arg list - the functions currently take 8 arguments.
- weaken self-referential things
=cut
=pod
A "rule" function gets as argument a list:
0 - the string to match
1 - an optional "continuation"
2 - the partially built match tree
3 - a leaf pointer in the match tree
4 - the grammar name
5 - pos
#6 - the whole string to match
7 - argument list - <subrule($x,$y)>
it modifies argument #3 to a Match object:
bool - an "assertion" (true/false)
from - string pointer for start of this match
to - string pointer for next match (end+1)
match - positional submatches
named - named submatches
capture - return'ed things
state - a "continuation" or undef
abort - the match was stopped by a { return } or a fail(),
and it should not backtrack or whatever
A "ruleop" function gets some arguments and returns a "rule" funtion.
( run in 2.058 seconds using v1.01-cache-2.11-cpan-56fb94df46f )