Skip to content

Commit 11affd8

Browse files
committed
Make sigspace after $<var> = binding significant
Doing this required complexifying the grammar some, but in the process I was at least able to simplify the $<quantified_atom> action
1 parent 1203912 commit 11affd8

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

src/QRegex/P6Regex/Actions.nqp

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

7676
method quantified_atom($/) {
7777
my $qast := $<atom>.ast;
78-
my $quant := $<quantifier>;
79-
if $quant {
80-
$/.CURSOR.panic('Quantifier quantifies nothing')
81-
unless $qast;
8278

83-
# Always capture sigspace before quantifier
84-
my $sig := $<sigmaybe>.ast if $<sigmaybe>;
85-
$qast := QAST::Regex.new(:rxtype<concat>, $qast, $sig) if $sig;
79+
my $sigmaybe := $<sigmaybe>.ast if $<sigmaybe>;
80+
$qast := QAST::Regex.new(:rxtype<concat>, $qast, $sigmaybe) if $sigmaybe;
8681

87-
my $ast := $quant[0].ast;
82+
if $<quantifier> {
83+
$/.CURSOR.panic('Quantifier quantifies nothing')
84+
unless $qast;
85+
my $ast := $<quantifier>.ast;
8886
$ast.unshift($qast);
8987
$qast := $ast;
9088
}
@@ -93,22 +91,16 @@ class QRegex::P6Regex::Actions is HLL::Actions {
9391
$/.CURSOR.panic("'" ~ $<separator>[0]<septype> ~
9492
"' many only be used immediately following a quantifier")
9593
}
96-
$qast.push($<separator>[0].ast);
97-
if $<separator>[0]<septype> eq '%%' {
94+
$qast.push($<separator>.ast);
95+
if $<separator><septype> eq '%%' {
9896
$qast := QAST::Regex.new( :rxtype<concat>, $qast,
99-
QAST::Regex.new( :rxtype<quant>, :min(0), :max(1), $<separator>[0].ast ));
97+
QAST::Regex.new( :rxtype<quant>, :min(0), :max(1), $<separator>.ast ));
10098
}
10199
}
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-
}
100+
101+
my $sigfinal := $<sigfinal>.ast if $<sigfinal>;
102+
$qast := QAST::Regex.new(:rxtype<concat>, $qast, $sigfinal) if $sigfinal;
103+
112104
if $qast {
113105
$qast.backtrack('r') if !$qast.backtrack && (%*RX<r> || $<backmod> && ~$<backmod>[0] eq ':');
114106
$qast.node($/);

src/QRegex/P6Regex/Grammar.nqp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,27 +148,34 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
148148
}
149149

150150
token termish {
151-
:my $*SIGOK := False;
152-
:my $*VARDEF := False;
151+
:my $*SIGOK := 0;
152+
:my $*VARDEF := 0;
153153
<noun=.quantified_atom>+
154154
}
155155

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

158158
token quantified_atom {
159159
<!rxstopper>
160-
<atom> <sigmaybe>?
160+
<atom>
161161
[
162+
|| <sigmaybe>?
162163
[
163-
| <!rxstopper> <quantifier> <.SIGOK> <sigfinal=.sigmaybe>?
164+
| <!rxstopper> <quantifier>
164165
| <?[:]> <backmod> <!alpha>
165166
]
166-
[ <separator> ]**0..1
167-
]**0..1
167+
[ <!{$*VARDEF}> <.SIGOK> <sigfinal=.sigmaybe> ]?
168+
[ <.ws> <separator> ]?
169+
|| [ <!{$*VARDEF}> <sigfinal=.sigmaybe> ]?
170+
]
171+
{ $*SIGOK := 0 }
168172
}
169173

170-
token separator {
171-
$<septype>=['%''%'?] <normspace>**0..1 <quantified_atom>
174+
rule separator {
175+
$<septype>=['%''%'?]
176+
:my $*VARDEF := 0;
177+
:my $*SIGOK := 0;
178+
<quantified_atom>
172179
}
173180

174181
token atom {
@@ -187,7 +194,6 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
187194

188195
token sigmaybe:sym<sigwhite> {
189196
<?{$*SIGOK}> <normspace>
190-
{ $*SIGOK := False }
191197
}
192198

193199
proto token quantifier { <...> }
@@ -265,9 +271,9 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
265271

266272
[
267273
<.ws> '=' <.ws>
268-
{$*VARDEF := True}
274+
{ $*VARDEF := 1 }
269275
<quantified_atom>
270-
{$*VARDEF := False}
276+
{ $*VARDEF := 0 }
271277
]**0..1
272278
<.SIGOK>
273279
}

0 commit comments

Comments
 (0)