Skip to content

Commit 26c85b4

Browse files
committed
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
1 parent 33fe530 commit 26c85b4

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

src/NQP/Actions.nqp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,19 +1225,24 @@ class NQP::Actions is HLL::Actions {
12251225
}
12261226
my $past;
12271227
if $<proto> {
1228-
$past := QAST::Block.new(
1229-
:name($name),
1230-
QAST::Op.new(
1231-
QAST::Var.new( :name('self'), :scope('local'), :decl('param') ),
1232-
QAST::SVal.new( :value($name) ),
1233-
:name('!protoregex'),
1234-
:op('callmethod')
1235-
),
1236-
:blocktype('declaration_static'),
1237-
:node($/)
1238-
);
1239-
$*W.pkg_add_method($*PACKAGE, 'add_method', $name,
1240-
$*W.create_code($past, $name, 0, :code_type_name<NQPRegex>));
1228+
$past := QAST::Block.new(
1229+
:name($name),
1230+
:blocktype('declaration_static'),
1231+
:node($/)
1232+
);
1233+
my $surrounding := QAST::Op.new(
1234+
QAST::Var.new( :name('self'), :scope('local'), :decl('param') ),
1235+
QAST::SVal.new( :value($name) ),
1236+
:name('!protoregex'),
1237+
:op('callmethod')
1238+
);
1239+
if $<protostart> || $<protoend> {
1240+
$surrounding.push($<protostart>.ast) if $<protostart>;
1241+
$surrounding.push($<protoend>.ast) if $<protoend>;
1242+
}
1243+
$past.push($surrounding);
1244+
$*W.pkg_add_method($*PACKAGE, 'add_method', $name,
1245+
$*W.create_code($past, $name, 0, :code_type_name<NQPRegex>));
12411246
}
12421247
else {
12431248
my $block := $*W.pop_lexpad();

src/NQP/Grammar.nqp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,14 +554,29 @@ grammar NQP::Grammar is HLL::Grammar {
554554
token regex_declarator {
555555
[
556556
| $<proto>=[proto] :s [regex|token|rule]
557+
# <.newpad>
557558
[
558559
|| '::(' <latename=variable> ')'
559560
|| <deflongname>
560561
]
562+
# :my %*RX;
563+
{
564+
%*RX<s> := $<sym> eq 'rule';
565+
%*RX<r> := $<sym> eq 'token' || $<sym> eq 'rule';
566+
}
561567
[
562568
|| '{*}'<?ENDSTMT>
563569
|| '{' '<...>' '}'<?ENDSTMT>
564570
|| '{' '<*>' '}'<?ENDSTMT>
571+
|| '{'
572+
[
573+
|| '{*}'
574+
|| <protostart=.LANG('Regex','nibbler')>'{*}'
575+
]
576+
[
577+
|| '}'
578+
|| <protoend=.LANG('Regex','nibbler')>'}'
579+
]<?ENDSTMT>
565580
|| <.panic: "Proto regex body must be \{*\} (or <*> or <...>, which are deprecated)">
566581
]
567582
| $<sym>=[regex|token|rule] :s

src/QRegex/Cursor.nqp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,18 @@ role NQPCursorRole is export {
232232
method !shared() { $!shared }
233233

234234
my @EMPTY := [];
235-
method !protoregex($name) {
235+
method !protoregex($name, $startrx = '', $endrx = '') {
236236
# Obtain and run NFA.
237237
my $shared := $!shared;
238-
my $nfa := self.HOW.cache(self, $name, { self.'!protoregex_nfa'($name) });
238+
my $nfa := QRegex::NFA.new;
239+
if $startrx || $endrx {
240+
$nfa.addnode($startrx) if $startrx;
241+
$nfa.addnode(self.HOW.cache(self, $name, { self.'!protoregex_nfa'($name) }));
242+
$nfa.addnode($endrx) if $endrx;
243+
}
244+
else {
245+
$nfa := self.HOW.cache(self, $name, { self.'!protoregex_nfa'($name) });
246+
}
239247
my @fates := $nfa.run(nqp::getattr_s($shared, ParseShared, '$!target'), $!pos);
240248

241249
# Update highwater mark.

0 commit comments

Comments
 (0)