Skip to content

Commit e31c208

Browse files
committed
Don't grab sigspace after $<var> = binding
1 parent d565a90 commit e31c208

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

src/QRegex/P6Regex/Actions.nqp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,21 @@ class QRegex::P6Regex::Actions is HLL::Actions {
7575

7676
method quantified_atom($/) {
7777
my $qast := $<atom>.ast;
78-
my $sig := $<sigmaybe>.ast;
79-
$qast := QAST::Regex.new(:rxtype<concat>, $qast, $sig) if $sig;
80-
if $<quantifier> {
78+
my $quant := $<quantifier>;
79+
if $quant {
8180
$/.CURSOR.panic('Quantifier quantifies nothing')
8281
unless $qast;
83-
my $ast := $<quantifier>[0].ast;
82+
83+
# Always capture sigspace before quantifier
84+
my $sig := $<sigmaybe>.ast if $<sigmaybe>;
85+
$qast := QAST::Regex.new(:rxtype<concat>, $qast, $sig) if $sig;
86+
87+
my $ast := $quant[0].ast;
8488
$ast.unshift($qast);
8589
$qast := $ast;
8690
}
8791
if $<separator> {
88-
unless $qast.rxtype eq 'quant' || $<sigfinal> {
92+
unless $qast.rxtype eq 'quant' {
8993
$/.CURSOR.panic("'" ~ $<separator>[0]<septype> ~
9094
"' many only be used immediately following a quantifier")
9195
}
@@ -95,11 +99,19 @@ class QRegex::P6Regex::Actions is HLL::Actions {
9599
QAST::Regex.new( :rxtype<quant>, :min(0), :max(1), $<separator>[0].ast ));
96100
}
97101
}
102+
# Don't capture trailing sigspace in a $<var> = binding
103+
unless $*VARDEF {
104+
my $sig;
105+
if $quant {
106+
$sig := $<sigfinal>[0].ast if $<sigfinal>;
107+
} else {
108+
$sig := $<sigmaybe>.ast if $<sigmaybe>;
109+
}
110+
$qast := QAST::Regex.new(:rxtype<concat>, $qast, $sig) if $sig && $qast;
111+
}
98112
if $qast {
99-
my $finalsig := $<sigfinal>[0].ast if $<quantifier>;
100-
$qast := QAST::Regex.new(:rxtype<concat>, $qast, $finalsig) if $finalsig && !$sig;
101113
$qast.backtrack('r') if !$qast.backtrack && (%*RX<r> || $<backmod> && ~$<backmod>[0] eq ':');
102-
$qast.node($/) if $qast;
114+
$qast.node($/);
103115
}
104116
make $qast;
105117
}

src/QRegex/P6Regex/Grammar.nqp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
101101
|| <?infixstopper>
102102
|| $$ <.panic: "Regex not terminated">
103103
|| (\W) { self.throw_unrecognized_metachar: ~$/[0] }
104-
|| <.panic: "Regex not terminated">
104+
|| (\w) { self.panic("missed one, boss: '"~$/[0]~"'") }
105+
|| {self.panic('Regex no term bra "'~$/~'", matched '~$<termseq>~'.')} #<.panic: "Regex not terminated">
105106
]
106107
}
107108

@@ -148,18 +149,19 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
148149
}
149150

150151
token termish {
151-
:my $*SIGOK := 0;
152+
:my $*SIGOK := False;
153+
:my $*VARDEF := False;
152154
<noun=.quantified_atom>+
153155
}
154156

155157
method SIGOK() { $*SIGOK := %*RX<s>; self }
156158

157159
token quantified_atom {
158160
<!rxstopper>
159-
<atom> <sigmaybe>
161+
<atom> <sigmaybe>?
160162
[
161163
[
162-
| <!rxstopper> <quantifier> <sigfinal=.sigmaybe>
164+
| <!rxstopper> <quantifier> <.SIGOK> <sigfinal=.sigmaybe>?
163165
| <?[:]> <backmod> <!alpha>
164166
]
165167
[ <separator> ]**0..1
@@ -189,8 +191,6 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
189191
{ $*SIGOK := False }
190192
}
191193

192-
token sigmaybe:sym<nosp> { <?[\S]> }
193-
194194
proto token quantifier { <...> }
195195
token quantifier:sym<*> { <sym> <backmod> }
196196
token quantifier:sym<+> { <sym> <backmod> }
@@ -264,7 +264,12 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
264264
| '$' $<pos>=[\d+]
265265
]
266266

267-
[ <.ws> '=' <.ws> <quantified_atom> ]**0..1
267+
[
268+
<.ws> '=' <.ws>
269+
{$*VARDEF := True}
270+
<quantified_atom>
271+
{$*VARDEF := False}
272+
]**0..1
268273
<.SIGOK>
269274
}
270275

0 commit comments

Comments
 (0)