Skip to content

Commit 210cd15

Browse files
committed
enable non-capturing groups and named captures
1 parent 8733f0e commit 210cd15

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/QRegex/P5Regex/Actions.nqp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,20 @@ class QRegex::P5Regex::Actions is HLL::Actions {
9999
);
100100
}
101101

102-
method p5metachar:sym<(? )>($/) {
103-
make $<assertion>.ast;
102+
method p5metachar:sym<(? )>($/) { # like P6's $<name>=[ ... ]
103+
my $qast;
104+
if $<nibbler> {
105+
$qast := QAST::Regex.new( :rxtype<subcapture>, :name(~$<name>),
106+
$<nibbler>.ast, :node($/) );
107+
}
108+
else {
109+
$qast := $<assertion>.ast;
110+
}
111+
make $qast;
112+
}
113+
114+
method p5metachar:sym<(?: )>($/) {
115+
make $<nibbler>.ast;
104116
}
105117

106118
method p5metachar:sym<( )>($/) {

src/QRegex/P5Regex/Grammar.nqp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ grammar QRegex::P5Regex::Grammar is HLL::Grammar {
8686
proto token p5metachar { <...> }
8787

8888
token p5metachar:sym<quant> {
89+
<![(?]>
8990
<quantifier=p5quantifier>
9091
<.panic: "quantifier quantifies nothing">
9192
}
@@ -96,9 +97,15 @@ grammar QRegex::P5Regex::Grammar is HLL::Grammar {
9697
'$' <?before \W | $>
9798
}
9899
token p5metachar:sym<(? )> {
99-
'(?' {} <assertion=p5assertion>
100-
[ ')' || <.panic: "Perl 5 regex assertion not terminated by parenthesis"> ]
100+
'(?' <![?]>
101+
[
102+
| <?[<]> '<' $<name>=[<-[>]>+] '>' {} <nibbler>
103+
| <?[']> "'" $<name>=[<-[']>+] "'" {} <nibbler>
104+
| <assertion=p5assertion>
105+
]
106+
[ ')' || <.panic: "Perl 5 named capture group not terminated by parenthesis"> ]
101107
}
108+
token p5metachar:sym<(?: )> { '(?:' {} <nibbler> ')' }
102109
token p5metachar:sym<( )> { '(' {} <nibbler> ')' }
103110
token p5metachar:sym<[ ]> { <?before '['> <cclass> }
104111

0 commit comments

Comments
 (0)