Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Non-functional sketch of proto regex with pre- and post- matches
I.e., proto rule foo { 'bar' {*} 'baz' }
as per S05:1330
Builds and doesn't break anything
  • Loading branch information
Mouq committed Oct 3, 2013
1 parent 33fe530 commit 26c85b4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
31 changes: 18 additions & 13 deletions src/NQP/Actions.nqp
Expand Up @@ -1225,19 +1225,24 @@ class NQP::Actions is HLL::Actions {
}
my $past;
if $<proto> {
$past := QAST::Block.new(
:name($name),
QAST::Op.new(
QAST::Var.new( :name('self'), :scope('local'), :decl('param') ),
QAST::SVal.new( :value($name) ),
:name('!protoregex'),
:op('callmethod')
),
:blocktype('declaration_static'),
:node($/)
);
$*W.pkg_add_method($*PACKAGE, 'add_method', $name,
$*W.create_code($past, $name, 0, :code_type_name<NQPRegex>));
$past := QAST::Block.new(
:name($name),
:blocktype('declaration_static'),
:node($/)
);
my $surrounding := QAST::Op.new(
QAST::Var.new( :name('self'), :scope('local'), :decl('param') ),
QAST::SVal.new( :value($name) ),
:name('!protoregex'),
:op('callmethod')
);
if $<protostart> || $<protoend> {
$surrounding.push($<protostart>.ast) if $<protostart>;
$surrounding.push($<protoend>.ast) if $<protoend>;
}
$past.push($surrounding);
$*W.pkg_add_method($*PACKAGE, 'add_method', $name,
$*W.create_code($past, $name, 0, :code_type_name<NQPRegex>));
}
else {
my $block := $*W.pop_lexpad();
Expand Down
15 changes: 15 additions & 0 deletions src/NQP/Grammar.nqp
Expand Up @@ -554,14 +554,29 @@ grammar NQP::Grammar is HLL::Grammar {
token regex_declarator {
[
| $<proto>=[proto] :s [regex|token|rule]
# <.newpad>
[
|| '::(' <latename=variable> ')'
|| <deflongname>
]
# :my %*RX;
{
%*RX<s> := $<sym> eq 'rule';
%*RX<r> := $<sym> eq 'token' || $<sym> eq 'rule';
}
[
|| '{*}'<?ENDSTMT>
|| '{' '<...>' '}'<?ENDSTMT>
|| '{' '<*>' '}'<?ENDSTMT>
|| '{'
[
|| '{*}'
|| <protostart=.LANG('Regex','nibbler')>'{*}'
]
[
|| '}'
|| <protoend=.LANG('Regex','nibbler')>'}'
]<?ENDSTMT>
|| <.panic: "Proto regex body must be \{*\} (or <*> or <...>, which are deprecated)">
]
| $<sym>=[regex|token|rule] :s
Expand Down
12 changes: 10 additions & 2 deletions src/QRegex/Cursor.nqp
Expand Up @@ -232,10 +232,18 @@ role NQPCursorRole is export {
method !shared() { $!shared }

my @EMPTY := [];
method !protoregex($name) {
method !protoregex($name, $startrx = '', $endrx = '') {
# Obtain and run NFA.
my $shared := $!shared;
my $nfa := self.HOW.cache(self, $name, { self.'!protoregex_nfa'($name) });
my $nfa := QRegex::NFA.new;
if $startrx || $endrx {
$nfa.addnode($startrx) if $startrx;
$nfa.addnode(self.HOW.cache(self, $name, { self.'!protoregex_nfa'($name) }));
$nfa.addnode($endrx) if $endrx;
}
else {
$nfa := self.HOW.cache(self, $name, { self.'!protoregex_nfa'($name) });
}
my @fates := $nfa.run(nqp::getattr_s($shared, ParseShared, '$!target'), $!pos);

# Update highwater mark.
Expand Down

0 comments on commit 26c85b4

Please sign in to comment.