blib/lib/Regexp/Grammars.pm

filter: Statementstmt 66.7% 545 / 816 Branchbran 59.3% 285 / 480 Conditioncond 53.4% 85 / 159 MC/DCmcdc 37.3% 34 / 91 Subroutinesub 71.0% 49 / 69 Podpod 0.0% 0 / 3 totaltotal 61.6% 998 / 1618 CC 307 SCAR 38.1
CC
307
Cov
63%
_translate_subrule_calls
145.9
_debug_context
132.0
_debug_interact
90.0
CRAP 45.2
↑ Summary
1 =encoding ISO8859-1
2 =cut
3  
4 package Regexp::Grammars;
5 82 ▶use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 82 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 82 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 82 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 82 ▶use vars ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 82 ▶use Scalar::Util qw< blessed reftype >;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 82 ▶use Data::Dumper qw< Dumper >;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 82 our $VERSION = '1.058';
16  
17 82 my $anon_scalar_ref = \do{my $var};
18 82 my $MAGIC_VARS = q{my ($CAPTURE, $CONTEXT, $DEBUG, $INDEX, $MATCH, %ARG, %MATCH);};
19  
20 82 my $PROBLEM_WITH_5_18 = <<'END_ERROR_MSG';
21 Warning: Regexp::Grammars is unsupported
22 under Perl 5.18.0 through 5.18.3 due to a bug
23 in regex parsing under those versions.
24  
25 Please upgrade to Perl 5.18.4 or later, or revert to
26 Perl 5.16 or earlier.
27 END_ERROR_MSG
28  
29 # Load the module...
30 94 ▶sub import {
Subroutine import: called CC=3 SCAR=11.2
31 # Signal lexical scoping (active, unless something was exported)...
32 94 $^H{'Regexp::Grammars::active'} = 1;
33  
34 # Process any regexes in module's active lexical scope...
35 82 ▶ use overload;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 overload::constant(
37 279 ▶ qr => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
38 279 my ($raw, $cooked, $type) = @_;
39 # In active scope and really a regex...
40 279 ▶ if (_module_is_active() && $type =~ /qq?/) {
Branch: 1 branch
BranchTrueFalseTotal
if (_module_is_active() and $type =~ /qq?/) { } 177 102 279
Condition66%2/3
_module_is_active() and $type =~ /qq?/
!ll&&!rl&&r
1020177
MC/DC50%1/2
_module_is_active() and $type =~ /qq?/
_module_is_active() $type =~ /qq?/
Truth table66%2/3
A and B
A= _module_is_active()B= $type =~ /qq?/
ABresult
0X0
100
111
41 177 return bless \$cooked, 'Regexp::Grammars::Precursor';
42 }
43 # Ignore everything else...
44 else {
45 102 return $cooked;
46 }
47 }
48 94 );
49  
50 # Deal with 5.18 issues...
51 94 ▶ if ($] >= 5.018) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.018") 94 0 94
52 # Issue warning...
53 94 ▶ if ($] < 5.018004) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.018004") 0 94 94
54 0 require Carp;
55 0 Carp::croak($PROBLEM_WITH_5_18);
56 }
57  
58 # Deal with cases where Perl 5.18+ complains about
59 # the injection of (??{...}) and (?{...})
60 94 require re;
61 94 re->import('eval');
62  
63 # Sanctify the standard Regexp::Grammars pseudo-variables from
64 # Perl 5.18's early enforcement of strictures...
65 94 my $caller = caller;
66 94 warnings->unimport('once');
67 94 @_ = ( 'vars', '$CAPTURE', '$CONTEXT', '$DEBUG', '$INDEX', '$MATCH', '%ARG', '%MATCH' );
68 94 goto &vars::import;
69 }
70 }
71  
72 # Deactivate module's regex effect when it is "anti-imported" with 'no'...
73 38 ▶sub unimport {
Subroutine unimport: called CC=1 SCAR=0.0
74 # Signal lexical (non-)scoping...
75 38 $^H{'Regexp::Grammars::active'} = 0;
76 38 require re;
77 38 re->unimport('eval');
78 }
79  
80 # Encapsulate the hoopy user-defined pragma interface...
81 279 ▶sub _module_is_active {
Subroutine _module_is_active: called CC=1 SCAR=0.0
82 279 return (caller 1)[10]->{'Regexp::Grammars::active'};
83 }
84  
85 82 my $RULE_HANDLER;
86 744 ▶sub clear_rule_handler { undef $RULE_HANDLER; }
Subroutine clear_rule_handler: called CC=1 SCAR=0.0
Pod undocumented
87  
88 21 ▶sub Regexp::with_actions {
Subroutine with_actions: called CC=1 SCAR=0.0
89 21 my ($self, $handler) = @_;
90 21 $RULE_HANDLER = $handler;
91 21 return $self;
92 }
93  
94 #=====[ COMPILE-TIME INTERIM REPRESENTATION OF GRAMMARS ]===================
95 {
96 package Regexp::Grammars::Precursor;
97  
98 # Only translate precursors once...
99 82 state %grammar_cache;
100  
101 82 ▶ use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
102 # Concatenation/interpolation just concatenates to the precursor...
103 97 ▶ q{.} => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
104 97 my ($x, $y, $reversed) = @_;
105 97 ▶ if (ref $x) { $x = ${$x} }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $x) 97 0 97
106 97 ▶ if (ref $y) { $y = ${$y} }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $y) 38 59 97
107 97 ▶ if ($reversed) { ($y,$x) = ($x,$y); }
Branch: 1 branch
BranchTrueFalseTotal
if ($reversed) 21 76 97
108 97 ▶ $x .= $y//q{};
Condition50%1/2
$y // ""
l!l
970
MC/DC0%0/1
$y // ""
$y
Truth table50%1/2
A
A= $y
Aresult
11
00
109 97 return bless \$x, 'Regexp::Grammars::Precursor';
110 },
111  
112 # Using as a string (i.e. matching) preprocesses the precursor...
113 139 ▶ q{""} => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
114 139 my ($obj) = @_;
115 return $grammar_cache{ overload::StrVal($$obj) }
116 139 ▶ //= Regexp::Grammars::_build_grammar( ${$obj} );
Condition33%1/3
$grammar_cache{overload::StrVal($$obj)} //= Regexp::Grammars::_build_grammar(${$obj;})
l!l&&r!l&&!r
01390
MC/DC0%0/2
$grammar_cache{overload::StrVal($$obj)} //= Regexp::Grammars::_build_grammar(${$obj;})
$grammar_cache{overload::StrVal($$obj)} Regexp::Grammars::_build_grammar(${$obj;})
Truth table33%1/3
A //= B
A= $grammar_cache{overload::StrVal($$obj)}B= Regexp::Grammars::_build_grammar(${$obj;})
ABresult
1X1
011
000
117 },
118  
119 # Everything else, as usual...
120 82 fallback => 1,
121 82 );
122 }
123  
124  
125 #=====[ SUPPORT FOR THE INTEGRATED DEBUGGER ]=========================
126  
127 # All messages go to STDERR by default...
128 82 *Regexp::Grammars::LOGFILE = *STDERR{IO};
129  
130 # Debugging levels indicate where to stop...
131 82 our %DEBUG_LEVEL = (
132 same => undef, # No change in debugging mode
133 off => 0, # No more debugging
134 run => 1, continue => 1, # Run to completion of regex match
135 match => 2, on => 2, # Run to next successful submatch
136 step => 3, try => 3, # Run to next reportable event
137 );
138  
139 # Debugging levels can be abbreviated to one character during interactions...
140 82 @DEBUG_LEVEL{ map {substr($_,0,1)} keys %DEBUG_LEVEL } = values %DEBUG_LEVEL;
141 82 $DEBUG_LEVEL{o} = $DEBUG_LEVEL{off}; # Not "on"
142 82 $DEBUG_LEVEL{s} = $DEBUG_LEVEL{step}; # Not "same"
143  
144 # Width of leading context field in debugging messages is constrained...
145 82 my $MAX_CONTEXT_WIDTH = 20;
146 82 my $MIN_CONTEXT_WIDTH = 6;
147  
148 0 ▶sub set_context_width {
Subroutine set_context_width: not called CC=2 SCAR=17.9
Pod undocumented
149 { package Regexp::Grammars::ContextRestorer;
150 0 ▶ sub new {
Subroutine new: not called CC=1 SCAR=6.9
151 0 my ($class, $old_context_width) = @_;
152 0 bless \$old_context_width, $class;
153 }
154 0 ▶ sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
155 0 my ($old_context_width_ref) = @_;
156 0 $MAX_CONTEXT_WIDTH = ${$old_context_width_ref};
157 }
158 }
159  
160 0 my ($new_context_width) = @_;
161 0 my $old_context_width = $MAX_CONTEXT_WIDTH;
162 0 $MAX_CONTEXT_WIDTH = $new_context_width;
163 0 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 0 0 0
164 0 return Regexp::Grammars::ContextRestorer->new($old_context_width);
165 }
166 }
167  
168 # Rewrite a string currently being matched, to make \n and \t visible
169 0 ▶sub _show_metas {
Subroutine _show_metas: not called CC=4 SCAR=30.0
170 0 ▶ my $context_str = shift // q{};
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
171  
172 # Quote newlines (\n -> \\n, without using a regex)...
173 0 my $index = index($context_str,"\n");
174 0 ▶ while ($index >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index >= 0) 0 0 0
175 0 substr($context_str, $index, 1, '\\n');
176 0 $index = index($context_str,"\n",$index+2);
177 }
178  
179 # Quote tabs (\t -> \\t, without using a regex)...
180 0 $index = index($context_str,"\t");
181 0 ▶ while ($index >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index >= 0) 0 0 0
182 0 substr($context_str, $index, 1, '\\t');
183 0 $index = index($context_str,"\t",$index+2);
184 }
185  
186 0 return $context_str;
187 }
188  
189 # Minimize whitespace in a string...
190 1826 ▶sub _squeeze_ws {
Subroutine _squeeze_ws: called CC=2 SCAR=6.9
191 1826 my ($str) = @_;
192  
193 1826 $str =~ tr/\n\t/ /;
194  
195 1826 my $index = index($str,q{ });
196 1826 ▶ while ($index >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index >= 0) 7254 1826 9080
197 7254 substr($str, $index, 2, q{ });
198 7254 $index = index($str,q{ },$index);
199 }
200  
201 1826 return $str;
202 }
203  
204 # Prepare for debugging...
205 0 ▶sub _init_try_stack {
Subroutine _init_try_stack: not called CC=1 SCAR=6.9
206 0 our (@try_stack, $last_try_pos, $last_context_str);
207  
208 # Start with a representation of the entire grammar match...
209 0 @try_stack = ({
210 subrule => '<grammar>',
211 height => 0,
212 errmsg => ' \\FAIL <grammar>',
213 });
214  
215 # Initialize tracking of location and context...
216 0 $last_try_pos = -1;
217 0 $last_context_str = q{};
218  
219 # Report...
220 0 say {*Regexp::Grammars::LOGFILE} _debug_context('=>')
221 . 'Trying <grammar> from position ' . pos();
222 }
223  
224 # Create a "context string" showing where the regex is currently matching...
225 0 ▶sub _debug_context {
Subroutine _debug_context: not called CC=11 SCAR=48.8
226 0 my ($fill_chars) = @_;
227  
228 # Determine minimal sufficient width for context field...
229 0 ▶ my $field_width = length(_show_metas($_//q{}));
Condition0%0/2
$_ // ""
l!l
00
MC/DC0%0/1
$_ // ""
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
230 0 ▶ if ($field_width > $MAX_CONTEXT_WIDTH) {
Branch: 2 branches
BranchTrueFalseTotal
if ($field_width > $MAX_CONTEXT_WIDTH) { } 0 0 0
elsif ($field_width < $MIN_CONTEXT_WIDTH) { } 0 0 0
231 0 $field_width = $MAX_CONTEXT_WIDTH;
232 }
233 elsif ($field_width < $MIN_CONTEXT_WIDTH) {
234 0 $field_width = $MIN_CONTEXT_WIDTH;
235 }
236  
237 # Get current matching position (and some additional trailing context)...
238 0 ▶ my $context_str
Condition0%0/4
$_ // ""
l!l
00
pos $_ // 0
l!l
00
MC/DC0%0/1
$_ // ""
$_
MC/DC0%0/1
pos $_ // 0
pos $_
Truth table0%0/2
A
A= $_
Aresult
11
00
Truth table0%0/2
A
A= pos $_
Aresult
11
00
239 = substr(_show_metas(substr(($_//q{}).q{},pos()//0,$field_width)),0,$field_width);
240  
241 # Build the context string, handling special cases...
242 0 ▶ our $last_context_str //= q{};
Condition0%0/2
our $last_context_str //= ""
l!l
00
MC/DC0%0/1
our $last_context_str //= ""
our $last_context_str
Truth table0%0/2
A
A= our $last_context_str
Aresult
11
00
243 0 ▶ if ($fill_chars) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fill_chars) { } 0 0 0
244 # If caller supplied a 1- or 2-char fill sequence, use that instead...
245 0 ▶ my $last_fill_char = length($fill_chars) > 1
Branch: 1 branch
BranchTrueFalseTotal
length $fill_chars > 1 ? : 0 0 0
246 ? substr($fill_chars,-1,1,q{})
247 : $fill_chars
248 ;
249 0 $context_str = $fill_chars x ($field_width-1) . $last_fill_char;
250 }
251 else {
252 # Make end-of-string visible in empty context string...
253 0 ▶ if ($context_str eq q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context_str eq "") 0 0 0
254 0 $context_str = '[eos]';
255 }
256  
257 # Don't repeat consecutive identical context strings...
258 0 ▶ if ($context_str eq $last_context_str) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context_str eq $Regexp::Grammars::last_context_str) { } 0 0 0
259 0 $context_str = q{ } x $field_width;
260 }
261 else {
262 # If not repeating, remember for next time...
263 0 $last_context_str = $context_str;
264 }
265 }
266  
267 # Left justify and return context string...
268 0 return sprintf("%-*s ",$field_width,$context_str);
269 }
270  
271 # Show a debugging message (mainly used for compile-time errors and info)...
272 2 ▶sub _debug_notify {
Subroutine _debug_notify: called CC=6 SCAR=19.2
273 # Single arg is a line to be printed with a null severity...
274 2 ▶ my ($severity, @lines) = @_==1 ? (q{},@_) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 2 2
275 2 chomp @lines;
276  
277 # Formatting string for all lines...
278 2 my $format = qq{%*s | %s\n};
279  
280 # Track previous severity and avoid repeating the same level...
281 2 state $prev_severity = q{};
282 2 ▶ if ($severity !~ /\S/) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $severity =~ /\S/) { } 0 2 2
elsif ($severity eq 'info' and $prev_severity eq 'info') { } 0 2 2
283 # Do nothing
284 }
285 ▶ elsif ($severity eq 'info' && $prev_severity eq 'info' ) {
Condition33%1/3
$severity eq "info" and $prev_severity eq "info"
!ll&&!rl&&r
200
MC/DC0%0/2
$severity eq "info" and $prev_severity eq "info"
$severity eq "info" $prev_severity eq "info"
Truth table33%1/3
A and B
A= $severity eq "info"B= $prev_severity eq "info"
ABresult
0X0
100
111
286 0 $severity = q{};
287 }
288 else {
289 2 $prev_severity = $severity;
290 }
291  
292 # Display first line with severity indicator (unless same as previous)...
293 2 printf {*Regexp::Grammars::LOGFILE} $format, $MIN_CONTEXT_WIDTH, $severity, shift @lines;
294  
295 # Display first line without severity indicator
296 2 for my $next_line (@lines) {
297 8 printf {*Regexp::Grammars::LOGFILE} $format, $MIN_CONTEXT_WIDTH, q{}, $next_line;
298 }
299 }
300  
301 # Handle user interactions during runtime debugging...
302 0 ▶sub _debug_interact {
Subroutine _debug_interact: not called CC=9 SCAR=45.0
303 0 my ($stack_height, $leader, $curr_frame_ref, $min_debug_level) = @_;
304  
305 0 our $DEBUG; # ...stores current debug level within regex
306  
307 # Only interact with terminals, and if debug level is appropriate...
308 0 ▶ if (-t *Regexp::Grammars::LOGFILE
Branch: 1 branch
BranchTrueFalseTotal
if (-t *LOGFILE and defined $Regexp::Grammars::DEBUG and ($Regexp::Grammars::DEBUG_LEVEL{$Regexp::Grammars::DEBUG} // 0) >= $Regexp::Grammars::DEBUG_LEVEL{$min_debug_level}) { } 0 0 0
Condition0%0/8
-t *LOGFILE and defined $Regexp::Grammars::DEBUG and ($Regexp::Grammars::DEBUG_LEVEL{$Regexp::Grammars::DEBUG} // 0) >= $Regexp::Grammars::DEBUG_LEVEL{$min_debug_level}
!ll&&!rl&&r
000
-t *LOGFILE and defined $Regexp::Grammars::DEBUG
!ll&&!rl&&r
000
$Regexp::Grammars::DEBUG_LEVEL{$Regexp::Grammars::DEBUG} // 0
l!l
00
MC/DC0%0/3
-t *LOGFILE and defined $Regexp::Grammars::DEBUG and ($Regexp::Grammars::DEBUG_LEVEL{$Regexp::Grammars::DEBUG} // 0) >= $Regexp::Grammars::DEBUG_LEVEL{$min_debug_level}
-t *LOGFILE defined $Regexp::Grammars::DEBUG $Regexp::Grammars::DEBUG_LEVEL{$Regexp::Grammars::DEBUG}
Truth table0%0/4
A and B and C
A= -t *LOGFILEB= defined $Regexp::Grammars::DEBUGC= $Regexp::Grammars::DEBUG_LEVEL{$Regexp::Grammars::DEBUG}
ABCresult
0XX0
10X0
1100
1111
309 && defined $DEBUG
310 && ($DEBUG_LEVEL{$DEBUG}//0) >= $DEBUG_LEVEL{$min_debug_level}
311 ) {
312 0 local $/ = "\n"; # ...in case some caller is being clever
313 INPUT:
314 0 while (1) {
315 0 ▶ my $cmd = readline // q{};
Condition0%0/2
readline ARGV // ""
l!l
00
MC/DC0%0/1
readline ARGV // ""
readline ARGV
Truth table0%0/2
A
A= readline ARGV
Aresult
11
00
316 0 chomp $cmd;
317  
318 # Input of 'd' means 'display current result frame'...
319 0 ▶ if ($cmd eq 'd') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq 'd') { } 0 0 0
320 0 print {*Regexp::Grammars::LOGFILE} join "\n",
321 0 ▶ map { $leader . ($stack_height?'| ':q{})
Branch: 1 branch
BranchTrueFalseTotal
$stack_height ? : 0 0 0
322 . ' : ' . $_
323 }
324 split "\n", q{ }x8 . substr(Dumper($curr_frame_ref),8);
325 0 print "\t";
326 }
327 # Any other (valid) input changes debugging level and continues...
328 else {
329 0 ▶ if (defined $DEBUG_LEVEL{$cmd}) { $DEBUG = $cmd; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Regexp::Grammars::DEBUG_LEVEL{$cmd}) 0 0 0
330 0 last INPUT;
331 }
332 }
333 }
334 # When interaction not indicated, just complete the debugging line...
335 else {
336 0 print {*Regexp::Grammars::LOGFILE} "\n";
337 }
338 }
339  
340 # Handle reporting of unsuccessful match attempts...
341 0 ▶sub _debug_handle_failures {
Subroutine _debug_handle_failures: not called CC=6 SCAR=37.4
342 0 my ($stack_height, $subrule, $in_match) = @_;
343 0 our @try_stack;
344  
345 # Unsuccessful match attempts leave "leftovers" on the attempt stack...
346 CLEANUP:
347 0 ▶ while (@try_stack && $try_stack[-1]{height} >= $stack_height) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Regexp::Grammars::try_stack and $Regexp::Grammars::try_stack[-1]{"height"} >= $stack_height) 0 0 0
Condition0%0/3
@Regexp::Grammars::try_stack and $Regexp::Grammars::try_stack[-1]{"height"} >= $stack_height
!ll&&!rl&&r
000
MC/DC0%0/2
@Regexp::Grammars::try_stack and $Regexp::Grammars::try_stack[-1]{"height"} >= $stack_height
@Regexp::Grammars::try_stack $Regexp::Grammars::try_stack[-1]{"height"} >= $stack_height
Truth table0%0/3
A and B
A= @Regexp::Grammars::try_stackB= $Regexp::Grammars::try_stack[-1]{"height"} >= $stack_height
ABresult
0X0
100
111
348 # Grab record of (potentially) unsuccessful attempt...
349 0 my $error_ref = pop @try_stack;
350  
351 # If attempt was the one whose match is being reported, go and report...
352 last CLEANUP if $in_match
353 && $error_ref->{height} == $stack_height
354 0 ▶ && $error_ref->{subrule} eq $subrule;
Branch: 1 branch
BranchTrueFalseTotal
if $in_match and $error_ref->{"height"} == $stack_height and $error_ref->{"subrule"} eq $subrule 0 0 0
Condition0%0/6
$in_match and $error_ref->{"height"} == $stack_height and $error_ref->{"subrule"} eq $subrule
!ll&&!rl&&r
000
$in_match and $error_ref->{"height"} == $stack_height
!ll&&!rl&&r
000
MC/DC0%0/3
$in_match and $error_ref->{"height"} == $stack_height and $error_ref->{"subrule"} eq $subrule
$in_match $error_ref->{"height"} == $stack_height $error_ref->{"subrule"} eq $subrule
Truth table0%0/4
A and B and C
A= $in_matchB= $error_ref->{"height"} == $stack_heightC= $error_ref->{"subrule"} eq $subrule
ABCresult
0XX0
10X0
1100
1111
355  
356 # Otherwise, report the match failure...
357 0 say {*Regexp::Grammars::LOGFILE} _debug_context(q{ }) . $error_ref->{errmsg};
358 }
359 }
360  
361 # Handle attempts to call non-existent subrules...
362 0 ▶sub _debug_fatal {
Subroutine _debug_fatal: not called CC=1 SCAR=6.9
363 0 my ($naughty_construct) = @_;
364  
365 0 print {*Regexp::Grammars::LOGFILE}
366 "_________________________________________________________________\n",
367 "Fatal error: Entire parse terminated prematurely while attempting\n",
368 " to call non-existent rule: $naughty_construct\n",
369 "_________________________________________________________________\n";
370 0 $@ = "Entire parse terminated prematurely while attempting to call non-existent rule: $naughty_construct";
371 }
372  
373 # Handle objrules that don't return hashes...
374 334 ▶sub _debug_non_hash {
Subroutine _debug_non_hash: called CC=2 SCAR=6.9
375 334 my ($obj, $name) = @_;
376  
377 # If the object is okay, no further action required...
378 334 ▶ return q{} if reftype($obj) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($obj) eq "HASH" 333 1 334
379  
380 # Generate error messages...
381 1 print {*Regexp::Grammars::LOGFILE}
382 "_________________________________________________________________\n",
383 "Fatal error: <objrule: $name> returned a non-hash-based object\n",
384 "_________________________________________________________________\n";
385 1 $@ = "<objrule: $name> returned a non-hash-based object";
386  
387 1 return '(*COMMIT)(*FAIL)';
388 }
389  
390  
391 # Print a <log:...> message in context...
392 0 ▶sub _debug_logmsg {
Subroutine _debug_logmsg: not called CC=1 SCAR=6.9
393 0 my ($stack_height, @msg) = @_;
394  
395 # Determine indent for messages...
396 0 my $leader = _debug_context() . q{| } x ($stack_height-1) . '|';
397  
398 # Report the attempt...
399 0 print {*Regexp::Grammars::LOGFILE} map { "$leader$_\n" } @msg;
400 }
401  
402 # Print a message indicating a (sub)match attempt...
403 0 ▶sub _debug_trying {
Subroutine _debug_trying: not called CC=5 SCAR=34.0
404 0 my ($stack_height, $curr_frame_ref, $subrule) = @_;
405  
406 # Clean up after any preceding unsuccessful attempts...
407 0 _debug_handle_failures($stack_height, $subrule);
408  
409 # Determine indent for messages...
410 0 my $leader = _debug_context() . q{| } x ($stack_height-2);
411  
412 # Detect and report any backtracking prior to this attempt...
413 0 ▶ our $last_try_pos //= 0; #...Stores the pos() of the most recent match attempt?
Condition0%0/2
our $last_try_pos //= 0
l!l
00
MC/DC0%0/1
our $last_try_pos //= 0
our $last_try_pos
Truth table0%0/2
A
A= our $last_try_pos
Aresult
11
00
414 0 my $backtrack_distance = $last_try_pos - pos();
415 0 ▶ if ($backtrack_distance > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backtrack_distance > 0) 0 0 0
416 0 ▶ say {*Regexp::Grammars::LOGFILE} ' <' . q{~} x (length(_debug_context(q{ }))-3) . q{ }
Branch: 1 branch
BranchTrueFalseTotal
$backtrack_distance > 1 ? : 0 0 0
417 . q{| } x ($stack_height-2)
418 . qq{|...Backtracking $backtrack_distance char}
419 . ($backtrack_distance > 1 ? q{s} : q{})
420 . q{ and trying new match}
421 ;
422 }
423  
424 # Report the attempt...
425 0 print {*Regexp::Grammars::LOGFILE} $leader, "|...Trying $subrule\t";
426  
427 # Handle user interactions during debugging...
428 0 _debug_interact($stack_height, $leader, $curr_frame_ref, 'step');
429  
430 # Record the attempt, for later error handling in _debug_matched()...
431 0 ▶ if ($subrule ne 'next alternative') {
Branch: 1 branch
BranchTrueFalseTotal
if ($subrule ne "next alternative") 0 0 0
432 0 our @try_stack;
433 0 push @try_stack, {
434 height => $stack_height,
435 subrule => $subrule,
436 # errmsg should align under: |...Trying $subrule\t
437 errmsg => q{| } x ($stack_height-2) . "| \\FAIL $subrule",
438 };
439 }
440 0 $last_try_pos = pos();
441 }
442  
443 # Print a message indicating a successful (sub)match...
444 0 ▶sub _debug_matched {
Subroutine _debug_matched: not called CC=9 SCAR=45.0
445 0 my ($stack_height, $curr_frame_ref, $subrule, $matched_text) = @_;
446  
447 # Clean up any intervening unsuccessful attempts...
448 0 _debug_handle_failures($stack_height, $subrule, 'in match');
449  
450 # Build debugging message...
451 0 my $debug_context = _debug_context();
452 0 my $leader = $debug_context . q{| } x ($stack_height-2);
453 0 ▶ my $message = ($stack_height ? '| ' : q{})
Branch: 1 branch
BranchTrueFalseTotal
$stack_height ? : 0 0 0
454 . " \\_____$subrule matched ";
455 0 ▶ my $filler = $stack_height
Branch: 1 branch
BranchTrueFalseTotal
$stack_height ? : 0 0 0
456 ? '| ' . q{ } x (length($message)-4)
457 : q{ } x length($message);
458  
459 0 ▶ our $last_try_pos //= 0; #...Stores the pos() of the most recent match attempt?
Condition0%0/2
our $last_try_pos //= 0
l!l
00
MC/DC0%0/1
our $last_try_pos //= 0
our $last_try_pos
Truth table0%0/2
A
A= our $last_try_pos
Aresult
11
00
460  
461 # Report if match required backtracking...
462 0 ▶ my $backtrack_distance = $last_try_pos - (pos()//0);
Condition0%0/2
pos $_ // 0
l!l
00
MC/DC0%0/1
pos $_ // 0
pos $_
Truth table0%0/2
A
A= pos $_
Aresult
11
00
463 0 ▶ if ($backtrack_distance > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backtrack_distance > 0) 0 0 0
464 0 ▶ say {*Regexp::Grammars::LOGFILE} ' <' . q{~} x (length(_debug_context(q{ }))-3) . q{ }
Branch: 1 branch
BranchTrueFalseTotal
$backtrack_distance > 1 ? : 0 0 0
465 . q{| } x ($stack_height-2)
466 . qq{|...Backtracking $backtrack_distance char}
467 . ($backtrack_distance > 1 ? q{s} : q{})
468 . qq{ and rematching $subrule}
469 ;
470 }
471 0 $last_try_pos = pos();
472  
473 # Format match text (splitting multi-line texts and indent them correctly)...
474 0 ▶ $matched_text = defined($matched_text)
Branch: 1 branch
BranchTrueFalseTotal
defined $matched_text ? : 0 0 0
475 ? $matched_text = q{'} . join("\n$leader$filler", split "\n", $matched_text) . q{'}
476 : q{};
477  
478 # Print match message...
479 0 print {*Regexp::Grammars::LOGFILE} $leader . $message . $matched_text . qq{\t};
480  
481 # Check for user interaction...
482 0 ▶ _debug_interact($stack_height, $leader, $curr_frame_ref, $stack_height ? 'match' : 'run');
Branch: 1 branch
BranchTrueFalseTotal
$stack_height ? : 0 0 0
483 }
484  
485 # Print a message indicating a successful (sub)match...
486 0 ▶sub _debug_require {
Subroutine _debug_require: not called CC=6 SCAR=37.4
487 0 my ($stack_height, $condition, $succeeded) = @_;
488  
489 # Build debugging message...
490 0 my $debug_context = _debug_context();
491 0 my $leader = $debug_context . q{| } x ($stack_height-1);
492 0 ▶ my $message1 = ($stack_height ? '|...' : q{})
Branch: 1 branch
BranchTrueFalseTotal
$stack_height ? : 0 0 0
493 . "Testing condition: $condition"
494 ;
495 0 ▶ my $message2 = ($stack_height ? '| ' : q{})
Branch: 2 branches
BranchTrueFalseTotal
$stack_height ? : 0 0 0
$succeeded ? : 0 0 0
496 . " \\_____"
497 . ($succeeded ? 'Satisfied' : 'FAILED')
498 ;
499  
500 # Report if match required backtracking...
501 0 our $last_try_pos;
502 0 my $backtrack_distance = $last_try_pos - pos();
503 0 ▶ if ($backtrack_distance > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backtrack_distance > 0) 0 0 0
504 0 ▶ say {*Regexp::Grammars::LOGFILE} ' <' . q{~} x (length(_debug_context(q{ }))-3) . q{ }
Branch: 1 branch
BranchTrueFalseTotal
$backtrack_distance > 1 ? : 0 0 0
505 . q{| } x ($stack_height-1)
506 . qq{|...Backtracking $backtrack_distance char}
507 . ($backtrack_distance > 1 ? q{s} : q{})
508 . qq{ and rematching}
509 ;
510 }
511  
512 # Remember where the condition was tried...
513 0 $last_try_pos = pos();
514  
515 # Print match message...
516 0 say {*Regexp::Grammars::LOGFILE} $leader . $message1;
517 0 say {*Regexp::Grammars::LOGFILE} $leader . $message2;
518 }
519  
520 # Print a message indicating a successful store-result-of-code-block...
521 0 ▶sub _debug_executed {
Subroutine _debug_executed: not called CC=1 SCAR=6.9
522 0 my ($stack_height, $curr_frame_ref, $subrule, $value) = @_;
523  
524 # Build message...
525 0 my $leader = _debug_context() . q{| } x ($stack_height-2);
526 0 my $message = "|...Action $subrule\n";
527 0 my $message2 = "| saved value: '";
528 0 $message .= $leader . $message2;
529 0 my $filler = q{ } x length($message2);
530  
531 # Split multiline results over multiple lines (properly indented)...
532 0 $value = join "\n$leader$filler", split "\n", $value;
533  
534 # Report the action...
535 0 print {*Regexp::Grammars::LOGFILE} $leader . $message . $value . qq{'\t};
536  
537 # Check for user interaction...
538 0 _debug_interact($stack_height, $leader, $curr_frame_ref, 'match');
539 }
540  
541 # Create the code to be inserted into the regex to facilitate debugging...
542 423 ▶sub _build_debugging_statements {
Subroutine _build_debugging_statements: called CC=3 SCAR=11.0
543 423 my ($debugging_active, $subrule, $extra_pre_indent) = @_;
544  
545 423 ▶ return (q{}, q{}) if ! $debugging_active;;
Branch: 1 branch
BranchTrueFalseTotal
unless $debugging_active 422 1 423
546  
547 1 ▶ $extra_pre_indent //= 0;
Condition50%1/2
$extra_pre_indent //= 0
l!l
10
MC/DC0%0/1
$extra_pre_indent //= 0
$extra_pre_indent
Truth table50%1/2
A
A= $extra_pre_indent
Aresult
11
00
548  
549 1 $subrule = "q{$subrule}";
550  
551 return (
552 1 qq{Regexp::Grammars::_debug_trying(\@Regexp::Grammars::RESULT_STACK+$extra_pre_indent, \$Regexp::Grammars::RESULT_STACK[-2+$extra_pre_indent], $subrule)
553 if \$Regexp::Grammars::DEBUG_LEVEL{\$Regexp::Grammars::DEBUG};},
554 qq{Regexp::Grammars::_debug_matched(\@Regexp::Grammars::RESULT_STACK+1, \$Regexp::Grammars::RESULT_STACK[-1], $subrule, \$^N)
555 if \$Regexp::Grammars::DEBUG_LEVEL{\$Regexp::Grammars::DEBUG};},
556 );
557 }
558  
559 1824 ▶sub _build_raw_debugging_statements {
Subroutine _build_raw_debugging_statements: called CC=4 SCAR=23.7
560 1824 my ($debugging_active, $subpattern, $extra_pre_indent) = @_;
561  
562 1824 ▶ return (q{}, q{}) if ! $debugging_active;
Branch: 1 branch
BranchTrueFalseTotal
unless $debugging_active 1824 0 1824
563  
564 0 ▶ $extra_pre_indent //= 0;
Condition0%0/2
$extra_pre_indent //= 0
l!l
00
MC/DC0%0/1
$extra_pre_indent //= 0
$extra_pre_indent
Truth table0%0/2
A
A= $extra_pre_indent
Aresult
11
00
565  
566 0 ▶ if ($subpattern eq '|') {
Branch: 1 branch
BranchTrueFalseTotal
if ($subpattern eq '|') { } 0 0 0
567 return (
568 0 q{},
569 qq{(?{;Regexp::Grammars::_debug_trying(\@Regexp::Grammars::RESULT_STACK+$extra_pre_indent,
570 \$Regexp::Grammars::RESULT_STACK[-2+$extra_pre_indent], 'next alternative')
571 if \$Regexp::Grammars::DEBUG_LEVEL{\$Regexp::Grammars::DEBUG};})},
572 );
573 }
574 else {
575 return (
576 0 qq{(?{;Regexp::Grammars::_debug_trying(\@Regexp::Grammars::RESULT_STACK+$extra_pre_indent,
577 \$Regexp::Grammars::RESULT_STACK[-2+$extra_pre_indent], q{subpattern /$subpattern/}, \$^N)
578 if \$Regexp::Grammars::DEBUG_LEVEL{\$Regexp::Grammars::DEBUG};})},
579 qq{(?{;Regexp::Grammars::_debug_matched(\@Regexp::Grammars::RESULT_STACK+1,
580 \$Regexp::Grammars::RESULT_STACK[-1], q{subpattern /$subpattern/}, \$^N)
581 if \$Regexp::Grammars::DEBUG_LEVEL{\$Regexp::Grammars::DEBUG};})},
582 );
583 }
584 }
585  
586  
587 #=====[ SUPPORT FOR AUTOMATIC TIMEOUTS ]=========================
588  
589 0 ▶sub _test_timeout {
Subroutine _test_timeout: not called CC=6 SCAR=37.4
590 0 our ($DEBUG, $TIMEOUT);
591  
592 0 ▶ return q{} if time() < $TIMEOUT->{'limit'};
Branch: 1 branch
BranchTrueFalseTotal
if time < $Regexp::Grammars::TIMEOUT->{"limit"} 0 0 0
593  
594 my $duration = "$TIMEOUT->{duration} second"
595 0 ▶ . ( $TIMEOUT->{duration} == 1 ? q{} : q{s} );
Branch: 1 branch
BranchTrueFalseTotal
$Regexp::Grammars::TIMEOUT->{'duration'} == 1 ? : 0 0 0
596  
597 0 ▶ if (defined($DEBUG) && $DEBUG ne 'off') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Regexp::Grammars::DEBUG and $Regexp::Grammars::DEBUG ne "off") 0 0 0
Condition0%0/3
defined $Regexp::Grammars::DEBUG and $Regexp::Grammars::DEBUG ne "off"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Regexp::Grammars::DEBUG and $Regexp::Grammars::DEBUG ne "off"
defined $Regexp::Grammars::DEBUG $Regexp::Grammars::DEBUG ne "off"
Truth table0%0/3
A and B
A= defined $Regexp::Grammars::DEBUGB= $Regexp::Grammars::DEBUG ne "off"
ABresult
0X0
100
111
598 0 my $leader = _debug_context(q{ });
599 0 say {*LOGFILE} $leader . '|';
600 0 say {*LOGFILE} $leader . "|...Invoking <timeout: $TIMEOUT->{duration}>";
601 0 say {*LOGFILE} $leader . "| \\_____No match after $duration";
602 0 say {*LOGFILE} $leader . '|';
603 0 say {*LOGFILE} $leader . " \\FAIL <grammar>";
604 }
605  
606 0 ▶ if (! @!) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@!) 0 0 0
607 0 @! = "Internal error: Timed out after $duration (as requested)";
608 }
609 0 return q{(*COMMIT)(*FAIL)};
610 }
611  
612  
613 #=====[ SUPPORT FOR UPDATING THE RESULT STACK ]=========================
614  
615 # Create a clone of the current result frame with an new key/value...
616 2011 ▶sub _extend_current_result_frame_with_scalar {
Subroutine _extend_current_result_frame_with_scalar: called CC=3 SCAR=11.8
617 2011 my ($stack_ref, $key, $value) = @_;
618  
619 # Autovivify null stacks (only occur when grammar invokes no subrules)...
620 2011 ▶ if (!@{$stack_ref}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$stack_ref;}) 0 2011 2011
621 0 $stack_ref = [{}];
622 }
623  
624 # Copy existing frame, appending new value so it overwrites any old value...
625 my $cloned_result_frame = {
626 2011 %{$stack_ref->[-1]},
627 $key => $value,
628 };
629  
630 # Make the copy into an object, if the original was one...
631 2011 ▶ if (my $class = blessed($stack_ref->[-1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = builtin::blessed($stack_ref->[-1])) 0 2011 2011
632 0 bless $cloned_result_frame, $class;
633 }
634  
635 2011 return $cloned_result_frame;
636 }
637  
638 # Create a clone of the current result frame with an additional key/value
639 # (As above, but preserving the "listiness" of the key being added to)...
640 131 ▶sub _extend_current_result_frame_with_list {
Subroutine _extend_current_result_frame_with_list: called CC=3 SCAR=11.2
641 131 my ($stack_ref, $key, $value) = @_;
642  
643 # Copy existing frame, appending new value to appropriate element's list...
644 my $cloned_result_frame = {
645 131 %{$stack_ref->[-1]},
646 $key => [
647 131 ▶ @{$stack_ref->[-1]{$key}//[]},
Condition100%2/2
$stack_ref->[-1]{$key} // []
l!l
3497
MC/DC100%1/1
$stack_ref->[-1]{$key} // []
$stack_ref->[-1]{$key}
Truth table100%2/2
A
A= $stack_ref->[-1]{$key}
Aresult
11
00
648 $value,
649 ],
650 };
651  
652 # Make the copy into an object, if the original was one...
653 131 ▶ if (my $class = blessed($stack_ref->[-1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = builtin::blessed($stack_ref->[-1])) 0 131 131
654 0 bless $cloned_result_frame, $class;
655 }
656  
657 131 return $cloned_result_frame;
658 }
659  
660 # Pop current result frame and add it to a clone of previous result frame
661 # (flattening it if possible, and preserving any blessing)...
662 820 ▶sub _pop_current_result_frame {
Subroutine _pop_current_result_frame: called CC=16 SCAR=27.9
663 820 my ($stack_ref, $key, $original_name, $value) = @_;
664  
665 # Where are we in the stack?
666 820 my $curr_frame = $stack_ref->[-1];
667 820 my $caller_frame = $stack_ref->[-2];
668  
669 # Track which frames are objects...
670 820 my $is_blessed_curr = blessed($curr_frame);
671 820 my $is_blessed_caller = blessed($caller_frame);
672  
673 # Remove "private" captures (i.e. those starting with _)...
674 820 delete @{$curr_frame}{grep {substr($_,0,1) eq '_'} keys %{$curr_frame} };
675  
676 # Remove "nocontext" marker...
677 820 my $nocontext = delete $curr_frame->{'~'};
678  
679 # Build a clone of the current frame...
680 my $cloned_result_frame
681 = exists $curr_frame->{'='} ? $curr_frame->{'='}
682 434 ▶ : $is_blessed_curr || length(join(q{}, keys %{$curr_frame})) ? { q{} => $value, %{$curr_frame} }
Branch: 1 branch
BranchTrueFalseTotal
keys %{$curr_frame;} ? : 0 102 102
683 820 ▶ : keys %{$curr_frame} ? $curr_frame->{q{}}
Branch: 2 branches
BranchTrueFalseTotal
exists $curr_frame->{'='} ? : 173 647 820
$is_blessed_curr || length join('', keys %{$curr_frame;}) ? : 545 102 647
Condition100%3/3
$is_blessed_curr or length join("", keys %{$curr_frame;})
l!l&&r!l&&!r
213332102
MC/DC100%2/2
$is_blessed_curr or length join("", keys %{$curr_frame;})
$is_blessed_curr length join("", keys %{$curr_frame;})
Truth table100%3/3
A or B
A= $is_blessed_currB= length join("", keys %{$curr_frame;})
ABresult
1X1
011
000
684 : $value
685 ;
686  
687 # Apply any appropriate handler...
688 820 ▶ if ($RULE_HANDLER) {
Branch: 1 branch
BranchTrueFalseTotal
if ($RULE_HANDLER) 88 732 820
689 88 ▶ if ($RULE_HANDLER->can($original_name) || $RULE_HANDLER->can('AUTOLOAD')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($RULE_HANDLER->can($original_name) or $RULE_HANDLER->can("AUTOLOAD")) 22 66 88
Condition66%2/3
$RULE_HANDLER->can($original_name) or $RULE_HANDLER->can("AUTOLOAD")
l!l&&r!l&&!r
22066
MC/DC50%1/2
$RULE_HANDLER->can($original_name) or $RULE_HANDLER->can("AUTOLOAD")
$RULE_HANDLER->can($original_name) $RULE_HANDLER->can("AUTOLOAD")
Truth table66%2/3
A or B
A= $RULE_HANDLER->can($original_name)B= $RULE_HANDLER->can("AUTOLOAD")
ABresult
1X1
011
000
690 22 my $replacement_result_frame
691 = $RULE_HANDLER->$original_name($cloned_result_frame);
692 22 ▶ if (defined $replacement_result_frame) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replacement_result_frame) 22 0 22
693 22 $cloned_result_frame = $replacement_result_frame;
694 }
695 }
696 }
697  
698 # Remove capture if not requested...
699 820 ▶ if ($nocontext && ref $cloned_result_frame eq 'HASH' && keys %{$cloned_result_frame} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nocontext and ref $cloned_result_frame eq "HASH" and keys %{$cloned_result_frame;} > 1) 28 792 820
Condition100%6/6
$nocontext and ref $cloned_result_frame eq "HASH" and keys %{$cloned_result_frame;} > 1
!ll&&!rl&&r
7108228
$nocontext and ref $cloned_result_frame eq "HASH"
!ll&&!rl&&r
7091110
MC/DC100%3/3
$nocontext and ref $cloned_result_frame eq "HASH" and keys %{$cloned_result_frame;} > 1
$nocontext ref $cloned_result_frame eq "HASH" keys %{$cloned_result_frame;} > 1
Truth table100%4/4
A and B and C
A= $nocontextB= ref $cloned_result_frame eq "HASH"C= keys %{$cloned_result_frame;} > 1
ABCresult
0XX0
10X0
1100
1111
700 28 delete $cloned_result_frame->{q{}};
701 }
702  
703 # Nest a clone of current frame inside a clone of the caller frame...
704 my $cloned_caller_frame = {
705 820 ▶ %{$caller_frame//{}},
Condition50%1/2
$caller_frame // {}
l!l
8200
MC/DC0%0/1
$caller_frame // {}
$caller_frame
Truth table50%1/2
A
A= $caller_frame
Aresult
11
00
706 $key => $cloned_result_frame,
707 };
708  
709 # Make the copies into objects, if the originals were...
710 820 ▶ if ($is_blessed_curr && !exists $curr_frame->{'='} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_blessed_curr and not exists $curr_frame->{"="}) 213 607 820
Condition66%2/3
$is_blessed_curr and not exists $curr_frame->{"="}
!ll&&!rl&&r
6070213
MC/DC50%1/2
$is_blessed_curr and not exists $curr_frame->{"="}
$is_blessed_curr not exists $curr_frame->{"="}
Truth table66%2/3
A and not(B)
A= $is_blessed_currB= not exists $curr_frame->{"="}
ABresult
0X0
100
111
711 213 bless $cloned_caller_frame->{$key}, $is_blessed_curr;
712 }
713 820 ▶ if ($is_blessed_caller) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_blessed_caller) 0 820 820
714 0 bless $cloned_caller_frame, $is_blessed_caller;
715 }
716  
717 820 return $cloned_caller_frame;
718 }
719  
720 # Pop current result frame and add it to a clone of previous result frame
721 # (flattening it if possible, and preserving any blessing)
722 # (As above, but preserving listiness of key being added to)...
723 1402 ▶sub _pop_current_result_frame_with_list {
Subroutine _pop_current_result_frame_with_list: called CC=16 SCAR=27.9
724 1402 my ($stack_ref, $key, $original_name, $value) = @_;
725  
726 # Where are we in the stack?
727 1402 my $curr_frame = $stack_ref->[-1];
728 1402 my $caller_frame = $stack_ref->[-2];
729  
730 # Track which frames are objects...
731 1402 my $is_blessed_curr = blessed($curr_frame);
732 1402 my $is_blessed_caller = blessed($caller_frame);
733  
734 # Remove "private" captures (i.e. those starting with _)...
735 1402 delete @{$curr_frame}{grep {substr($_,0,1) eq '_'} keys %{$curr_frame} };
736  
737 # Remove "nocontext" marker...
738 1402 my $nocontext = delete $curr_frame->{'~'};
739  
740 # Clone the current frame...
741 my $cloned_result_frame
742 = exists $curr_frame->{'='} ? $curr_frame->{'='}
743 1064 ▶ : $is_blessed_curr || length(join(q{}, keys %{$curr_frame})) ? { q{} => $value, %{$curr_frame} }
Branch: 1 branch
BranchTrueFalseTotal
keys %{$curr_frame;} ? : 0 951 951
744 1402 ▶ : keys %{$curr_frame} ? $curr_frame->{q{}}
Branch: 2 branches
BranchTrueFalseTotal
exists $curr_frame->{'='} ? : 218 1184 1402
$is_blessed_curr || length join('', keys %{$curr_frame;}) ? : 233 951 1184
Condition100%3/3
$is_blessed_curr or length join("", keys %{$curr_frame;})
l!l&&r!l&&!r
120113951
MC/DC100%2/2
$is_blessed_curr or length join("", keys %{$curr_frame;})
$is_blessed_curr length join("", keys %{$curr_frame;})
Truth table100%3/3
A or B
A= $is_blessed_currB= length join("", keys %{$curr_frame;})
ABresult
1X1
011
000
745 : $value
746 ;
747  
748 # Apply any appropriate handler...
749 1402 ▶ if ($RULE_HANDLER) {
Branch: 1 branch
BranchTrueFalseTotal
if ($RULE_HANDLER) 174 1228 1402
750 174 ▶ if ($RULE_HANDLER->can($original_name) || $RULE_HANDLER->can('AUTOLOAD')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($RULE_HANDLER->can($original_name) or $RULE_HANDLER->can("AUTOLOAD")) 106 68 174
Condition66%2/3
$RULE_HANDLER->can($original_name) or $RULE_HANDLER->can("AUTOLOAD")
l!l&&r!l&&!r
106068
MC/DC50%1/2
$RULE_HANDLER->can($original_name) or $RULE_HANDLER->can("AUTOLOAD")
$RULE_HANDLER->can($original_name) $RULE_HANDLER->can("AUTOLOAD")
Truth table66%2/3
A or B
A= $RULE_HANDLER->can($original_name)B= $RULE_HANDLER->can("AUTOLOAD")
ABresult
1X1
011
000
751 106 my $replacement_result_frame
752 = $RULE_HANDLER->$original_name($cloned_result_frame);
753 106 ▶ if (defined $replacement_result_frame) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replacement_result_frame) 106 0 106
754 106 $cloned_result_frame = $replacement_result_frame;
755 }
756 }
757 }
758  
759 # Remove capture if not requested...
760 1402 ▶ if ($nocontext && ref $cloned_result_frame eq 'HASH' && keys %{$cloned_result_frame} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nocontext and ref $cloned_result_frame eq "HASH" and keys %{$cloned_result_frame;} > 1) 82 1320 1402
Condition100%6/6
$nocontext and ref $cloned_result_frame eq "HASH" and keys %{$cloned_result_frame;} > 1
!ll&&!rl&&r
1316482
$nocontext and ref $cloned_result_frame eq "HASH"
!ll&&!rl&&r
1310686
MC/DC100%3/3
$nocontext and ref $cloned_result_frame eq "HASH" and keys %{$cloned_result_frame;} > 1
$nocontext ref $cloned_result_frame eq "HASH" keys %{$cloned_result_frame;} > 1
Truth table100%4/4
A and B and C
A= $nocontextB= ref $cloned_result_frame eq "HASH"C= keys %{$cloned_result_frame;} > 1
ABCresult
0XX0
10X0
1100
1111
761 82 delete $cloned_result_frame->{q{}};
762 }
763  
764 # Append a clone of current frame inside a clone of the caller frame...
765 my $cloned_caller_frame = {
766 1402 %{$caller_frame},
767 $key => [
768 1402 ▶ @{$caller_frame->{$key}//[]},
Condition100%2/2
$caller_frame->{$key} // []
l!l
716686
MC/DC100%1/1
$caller_frame->{$key} // []
$caller_frame->{$key}
Truth table100%2/2
A
A= $caller_frame->{$key}
Aresult
11
00
769 $cloned_result_frame,
770 ],
771 };
772  
773 # Make the copies into objects, if the originals were...
774 1402 ▶ if ($is_blessed_curr && !exists $curr_frame->{'='} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_blessed_curr and not exists $curr_frame->{"="}) 120 1282 1402
Condition66%2/3
$is_blessed_curr and not exists $curr_frame->{"="}
!ll&&!rl&&r
12820120
MC/DC50%1/2
$is_blessed_curr and not exists $curr_frame->{"="}
$is_blessed_curr not exists $curr_frame->{"="}
Truth table66%2/3
A and not(B)
A= $is_blessed_currB= not exists $curr_frame->{"="}
ABresult
0X0
100
111
775 120 bless $cloned_caller_frame->{$key}[-1], $is_blessed_curr;
776 }
777 1402 ▶ if ($is_blessed_caller) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_blessed_caller) 0 1402 1402
778 0 bless $cloned_caller_frame, $is_blessed_caller;
779 }
780  
781 1402 return $cloned_caller_frame;
782 }
783  
784  
785 #=====[ MISCELLANEOUS CONSTANTS ]=========================
786  
787 # Namespace in which grammar inheritance occurs...
788 82 my $CACHE = 'Regexp::Grammars::_CACHE_::';
789 82 my $CACHE_LEN = length $CACHE;
790 82 my %CACHE; #...for subrule tracking
791  
792 # This code inserted at the start of every grammar regex
793 # (initializes the result stack cleanly and backtrackably, via local)...
794 82 my $PROLOGUE = q{((?{; @! = () if !pos;
795 local @Regexp::Grammars::RESULT_STACK
796 = (@Regexp::Grammars::RESULT_STACK, {});
797 local $Regexp::Grammars::TIMEOUT = { limit => -1>>1 };
798 local $Regexp::Grammars::DEBUG = 'off' }) };
799  
800 # This code inserted at the end of every grammar regex
801 # (puts final result in %/. Also defines default <ws>, <hk>, etc.)...
802 82 my $EPILOGUE = q{)(?{; $Regexp::Grammars::RESULT_STACK[-1]{q{}} //= $^N;;
803 local $Regexp::Grammars::match_frame = pop @Regexp::Grammars::RESULT_STACK;
804 delete @{$Regexp::Grammars::match_frame}{
805 '~', grep {substr($_,0,1) eq '_'} keys %{$Regexp::Grammars::match_frame}
806 };
807 if (exists $Regexp::Grammars::match_frame->{'='}) {
808 if (ref($Regexp::Grammars::match_frame->{'='}) eq 'HASH') {
809 $Regexp::Grammars::match_frame
810 = $Regexp::Grammars::match_frame->{'='};
811 }
812 }
813 if (@Regexp::Grammars::RESULT_STACK) {
814 $Regexp::Grammars::RESULT_STACK[-1]{'(?R)'}
815 = $Regexp::Grammars::match_frame;
816 }
817 Regexp::Grammars::clear_rule_handler();
818 */ = $Regexp::Grammars::match_frame;
819 })|\Z(?{Regexp::Grammars::clear_rule_handler();})(?!)(?(DEFINE)
820 (?<ws__implicit__> \\s* )
821 (?<ws>
822 (?{$Regexp::Grammars::RESULT_STACK[-1]{'!'}=$#{!};})
823 \\s*
824 (?{;$#{!}=delete($Regexp::Grammars::RESULT_STACK[-1]{'!'})//0;
825 delete($Regexp::Grammars::RESULT_STACK[-1]{'@'});
826 })
827 )
828 (?<hk__implicit__> \\S+ )
829 (?<hk>
830 (?{$Regexp::Grammars::RESULT_STACK[-1]{'!'}=$#{!};})
831 \\S+
832 (?{;$#{!}=delete($Regexp::Grammars::RESULT_STACK[-1]{'!'})//0;
833 delete($Regexp::Grammars::RESULT_STACK[-1]{'@'});
834 })
835 )
836 (?<matchpos> (?{; $Regexp::Grammars::RESULT_STACK[-1]{"="} = pos; }) )
837 (?<matchline> (?{; $Regexp::Grammars::RESULT_STACK[-1]{"="} = 1 + substr($_,0,pos) =~ tr/\n/\n/; }) )
838 )
839 };
840 82 my $EPILOGUE_NC = $EPILOGUE;
841 82 $EPILOGUE_NC =~ s{ ; .* ;;}{;}xms;
842  
843  
844 #=====[ MISCELLANEOUS PATTERNS THAT MATCH USEFUL THINGS ]========
845  
846 # Match an identifier...
847 82 my $IDENT = qr{ [^\W\d] \w*+ }xms;
848 82 my $QUALIDENT = qr{ (?: $IDENT :: )*+ $IDENT }xms;
849  
850 # Match balanced parentheses, taking into account \-escapes and []-escapes...
851 82 my $PARENS = qr{
852 (?&VAR_PARENS)
853 (?(DEFINE)
854 (?<VAR_PARENS> \( (?: \\. | (?&VAR_PARENS) | (?&CHARSET) | [^][()\\]++)*+ \) )
855 (?<CHARSET> \[ \^?+ \]?+ (?: \[:\w+:\] | \\. | [^]])*+ \] )
856  
857 )
858 }xms;
859  
860 # Match a <ws:...> directive within rules...
861 82 my $WS_PATTERN = qr{<ws: ((?: \\. | [^\\()>]++ | $PARENS )*+) >}xms;
862  
863  
864 #=====[ UTILITY SUBS FOR ERROR AND WARNING MESSAGES ]========
865  
866 53 ▶sub _uniq {
Subroutine _uniq: called CC=2 SCAR=7.0
867 53 my %seen;
868 53 ▶ return grep { defined $_ && !$seen{$_}++ } @_;
Condition66%2/3
defined $_ && !$seen{$_}++
!ll&&!rl&&r
0383
MC/DC50%1/2
defined $_ && !$seen{$_}++
defined $_ !$seen{$_}++
Truth table66%2/3
A && not(B)
A= defined $_B= !$seen{$_}++
ABresult
0X0
100
111
869 }
870  
871 # Default translator for error messages...
872 42 ▶my $ERRORMSG_TRANSLATOR = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
873 42 my ($errormsg, $rulename, $context) = @_;
874  
875 42 ▶ $rulename = 'valid input' if $rulename eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $rulename eq "" 15 27 42
876 42 ▶ $context //= '<end of string>';
Condition50%1/2
$context //= "<end of string>"
l!l
420
MC/DC0%0/1
$context //= "<end of string>"
$context
Truth table50%1/2
A
A= $context
Aresult
11
00
877  
878 # Unimplemented subrule when rulename starts with '-'...
879 42 ▶ if (substr($rulename,0,1) eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($rulename, 0, 1) eq "-") 8 34 42
880 8 $rulename = substr($rulename,1);
881 8 return "Can't match subrule <$rulename> (not implemented)";
882 }
883  
884 # Empty message converts to a "Expected...but found..." message...
885 34 ▶ if ($errormsg eq q{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errormsg eq "") 22 12 34
886 22 $rulename =~ tr/_/ /;
887 22 $rulename = lc($rulename);
888 22 return "Expected $rulename, but found '$context' instead";
889 }
890  
891 # "Expecting..." messages get "but found" added...
892 12 ▶ if (lc(substr($errormsg,0,6)) eq 'expect') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc substr($errormsg, 0, 6) eq "expect") 5 7 12
893 5 return "$errormsg, but found '$context' instead";
894 }
895  
896 # Everything else stays "as is"...
897 7 return $errormsg;
898 82 };
899  
900 # Allow user to set translation...
901 1 ▶sub set_error_translator {
Subroutine set_error_translator: called CC=3 SCAR=11.1
Pod undocumented
902 { package Regexp::Grammars::TranslatorRestorer;
903 1 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
904 1 my ($class, $old_translator) = @_;
905 1 bless \$old_translator, $class;
906 }
907 1 ▶ sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
908 1 my ($old_translator_ref) = @_;
909 1 $ERRORMSG_TRANSLATOR = ${$old_translator_ref};
910 }
911 }
912  
913 1 my ($translator_ref) = @_;
914 1 ▶ die "Usage: set_error_translator(\$subroutine_reference)\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $translator_ref ne "CODE" 0 1 1
915 if ref($translator_ref) ne 'CODE';
916  
917 1 my $old_translator_ref = $ERRORMSG_TRANSLATOR;
918 1 $ERRORMSG_TRANSLATOR = $translator_ref;
919  
920 return defined wantarray
921 1 ▶ ? Regexp::Grammars::TranslatorRestorer->new($old_translator_ref)
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 1 0 1
922 : ();
923 }
924  
925 # Dispatch to current translator for error messages...
926 53 ▶sub _translate_errormsg {
Subroutine _translate_errormsg: called CC=1 SCAR=0.0
927 53 goto &{$ERRORMSG_TRANSLATOR};
928 }
929  
930 #=====[ SUPPORT FOR TRANSLATING GRAMMAR-ENHANCED REGEX TO NATIVE REGEX ]====
931  
932 # Store any specified grammars...
933 82 my %user_defined_grammar;
934  
935 82 my %REPETITION_DESCRIPTION_FOR = (
936 '+' => 'once or more',
937 '*' => 'any number of times',
938 '?' => 'if possible',
939 '+?' => 'as few times as possible',
940 '*?' => 'as few times as possible',
941 '??' => 'if necessary',
942 '++' => 'as many times as possible',
943 '*+' => 'as many times as possible',
944 '?+' => 'if possible',
945 );
946  
947 1826 ▶sub _translate_raw_regex {
Subroutine _translate_raw_regex: called CC=11 SCAR=30.6
948 1826 my ($regex, $debug_build, $debug_runtime) = @_;
949  
950 1826 ▶ my $is_comment = substr($regex, 0, 1) eq q{#}
Condition66%2/3
substr($regex, 0, 1) eq '#' || substr($regex, 0, 3) eq '(?#'
l!l&&r!l&&!r
201824
MC/DC50%1/2
substr($regex, 0, 1) eq '#' || substr($regex, 0, 3) eq '(?#'
substr($regex, 0, 1) eq '#' substr($regex, 0, 3) eq '(?#'
Truth table66%2/3
A || B
A= substr($regex, 0, 1) eq '#'B= substr($regex, 0, 3) eq '(?#'
ABresult
1X1
011
000
951 || substr($regex, 0, 3) eq q{(?#};
952 1826 my $visible_regex = _squeeze_ws($regex);
953  
954 # Report how regex was interpreted, if requested to...
955 1826 ▶ if ($debug_build && $visible_regex ne q{} && $visible_regex ne q{ }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_build and $visible_regex ne "" and $visible_regex ne " ") 0 1826 1826
Condition33%2/6
$debug_build and $visible_regex ne "" and $visible_regex ne " "
!ll&&!rl&&r
182600
$debug_build and $visible_regex ne ""
!ll&&!rl&&r
182600
MC/DC0%0/3
$debug_build and $visible_regex ne "" and $visible_regex ne " "
$debug_build $visible_regex ne "" $visible_regex ne " "
Truth table25%1/4
A and B and C
A= $debug_buildB= $visible_regex ne ""C= $visible_regex ne " "
ABCresult
0XX0
10X0
1100
1111
956 0 ▶ _debug_notify( info =>
Branch: 1 branch
BranchTrueFalseTotal
$is_comment ? : 0 0 0
957 " |",
958 " |...Treating '$visible_regex' as:",
959 ($is_comment ? " | \\ a comment (which will be ignored)"
960 : " | \\ normal Perl regex syntax"
961 ),
962 );
963 }
964  
965 1826 ▶ return q{} if $is_comment;
Branch: 1 branch
BranchTrueFalseTotal
if $is_comment 2 1824 1826
966  
967 # Generate run-time debugging code (if any)...
968 1824 my ($debug_pre, $debug_post)
969 = _build_raw_debugging_statements($debug_runtime,$visible_regex, +1);
970  
971 # Replace negative lookahead with one that works under R::G...
972 1824 $regex =~ s{\(\?!}{(?!(?!)|}gxms;
973 # TODO: Also replace positive lookahead with one that works under R::G...
974 # This replacement should be of the form:
975 # $regex =~ s{\(\?!}{(?!(?!)|(?!(?!)|}gxms;
976 # but need to find a way to insert the extra ) at the other end
977  
978 1824 ▶ return $debug_runtime && $regex eq '|' ? $regex . $debug_post
Branch: 2 branches
BranchTrueFalseTotal
$debug_runtime && $regex eq '|' ? : 0 1824 1824
$debug_runtime && $regex =~ /\S/ ? : 0 1824 1824
Condition33%2/6
$debug_runtime and $regex eq "|"
!ll&&!rl&&r
182400
$debug_runtime and $regex =~ /\S/
!ll&&!rl&&r
182400
MC/DC0%0/2
$debug_runtime and $regex eq "|"
$debug_runtime $regex eq "|"
MC/DC0%0/2
$debug_runtime and $regex =~ /\S/
$debug_runtime $regex =~ /\S/
Truth table33%1/3
A and B
A= $debug_runtimeB= $regex eq "|"
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $debug_runtimeB= $regex =~ /\S/
ABresult
0X0
100
111
979 : $debug_runtime && $regex =~ /\S/ ? "(?#)(?:$debug_pre($regex)$debug_post(?#))"
980  
981 # TODO: REWORK THIS INSUFFICENT FIX FOR t/grammar_autospace.t...
982 # : $regex !~ /\S/ ? "(?:$regex)"
983  
984 : $regex;
985 }
986  
987 # Report and convert a debugging directive...
988 0 ▶sub _translate_debug_directive {
Subroutine _translate_debug_directive: not called CC=2 SCAR=17.9
989 0 my ($construct, $cmd, $debug_build) = @_;
990  
991 # Report how directive was interpreted, if requested to...
992 0 ▶ if ($debug_build) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_build) 0 0 0
993 0 _debug_notify( info =>
994 " |",
995 " |...Treating $construct as:",
996 " | \\ Change run-time debugging mode to '$cmd'",
997 );
998 }
999  
1000 0 return qq{(?{; local \$Regexp::Grammars::DEBUG = q{$cmd}; }) };
1001 }
1002  
1003 # Report and convert a timeout directive...
1004 0 ▶sub _translate_timeout_directive {
Subroutine _translate_timeout_directive: not called CC=5 SCAR=34.0
1005 0 my ($construct, $timeout, $debug_build) = @_;
1006  
1007 # Report how directive was interpreted, if requested to...
1008 0 ▶ if ($debug_build) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_build) 0 0 0
1009 0 ▶ _debug_notify( info =>
Branch: 2 branches
BranchTrueFalseTotal
$timeout > 0 ? : 0 0 0
$timeout == 1 ? : 0 0 0
1010 " |",
1011 " |...Treating $construct as:",
1012 ($timeout > 0
1013 ? " | \\ Cause the entire parse to fail after $timeout second" . ($timeout==1 ? q{} : q{s})
1014 : " | \\ Cause the entire parse to fail immediately"
1015 ),
1016 );
1017 }
1018  
1019 0 ▶ return $timeout > 0
Branch: 1 branch
BranchTrueFalseTotal
$timeout > 0 ? : 0 0 0
1020 ? qq{(?{; local \$Regexp::Grammars::TIMEOUT = { duration => $timeout, limit => time() + $timeout }; }) }
1021 : qq{(*COMMIT)(*FAIL)};
1022 }
1023  
1024 # Report and convert a <require:...> directive...
1025 0 ▶sub _translate_require_directive {
Subroutine _translate_require_directive: not called CC=2 SCAR=17.9
1026 0 my ($construct, $condition, $debug_build) = @_;
1027  
1028 0 $condition = substr($condition, 3, -2);
1029  
1030 # Report how directive was interpreted, if requested to...
1031 0 ▶ if ($debug_build) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_build) 0 0 0
1032 0 _debug_notify( info =>
1033 " |",
1034 " |...Treating $construct as:",
1035 " | \\ Require that {$condition} is true",
1036 );
1037 }
1038  
1039 0 my $quoted_condition = $condition;
1040 0 $quoted_condition =~ s{\$}{}xms;
1041  
1042 0 return qq{(?(?{;$condition})
1043 (?{;Regexp::Grammars::_debug_require(
1044 scalar \@Regexp::Grammars::RESULT_STACK, q{$quoted_condition}, 1)
1045 if \$Regexp::Grammars::DEBUG_LEVEL{\$Regexp::Grammars::DEBUG}})
1046 | (?{;Regexp::Grammars::_debug_require(
1047 scalar \@Regexp::Grammars::RESULT_STACK, q{$quoted_condition}, 0)
1048 if \$Regexp::Grammars::DEBUG_LEVEL{\$Regexp::Grammars::DEBUG}})(?!))
1049 };
1050 }
1051  
1052  
1053 # Report and convert a <minimize:> directive...
1054 3 ▶sub _translate_minimize_directive {
Subroutine _translate_minimize_directive: called CC=2 SCAR=7.6
1055 3 my ($construct, $debug_build) = @_;
1056  
1057 # Report how directive was interpreted, if requested to...
1058 3 ▶ if ($debug_build) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_build) 0 3 3
1059 0 _debug_notify( info =>
1060 " |",
1061 " |...Treating $construct as:",
1062 " | \\ Minimize result value if possible",
1063 );
1064 }
1065  
1066 3 return q{(?{;
1067 if (1 == grep { $_ ne '!' && $_ ne '@' && $_ ne '~' } keys %MATCH) { # ...single alnum key
1068 local %Regexp::Grammars::matches = %MATCH;
1069 delete @Regexp::Grammars::matches{'!', '@', '~'};
1070 local ($Regexp::Grammars::only_key) = keys %Regexp::Grammars::matches;
1071 local $Regexp::Grammars::array_ref = $MATCH{$Regexp::Grammars::only_key};
1072 if (ref($Regexp::Grammars::array_ref) eq 'ARRAY' && 1 == @{$Regexp::Grammars::array_ref}) {
1073 $MATCH = $Regexp::Grammars::array_ref->[0];
1074 }
1075 }
1076 })};
1077 }
1078  
1079 # Report and convert a debugging directive...
1080 19 ▶sub _translate_error_directive {
Subroutine _translate_error_directive: called CC=9 SCAR=22.9
1081 19 my ($construct, $type, $msg, $debug_build, $subrule_name) = @_;
1082 19 ▶ $subrule_name //= 'undef';
Condition50%1/2
$subrule_name //= "undef"
l!l
190
MC/DC0%0/1
$subrule_name //= "undef"
$subrule_name
Truth table50%1/2
A
A= $subrule_name
Aresult
11
00
1083  
1084 # Determine severity...
1085 19 ▶ my $severity = ($type eq 'error') ? 'fail' : 'non-fail';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'error' ? : 11 8 19
1086  
1087 # Determine fatality (and build code to invoke it)...
1088 19 ▶ my $fatality = ($type eq 'fatal') ? '(*COMMIT)(*FAIL)' : q{};
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'fatal' ? : 2 17 19
1089  
1090 # Unpack message...
1091 19 ▶ if (substr($msg,0,3) eq '(?{') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($msg, 0, 3) eq '(?{') { } 4 15 19
1092 4 $msg = 'do'. substr($msg,2,-1);
1093 }
1094 else {
1095 15 $msg = quotemeta $msg;
1096 15 $msg = qq{qq{$msg}};
1097 }
1098  
1099 # Report how directive was interpreted, if requested to...
1100 19 ▶ if ($debug_build) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_build) 0 19 19
1101 0 ▶ _debug_notify( info => " |",
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'log' ? : 0 0 0
1102 " |...Treating $construct as:",
1103 ( $type eq 'log' ? " | \\ Log a message to the logfile"
1104 : " | \\ Append a $severity error message to \@!"
1105 ),
1106 );
1107 }
1108  
1109 # Generate the regex...
1110 19 ▶ return $type eq 'log'
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'log' ? : 0 19 19
$severity eq 'fail' ? : 11 8 19
1111 ? qq{(?{Regexp::Grammars::_debug_logmsg(scalar \@Regexp::Grammars::RESULT_STACK,$msg)
1112 if \$Regexp::Grammars::DEBUG_LEVEL{\$Regexp::Grammars::DEBUG}
1113 })}
1114  
1115 : qq{(?:(?{;local \$Regexp::Grammar::_memopos=pos();})
1116 (?>\\s*+((?-s).{0,$MAX_CONTEXT_WIDTH}+))
1117 (?{; pos() = \$Regexp::Grammar::_memopos;
1118 @! = Regexp::Grammars::_uniq(
1119 @!,
1120 Regexp::Grammars::_translate_errormsg($msg,q{$subrule_name},\$CONTEXT)
1121 ) }) (?!)|}
1122 . ($severity eq 'fail' ? q{(?!)} : $fatality)
1123 . q{)}
1124 ;
1125 }
1126  
1127 110 ▶sub _translate_subpattern {
Subroutine _translate_subpattern: called CC=18 SCAR=31.8
1128 110 my ($construct, $alias, $subpattern, $savemode, $postmodifier, $debug_build, $debug_runtime, $timeout, $backref)
1129 = @_;
1130  
1131 # Determine save behaviour...
1132 110 my $is_noncapturing = $savemode eq 'noncapturing';
1133 110 my $is_listifying = $savemode eq 'list';
1134 110 my $is_codeblock = substr($subpattern,0,3) eq '(?{';
1135 110 ▶ my $value_saved = $is_codeblock ? '$^R' : '$^N';
Branch: 1 branch
BranchTrueFalseTotal
$is_codeblock ? : 21 89 110
1136 110 ▶ my $do_something_with = $is_codeblock ? 'execute the code block' : 'match the pattern';
Branch: 1 branch
BranchTrueFalseTotal
$is_codeblock ? : 21 89 110
1137 110 ▶ my $result = $is_codeblock ? 'result' : 'matched substring';
Branch: 1 branch
BranchTrueFalseTotal
$is_codeblock ? : 21 89 110
1138 110 ▶ my $description = $is_codeblock ? substr($subpattern,2,-1)
Branch: 2 branches
BranchTrueFalseTotal
$is_codeblock ? : 21 89 110
defined $backref ? : 10 79 89
1139 : defined $backref ? $backref
1140 : $subpattern;
1141 110 ▶ my $debug_construct
Branch: 1 branch
BranchTrueFalseTotal
$is_codeblock ? : 21 89 110
1142 = $is_codeblock ? '<' . substr($alias,1,-1) . '= (?{;' . substr($subpattern,3,-2) . '})>'
1143 : $construct
1144 ;
1145  
1146 # Report how construct was interpreted, if requested to...
1147 110 ▶ my $repeatedly = $REPETITION_DESCRIPTION_FOR{$postmodifier} // q{};
Condition50%1/2
$REPETITION_DESCRIPTION_FOR{$postmodifier} // ""
l!l
0110
MC/DC0%0/1
$REPETITION_DESCRIPTION_FOR{$postmodifier} // ""
$REPETITION_DESCRIPTION_FOR{$postmodifier}
Truth table50%1/2
A
A= $REPETITION_DESCRIPTION_FOR{$postmodifier}
Aresult
11
00
1148 110 ▶ my $results = $is_listifying && $postmodifier ? "each $result"
Branch: 3 branches
BranchTrueFalseTotal
$is_listifying && $postmodifier ? : 0 110 110
substr($postmodifier, 0, 1) eq '?' ? : 0 110 110
$postmodifier && !$is_noncapturing ? : 0 110 110
Condition50%3/6
$is_listifying and $postmodifier
!ll&&!rl&&r
92180
$postmodifier and not $is_noncapturing
!ll&&!rl&&r
11000
MC/DC0%0/2
$is_listifying and $postmodifier
$is_listifying $postmodifier
MC/DC0%0/2
$postmodifier and not $is_noncapturing
$postmodifier not $is_noncapturing
Truth table66%2/3
A and B
A= $is_listifyingB= $postmodifier
ABresult
0X0
100
111
Truth table33%1/3
A and not(B)
A= $postmodifierB= not $is_noncapturing
ABresult
0X0
100
111
1149 : substr($postmodifier,0,1) eq '?' ? "any $result"
1150 : $postmodifier && !$is_noncapturing ? "only the final $result"
1151 : "the $result"
1152 ;
1153 110 ▶ if ($debug_build) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_build) 0 110 110
1154 0 ▶ _debug_notify( info =>
Branch: 2 branches
BranchTrueFalseTotal
$is_noncapturing ? : 0 0 0
$is_listifying ? : 0 0 0
1155 " |",
1156 " |...Treating $construct as:",
1157 " | | $do_something_with $description $repeatedly",
1158 ( $is_noncapturing ? " | \\ but don't save $results"
1159 : $is_listifying ? " | \\ appending $results to \@{\$MATCH{$alias}}"
1160 : " | \\ saving $results in \$MATCH{$alias}"
1161 )
1162 );
1163 }
1164  
1165 # Generate run-time debugging code (if any)...
1166 110 my ($debug_pre, $debug_post)
1167 = _build_debugging_statements($debug_runtime,$debug_construct, +1);
1168  
1169 # Generate post-match result-capturing code, if match captures...
1170 110 ▶ my $post_action = $is_noncapturing
Branch: 1 branch
BranchTrueFalseTotal
$is_noncapturing ? : 6 104 110
1171 ? q{}
1172 : qq{local \@Regexp::Grammars::RESULT_STACK = (
1173 \@Regexp::Grammars::RESULT_STACK[0..\@Regexp::Grammars::RESULT_STACK-2],
1174 Regexp::Grammars::_extend_current_result_frame_with_$savemode(
1175 \\\@Regexp::Grammars::RESULT_STACK, $alias, $value_saved
1176 ),
1177 );}
1178 ;
1179  
1180 # Generate timeout test...
1181 110 ▶ my $timeout_test = $timeout ? q{(??{;Regexp::Grammars::_test_timeout()})} : q{};
Branch: 1 branch
BranchTrueFalseTotal
$timeout ? : 0 110 110
1182  
1183 # Translate to standard regex code...
1184 110 return qq{$timeout_test(?{;local \@Regexp::Grammars::RESULT_STACK = \@Regexp::Grammars::RESULT_STACK;$debug_pre})(?:($subpattern)(?{;$post_action$debug_post}))$postmodifier};
1185 }
1186  
1187  
1188 14 ▶sub _translate_hashmatch {
Subroutine _translate_hashmatch: called CC=14 SCAR=28.9
1189 14 my ($construct, $alias, $hashname, $keypat, $savemode, $postmodifier, $debug_build, $debug_runtime, $timeout)
1190 = @_;
1191  
1192 # Empty or missing keypattern defaults to <.hk>...
1193 14 ▶ if (!defined $keypat || $keypat !~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $keypat && $keypat =~ /\S/) { } 8 6 14
Condition66%2/3
defined $keypat && $keypat =~ /\S/
!ll&&!rl&&r
806
MC/DC50%1/2
defined $keypat && $keypat =~ /\S/
defined $keypat $keypat =~ /\S/
Truth table66%2/3
A && B
A= defined $keypatB= $keypat =~ /\S/
ABresult
0X0
100
111
1194 8 $keypat = '(?&hk__implicit__)'
1195 }
1196 else {
1197 6 $keypat = substr($keypat, 1, -1);
1198 }
1199  
1200 # Determine save behaviour...
1201 14 my $is_noncapturing = $savemode eq 'noncapturing';
1202 14 my $is_listifying = $savemode eq 'list';
1203  
1204 # Convert hash to hash lookup...
1205 14 my $hash_lookup = '$' . substr($hashname, 1). '{$^N}';
1206  
1207 # Report how construct was interpreted, if requested to...
1208 14 ▶ my $repeatedly = $REPETITION_DESCRIPTION_FOR{$postmodifier} // q{};
Condition100%2/2
$REPETITION_DESCRIPTION_FOR{$postmodifier} // ""
l!l
68
MC/DC100%1/1
$REPETITION_DESCRIPTION_FOR{$postmodifier} // ""
$REPETITION_DESCRIPTION_FOR{$postmodifier}
Truth table100%2/2
A
A= $REPETITION_DESCRIPTION_FOR{$postmodifier}
Aresult
11
00
1209 14 ▶ my $results = $is_listifying && $postmodifier ? 'each matched key'
Branch: 3 branches
BranchTrueFalseTotal
$is_listifying && $postmodifier ? : 3 11 14
substr($postmodifier, 0, 1) eq '?' ? : 0 11 11
$postmodifier && !$is_noncapturing ? : 0 11 11
Condition66%4/6
$is_listifying and $postmodifier
!ll&&!rl&&r
1103
$postmodifier and not $is_noncapturing
!ll&&!rl&&r
830
MC/DC50%1/2
$is_listifying and $postmodifier
$is_listifying $postmodifier
MC/DC0%0/2
$postmodifier and not $is_noncapturing
$postmodifier not $is_noncapturing
Truth table66%2/3
A and B
A= $is_listifyingB= $postmodifier
ABresult
0X0
100
111
Truth table66%2/3
A and not(B)
A= $postmodifierB= not $is_noncapturing
ABresult
0X0
100
111
1210 : substr($postmodifier,0,1) eq '?' ? 'any matched key'
1211 : $postmodifier && !$is_noncapturing ? 'only the final matched key'
1212 : 'the matched key'
1213 ;
1214 14 ▶ if ($debug_build) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_build) 0 14 14
1215 0 ▶ _debug_notify( info =>
Branch: 2 branches
BranchTrueFalseTotal
$is_noncapturing ? : 0 0 0
$is_listifying ? : 0 0 0
1216 " |",
1217 " |...Treating $construct as:",
1218 " | | match a key from the hash $hashname $repeatedly",
1219 ( $is_noncapturing ? " | \\ but don't save $results"
1220 : $is_listifying ? " | \\ appending $results to \$MATCH{$alias}"
1221 : " | \\ saving $results in \$MATCH{$alias}"
1222 )
1223 );
1224 }
1225  
1226 # Generate run-time debugging code (if any)...
1227 14 my ($debug_pre, $debug_post)
1228 = _build_debugging_statements($debug_runtime,$construct, +1);
1229  
1230 # Generate post-match result-capturing code, if match captures...
1231 14 ▶ my $post_action = $is_noncapturing
Branch: 1 branch
BranchTrueFalseTotal
$is_noncapturing ? : 4 10 14
1232 ? q{}
1233 : qq{local \@Regexp::Grammars::RESULT_STACK = (
1234 \@Regexp::Grammars::RESULT_STACK[0..\@Regexp::Grammars::RESULT_STACK-2],
1235 Regexp::Grammars::_extend_current_result_frame_with_$savemode(
1236 \\\@Regexp::Grammars::RESULT_STACK, $alias, \$^N
1237 ),
1238 );}
1239 ;
1240  
1241 # Generate timeout test...
1242 14 ▶ my $timeout_test = $timeout ? q{(??{;Regexp::Grammars::_test_timeout()})} : q{};
Branch: 1 branch
BranchTrueFalseTotal
$timeout ? : 0 14 14
1243  
1244 # Translate to standard regex code...
1245 14 return qq{$timeout_test(?:(?{;local \@Regexp::Grammars::RESULT_STACK
1246 = \@Regexp::Grammars::RESULT_STACK;$debug_pre})(?:($keypat)(??{exists $hash_lookup ? q{} : q{(?!)}})(?{;$post_action$debug_post})))$postmodifier};
1247 }
1248  
1249  
1250 # Convert a "<rule><qualifier> % <rule>" construct to pure Perl 5.10...
1251 73 ▶sub _translate_separated_list {
Subroutine _translate_separated_list: called CC=14 SCAR=26.8
1252 73 my ($term, $op, $separator, $term_trans, $sep_trans,
1253 $ws, $debug_build, $debug_runtime, $timeout) = @_;
1254  
1255 # This insertion ensures backtracking upwinds the stack correctly...
1256 73 state $CHECKPOINT = q{(?{;@Regexp::Grammars::RESULT_STACK = @Regexp::Grammars::RESULT_STACK;})};
1257  
1258 # Translate meaningful whitespace...
1259 73 ▶ $ws = length($ws) ? q{(?&ws__implicit__)} : q{};
Branch: 1 branch
BranchTrueFalseTotal
length $ws ? : 70 3 73
1260  
1261 # Generate support for optional trailing separator...
1262 73 ▶ my $opt_trailing = substr($op,-2) eq '%%' ? qq{$ws$sep_trans?}
Branch: 1 branch
BranchTrueFalseTotal
substr($op, -2) eq '%%' ? : 17 56 73
1263 : q{};
1264  
1265 # Generate timeout test...
1266 73 ▶ my $timeout_test = $timeout ? q{(??{;Regexp::Grammars::_test_timeout()})} : q{};
Branch: 1 branch
BranchTrueFalseTotal
$timeout ? : 0 73 73
1267  
1268 # Report how construct was interpreted, if requested to...
1269 73 ▶ if ($debug_build) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_build) 0 73 73
1270 0 ▶ _debug_notify( info =>
Branch: 1 branch
BranchTrueFalseTotal
substr($op, -2) eq '%%' ? : 0 0 0
1271 " |",
1272 " |...Treating $term $op $separator as:",
1273 " | | repeatedly match the subrule $term",
1274 " | \\ as long as the matches are separated by matches of $separator",
1275 (substr($op,-2) eq '%%' ?
1276 " | \\ and allowing an optional trailing $separator"
1277 : q{}
1278 )
1279 );
1280 }
1281  
1282 # One-or-more...
1283 73 ▶ return qq{$timeout_test(?:$ws$CHECKPOINT$sep_trans$ws$term_trans)*$+$opt_trailing}
Branch: 1 branch
BranchTrueFalseTotal
if $op =~ / [*][*]() | [+]([+?]?) \s* %%?+ | \{ 1, \}([+?]?) \s* %%?+ /msx 51 22 73
1284 if $op =~ m{ [*][*]() | [+]([+?]?) \s* %%?+ | \{ 1, \}([+?]?) \s* %%?+ }xms;
1285  
1286 # Zero-or-more...
1287 return
1288 22 ▶ qq{{0}$timeout_test$ws(?:$term_trans(?:$ws$CHECKPOINT$sep_trans$ws$term_trans)*$+)?$+$opt_trailing}
Branch: 1 branch
BranchTrueFalseTotal
if $op =~ / [*]([+?]?) \s* %%? | \{ 0, \}([+?]?) \s* %%? /msx 4 18 22
1289 if $op =~ m{ [*]([+?]?) \s* %%? | \{ 0, \}([+?]?) \s* %%? }xms;
1290  
1291 # One-or-zero...
1292 18 ▶ return qq{?$+$opt_trailing}
Branch: 1 branch
BranchTrueFalseTotal
if $op =~ / [?]([+?]?) \s* %%? | \{ 0,1 \}([+?]?) \s* %%? /msx 4 14 18
1293 if $op =~ m{ [?]([+?]?) \s* %%? | \{ 0,1 \}([+?]?) \s* %%? }xms;
1294  
1295 # Zero exactly...
1296 14 ▶ return qq{{0}$ws$opt_trailing}
Branch: 1 branch
BranchTrueFalseTotal
if $op =~ / \{ 0 \}[+?]? \s* %%? /msx 2 12 14
1297 if $op =~ m{ \{ 0 \}[+?]? \s* %%? }xms;
1298  
1299 # N exactly...
1300 12 ▶ if ($op =~ m{ \{ (\d+) \}([+?]?) \s* %%? }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ / \{ (\d+) \}([+?]?) \s* %%? /msx) 2 10 12
1301 2 my $min = $1-1;
1302 return
1303 2 qq{{0}$timeout_test$ws(?:$term_trans(?:$ws$CHECKPOINT$sep_trans$ws$term_trans){$min}$+$opt_trailing)}
1304 }
1305  
1306 # Zero-to-N...
1307 10 ▶ if ($op =~ m{ \{ 0,(\d+) \}([+?]?) \s* %%? }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ / \{ 0,(\d+) \}([+?]?) \s* %%? /msx) 2 8 10
1308 2 my $max = $1-1;
1309 return
1310 2 qq{{0}$timeout_test$ws(?:$term_trans(?:$ws$CHECKPOINT$sep_trans$ws$term_trans){0,$max}$+)?$+$opt_trailing}
1311 }
1312  
1313 # M-to-N and M-to-whatever...
1314 8 ▶ if ($op =~ m{ \{ (\d+),(\d*) \} ([+?]?) \s* %%? }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ / \{ (\d+),(\d*) \} ([+?]?) \s* %%? /msx) 8 0 8
1315 8 my $min = $1-1;
1316 8 ▶ my $max = $2 ? $2-1 : q{};
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 6 2 8
1317 return
1318 8 qq{{0}$timeout_test$ws(?:$term_trans(?:$ws$CHECKPOINT$sep_trans$ws$term_trans){$min,$max}$+$opt_trailing)}
1319 }
1320  
1321 # Somehow we missed a case (this should never happen)...
1322 0 die "Internal error: missing case in separated list handler";
1323 }
1324  
1325 299 ▶sub _translate_subrule_call {
Subroutine _translate_subrule_call: called CC=22 SCAR=31.8
1326 299 my ($source_line, $source_file, $rulename, $grammar_name, $construct, $alias,
1327 $subrule, $args, $savemode, $postmodifier,
1328 $debug_build, $debug_runtime, $timeout, $valid_subrule_names_ref) = @_;
1329  
1330 # Translate arg list, if provided...
1331 299 my $arg_desc;
1332 299 ▶ if ($args eq q{}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args eq '') { } 289 10 299
elsif (substr($args, 0, 3) eq '(?{') { } 1 9 10
1333 289 $args = q{()};
1334 }
1335 elsif (substr($args,0,3) eq '(?{') {
1336 # Turn parencode into do block...
1337 1 $arg_desc = substr($args,3,-2);
1338 1 substr($args,1,1) = 'do';
1339 }
1340 else {
1341 # Turn abbreviated format into a key=>value list...
1342 9 $args =~ s{ [(,] \s* \K : (\w+) (?= \s* [,)] ) }{$1 => \$MATCH{'$1'}}gxms;
1343 9 $arg_desc = substr($args,1,-1);
1344 }
1345  
1346 # Transform qualified subrule names...
1347 299 my $simple_subrule = $subrule;
1348 299 ▶ my $start_grammar = (($simple_subrule =~ s{(.*)::}{}xms) ? $1 : "");
Branch: 1 branch
BranchTrueFalseTotal
$simple_subrule =~ s/(.*):://msx ? : 3 296 299
1349 299 ▶ if ($start_grammar !~ /^NEXT$|::/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($start_grammar =~ /^NEXT$|::/) 297 2 299
1350 297 $start_grammar = caller(3).'::'.$start_grammar;
1351 }
1352  
1353 299 ▶ my @candidates = $start_grammar eq 'NEXT' ? _ancestry_of($grammar_name)
Branch: 1 branch
BranchTrueFalseTotal
$start_grammar eq 'NEXT' ? : 1 298 299
1354 : _ancestry_of($start_grammar);
1355  
1356 # Rename fully-qualified rule call, if to ancestor grammar...
1357 RESOLVING:
1358 299 for my $parent_class (@candidates) {
1359 302 my $inherited_subrule = $parent_class.'::'.$simple_subrule;
1360 302 ▶ if ($CACHE{$inherited_subrule}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CACHE{$inherited_subrule}) 3 299 302
1361 3 $subrule = $inherited_subrule;
1362 3 last RESOLVING;
1363 }
1364 }
1365  
1366 # Replace package separators, which regex engine can't handle...
1367 299 my $internal_subrule = $subrule;
1368 299 $internal_subrule =~ s{::}{_88_}gxms;
1369  
1370 # Shortcircuit if unknown subrule invoked...
1371 299 ▶ if (!$valid_subrule_names_ref->{$subrule}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($valid_subrule_names_ref->{$subrule}) 0 299 299
1372 0 _debug_notify( error =>
1373 qq{Found call to $construct inside definition of $rulename},
1374 qq{near $source_file line $source_line.},
1375 qq{But no <rule: $subrule> or <token: $subrule> was defined in the grammar},
1376 qq{(Did you misspell $construct? Or forget to define the rule?)},
1377 q{},
1378 );
1379 0 return "(?{Regexp::Grammars::_debug_fatal('$construct')})(*COMMIT)(*FAIL)";
1380 }
1381  
1382 # Determine save behaviour...
1383 299 my $is_noncapturing = $savemode =~ /noncapturing|lookahead/;
1384 299 my $is_listifying = $savemode eq 'list';
1385  
1386 299 ▶ my $save_code =
Branch: 2 branches
BranchTrueFalseTotal
$is_noncapturing ? : 10 289 299
$is_listifying ? : 92 197 289
1387 $is_noncapturing?
1388 q{ @Regexp::Grammars::RESULT_STACK[0..@Regexp::Grammars::RESULT_STACK-2] }
1389 : $is_listifying?
1390 qq{ \@Regexp::Grammars::RESULT_STACK[0..\@Regexp::Grammars::RESULT_STACK-3],
1391 Regexp::Grammars::_pop_current_result_frame_with_list(
1392 \\\@Regexp::Grammars::RESULT_STACK, $alias, '$simple_subrule', \$^N
1393 ),
1394 }
1395 :
1396 qq{ \@Regexp::Grammars::RESULT_STACK[0..\@Regexp::Grammars::RESULT_STACK-3],
1397 Regexp::Grammars::_pop_current_result_frame(
1398 \\\@Regexp::Grammars::RESULT_STACK, $alias, '$simple_subrule', \$^N
1399 ),
1400 }
1401 ;
1402  
1403 # Report how construct was interpreted, if requested to...
1404 299 ▶ my $repeatedly = $REPETITION_DESCRIPTION_FOR{$postmodifier} // q{};
Condition100%2/2
$REPETITION_DESCRIPTION_FOR{$postmodifier} // ""
l!l
18281
MC/DC100%1/1
$REPETITION_DESCRIPTION_FOR{$postmodifier} // ""
$REPETITION_DESCRIPTION_FOR{$postmodifier}
Truth table100%2/2
A
A= $REPETITION_DESCRIPTION_FOR{$postmodifier}
Aresult
11
00
1405 299 ▶ my $results = $is_listifying && $postmodifier ? 'each match'
Branch: 2 branches
BranchTrueFalseTotal
$is_listifying && $postmodifier ? : 8 291 299
substr($postmodifier, 0, 1) eq '?' ? : 10 281 291
Condition100%3/3
$is_listifying and $postmodifier
!ll&&!rl&&r
207848
MC/DC100%2/2
$is_listifying and $postmodifier
$is_listifying $postmodifier
Truth table100%3/3
A and B
A= $is_listifyingB= $postmodifier
ABresult
0X0
100
111
1406 : substr($postmodifier,0,1) eq '?' ? 'any match'
1407 : 'the match'
1408 ;
1409 299 ▶ my $do_something_with = $savemode eq 'neglookahead' ? 'lookahead for anything except'
Branch: 2 branches
BranchTrueFalseTotal
$savemode eq 'neglookahead' ? : 1 298 299
$savemode eq 'poslookahead' ? : 1 297 298
1410 : $savemode eq 'poslookahead' ? 'lookahead for'
1411 : 'match'
1412 ;
1413 299 ▶ if ($debug_build) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug_build) 0 299 299
1414 0 ▶ _debug_notify( info =>
Branch: 3 branches
BranchTrueFalseTotal
defined $arg_desc ? : 0 0 0
$is_noncapturing ? : 0 0 0
$is_listifying ? : 0 0 0
1415 " |",
1416 " |...Treating $construct as:",
1417 " | | $do_something_with the subrule <$subrule> $repeatedly",
1418 (defined $arg_desc ? " | | passing the args: ($arg_desc)"
1419 : ()
1420 ),
1421 ( $is_noncapturing ? " | \\ but don't save anything"
1422 : $is_listifying ? " | \\ appending $results to \$MATCH{$alias}"
1423 : " | \\ saving $results in \$MATCH{$alias}"
1424 ),
1425 );
1426 }
1427  
1428 # Generate post-match result-capturing code, if match captures...
1429 299 my ($debug_pre, $debug_post)
1430 = _build_debugging_statements($debug_runtime, $construct);
1431  
1432 # Generate timeout test...
1433 299 ▶ my $timeout_test = $timeout ? q{(??{;Regexp::Grammars::_test_timeout()})} : q{};
Branch: 1 branch
BranchTrueFalseTotal
$timeout ? : 0 299 299
1434  
1435 # Translate to standard regex code...
1436 299 return qq{(?:$timeout_test(?{;
1437 local \@Regexp::Grammars::RESULT_STACK = (\@Regexp::Grammars::RESULT_STACK, {'\@'=>{$args}});
1438 $debug_pre})((?&$internal_subrule))(?{;
1439 local \@Regexp::Grammars::RESULT_STACK = (
1440 $save_code
1441 );$debug_post
1442 }))$postmodifier};
1443 }
1444  
1445 260 ▶sub _translate_rule_def {
Subroutine _translate_rule_def: called CC=4 SCAR=13.9
1446 260 my ($type, $qualifier, $name, $callname, $qualname, $body, $objectify, $local_ws, $nocontext)
1447 = @_;
1448 260 $qualname =~ s{::}{_88_}gxms;
1449  
1450 # Return object if requested...
1451 260 ▶ my $objectification =
Branch: 1 branch
BranchTrueFalseTotal
$objectify ? : 39 221 260
1452 $objectify ? qq{(??{; local \@Regexp::Grammars::RESULT_STACK = \@Regexp::Grammars::RESULT_STACK;
1453 \$Regexp::Grammars::RESULT_STACK[-1] = '$qualifier$name'->can('new')
1454 ? '$qualifier$name'->new(\$Regexp::Grammars::RESULT_STACK[-1])
1455 : bless \$Regexp::Grammars::RESULT_STACK[-1], '$qualifier$name';
1456 Regexp::Grammars::_debug_non_hash(\$Regexp::Grammars::RESULT_STACK[-1],'$name');
1457 })}
1458 : q{};
1459  
1460 # Each rule or token becomes a DEFINE'd Perl 5.10 named capture...
1461 260 ▶ my $implicit_version
Branch: 1 branch
BranchTrueFalseTotal
$callname eq 'ws' || $callname eq 'hk' ? : 6 254 260
Condition100%3/3
$callname eq "ws" or $callname eq "hk"
l!l&&r!l&&!r
15254
MC/DC100%2/2
$callname eq "ws" or $callname eq "hk"
$callname eq "ws" $callname eq "hk"
Truth table100%3/3
A or B
A= $callname eq "ws"B= $callname eq "hk"
ABresult
1X1
011
000
1462 = ($callname eq 'ws' || $callname eq 'hk')
1463 ? qq{(?<${callname}__implicit__> $body) }
1464 : qq{};
1465 260 return qq{
1466 (?(DEFINE) $local_ws
1467 (?<$qualname>
1468 (?<$callname>
1469 (?{\@{\$Regexp::Grammars::RESULT_STACK[-1]}{'!','~'}=(\$#{!}, $nocontext);})
1470 (?:$body) $objectification
1471 (?{;\$#{!}=delete(\$Regexp::Grammars::RESULT_STACK[-1]{'!'})//0;
1472 delete(\$Regexp::Grammars::RESULT_STACK[-1]{'\@'});
1473 })
1474 ))
1475 $implicit_version
1476 )
1477 };
1478 }
1479  
1480  
1481 # Locate any valid <...> sequences and replace with native regex code...
1482 389 ▶sub _translate_subrule_calls {
Subroutine _translate_subrule_calls: called CC=57 SCAR=49.8
1483 389 my ($source_file, $source_line,
1484 $grammar_name,
1485 $grammar_spec,
1486 $compiletime_debugging_requested,
1487 $runtime_debugging_requested,
1488 $timeout_requested,
1489 $pre_match_debug,
1490 $post_match_debug,
1491 $rule_name,
1492 $subrule_names_ref,
1493 $magic_ws,
1494 ) = @_;
1495  
1496 389 ▶ my $pretty_rule_name = $rule_name ? ($magic_ws ? '<rule' : '<token') . ": $rule_name>"
Branch: 2 branches
BranchTrueFalseTotal
$rule_name ? : 260 129 389
$magic_ws ? : 135 125 260
1497 : 'main regex (before first rule)';
1498  
1499 # Remember the preceding construct, so as to implement the +% etc. operators...
1500 389 my $prev_construct = q{};
1501 389 my $prev_translation = q{};
1502 389 my $curr_line_num = 1;
1503  
1504 # Translate all other calls (MAIN GRAMMAR FOR MODULE)...
1505 389 $grammar_spec =~ s{
1506 2357 (?{ $curr_line_num = substr($_, 0, pos) =~ tr/\n//; })
1507 (?<list_marker> (?<ws1> \s*+) (?<op> (?&SEPLIST_OP) ) (?<ws2> \s*+) )?
1508 (?<construct>
1509 (?<! \(\? )
1510 <
1511 (?:
1512 (?<self_subrule_scalar_nocap>
1513 \. \s* (?<subrule>(?&QUALIDENT)) \s* (?<args>(?&ARGLIST)) \s*
1514 )
1515 | (?<self_subrule_lookahead>
1516 (?<sign> \? | \! ) \s* (?<subrule>(?&QUALIDENT)) \s* (?<args>(?&ARGLIST)) \s*
1517 )
1518 | (?<self_subrule_scalar>
1519 \s* (?<subrule>(?&QUALIDENT)) \s* (?<args>(?&ARGLIST)) \s*
1520  
1521 )
1522 | (?<self_subrule_list>
1523 \[ \s* (?<subrule>(?&QUALIDENT)) \s* (?<args>(?&ARGLIST)) \s* \]
1524 )
1525 | (?<alias_subrule_scalar>
1526 (?<alias>(?&IDENT)) \s* = \s* (?<subrule>(?&QUALIDENT)) \s* (?<args>(?&ARGLIST)) \s*
1527  
1528 )
1529 | (?<alias_subrule_list>
1530 \[ (?<alias>(?&IDENT)) \s* = \s* (?<subrule>(?&QUALIDENT)) \s* (?<args>(?&ARGLIST)) \s* \]
1531 )
1532  
1533 | (?<self_argrule_scalar>
1534 \s* : (?<subrule>(?&QUALIDENT)) \s*
1535 )
1536 | (?<alias_argrule_scalar>
1537 (?<alias>(?&IDENT)) \s* = \s* : (?<subrule>(?&QUALIDENT)) \s*
1538 )
1539 | (?<alias_argrule_list>
1540 \[ (?<alias>(?&IDENT)) \s* = \s* : (?<subrule>(?&QUALIDENT)) \s* \]
1541 )
1542  
1543 | (?<alias_parens_scalar_nocap>
1544 \. (?<alias>(?&IDENT)) \s* = \s* (?<pattern>(?&PARENCODE)|(?&PARENS)|(?&LITERAL)) \s*
1545 )
1546 | (?<alias_parens_scalar>
1547 (?<alias>(?&IDENT)) \s* = \s* (?<pattern>(?&PARENCODE)|(?&PARENS)|(?&LITERAL)) \s*
1548 )
1549 | (?<alias_parens_list>
1550 \[ (?<alias>(?&IDENT)) \s* = \s* (?<pattern>(?&PARENCODE)|(?&PARENS)|(?&LITERAL)) \s* \]
1551 )
1552 | (?<alias_hash_scalar_nocap>
1553 (?<varname>(?&HASH)) \s* (?<keypat>(?&BRACES))? \s*
1554 )
1555 | (?<alias_hash_scalar>
1556 (?<alias>(?&IDENT)) \s* = \s* (?<varname>(?&HASH)) \s* (?<keypat>(?&BRACES))? \s*
1557 )
1558 | (?<alias_hash_list>
1559 \[ (?<alias>(?&IDENT)) \s* = \s* (?<varname>(?&HASH)) \s* (?<keypat>(?&BRACES))? \s* \]
1560 )
1561 | (?<backref>
1562 \s* (?<slash> \\ | /) (?<subrule> [:] (?&QUALIDENT)) \s*
1563 | \s* (?<slash> \\_ | /) (?<subrule> (?&QUALIDENT)) \s*
1564 )
1565 | (?<alias_backref>
1566 (?<alias>(?&IDENT)) \s* = \s* (?<slash> \\ | /) (?<subrule> [:] (?&QUALIDENT)) \s*
1567 | (?<alias>(?&IDENT)) \s* = \s* (?<slash> \\_ | /) (?<subrule> (?&QUALIDENT)) \s*
1568 )
1569 | (?<alias_backref_list>
1570 \[ (?<alias>(?&IDENT)) \s* = \s* (?<slash> \\ | /) (?<subrule> [:] (?&QUALIDENT)) \s* \]
1571 | \[ (?<alias>(?&IDENT)) \s* = \s* (?<slash> \\_ | /) (?<subrule> (?&QUALIDENT)) \s* \]
1572 )
1573 |
1574 (?<minimize_directive>
1575 minimize \s* : \s*
1576 )
1577 |
1578 (?<require_directive>
1579 require \s* : \s* (?<condition> (?&PARENCODE) ) \s*
1580 )
1581 |
1582 (?<debug_directive>
1583 debug \s* : \s* (?<cmd> run | match | step | try | off | on) \s*
1584 )
1585 |
1586 (?<timeout_directive>
1587 timeout \s* : \s* (?<timeout> \d+) \s*
1588 )
1589 |
1590 (?<context_directive>
1591 context \s* : \s*
1592 )
1593 |
1594 (?<nocontext_directive>
1595 nocontext \s* : \s*
1596 )
1597 |
1598 (?<yadaerror_directive>
1599 [.][.][.]
1600 | [!][!][!]
1601 | [?][?][?]
1602 )
1603 |
1604 (?<autoerror_directive>
1605 (?<error_type> error | fatal ) \s*+ : \s*+
1606 )
1607 |
1608 (?<error_directive>
1609 (?<error_type> log | error | warning | fatal )
1610 \s*+ : \s*+
1611 (?<msg> (?&PARENCODE) | .+? )
1612 \s*+
1613 )
1614 )
1615 > (?<modifier> \s* (?! (?&SEPLIST_OP) ) [?+*][?+]? | )
1616 |
1617 (?<ws_directive>
1618 $WS_PATTERN
1619 )
1620 |
1621 (?<complex_repetition>
1622 (?&SEPLIST_OP) \s* (?<complex_separator> \S* )
1623 )
1624 |
1625 (?<raw_regex>
1626 \(\?\<\w+\>
1627 )
1628 |
1629 (?<incomplete_request>
1630 < [^>\n]* [>\n]
1631 )
1632 |
1633 (?<loose_quantifier>
1634 (?<! \| ) \s++ (?&QUANTIFIER)
1635 | (?<! \A ) \s++ (?&QUANTIFIER)
1636 )
1637 |
1638 (?<reportable_raw_regex>
1639 (?: \\[^shv]
1640 | (?! (?&PARENCODE) ) (?&PARENS)
1641 | (?&CHARSET)
1642 | \w++
1643 | \|
1644 )
1645 (?&QUANTIFIER)?
1646 )
1647 |
1648 (?<raw_regex>
1649 \s++
1650 | \\. (?&QUANTIFIER)?
1651 | \(\?!
1652 | \(\?\# [^)]* \) # (?# -> old style inline comment)
1653 | (?&PARENCODE)
1654 | \# [^\n]*+
1655 | [^][\s()<>#\\]++
1656 )
1657 )
1658  
1659 (?(DEFINE)
1660 (?<SEPLIST_OP> \*\* | [*+?] [+?]?+ \s* %%?+ | \{ \d+(,\d*)? \} [+?]?+ \s* %%?+ )
1661 (?<PARENS> \( (?:[?] (?: <[=!] | [:>] ))?
1662 (?: \\. | (?&PARENCODE) | (?&PARENS) | (?&CHARSET) | [^][()\\<>]++ )*+
1663 \)
1664 )
1665 (?<BRACES> \{ (?: \\. | (?&BRACES) | [^{}\\]++ )*+ \} )
1666 (?<PARENCODE> \(\?\??\{ (?: \\. | (?&BRACES) | [^{}\\]++ )*+ \}\) )
1667 (?<HASH> \% (?&IDENT) (?: :: (?&IDENT) )* )
1668 (?<CHARSET> \[ \^?+ \]?+ (?: \[:\w+:\] | \\. | [^]] )*+ \] )
1669 (?<IDENT> [^\W\d]\w*+ )
1670 (?<QUALIDENT> (?: [^\W\d]\w*+ :: )* [^\W\d]\w*+ )
1671 (?<LITERAL> (?&NUMBER) | (?&STRING) | (?&VAR) )
1672 (?<NUMBER> [+-]? \d++ (?:\. \d++)? (?:[eE] [+-]? \d++)? )
1673 (?<STRING> ' [^\\']++ (?: \\. [^\\']++ )* ' )
1674 (?<ARGLIST> (?&PARENCODE) | \( \s* (?&ARGS)? \s* \) | (?# NOTHING ) )
1675 (?<ARGS> (?&ARG) \s* (?: , \s* (?&ARG) \s* )* ,? )
1676 (?<ARG> (?&VAR) | (?&KEY) \s* => \s* (?&LITERAL) )
1677 (?<VAR> : (?&IDENT) )
1678 (?<KEY> (?&IDENT) | (?&LITERAL) )
1679 (?<QUANTIFIER> [*+?][+?]? | \{ \d+,?\d* \} [+?]? )
1680 )
1681 }{
1682 2275 my $curr_construct = $+{construct};
1683 2275 ▶ my $list_marker = $+{list_marker} // q{};
Condition100%2/2
$+{"list_marker"} // ""
l!l
732202
MC/DC100%1/1
$+{"list_marker"} // ""
$+{"list_marker"}
Truth table100%2/2
A
A= $+{"list_marker"}
Aresult
11
00
1684 2275 ▶ my $alias = ($+{alias}//'MATCH') eq 'MATCH' ? q{'='} : qq{'$+{alias}'};
Branch: 1 branch
BranchTrueFalseTotal
($+{'alias'} // 'MATCH') eq 'MATCH' ? : 2138 137 2275
Condition100%2/2
$+{"alias"} // "MATCH"
l!l
1662109
MC/DC100%1/1
$+{"alias"} // "MATCH"
$+{"alias"}
Truth table100%2/2
A
A= $+{"alias"}
Aresult
11
00
1685  
1686 # Determine and remember the necessary translation...
1687 2275 my $curr_translation = do{
1688  
1689 # Translate subrule calls of the form: <ALIAS=(...)>...
1690 2275 ▶ if (defined $+{alias_parens_scalar}) {
Branch: 31 branches
BranchTrueFalseTotal
if (defined $+{'alias_parens_scalar'}) { } 84 2191 2275
elsif (defined $+{'alias_parens_scalar_nocap'}) { } 0 2191 2191
elsif (defined $+{'alias_parens_list'}) { } 16 2175 2191
elsif (defined $+{'alias_hash_scalar'}) { } 7 2168 2175
elsif (defined $+{'alias_hash_scalar_nocap'}) { } 4 2164 2168
elsif (defined $+{'alias_hash_list'}) { } 3 2161 2164
elsif (defined $+{'alias_subrule_scalar'}) { } 22 2139 2161
elsif (defined $+{'alias_subrule_list'}) { } 30 2109 2139
elsif (defined $+{'self_subrule_lookahead'}) { } 2 2107 2109
elsif (defined $+{'self_subrule_scalar_nocap'}) { } 8 2099 2107
elsif (defined $+{'self_subrule_scalar'}) { } 175 1924 2099
elsif (defined $+{'self_subrule_list'}) { } 62 1862 1924
elsif (defined $+{'alias_argrule_scalar'}) { } 0 1862 1862
elsif (defined $+{'alias_argrule_list'}) { } 0 1862 1862
elsif (defined $+{'self_argrule_scalar'}) { } 1 1861 1862
elsif (defined $+{'backref'} or $+{'alias_backref'} or $+{'alias_backref_list'}) { } 9 1852 1861
elsif (defined $+{'reportable_raw_regex'}) { } 481 1371 1852
elsif (defined $+{'complex_repetition'}) { } 0 1371 1371
elsif (defined $+{'raw_regex'}) { } 1345 26 1371
elsif (defined $+{'require_directive'}) { } 0 26 26
elsif (defined $+{'minimize_directive'}) { } 3 23 26
elsif (defined $+{'debug_directive'}) { } 0 23 23
elsif (defined $+{'timeout_directive'}) { } 0 23 23
elsif (defined $+{'error_directive'}) { } 8 15 23
elsif (defined $+{'autoerror_directive'}) { } 7 8 15
elsif (defined $+{'yadaerror_directive'}) { } 4 4 8
elsif (defined $+{'context_directive'}) { } 0 4 4
elsif (defined $+{'nocontext_directive'}) { } 0 4 4
elsif (defined $+{'ws_directive'}) { } 4 0 4
elsif (defined $+{'incomplete_request'}) { } 0 0 0
elsif (defined $+{'loose_quantifier'}) { } 0 0 0
1691 84 ▶ my $pattern = substr($+{pattern},0,1) eq '(' ? $+{pattern} : "(?{$+{pattern}})";
Branch: 1 branch
BranchTrueFalseTotal
substr($+{'pattern'}, 0, 1) eq '(' ? : 80 4 84
1692 _translate_subpattern(
1693 $curr_construct, $alias, $pattern, 'scalar', $+{modifier},
1694 84 $compiletime_debugging_requested,
1695 $runtime_debugging_requested, $timeout_requested,
1696 );
1697 }
1698 elsif (defined $+{alias_parens_scalar_nocap}) {
1699 0 ▶ my $pattern = substr($+{pattern},0,1) eq '(' ? $+{pattern} : "(?{$+{pattern}})";
Branch: 1 branch
BranchTrueFalseTotal
substr($+{'pattern'}, 0, 1) eq '(' ? : 0 0 0
1700 _translate_subpattern(
1701 $curr_construct, $alias, $pattern, 'noncapturing', $+{modifier},
1702 0 $compiletime_debugging_requested,
1703 $runtime_debugging_requested, $timeout_requested,
1704 );
1705 }
1706 elsif (defined $+{alias_parens_list}) {
1707 16 ▶ my $pattern = substr($+{pattern},0,1) eq '(' ? $+{pattern} : "(?{$+{pattern}})";
Branch: 1 branch
BranchTrueFalseTotal
substr($+{'pattern'}, 0, 1) eq '(' ? : 16 0 16
1708 _translate_subpattern(
1709 $curr_construct, $alias, $pattern, 'list', $+{modifier},
1710 16 $compiletime_debugging_requested,
1711 $runtime_debugging_requested, $timeout_requested,
1712 );
1713 }
1714  
1715 # Translate subrule calls of the form: <ALIAS=%HASH>...
1716 elsif (defined $+{alias_hash_scalar}) {
1717 _translate_hashmatch(
1718 $curr_construct, $alias, $+{varname}, $+{keypat}, 'scalar', $+{modifier},
1719 7 $compiletime_debugging_requested,
1720 $runtime_debugging_requested,
1721 $timeout_requested,
1722 );
1723 }
1724 elsif (defined $+{alias_hash_scalar_nocap}) {
1725 _translate_hashmatch(
1726 $curr_construct, $alias, $+{varname}, $+{keypat}, 'noncapturing', $+{modifier},
1727 4 $compiletime_debugging_requested,
1728 $runtime_debugging_requested,
1729 $timeout_requested,
1730 );
1731 }
1732 elsif (defined $+{alias_hash_list}) {
1733 _translate_hashmatch(
1734 $curr_construct, $alias, $+{varname}, $+{keypat}, 'list', $+{modifier},
1735 3 $compiletime_debugging_requested,
1736 $runtime_debugging_requested,
1737 $timeout_requested,
1738 );
1739 }
1740  
1741 # Translate subrule calls of the form: <ALIAS=RULENAME>...
1742 elsif (defined $+{alias_subrule_scalar}) {
1743 _translate_subrule_call(
1744 $source_line, $source_file,
1745 $pretty_rule_name,
1746 $grammar_name,
1747 $curr_construct, $alias, $+{subrule}, $+{args}, 'scalar', $+{modifier},
1748 22 $compiletime_debugging_requested,
1749 $runtime_debugging_requested,
1750 $timeout_requested,
1751 $subrule_names_ref,
1752 );
1753 }
1754 elsif (defined $+{alias_subrule_list}) {
1755 _translate_subrule_call(
1756 $source_line, $source_file,
1757 $pretty_rule_name,
1758 $grammar_name,
1759 $curr_construct, $alias, $+{subrule}, $+{args}, 'list', $+{modifier},
1760 30 $compiletime_debugging_requested,
1761 $runtime_debugging_requested,
1762 $timeout_requested,
1763 $subrule_names_ref,
1764 );
1765 }
1766  
1767 # Translate subrule calls of the form: <?RULENAME> and <!RULENAME>...
1768 elsif (defined $+{self_subrule_lookahead}) {
1769  
1770 # Determine type of lookahead, and work around capture problem...
1771 2 my ($type, $pre, $post) = ( 'neglookahead', '(?!(?!)|', ')' );
1772 2 ▶ if ($+{sign} eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($+{"sign"} eq "?") 1 1 2
1773 1 $type = 'poslookahead';
1774 1 $pre x= 2;
1775 1 $post x= 2;
1776 }
1777  
1778 $pre . _translate_subrule_call(
1779 $source_line, $source_file,
1780 $pretty_rule_name,
1781 $grammar_name,
1782 2 $curr_construct, qq{'$+{subrule}'}, $+{subrule}, $+{args}, $type, q{},
1783 $compiletime_debugging_requested,
1784 $runtime_debugging_requested,
1785 $timeout_requested,
1786 $subrule_names_ref,
1787 )
1788 . $post;
1789 }
1790 elsif (defined $+{self_subrule_scalar_nocap}) {
1791 _translate_subrule_call(
1792 $source_line, $source_file,
1793 $pretty_rule_name,
1794 $grammar_name,
1795 $curr_construct, qq{'$+{subrule}'}, $+{subrule}, $+{args}, 'noncapturing', $+{modifier},
1796 8 $compiletime_debugging_requested,
1797 $runtime_debugging_requested,
1798 $timeout_requested,
1799 $subrule_names_ref,
1800 );
1801 }
1802 elsif (defined $+{self_subrule_scalar}) {
1803 _translate_subrule_call(
1804 $source_line, $source_file,
1805 $pretty_rule_name,
1806 $grammar_name,
1807 $curr_construct, qq{'$+{subrule}'}, $+{subrule}, $+{args}, 'scalar', $+{modifier},
1808 175 $compiletime_debugging_requested,
1809 $runtime_debugging_requested,
1810 $timeout_requested,
1811 $subrule_names_ref,
1812 );
1813 }
1814 elsif (defined $+{self_subrule_list}) {
1815 _translate_subrule_call(
1816 $source_line, $source_file,
1817 $pretty_rule_name,
1818 $grammar_name,
1819 $curr_construct, qq{'$+{subrule}'}, $+{subrule}, $+{args}, 'list', $+{modifier},
1820 62 $compiletime_debugging_requested,
1821 $runtime_debugging_requested,
1822 $timeout_requested,
1823 $subrule_names_ref,
1824 );
1825 }
1826  
1827 # Translate subrule calls of the form: <ALIAS=:ARGNAME>...
1828 elsif (defined $+{alias_argrule_scalar}) {
1829 0 my $pattern = qq{(??{;\$Regexp::Grammars::RESULT_STACK[-1]{'\@'}{'$+{subrule}'} // '(?!)'})};
1830 _translate_subpattern(
1831 $curr_construct, $alias, $pattern, 'scalar', $+{modifier},
1832 0 $compiletime_debugging_requested, $runtime_debugging_requested, $timeout_requested,
1833 "in \$ARG{'$+{subrule}'}"
1834 );
1835 }
1836 elsif (defined $+{alias_argrule_list}) {
1837 0 my $pattern = qq{(??{;\$Regexp::Grammars::RESULT_STACK[-1]{'\@'}{'$+{subrule}'} // '(?!)'})};
1838 _translate_subpattern(
1839 $curr_construct, $alias, $pattern, 'list', $+{modifier},
1840 0 $compiletime_debugging_requested, $runtime_debugging_requested, $timeout_requested,
1841 "in \$ARG{'$+{subrule}'}"
1842 );
1843 }
1844  
1845 # Translate subrule calls of the form: <:ARGNAME>...
1846 elsif (defined $+{self_argrule_scalar}) {
1847 1 my $pattern = qq{(??{;\$Regexp::Grammars::RESULT_STACK[-1]{'\@'}{'$+{subrule}'} // '(?!)'})};
1848 _translate_subpattern(
1849 $curr_construct, qq{'$+{subrule}'}, $pattern, 'noncapturing', $+{modifier},
1850 1 $compiletime_debugging_requested, $runtime_debugging_requested, $timeout_requested,
1851 "in \$ARG{'$+{subrule}'}"
1852 );
1853 }
1854  
1855 # Translate subrule calls of the form: <\IDENT> or </IDENT>...
1856 ▶ elsif (defined $+{backref} || $+{alias_backref} || $+{alias_backref_list}) {
Condition100%6/6
defined $+{"backref"} or $+{"alias_backref"} or $+{"alias_backref_list"}
l!l&&r!l&&!r
721852
defined $+{"backref"} or $+{"alias_backref"}
l!l&&r!l&&!r
521854
MC/DC100%3/3
defined $+{"backref"} or $+{"alias_backref"} or $+{"alias_backref_list"}
defined $+{"backref"} $+{"alias_backref"} $+{"alias_backref_list"}
Truth table100%4/4
A or B or C
A= defined $+{"backref"}B= $+{"alias_backref"}C= $+{"alias_backref_list"}
ABCresult
1XX1
01X1
0011
0000
1857 # Use "%ARGS" if subrule names starts with a colon...
1858 9 my $subrule = $+{subrule};
1859 9 ▶ if (substr($subrule,0,1) eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($subrule, 0, 1) eq ":") 3 6 9
1860 3 substr($subrule,0,1,"\@'}{'");
1861 }
1862  
1863 9 my $backref = qq{\$Regexp::Grammars::RESULT_STACK[-1]{'$subrule'}};
1864 9 ▶ my $quoter = $+{slash} eq '\\' || $+{slash} eq '\\_'
Branch: 1 branch
BranchTrueFalseTotal
$+{'slash'} eq '\\' || $+{'slash'} eq '\\_' ? : 5 4 9
Condition100%3/3
$+{"slash"} eq "\\" or $+{"slash"} eq "\\_"
l!l&&r!l&&!r
234
MC/DC100%2/2
$+{"slash"} eq "\\" or $+{"slash"} eq "\\_"
$+{"slash"} eq "\\" $+{"slash"} eq "\\_"
Truth table100%3/3
A or B
A= $+{"slash"} eq "\\"B= $+{"slash"} eq "\\_"
ABresult
1X1
011
000
1865 ? "quotemeta($backref)"
1866 : "Regexp::Grammars::_invert_delim($backref)"
1867 ;
1868 9 my $pattern = qq{ (??{ defined $backref ? $quoter : q{(?!)}})};
1869 my $type = $+{backref} ? 'noncapturing'
1870 9 ▶ : $+{alias_backref} ? 'scalar'
Branch: 2 branches
BranchTrueFalseTotal
$+{'backref'} ? : 5 4 9
$+{'alias_backref'} ? : 2 2 4
1871 : 'list'
1872 ;
1873 _translate_subpattern(
1874 $curr_construct, $alias, $pattern, $type, $+{modifier},
1875 9 $compiletime_debugging_requested, $runtime_debugging_requested, $timeout_requested,
1876 "in \$MATCH{'$subrule'}"
1877 );
1878 }
1879  
1880 # Translate reportable raw regexes (add debugging support)...
1881 elsif (defined $+{reportable_raw_regex}) {
1882 _translate_raw_regex(
1883 481 $+{reportable_raw_regex}, $compiletime_debugging_requested, $runtime_debugging_requested
1884 );
1885 }
1886  
1887 # Translate too-complex repetition specifications...
1888 elsif (defined $+{complex_repetition}) {
1889 0 my ($repetition, $separator) = @+{'complex_repetition', 'complex_separator'};
1890 0 my ($metaop) = $repetition =~ m{(%%?)};
1891 0 my $quotedop = quotemeta($metaop);
1892 0 $separator =~ s/\s+/ /g;
1893 0 ▶ my $problem = $separator =~ /\S/
Branch: 1 branch
BranchTrueFalseTotal
$separator =~ /\S/ ? : 0 0 0
1894 ? ["The $separator... separator you specified after the $metaop is too complex",
1895 "(Try refactoring it into a single subrule call)",
1896 ]
1897 : ["No separator was specified after the $metaop",
1898 "(Or did you need a $quotedop instead, to match a literal '$metaop'?)",
1899 ];
1900 _debug_notify( fatal =>
1901 "Invalid separation specifier: $metaop",
1902 "at line $curr_line_num of $pretty_rule_name",
1903 0 @{$problem},
1904 );
1905 0 exit(1);
1906 }
1907  
1908 # Translate non-reportable raw regexes (leave as is)...
1909 elsif (defined $+{raw_regex}) {
1910 # Handle raw % and %%
1911 1345 my $raw_regex = $+{raw_regex};
1912 1345 ▶ if ($raw_regex =~ / \A %%?+ /x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_regex =~ / \A %%?+ /x) 0 1345 1345
1913 0 _debug_notify( fatal =>
1914 "Invalid separation specifier: $&",
1915 "at line $curr_line_num of $pretty_rule_name",
1916 "(Did you forget to put a repetition quantifier before the $&",
1917 " or did you need a " . quotemeta($&) . " instead, to match a literal '$&'?)",
1918 );
1919 0 exit(1);
1920 }
1921  
1922 # Handle any other raw regex...
1923 _translate_raw_regex(
1924 1345 $raw_regex, $compiletime_debugging_requested
1925 );
1926 }
1927  
1928 # Translate directives...
1929 elsif (defined $+{require_directive}) {
1930 _translate_require_directive(
1931 0 $curr_construct, $+{condition}, $compiletime_debugging_requested
1932 );
1933 }
1934 elsif (defined $+{minimize_directive}) {
1935 _translate_minimize_directive(
1936 3 $curr_construct, $+{condition}, $compiletime_debugging_requested
1937 );
1938 }
1939 elsif (defined $+{debug_directive}) {
1940 _translate_debug_directive(
1941 0 $curr_construct, $+{cmd}, $compiletime_debugging_requested
1942 );
1943 }
1944 elsif (defined $+{timeout_directive}) {
1945 _translate_timeout_directive(
1946 0 $curr_construct, $+{timeout}, $compiletime_debugging_requested
1947 );
1948 }
1949 elsif (defined $+{error_directive}) {
1950 _translate_error_directive(
1951 $curr_construct, $+{error_type}, $+{msg},
1952 8 $compiletime_debugging_requested, $rule_name
1953 );
1954 }
1955 elsif (defined $+{autoerror_directive}) {
1956 _translate_error_directive(
1957 7 $curr_construct, $+{error_type}, q{},
1958 $compiletime_debugging_requested, $rule_name
1959 );
1960 }
1961 elsif (defined $+{yadaerror_directive}) {
1962 _translate_error_directive(
1963 $curr_construct,
1964 4 ▶ ($+{yadaerror_directive} eq '???' ? 'warning' : 'error'),
Branch: 1 branch
BranchTrueFalseTotal
$+{'yadaerror_directive'} eq '???' ? : 0 4 4
1965 q{},
1966 $compiletime_debugging_requested, -$rule_name
1967 );
1968 }
1969 elsif (defined $+{context_directive}) {
1970 0 ▶ if ($compiletime_debugging_requested) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compiletime_debugging_requested) 0 0 0
1971 0 _debug_notify( info => " |",
1972 " |...Treating $curr_construct as:",
1973 " | \\ Turn on context-saving for the current rule"
1974 );
1975 }
1976 0 q{}; # Remove the directive
1977 }
1978 elsif (defined $+{nocontext_directive}) {
1979 0 ▶ if ($compiletime_debugging_requested) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compiletime_debugging_requested) 0 0 0
1980 0 _debug_notify( info => " |",
1981 " |...Treating $curr_construct as:",
1982 " | \\ Turn off context-saving for the current rule"
1983 );
1984 }
1985 0 q{}; # Remove the directive
1986 }
1987 elsif (defined $+{ws_directive}) {
1988 4 ▶ if ($compiletime_debugging_requested) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compiletime_debugging_requested) 0 4 4
1989 0 _debug_notify( info => " |",
1990 " |...Treating $curr_construct as:",
1991 " | \\ Change whitespace matching for the current rule"
1992 );
1993 }
1994 4 $curr_construct;
1995 }
1996  
1997 # Something that looks like a rule call or directive, but isn't...
1998 elsif (defined $+{incomplete_request}) {
1999 0 my $request = $+{incomplete_request};
2000 0 $request =~ s/\n//g;
2001 0 ▶ if ($request =~ /\A\s*<\s*\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request =~ /\A\s*<\s*\Z/) { } 0 0 0
2002 0 _debug_notify( fatal =>
2003 qq{Invalid < metacharacter near line $curr_line_num of $pretty_rule_name},
2004 qq{If you meant to match a literal '<', use: \\<},
2005 );
2006 }
2007 else {
2008 0 _debug_notify( fatal =>
2009 qq{Possible failed attempt to specify},
2010 qq{a subrule call or directive: $request},
2011 qq{near line $curr_line_num of $pretty_rule_name},
2012 qq{If you meant to match literally, use: \\$request},
2013 );
2014 }
2015 0 exit(1);
2016 }
2017  
2018 # A quantifier that isn't quantifying anything...
2019 elsif (defined $+{loose_quantifier}) {
2020 0 my $quant = $+{loose_quantifier};
2021 0 $quant =~ s{^\s+}{};
2022 0 my $literal = quotemeta($quant);
2023 0 _debug_notify( fatal =>
2024 qq{Quantifier that doesn't quantify anything: $quant},
2025 qq{at line $curr_line_num in declaration of $pretty_rule_name},
2026 qq{(Did you mean to match literally? If so, try: $literal)},
2027 q{},
2028 );
2029 0 exit(1);
2030 }
2031  
2032 # There shouldn't be any other possibility...
2033 else {
2034 0 die qq{Internal error: this shouldn't happen!\n},
2035 qq{Near '$curr_construct' at $curr_line_num of $pretty_rule_name\n};
2036 }
2037 };
2038  
2039 # Handle the **/*%/*%%/+%/{n,m}%/etc operators...
2040 2275 ▶ if ($list_marker) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_marker) 73 2202 2275
2041 73 ▶ my $ws = $magic_ws ? $+{ws1} . $+{ws2} : q{};
Branch: 1 branch
BranchTrueFalseTotal
$magic_ws ? : 70 3 73
2042 73 my $op = $+{op};
2043  
2044 73 $curr_translation = _translate_separated_list(
2045 $prev_construct, $op, $curr_construct,
2046 $prev_translation, $curr_translation, $ws,
2047 $compiletime_debugging_requested,
2048 $runtime_debugging_requested, $timeout_requested,
2049 );
2050 73 $curr_construct = qq{$prev_construct $op $curr_construct};
2051 }
2052  
2053 # Finally, remember this latest translation, and return it...
2054 2275 $prev_construct = $curr_construct;
2055 2275 $prev_translation = $curr_translation;;
2056 }exmsg;
2057  
2058 # Translate magic hash accesses...
2059 389 $grammar_spec =~ s{\$(?:\:\:)?MATCH (?= \s*\{) }
2060 {\$Regexp::Grammars::RESULT_STACK[-1]}xmsg;
2061 389 $grammar_spec =~ s{\$(?:\:\:)?ARG (?= \s*\{) }
2062 {\$Regexp::Grammars::RESULT_STACK[-1]{'\@'}}xmsg;
2063  
2064 # Translate magic scalars and hashes...
2065 389 state $translate_scalar = {
2066 q{%$MATCH} => q{%{$Regexp::Grammars::RESULT_STACK[-1]{q{=}}}},
2067 q{@$MATCH} => q{@{$Regexp::Grammars::RESULT_STACK[-1]{q{=}}}},
2068 q{$MATCH} => q{$Regexp::Grammars::RESULT_STACK[-1]{q{=}}},
2069 q{%MATCH} => q{%{$Regexp::Grammars::RESULT_STACK[-1]}},
2070 q{$CAPTURE} => q{$^N},
2071 q{$CONTEXT} => q{$^N},
2072 q{$DEBUG} => q{$Regexp::Grammars::DEBUG},
2073 q{$INDEX} => q{${\\pos()}},
2074 q{%ARG} => q{%{$Regexp::Grammars::RESULT_STACK[-1]{'@'}}},
2075  
2076 q{%$::MATCH} => q{%{$Regexp::Grammars::RESULT_STACK[-1]{q{=}}}},
2077 q{@$::MATCH} => q{@{$Regexp::Grammars::RESULT_STACK[-1]{q{=}}}},
2078 q{$::MATCH} => q{$Regexp::Grammars::RESULT_STACK[-1]{q{=}}},
2079 q{%::MATCH} => q{%{$Regexp::Grammars::RESULT_STACK[-1]}},
2080 q{$::CAPTURE} => q{$^N},
2081 q{$::CONTEXT} => q{$^N},
2082 q{$::DEBUG} => q{$Regexp::Grammars::DEBUG},
2083 q{$::INDEX} => q{${\\pos()}},
2084 q{%::ARG} => q{%{$Regexp::Grammars::RESULT_STACK[-1]{'@'}}},
2085  
2086 };
2087 state $translatable_scalar
2088 1440 = join '|', map {quotemeta $_}
2089 4370 sort {length $b <=> length $a}
2090 389 keys %{$translate_scalar};
2091  
2092 389 $grammar_spec =~ s{ ($translatable_scalar) (?! \s* (?: \[ | \{) ) }
2093 {$translate_scalar->{$1}}oxmsg;
2094  
2095 389 return $grammar_spec;
2096 }
2097  
2098 # Generate a "decimal timestamp" and insert in a template...
2099 0 ▶sub _timestamp {
Subroutine _timestamp: not called CC=2 SCAR=17.9
2100 0 my ($template) = @_;
2101  
2102 # Generate and insert any timestamp...
2103 0 ▶ if ($template =~ /%t/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template =~ /%t/) 0 0 0
2104 0 my ($sec, $min, $hour, $day, $mon, $year) = localtime;
2105 0 $mon++; $year+=1900;
2106 0 my $timestamp = sprintf("%04d%02d%02d.%02d%02d%02d",
2107 $year, $mon, $day, $hour, $min, $sec);
2108 0 $template =~ s{%t}{$timestamp}xms;;
2109 }
2110  
2111 0 return $template;
2112 }
2113  
2114 # Open (or re-open) the requested log file...
2115 0 ▶sub _autoflush {
Subroutine _autoflush: not called CC=1 SCAR=6.9
2116 0 my ($fh) = @_;
2117 0 my $originally_selected = select $fh;
2118 0 $|=1;
2119 0 select $originally_selected;
2120 }
2121  
2122 3778 ▶sub _open_log {
Subroutine _open_log: called CC=5 SCAR=24.7
2123 3778 my ($mode, $filename, $from_where) = @_;
2124 3778 ▶ $from_where //= q{};
Condition50%1/2
$from_where //= ""
l!l
37780
MC/DC0%0/1
$from_where //= ""
$from_where
Truth table50%1/2
A
A= $from_where
Aresult
11
00
2125  
2126 # Special case: '-' --> STDERR
2127 3778 ▶ if ($filename eq q{-}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($filename eq '-') { } 3778 0 3778
elsif (open my $fh, $mode, $filename) { } 0 0 0
2128 3778 return *STDERR{IO};
2129 }
2130 # Otherwise, just open the named file...
2131 elsif (open my $fh, $mode, $filename) {
2132 0 _autoflush($fh);
2133 0 return $fh;
2134 }
2135 # Otherwise, generate a warning and default to STDERR...
2136 else {
2137 0 local *Regexp::Grammars::LOGFILE = *STDERR{IO};
2138 0 ▶ _debug_notify( warn =>
Branch: 1 branch
BranchTrueFalseTotal
$from_where ? : 0 0 0
2139 qq{Unable to open log file '$filename'},
2140 ($from_where ? $from_where : ()),
2141 qq{($!)},
2142 qq{Defaulting to STDERR instead.},
2143 q{},
2144 );
2145 0 return *STDERR{IO};
2146 }
2147 }
2148  
2149 1087 ▶sub _invert_delim {
Subroutine _invert_delim: called CC=1 SCAR=0.0
2150 1087 my ($delim) = @_;
2151 1087 $delim = reverse $delim;
2152 1087 $delim =~ tr/<>[]{}()??`'/><][}{)(??'`/;
2153 1087 return quotemeta $delim;
2154 }
2155  
2156 # Regex to detect if other regexes contain a grammar specification...
2157 82 my $GRAMMAR_DIRECTIVE
2158 = qr{ < grammar: \s* (?<grammar_name> $QUALIDENT ) \s* > }xms;
2159  
2160 # Regex to detect if other regexes contain a grammar inheritance...
2161 82 my $EXTENDS_DIRECTIVE
2162 = qr{ < extends: \s* (?<base_grammar_name> $QUALIDENT ) \s* > }xms;
2163  
2164 # Cache of rule/token names within defined grammars...
2165 82 my %subrule_names_for;
2166  
2167 # Build list of ancestors for a given grammar...
2168 434 ▶sub _ancestry_of {
Subroutine _ancestry_of: called CC=2 SCAR=7.0
2169 434 my ($grammar_name) = @_;
2170  
2171 434 ▶ return () if !$grammar_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $grammar_name 0 434 434
2172  
2173 82 ▶ use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
2174 434 return map { substr($_, $CACHE_LEN) } @{mro::get_linear_isa($CACHE.$grammar_name, 'c3')};
2175 }
2176  
2177 # Detect and translate any requested grammar inheritances...
2178 135 ▶sub _extract_inheritances {
Subroutine _extract_inheritances: called CC=6 SCAR=18.1
2179 135 my ($source_line, $source_file, $regex, $compiletime_debugging_requested, $derived_grammar_name) = @_;
2180  
2181  
2182 # Detect and remove inheritance requests...
2183 135 ▶ while ($regex =~ s{$EXTENDS_DIRECTIVE}{}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($regex =~ s/$EXTENDS_DIRECTIVE//msx) 13 135 148
2184 # Normalize grammar name and report...
2185 13 my $orig_grammar_name = $+{base_grammar_name};
2186 13 my $grammar_name = $orig_grammar_name;
2187 13 ▶ if ($grammar_name !~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($grammar_name =~ /::/) 4 9 13
2188 4 $grammar_name = caller(2).'::'.$grammar_name;
2189 }
2190  
2191 13 ▶ if (exists $user_defined_grammar{$grammar_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $user_defined_grammar{$grammar_name}) { } 13 0 13
2192 13 ▶ if ($compiletime_debugging_requested) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compiletime_debugging_requested) 0 13 13
2193 0 _debug_notify( info =>
2194 "Processing inheritance request for $grammar_name...",
2195 q{},
2196 );
2197 }
2198  
2199 # Specify new relationship...
2200 82 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
2201 13 push @{$CACHE.$derived_grammar_name.'::ISA'}, $CACHE.$grammar_name;
2202 }
2203 else {
2204 0 _debug_notify( fatal =>
2205 "Inheritance from unknown grammar requested",
2206 "by <extends: $grammar_name> directive",
2207 "in regex grammar declared at $source_file line $source_line",
2208 q{},
2209 );
2210 0 exit(1);
2211 }
2212 }
2213  
2214 # Retrieve ancestors (but not self) in C3 dispatch order...
2215 135 my (undef, @ancestors) = _ancestry_of($derived_grammar_name);
2216  
2217 # Extract subrule names and implementations for ancestors...
2218 135 my %subrule_names = map { %{$subrule_names_for{$_}} } @ancestors;
2219 135 $_ = -1 for values %subrule_names;
2220 my $implementation
2221 135 = join "\n", map { $user_defined_grammar{$_} } @ancestors;
2222  
2223 135 return $implementation, \%subrule_names;
2224 }
2225  
2226 # Transform grammar-augmented regex into pure Perl 5.10 regex...
2227 139 ▶sub _build_grammar {
Subroutine _build_grammar: called CC=35 SCAR=39.1
2228 139 my ($grammar_spec) = @_;
2229 139 $grammar_spec .= q{};
2230  
2231 # Check for lack of Regexp::Grammar-y constructs and short-circuit...
2232 139 ▶ if ($grammar_spec !~ m{ < (?: [.?![:%\\/]? [^\W\d]\w* [^>]* | [.?!]{3} ) > }xms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($grammar_spec =~ m( < (?: [.?![:%\\/]? [^\W\d]\w* [^>]* | [.?!]{3} ) > )msx) 4 135 139
2233 4 return $grammar_spec;
2234 }
2235  
2236 # Remember where we parked...
2237 135 my ($source_file, $source_line) = (caller 1)[1,2];
2238 135 $source_line -= $grammar_spec =~ tr/\n//;
2239  
2240 # Check for dubious repeated <SUBRULE> constructs that throw away captures...
2241 135 my $dubious_line = $source_line;
2242 135 ▶ while ($grammar_spec =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if ($grammar_spec =~ / (.*?) ( < (?! \[ ) # not <[SUBRULE]> ( $IDENT (?: = [^>]*)? ) # but <SUBRULE> or <SUBRULE=*> > \s* ( # followed by a quantifier... [+*][?+]? # either symbolic | \{\d+(?:,\d*)?\}[?+]? # or numeric ) ) /gmsx) 0 135 135
2243 (.*?)
2244 (
2245 < (?! \[ ) # not <[SUBRULE]>
2246 ( $IDENT (?: = [^>]*)? ) # but <SUBRULE> or <SUBRULE=*>
2247 > \s*
2248 ( # followed by a quantifier...
2249 [+*][?+]? # either symbolic
2250 | \{\d+(?:,\d*)?\}[?+]? # or numeric
2251 )
2252 )
2253 }gxms) {
2254 0 my ($prefix, $match, $rule, $qual) = ($1, $2, $3, $4);
2255 0 $dubious_line += $prefix =~ tr/\n//;
2256 0 _debug_notify( warn =>
2257 qq{Repeated subrule <$rule>$qual},
2258 qq{at $source_file line $dubious_line},
2259 qq{will only capture its final match},
2260 qq{(Did you mean <[$rule]>$qual instead?)},
2261 q{},
2262 );
2263 0 $dubious_line += $match =~ tr/\n//;
2264 }
2265  
2266 # Check for dubious non-backtracking <SUBRULE> constructs...
2267 135 $dubious_line = $source_line;
2268 135 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($grammar_spec =~ / (.*?) ( < (?! (?:obj)? (?:rule: | token ) ) ( [^>]+ ) > \s* ( [?+*][+] | \{.*\}[+] ) ) /gmsx) 2 135 137
2269 $grammar_spec =~ m{
2270 (.*?)
2271 (
2272 <
2273 (?! (?:obj)? (?:rule: | token ) )
2274 ( [^>]+ )
2275 >
2276 \s*
2277 ( [?+*][+] | \{.*\}[+] )
2278 )
2279 }gxms) {
2280 2 my ($prefix, $match, $rule, $qual) = ($1, $2, $3, $4);
2281 2 $dubious_line += $prefix =~ tr/\n//;
2282 2 my $safe_qual = substr($qual,0,-1);
2283 2 _debug_notify( warn =>
2284 qq{Non-backtracking subrule call <$rule>$qual},
2285 qq{at $source_file line $dubious_line},
2286 qq{may not revert correctly during backtracking.},
2287 qq{(If grammar does not work, try <$rule>$safe_qual instead)},
2288 q{},
2289 );
2290 2 $dubious_line += $match =~ tr/\n//;
2291 }
2292  
2293 # Check whether a log file was specified...
2294 135 my $compiletime_debugging_requested;
2295 135 local *Regexp::Grammars::LOGFILE = *Regexp::Grammars::LOGFILE;
2296 135 my $logfile = q{-};
2297  
2298 135 my $log_where = "for regex grammar defined at $source_file line $source_line";
2299 135 $grammar_spec =~ s{ ^ [^#]* < logfile: \s* ([^>]+?) \s* > }{
2300 0 $logfile = _timestamp($1);
2301  
2302 # Presence of <logfile:...> implies compile-time logging...
2303 0 $compiletime_debugging_requested = 1;
2304 0 *Regexp::Grammars::LOGFILE = _open_log('>',$logfile, $log_where );
2305  
2306 # Delete <logfile:...> directive...
2307 0 q{};
2308 }gexms;
2309  
2310 # Look ahead for any run-time debugging or timeout requests...
2311 135 my $runtime_debugging_requested
2312 = $grammar_spec =~ m{
2313 ^ [^#]*
2314 < debug: \s* (run | match | step | try | on | same ) \s* >
2315 | \$DEBUG (?! \s* (?: \[ | \{) )
2316 }xms;
2317  
2318 135 my $timeout_requested
2319 = $grammar_spec =~ m{
2320 ^ [^#]*
2321 < timeout: \s* \d+ \s* >
2322 }xms;
2323  
2324  
2325 # Standard actions set up and clean up any regex debugging...
2326 # Before entire match, set up a stack of attempt records and report...
2327 135 ▶ my $pre_match_debug
Branch: 1 branch
BranchTrueFalseTotal
$runtime_debugging_requested ? : 1 134 135
2328 = $runtime_debugging_requested
2329 ? qq{(?{; *Regexp::Grammars::LOGFILE
2330 = Regexp::Grammars::_open_log('>>','$logfile', '$log_where');
2331 Regexp::Grammars::_init_try_stack(); })}
2332 : qq{(?{; *Regexp::Grammars::LOGFILE
2333 = Regexp::Grammars::_open_log('>>','$logfile', '$log_where'); })}
2334 ;
2335  
2336 # After entire match, report whether successful or not...
2337 135 ▶ my $post_match_debug
Branch: 1 branch
BranchTrueFalseTotal
$runtime_debugging_requested ? : 1 134 135
2338 = $runtime_debugging_requested
2339 ? qq{(?{;Regexp::Grammars::_debug_matched(0,\\%/,'<grammar>',\$^N)})
2340 |(?>(?{;Regexp::Grammars::_debug_handle_failures(0,'<grammar>'); }) (?!))
2341 }
2342 : q{}
2343 ;
2344  
2345 # Remove comment lines...
2346 135 $grammar_spec =~ s{^ ([^#\n]*) \s \# [^\n]* }{$1}gxms;
2347  
2348 # Subdivide into rule and token definitions, preparing to process each...
2349 # REWRITE THIS, USING (PROBABLY NEED TO REFACTOR ALL GRAMMARS TO REUSe
2350 # THESE COMPONENTS:
2351 # (?<PARAMLIST> \( \s* (?&PARAMS)? \s* \) | (?# NOTHING ) )
2352 # (?<PARAMS> (?&PARAM) \s* (?: , \s* (?&PARAM) \s* )* ,? )
2353 # (?<PARAM> (?&VAR) (?: \s* = \s* (?: (?&LITERAL) | (?&PARENCODE) ) )? )
2354 # (?<LITERAL> (?&NUMBER) | (?&STRING) | (?&VAR) )
2355 # (?<VAR> : (?&IDENT) )
2356 135 my @defns = split m{
2357 (< (obj|)(rule|token) \s*+ :
2358 \s*+ ((?:${IDENT}::)*+) (?: ($IDENT) \s*+ = \s*+ )?+
2359 ($IDENT)
2360 \s* >)
2361 }xms, $grammar_spec;
2362  
2363 # Extract up list of names of defined rules/tokens...
2364 # (Name is every 6th item out of every seven, skipping the first item)
2365 135 my @subrule_names = @defns[ map { $_ * 7 + 6 } 0 .. ((@defns-1)/7-1) ];
2366 135 my @defns_copy = @defns[1..$#defns];
2367 135 my %subrule_names;
2368  
2369 # Build a look-up table of subrule names, checking for duplicates...
2370 135 my $defn_line = $source_line + $defns[0] =~ tr/\n//;
2371 135 my %first_decl_explanation;
2372 135 for my $subrule_name (@subrule_names) {
2373 260 my ($full_decl, $objectify, $type, $qualifier, $name, $callname, $body) = splice(@defns_copy, 0, 7);
2374 260 ▶ if (++$subrule_names{$subrule_name} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$subrule_names{$subrule_name} > 1) { } 0 260 260
2375 _debug_notify( warn =>
2376 "Redeclaration of <$objectify$type: $subrule_name>",
2377 "at $source_file line $defn_line",
2378 "will be ignored.",
2379 0 @{ $first_decl_explanation{$subrule_name} },
2380 q{},
2381 );
2382 }
2383 else {
2384 260 $first_decl_explanation{$subrule_name} = [
2385 "(Hidden by the earlier declaration of <$objectify$type: $subrule_name>",
2386 " at $source_file line $defn_line)"
2387 ];
2388 }
2389 260 $defn_line += ($full_decl.$body) =~ tr/\n//;
2390 }
2391  
2392 # Add the built-ins...
2393 135 @subrule_names{'ws', 'hk', 'matchpos', 'matchline'} = (1) x 4;
2394  
2395 # An empty main rule will never match anything...
2396 135 my $main_regex = shift @defns;
2397 135 ▶ if ($main_regex =~ m{\A (?: \s++ | \(\?\# [^)]* \) | \# [^\n]++ )* \z}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main_regex =~ /\A (?: \s++ | \(\?\# [^)]* \) | \# [^\n]++ )* \z/msx) 0 135 135
2398 0 _debug_notify( error =>
2399 "No main regex specified before rule definitions",
2400 "in regex grammar declared at $source_file line $source_line",
2401 "Grammar will never match anything.",
2402 "(Or did you forget a <grammar:...> specification?)",
2403 q{},
2404 );
2405 }
2406  
2407 # Compile the regex or grammar...
2408 135 my $regex = q{};
2409 135 my $grammar_name;
2410 my $is_grammar;
2411  
2412 # Is this a grammar specification?
2413 135 ▶ if ($main_regex =~ $GRAMMAR_DIRECTIVE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main_regex =~ /$GRAMMAR_DIRECTIVE/) { } 6 129 135
2414 # Normalize grammar name and report...
2415 6 $grammar_name = $+{grammar_name};
2416 6 ▶ if ($grammar_name !~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($grammar_name =~ /::/) 4 2 6
2417 4 $grammar_name = caller(1) . "::$grammar_name";
2418 }
2419 6 $is_grammar = 1;
2420  
2421 # Add subrule definitions to namespace...
2422 6 for my $subrule_name (@subrule_names) {
2423 11 $CACHE{$grammar_name.'::'.$subrule_name} = 1;
2424 }
2425 }
2426 else {
2427 129 state $dummy_grammar_index = 0;
2428 129 $grammar_name = '______' . $dummy_grammar_index++;
2429 }
2430  
2431 # Extract any inheritance information...
2432 135 my ($inherited_rules, $inherited_subrule_names)
2433 = _extract_inheritances(
2434 $source_line, $source_file,
2435 $main_regex,
2436 $compiletime_debugging_requested,
2437 $grammar_name
2438 );
2439  
2440 # Remove <extends:...> requests...
2441 135 $main_regex =~ s{ $EXTENDS_DIRECTIVE }{}gxms;
2442  
2443 # Add inherited subrule names to allowed subrule names;
2444 135 @subrule_names{ keys %{$inherited_subrule_names} }
2445 135 = values %{$inherited_subrule_names};
2446  
2447 # Remove comments from top-level grammar...
2448 135 $main_regex =~ s{
2449 \(\?\# [^)]* \)
2450 | (?<! \\ ) [#] [^\n]+
2451 }{}gxms;
2452  
2453 # Remove any top-level nocontext directive...
2454 # 1 2 3 4
2455 0 ▶ $main_regex =~ s{^( (.*?) (\\*) (\# [^\n]*) )$}{length($3) % 2 ? $1 : $2.substr($3,0,-1)}gexms;
Branch: 1 branch
BranchTrueFalseTotal
length($3) % 2 ? : 0 0 0
2456 135 ▶ my $nocontext = ($main_regex =~ s{ < nocontext \s* : \s* > }{}gxms) ? 1
Branch: 2 branches
BranchTrueFalseTotal
$main_regex =~ s/ < nocontext \s* : \s* > //gmsx ? : 4 131 135
$main_regex =~ s/ < context \s* : \s* > //gmsx ? : 0 131 131
2457 : ($main_regex =~ s{ < context \s* : \s* > }{}gxms) ? 0
2458 : 0;
2459  
2460 # If so, set up to save the grammar...
2461 135 ▶ if ($is_grammar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_grammar) { } 6 129 135
2462 # Normalize grammar name and report...
2463 6 ▶ if ($grammar_name !~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($grammar_name =~ /::/) 0 6 6
2464 0 $grammar_name = caller(1) . "::$grammar_name";
2465 }
2466 6 ▶ if ($compiletime_debugging_requested) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compiletime_debugging_requested) 0 6 6
2467 0 _debug_notify( info =>
2468 "Processing definition of grammar $grammar_name...",
2469 q{},
2470 );
2471 }
2472  
2473 # Remove the grammar directive...
2474 6 $main_regex =~ s{
2475 ( $GRAMMAR_DIRECTIVE
2476 | < debug: \s* (run | match | step | try | on | off | same ) \s* >
2477 )
2478 6 }{$source_line += $1 =~ tr/\n//; q{}}gexms;
2479  
2480 # Check for anything else in the main regex...
2481 6 ▶ if ($main_regex =~ /\A(\s*)\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main_regex =~ /\A(\s*)\S/) 0 6 6
2482 0 $source_line += $1 =~ tr/\n//;
2483 _debug_notify( warn =>
2484 "Unexpected item before first subrule specification",
2485 "in definition of <grammar: $grammar_name>",
2486 "at $source_file line $source_line:",
2487 0 map({ " $_"} grep /\S/, split "\n", $main_regex),
2488 "(this will be ignored when defining the grammar)",
2489 q{},
2490 );
2491 }
2492  
2493 # Remember set of valid subrule names...
2494 $subrule_names_for{$grammar_name}
2495 = {
2496 56 map({ ($_ => 1) } keys %subrule_names),
2497 6 map({ ($grammar_name.'::'.$_ => 1) } grep { !/::/ } keys %subrule_names),
2498 };
2499 }
2500 else { #...not a grammar specification
2501 # Report how main regex was interpreted, if requested to...
2502 129 ▶ if ($compiletime_debugging_requested) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compiletime_debugging_requested) 0 129 129
2503 0 _debug_notify( info =>
2504 "Processing the main regex before any rule definitions",
2505 );
2506 }
2507  
2508 # Any actual regex is processed first...
2509 129 $regex = _translate_subrule_calls(
2510 $source_file, $source_line,
2511 $grammar_name,
2512 $main_regex,
2513 $compiletime_debugging_requested,
2514 $runtime_debugging_requested,
2515 $timeout_requested,
2516 $pre_match_debug,
2517 $post_match_debug,
2518 q{}, # Expected...what?
2519 \%subrule_names,
2520 0, # Whitespace isn't magical
2521 );
2522  
2523 # Wrap the main regex (to ensure |'s don't segment pre and post commands)...
2524 129 $regex = "(?:$regex)";
2525  
2526 # Report how construct was interpreted, if requested to...
2527 129 ▶ if ($compiletime_debugging_requested) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compiletime_debugging_requested) 0 129 129
2528 0 _debug_notify( q{} =>
2529 q{ |},
2530 q{ \\___End of main regex},
2531 q{},
2532 );
2533 }
2534 }
2535  
2536 # Update line number...
2537 135 $source_line += $main_regex =~ tr/\n//;
2538  
2539 # Then iterate any following rule definitions...
2540 135 ▶ while (@defns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@defns) 260 135 395
2541 # Grab details of each rule defn (as extracted by previous split)...
2542 260 my ($full_decl, $objectify, $type, $qualifier, $name, $callname, $body) = splice(@defns, 0, 7);
2543 260 ▶ $name //= $callname;
Condition66%2/3
$name //= $callname
l!l&&r!l&&!r
92510
MC/DC0%0/2
$name //= $callname
$name $callname
Truth table66%2/3
A //= B
A= $nameB= $callname
ABresult
1X1
011
000
2544 260 my $qualified_name = $grammar_name.'::'.$callname;
2545  
2546 # Report how construct was interpreted, if requested to...
2547 260 ▶ if ($compiletime_debugging_requested) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compiletime_debugging_requested) 0 260 260
2548 0 ▶ _debug_notify( info =>
Branch: 1 branch
BranchTrueFalseTotal
$objectify ? : 0 0 0
2549 "Defining a $type: <$callname>",
2550 " |...Returns: " . ($objectify ? "an object of class '$qualifier$name'" : "a hash"),
2551 );
2552 }
2553  
2554 260 ▶ my $local_nocontext
Branch: 2 branches
BranchTrueFalseTotal
$body =~ s/ < nocontext \s* : \s* > //gmsx ? : 4 256 260
$body =~ s/ < context \s* : \s* > //gmsx ? : 4 252 256
2555 = ($body =~ s{ < nocontext \s* : \s* > }{}gxms) ? 1
2556 : ($body =~ s{ < context \s* : \s* > }{}gxms) ? 0
2557 : $nocontext;
2558  
2559 # Translate any nested <...> constructs...
2560 260 my $trans_body = _translate_subrule_calls(
2561 $source_file, $source_line,
2562 $grammar_name,
2563 $body,
2564 $compiletime_debugging_requested,
2565 $runtime_debugging_requested,
2566 $timeout_requested,
2567 $pre_match_debug,
2568 $post_match_debug,
2569 $callname, # Expected...what?
2570 \%subrule_names,
2571 $type eq 'rule', # Is whitespace magical?
2572 );
2573  
2574 # Report how construct was interpreted, if requested to...
2575 260 ▶ if ($compiletime_debugging_requested) {
Branch: 1 branch
BranchTrueFalseTotal
if ($compiletime_debugging_requested) 0 260 260
2576 0 _debug_notify( q{} =>
2577 q{ |},
2578 q{ \\___End of rule definition},
2579 q{},
2580 );
2581 }
2582  
2583 # Make allowance for possible local whitespace definitions...
2584 260 my $local_ws_defn = q{};
2585 260 my $local_ws_call = q{(?&ws__implicit__)};
2586  
2587 # Rules make non-code literal whitespace match textual whitespace...
2588 260 ▶ if ($type eq 'rule') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'rule') { } 135 125 260
2589 # Implement any local whitespace definition...
2590 135 my $first_ws = 1;
2591 WS_DIRECTIVE:
2592 135 ▶ while ($trans_body =~ s{$WS_PATTERN}{}oxms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trans_body =~ s/$WS_PATTERN//mosx) 4 135 139
2593 4 my $defn = $1;
2594 4 ▶ if ($defn !~ m{\S}xms) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $defn =~ /\S/msx) { } 0 4 4
elsif (not $first_ws) { } 0 4 4
2595 0 _debug_notify( warn =>
2596 qq{Ignoring useless empty <ws:> directive},
2597 qq{in definition of <rule: $name>},
2598 qq{near $source_file line $source_line},
2599 qq{(Did you mean <ws> instead?)},
2600 q{},
2601 );
2602 0 next WS_DIRECTIVE;
2603 }
2604 elsif (!$first_ws) {
2605 0 _debug_notify( warn =>
2606 qq{Ignoring useless extra <ws:$defn> directive},
2607 qq{in definition of <rule: $name>},
2608 qq{at $source_file line $source_line},
2609 qq{(No more than one is permitted per rule!)},
2610 q{},
2611 );
2612 0 next WS_DIRECTIVE;
2613 }
2614 else {
2615 4 $first_ws = 0;
2616 }
2617 4 state $ws_counter = 0;
2618 4 $ws_counter++;
2619 4 $local_ws_defn = qq{(?<__RG_ws_$ws_counter> $defn)};
2620 4 $local_ws_call = qq{(?&__RG_ws_$ws_counter)};
2621 }
2622  
2623 # Implement auto-whitespace...
2624 135 state $CODE_OR_SPACE = qr{
2625  
2626 # TODO: REWORK THIS INSUFFICENT FIX FOR t/grammar_autospace.t...
2627 #
2628 # (?<magic_space> \(\?: \s++ \) ) # Explicitly walled off space is magic
2629 # |
2630  
2631 (?<ignorable_space> # These are not magic...
2632 \( \?\?? (?&BRACED) \) # Embedded code blocks
2633 | \s++ # Whitespace followed by...
2634 (?= \| # ...an OR
2635 | \(\?\#\) # ...a null comment
2636 | (?: \) \s* )? \z # ...the end of the rule
2637 | \(\(?\?\&ws\) # ...an explicit ws match
2638 | \(\?\??\{ # ...an embedded code block
2639 | \\[shv] # ...an explicit space match
2640 )
2641 )
2642 |
2643 (?<magic_space> \s++ ) # All other whitespace is magic
2644  
2645 (?(DEFINE) (?<BRACED> \{ (?: \\. | (?&BRACED) | [^{}] )* \} ) )
2646 }xms;
2647 135 ▶ $trans_body =~ s{($CODE_OR_SPACE)}{ $+{ignorable_space} // $local_ws_call }exmsg;
Condition66%2/3
$+{"ignorable_space"} // $local_ws_call
l!l&&r!l&&!r
8363660
MC/DC0%0/2
$+{"ignorable_space"} // $local_ws_call
$+{"ignorable_space"} $local_ws_call
Truth table66%2/3
A // B
A= $+{"ignorable_space"}B= $local_ws_call
ABresult
1X1
011
000
2648 }
2649 else {
2650 125 ▶ while ($trans_body =~ s{$WS_PATTERN}{}oxms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trans_body =~ s/$WS_PATTERN//mosx) 0 125 125
2651 0 _debug_notify( warn =>
2652 qq{Ignoring useless <ws:$1> directive},
2653 qq{in definition of <token: $name>},
2654 qq{at $source_file line $source_line},
2655 qq{(Did you need to define <rule: $name> instead of <token: $name>?)},
2656 q{},
2657 );
2658 }
2659 }
2660  
2661 260 $regex
2662 .= "\n###############[ $source_file line $source_line ]###############\n"
2663 . _translate_rule_def(
2664 $type, $qualifier, $name, $callname, $qualified_name, $trans_body, $objectify,
2665 $local_ws_defn, $local_nocontext,
2666 );
2667  
2668 # Update line number...
2669 260 $source_line += ($full_decl.$body) =~ tr/\n//;
2670 }
2671  
2672 # Insert checkpoints into any user-defined code block...
2673 135 $regex =~ s{ \( \?\?? \{ \K (?!;) }{
2674 local \@Regexp::Grammars::RESULT_STACK = \@Regexp::Grammars::RESULT_STACK;
2675 }xmsg;
2676  
2677 # Check for any suspicious left-overs from the start of the regex...
2678 135 pos $regex = 0;
2679  
2680 # If a grammar definition, save grammar and return a placeholder...
2681 135 ▶ if ($is_grammar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_grammar) { } 6 129 135
2682 6 $user_defined_grammar{$grammar_name} = $regex;
2683 6 return qq{(?{
2684 warn "Can't match directly against a pure grammar: <grammar: $grammar_name>\n";
2685 })(*COMMIT)(?!)};
2686 }
2687 # Otherwise, aggregrate the final grammar...
2688 else {
2689 129 return _complete_regex($regex.$inherited_rules, $pre_match_debug, $post_match_debug, $nocontext);
2690 }
2691 }
2692  
2693 129 ▶sub _complete_regex {
Subroutine _complete_regex: called CC=2 SCAR=6.9
2694 129 my ($regex, $pre_match_debug, $post_match_debug, $nocontext) = @_;
2695  
2696 129 ▶ return $nocontext ? qq{(?x)$pre_match_debug$PROLOGUE$regex$EPILOGUE_NC$post_match_debug}
Branch: 1 branch
BranchTrueFalseTotal
$nocontext ? : 4 125 129
2697 : qq{(?x)$pre_match_debug$PROLOGUE$regex$EPILOGUE$post_match_debug};
2698 }
2699  
2700 82 1; # Magic true value required at end of module
2701  
2702 __END__
↑ Summary