Skip to content

Commit 729fab5

Browse files
committed
Do sigspace even more right
Now only finds sigspace after an allowed metachar
1 parent 9939724 commit 729fab5

File tree

2 files changed

+63
-42
lines changed

2 files changed

+63
-42
lines changed

src/QRegex/P6Regex/Actions.nqp

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

7373
method quantified_atom($/) {
7474
my $qast := $<atom>.ast;
75-
if $<sigspace> {
76-
my $sig := $<sigspace>[0].ast;
75+
my $sig := $<sigmaybe>.ast;
76+
if $sig {
7777
$sig.unshift($qast);
7878
$qast := $sig;
7979
}
@@ -83,15 +83,14 @@ class QRegex::P6Regex::Actions is HLL::Actions {
8383
my $ast := $<quantifier>[0].ast;
8484
$ast.unshift($qast);
8585
$qast := $ast;
86-
if $<quantspace> && !$<sigspace> {
87-
my $qsig := $<quantspace>[0].ast;
88-
$qsig.unshift($qast);
89-
$qast := $qsig;
86+
my $finalsig := $<sigfinal>.ast;
87+
if $finalsig && !$sig {
88+
$finalsig.unshift($qast);
89+
$qast := $finalsig;
9090
}
9191
}
9292
if $<separator> {
93-
unless $qast.rxtype eq 'quant' ||
94-
$qast.rxtype eq 'concat' && $qast[0].rxtype eq 'quant' {
93+
unless $qast.rxtype eq 'quant' || $<quantspace> {
9594
$/.CURSOR.panic("'" ~ $<separator>[0]<septype> ~
9695
"' many only be used immediately following a quantifier")
9796
}
@@ -103,7 +102,7 @@ class QRegex::P6Regex::Actions is HLL::Actions {
103102
}
104103
$qast.backtrack('r') if $qast && !$qast.backtrack &&
105104
(%*RX<r> || $<backmod> && ~$<backmod>[0] eq ':');
106-
$qast.node($/);
105+
$qast.node($/) if $qast;
107106
make $qast;
108107
}
109108

@@ -122,13 +121,18 @@ class QRegex::P6Regex::Actions is HLL::Actions {
122121
}
123122
}
124123

125-
method sigspace($/) {
126-
my $qast := %*RX<s>
127-
?? QAST::Regex.new(:rxtype<concat>,
128-
QAST::Regex.new(:rxtype<ws>, :subtype<method>, :node($/),
129-
QAST::Node.new(QAST::SVal.new( :value('ws') ))))
130-
!! 0;
131-
make $qast;
124+
method sigmaybe:sym<normspace>($/) { make 0 }
125+
method sigmaybe:sym<nosp>($/) { make 0 }
126+
method sigmaybe:sym<sigwhite>($/) {
127+
make QAST::Regex.new(
128+
:rxtype<concat>,
129+
QAST::Regex.new(
130+
:rxtype<subrule>,
131+
:subtype<method>,
132+
:node($/),
133+
:name<ws>,
134+
QAST::Node.new(QAST::SVal.new( :value('ws') ))
135+
));
132136
}
133137

134138
method quantifier:sym<*>($/) {
@@ -649,11 +653,7 @@ class QRegex::P6Regex::Actions is HLL::Actions {
649653
$/.CURSOR.panic("Internal modifier strings must be literals");
650654
}
651655
}
652-
else {
653-
my $n := $<n>[0] gt '' ?? +$<n>[0] !! 1;
654-
%*RX{ ~$<mod_ident><sym> } := $n;
655-
make 0;
656-
}
656+
else { make 0 }
657657
}
658658

659659
sub backmod($ast, $backmod) {

src/QRegex/P6Regex/Grammar.nqp

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,22 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
137137
}
138138

139139
token termish {
140+
:my $*SIGOK := 0;
141+
[
140142
|| <noun=.quantified_atom>+
141143
|| <?before <stopper> | <[&|~]> > <.throw_null_pattern>
142-
# || (\W) { self.throw_unrecognized_metachar: ~$/[0] }
144+
|| (\W) { self.throw_unrecognized_metachar: ~$/[0] }
145+
]
143146
}
144147

148+
method SIGOK() { $*SIGOK := %*RX<s>; self }
149+
145150
token quantified_atom {
146151
<!rxstopper>
147-
<atom> <sigspace>**0..1
152+
<atom> <sigmaybe>
148153
[
149154
[
150-
| <!rxstopper> <quantifier> <quantspace=.sigspace>**0..1
155+
| <!rxstopper> <quantifier> <sigfinal=.sigmaybe>
151156
| <?before ':'> <backmod> <!alpha>
152157
]
153158
[ <separator> ]**0..1
@@ -161,12 +166,23 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
161166
token atom {
162167
# :dba('regex atom')
163168
[
164-
| \w [ \w+! <?before \w> ]?
169+
| \w [ \w+! <?before \w> ]? <.SIGOK>
165170
| <metachar>
166171
]
167172
}
168173

169-
token sigspace { <?[\s#]> <normspace> }
174+
proto token sigmaybe { <...> }
175+
176+
token sigmaybe:sym<normspace> {
177+
<!{$*SIGOK}> <normspace>
178+
}
179+
180+
token sigmaybe:sym<sigwhite> {
181+
<?{$*SIGOK}> <normspace>
182+
{ $*SIGOK := 0 }
183+
}
184+
185+
token sigmaybe:sym<nosp> { <?[\S]> }
170186

171187
proto token quantifier { <...> }
172188
token quantifier:sym<*> { <sym> <backmod> }
@@ -197,22 +213,22 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
197213
token backmod { ':'? [ '?' | '!' | <!before ':'> ] }
198214

199215
proto token metachar { <...> }
200-
token metachar:sym<[ ]> { '[' <nibbler> ']' }
201-
token metachar:sym<( )> { '(' <nibbler> ')' }
202-
token metachar:sym<'> { <?[']> <quote_EXPR: ':q'> }
203-
token metachar:sym<"> { <?["]> <quote_EXPR: ':qq'> }
204-
token metachar:sym<.> { <sym> }
205-
token metachar:sym<^> { <sym> }
206-
token metachar:sym<^^> { <sym> }
207-
token metachar:sym<$> { <sym> }
208-
token metachar:sym<$$> { <sym> }
216+
token metachar:sym<[ ]> { '[' <nibbler> ']' <.SIGOK> }
217+
token metachar:sym<( )> { '(' <nibbler> ')' <.SIGOK> }
218+
token metachar:sym<'> { <?[']> <quote_EXPR: ':q'> <.SIGOK> }
219+
token metachar:sym<"> { <?["]> <quote_EXPR: ':qq'> <.SIGOK> }
220+
token metachar:sym<.> { <sym> <.SIGOK> }
221+
token metachar:sym<^> { <sym> <.SIGOK> }
222+
token metachar:sym<^^> { <sym> <.SIGOK> }
223+
token metachar:sym<$> { <sym> <.SIGOK> }
224+
token metachar:sym<$$> { <sym> <.SIGOK> }
209225
token metachar:sym<:::> { <sym> <.panic: '::: not yet implemented'> }
210226
token metachar:sym<::> { <sym> <.panic: ':: not yet implemented'> }
211-
token metachar:sym<lwb> { $<sym>=['<<'|'«'] }
212-
token metachar:sym<rwb> { $<sym>=['>>'|'»'] }
213-
token metachar:sym<from> { '<(' }
214-
token metachar:sym<to> { ')>' }
215-
token metachar:sym<bs> { \\ <backslash> }
227+
token metachar:sym<lwb> { $<sym>=['<<'|'«'] <.SIGOK> }
228+
token metachar:sym<rwb> { $<sym>=['>>'|'»'] <.SIGOK> }
229+
token metachar:sym<from> { '<(' <.SIGOK> }
230+
token metachar:sym<to> { ')>' <.SIGOK> }
231+
token metachar:sym<bs> { \\ <backslash> <.SIGOK> }
216232
token metachar:sym<mod> { <mod_internal> }
217233
token metachar:sym<quantifier> {
218234
<!rxstopper> <quantifier> <.panic: 'Quantifier quantifies nothing'>
@@ -230,8 +246,7 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
230246
[ \h* '#= ' \h* $<key>=[\S+ [\h+ \S+]*] ]**0..1
231247
}
232248
token metachar:sym<assert> {
233-
'<' <assertion>
234-
[ '>' || <.panic: 'regex assertion not terminated by angle bracket'> ]
249+
'<' ~ '>' <assertion> <.SIGOK>
235250
}
236251

237252
token sigil { <[$@%&]> }
@@ -335,6 +350,12 @@ grammar QRegex::P6Regex::Grammar is HLL::Grammar {
335350
')'
336351
]**0..1
337352
]
353+
{
354+
if !$<quote_EXPR> {
355+
my $n := $<n>[0] gt '' ?? +$<n>[0] !! 1;
356+
%*RX{ ~$<mod_ident><sym> } := $n;
357+
}
358+
}
338359
}
339360

340361
proto token mod_ident { <...> }

0 commit comments

Comments
 (0)