blib/lib/MarpaX/G4/MarpaGen.pm

filter: Statementstmt 69.9% 667 / 954 Branchbran 58.0% 310 / 534 Conditioncond 51.9% 159 / 306 MC/DCmcdc 25.4% 46 / 181 Subroutinesub 77.6% 59 / 76 Podpod 0.0% 0 / 70 totaltotal 58.5% 1241 / 2121 CC 393 SCAR 44.8
CC
393
Cov
63%
retagSimpleRule
334.7
processRightSides
220.3
walktoken
202.3
CRAP 88.5
« blib/lib/MarpaX/G4/Symboltable.pm ↑ Summary blib/lib/MarpaX/G4/Parser.pm »
1 # ----------------------------------------------------------------------------------------------------- #
2 # MarpaX::MarpaGen #
3 # #
4 # translate the parsed antlr4 rules to Marpa::R2 syntax. #
5 # #
6 # ----------------------------------------------------------------------------------------------------- #
7  
8 package MarpaX::G4::MarpaGen;
9  
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use MarpaX::G4::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use MarpaX::G4::Symboltable;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # --------------------------------------------------- #
20 # exposed class methods #
21 # --------------------------------------------------- #
22  
23 sub new
24 {
25 1 ▶ my $invocant = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
26 1 ▶ my $class = ref($invocant) || $invocant; # Object or class name
Condition33%1/3
ref $invocant || $invocant
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table33%1/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
27 1 my $self = {}; # initiate our handy hashref
28 1 bless($self,$class); # make it usable
29  
30 1 $self->{symboltable} = undef;
31 1 $self->{verbosity} = 0;
32 1 $self->{level} = -1;
33 1 $self->{outputfilename} = "-";
34 1 $self->{outf} = undef;
35 1 $self->{rules} = {};
36 1 $self->{subrules} = {};
37 1 $self->{generationoptions} = {};
38  
39 1 return $self;
40 }
41  
42 0 ▶sub setVerbosity { my ($self, $verbosity) = @_; $self->{verbosity} = defined($verbosity) ? $verbosity : 0; }
Subroutine setVerbosity: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $verbosity ? : 0 0 0
43 48 ▶sub symboltable { my ($self) = @_; return $self->{symboltable}; }
Subroutine symboltable: called CC=1 SCAR=0.0
Pod undocumented
44 1 ▶sub setoutputfile { my ($self, $outputfilename) = @_; $self->{outputfilename} = $outputfilename; }
Subroutine setoutputfile: called CC=1 SCAR=0.0
Pod undocumented
45  
46 1 ▶sub fragment2class { my ($self) = @_; $self->{generationoptions}{fragment2class} = 'true'; }
Subroutine fragment2class: called CC=1 SCAR=0.0
Pod undocumented
47 0 ▶sub embedactions { my ($self) = @_; $self->{generationoptions}{embedactions} = 'true'; }
Subroutine embedactions: not called CC=1 SCAR=6.9
Pod undocumented
48 0 ▶sub stripactions { my ($self) = @_; $self->{generationoptions}{stripactions} = 'true'; }
Subroutine stripactions: not called CC=1 SCAR=6.9
Pod undocumented
49 0 ▶sub stripallcomments { my ($self) = @_; $self->{generationoptions}{stripallcomments} = 'true'; }
Subroutine stripallcomments: not called CC=1 SCAR=6.9
Pod undocumented
50 1 ▶sub matchcaseinsensitive { my ($self) = @_; $self->{generationoptions}{matchcaseinsensitive} = 'true'; }
Subroutine matchcaseinsensitive: called CC=1 SCAR=0.0
Pod undocumented
51 0 ▶sub shiftlazytogreedy { my ($self) = @_; $self->{generationoptions}{shiftlazytogreedy} = 'true'; }
Subroutine shiftlazytogreedy: not called CC=1 SCAR=6.9
Pod undocumented
52 1 ▶sub buildkeywords { my ($self) = @_; $self->{generationoptions}{buildkeywords} = 'true'; }
Subroutine buildkeywords: called CC=1 SCAR=0.0
Pod undocumented
53  
54 sub testoption
55 {
56 189 ▶ my ( $self, $option ) = @_;
Subroutine testoption: called CC=3 SCAR=11.3
Pod undocumented
57  
58 189 ▶ return undef if !defined $option;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $option 0 189 189
59 189 $option = lc $option;
60 189 ▶ return undef if !exists $self->{generationoptions}{$option};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"generationoptions"}{$option} 0 189 189
61  
62 189 return $self->{generationoptions}{$option};
63 }
64  
65 sub dumpStructure
66 {
67 0 ▶ my ( $self, $title, $structure ) = @_;
Subroutine dumpStructure: not called CC=1 SCAR=6.9
Pod undocumented
68  
69 0 printf "=== %s\n", $title;
70 0 $Data::Dumper::Indent = 1;
71 0 print Dumper($structure);
72 }
73  
74 sub abortWithError
75 {
76 0 ▶ my ( $self, $msg, $structure ) = @_;
Subroutine abortWithError: not called CC=1 SCAR=6.9
Pod undocumented
77  
78 0 $self->dumpStructure($msg, $structure);
79 0 die "aborting on unrecoverable error";
80 }
81  
82 sub mydie
83 {
84 0 ▶ my ( $msg ) = @_;
Subroutine mydie: not called CC=1 SCAR=6.9
Pod undocumented
85 0 die "${msg}";
86 }
87  
88 sub addMarpaRule
89 {
90 32 ▶ my ( $self, $rulename, $marparule ) = @_;
Subroutine addMarpaRule: called CC=2 SCAR=7.0
Pod undocumented
91  
92 32 my $rules = $self->{rules};
93 32 ▶ mydie "INTERNAL: rule $rulename defined more than once" if exists $rules->{$rulename};
Branch: 1 branch
BranchTrueFalseTotal
if exists $rules->{$rulename} 0 32 32
94 32 $rules->{$rulename} = $marparule;
95 }
96  
97 ##
98 # deleteMarpaRule : delete 'subrulename' if it is a subrule of 'rulename'
99 ##
100 sub deleteMarpaRule
101 {
102 0 ▶ my ( $self, $rulename ) = @_;
Subroutine deleteMarpaRule: not called CC=2 SCAR=17.9
Pod undocumented
103  
104 0 my $rules = $self->{rules};
105  
106 0 ▶ mydie "INTERNAL: can't delete undefined rule $rulename" if !exists $rules->{$rulename};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rules->{$rulename} 0 0 0
107  
108 0 $rules->{$rulename} = { deletedafterconversiontoclass => "true" };
109 0 $self->deleteAllSubrules($rulename);
110 }
111  
112 ##
113 # deleteSingleSubrule : delete 'subrulename' if it is a subrule of 'rulename'
114 ##
115 sub deleteSingleSubrule
116 {
117 0 ▶ my ( $self, $rulename, $subrulename ) = @_;
Subroutine deleteSingleSubrule: not called CC=5 SCAR=34.0
Pod undocumented
118  
119 0 ▶ return if !exists $self->{subrules}{$rulename};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"subrules"}{$rulename} 0 0 0
120  
121 0 my $subrules = $self->{subrules}{$rulename};
122  
123 0 my $status = 0;
124 0 my @newrules;
125  
126 map {
127 0 ▶ if ( $_ eq $subrulename )
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq $subrulename) { } 0 0 0
128 {
129 0 $status = 1;
130 }
131 else
132 {
133 0 push @newrules, $_;
134 }
135 } @$subrules;
136  
137 0 $self->{subrules}{$rulename} = \@newrules;
138 0 ▶ if ($status)
Branch: 1 branch
BranchTrueFalseTotal
if ($status) 0 0 0
139 {
140 0 my $rules = $self->{rules};
141 0 ▶ mydie "INTERNAL: can't delete undefined rule $subrulename" if !exists $rules->{$subrulename};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rules->{$subrulename} 0 0 0
142 0 $rules->{$subrulename} = { deletedafterconversiontoclass => "true" }
143 }
144 }
145  
146 sub checkMarpaRuleExists
147 {
148 5 ▶ my ( $self, $rulename ) = @_;
Subroutine checkMarpaRuleExists: called CC=2 SCAR=7.0
Pod undocumented
149  
150 5 my $rules = $self->{rules};
151  
152 5 ▶ return undef if !exists $rules->{$rulename};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rules->{$rulename} 0 5 5
153  
154 5 return $rules->{$rulename};
155 }
156  
157 sub getMarpaRule
158 {
159 96 ▶ my ( $self, $parent, $rulename ) = @_;
Subroutine getMarpaRule: called CC=3 SCAR=14.2
Pod undocumented
160  
161 96 my $rules = $self->{rules};
162  
163 96 ▶ if (!exists $rules->{$rulename})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $rules->{$rulename}) 0 96 96
164 {
165 0 ▶ $parent = "<ROOT>" if !defined $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parent 0 0 0
166 0 mydie "INTERNAL: rule $rulename is undefined under parent $parent";
167 }
168  
169 96 return $rules->{$rulename};
170 }
171  
172 sub tagMarpaRule
173 {
174 32 ▶ my ( $self, $rulename ) = @_;
Subroutine tagMarpaRule: called CC=2 SCAR=7.0
Pod undocumented
175  
176 32 my $rules = $self->{rules};
177 32 ▶ mydie "INTERNAL: rule $rulename is undefined" if !exists $rules->{$rulename};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rules->{$rulename} 0 32 32
178 32 $rules->{$rulename}{status} = "done";
179 }
180  
181 sub isSubrule
182 {
183 5 ▶ my ( $self, $rulename, $subrulename ) = @_;
Subroutine isSubrule: called CC=4 SCAR=14.1
Pod undocumented
184  
185 5 my $subrules = $self->{subrules};
186  
187 5 ▶ return 0 if !exists $subrules->{$rulename};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $subrules->{$rulename} 0 5 5
188  
189 5 for my $sr (@{$subrules->{$rulename}})
190 {
191 9 ▶ return 1 if $sr eq $subrulename;
Branch: 1 branch
BranchTrueFalseTotal
if $sr eq $subrulename 5 4 9
192 }
193  
194 0 return 0;
195 }
196  
197 # --------------------------------------------------- #
198 # grammar generator #
199 # --------------------------------------------------- #
200  
201 sub enterLevel
202 {
203 14 ▶ my ($self, $rulename, $context) = @_;
Subroutine enterLevel: called CC=3 SCAR=11.3
Pod undocumented
204  
205 14 ▶ my $level = $context->{level} if exists $context->{level};
Branch: 1 branch
BranchTrueFalseTotal
if exists $context->{"level"} 14 0 14
206 14 ▶ $level = -1 if !defined $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 0 14 14
207 14 ++$level;
208 14 $context->{level} = $level;
209 }
210  
211 sub exitLevel
212 {
213 14 ▶ my ($self, $rulename, $context) = @_;
Subroutine exitLevel: called CC=1 SCAR=0.0
Pod undocumented
214  
215 14 my $level = $context->{level};
216 14 --$level;
217 14 $context->{level} = $level;
218 }
219  
220 sub generateSubruleName
221 {
222 18 ▶ my ($self, $rulename, $cardinality, $context) = @_;
Subroutine generateSubruleName: called CC=4 SCAR=13.9
Pod undocumented
223  
224 18 ▶ $context->{subruleindex}{$rulename}{index} = 0 if !exists $context->{subruleindex}{$rulename}{index};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $context->{"subruleindex"}{$rulename}{"index"} 8 10 18
225  
226 18 my $subruleindex = ++$context->{subruleindex}{$rulename}{index};
227 18 my $subrulename = sprintf "%s_%03d", $rulename, $subruleindex;
228  
229 18 ▶ if ( defined $cardinality && $cardinality eq "?" )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cardinality and $cardinality eq "?") 4 14 18
Condition100%3/3
defined $cardinality and $cardinality eq "?"
!ll&&!rl&&r
954
MC/DC100%2/2
defined $cardinality and $cardinality eq "?"
defined $cardinality $cardinality eq "?"
Truth table100%3/3
A and B
A= defined $cardinalityB= $cardinality eq "?"
ABresult
0X0
100
111
230 {
231 4 $subrulename = "opt_" . $subrulename;
232 }
233  
234 18 push @{$self->{subrules}{$rulename}}, $subrulename;
235  
236 18 return $subrulename;
237 }
238  
239 sub retrieveModifier
240 {
241 360 ▶ my ($token, $tag) = @_;
Subroutine retrieveModifier: called CC=3 SCAR=11.0
Pod undocumented
242  
243 360 ▶ return undef if ref $token ne "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $token ne "HASH" 0 360 360
244  
245 360 my $modifier = undef;
246 360 ▶ $modifier = $token->{$tag} if exists $token->{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if exists $token->{$tag} 51 309 360
247  
248 360 return $modifier;
249 }
250  
251 sub negateElement
252 {
253 1 ▶ my ($self, $rulename, $mxtoken, $context) = @_;
Subroutine negateElement: called CC=8 SCAR=26.7
Pod undocumented
254  
255 # if ($rulename =~ /NATIONAL_CHAR_STRING_LIT/)
256 # {
257 # printf "found!\n";
258 # }
259  
260 1 ▶ mydie "INTERNAL: negated element is not a hash in rule $rulename" if ref $mxtoken ne "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $mxtoken ne "HASH" 0 1 1
261  
262 1 my $mxelement;
263  
264 1 ▶ if (! exists $mxtoken->{class4list} )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $mxtoken->{"class4list"}) 0 1 1
265 {
266 0 mydie "INTERNAL: can't negate missing class4list in rule $rulename";
267 ##
268 # TODO: verify if we could continue by mapping this error to 'unsupported'
269 # so that we could process the complete input
270 ##
271 0 $mxelement = { type => 'unsupported', msg => "can't negate non-class token in $rulename" };
272 }
273  
274 1 my $class4list = $mxtoken->{class4list};
275 1 $mxelement = { type => 'negatedclass', value => "^${class4list}" };
276  
277 1 ▶ $mxelement->{grammarstate} = $mxtoken->{grammarstate} if exists $mxtoken->{grammarstate};
Branch: 1 branch
BranchTrueFalseTotal
if exists $mxtoken->{"grammarstate"} 0 1 1
278 1 ▶ $mxelement->{isFragmentOrChild} = $mxtoken->{isFragmentOrChild} if exists $mxtoken->{isFragmentOrChild};
Branch: 1 branch
BranchTrueFalseTotal
if exists $mxtoken->{"isFragmentOrChild"} 0 1 1
279  
280 ##
281 # delete the negated subrule since it will be replaced with a new subrule.
282 ##
283 1 ▶ if (exists $mxtoken->{rhs} && exists $mxtoken->{rhs}{token})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $mxtoken->{"rhs"} and exists $mxtoken->{"rhs"}{"token"}) 0 1 1
Condition33%1/3
exists $mxtoken->{"rhs"} and exists $mxtoken->{"rhs"}{"token"}
!ll&&!rl&&r
100
MC/DC0%0/2
exists $mxtoken->{"rhs"} and exists $mxtoken->{"rhs"}{"token"}
exists $mxtoken->{"rhs"} exists $mxtoken->{"rhs"}{"token"}
Truth table33%1/3
A and B
A= exists $mxtoken->{"rhs"}B= exists $mxtoken->{"rhs"}{"token"}
ABresult
0X0
100
111
284 {
285 0 my $token = $mxtoken->{rhs}{token};
286 0 ▶ if (ref $token eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $token eq "") 0 0 0
287 {
288 0 $self->deleteSingleSubrule($rulename, $token);
289 }
290 }
291  
292 1 return $mxelement;
293 }
294  
295 sub translateLiteralCase
296 {
297 0 ▶ my ($string, $case) = @_;
Subroutine translateLiteralCase: not called CC=2 SCAR=17.9
Pod undocumented
298  
299 0 ▶ return $case =~ /^U/i ? uc $string : lc $string;
Branch: 1 branch
BranchTrueFalseTotal
$case =~ /^U/i ? : 0 0 0
300 }
301  
302 ##
303 # generatematchcaseinsensitive : make a literal (somewhat) case-insensitive by creating a subrule with 2 match alternatives :
304 #
305 # - all uppercase
306 # - characterclass with the 1st character in upper/lower case, the rest all lowercase
307 #
308 # if the literal is only a single letter, we only retain the characterclass
309 # if the first character is not a letter, we don't generate a class.
310 #
311 ## --------------------------------------------------------------------------------------------------------- ##
312 ## CAVEAT : introduction of the ':i' and ':ic' literal and class modifiers has made this function obsolete. ##
313 ## we retain it as a demo of generated subrules. ##
314 ## --------------------------------------------------------------------------------------------------------- ##
315 sub generateMatchCaseInsensitive
316 {
317 0 ▶ my ($tokenvalue) = @_;
Subroutine generateMatchCaseInsensitive: not called CC=5 SCAR=34.0
Pod undocumented
318  
319 0 my $lctoken = translateLiteralCase($tokenvalue, 'L');
320 0 my $uctoken = translateLiteralCase($tokenvalue, 'U');
321 0 my $prefix = substr($lctoken, 0, 1);
322 0 my $rest = substr($lctoken, 1);
323 0 my $lcp = lc $prefix;
324 0 my $ucp = uc $prefix;
325 0 my $mixedcase = "${lcp}${ucp}";
326  
327 0 my $literaltoken;
328  
329 0 ▶ if ($lcp =~ /[A-Z]/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($lcp =~ /[A-Z]/i) { } 0 0 0
330 {
331 0 $literaltoken = { type => 'tokengroup',
332 definition => [{token => {type => 'class', value => $mixedcase}}]
333 };
334 0 ▶ push @{$literaltoken->{definition}}, {token => {type => 'literal', value => $rest }} if length($rest) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $rest > 0 0 0 0
335 0 ▶ push @{$literaltoken->{definition}}, {token => {type => 'literal', value => $uctoken}, alternative=> 'true'} if length($uctoken) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $uctoken > 1 0 0 0
336 }
337 else
338 {
339 0 $literaltoken = { type => 'tokengroup',
340 definition => [{token => {type => 'literal', value => $lctoken}}]
341 };
342 0 ▶ push @{$literaltoken->{definition}}, {token => {type => 'literal', value => $uctoken}, alternative=> 'true'} if length($uctoken) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $uctoken > 1 0 0 0
343 }
344  
345 0 return $literaltoken;
346 }
347  
348 sub isSingleChar
349 {
350 21 ▶ my ($string) = @_;
Subroutine isSingleChar: called CC=5 SCAR=16.2
Pod undocumented
351  
352 21 ▶ return 1 if $string =~ /^.$/;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^.$/ 17 4 21
353 4 ▶ return 1 if $string =~ /^\\[^u]$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^\\[^u]$/i 1 3 4
354 3 ▶ return 1 if $string =~ /^\\u[0-9A-F]{4,4}$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^\\u[0-9A-F]{4,4}$/i 0 3 3
355 3 ▶ return 1 if $string =~ /^\\u\{[0-9A-F]{5,5}\}$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /^\\u\{[0-9A-F]{5,5}\}$/i 0 3 3
356 3 return 0;
357 }
358  
359 sub isKeywordLetter
360 {
361 71 ▶ my ($self, $rulename, $rule ) = @_;
Subroutine isKeywordLetter: called CC=6 SCAR=22.1
Pod undocumented
362  
363 71 ▶ return 0 if !exists $rule->{class4list};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rule->{"class4list"} 62 9 71
364 9 my $s = $rule->{class4list};
365 9 ▶ return 1 if $rulename =~ /^[a-z]$/i && $s =~ /([a-z])([a-z])/i && ( uc "$1" eq "$2" || lc "$1" eq "$2");
Branch: 1 branch
BranchTrueFalseTotal
if $rulename =~ /^[a-z]$/i and $s =~ /([a-z])([a-z])/i and uc "$1" eq "$2" || lc "$1" eq "$2" 0 9 9
Condition22%2/9
$rulename =~ /^[a-z]$/i and $s =~ /([a-z])([a-z])/i and uc "$1" eq "$2" || lc "$1" eq "$2"
!ll&&!rl&&r
900
$rulename =~ /^[a-z]$/i and $s =~ /([a-z])([a-z])/i
!ll&&!rl&&r
900
uc "$1" eq "$2" or lc "$1" eq "$2"
l!l&&r!l&&!r
000
MC/DC0%0/4
$rulename =~ /^[a-z]$/i and $s =~ /([a-z])([a-z])/i and uc "$1" eq "$2" || lc "$1" eq "$2"
$rulename =~ /^[a-z]$/i $s =~ /([a-z])([a-z])/i uc "$1" eq "$2" lc "$1" eq "$2"
Truth table20%1/5
A and B and C or D
A= $rulename =~ /^[a-z]$/iB= $s =~ /([a-z])([a-z])/iC= uc "$1" eq "$2"D= lc "$1" eq "$2"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
366 9 return 0;
367 }
368  
369 ##
370 # isKeywordFragment : return 1 if '$string' is a characterclass with a pair of lower/upper case letters
371 ##
372 sub isKeywordFragment
373 {
374 60 ▶ my ($self, $rulename, $rule ) = @_;
Subroutine isKeywordFragment: called CC=7 SCAR=19.5
Pod undocumented
375  
376 60 ▶ return undef if !exists $rule->{class4list};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rule->{"class4list"} 30 30 60
377 30 my $string = $rule->{class4list};
378  
379 30 ▶ return lc substr($string, 0, 1) if $string =~ /([a-z])([a-z])/i && ( uc "$1" eq "$2" || lc "$1" eq "$2");
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /([a-z])([a-z])/i and uc "$1" eq "$2" || lc "$1" eq "$2" 1 29 30
Condition83%5/6
$string =~ /([a-z])([a-z])/i and uc "$1" eq "$2" || lc "$1" eq "$2"
!ll&&!rl&&r
2361
uc "$1" eq "$2" or lc "$1" eq "$2"
l!l&&r!l&&!r
016
MC/DC66%2/3
$string =~ /([a-z])([a-z])/i and uc "$1" eq "$2" || lc "$1" eq "$2"
$string =~ /([a-z])([a-z])/i uc "$1" eq "$2" lc "$1" eq "$2"
Truth table75%3/4
A and B or C
A= $string =~ /([a-z])([a-z])/iB= uc "$1" eq "$2"C= lc "$1" eq "$2"
ABCresult
0XX0
1000
11X1
1011
380 29 ▶ return substr($string, 0, 1) if $string !~ /[a-z]/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /[a-z]/i 21 8 29
381 8 ▶ return substr($string, 0, 1) if $string =~ /[0-9]/i;
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /[0-9]/i 5 3 8
382  
383 3 return undef;
384 }
385  
386 sub convertLiteralToClass
387 {
388 21 ▶ my ($literal) = @_;
Subroutine convertLiteralToClass: called CC=2 SCAR=6.9
Pod undocumented
389  
390 21 ▶ return undef if !isSingleChar($literal);
Branch: 1 branch
BranchTrueFalseTotal
unless isSingleChar($literal) 3 18 21
391  
392 18 return "${literal}";
393 }
394  
395 sub convertRangeToClass
396 {
397 0 ▶ my ($token) = @_;
Subroutine convertRangeToClass: not called CC=5 SCAR=34.0
Pod undocumented
398  
399 0 my $begr = $token->{begr};
400 0 my $endr = $token->{endr};
401  
402 0 ▶ return { type => 'unsupported', msg => "can't convert non-literal beg/end range to class" } if $begr->{type} ne 'literal' || $endr->{type} ne 'literal';
Branch: 1 branch
BranchTrueFalseTotal
if $begr->{"type"} ne "literal" or $endr->{"type"} ne "literal" 0 0 0
Condition0%0/3
$begr->{"type"} ne "literal" or $endr->{"type"} ne "literal"
l!l&&r!l&&!r
000
MC/DC0%0/2
$begr->{"type"} ne "literal" or $endr->{"type"} ne "literal"
$begr->{"type"} ne "literal" $endr->{"type"} ne "literal"
Truth table0%0/3
A or B
A= $begr->{"type"} ne "literal"B= $endr->{"type"} ne "literal"
ABresult
1X1
011
000
403  
404 0 $begr = $begr->{value};
405 0 $endr = $endr->{value};
406  
407 0 ▶ return { type => 'unsupported', msg => "can't convert range (${begr} .. ${endr}) to class" } if !isSingleChar($begr) || !isSingleChar($endr);
Branch: 1 branch
BranchTrueFalseTotal
unless isSingleChar($begr) and isSingleChar($endr) 0 0 0
Condition0%0/3
isSingleChar($begr) and isSingleChar($endr)
!ll&&!rl&&r
000
MC/DC0%0/2
isSingleChar($begr) and isSingleChar($endr)
isSingleChar($begr) isSingleChar($endr)
Truth table0%0/3
A and B
A= isSingleChar($begr)B= isSingleChar($endr)
ABresult
0X0
100
111
408  
409 0 my $classtext = "${begr}-${endr}";
410  
411 0 return { type => 'class', value => $classtext, isLexeme => 1, class4list => $classtext };
412 }
413  
414 sub mergeClass4List
415 {
416 120 ▶ my ($tracker, $mxelement) = @_;
Subroutine mergeClass4List: called CC=4 SCAR=13.9
Pod undocumented
417  
418 120 ▶ mydie "tracker has no 'status'" if !exists $tracker->{status};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tracker->{"status"} 0 120 120
419  
420 120 ▶ if ( !exists $mxelement->{class4list} || $tracker->{status} == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $mxelement->{'class4list'} or $tracker->{'status'} == -1) { } 90 30 120
Condition100%3/3
not exists $mxelement->{"class4list"} or $tracker->{"status"} == -1
l!l&&r!l&&!r
781230
MC/DC100%2/2
not exists $mxelement->{"class4list"} or $tracker->{"status"} == -1
not exists $mxelement->{"class4list"} $tracker->{"status"} == -1
Truth table100%3/3
not(A) or B
A= not exists $mxelement->{"class4list"}B= $tracker->{"status"} == -1
ABresult
1X1
011
000
421 {
422 90 $tracker->{status} = -1;
423 }
424 else
425 {
426 30 $tracker->{value} .= $mxelement->{class4list};
427 }
428 }
429  
430 sub mergeKeywordFragment
431 {
432 60 ▶ my ($tracker, $mxelement) = @_;
Subroutine mergeKeywordFragment: called CC=4 SCAR=13.9
Pod undocumented
433  
434 60 ▶ mydie "tracker has no 'status'" if !exists $tracker->{status};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tracker->{"status"} 0 60 60
435  
436 60 ▶ if ( !exists $mxelement->{keywordfragment} || $tracker->{status} == -1 )
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $mxelement->{'keywordfragment'} or $tracker->{'status'} == -1) { } 45 15 60
Condition100%3/3
not exists $mxelement->{"keywordfragment"} or $tracker->{"status"} == -1
l!l&&r!l&&!r
37815
MC/DC100%2/2
not exists $mxelement->{"keywordfragment"} or $tracker->{"status"} == -1
not exists $mxelement->{"keywordfragment"} $tracker->{"status"} == -1
Truth table100%3/3
not(A) or B
A= not exists $mxelement->{"keywordfragment"}B= $tracker->{"status"} == -1
ABresult
1X1
011
000
437 {
438 45 $tracker->{status} = -1;
439 }
440 else
441 {
442 15 $tracker->{value} .= $mxelement->{keywordfragment};
443 }
444 }
445  
446 ##
447 # createSubRule : create a (non-)anonymous subrule to 'rulename'.
448 # create a negated class if the class or token group was negated.
449 #
450 # CAVEAT: remember to reset the 'negation' flag in the caller
451 # to avoid negating twice.
452 ##
453 sub createSubRule
454 {
455 18 ▶ my ($self, $rulename, $mxelement, $negation, $cardinality, $context ) = @_;
Subroutine createSubRule: called CC=10 SCAR=23.2
Pod undocumented
456  
457 18 ▶ if ( defined $negation )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $negation) 0 18 18
458 {
459 0 $mxelement = $self->negateElement($rulename, $mxelement, $context);
460 }
461  
462 18 my $subrulename = $self->generateSubruleName($rulename, $cardinality, $context);
463  
464 ##
465 # propagate grammar fragment states to subrules (i.e. children)
466 ##
467 18 ▶ $mxelement->{grammarstate} = $context->{subruleindex}{$rulename}{grammarstate} if exists $context->{subruleindex}{$rulename}{grammarstate};
Branch: 1 branch
BranchTrueFalseTotal
if exists $context->{"subruleindex"}{$rulename}{"grammarstate"} 14 4 18
468 18 ▶ $mxelement->{isFragmentOrChild} = $context->{subruleindex}{$rulename}{isFragmentOrChild} if exists $context->{subruleindex}{$rulename}{isFragmentOrChild};
Branch: 1 branch
BranchTrueFalseTotal
if exists $context->{"subruleindex"}{$rulename}{"isFragmentOrChild"} 7 11 18
469  
470 ##
471 # wrap cardinality "?" into an 'opt_' rule
472 ##
473 18 ▶ $mxelement->{cardinality} = $cardinality if defined $cardinality && $cardinality eq "?";
Branch: 1 branch
BranchTrueFalseTotal
if defined $cardinality and $cardinality eq "?" 4 14 18
Condition100%3/3
defined $cardinality and $cardinality eq "?"
!ll&&!rl&&r
954
MC/DC100%2/2
defined $cardinality and $cardinality eq "?"
defined $cardinality $cardinality eq "?"
Truth table100%3/3
A and B
A= defined $cardinalityB= $cardinality eq "?"
ABresult
0X0
100
111
474  
475 18 $self->addMarpaRule($subrulename, $mxelement);
476  
477 18 my $mxalias = { rule => $subrulename, rhs => { token => $subrulename } };
478  
479 ##
480 # CAVEAT: don't propagate the lexeme tag upwards.
481 # this avoids confusion in G1 rules.
482 ##
483 18 ▶ $mxalias->{class4list} = $mxelement->{class4list} if exists $mxelement->{class4list} && !defined $cardinality;
Branch: 1 branch
BranchTrueFalseTotal
if exists $mxelement->{"class4list"} and not defined $cardinality 0 18 18
Condition66%2/3
exists $mxelement->{"class4list"} and not defined $cardinality
!ll&&!rl&&r
1440
MC/DC0%0/2
exists $mxelement->{"class4list"} and not defined $cardinality
exists $mxelement->{"class4list"} not defined $cardinality
Truth table66%2/3
A and not(B)
A= exists $mxelement->{"class4list"}B= not defined $cardinality
ABresult
0X0
100
111
484 18 ▶ $mxalias->{grammarstate} = $context->{subruleindex}{$rulename}{grammarstate} if exists $context->{subruleindex}{$rulename}{grammarstate};
Branch: 1 branch
BranchTrueFalseTotal
if exists $context->{"subruleindex"}{$rulename}{"grammarstate"} 14 4 18
485 18 ▶ $mxalias->{isFragmentOrChild} = $context->{subruleindex}{$rulename}{isFragmentOrChild} if exists $context->{subruleindex}{$rulename}{isFragmentOrChild};
Branch: 1 branch
BranchTrueFalseTotal
if exists $context->{"subruleindex"}{$rulename}{"isFragmentOrChild"} 7 11 18
486  
487 18 return $mxalias;
488 }
489  
490 sub deleteAllSubrules
491 {
492 1 ▶ my ($self, $rulename ) = @_;
Subroutine deleteAllSubrules: called CC=3 SCAR=13.3
Pod undocumented
493  
494 1 my $subrules = $self->{subrules};
495 1 my $rules = $self->{rules};
496  
497 1 ▶ return if !exists $subrules->{$rulename};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $subrules->{$rulename} 1 0 1
498  
499 0 for my $subrulename (@{$subrules->{$rulename}})
500 {
501 0 $rules->{$subrulename} = { deletedafterconversiontoclass => "true" };
502 }
503 }
504  
505 ## ------------------------------------------------------
506 # 'walktoken' : process all token flavours
507 ## ------------------------------------------------------
508 sub walktoken
509 {
510 40 ▶ my ($self, $rulename, $token, $context) = @_;
Subroutine walktoken: called CC=39 SCAR=53.1
Pod undocumented
511  
512 # if ( $rulename eq "EOF")
513 # {
514 # printf "found!\n";
515 # }
516  
517 40 my $alternative = retrieveModifier( $token, 'alternative' );
518 40 my $cardinality = retrieveModifier( $token, 'cardinality' );
519  
520 40 my $mxToken = {};
521 40 my $comments = [];
522  
523 40 ▶ if (ref $token eq "HASH" && exists $token->{comment} && !$self->testoption('stripallcomments'))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $token eq "HASH" and exists $token->{"comment"} and not $self->testoption("stripallcomments")) 0 40 40
Condition33%2/6
ref $token eq "HASH" and exists $token->{"comment"} and not $self->testoption("stripallcomments")
!ll&&!rl&&r
4000
ref $token eq "HASH" and exists $token->{"comment"}
!ll&&!rl&&r
0400
MC/DC0%0/3
ref $token eq "HASH" and exists $token->{"comment"} and not $self->testoption("stripallcomments")
ref $token eq "HASH" exists $token->{"comment"} not $self->testoption("stripallcomments")
Truth table25%1/4
A and B and not(C)
A= ref $token eq "HASH"B= exists $token->{"comment"}C= not $self->testoption("stripallcomments")
ABCresult
0XX0
10X0
1100
1111
524 {
525 0 ▶ mydie "comment not a plain string in rule $rulename" if ref $token->{comment} ne "";
Branch: 1 branch
BranchTrueFalseTotal
if ref $token->{"comment"} ne "" 0 0 0
526 0 my $cl = $self->renderComment($rulename, [$token->{comment}]);
527 0 push (@$comments, @$cl);
528 }
529  
530 40 my $negation = retrieveModifier($token, 'negation');
531  
532 SWITCH:
533 {
534 40 ▶ (ref $token eq "") && do {
Branch: 1 branch
BranchTrueFalseTotal
ref $token eq "" and do { $mxToken = $self->processSubRule($token, $context); $mxToken = $self->createSubRule($rulename, $mxToken, $negation, $cardinality, $context); $negation = undef; last SWITCH } 0 40 40
535 0 $mxToken = $self->processSubRule($token, $context);
536 0 $mxToken = $self->createSubRule( $rulename, $mxToken, $negation, $cardinality, $context );
537 # reset 'negation' since it was processed by 'createSubRule'
538 0 $negation = undef;
539 0 last SWITCH;
540 };
541 40 ▶ (ref $token eq "HASH" && exists $token->{type} && $token->{type} eq "tokengroup") && do {
Branch: 1 branch
BranchTrueFalseTotal
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "tokengroup" and do { $mxToken = $self->walkgroup($rulename, $token, $context); $mxToken = $self->createSubRule($rulename, $mxToken, $negation, $cardinality, $context); $negation = undef; last SWITCH } 9 31 40
Condition83%5/6
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "tokengroup"
!ll&&!rl&&r
1309
ref $token eq "HASH" and exists $token->{"type"}
!ll&&!rl&&r
0139
MC/DC66%2/3
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "tokengroup"
ref $token eq "HASH" exists $token->{"type"} $token->{"type"} eq "tokengroup"
Truth table75%3/4
A and B and C
A= ref $token eq "HASH"B= exists $token->{"type"}C= $token->{"type"} eq "tokengroup"
ABCresult
0XX0
10X0
1100
1111
542 9 $mxToken = $self->walkgroup($rulename, $token, $context);
543 9 $mxToken = $self->createSubRule( $rulename, $mxToken, $negation, $cardinality, $context );
544 # reset 'negation' since it was processed by 'createSubRule'
545 9 $negation = undef;
546 9 last SWITCH;
547 };
548 31 ▶ (ref $token eq "HASH" && exists $token->{type} && $token->{type} eq "rulegroup") && do {
Branch: 1 branch
BranchTrueFalseTotal
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "rulegroup" and do { $mxToken = $self->walkgroup($rulename, $token, $context); delete $mxToken->{"isLexeme"}; $mxToken = $self->createSubRule($rulename, $mxToken, $negation, $cardinality, $context); $negation = undef; last SWITCH } 0 31 31
Condition66%4/6
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "rulegroup"
!ll&&!rl&&r
1300
ref $token eq "HASH" and exists $token->{"type"}
!ll&&!rl&&r
0130
MC/DC0%0/3
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "rulegroup"
ref $token eq "HASH" exists $token->{"type"} $token->{"type"} eq "rulegroup"
Truth table50%2/4
A and B and C
A= ref $token eq "HASH"B= exists $token->{"type"}C= $token->{"type"} eq "rulegroup"
ABCresult
0XX0
10X0
1100
1111
549 0 $mxToken = $self->walkgroup($rulename, $token, $context);
550 # strip lexeme status off rule groups
551 0 delete $mxToken->{isLexeme};
552 0 $mxToken = $self->createSubRule( $rulename, $mxToken, $negation, $cardinality, $context );
553 # reset 'negation' since it was processed by 'createSubRule'
554 0 $negation = undef;
555 0 last SWITCH;
556 };
557 31 ▶ (ref $token eq "HASH" && exists $token->{type} && $token->{type} eq "literal") && do {
Branch: 1 branch
BranchTrueFalseTotal
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "literal" and do { my $tokenvalue = $token->{"value"}; $mxToken = {"type", "literal", "value", $tokenvalue, "isLexeme", 1}; my $class4list = convertLiteralToClass($tokenvalue); $mxToken->{"class4list"} = $class4list if defined $class4list; last SWITCH } 21 10 31
Condition83%5/6
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "literal"
!ll&&!rl&&r
1921
ref $token eq "HASH" and exists $token->{"type"}
!ll&&!rl&&r
0130
MC/DC66%2/3
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "literal"
ref $token eq "HASH" exists $token->{"type"} $token->{"type"} eq "literal"
Truth table75%3/4
A and B and C
A= ref $token eq "HASH"B= exists $token->{"type"}C= $token->{"type"} eq "literal"
ABCresult
0XX0
10X0
1100
1111
558 21 my $tokenvalue = $token->{value};
559 # if ( !$self->testoption('matchcaseinsensitive') || !$self->isAlphaLiteral($tokenvalue) )
560 # {
561 21 $mxToken = { type => 'literal', value => $tokenvalue, isLexeme => 1 };
562 21 my $class4list = convertLiteralToClass($tokenvalue);
563 21 ▶ $mxToken->{class4list} = $class4list if defined $class4list;
Branch: 1 branch
BranchTrueFalseTotal
if defined $class4list 18 3 21
564 # }
565 # else
566 # {
567 # my $literaltoken = generatematchcaseinsensitive($tokenvalue);
568 # delete $self->{generationoptions}{matchcaseinsensitive};
569 # $mxToken = $self->walkgroup($rulename, $literaltoken, $context);
570 # $mxToken = $self->createSubRule( $rulename, $mxToken, $negation, $cardinality, $context );
571 # $self->{generationoptions}{matchcaseinsensitive} = 'true';
572 # }
573 21 last SWITCH;
574 };
575 10 ▶ (ref $token eq "HASH" && exists $token->{type} && $token->{type} eq "class") && do {
Branch: 1 branch
BranchTrueFalseTotal
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "class" and do { $mxToken = {"type", "class", "value", $token->{"value"}, "isLexeme", 1, "class4list", $token->{"value"}}; last SWITCH } 9 1 10
Condition66%4/6
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "class"
!ll&&!rl&&r
109
ref $token eq "HASH" and exists $token->{"type"}
!ll&&!rl&&r
019
MC/DC33%1/3
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "class"
ref $token eq "HASH" exists $token->{"type"} $token->{"type"} eq "class"
Truth table50%2/4
A and B and C
A= ref $token eq "HASH"B= exists $token->{"type"}C= $token->{"type"} eq "class"
ABCresult
0XX0
10X0
1100
1111
576 9 $mxToken = { type => 'class', value => $token->{value}, isLexeme => 1, class4list => $token->{value} };
577 9 last SWITCH;
578 };
579 1 ▶ (ref $token eq "HASH" && exists $token->{type} && $token->{type} eq "regex") && do {
Branch: 1 branch
BranchTrueFalseTotal
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "regex" and do { $mxToken = {"type", "unsupported", "msg", "can't convert arbitrary regex to Marpa in $rulename"}; last SWITCH } 0 1 1
Condition33%2/6
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "regex"
!ll&&!rl&&r
100
ref $token eq "HASH" and exists $token->{"type"}
!ll&&!rl&&r
010
MC/DC0%0/3
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "regex"
ref $token eq "HASH" exists $token->{"type"} $token->{"type"} eq "regex"
Truth table25%1/4
A and B and C
A= ref $token eq "HASH"B= exists $token->{"type"}C= $token->{"type"} eq "regex"
ABCresult
0XX0
10X0
1100
1111
580 0 $mxToken = { type => 'unsupported', msg => "can't convert arbitrary regex to Marpa in $rulename" };
581 0 last SWITCH;
582 };
583 1 ▶ (ref $token eq "HASH" && exists $token->{type} && $token->{type} eq "range") && do {
Branch: 1 branch
BranchTrueFalseTotal
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "range" and do { $mxToken = convertRangeToClass($token); last SWITCH } 0 1 1
Condition33%2/6
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "range"
!ll&&!rl&&r
100
ref $token eq "HASH" and exists $token->{"type"}
!ll&&!rl&&r
010
MC/DC0%0/3
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "range"
ref $token eq "HASH" exists $token->{"type"} $token->{"type"} eq "range"
Truth table25%1/4
A and B and C
A= ref $token eq "HASH"B= exists $token->{"type"}C= $token->{"type"} eq "range"
ABCresult
0XX0
10X0
1100
1111
584 0 $mxToken = convertRangeToClass($token);
585 0 last SWITCH;
586 };
587 1 ▶ (ref $token eq "HASH" && exists $token->{type} && $token->{type} eq "value") && do {
Branch: 1 branch
BranchTrueFalseTotal
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "value" and do { $mxToken = {"type", "unsupported", "msg", "can't convert 'value' to Marpa in $rulename"}; last SWITCH } 0 1 1
Condition33%2/6
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "value"
!ll&&!rl&&r
100
ref $token eq "HASH" and exists $token->{"type"}
!ll&&!rl&&r
010
MC/DC0%0/3
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "value"
ref $token eq "HASH" exists $token->{"type"} $token->{"type"} eq "value"
Truth table25%1/4
A and B and C
A= ref $token eq "HASH"B= exists $token->{"type"}C= $token->{"type"} eq "value"
ABCresult
0XX0
10X0
1100
1111
588 0 $mxToken = { type => 'unsupported', msg => "can't convert 'value' to Marpa in $rulename" };
589 0 last SWITCH;
590 };
591 1 ▶ (ref $token eq "HASH" && exists $token->{token}) && do {
Branch: 1 branch
BranchTrueFalseTotal
ref $token eq "HASH" and exists $token->{"token"} and do { $mxToken = $self->walktoken($rulename, $token->{"token"}, $context); last SWITCH } 1 0 1
Condition33%1/3
ref $token eq "HASH" and exists $token->{"token"}
!ll&&!rl&&r
001
MC/DC0%0/2
ref $token eq "HASH" and exists $token->{"token"}
ref $token eq "HASH" exists $token->{"token"}
Truth table33%1/3
A and B
A= ref $token eq "HASH"B= exists $token->{"token"}
ABresult
0X0
100
111
592 1 $mxToken = $self->walktoken($rulename, $token->{token}, $context);
593 1 last SWITCH;
594 };
595 0 ▶ (ref $token eq "HASH" && exists $token->{action}) && do {
Branch: 1 branch
BranchTrueFalseTotal
ref $token eq "HASH" and exists $token->{"action"} and do { $mxToken = {"type", "ignore", "msg", "embedded action in $rulename"}; last SWITCH } 0 0 0
Condition0%0/3
ref $token eq "HASH" and exists $token->{"action"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $token eq "HASH" and exists $token->{"action"}
ref $token eq "HASH" exists $token->{"action"}
Truth table0%0/3
A and B
A= ref $token eq "HASH"B= exists $token->{"action"}
ABresult
0X0
100
111
596 0 $mxToken = { type => 'ignore', msg => "embedded action in $rulename" };
597 0 last SWITCH;
598 };
599 0 ▶ (ref $token eq "HASH" && exists $token->{comment}) && do {
Branch: 1 branch
BranchTrueFalseTotal
ref $token eq "HASH" and exists $token->{"comment"} and do { $mxToken = {"type", "ignore", "msg", "comment in $rulename"}; last SWITCH } 0 0 0
Condition0%0/3
ref $token eq "HASH" and exists $token->{"comment"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $token eq "HASH" and exists $token->{"comment"}
ref $token eq "HASH" exists $token->{"comment"}
Truth table0%0/3
A and B
A= ref $token eq "HASH"B= exists $token->{"comment"}
ABresult
0X0
100
111
600 0 $mxToken = { type => 'ignore', msg => "comment in $rulename" };
601 0 last SWITCH;
602 };
603 0 do {
604 0 $self->abortWithError( "can't process token for rule $rulename", $token );
605 0 last SWITCH;
606 };
607 }
608  
609 ##
610 # apply 'negation' to every token type but groups and subrules
611 ##
612 40 ▶ if ( defined $negation )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $negation) 1 39 40
613 {
614 1 $mxToken = $self->negateElement($rulename, $mxToken, $context);
615 1 $negation = undef;
616 }
617  
618 ###
619 # create explicit subrule for
620 # - nonterminals
621 # - subgroups
622 # annotated for cardinality
623 ##
624 40 ▶ if ( defined $cardinality )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cardinality) 0 40 40
625 {
626 0 $mxToken = $self->createSubRule( $rulename, $mxToken, undef, $cardinality, $context );
627  
628 # strip lexeme status off annotated subrules
629 0 delete $mxToken->{isLexeme};
630 }
631  
632 40 ▶ $mxToken->{alternative} = 'A' if defined $alternative;
Branch: 1 branch
BranchTrueFalseTotal
if defined $alternative 0 40 40
633 40 ▶ $mxToken->{comments} = $comments if scalar @$comments > 0 && !$self->testoption('stripallcomments');
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$comments > 0 and not $self->testoption("stripallcomments") 0 40 40
Condition33%1/3
scalar @$comments > 0 and not $self->testoption("stripallcomments")
!ll&&!rl&&r
4000
MC/DC0%0/2
scalar @$comments > 0 and not $self->testoption("stripallcomments")
scalar @$comments > 0 not $self->testoption("stripallcomments")
Truth table33%1/3
A and not(B)
A= scalar @$comments > 0B= not $self->testoption("stripallcomments")
ABresult
0X0
100
111
634  
635 40 return $mxToken;
636 }
637  
638 sub walknonterminal
639 {
640 0 ▶ my ( $self, $rulename, $nonterminal, $context ) = @_;
Subroutine walknonterminal: not called CC=3 SCAR=24.8
Pod undocumented
641  
642 0 ▶ mydie "nonterminal is not a hash for rule $rulename" if ref $nonterminal ne "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $nonterminal ne "HASH" 0 0 0
643  
644 0 my $mxToken = {};
645  
646 SWITCH:
647 {
648 0 ▶ (exists $nonterminal->{rhs}) && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $nonterminal->{"rhs"} and do { my $rhs = $nonterminal->{"rhs"}; $mxToken = $self->walktoken($rulename, $rhs, $context); last SWITCH } 0 0 0
649 0 my $rhs = $nonterminal->{rhs};
650 0 $mxToken = $self->walktoken($rulename, $rhs, $context);
651 0 last SWITCH;
652 };
653 0 do {
654 0 $self->abortWithError( "can't process nonterminal for rule $rulename", $nonterminal );
655 0 last SWITCH;
656 };
657 }
658  
659 0 return $mxToken;
660 }
661  
662 ## -------------------------------------------------------------------------------
663 # 'retagSimpleRule' : check if the cardinality can be added to 'mxtoken'.
664 # Rules :
665 # - '?' is consumed by 'opt_' subrules, so we have to reject it here
666 # - 'mxtoken' must be a subrule of 'rule'
667 # - the parent rule must consist of a single element
668 # (responsibility of the caller to verify)
669 ## -------------------------------------------------------------------------------
670 sub retagSimpleRule
671 {
672 6 ▶ my ( $self, $rulename, $mxtoken, $cardinality, $context, $options ) = @_;
Subroutine retagSimpleRule: called CC=36 SCAR=58.1
Pod undocumented
673  
674 6 ▶ mydie "token must be a hash in $rulename" if ref $mxtoken ne "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $mxtoken ne "HASH" 0 6 6
675 6 ▶ return 0 if $cardinality eq "?";
Branch: 1 branch
BranchTrueFalseTotal
if $cardinality eq "?" 0 6 6
676  
677 6 ▶ $cardinality =~ s/([+*])\?/$1/ if $cardinality =~ /[+*]\?/ && $self->testoption('shiftlazytogreedy');
Branch: 1 branch
BranchTrueFalseTotal
if $cardinality =~ /[+*]\?/ and $self->testoption("shiftlazytogreedy") 0 6 6
Condition33%1/3
$cardinality =~ /[+*]\?/ and $self->testoption("shiftlazytogreedy")
!ll&&!rl&&r
600
MC/DC0%0/2
$cardinality =~ /[+*]\?/ and $self->testoption("shiftlazytogreedy")
$cardinality =~ /[+*]\?/ $self->testoption("shiftlazytogreedy")
Truth table33%1/3
A and B
A= $cardinality =~ /[+*]\?/B= $self->testoption("shiftlazytogreedy")
ABresult
0X0
100
111
678 6 ▶ mydie sprintf "lazy quantifier %s in rule %s not supported by Marpa", $cardinality, $rulename if $cardinality =~ /[+*]\?/;
Branch: 1 branch
BranchTrueFalseTotal
if $cardinality =~ /[+*]\?/ 0 6 6
679  
680 6 ▶ my $abortatfirstlevel = defined $options && ref $options eq "HASH" && exists $options->{abortatfirstlevel};
Condition66%4/6
defined $options && ref $options eq 'HASH' && exists $options->{'abortatfirstlevel'}
!ll&&!rl&&r
105
defined $options and ref $options eq "HASH"
!ll&&!rl&&r
105
MC/DC33%1/3
defined $options && ref $options eq 'HASH' && exists $options->{'abortatfirstlevel'}
defined $options ref $options eq "HASH" exists $options->{'abortatfirstlevel'}
Truth table50%2/4
A and B && C
A= defined $optionsB= ref $options eq "HASH"C= exists $options->{'abortatfirstlevel'}
ABCresult
0XX0
10X0
1100
1111
681  
682 SWITCH: {
683 6 ▶ (exists $mxtoken->{rhs}) && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $mxtoken->{"rhs"} and do { my $rhs = $mxtoken->{"rhs"}; mydie("'rhs' must be a hash in $rulename") if ref $rhs ne "HASH"; mydie("'token' must be a scalar in 'rhs' in rule $rulename") if not exists $rhs->{"token"} or ref $rhs->{"token"} ne ""; return 0 unless defined $self->checkMarpaRuleExists($rhs->{"token"}); my $rc = 0; if ($abortatfirstlevel) { my $subrulename = $rhs->{"token"}; return 0 unless $self->isSubrule($rulename, $subrulename); my $rule = $self->getMarpaRule($rulename, $subrulename); if ($cardinality ne "?") { $rule->{"cardinality"} = $cardinality; delete $rule->{"class4list"}; $rc = 1; } ; } else { my $subrulename = $rhs->{"token"}; return 0 unless $self->isSubrule($rulename, $subrulename); my $rule = $self->getMarpaRule($rulename, $subrulename); $rc = $self->retagSimpleRule($rulename, $rule, $cardinality, $context, $options); if ($rc) { mydie("can't retag subrule $subrulename twice in rule $rulename") if exists $rule->{"cardinality"}; $rule->{"cardinality"} = $cardinality; delete $rule->{"class4list"}; } ; } ; return $rc; last SWITCH } 5 1 6
684 5 my $rhs = $mxtoken->{rhs};
685  
686 5 ▶ mydie "'rhs' must be a hash in $rulename" if ref $rhs ne "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $rhs ne "HASH" 0 5 5
687 5 ▶ mydie "'token' must be a scalar in 'rhs' in rule $rulename" if !exists $rhs->{token} || ref $rhs->{token} ne "";
Branch: 1 branch
BranchTrueFalseTotal
if not exists $rhs->{"token"} or ref $rhs->{"token"} ne "" 0 5 5
Condition33%1/3
not exists $rhs->{"token"} or ref $rhs->{"token"} ne ""
l!l&&r!l&&!r
005
MC/DC0%0/2
not exists $rhs->{"token"} or ref $rhs->{"token"} ne ""
not exists $rhs->{"token"} ref $rhs->{"token"} ne ""
Truth table33%1/3
not(A) or B
A= not exists $rhs->{"token"}B= ref $rhs->{"token"} ne ""
ABresult
1X1
011
000
688  
689 5 ▶ return 0 if !defined $self->checkMarpaRuleExists($rhs->{token});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->checkMarpaRuleExists($rhs->{"token"}) 0 5 5
690  
691 5 my $rc = 0;
692  
693 5 ▶ if ($abortatfirstlevel)
Branch: 1 branch
BranchTrueFalseTotal
if ($abortatfirstlevel) { } 5 0 5
694 {
695 5 my $subrulename = $rhs->{token};
696  
697 # don't push cardinality to a rule unless it is a subrule of the parent
698 5 ▶ return 0 if !$self->isSubrule($rulename, $subrulename);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isSubrule($rulename, $subrulename) 0 5 5
699  
700 5 my $rule = $self->getMarpaRule($rulename, $subrulename);
701 5 ▶ if ($cardinality ne "?")
Branch: 1 branch
BranchTrueFalseTotal
if ($cardinality ne "?") 5 0 5
702 {
703 5 $rule->{cardinality} = $cardinality;
704 # cardinality-tagged tokens loose 'class4list' status
705 5 delete $rule->{class4list};
706 5 $rc = 1;
707 }
708 }
709 else
710 {
711 0 my $subrulename = $rhs->{token};
712  
713 # don't push cardinality to a rule unless it is a subrule of the parent
714 0 ▶ return 0 if !$self->isSubrule($rulename, $subrulename);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isSubrule($rulename, $subrulename) 0 0 0
715  
716 0 my $rule = $self->getMarpaRule($rulename, $subrulename);
717 0 $rc = $self->retagSimpleRule($rulename, $rule, $cardinality, $context, $options);
718 0 ▶ if ($rc)
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) 0 0 0
719 {
720 0 ▶ mydie "can't retag subrule $subrulename twice in rule $rulename" if exists $rule->{cardinality};
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{"cardinality"} 0 0 0
721 0 $rule->{cardinality} = $cardinality;
722 # cardinality-tagged tokens loose 'class4list' status
723 0 delete $rule->{class4list};
724 }
725 }
726 5 return $rc;
727 0 last SWITCH;
728 };
729 1 ▶ (exists $mxtoken->{type} && $mxtoken->{type} eq "group") && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $mxtoken->{"type"} and $mxtoken->{"type"} eq "group" and do { mydie("group rhs must contain 'list' in $rulename") unless exists $mxtoken->{"list"}; my $ec = 0; foreach my $al (@{$$mxtoken{"list"};}) { mydie("alternative list is not an array in rule $rulename") if ref $al ne "HASH" or not exists $al->{"list"}; foreach my $le (@{$$al{"list"};}) { mydie("alternative element is not simple in rule $rulename") if exists $le->{"type"} and not $le->{"type"} =~ /literal|class/; $ec += 1; } ; } ; return $ec <= 1; last SWITCH } 0 1 1
Condition33%1/3
exists $mxtoken->{"type"} and $mxtoken->{"type"} eq "group"
!ll&&!rl&&r
010
MC/DC0%0/2
exists $mxtoken->{"type"} and $mxtoken->{"type"} eq "group"
exists $mxtoken->{"type"} $mxtoken->{"type"} eq "group"
Truth table33%1/3
A and B
A= exists $mxtoken->{"type"}B= $mxtoken->{"type"} eq "group"
ABresult
0X0
100
111
730 0 ▶ mydie "group rhs must contain 'list' in $rulename" if !exists $mxtoken->{list};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $mxtoken->{"list"} 0 0 0
731 0 my $ec = 0;
732 0 for my $al (@{$mxtoken->{list}})
733 {
734 0 ▶ mydie "alternative list is not an array in rule $rulename" if ref $al ne "HASH" || !exists $al->{list};
Branch: 1 branch
BranchTrueFalseTotal
if ref $al ne "HASH" or not exists $al->{"list"} 0 0 0
Condition0%0/3
ref $al ne "HASH" or not exists $al->{"list"}
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $al ne "HASH" or not exists $al->{"list"}
ref $al ne "HASH" not exists $al->{"list"}
Truth table0%0/3
A or not(B)
A= ref $al ne "HASH"B= not exists $al->{"list"}
ABresult
1X1
011
000
735 0 for my $le (@{$al->{list}})
736 {
737 0 ▶ mydie "alternative element is not simple in rule $rulename" if exists $le->{type} && $le->{type} !~ /literal|class/;
Branch: 1 branch
BranchTrueFalseTotal
if exists $le->{"type"} and not $le->{"type"} =~ /literal|class/ 0 0 0
Condition0%0/3
exists $le->{"type"} and not $le->{"type"} =~ /literal|class/
!ll&&!rl&&r
000
MC/DC0%0/2
exists $le->{"type"} and not $le->{"type"} =~ /literal|class/
exists $le->{"type"} not $le->{"type"} =~ /literal|class/
Truth table0%0/3
A and not(B)
A= exists $le->{"type"}B= not $le->{"type"} =~ /literal|class/
ABresult
0X0
100
111
738 0 $ec += 1;
739 }
740 }
741 0 return $ec <= 1;
742 0 last SWITCH;
743 };
744 1 ▶ (exists $mxtoken->{type} && $mxtoken->{type} eq "literal") && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $mxtoken->{"type"} and $mxtoken->{"type"} eq "literal" and do { $mxtoken->{"cardinality"} = $cardinality; return 1; last SWITCH } 0 1 1
Condition33%1/3
exists $mxtoken->{"type"} and $mxtoken->{"type"} eq "literal"
!ll&&!rl&&r
010
MC/DC0%0/2
exists $mxtoken->{"type"} and $mxtoken->{"type"} eq "literal"
exists $mxtoken->{"type"} $mxtoken->{"type"} eq "literal"
Truth table33%1/3
A and B
A= exists $mxtoken->{"type"}B= $mxtoken->{"type"} eq "literal"
ABresult
0X0
100
111
745 0 $mxtoken->{cardinality} = $cardinality;
746 0 return 1;
747 0 last SWITCH;
748 };
749 1 ▶ (exists $mxtoken->{type} && $mxtoken->{type} eq "class") && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $mxtoken->{"type"} and $mxtoken->{"type"} eq "class" and do { mydie("'class' can't be multiplied twice in $rulename") if exists $mxtoken->{"cardinality"}; $mxtoken->{"cardinality"} = $cardinality; return 1; last SWITCH } 1 0 1
Condition33%1/3
exists $mxtoken->{"type"} and $mxtoken->{"type"} eq "class"
!ll&&!rl&&r
001
MC/DC0%0/2
exists $mxtoken->{"type"} and $mxtoken->{"type"} eq "class"
exists $mxtoken->{"type"} $mxtoken->{"type"} eq "class"
Truth table33%1/3
A and B
A= exists $mxtoken->{"type"}B= $mxtoken->{"type"} eq "class"
ABresult
0X0
100
111
750 1 ▶ mydie "'class' can't be multiplied twice in $rulename" if exists $mxtoken->{cardinality};
Branch: 1 branch
BranchTrueFalseTotal
if exists $mxtoken->{"cardinality"} 0 1 1
751 1 $mxtoken->{cardinality} = $cardinality;
752 1 return 1;
753 0 last SWITCH;
754 };
755 0 ▶ (exists $mxtoken->{type} && $mxtoken->{type} eq "negatedclass") && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $mxtoken->{"type"} and $mxtoken->{"type"} eq "negatedclass" and do { mydie("'negatedclass' can't be multiplied twice in $rulename") if exists $mxtoken->{"cardinality"}; $mxtoken->{"cardinality"} = $cardinality; return 1; last SWITCH } 0 0 0
Condition0%0/3
exists $mxtoken->{"type"} and $mxtoken->{"type"} eq "negatedclass"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $mxtoken->{"type"} and $mxtoken->{"type"} eq "negatedclass"
exists $mxtoken->{"type"} $mxtoken->{"type"} eq "negatedclass"
Truth table0%0/3
A and B
A= exists $mxtoken->{"type"}B= $mxtoken->{"type"} eq "negatedclass"
ABresult
0X0
100
111
756 0 ▶ mydie "'negatedclass' can't be multiplied twice in $rulename" if exists $mxtoken->{cardinality};
Branch: 1 branch
BranchTrueFalseTotal
if exists $mxtoken->{"cardinality"} 0 0 0
757 0 $mxtoken->{cardinality} = $cardinality;
758 0 return 1;
759 0 last SWITCH;
760 };
761 0 do {
762 0 $self->abortWithError( "don't know how to analyze element in $rulename", $mxtoken );
763 0 last SWITCH;
764 };
765 }
766  
767 0 return 0;
768 }
769  
770 ## ------------------------------------------------------
771 # removeCaseEquivalentBranches :
772 # go over the branches from the alternative lists.
773 # if 2 branches
774 # - consist of exactly 1 symbol
775 # - are case-equivalent
776 # then remove one of them.
777 ## ------------------------------------------------------
778 sub removeCaseEquivalentBranches
779 {
780 23 ▶ my ($self, $alternativelists) = @_;
Subroutine removeCaseEquivalentBranches: called CC=12 SCAR=25.5
Pod undocumented
781  
782 23 ▶ return $alternativelists if scalar @$alternativelists < 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$alternativelists < 2 17 6 23
783  
784 6 my $filteredlist = [];
785 6 my $filtered = 0;
786 6 my $literal;
787  
788 6 for my $branch (@$alternativelists)
789 {
790 17 ▶ if ( !exists $branch->{list})
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $branch->{'list'}) { } 0 17 17
791 {
792 0 push @$filteredlist, $branch;
793 }
794 else
795 {
796 17 my $branchlist = $branch->{list};
797 17 ▶ if (scalar @$branchlist > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$branchlist > 1) { } 5 12 17
798 {
799 5 push @$filteredlist, $branch;
800 }
801 else
802 {
803 12 my $rhs = $branchlist->[0];
804 12 my $matchfound = 0;
805  
806 12 ▶ if ( exists $rhs->{type} && $rhs->{type} eq 'literal' )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rhs->{"type"} and $rhs->{"type"} eq "literal") 4 8 12
Condition100%3/3
exists $rhs->{"type"} and $rhs->{"type"} eq "literal"
!ll&&!rl&&r
714
MC/DC100%2/2
exists $rhs->{"type"} and $rhs->{"type"} eq "literal"
exists $rhs->{"type"} $rhs->{"type"} eq "literal"
Truth table100%3/3
A and B
A= exists $rhs->{"type"}B= $rhs->{"type"} eq "literal"
ABresult
0X0
100
111
807 {
808 4 my $newliteral = $rhs->{value};
809  
810 4 ▶ if (defined $literal)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $literal) { } 2 2 4
811 {
812 2 my $tmp1 = uc $literal;
813 2 my $tmp2 = uc $newliteral;
814 2 ▶ $matchfound = $tmp1 ne "" && $tmp1 eq $tmp2;
Condition33%1/3
$tmp1 ne '' && $tmp1 eq $tmp2
!ll&&!rl&&r
020
MC/DC0%0/2
$tmp1 ne '' && $tmp1 eq $tmp2
$tmp1 ne '' $tmp1 eq $tmp2
Truth table33%1/3
A && B
A= $tmp1 ne ''B= $tmp1 eq $tmp2
ABresult
0X0
100
111
815 2 ▶ $filtered = 1 if $matchfound;
Branch: 1 branch
BranchTrueFalseTotal
if $matchfound 0 2 2
816 }
817 else
818 {
819 2 $literal = $newliteral;
820 }
821 }
822  
823 12 ▶ push @$filteredlist, $branch if !$matchfound;
Branch: 1 branch
BranchTrueFalseTotal
unless $matchfound 12 0 12
824 }
825 }
826 }
827  
828 6 ▶ return $alternativelists if !$filtered;
Branch: 1 branch
BranchTrueFalseTotal
unless $filtered 6 0 6
829  
830 0 return $filteredlist;
831 }
832  
833 ## ------------------------------------------------------
834 # 'processRightSides' converts a single list of tokens some of which
835 # are tagged as 'alternative' into (possibly) multiple lists
836 # each of which is an alternative for a group or rule
837 ## ------------------------------------------------------
838 sub processRightSides
839 {
840 23 ▶ my ( $self, $rulename, $rhslist, $context ) = @_;
Subroutine processRightSides: called CC=58 SCAR=53.9
Pod undocumented
841  
842 # symbol lists of alternative branches
843 23 my $alternativelists = [];
844 # current alternative branch
845 23 my $currentlist = [];
846 # number of symbols in current alternative branch
847 23 my $alternativelength = 0;
848  
849 23 my $metalist = { comments => [], actions => [] };
850  
851 # status tracking for conversion to keyword, lexeme or class
852 23 my $class4list = { status => 0, value => "" };
853 23 my $class4group = { status => 0, value => "" };
854 23 my $groupisLexeme = { status => 0 };
855 23 my $keywordfragment = { status => 0 };
856  
857 # if ( $rulename eq "outer_join_sign")
858 # {
859 # printf "found!\n";
860 # }
861  
862 23 for my $e (@$rhslist)
863 {
864 60 ▶ $e = $e->{rhs} if ref $e eq "HASH" && exists $e->{rhs} && !exists $e->{token};
Branch: 1 branch
BranchTrueFalseTotal
if ref $e eq "HASH" and exists $e->{"rhs"} and not exists $e->{"token"} 37 23 60
Condition66%4/6
ref $e eq "HASH" and exists $e->{"rhs"} and not exists $e->{"token"}
!ll&&!rl&&r
23037
ref $e eq "HASH" and exists $e->{"rhs"}
!ll&&!rl&&r
02337
MC/DC33%1/3
ref $e eq "HASH" and exists $e->{"rhs"} and not exists $e->{"token"}
ref $e eq "HASH" exists $e->{"rhs"} not exists $e->{"token"}
Truth table50%2/4
A and B and not(C)
A= ref $e eq "HASH"B= exists $e->{"rhs"}C= not exists $e->{"token"}
ABCresult
0XX0
10X0
1100
1111
865 60 ▶ $e = { token => $e } if ref $e eq "HASH" && exists $e->{type} && exists $e->{definition};
Branch: 1 branch
BranchTrueFalseTotal
if ref $e eq "HASH" and exists $e->{"type"} and exists $e->{"definition"} 4 56 60
Condition66%4/6
ref $e eq "HASH" and exists $e->{"type"} and exists $e->{"definition"}
!ll&&!rl&&r
5604
ref $e eq "HASH" and exists $e->{"type"}
!ll&&!rl&&r
0564
MC/DC33%1/3
ref $e eq "HASH" and exists $e->{"type"} and exists $e->{"definition"}
ref $e eq "HASH" exists $e->{"type"} exists $e->{"definition"}
Truth table50%2/4
A and B and C
A= ref $e eq "HASH"B= exists $e->{"type"}C= exists $e->{"definition"}
ABCresult
0XX0
10X0
1100
1111
866  
867 60 my $negation = retrieveModifier($e, 'negation');
868 60 my $cardinality = retrieveModifier($e, 'cardinality');
869  
870 60 ▶ if (exists $e->{comment} || exists $e->{action})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $e->{"comment"} or exists $e->{"action"}) 0 60 60
Condition33%1/3
exists $e->{"comment"} or exists $e->{"action"}
l!l&&r!l&&!r
0060
MC/DC0%0/2
exists $e->{"comment"} or exists $e->{"action"}
exists $e->{"comment"} exists $e->{"action"}
Truth table33%1/3
A or B
A= exists $e->{"comment"}B= exists $e->{"action"}
ABresult
1X1
011
000
871 {
872 0 ▶ if (exists $e->{comment} && !$self->testoption('stripallcomments'))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $e->{"comment"} and not $self->testoption("stripallcomments")) 0 0 0
Condition0%0/3
exists $e->{"comment"} and not $self->testoption("stripallcomments")
!ll&&!rl&&r
000
MC/DC0%0/2
exists $e->{"comment"} and not $self->testoption("stripallcomments")
exists $e->{"comment"} not $self->testoption("stripallcomments")
Truth table0%0/3
A and not(B)
A= exists $e->{"comment"}B= not $self->testoption("stripallcomments")
ABresult
0X0
100
111
873 {
874 0 push @{$metalist->{comments}}, $self->renderComment($rulename, [$e->{comment}]);
875 0 $metalist->{state} = 1;
876 }
877  
878 0 ▶ if (exists $e->{action} && !$self->testoption('stripactions'))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $e->{"action"} and not $self->testoption("stripactions")) 0 0 0
Condition0%0/3
exists $e->{"action"} and not $self->testoption("stripactions")
!ll&&!rl&&r
000
MC/DC0%0/2
exists $e->{"action"} and not $self->testoption("stripactions")
exists $e->{"action"} not $self->testoption("stripactions")
Truth table0%0/3
A and not(B)
A= exists $e->{"action"}B= not $self->testoption("stripactions")
ABresult
0X0
100
111
879 {
880 0 ▶ push @{$metalist->{actions}}, $self->testoption('embedactions') ? $e->{action} : $self->renderComment($rulename, [$e->{action}]);
Branch: 1 branch
BranchTrueFalseTotal
$self->testoption('embedactions') ? : 0 0 0
881 0 $metalist->{state} = 1;
882 }
883  
884 ##
885 # skip the rest of processing for comment/action tokens
886 ##
887 0 next;
888 }
889  
890 60 ▶ if (ref $e eq "HASH" && !exists $e->{token})
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e eq "HASH" and not exists $e->{"token"}) 0 60 60
Condition33%1/3
ref $e eq "HASH" and not exists $e->{"token"}
!ll&&!rl&&r
0600
MC/DC0%0/2
ref $e eq "HASH" and not exists $e->{"token"}
ref $e eq "HASH" not exists $e->{"token"}
Truth table33%1/3
A and not(B)
A= ref $e eq "HASH"B= not exists $e->{"token"}
ABresult
0X0
100
111
891 {
892 0 ▶ $self->abortWithError( "generic regex rules not supported in rule $rulename", $e ) if exists $e->{type} && $e->{type} eq "regex";
Branch: 1 branch
BranchTrueFalseTotal
if exists $e->{"type"} and $e->{"type"} eq "regex" 0 0 0
Condition0%0/3
exists $e->{"type"} and $e->{"type"} eq "regex"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $e->{"type"} and $e->{"type"} eq "regex"
exists $e->{"type"} $e->{"type"} eq "regex"
Truth table0%0/3
A and B
A= exists $e->{"type"}B= $e->{"type"} eq "regex"
ABresult
0X0
100
111
893 0 $self->abortWithError( "INTERNAL: 'e' must be a hash and contain 'token' in rule $rulename", $e );
894 }
895  
896 60 my $alternative = retrieveModifier($e, 'alternative');
897  
898 60 ▶ if (defined $alternative)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alternative) { } 11 49 60
899 {
900 11 ▶ if (scalar @$currentlist > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$currentlist > 0) 11 0 11
901 {
902 ##
903 # close the current alternative list
904 ##
905 11 my $newlist;
906  
907 ##
908 # replace a keyword built from lower/upper case character classes by a literal
909 ##
910 11 my $keyword = $keywordfragment->{value};
911 11 ▶ if ($self->testoption("buildkeywords") && $keywordfragment->{status} == 0 && $keyword =~ /^[a-z]/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->testoption('buildkeywords') and $keywordfragment->{'status'} == 0 and $keyword =~ /^[a-z]/i) { } 0 11 11
Condition66%4/6
$self->testoption("buildkeywords") and $keywordfragment->{"status"} == 0 and $keyword =~ /^[a-z]/i
!ll&&!rl&&r
1010
$self->testoption("buildkeywords") and $keywordfragment->{"status"} == 0
!ll&&!rl&&r
0101
MC/DC0%0/3
$self->testoption("buildkeywords") and $keywordfragment->{"status"} == 0 and $keyword =~ /^[a-z]/i
$self->testoption("buildkeywords") $keywordfragment->{"status"} == 0 $keyword =~ /^[a-z]/i
Truth table50%2/4
A and B and C
A= $self->testoption("buildkeywords")B= $keywordfragment->{"status"} == 0C= $keyword =~ /^[a-z]/i
ABCresult
0XX0
10X0
1100
1111
912 {
913 0 $currentlist = [
914 { type => "literal", value => $keyword, isLexeme => 1 }
915 ];
916 0 $newlist = { list => $currentlist, isLexeme => 1 };
917 }
918 else
919 {
920 11 $newlist = { list => $currentlist };
921 11 ▶ $newlist->{class4list} = $class4list->{value} if $class4list->{status} != -1;
Branch: 1 branch
BranchTrueFalseTotal
if $class4list->{"status"} != -1 2 9 11
922 11 ▶ $newlist->{metalist} = $metalist if exists $metalist->{state};
Branch: 1 branch
BranchTrueFalseTotal
if exists $metalist->{"state"} 0 11 11
923 }
924  
925 11 push @$alternativelists, $newlist;
926 ##
927 # re-initialize the list state
928 ##
929 11 $currentlist = [];
930 11 $class4list = { status => 0, value => "" };
931 11 $metalist = { comments => [], actions => [] };
932 11 $keywordfragment = { status => 0 };
933 11 $alternativelength = 0;
934 }
935 }
936 else
937 {
938 ##
939 # a sequence of literals can't be mapped to a class, so we reset class4list
940 ##
941 49 ▶ if ($alternativelength > 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($alternativelength > 1) 12 37 49
942 {
943 12 $class4group->{status} = -1;
944 12 delete $class4group->{value};
945 }
946 }
947  
948 60 my $mxelement;
949  
950 60 ▶ if (ref $e->{token} eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e->{'token'} eq '') { } 21 39 60
951 {
952 ##
953 # translate a G4 subrule into a Marpa subrule
954 ##
955 21 my $symbol = $e->{token};
956 21 $mxelement = $self->processSubRule($symbol, $context);
957 }
958 else
959 {
960 39 ▶ $self->abortWithError( "can't process group for rule $rulename", $rhslist ) if ref $e ne "HASH" || !exists $e->{token};
Branch: 1 branch
BranchTrueFalseTotal
if ref $e ne "HASH" or not exists $e->{"token"} 0 39 39
Condition33%1/3
ref $e ne "HASH" or not exists $e->{"token"}
l!l&&r!l&&!r
0039
MC/DC0%0/2
ref $e ne "HASH" or not exists $e->{"token"}
ref $e ne "HASH" not exists $e->{"token"}
Truth table33%1/3
A or not(B)
A= ref $e ne "HASH"B= not exists $e->{"token"}
ABresult
1X1
011
000
961 39 $mxelement = $self->walktoken($rulename, $e->{token}, $context);
962 # ignore embedded comments/actions
963 39 ▶ next if exists $mxelement->{type} && $mxelement->{type} eq "ignore";
Branch: 1 branch
BranchTrueFalseTotal
if exists $mxelement->{"type"} and $mxelement->{"type"} eq "ignore" 0 39 39
Condition66%2/3
exists $mxelement->{"type"} and $mxelement->{"type"} eq "ignore"
!ll&&!rl&&r
9300
MC/DC0%0/2
exists $mxelement->{"type"} and $mxelement->{"type"} eq "ignore"
exists $mxelement->{"type"} $mxelement->{"type"} eq "ignore"
Truth table66%2/3
A and B
A= exists $mxelement->{"type"}B= $mxelement->{"type"} eq "ignore"
ABresult
0X0
100
111
964  
965 39 my $keywordfragment = $self->isKeywordFragment($rulename, $mxelement);
966 39 ▶ $mxelement->{keywordfragment} = $keywordfragment if defined $keywordfragment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $keywordfragment 23 16 39
967 }
968  
969 ##
970 # collect embedded comments in the alternative branch state
971 ##
972 60 ▶ if (exists $mxelement->{comments})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $mxelement->{"comments"}) 0 60 60
973 {
974 0 push (@{$metalist->{comments}}, @{$mxelement->{comments}});
975 0 $metalist->{state} = 1;
976 0 delete $mxelement->{comments};
977 0 ▶ next if scalar keys %$mxelement == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %$mxelement == 0 0 0 0
978 }
979  
980 ##
981 # CAVEAT: retrieve 'isLexeme' from $mxelement
982 ##
983 60 my $il = retrieveModifier($mxelement, 'isLexeme');
984 60 ▶ $groupisLexeme->{status} = -1 if !defined $il;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $il 31 29 60
985  
986 ##
987 # negation should not come from the token
988 ##
989 60 ▶ mydie "processRightsides : unexpected negation in 'mxelement'" if exists $mxelement->{negation};
Branch: 1 branch
BranchTrueFalseTotal
if exists $mxelement->{"negation"} 0 60 60
990  
991 60 ▶ if ( defined $negation )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $negation) 0 60 60
992 {
993 0 $mxelement = $self->negateElement($rulename, $mxelement, $context);
994 }
995  
996 ##
997 # CAVEAT: use 'cardinality' from $e since $mxtoken
998 # was computed without metainformation.
999 ##
1000 60 ▶ if (defined $cardinality)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cardinality) 10 50 60
1001 {
1002 ##
1003 # if both the parent and the sub rules are simple, we can propagate the cardinality
1004 # to the subrule.
1005 # in all other cases we create a new subrule.
1006 ##
1007 10 ▶ if ( scalar @$rhslist <= 1 && $self->retagSimpleRule($rulename, $mxelement, $cardinality, $context) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$rhslist <= 1 and $self->retagSimpleRule($rulename, $mxelement, $cardinality, $context)) { } 1 9 10
Condition66%2/3
scalar @$rhslist <= 1 and $self->retagSimpleRule($rulename, $mxelement, $cardinality, $context)
!ll&&!rl&&r
901
MC/DC50%1/2
scalar @$rhslist <= 1 and $self->retagSimpleRule($rulename, $mxelement, $cardinality, $context)
scalar @$rhslist <= 1 $self->retagSimpleRule($rulename, $mxelement, $cardinality, $context)
Truth table66%2/3
A and B
A= scalar @$rhslist <= 1B= $self->retagSimpleRule($rulename, $mxelement, $cardinality, $context)
ABresult
0X0
100
111
1008 {
1009 ##
1010 # if the tagged element is part of an alternative branch, we have to make it into a subrule
1011 ##
1012 1 ▶ if (defined $alternative)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alternative) 0 1 1
1013 {
1014 0 ▶ $mxelement = $self->createSubRule( $rulename, $mxelement, undef, undef, $context ) if scalar @$rhslist > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$rhslist > 1 0 0 0
1015 }
1016 }
1017 else
1018 {
1019 ##
1020 # create a subrule for cardinality-tagged element lists
1021 ##
1022 9 $mxelement = $self->createSubRule( $rulename, $mxelement, undef, $cardinality, $context );
1023  
1024 ##
1025 # propagate the cardinality to the new subrule
1026 # ("?" is wrapped into an 'opt_' rule in 'createSubRule', so it was already consumed).
1027 #
1028 # TODO : check if the '!defined $alternative' clause below should be reinstated
1029 ##
1030 # if ( !defined $alternative && $cardinality ne "?" && !$self->retagSimpleRule($rulename, $mxelement, $cardinality, $context, {abortatfirstlevel => 'true'}) )
1031 9 ▶ if ( $cardinality ne "?" && !$self->retagSimpleRule($rulename, $mxelement, $cardinality, $context, {abortatfirstlevel => 'true'}) )
Branch: 1 branch
BranchTrueFalseTotal
if ($cardinality ne "?" and not $self->retagSimpleRule($rulename, $mxelement, $cardinality, $context, {"abortatfirstlevel", "true"})) 0 9 9
Condition66%2/3
$cardinality ne "?" and not $self->retagSimpleRule($rulename, $mxelement, $cardinality, $context, {"abortatfirstlevel", "true"})
!ll&&!rl&&r
450
MC/DC0%0/2
$cardinality ne "?" and not $self->retagSimpleRule($rulename, $mxelement, $cardinality, $context, {"abortatfirstlevel", "true"})
$cardinality ne "?" not $self->retagSimpleRule($rulename, $mxelement, $cardinality, $context, {"abortatfirstlevel", "true"})
Truth table66%2/3
A and not(B)
A= $cardinality ne "?"B= not $self->retagSimpleRule($rulename, $mxelement, $cardinality, $context, {"abortatfirstlevel", "true"})
ABresult
0X0
100
111
1032 {
1033 0 ▶ mydie "INTERNAL: no destination for cardinality found in rule '$rulename'" if scalar @$rhslist > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$rhslist > 1 0 0 0
1034 0 ▶ $mxelement->{cardinality} = $cardinality if $cardinality ne "?";
Branch: 1 branch
BranchTrueFalseTotal
if $cardinality ne "?" 0 0 0
1035 }
1036 }
1037  
1038 # cardinality-annotated subrules loose lexeme status
1039 10 delete $mxelement->{class4list};
1040 10 delete $mxelement->{isLexeme};
1041 10 $groupisLexeme->{status} = -1;
1042 }
1043  
1044 60 mergeClass4List( \%$class4list, $mxelement );
1045 60 mergeClass4List( \%$class4group, $mxelement );
1046 60 mergeKeywordFragment( \%$keywordfragment, $mxelement );
1047  
1048 60 push @$currentlist, $mxelement;
1049 60 ++$alternativelength;
1050  
1051 ##
1052 # a list of lexemes is not itself a lexeme (unless declared in the 'lexer' grammar).
1053 ##
1054 60 ▶ $groupisLexeme->{status} = -1 if $alternativelength > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $alternativelength > 1 26 34 60
1055 }
1056  
1057 ##
1058 # replace a keyword built from lower/upper case character classes by a literal.
1059 # keyword literals must start with a letter.
1060 ##
1061 23 ▶ if ($self->testoption("buildkeywords") && $keywordfragment->{status} == 0 && $keywordfragment->{value} =~ /^[a-z]/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->testoption("buildkeywords") and $keywordfragment->{"status"} == 0 and $keywordfragment->{"value"} =~ /^[a-z]/i) 0 23 23
Condition66%4/6
$self->testoption("buildkeywords") and $keywordfragment->{"status"} == 0 and $keywordfragment->{"value"} =~ /^[a-z]/i
!ll&&!rl&&r
2030
$self->testoption("buildkeywords") and $keywordfragment->{"status"} == 0
!ll&&!rl&&r
0203
MC/DC0%0/3
$self->testoption("buildkeywords") and $keywordfragment->{"status"} == 0 and $keywordfragment->{"value"} =~ /^[a-z]/i
$self->testoption("buildkeywords") $keywordfragment->{"status"} == 0 $keywordfragment->{"value"} =~ /^[a-z]/i
Truth table50%2/4
A and B and C
A= $self->testoption("buildkeywords")B= $keywordfragment->{"status"} == 0C= $keywordfragment->{"value"} =~ /^[a-z]/i
ABCresult
0XX0
10X0
1100
1111
1062 {
1063 $currentlist = [
1064 0 { type => "literal", value => $keywordfragment->{value}, isLexeme => 1 }
1065 ];
1066 0 $groupisLexeme->{status} = 0;
1067 }
1068  
1069 ##
1070 # append the current list to the alternative list if it has entries.
1071 ##
1072 23 ▶ if ( scalar @$currentlist > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$currentlist > 0) 23 0 23
1073 {
1074 23 my $newlist = { list => $currentlist };
1075 23 ▶ $newlist->{class4list} = $class4list->{value} if $class4list->{status} != -1;
Branch: 1 branch
BranchTrueFalseTotal
if $class4list->{"status"} != -1 3 20 23
1076 23 ▶ $newlist->{isLexeme} = 1 if $groupisLexeme->{status} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $groupisLexeme->{"status"} == 0 1 22 23
1077 23 ▶ $newlist->{metalist} = $metalist if exists $metalist->{state};
Branch: 1 branch
BranchTrueFalseTotal
if exists $metalist->{"state"} 0 23 23
1078 23 push @$alternativelists, $newlist;
1079 }
1080  
1081 ##
1082 # remove branches that are case-equivalent
1083 ##
1084 23 ▶ $alternativelists = $self->removeCaseEquivalentBranches($alternativelists) if $self->testoption('matchcaseinsensitive');
Branch: 1 branch
BranchTrueFalseTotal
if $self->testoption("matchcaseinsensitive") 23 0 23
1085  
1086 23 my $grouplist = { type => 'group', list => $alternativelists };
1087 # single lists of literals can be tagged as lexemes
1088 # regular rules with alternative right sides will be tagged as G1
1089 23 ▶ $grouplist->{class4list} = $class4group->{value} if $class4group->{status} != -1;
Branch: 1 branch
BranchTrueFalseTotal
if $class4group->{"status"} != -1 1 22 23
1090 23 ▶ $grouplist->{isLexeme} = 1 if $groupisLexeme->{status} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $groupisLexeme->{"status"} == 0 1 22 23
1091  
1092 23 return $grouplist;
1093 }
1094  
1095 ## ------------------------------------------------------
1096 # 'walkgroup' processes the tokens from a parenthesized group
1097 # or from a complete rule
1098 ## ------------------------------------------------------
1099 sub walkgroup
1100 {
1101 9 ▶ my ($self, $rulename, $tokengroup, $context) = @_;
Subroutine walkgroup: called CC=3 SCAR=11.8
Pod undocumented
1102  
1103 9 ▶ $self->abortWithError( "INTERNAL: group is missing 'definition' in rule $rulename", $tokengroup ) if ref $tokengroup ne "HASH" || !exists $tokengroup->{definition};
Branch: 1 branch
BranchTrueFalseTotal
if ref $tokengroup ne "HASH" or not exists $tokengroup->{"definition"} 0 9 9
Condition33%1/3
ref $tokengroup ne "HASH" or not exists $tokengroup->{"definition"}
l!l&&r!l&&!r
009
MC/DC0%0/2
ref $tokengroup ne "HASH" or not exists $tokengroup->{"definition"}
ref $tokengroup ne "HASH" not exists $tokengroup->{"definition"}
Truth table33%1/3
A or not(B)
A= ref $tokengroup ne "HASH"B= not exists $tokengroup->{"definition"}
ABresult
1X1
011
000
1104 9 my $definition = $tokengroup->{definition};
1105 9 my $grouplist = $self->processRightSides( $rulename, $definition, $context );
1106  
1107 9 return $grouplist;
1108 }
1109  
1110 ## ------------------------------------------------------
1111 # 'walkrule' processes the 'rightsides' list of a parsed rule
1112 ## ------------------------------------------------------
1113 sub walkrule
1114 {
1115 23 ▶ my ($self, $rulename, $rule, $context) = @_;
Subroutine walkrule: called CC=18 SCAR=30.2
Pod undocumented
1116  
1117 # if ( $rulename eq "outer_join_sign")
1118 # {
1119 # printf "found!\n";
1120 # }
1121  
1122 23 my $symboltable = $self->symboltable;
1123 23 my $rulestatus = $symboltable->rulestatus($rulename);
1124  
1125 # test if we traversed this node already
1126 23 ▶ if ( defined $rulestatus )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rulestatus) 9 14 23
1127 {
1128 9 ▶ if ($rulestatus eq "synthetic" && !$self->checkMarpaRuleExists($rulename))
Branch: 1 branch
BranchTrueFalseTotal
if ($rulestatus eq "synthetic" and not $self->checkMarpaRuleExists($rulename)) 0 9 9
Condition33%1/3
$rulestatus eq "synthetic" and not $self->checkMarpaRuleExists($rulename)
!ll&&!rl&&r
900
MC/DC0%0/2
$rulestatus eq "synthetic" and not $self->checkMarpaRuleExists($rulename)
$rulestatus eq "synthetic" not $self->checkMarpaRuleExists($rulename)
Truth table33%1/3
A and not(B)
A= $rulestatus eq "synthetic"B= not $self->checkMarpaRuleExists($rulename)
ABresult
0X0
100
111
1129 {
1130 0 my $mxrightside = $self->processRightSides( $rulename, $rule->{rightsides}, $context );
1131 0 $self->addMarpaRule($rulename, $mxrightside);
1132 0 $symboltable->tagrule($rulename, 'done');
1133 0 $rulestatus = $symboltable->rulestatus($rulename);
1134 }
1135  
1136 9 my $mxrule;
1137  
1138 SWITCH: {
1139 9 ▶ ($rulestatus eq "inprogress") && do {$mxrule = $symboltable->rule( $rulename ); last SWITCH; };
Branch: 1 branch
BranchTrueFalseTotal
$rulestatus eq "inprogress" and do { $mxrule = $symboltable->rule($rulename); last SWITCH } 3 6 9
1140 6 ▶ ($rulestatus eq "done") && do {$mxrule = $self->getMarpaRule( undef, $rulename ); last SWITCH; };
Branch: 1 branch
BranchTrueFalseTotal
$rulestatus eq "done" and do { $mxrule = $self->getMarpaRule(undef, $rulename); last SWITCH } 6 0 6
1141 0 do { mydie "unexpected rule status $rulestatus in rule $rulename" };
1142 }
1143  
1144 9 my $result = { mxrule => $rulename, rhs => { token => $rulename } };
1145 9 ▶ $result->{class4list} = $mxrule->{class4list} if exists $mxrule->{class4list};
Branch: 1 branch
BranchTrueFalseTotal
if exists $mxrule->{"class4list"} 3 6 9
1146 9 return $result;
1147 }
1148  
1149 14 $self->enterLevel($rulename, $context);
1150  
1151 14 my $mxrightside = {};
1152 14 my $rightside = $rule->{rightsides};
1153 14 ▶ if (defined $rightside)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rightside) 14 0 14
1154 {
1155 14 ▶ $self->abortWithError( "rhs is not an array ref in $rulename", $rightside ) if ref $rightside ne "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if ref $rightside ne "ARRAY" 0 14 14
1156  
1157 14 ▶ $context->{subruleindex}{$rulename}{grammarstate} = $rule->{grammarstate} if exists $rule->{grammarstate} && $rule->{grammarstate} eq "lexer";
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{"grammarstate"} and $rule->{"grammarstate"} eq "lexer" 9 5 14
Condition66%2/3
exists $rule->{"grammarstate"} and $rule->{"grammarstate"} eq "lexer"
!ll&&!rl&&r
509
MC/DC50%1/2
exists $rule->{"grammarstate"} and $rule->{"grammarstate"} eq "lexer"
exists $rule->{"grammarstate"} $rule->{"grammarstate"} eq "lexer"
Truth table66%2/3
A and B
A= exists $rule->{"grammarstate"}B= $rule->{"grammarstate"} eq "lexer"
ABresult
0X0
100
111
1158 14 ▶ $context->{subruleindex}{$rulename}{isFragmentOrChild} = 'true' if exists $rule->{type} && $rule->{type} eq "fragment";
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{"type"} and $rule->{"type"} eq "fragment" 6 8 14
Condition66%2/3
exists $rule->{"type"} and $rule->{"type"} eq "fragment"
!ll&&!rl&&r
806
MC/DC50%1/2
exists $rule->{"type"} and $rule->{"type"} eq "fragment"
exists $rule->{"type"} $rule->{"type"} eq "fragment"
Truth table66%2/3
A and B
A= exists $rule->{"type"}B= $rule->{"type"} eq "fragment"
ABresult
0X0
100
111
1159  
1160 ##
1161 # prevent infinite recursion by tagging any symbol
1162 # before processing begins with 'inprogress'.
1163 # the 'defined($rulestatus)' clause at the top ensures
1164 # that we don't pick up a symbol that is already
1165 # being processed.
1166 ##
1167 14 $symboltable->tagrule($rulename, 'inprogress');
1168 14 $mxrightside = $self->processRightSides( $rulename, $rightside, $context );
1169 14 $symboltable->tagrule($rulename, 'done');
1170 }
1171  
1172 14 ▶ $mxrightside->{grammarstate} = $rule->{grammarstate} if exists $rule->{grammarstate} && $rule->{grammarstate} eq "lexer";
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{"grammarstate"} and $rule->{"grammarstate"} eq "lexer" 9 5 14
Condition66%2/3
exists $rule->{"grammarstate"} and $rule->{"grammarstate"} eq "lexer"
!ll&&!rl&&r
509
MC/DC50%1/2
exists $rule->{"grammarstate"} and $rule->{"grammarstate"} eq "lexer"
exists $rule->{"grammarstate"} $rule->{"grammarstate"} eq "lexer"
Truth table66%2/3
A and B
A= exists $rule->{"grammarstate"}B= $rule->{"grammarstate"} eq "lexer"
ABresult
0X0
100
111
1173 14 ▶ $mxrightside->{isFragmentOrChild} = 'true' if exists $rule->{type} && $rule->{type} eq "fragment";
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{"type"} and $rule->{"type"} eq "fragment" 6 8 14
Condition66%2/3
exists $rule->{"type"} and $rule->{"type"} eq "fragment"
!ll&&!rl&&r
806
MC/DC50%1/2
exists $rule->{"type"} and $rule->{"type"} eq "fragment"
exists $rule->{"type"} $rule->{"type"} eq "fragment"
Truth table66%2/3
A and B
A= exists $rule->{"type"}B= $rule->{"type"} eq "fragment"
ABresult
0X0
100
111
1174 14 ▶ if (exists $rule->{redirect})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"redirect"}) 1 13 14
1175 {
1176 1 $mxrightside->{redirected} = 'true';
1177 1 push @{$self->{discarded}}, $rulename;
1178 }
1179  
1180 14 $self->addMarpaRule($rulename, $mxrightside);
1181 14 $self->exitLevel($rulename, $context);
1182  
1183 14 return $mxrightside;
1184 }
1185  
1186 ## ------------------------------------------------------
1187 # 'processSub' processes a reference to a G4 subrule
1188 ## ------------------------------------------------------
1189 sub processSubRule
1190 {
1191 21 ▶ my ($self, $rulename, $context) = @_;
Subroutine processSubRule: called CC=2 SCAR=6.9
Pod undocumented
1192  
1193 21 my $symboltable = $self->symboltable;
1194 21 my $rule = $symboltable->rule($rulename);
1195  
1196 21 my $mxrule = $self->walkrule( $rulename, $rule, $context );
1197  
1198 21 my $result = { rule => $rulename, rhs => { token => $rulename } };
1199  
1200 21 my $keywordfragment = $self->isKeywordFragment($rulename, $mxrule);
1201 21 ▶ $result->{keywordfragment} = $keywordfragment if defined $keywordfragment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $keywordfragment 4 17 21
1202  
1203 21 return $result;
1204 }
1205  
1206 ## ------------------------------------------------------
1207 # 'reportUnusedRules' report all symbol table entries
1208 # that are not referred to by
1209 # parent rules.
1210 ## ------------------------------------------------------
1211 sub printHeaderUnusedRulesReport
1212 {
1213 0 ▶ printf "\n";
Subroutine printHeaderUnusedRulesReport: not called CC=1 SCAR=6.9
Pod undocumented
1214 0 printf "WARNING: the rules listed below are orphaned. They can't be reached from the start rule:\n";
1215 0 printf "=======:\n\n";
1216 0 printf <<'END_OF_SOURCE';
1217 +----------------------------- rule name
1218 +--!----------------------------- Lexical (L) or parser rule
1219 +--!--!----------------------------- Fragment (F) or regular rule
1220 ! ! !
1221 V V V
1222 END_OF_SOURCE
1223 }
1224  
1225 sub reportUnusedRules
1226 {
1227 1 ▶ my ($self) = @_;
Subroutine reportUnusedRules: called CC=8 SCAR=30.8
Pod undocumented
1228  
1229 1 my $symboltable = $self->symboltable;
1230  
1231 1 my $status = 0;
1232 1 my @symbols = $symboltable->symbols;
1233  
1234 1 for my $rulename (sort @symbols)
1235 {
1236 14 my $rule = $symboltable->rule($rulename);
1237 14 ▶ if (!exists $rule->{generationstatus})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $rule->{"generationstatus"}) 0 14 14
1238 {
1239 0 ▶ if (!$status)
Branch: 1 branch
BranchTrueFalseTotal
unless ($status) 0 0 0
1240 {
1241 0 printHeaderUnusedRulesReport();
1242 0 $status = 1;
1243 }
1244  
1245 printf "[%1s][%1s] %s\n",
1246 (exists $rule->{grammarstate} && $rule->{grammarstate} =~ /lexer/i) ? "L" : "",
1247 0 ▶ (exists $rule->{isFragmentOrChild}) ? "F" : "",
Branch: 2 branches
BranchTrueFalseTotal
exists $rule->{'grammarstate'} && $rule->{'grammarstate'} =~ /lexer/i ? : 0 0 0
exists $rule->{'isFragmentOrChild'} ? : 0 0 0
Condition0%0/3
exists $rule->{"grammarstate"} and $rule->{"grammarstate"} =~ /lexer/i
!ll&&!rl&&r
000
MC/DC0%0/2
exists $rule->{"grammarstate"} and $rule->{"grammarstate"} =~ /lexer/i
exists $rule->{"grammarstate"} $rule->{"grammarstate"} =~ /lexer/i
Truth table0%0/3
A and B
A= exists $rule->{"grammarstate"}B= $rule->{"grammarstate"} =~ /lexer/i
ABresult
0X0
100
111
1248 $rulename;
1249 }
1250 }
1251  
1252 1 ▶ printf "\n\n" if $status;
Branch: 1 branch
BranchTrueFalseTotal
if $status 0 1 1
1253 }
1254  
1255 ## ------------------------------------------------------
1256 # 'processRedirectRules' define Marpa rules for all
1257 # G4 rules that are tagged 'redirect'
1258 # and are not referred in parent rules
1259 ## ------------------------------------------------------
1260 sub processRedirectRules
1261 {
1262 1 ▶ my ($self) = @_;
Subroutine processRedirectRules: called CC=4 SCAR=13.9
Pod undocumented
1263  
1264 1 my $symboltable = $self->symboltable;
1265  
1266 1 my @symbols = $symboltable->symbols;
1267  
1268 1 for my $rulename (sort @symbols)
1269 {
1270 14 my $rule = $symboltable->rule($rulename);
1271 14 ▶ if (!exists $rule->{generationstatus} && exists $rule->{redirect})
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $rule->{"generationstatus"} and exists $rule->{"redirect"}) 1 13 14
Condition66%2/3
not exists $rule->{"generationstatus"} and exists $rule->{"redirect"}
!ll&&!rl&&r
1301
MC/DC50%1/2
not exists $rule->{"generationstatus"} and exists $rule->{"redirect"}
not exists $rule->{"generationstatus"} exists $rule->{"redirect"}
Truth table66%2/3
not(A) and B
A= not exists $rule->{"generationstatus"}B= exists $rule->{"redirect"}
ABresult
0X0
100
111
1272 {
1273 1 $self->walkrule( $rulename, $rule, {level => 0} );
1274 }
1275 }
1276 }
1277  
1278 ## -------------------------------------------------------------------------------------
1279 # 'translateG4Grammar' create nested Marpa rules for the G4 rules from the symbol table
1280 # by doing a depth-first traversal starting with the grammar's
1281 # start symbol.
1282 # this creates the convex hull of the start symbol collecting all
1283 # symbols that can be reached from the start symbol.
1284 # 'reportUnusedRules' creates a report of all symbols from the
1285 # parse tree that are not part of the convex hull.
1286 ## -------------------------------------------------------------------------------------
1287 sub translateG4Grammar
1288 {
1289 1 ▶ my ($self) = @_;
Subroutine translateG4Grammar: called CC=2 SCAR=6.9
Pod undocumented
1290  
1291 1 my $symboltable = $self->symboltable;
1292  
1293 1 my $startrule = $symboltable->startrule;
1294 1 my $startrulename = $startrule->{name};
1295 1 $startrule = $symboltable->rule($startrulename);
1296  
1297 1 ▶ mydie "INTERNAL: start rule $startrulename is missing 'rightsides'" if !exists $startrule->{rightsides};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $startrule->{"rightsides"} 0 1 1
1298  
1299 1 $self->walkrule( $startrulename, $startrule, {level => 0} );
1300  
1301 1 $self->processRedirectRules;
1302 1 $self->reportUnusedRules;
1303 }
1304  
1305 ## ================================================== #
1306 # write the generated Marpa rules to the output file #
1307 ## ================================================== #
1308  
1309 sub openOutputFile
1310 {
1311 1 ▶ my ($self) = @_;
Subroutine openOutputFile: called CC=3 SCAR=11.8
Pod undocumented
1312  
1313 1 my $outputfile = $self->{outputfilename};
1314  
1315 1 ▶ if ( $outputfile ne "-" )
Branch: 1 branch
BranchTrueFalseTotal
if ($outputfile ne '-') { } 1 0 1
1316 {
1317 1 my $outf;
1318 1 ▶ mydie("cannot open output file $outputfile") unless open( $outf, ">$outputfile" );
Branch: 1 branch
BranchTrueFalseTotal
unless open $outf, ">$outputfile" 0 1 1
1319 1 $self->{outf} = $outf;
1320 1 $self->{is_stdout} = 0;
1321 }
1322 else
1323 {
1324 0 $self->{outf} = *STDOUT;
1325 0 $self->{is_stdout} = 1;
1326 }
1327 }
1328  
1329 sub closeOutputFile
1330 {
1331 1 ▶ my ($self) = @_;
Subroutine closeOutputFile: called CC=2 SCAR=7.2
Pod undocumented
1332 1 ▶ close($self->{outf}) if $self->{is_stdout};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_stdout"} 0 1 1
1333 }
1334  
1335 sub pad
1336 {
1337 50 ▶ my ($self, $s ) = @_;
Subroutine pad: called CC=3 SCAR=11.0
Pod undocumented
1338  
1339 50 ▶ return undef if !defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 50 50
1340  
1341 50 my $len = length($s);
1342 50 my $pad = $self->{indent} - $len;
1343  
1344 50 ▶ return $s if $pad < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pad < 0 1 49 50
1345  
1346 49 return $s . (" " x $pad);
1347 }
1348  
1349 sub renderComment
1350 {
1351 0 ▶ my ($self, $rulename, $comments) = @_;
Subroutine renderComment: not called CC=4 SCAR=30.0
Pod undocumented
1352  
1353 0 ▶ mydie "INTERNAL : 'comments' is not an array in rule $rulename" if ref $comments ne "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if ref $comments ne "ARRAY" 0 0 0
1354  
1355 0 my $result = [];
1356 0 for my $comment (@$comments)
1357 {
1358 0 $comment =~ s/\/\*(.*)\*\//$1/;
1359 0 my @commentlist = split '\n', $comment;
1360  
1361 0 for my $cl (@commentlist)
1362 {
1363 0 $cl =~ s/^\/\/\s*//;
1364 0 $cl =~ s/^#\s*//;
1365 0 push @$result, "# ${cl}";
1366 }
1367 }
1368  
1369 0 return $result;
1370 }
1371  
1372 sub processRedirected
1373 {
1374 39 ▶ my ($self, $token) = @_;
Subroutine processRedirected: called CC=5 SCAR=19.7
Pod undocumented
1375  
1376 39 ▶ mydie "processRedirected : 'token' must be a scalar" if ref $token ne "";
Branch: 1 branch
BranchTrueFalseTotal
if ref $token ne "" 0 39 39
1377  
1378 39 my $rule = $self->getMarpaRule(undef, $token);
1379  
1380 39 ▶ if ($self->testoption('matchcaseinsensitive') && $self->isKeywordLetter($token, $rule))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->testoption("matchcaseinsensitive") and $self->isKeywordLetter($token, $rule)) 0 39 39
Condition33%1/3
$self->testoption("matchcaseinsensitive") and $self->isKeywordLetter($token, $rule)
!ll&&!rl&&r
0390
MC/DC0%0/2
$self->testoption("matchcaseinsensitive") and $self->isKeywordLetter($token, $rule)
$self->testoption("matchcaseinsensitive") $self->isKeywordLetter($token, $rule)
Truth table33%1/3
A and B
A= $self->testoption("matchcaseinsensitive")B= $self->isKeywordLetter($token, $rule)
ABresult
0X0
100
111
1381 {
1382 0 my $value = $rule->{class4list};
1383 0 $value = substr($value, 0, 1);
1384 0 return { value => "'${value}':i" };
1385 }
1386  
1387 39 ▶ return { discard => 'true' } if exists $rule->{redirected};
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{"redirected"} 0 39 39
1388  
1389 39 return { value => $token };
1390 }
1391  
1392 sub fragmentEligible2Convert
1393 {
1394 28 ▶ my ($rule) = @_;
Subroutine fragmentEligible2Convert: called CC=2 SCAR=6.9
Pod undocumented
1395  
1396 28 ▶ return 0 if !exists $rule->{class4list};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rule->{"class4list"} 27 1 28
1397 1 my $class4list = $rule->{class4list};
1398  
1399 # replace escaped characters and unicode codepoints with a single character
1400 1 $class4list =~ s/\\([^u])/$1/g;
1401 1 $class4list =~ s/\\u([0-9a-f]{4,4})/u/ig;
1402  
1403 1 return length($class4list) > 1;
1404 }
1405  
1406 sub writeFragmentAsClass
1407 {
1408 1 ▶ my ($self, $rulename, $rule, $options ) = @_;
Subroutine writeFragmentAsClass: called CC=1 SCAR=0.0
Pod undocumented
1409  
1410 1 my $synthclass = { type => "class", value => $rule->{class4list} };
1411 1 my $outputline = $self->printRhs( $rulename, $synthclass, {status => 0, delimiter => '|', assignop => '~'} );
1412 1 $self->deleteAllSubrules($rulename);
1413  
1414 1 my $outf = $self->{outf};
1415 1 printf $outf "%s\n", $outputline;
1416 }
1417  
1418 ##
1419 # normalizeClassString : remove class elements that are redundant when the class is made case-insensitive
1420 # return the modified class text as well as an indicator if the class contains letters
1421 ##
1422 sub normalizeClassString
1423 {
1424 3 ▶ my ($self, $classstring ) = @_;
Subroutine normalizeClassString: called CC=15 SCAR=28.4
Pod undocumented
1425  
1426 3 my $classhash = {};
1427 3 my $characterhash = {};
1428  
1429 # strip [/] from classtring
1430 3 $classstring =~ s/^\[([^\]]*)\]$/$1/;
1431  
1432 3 my $result = "";
1433 3 my $isalphaclass = 0;
1434 3 ▶ while ( $classstring ne "" )
Branch: 1 branch
BranchTrueFalseTotal
if ($classstring ne "") 12 3 15
1435 {
1436 SWITCH: {
1437 12 ▶ ($classstring =~ /(\\u[0-9a-f]+)-(\\u[0-9a-f]+)(.*)$/i) && do {
Branch: 1 branch
BranchTrueFalseTotal
$classstring =~ /(\\u[0-9a-f]+)-(\\u[0-9a-f]+)(.*)$/i and do { my $beg = $1; my $end = $2; $result .= "$1-$2"; $classstring = $3; last SWITCH } 0 12 12
1438 0 my $beg = $1;
1439 0 my $end = $2;
1440 0 $result .= "${1}-${2}";
1441 0 $classstring = $3;
1442 0 last SWITCH;
1443 };
1444 12 ▶ ($classstring =~ /(\\u[0-9a-f]{4,4})(.*)$/i) && do {
Branch: 1 branch
BranchTrueFalseTotal
$classstring =~ /(\\u[0-9a-f]{4,4})(.*)$/i and do { my $c = $1; $result .= $c; $classstring = $2; last SWITCH } 0 12 12
1445 0 my $c = $1;
1446 0 $result .= $c;
1447 0 $classstring = $2;
1448 0 last SWITCH;
1449 };
1450 12 ▶ ($classstring =~ /([^-])-([^-])(.*)$/) && do {
Branch: 1 branch
BranchTrueFalseTotal
$classstring =~ /([^-])-([^-])(.*)$/ and do { my $beg = $1; my $end = $2; my $key = "$beg-$end"; unless (exists $$classhash{lc $key} or exists $$classhash{uc $key}) { $classhash->{$key} = 1; $result .= $key; $isalphaclass = 1 if $beg =~ /[a-z]/i or $end =~ /[a-z]/i; } ; $classstring = $3; last SWITCH } 3 9 12
1451 3 my $beg = $1;
1452 3 my $end = $2;
1453 3 my $key = "${beg}-${end}";
1454 3 ▶ if (!exists $classhash->{lc $key} && !exists $classhash->{uc $key})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$classhash{lc $key} or exists $$classhash{uc $key}) 2 1 3
Condition66%2/3
exists $$classhash{lc $key} or exists $$classhash{uc $key}
l!l&&r!l&&!r
102
MC/DC50%1/2
exists $$classhash{lc $key} or exists $$classhash{uc $key}
exists $$classhash{lc $key} exists $$classhash{uc $key}
Truth table66%2/3
A or B
A= exists $$classhash{lc $key}B= exists $$classhash{uc $key}
ABresult
1X1
011
000
1455 {
1456 2 $classhash->{$key} = 1;
1457 2 $result .= $key;
1458 2 ▶ $isalphaclass = 1 if $beg =~ /[a-z]/i || $end =~ /[a-z]/i;
Branch: 1 branch
BranchTrueFalseTotal
if $beg =~ /[a-z]/i or $end =~ /[a-z]/i 1 1 2
Condition66%2/3
$beg =~ /[a-z]/i or $end =~ /[a-z]/i
l!l&&r!l&&!r
101
MC/DC50%1/2
$beg =~ /[a-z]/i or $end =~ /[a-z]/i
$beg =~ /[a-z]/i $end =~ /[a-z]/i
Truth table66%2/3
A or B
A= $beg =~ /[a-z]/iB= $end =~ /[a-z]/i
ABresult
1X1
011
000
1459 }
1460 3 $classstring = $3;
1461 3 last SWITCH;
1462 };
1463 9 ▶ ($classstring =~ /(\\.)(.*)$/) && do {
Branch: 1 branch
BranchTrueFalseTotal
$classstring =~ /(\\.)(.*)$/ and do { my $c = $1; unless (exists $characterhash->{$c}) { $characterhash->{$c} = 1; $result .= $c; } ; $classstring = $2; last SWITCH } 1 8 9
1464 1 my $c = $1;
1465 1 ▶ if (!exists $characterhash->{$c})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $characterhash->{$c}) 1 0 1
1466 {
1467 1 $characterhash->{$c} = 1;
1468 1 $result .= $c;
1469 }
1470 1 $classstring = $2;
1471 1 last SWITCH;
1472 };
1473 8 ▶ ($classstring =~ /(.)(.*)$/) && do {
Branch: 1 branch
BranchTrueFalseTotal
$classstring =~ /(.)(.*)$/ and do { my $c = $1; unless (exists $$characterhash{lc $c} or exists $$characterhash{uc $c}) { $characterhash->{$c} = 1; $result .= $c; $isalphaclass = 1 if $c =~ /^[a-z]$/i; } ; $classstring = $2; last SWITCH } 8 0 8
1474 8 my $c = $1;
1475 8 ▶ if (!exists $characterhash->{lc $c} && !exists $characterhash->{uc $c})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$characterhash{lc $c} or exists $$characterhash{uc $c}) 7 1 8
Condition66%2/3
exists $$characterhash{lc $c} or exists $$characterhash{uc $c}
l!l&&r!l&&!r
017
MC/DC50%1/2
exists $$characterhash{lc $c} or exists $$characterhash{uc $c}
exists $$characterhash{lc $c} exists $$characterhash{uc $c}
Truth table66%2/3
A or B
A= exists $$characterhash{lc $c}B= exists $$characterhash{uc $c}
ABresult
1X1
011
000
1476 {
1477 7 $characterhash->{$c} = 1;
1478 7 $result .= $c;
1479 7 ▶ $isalphaclass = 1 if $c =~ /^[a-z]$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /^[a-z]$/i 6 1 7
1480 }
1481 8 $classstring = $2;
1482 8 last SWITCH;
1483 };
1484 }
1485 }
1486  
1487 3 return { isalphaclass => $isalphaclass, classstring => $result };
1488 }
1489  
1490 sub isAlphaLiteral
1491 {
1492 29 ▶ my ($self, $literal ) = @_;
Subroutine isAlphaLiteral: called CC=6 SCAR=17.9
Pod undocumented
1493  
1494 29 ▶ return 0 if $literal !~ /[a-z]/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $literal =~ /[a-z]/i 21 8 29
1495  
1496 8 my $isAlpha = 0;
1497  
1498 8 ▶ while ( $literal ne "" )
Branch: 1 branch
BranchTrueFalseTotal
if ($literal ne "") 43 8 51
1499 {
1500 SWITCH: {
1501 43 ▶ ($literal =~ /(\\.)(.*)$/) && do {
Branch: 1 branch
BranchTrueFalseTotal
$literal =~ /(\\.)(.*)$/ and do { my $c = $1; $literal = $2; last SWITCH } 4 39 43
1502 4 my $c = $1;
1503 4 $literal = $2;
1504 4 last SWITCH;
1505 };
1506 39 ▶ ($literal =~ /(.)(.*)$/) && do {
Branch: 1 branch
BranchTrueFalseTotal
$literal =~ /(.)(.*)$/ and do { my $c = $1; $literal = $2; $isAlpha = 1 if $c =~ /^[a-z]$/i; last SWITCH } 39 0 39
1507 39 my $c = $1;
1508 39 $literal = $2;
1509 39 ▶ $isAlpha = 1 if $c =~ /^[a-z]$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /^[a-z]$/i 25 14 39
1510 39 last SWITCH;
1511 };
1512 }
1513 }
1514  
1515 8 return $isAlpha;
1516 }
1517  
1518 ##
1519 # computeRhs :
1520 ##
1521 sub computeRhs
1522 {
1523 69 ▶ my ($self, $rulename, $rhs, $options ) = @_;
Subroutine computeRhs: called CC=25 SCAR=40.0
Pod undocumented
1524  
1525 69 ▶ my $isLexeme = 1 if exists $rhs->{isLexeme};
Branch: 1 branch
BranchTrueFalseTotal
if exists $rhs->{"isLexeme"} 27 42 69
1526  
1527 # if ($rulename eq "HEXNUMBER_003")
1528 # {
1529 # printf "found!\n";
1530 # }
1531  
1532 SWITCH: {
1533 69 ▶ (exists $rhs->{type} && $rhs->{type} eq "negatedclass") && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $rhs->{"type"} and $rhs->{"type"} eq "negatedclass" and do { mydie("computeRhs : 'value' missing in negatedclass $rulename") unless exists $rhs->{"value"}; $rhs = {"value", '[' . $rhs->{"value"} . ']'}; last SWITCH } 1 68 69
Condition100%3/3
exists $rhs->{"type"} and $rhs->{"type"} eq "negatedclass"
!ll&&!rl&&r
39291
MC/DC100%2/2
exists $rhs->{"type"} and $rhs->{"type"} eq "negatedclass"
exists $rhs->{"type"} $rhs->{"type"} eq "negatedclass"
Truth table100%3/3
A and B
A= exists $rhs->{"type"}B= $rhs->{"type"} eq "negatedclass"
ABresult
0X0
100
111
1534 1 ▶ mydie "computeRhs : 'value' missing in negatedclass $rulename" if !exists $rhs->{value};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rhs->{"value"} 0 1 1
1535 1 $rhs = { value => "[" . $rhs->{value} . "]" };
1536 1 last SWITCH;
1537 };
1538 68 ▶ (exists $rhs->{deletedafterconversiontoclass}) && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $rhs->{"deletedafterconversiontoclass"} and do { $rhs = {"discard", "true"}; last SWITCH } 0 68 68
1539 0 $rhs = { discard => 'true' };
1540 0 last SWITCH;
1541 };
1542 68 ▶ (exists $rhs->{type} && $rhs->{type} eq "unsupported") && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $rhs->{"type"} and $rhs->{"type"} eq "unsupported" and do { mydie($rhs->{"msg"}); last SWITCH } 0 68 68
Condition66%2/3
exists $rhs->{"type"} and $rhs->{"type"} eq "unsupported"
!ll&&!rl&&r
39290
MC/DC0%0/2
exists $rhs->{"type"} and $rhs->{"type"} eq "unsupported"
exists $rhs->{"type"} $rhs->{"type"} eq "unsupported"
Truth table66%2/3
A and B
A= exists $rhs->{"type"}B= $rhs->{"type"} eq "unsupported"
ABresult
0X0
100
111
1543 0 mydie $rhs->{msg};
1544 0 last SWITCH;
1545 };
1546 68 ▶ (exists $rhs->{type} && $rhs->{type} eq "class") && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $rhs->{"type"} and $rhs->{"type"} eq "class" and do { my $value = $rhs->{"value"}; my $modifier = ""; if ($self->testoption("matchcaseinsensitive") and $self->isAlphaLiteral($value)) { my $normclass = $self->normalizeClassString($value); $value = $normclass->{"classstring"}; $modifier = ":ic" if $normclass->{"isalphaclass"}; } ; $rhs = {"value", "[$value]$modifier"}; last SWITCH } 8 60 68
Condition100%3/3
exists $rhs->{"type"} and $rhs->{"type"} eq "class"
!ll&&!rl&&r
39218
MC/DC100%2/2
exists $rhs->{"type"} and $rhs->{"type"} eq "class"
exists $rhs->{"type"} $rhs->{"type"} eq "class"
Truth table100%3/3
A and B
A= exists $rhs->{"type"}B= $rhs->{"type"} eq "class"
ABresult
0X0
100
111
1547 8 my $value = $rhs->{value};
1548 8 my $modifier = "";
1549 8 ▶ if ($self->testoption('matchcaseinsensitive') && $self->isAlphaLiteral($value))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->testoption("matchcaseinsensitive") and $self->isAlphaLiteral($value)) 3 5 8
Condition66%2/3
$self->testoption("matchcaseinsensitive") and $self->isAlphaLiteral($value)
!ll&&!rl&&r
053
MC/DC50%1/2
$self->testoption("matchcaseinsensitive") and $self->isAlphaLiteral($value)
$self->testoption("matchcaseinsensitive") $self->isAlphaLiteral($value)
Truth table66%2/3
A and B
A= $self->testoption("matchcaseinsensitive")B= $self->isAlphaLiteral($value)
ABresult
0X0
100
111
1550 {
1551 3 my $normclass = $self->normalizeClassString($value);
1552 3 $value = $normclass->{classstring};
1553 3 ▶ $modifier = ':ic' if $normclass->{isalphaclass};
Branch: 1 branch
BranchTrueFalseTotal
if $normclass->{"isalphaclass"} 3 0 3
1554 }
1555 8 $rhs = { value => "[${value}]${modifier}" };
1556 8 last SWITCH;
1557 };
1558 60 ▶ (exists $rhs->{type} && $rhs->{type} eq "literal") && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $rhs->{"type"} and $rhs->{"type"} eq "literal" and do { my $literal = $rhs->{"value"}; $literal = $literal eq "\\'" ? "[']" : "'${literal}'"; if ($self->testoption("matchcaseinsensitive") and $self->isAlphaLiteral($literal)) { $literal = lc $literal; $literal .= ':i'; } ; $rhs = {"value", $literal}; last SWITCH } 21 39 60
Condition66%2/3
exists $rhs->{"type"} and $rhs->{"type"} eq "literal"
!ll&&!rl&&r
39021
MC/DC50%1/2
exists $rhs->{"type"} and $rhs->{"type"} eq "literal"
exists $rhs->{"type"} $rhs->{"type"} eq "literal"
Truth table66%2/3
A and B
A= exists $rhs->{"type"}B= $rhs->{"type"} eq "literal"
ABresult
0X0
100
111
1559 21 my $literal = $rhs->{value};
1560 21 ▶ $literal = ($literal eq "\\'") ? "[']" : "'${literal}'";
Branch: 1 branch
BranchTrueFalseTotal
$literal eq q[\\'] ? : 0 21 21
1561 21 ▶ if ($self->testoption('matchcaseinsensitive') && $self->isAlphaLiteral($literal))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->testoption("matchcaseinsensitive") and $self->isAlphaLiteral($literal)) 4 17 21
Condition66%2/3
$self->testoption("matchcaseinsensitive") and $self->isAlphaLiteral($literal)
!ll&&!rl&&r
0174
MC/DC50%1/2
$self->testoption("matchcaseinsensitive") and $self->isAlphaLiteral($literal)
$self->testoption("matchcaseinsensitive") $self->isAlphaLiteral($literal)
Truth table66%2/3
A and B
A= $self->testoption("matchcaseinsensitive")B= $self->isAlphaLiteral($literal)
ABresult
0X0
100
111
1562 {
1563 4 $literal = lc $literal;
1564 4 $literal .= ':i';
1565 }
1566 21 $rhs = { value => $literal };
1567 21 last SWITCH;
1568 };
1569 39 ▶ (exists $rhs->{type} && $rhs->{type} eq "action") && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $rhs->{"type"} and $rhs->{"type"} eq "action" and do { push @{$$options{"comments"};}, ' # Action: ' . $rhs->{"token"}{"action"}; $rhs = {"discard", "true"}; last SWITCH } 0 39 39
Condition33%1/3
exists $rhs->{"type"} and $rhs->{"type"} eq "action"
!ll&&!rl&&r
3900
MC/DC0%0/2
exists $rhs->{"type"} and $rhs->{"type"} eq "action"
exists $rhs->{"type"} $rhs->{"type"} eq "action"
Truth table33%1/3
A and B
A= exists $rhs->{"type"}B= $rhs->{"type"} eq "action"
ABresult
0X0
100
111
1570 0 push @{$options->{comments}}, " # Action: " . $rhs->{token}{action};
1571 0 $rhs = { discard => 'true' };
1572 0 last SWITCH;
1573 };
1574 39 ▶ (exists $rhs->{rhs}) && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $rhs->{"rhs"} and do { $rhs = $rhs->{"rhs"}; mydie("computeRhs: scalar 'token' missing in rhs") if not exists $rhs->{"token"} or ref $rhs->{"token"} ne ""; $rhs = $self->processRedirected($rhs->{"token"}); last SWITCH } 39 0 39
1575 39 $rhs = $rhs->{rhs};
1576 39 ▶ mydie "computeRhs: scalar 'token' missing in rhs" if !exists $rhs->{token} || ref $rhs->{token} ne "";
Branch: 1 branch
BranchTrueFalseTotal
if not exists $rhs->{"token"} or ref $rhs->{"token"} ne "" 0 39 39
Condition33%1/3
not exists $rhs->{"token"} or ref $rhs->{"token"} ne ""
l!l&&r!l&&!r
0039
MC/DC0%0/2
not exists $rhs->{"token"} or ref $rhs->{"token"} ne ""
not exists $rhs->{"token"} ref $rhs->{"token"} ne ""
Truth table33%1/3
not(A) or B
A= not exists $rhs->{"token"}B= ref $rhs->{"token"} ne ""
ABresult
1X1
011
000
1577 39 $rhs = $self->processRedirected($rhs->{token});
1578 39 last SWITCH;
1579 };
1580 0 ▶ (exists $rhs->{token}) && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $rhs->{"token"} and do { $rhs = $self->processRedirected($rhs->{"token"}); last SWITCH } 0 0 0
1581 0 $rhs = $self->processRedirected($rhs->{token});
1582 0 last SWITCH;
1583 };
1584 0 ▶ (exists $rhs->{comments}) && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $rhs->{"comments"} and do { my $cl = $self->renderComment($rulename, $rhs->{"comments"}); push @{$$options{"comments"};}, @$cl; $rhs = {"discard", "true"}; last SWITCH } 0 0 0
1585 0 my $cl = $self->renderComment($rulename, $rhs->{comments});
1586 0 push (@{$options->{comments}}, @$cl );
1587 0 $rhs = { discard => 'true' };
1588 0 last SWITCH;
1589 };
1590 0 do {
1591 0 $self->abortWithError( "computeRhs : don't know how to process rhs for $rulename, modify this rule to make it convertible", $rhs );
1592 };
1593 }
1594  
1595 69 return $rhs;
1596 }
1597  
1598 ##
1599 # printRhs : print a single right hand clause (alternative) of a rule.
1600 # depending on $option->{status} format the clause :
1601 # 0 : prefix the rule name and use '::=' or '~' as operator
1602 # 1 : start an alternative branch
1603 # 2 : add a clause to a branch
1604 ##
1605 sub printRhs
1606 {
1607 69 ▶ my ( $self, $rulename, $rule, $options ) = @_;
Subroutine printRhs: called CC=15 SCAR=28.4
Pod undocumented
1608  
1609 69 ▶ mydie "printRhs: 'status' not defined for rule $rulename" if !exists $options->{status};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"status"} 0 69 69
1610  
1611 69 my $status = $options->{status};
1612 69 my $rhs = $self->computeRhs($rulename, $rule, $options);
1613  
1614 69 ▶ return "" if exists $rhs->{discard};
Branch: 1 branch
BranchTrueFalseTotal
if exists $rhs->{"discard"} 0 69 69
1615  
1616 69 ▶ mydie "'options' not well-defined" if ref $options ne "HASH" || !exists $options->{delimiter};
Branch: 1 branch
BranchTrueFalseTotal
if ref $options ne "HASH" or not exists $options->{"delimiter"} 0 69 69
Condition33%1/3
ref $options ne "HASH" or not exists $options->{"delimiter"}
l!l&&r!l&&!r
0069
MC/DC0%0/2
ref $options ne "HASH" or not exists $options->{"delimiter"}
ref $options ne "HASH" not exists $options->{"delimiter"}
Truth table33%1/3
A or not(B)
A= ref $options ne "HASH"B= not exists $options->{"delimiter"}
ABresult
1X1
011
000
1617  
1618 69 my $delimiter = $options->{delimiter};
1619  
1620 69 my $cardinality = "";
1621 69 ▶ $cardinality = $options->{cardinality} if exists $options->{cardinality};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"cardinality"} 10 59 69
1622 69 ▶ $cardinality =~ s/([+*])\?/$1/ if $cardinality =~ /[+*]\?/ && $self->testoption('shiftlazytogreedy');
Branch: 1 branch
BranchTrueFalseTotal
if $cardinality =~ /[+*]\?/ and $self->testoption("shiftlazytogreedy") 0 69 69
Condition33%1/3
$cardinality =~ /[+*]\?/ and $self->testoption("shiftlazytogreedy")
!ll&&!rl&&r
6900
MC/DC0%0/2
$cardinality =~ /[+*]\?/ and $self->testoption("shiftlazytogreedy")
$cardinality =~ /[+*]\?/ $self->testoption("shiftlazytogreedy")
Truth table33%1/3
A and B
A= $cardinality =~ /[+*]\?/B= $self->testoption("shiftlazytogreedy")
ABresult
0X0
100
111
1623 69 ▶ mydie sprintf "lazy quantifier %s in rule %s not supported by Marpa", $cardinality, $rulename if $cardinality =~ /[+*]\?/;
Branch: 1 branch
BranchTrueFalseTotal
if $cardinality =~ /[+*]\?/ 0 69 69
1624  
1625 # if ($rulename eq "local_xmlindex_clause_001")
1626 # {
1627 # printf "found!\n";
1628 # }
1629  
1630 69 my $definitionop = "::=";
1631 69 ▶ $definitionop = $options->{assignop} if exists $options->{assignop};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"assignop"} 40 29 69
1632  
1633 69 my $result;
1634  
1635 # process optional rules
1636 69 ▶ if ( defined $cardinality && $cardinality eq "?")
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cardinality and $cardinality eq "?") 4 65 69
Condition66%2/3
defined $cardinality and $cardinality eq "?"
!ll&&!rl&&r
0654
MC/DC50%1/2
defined $cardinality and $cardinality eq "?"
defined $cardinality $cardinality eq "?"
Truth table66%2/3
A and B
A= defined $cardinalityB= $cardinality eq "?"
ABresult
0X0
100
111
1637 {
1638 4 $result = sprintf "%s %-3s %s%s\n", $self->pad($rulename), $definitionop, "", "";
1639 4 $cardinality = "";
1640 }
1641  
1642 SWITCH: {
1643 69 ▶ ($status == 0) && do {
Branch: 1 branch
BranchTrueFalseTotal
$status == 0 and do { $result .= sprintf("%s %-3s %s%s", $self->pad($rulename), $definitionop, $rhs->{"value"}, $cardinality); last SWITCH } 32 37 69
1644 32 $result .= sprintf "%s %-3s %s%s", $self->pad($rulename), $definitionop, $rhs->{value}, $cardinality;
1645 32 last SWITCH;
1646 };
1647 37 ▶ ($status == 1) && do {
Branch: 1 branch
BranchTrueFalseTotal
$status == 1 and do { $result .= sprintf("%s %-3s %s%s", $self->pad(""), $delimiter, $rhs->{"value"}, $cardinality); last SWITCH } 11 26 37
1648 11 $result .= sprintf "%s %-3s %s%s", $self->pad(""), $delimiter, $rhs->{value}, $cardinality;
1649 11 last SWITCH;
1650 };
1651 26 ▶ ($status == 2) && do {
Branch: 1 branch
BranchTrueFalseTotal
$status == 2 and do { $result .= sprintf(' %s%s', $rhs->{"value"}, $cardinality); last SWITCH } 26 0 26
1652 26 $result .= sprintf " %s%s", $rhs->{value}, $cardinality;
1653 26 last SWITCH;
1654 };
1655 0 do {
1656 0 mydie "printRhs : illegal status";
1657 };
1658 }
1659  
1660 69 return $result;
1661 }
1662  
1663 ##
1664 # writeMarpaRuleList : print all alternative branches of a rule
1665 ##
1666 sub writeMarpaRuleList
1667 {
1668 22 ▶ my ( $self, $rulename, $rule, $options ) = @_;
Subroutine writeMarpaRuleList: called CC=27 SCAR=49.8
Pod undocumented
1669  
1670 # if ($rulename eq "MINUSCHAR")
1671 # {
1672 # printf "found!\n";
1673 # }
1674  
1675 22 $options->{status} = 0;
1676  
1677 22 my $outf = $self->{outf};
1678  
1679 22 ▶ mydie "writeMarpaRuleList : 'list' missing from 'rule' in $rulename" if !exists $rule->{list};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rule->{"list"} 0 22 22
1680  
1681 22 my $list = $rule->{list};
1682 22 my $actiontext;
1683  
1684 22 for my $rulelist (@$list)
1685 {
1686 33 ▶ mydie "writeMarpaRuleList : 'list' missing from 'rulelist' in $rulename" if !exists $rulelist->{list};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $rulelist->{"list"} 0 33 33
1687  
1688 33 ▶ if (exists $rulelist->{metalist})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rulelist->{"metalist"}) 0 33 33
1689 {
1690 0 my $metalist = $rulelist->{metalist};
1691  
1692 0 for my $cl (@{$metalist->{comments}})
1693 {
1694 0 ▶ $cl = $cl->[0] if ref $cl eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if ref $cl eq "ARRAY" 0 0 0
1695 0 printf $outf "%s\n", $cl;
1696 }
1697 0 ▶ if (exists $metalist->{actions})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $metalist->{"actions"}) 0 0 0
1698 {
1699 0 ▶ if (!$self->testoption('embedactions'))
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->testoption('embedactions')) { } 0 0 0
1700 {
1701 0 for my $cl (@{$metalist->{actions}})
1702 {
1703 0 ▶ $cl = $cl->[0] if ref $cl eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if ref $cl eq "ARRAY" 0 0 0
1704 0 printf $outf "%s\n", $cl;
1705 }
1706 }
1707 else
1708 {
1709 0 $actiontext = "";
1710 0 for my $cl (@{$metalist->{actions}})
1711 {
1712 0 ▶ $cl = $cl->[0] if ref $cl eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if ref $cl eq "ARRAY" 0 0 0
1713 0 $actiontext .= $cl;
1714 }
1715 }
1716 }
1717 }
1718  
1719 33 my $outputline = "";
1720 33 my $linelen = 0;
1721  
1722 33 for my $rhs (@{$rulelist->{list}})
1723 {
1724 59 ▶ my $cardinality = $rhs->{cardinality} if exists $rhs->{cardinality};
Branch: 1 branch
BranchTrueFalseTotal
if exists $rhs->{"cardinality"} 1 58 59
1725  
1726 59 $options->{delimiter} = "|";
1727 59 ▶ $options->{cardinality} = $cardinality if defined $cardinality;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cardinality 1 58 59
1728  
1729 59 ▶ if (exists $rhs->{comments})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rhs->{"comments"}) 0 59 59
1730 {
1731 0 my $cl = $rhs->{comments};
1732 0 mydie "TODO : comments";
1733 }
1734  
1735 59 my $nextclause;
1736  
1737 SWITCH: {
1738 59 ▶ (exists $rhs->{type} && $rhs->{type} eq "group" && exists $rhs->{list}) && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $rhs->{"type"} and $rhs->{"type"} eq "group" and exists $rhs->{"list"} and do { if (exists $rhs->{"class4list"}) { $nextclause = $self->printRhs($rulename, $rhs, $options); $options->{"status"} = 2; } else { mydie("writeMarpaRuleList : unexpected embedded 'list'"); } ; last SWITCH } 0 59 59
Condition50%3/6
exists $rhs->{"type"} and $rhs->{"type"} eq "group" and exists $rhs->{"list"}
!ll&&!rl&&r
5900
exists $rhs->{"type"} and $rhs->{"type"} eq "group"
!ll&&!rl&&r
34250
MC/DC0%0/3
exists $rhs->{"type"} and $rhs->{"type"} eq "group" and exists $rhs->{"list"}
exists $rhs->{"type"} $rhs->{"type"} eq "group" exists $rhs->{"list"}
Truth table50%2/4
A and B and C
A= exists $rhs->{"type"}B= $rhs->{"type"} eq "group"C= exists $rhs->{"list"}
ABCresult
0XX0
10X0
1100
1111
1739 0 ▶ if ( exists $rhs->{class4list})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rhs->{'class4list'}) { } 0 0 0
1740 {
1741 0 $nextclause = $self->printRhs( $rulename, $rhs, $options );
1742 0 $options->{status} = 2;
1743 }
1744 else
1745 {
1746 0 mydie "writeMarpaRuleList : unexpected embedded 'list'";
1747 }
1748 0 last SWITCH;
1749 };
1750 59 do {
1751 59 $nextclause = $self->printRhs( $rulename, $rhs, $options );
1752 59 $options->{status} = 2;
1753 59 last SWITCH;
1754 };
1755 }
1756  
1757 59 $options->{delimiter}= "|";
1758  
1759 59 ▶ if ($linelen + length($nextclause) > $self->{indent} + 3 + 125)
Branch: 1 branch
BranchTrueFalseTotal
if ($linelen + length($nextclause) > $self->{'indent'} + 3 + 125) { } 0 59 59
1760 {
1761 0 $outputline .= "\n" . (' ' x $self->{indent}) . (' ' x 3) . ' ' . $nextclause;
1762 0 $linelen = $self->{indent} + 3 + length($nextclause);
1763 }
1764 else
1765 {
1766 59 $outputline .= $nextclause;
1767 59 $linelen += length($nextclause);
1768 }
1769 }
1770  
1771 33 ▶ if (defined $actiontext)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $actiontext) 0 33 33
1772 {
1773 0 my $padlen = $self->{indent} + 40 - length($outputline);
1774 0 ▶ $padlen = 1 if $padlen < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $padlen < 0 0 0 0
1775 0 $outputline .= ' ' x $padlen . "action => ${actiontext}";
1776 0 $actiontext = undef;
1777 }
1778  
1779 33 $options->{status} = 1;
1780 33 ▶ printf $outf "%s\n", $outputline if length($outputline) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $outputline > 0 33 0 33
1781 33 $linelen = 0;
1782 }
1783  
1784 22 ▶ if (exists $options->{comments})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"comments"}) 0 22 22
1785 {
1786 0 for my $cl (@{$options->{comments}})
1787 {
1788 0 printf $outf "%s\n", $cl;
1789 }
1790 0 delete $options->{comments};
1791 }
1792 }
1793  
1794 ##
1795 # writeMarpaRule : print a complute rule :
1796 # - rule name + 1st branch
1797 # - all subsequent branches
1798 ##
1799 sub writeMarpaRule
1800 {
1801 32 ▶ my ($self, $rulename ) = @_;
Subroutine writeMarpaRule: called CC=15 SCAR=27.6
Pod undocumented
1802  
1803 32 ▶ $self->dumpMarpaRule($rulename) if $self->{verbosity};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbosity"} 0 32 32
1804  
1805 32 $self->tagMarpaRule($rulename);
1806  
1807 # if ( $rulename eq "IDENTIFIER")
1808 # {
1809 # printf "found!\n";
1810 # }
1811  
1812 32 my $rule = $self->getMarpaRule(undef, $rulename);
1813  
1814 32 ▶ my $cardinality = $rule->{cardinality} if exists $rule->{cardinality};
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{"cardinality"} 9 23 32
1815  
1816 32 my $options = { delimiter => "", status => 0 };
1817 32 ▶ $options->{cardinality} = $cardinality if defined $cardinality;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cardinality 9 23 32
1818 32 ▶ $options->{assignop} = '~' if exists $rule->{isLexeme} || exists $rule->{isFragmentOrChild} || exists $rule->{grammarstate};
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{"isLexeme"} or exists $rule->{"isFragmentOrChild"} or exists $rule->{"grammarstate"} 18 9 27
Condition100%6/6
exists $rule->{"isLexeme"} or exists $rule->{"isFragmentOrChild"} or exists $rule->{"grammarstate"}
l!l&&r!l&&!r
1589
exists $rule->{"isLexeme"} or exists $rule->{"isFragmentOrChild"}
l!l&&r!l&&!r
51017
MC/DC100%3/3
exists $rule->{"isLexeme"} or exists $rule->{"isFragmentOrChild"} or exists $rule->{"grammarstate"}
exists $rule->{"isLexeme"} exists $rule->{"isFragmentOrChild"} exists $rule->{"grammarstate"}
Truth table100%4/4
A or B or C
A= exists $rule->{"isLexeme"}B= exists $rule->{"isFragmentOrChild"}C= exists $rule->{"grammarstate"}
ABCresult
1XX1
01X1
0011
0000
1819  
1820 32 my $outputline = "";
1821 32 my $outf = $self->{outf};
1822  
1823 32 my $printoutputcreated = 1;
1824  
1825 SWITCH:
1826 {
1827 ##
1828 # discard rules that implement a case-insensitive single-letter literal
1829 # if we are building case-insensitive keyword literals.
1830 ##
1831 32 ▶ ($self->testoption('buildkeywords') && $self->isKeywordLetter($rulename, $rule)) && do {
Branch: 1 branch
BranchTrueFalseTotal
$self->testoption("buildkeywords") and $self->isKeywordLetter($rulename, $rule) and do { $self->deleteMarpaRule($rulename); $printoutputcreated = 0; last SWITCH } 0 32 32
Condition33%1/3
$self->testoption("buildkeywords") and $self->isKeywordLetter($rulename, $rule)
!ll&&!rl&&r
0320
MC/DC0%0/2
$self->testoption("buildkeywords") and $self->isKeywordLetter($rulename, $rule)
$self->testoption("buildkeywords") $self->isKeywordLetter($rulename, $rule)
Truth table33%1/3
A and B
A= $self->testoption("buildkeywords")B= $self->isKeywordLetter($rulename, $rule)
ABresult
0X0
100
111
1832 0 $self->deleteMarpaRule($rulename);
1833 0 $printoutputcreated = 0;
1834 0 last SWITCH;
1835 };
1836 #(exists $rule->{isFragmentOrChild} && $self->testoption('fragment2class') && fragmentEligible2Convert($rule)) && do {
1837 ##
1838 # conditionally translate eligible fragments to classes if the fragment is not tagged with a '?' quantifier
1839 ##
1840 32 ▶ ($self->testoption('fragment2class') && $rulename !~ /^opt_/ && fragmentEligible2Convert($rule)) && do {
Branch: 1 branch
BranchTrueFalseTotal
$self->testoption("fragment2class") and not $rulename =~ /^opt_/ and fragmentEligible2Convert($rule) and do { $self->writeFragmentAsClass($rulename, $rule, $options); last SWITCH } 1 31 32
Condition83%5/6
$self->testoption("fragment2class") and not $rulename =~ /^opt_/ and fragmentEligible2Convert($rule)
!ll&&!rl&&r
4271
$self->testoption("fragment2class") and not $rulename =~ /^opt_/
!ll&&!rl&&r
0428
MC/DC66%2/3
$self->testoption("fragment2class") and not $rulename =~ /^opt_/ and fragmentEligible2Convert($rule)
$self->testoption("fragment2class") not $rulename =~ /^opt_/ fragmentEligible2Convert($rule)
Truth table75%3/4
A and not(B) and C
A= $self->testoption("fragment2class")B= not $rulename =~ /^opt_/C= fragmentEligible2Convert($rule)
ABCresult
0XX0
10X0
1100
1111
1841 1 $self->writeFragmentAsClass($rulename, $rule, $options );
1842 1 last SWITCH;
1843 };
1844 ##
1845 # print a rule with alternative branches
1846 ##
1847 31 ▶ (exists $rule->{list}) && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $rule->{"list"} and do { $self->writeMarpaRuleList($rulename, $rule, $options); last SWITCH } 22 9 31
1848 22 $self->writeMarpaRuleList($rulename, $rule, $options );
1849 22 last SWITCH;
1850 };
1851 ##
1852 # print a simple rule
1853 ##
1854 9 do {
1855 9 $outputline = $self->printRhs( $rulename, $rule, $options );
1856 9 ▶ $printoutputcreated = 0 if !length($outputline);
Branch: 1 branch
BranchTrueFalseTotal
unless length $outputline 0 9 9
1857 9 last SWITCH;
1858 };
1859 }
1860  
1861 32 ▶ printf $outf "%s\n", $outputline if length($outputline) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $outputline > 0 9 23 32
1862  
1863 32 return $printoutputcreated;
1864 }
1865  
1866 sub dumpMarpaRule
1867 {
1868 0 ▶ my ( $self, $rulename ) = @_;
Subroutine dumpMarpaRule: not called CC=1 SCAR=6.9
Pod undocumented
1869  
1870 0 my $marparule = $self->getMarpaRule(undef, $rulename);
1871 0 $self->dumpStructure( sprintf("=== %s\n", $rulename), $marparule);
1872 }
1873  
1874 ##
1875 # 'computeIndentation' : compute the maximum rulename length
1876 ##
1877 sub computeIndentation
1878 {
1879 1 ▶ my ($self) = @_;
Subroutine computeIndentation: called CC=10 SCAR=23.2
Pod undocumented
1880  
1881 1 my $symboltable = $self->{symboltable};
1882 1 my $ruletable = $symboltable->ruletable;
1883 1 my $subrules = $self->{subrules};
1884  
1885 1 my $indent = -1;
1886 1 for my $rule (@$ruletable)
1887 {
1888 16 ▶ next if !defined $rule || !exists $rule->{name} || !exists $rule->{generationstatus};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rule and exists $rule->{"name"} and exists $rule->{"generationstatus"} 0 14 14
Condition66%4/6
defined $rule and exists $rule->{"name"} and exists $rule->{"generationstatus"}
!ll&&!rl&&r
2014
defined $rule and exists $rule->{"name"}
!ll&&!rl&&r
2014
MC/DC33%1/3
defined $rule and exists $rule->{"name"} and exists $rule->{"generationstatus"}
defined $rule exists $rule->{"name"} exists $rule->{"generationstatus"}
Truth table50%2/4
A and B and C
A= defined $ruleB= exists $rule->{"name"}C= exists $rule->{"generationstatus"}
ABCresult
0XX0
10X0
1100
1111
1889 14 my $rulename = $rule->{name};
1890 14 ▶ $indent = length($rulename) if length($rulename) > $indent;
Branch: 1 branch
BranchTrueFalseTotal
if length $rulename > $indent 1 13 14
1891 14 ▶ next if !exists $subrules->{$rulename};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $subrules->{$rulename} 6 8 14
1892 8 for my $subrulename (@{$subrules->{$rulename}})
1893 {
1894 18 ▶ $indent = length($subrulename) if length($subrulename) > $indent;
Branch: 1 branch
BranchTrueFalseTotal
if length $subrulename > $indent 3 15 18
1895 }
1896 }
1897  
1898 1 ▶ mydie "INTERNAL: couldn't compute rhs indentation" if $indent == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $indent == -1 0 1 1
1899  
1900 1 $self->{indent} = $indent;
1901 }
1902  
1903 sub generateGenericOptions
1904 {
1905 1 ▶ my ($self) = @_;
Subroutine generateGenericOptions: called CC=1 SCAR=0.0
Pod undocumented
1906 1 my $outf = $self->{outf};
1907 1 printf $outf "%s\n\n", "lexeme default = latm => 1";
1908 }
1909  
1910 sub generateStartClause
1911 {
1912 1 ▶ my ($self) = @_;
Subroutine generateStartClause: called CC=2 SCAR=7.0
Pod undocumented
1913  
1914 1 my $startrule = $self->symboltable->startrule;
1915 1 ▶ return if !defined $startrule;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $startrule 0 1 1
1916  
1917 1 my $outf = $self->{outf};
1918  
1919 1 printf $outf "%s %-3s %s\n\n", $self->pad(":start"), "::=", $startrule->{name};
1920 }
1921  
1922 sub convertRedirectToDiscard
1923 {
1924 1 ▶ my ($self) = @_;
Subroutine convertRedirectToDiscard: called CC=5 SCAR=16.6
Pod undocumented
1925  
1926 1 ▶ return if !exists $self->{discarded} || ref $self->{discarded} ne "ARRAY" || scalar @{$self->{discarded}} < 1;
Branch: 1 branch
BranchTrueFalseTotal
if not exists $self->{"discarded"} or ref $self->{"discarded"} ne "ARRAY" or scalar @{$self->{"discarded"};} < 1 0 1 1
Condition33%2/6
not exists $self->{"discarded"} or ref $self->{"discarded"} ne "ARRAY" or scalar @{$self->{"discarded"};} < 1
l!l&&r!l&&!r
001
not exists $self->{"discarded"} or ref $self->{"discarded"} ne "ARRAY"
l!l&&r!l&&!r
001
MC/DC0%0/3
not exists $self->{"discarded"} or ref $self->{"discarded"} ne "ARRAY" or scalar @{$self->{"discarded"};} < 1
not exists $self->{"discarded"} ref $self->{"discarded"} ne "ARRAY" scalar @{$self->{"discarded"};} < 1
Truth table25%1/4
not(A) or B or C
A= not exists $self->{"discarded"}B= ref $self->{"discarded"} ne "ARRAY"C= scalar @{$self->{"discarded"};} < 1
ABCresult
1XX1
01X1
0011
0000
1927  
1928 1 my $outf = $self->{outf};
1929  
1930 1 printf $outf "# ---\n";
1931 1 printf $outf "# Discard rule from redirect options :\n";
1932 1 printf $outf "%s %-3s %s\n", $self->pad(":discard"), "~", "<discarded redirects>";
1933  
1934 1 my $lhs = "<discarded redirects>";
1935 1 my $delimiter = "~";
1936 1 for my $rulename (@{$self->{discarded}})
1937 {
1938 1 printf $outf "%s %-3s %s\n", $self->pad($lhs), $delimiter, $rulename;
1939 1 $lhs = "";
1940 1 $delimiter = "|";
1941 }
1942  
1943 1 printf $outf "# ---\n";
1944 }
1945  
1946 ## -------------------------------------------------------------------------------------
1947 # 'writeMarpaGrammar' create an output file with the collected rules in Marpa syntax.
1948 # the sequence of rules is identical to that from the original
1949 # input files. Subrules are aligned with parent rules.
1950 ## -------------------------------------------------------------------------------------
1951 sub writeMarpaGrammar
1952 {
1953 1 ▶ my ($self) = @_;
Subroutine writeMarpaGrammar: called CC=21 SCAR=33.0
Pod undocumented
1954  
1955 1 my $symboltable = $self->{symboltable};
1956 1 my $startrule = $symboltable->startrule;
1957 1 my $startrulename = $startrule->{name};
1958  
1959 1 my $ruletable = $symboltable->ruletable;
1960 1 my $subrules = $self->{subrules};
1961  
1962 1 $self->computeIndentation;
1963  
1964 1 $self->openOutputFile;
1965  
1966 1 $self->generateGenericOptions;
1967 1 $self->generateStartClause;
1968  
1969 # create a ':discard' rule from the rules
1970 # tagged 'redirect' at the top of the
1971 # output file.
1972 1 $self->convertRedirectToDiscard;
1973  
1974 1 my $outf = $self->{outf};
1975  
1976 ##
1977 # print rules in the order of the input file
1978 ##
1979 1 my $status = 0;
1980 1 my $subrulestatus = 0;
1981 1 my $rulegeneratedoutput = 1;
1982  
1983 1 for my $rule (@$ruletable)
1984 {
1985 16 ▶ if (exists $rule->{comment} && !$self->testoption('stripallcomments'))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"comment"} and not $self->testoption("stripallcomments")) 0 16 16
Condition33%1/3
exists $rule->{"comment"} and not $self->testoption("stripallcomments")
!ll&&!rl&&r
1600
MC/DC0%0/2
exists $rule->{"comment"} and not $self->testoption("stripallcomments")
exists $rule->{"comment"} not $self->testoption("stripallcomments")
Truth table33%1/3
A and not(B)
A= exists $rule->{"comment"}B= not $self->testoption("stripallcomments")
ABresult
0X0
100
111
1986 {
1987 0 my $comment = $rule->{comment};
1988 0 ▶ $comment = [$comment] if ref $comment ne "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if ref $comment ne "ARRAY" 0 0 0
1989 0 my $cl = $self->renderComment("grammar", $comment);
1990 0 for my $s (@$cl)
1991 {
1992 0 printf $outf "%s\n", $s;
1993 }
1994 }
1995  
1996 16 ▶ next if !defined $rule || !exists $rule->{name} || !exists $rule->{generationstatus};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rule and exists $rule->{"name"} and exists $rule->{"generationstatus"} 2 14 16
Condition66%4/6
defined $rule and exists $rule->{"name"} and exists $rule->{"generationstatus"}
!ll&&!rl&&r
2014
defined $rule and exists $rule->{"name"}
!ll&&!rl&&r
0214
MC/DC33%1/3
defined $rule and exists $rule->{"name"} and exists $rule->{"generationstatus"}
defined $rule exists $rule->{"name"} exists $rule->{"generationstatus"}
Truth table50%2/4
A and B and C
A= defined $ruleB= exists $rule->{"name"}C= exists $rule->{"generationstatus"}
ABCresult
0XX0
10X0
1100
1111
1997  
1998 14 my $rulename = $rule->{name};
1999  
2000 14 ▶ if ( !$status )
Branch: 1 branch
BranchTrueFalseTotal
unless ($status) 1 13 14
2001 {
2002 1 ▶ printf "WARNING : first rule '%s' is not startrule '%s'\n", $rulename, $startrulename if $rulename ne $startrulename;
Branch: 1 branch
BranchTrueFalseTotal
if $rulename ne $startrulename 0 1 1
2003 1 $status = 1;
2004 }
2005  
2006 ##
2007 # create a block of text for a rule with its subrules
2008 ##
2009 14 ▶ printf $outf "\n" if exists $subrules->{$rulename} && !$subrulestatus && $rulegeneratedoutput;
Branch: 1 branch
BranchTrueFalseTotal
if exists $subrules->{$rulename} and not $subrulestatus and $rulegeneratedoutput 4 10 14
Condition83%5/6
exists $subrules->{$rulename} and not $subrulestatus and $rulegeneratedoutput
!ll&&!rl&&r
1004
exists $subrules->{$rulename} and not $subrulestatus
!ll&&!rl&&r
644
MC/DC66%2/3
exists $subrules->{$rulename} and not $subrulestatus and $rulegeneratedoutput
exists $subrules->{$rulename} not $subrulestatus $rulegeneratedoutput
Truth table75%3/4
A and not(B) and C
A= exists $subrules->{$rulename}B= not $subrulestatusC= $rulegeneratedoutput
ABCresult
0XX0
10X0
1100
1111
2010 14 $subrulestatus = 0;
2011  
2012 14 $rulegeneratedoutput = $self->writeMarpaRule($rulename);
2013  
2014 ##
2015 # print the generated subrules immediately after the parent rule
2016 ##
2017 14 ▶ if (exists $subrules->{$rulename})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $subrules->{$rulename}) 8 6 14
2018 {
2019 8 my $subrulegeneratedoutput = 0;
2020  
2021 8 for my $subrulename (@{$subrules->{$rulename}})
2022 {
2023 18 $subrulegeneratedoutput += $self->writeMarpaRule($subrulename);
2024 }
2025  
2026 8 ▶ printf $outf "\n" if $subrulegeneratedoutput;
Branch: 1 branch
BranchTrueFalseTotal
if $subrulegeneratedoutput 8 0 8
2027 8 $subrulestatus = 1;
2028 }
2029 }
2030  
2031 ##
2032 # print the rest (if any)
2033 ##
2034 1 for my $rulename (sort keys %{$self->{rules}})
2035 {
2036 32 my $g4rule = $symboltable->rule($rulename);
2037 32 ▶ next if !exists $g4rule->{generationstatus};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $g4rule->{"generationstatus"} 18 14 32
2038  
2039 14 my $marparule = $self->getMarpaRule(undef, $rulename);
2040 14 ▶ next if exists $marparule->{status};
Branch: 1 branch
BranchTrueFalseTotal
if exists $marparule->{"status"} 14 0 14
2041  
2042 0 ▶ printf "%s\n", $rulename if $self->{verbosity};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbosity"} 0 0 0
2043 0 $self->writeMarpaRule($rulename);
2044 }
2045  
2046 1 $self->closeOutputFile;
2047 }
2048  
2049 # --------------------------------------------------- #
2050 # driver #
2051 # --------------------------------------------------- #
2052  
2053 sub generate
2054 {
2055 1 ▶ my ($self, $symboltable) = @_;
Subroutine generate: called CC=2 SCAR=7.0
Pod undocumented
2056  
2057 1 $self->{symboltable} = $symboltable;
2058  
2059 1 $self->translateG4Grammar;
2060 1 ▶ $self->dumpStructure("Rules", $self->{rules}) if $self->{verbosity} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbosity"} > 1 0 1 1
2061 1 $self->writeMarpaGrammar;
2062 }
2063  
2064 2 1;
2065  
2066 # ABSTRACT: translate parsed antlr4 rules to Marpa2 syntax
2067  
2068 =head1 SYNOPSIS
2069 use MarpaX::G4::MarpaGen;
2070 my $generator = new MarpaX::MarpaGen;
2071 $generator->generate($symboltable);
2072 =head1 DESCRIPTION
2073 Translate the rules from the symbol table created from the imported ANTLR4 grammar
2074 into Marpa syntax and write them to output.
2075  
2076 use MarpaX::Symboltable;
2077 my $symboltable = new MarpaX::Symboltable;
2078  
2079 my $grammartext = readFile($infile);
2080 my $data = MarpaX::G4::parse_rules($grammartext);
2081 $symboltable->importParseTree($data);
2082 $symboltable->validateSymbolTable();
2083  
2084 my $generator = new MarpaX::MarpaGen;
2085  
2086 $generator->stripallcomments if exists $options->{c};
2087 $generator->embedactions if exists $options->{e};
2088 $generator->fragment2class if exists $options->{f};
2089 $generator->shiftlazytogreedy if exists $options->{g};
2090 $generator->buildkeywords if exists $options->{k};
2091 $generator->stripactions if exists $options->{p};
2092 $generator->setVerbosity(2) if exists $options->{t};
2093 $generator->matchcaseinsensitive if exists $options->{u} || exists $options->{k};
2094  
2095 my $outputfile = '-';
2096 $outputfile = $options->{o} if exists $options->{o};
2097 $generator->setoutputfile($outputfile);
2098  
2099 $generator->generate($symboltable);
2100 =cut
« blib/lib/MarpaX/G4/Symboltable.pm ↑ Summary blib/lib/MarpaX/G4/Parser.pm »