lib/MarpaX/G4/Symboltable.pm

filter: Statementstmt 29.3% 68 / 232 Branchbran 19.3% 19 / 98 Conditioncond 5.7% 5 / 87 MC/DCmcdc 2.0% 1 / 49 Subroutinesub 56.5% 13 / 23 Podpod 0.0% 0 / 19 totaltotal 20.8% 106 / 508 CC 86 SCAR 57.6
CC
86
Cov
22%
walktoken
870.0
validateSymbolTable
420.0
walksubrule
42.0
CRAP 318.7
↑ Summary lib/MarpaX/G4/MarpaGen.pm »
1 # ----------------------------------------------------------------------------------------------------- #
2 # MarpaX::Symboltable #
3 # #
4 # manage a symbol table with rules parsed from an antlr4 grammar. #
5 # #
6 # ----------------------------------------------------------------------------------------------------- #
7  
8 package MarpaX::G4::Symboltable;
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 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use MarpaX::G4::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub new
16 1 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
17 1 my $invocant = shift;
18 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
19 1 my $self = {}; # initiate our handy hashref
20 1 bless($self,$class); # make it usable
21  
22 1 $self->{symboltable} = {};
23 1 $self->{startrule} = undef;
24 1 $self->{currentidx} = -1;
25 1 $self->{ruletable} = [];
26  
27 1 return $self;
28 }
29  
30 2 ▶sub symbols { my ($self) = @_; return keys %{$self->{symboltable}}; }
Subroutine symbols: called CC=1 SCAR=0.0
Pod undocumented
31 3 ▶sub startrule { my ($self) = @_; return $self->{startrule}; }
Subroutine startrule: called CC=1 SCAR=0.0
Pod undocumented
32 2 ▶sub ruletable { my ($self) = @_; return $self->{ruletable}; }
Subroutine ruletable: called CC=1 SCAR=0.0
Pod undocumented
33  
34 sub setStartRule
35 0 ▶{
Subroutine setStartRule: not called CC=2 SCAR=17.9
Pod undocumented
36 0 my ($self, $rulename) = @_;
37 0 ▶ die "can't set non-existent rule $rulename as start rule" if !exists $self->{symboltable}{$rulename};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"symboltable"}{$rulename} 0 0 0
38 0 my $symbol = $self->{symboltable}{$rulename};
39 0 $self->{startrule} = { name => $rulename, index => $symbol->{index} };
40 }
41  
42 sub rule
43 85 ▶{
Subroutine rule: called CC=5 SCAR=17.2
Pod undocumented
44 85 my ($self, $rulename) = @_;
45 85 ▶ $self->addEOF() if $rulename eq "EOF" && !exists $self->{symboltable}{$rulename};
Branch: 1 branch
BranchTrueFalseTotal
if $rulename eq "EOF" and not exists $self->{"symboltable"}{$rulename} 0 85 85
Condition33%1/3
$rulename eq "EOF" and not exists $self->{"symboltable"}{$rulename}
!ll&&!rl&&r
8500
MC/DC0%0/2
$rulename eq "EOF" and not exists $self->{"symboltable"}{$rulename}
$rulename eq "EOF" not exists $self->{"symboltable"}{$rulename}
Truth table33%1/3
A and not(B)
A= $rulename eq "EOF"B= not exists $self->{"symboltable"}{$rulename}
ABresult
0X0
100
111
46 85 ▶ return undef if !defined $rulename || !exists $self->{symboltable}{$rulename};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rulename and exists $self->{"symboltable"}{$rulename} 18 67 85
Condition66%2/3
defined $rulename and exists $self->{"symboltable"}{$rulename}
!ll&&!rl&&r
01867
MC/DC50%1/2
defined $rulename and exists $self->{"symboltable"}{$rulename}
defined $rulename exists $self->{"symboltable"}{$rulename}
Truth table66%2/3
A and B
A= defined $rulenameB= exists $self->{"symboltable"}{$rulename}
ABresult
0X0
100
111
47 67 return $self->{symboltable}{$rulename};
48 }
49  
50 sub tagrule
51 28 ▶{
Subroutine tagrule: called CC=3 SCAR=11.4
Pod undocumented
52 28 my ($self, $rulename, $status) = @_;
53 28 ▶ die "trying to tag nonexistent rule '$rulename'" if !exists $self->{symboltable}{$rulename};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"symboltable"}{$rulename} 0 28 28
54 28 my $symbol = $self->{symboltable}{$rulename};
55 28 ▶ $symbol->{generationstatus} = defined($status) ? $status : 'todo';
Branch: 1 branch
BranchTrueFalseTotal
defined $status ? : 28 0 28
56 }
57  
58 ##
59 # create a synthetic 'EOF' token, so that the grammar won't fail.
60 ##
61 sub addEOF
62 0 ▶{
Subroutine addEOF: not called CC=1 SCAR=6.9
Pod undocumented
63 0 my ($self) = @_;
64  
65 0 $self->addRule( -1,
66 {
67 name => 'EOF',
68 type => 'fragment',
69 generationstatus => 'synthetic',
70 'rightsides' => [
71 {
72 'rhs' => {
73 'token' => {
74 'value' => '\z',
75 'type' => 'literal'
76 }
77 }
78 }
79 ],
80 });
81 }
82  
83 sub rulestatus
84 23 ▶{
Subroutine rulestatus: called CC=5 SCAR=17.2
Pod undocumented
85 23 my ($self, $rulename, $status) = @_;
86  
87 23 ▶ $self->addEOF() if $rulename eq "EOF" && !exists $self->{symboltable}{$rulename};
Branch: 1 branch
BranchTrueFalseTotal
if $rulename eq "EOF" and not exists $self->{"symboltable"}{$rulename} 0 23 23
Condition33%1/3
$rulename eq "EOF" and not exists $self->{"symboltable"}{$rulename}
!ll&&!rl&&r
2300
MC/DC0%0/2
$rulename eq "EOF" and not exists $self->{"symboltable"}{$rulename}
$rulename eq "EOF" not exists $self->{"symboltable"}{$rulename}
Truth table33%1/3
A and not(B)
A= $rulename eq "EOF"B= not exists $self->{"symboltable"}{$rulename}
ABresult
0X0
100
111
88  
89 23 ▶ die "trying to query nonexistent rule '$rulename'" if !exists $self->{symboltable}{$rulename};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"symboltable"}{$rulename} 0 23 23
90 23 my $symbol = $self->{symboltable}{$rulename};
91 23 ▶ return exists $symbol->{generationstatus} ? $symbol->{generationstatus} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $symbol->{'generationstatus'} ? : 9 14 23
92 }
93  
94 ## -----------
95 # import the parse tree into the symbol table
96 ## -----------
97 sub importParseTree
98 1 ▶{
Subroutine importParseTree: called CC=8 SCAR=23.4
Pod undocumented
99 1 my ($self, $tree) = @_;
100  
101 1 ▶ die "parse tree must be an array of rules" if ref($tree) ne "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if ref $tree ne "ARRAY" 0 1 1
102 1 my $ruleindex = $self->{currentidx};
103  
104 1 for my $rule (@$tree)
105 {
106 16 ++$ruleindex;
107 16 ▶ die "rule[$ruleindex] is not a hash" if ref($rule) ne "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $rule ne "HASH" 0 16 16
108  
109 SWITCH: {
110 16 ▶ (exists $rule->{name}) && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $rule->{"name"} and do { my $name = $rule->{"name"}; $self->addRule($ruleindex, $rule); $self->{"startrule"} = {"name", $name, "index", $ruleindex} unless defined $self->{"startrule"}; last SWITCH } 14 2 16
111 14 my $name = $rule->{name};
112 # printf "rule[$ruleindex] : %s\n", $name;
113 14 $self->addRule($ruleindex, $rule);
114 14 ▶ $self->{startrule} = { name => $name, index => $ruleindex } if !defined $self->{startrule};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"startrule"} 1 13 14
115 14 last SWITCH;
116 };
117 2 ▶ (exists $rule->{grammarspec}) && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $rule->{"grammarspec"} and do { last SWITCH } 2 0 2
118 # printf "rule[$ruleindex] : grammar %s\n", $rule->{grammarspec};
119 2 last SWITCH;
120 };
121 0 ▶ (exists $rule->{comment}) && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $rule->{"comment"} and do { $self->addComment($ruleindex, $rule); last SWITCH } 0 0 0
122 # printf "rule[$ruleindex] : comment\n";
123 0 $self->addComment($ruleindex, $rule);
124 0 last SWITCH;
125 };
126 0 do {
127 0 die "rule[$ruleindex] : can't process";
128 0 last SWITCH;
129 };
130 }
131 }
132  
133 1 $self->{currentidx} = $ruleindex;
134 }
135  
136 sub addRule
137 14 ▶{
Subroutine addRule: called CC=4 SCAR=14.9
Pod undocumented
138 14 my ($self, $ruleindex, $rule) = @_;
139  
140 14 my $name = $rule->{name};
141 14 my $symboltable = \%{$self->{symboltable}};
142  
143 SWITCH: {
144 14 ▶ (exists $rule->{rightsides}) && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $rule->{"rightsides"} and do { die "$name is a duplicate rule" if exists $symboltable->{$name}; $rule->{"index"} = $ruleindex; $symboltable->{$name} = $rule; last SWITCH } 14 0 14
145 14 ▶ die "$name is a duplicate rule" if exists $symboltable->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $symboltable->{$name} 0 14 14
146 14 $rule->{index} = $ruleindex;
147 14 $symboltable->{$name} = $rule;
148 14 last SWITCH;
149 };
150 0 do {
151 0 die "can't import rule[$ruleindex] : $name";
152 0 last SWITCH;
153 };
154 }
155  
156 # add the rule to the index-based lookup table if it is not a synthetic rule.
157 14 ▶ $self->{ruletable}->[$ruleindex] = $rule if $ruleindex != -1;
Branch: 1 branch
BranchTrueFalseTotal
if $ruleindex != -1 14 0 14
158 }
159  
160 sub addComment
161 0 ▶{
Subroutine addComment: not called CC=1 SCAR=6.9
Pod undocumented
162 0 my ($self, $ruleindex, $rule) = @_;
163 0 $self->{ruletable}->[$ruleindex] = $rule;
164 }
165  
166 ## -----------
167 # recursively walk the symbol table to verify consistency
168 ## -----------
169  
170 sub walkgroup
171 0 ▶{
Subroutine walkgroup: not called CC=5 SCAR=34.0
Pod undocumented
172 0 my ($rulename, $tokengroup) = @_;
173  
174 0 my $namelist = [];
175  
176 0 my $definition = $tokengroup->{definition};
177 0 for my $e (@$definition)
178 {
179 0 ▶ if (ref $e->{token} eq "")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e->{'token'} eq '') { } 0 0 0
180 {
181 0 push @$namelist, $e->{token};
182 }
183 else
184 {
185 0 ▶ if (ref $e eq "HASH" && exists $e->{token})
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e eq 'HASH' and exists $e->{'token'}) { } 0 0 0
Condition0%0/3
ref $e eq "HASH" and exists $e->{"token"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $e eq "HASH" and exists $e->{"token"}
ref $e eq "HASH" exists $e->{"token"}
Truth table0%0/3
A and B
A= ref $e eq "HASH"B= exists $e->{"token"}
ABresult
0X0
100
111
186 {
187 0 my $sr = walktoken($rulename, $e->{token});
188 0 push (@$namelist, @$sr);
189 }
190 else
191 {
192 0 $Data::Dumper::Indent = 1;
193 0 print Dumper($tokengroup);
194 0 die "can't process group for rule $rulename";
195 }
196 }
197 }
198  
199 0 return $namelist;
200 }
201  
202 sub walktoken
203 0 ▶{
Subroutine walktoken: not called CC=29 SCAR=67.7
Pod undocumented
204 0 my ($rulename, $token) = @_;
205  
206 0 my $namelist = [];
207  
208 SWITCH:
209 {
210 0 ▶ (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 { my $sr = walkgroup($rulename, $token->{"token"}); push @$namelist, @$sr; last SWITCH } 0 0 0
Condition0%0/6
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "rulegroup"
!ll&&!rl&&r
000
ref $token eq "HASH" and exists $token->{"type"}
!ll&&!rl&&r
000
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 table0%0/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
211 0 my $sr = walkgroup($rulename, $token->{token});
212 0 push (@$namelist, @$sr);
213 0 last SWITCH;
214 };
215 0 ▶ (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 { my $sr = walkgroup($rulename, $token->{"token"}); push @$namelist, @$sr; last SWITCH } 0 0 0
Condition0%0/6
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "tokengroup"
!ll&&!rl&&r
000
ref $token eq "HASH" and exists $token->{"type"}
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/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
216 0 my $sr = walkgroup($rulename, $token->{token});
217 0 push (@$namelist, @$sr);
218 0 last SWITCH;
219 };
220 0 ▶ (ref $token eq "HASH" && exists $token->{token}) && do {
Branch: 1 branch
BranchTrueFalseTotal
ref $token eq "HASH" and exists $token->{"token"} and do { my $nestedtoken = $token->{"token"}; my $sr = walktoken($rulename, $nestedtoken); push @$namelist, @$sr; last SWITCH } 0 0 0
Condition0%0/3
ref $token eq "HASH" and exists $token->{"token"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $token eq "HASH" and exists $token->{"token"}
ref $token eq "HASH" exists $token->{"token"}
Truth table0%0/3
A and B
A= ref $token eq "HASH"B= exists $token->{"token"}
ABresult
0X0
100
111
221 0 my $nestedtoken = $token->{token};
222 0 my $sr = walktoken($rulename, $nestedtoken);
223 0 push (@$namelist, @$sr);
224 0 last SWITCH;
225 };
226 0 ▶ (ref $token eq "") && do {
Branch: 1 branch
BranchTrueFalseTotal
ref $token eq "" and do { push @$namelist, $token; last SWITCH } 0 0 0
227 0 push @$namelist, $token;
228 0 last SWITCH;
229 };
230 0 ▶ (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 { last SWITCH } 0 0 0
Condition0%0/6
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "literal"
!ll&&!rl&&r
000
ref $token eq "HASH" and exists $token->{"type"}
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/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
231 0 last SWITCH;
232 };
233 0 ▶ (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 { last SWITCH } 0 0 0
Condition0%0/6
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "class"
!ll&&!rl&&r
000
ref $token eq "HASH" and exists $token->{"type"}
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/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
234 0 last SWITCH;
235 };
236 0 ▶ (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 { last SWITCH } 0 0 0
Condition0%0/6
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "regex"
!ll&&!rl&&r
000
ref $token eq "HASH" and exists $token->{"type"}
!ll&&!rl&&r
000
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 table0%0/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
237 0 last SWITCH;
238 };
239 0 ▶ (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 { last SWITCH } 0 0 0
Condition0%0/6
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "range"
!ll&&!rl&&r
000
ref $token eq "HASH" and exists $token->{"type"}
!ll&&!rl&&r
000
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 table0%0/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
240 0 last SWITCH;
241 };
242 0 ▶ (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 { last SWITCH } 0 0 0
Condition0%0/6
ref $token eq "HASH" and exists $token->{"type"} and $token->{"type"} eq "value"
!ll&&!rl&&r
000
ref $token eq "HASH" and exists $token->{"type"}
!ll&&!rl&&r
000
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 table0%0/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
243 0 last SWITCH;
244 };
245 0 ▶ (ref $token eq "HASH" && exists $token->{comment}) && do {
Branch: 1 branch
BranchTrueFalseTotal
ref $token eq "HASH" and exists $token->{"comment"} and do { 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
246 0 last SWITCH;
247 };
248 0 ▶ (ref $token eq "HASH" && exists $token->{action}) && do {
Branch: 1 branch
BranchTrueFalseTotal
ref $token eq "HASH" and exists $token->{"action"} and do { 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
249 0 last SWITCH;
250 };
251 0 do {
252 0 $Data::Dumper::Indent = 1;
253 0 print Dumper($token);
254 0 die "can't process token for rule $rulename";
255 0 last SWITCH;
256 };
257 }
258  
259 0 return $namelist;
260 }
261  
262 sub walknonterminal
263 0 ▶{
Subroutine walknonterminal: not called CC=2 SCAR=17.9
Pod undocumented
264 0 my ( $rulename, $nonterminal ) = @_;
265  
266 0 my $namelist = [];
267  
268 SWITCH:
269 {
270 0 ▶ (exists $nonterminal->{rhs}) && do {
Branch: 1 branch
BranchTrueFalseTotal
exists $nonterminal->{"rhs"} and do { my $rhs = $nonterminal->{"rhs"}; my $sr = walktoken($rulename, $rhs); push @$namelist, @$sr; last SWITCH } 0 0 0
271 0 my $rhs = $nonterminal->{rhs};
272 0 my $sr = walktoken($rulename, $rhs);
273 0 push (@$namelist, @$sr);
274 0 last SWITCH;
275 };
276 0 do {
277 0 $Data::Dumper::Indent = 1;
278 0 print Dumper($nonterminal);
279 0 die "can't process nonterminal for rule $rulename";
280 0 last SWITCH;
281 };
282 }
283  
284 0 return $namelist;
285 }
286  
287 sub walksubrule
288 0 ▶{
Subroutine walksubrule: not called CC=6 SCAR=37.4
Pod undocumented
289 0 my ($rulename, $rule) = @_;
290  
291 0 ▶ if (ref $rule ne "HASH" || !exists $rule->{rightsides})
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule ne "HASH" or not exists $rule->{"rightsides"}) 0 0 0
Condition0%0/3
ref $rule ne "HASH" or not exists $rule->{"rightsides"}
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $rule ne "HASH" or not exists $rule->{"rightsides"}
ref $rule ne "HASH" not exists $rule->{"rightsides"}
Truth table0%0/3
A or not(B)
A= ref $rule ne "HASH"B= not exists $rule->{"rightsides"}
ABresult
1X1
011
000
292 {
293 0 $Data::Dumper::Indent = 1;
294 0 print Dumper($rule);
295 0 die "rule '$rulename' is not a hash";
296 }
297  
298 0 my $rhs = $rule->{rightsides};
299  
300 0 ▶ return [] if !defined $rhs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rhs 0 0 0
301  
302 0 ▶ if (ref $rhs ne "ARRAY")
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rhs ne "ARRAY") 0 0 0
303 {
304 0 $Data::Dumper::Indent = 1;
305 0 print Dumper($rhs);
306 0 die "'rhs' is not an array ref in '$rulename'";
307 }
308  
309 0 my $namelist = [];
310  
311 0 for my $r (@$rhs)
312 {
313 0 my $sr = walknonterminal($rulename, $r);
314 0 push (@$namelist, @$sr);
315 }
316  
317 0 return $namelist;
318 }
319  
320 sub joinReferences
321 0 ▶{
Subroutine joinReferences: not called CC=5 SCAR=34.0
Pod undocumented
322 0 my ($sr) = @_;
323  
324 0 my $temp = {};
325 0 my $result = "";
326 0 my $delim = "";
327  
328 0 for my $s (@$sr)
329 {
330 0 ▶ if (!exists $temp->{$s})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $temp->{$s}) 0 0 0
331 {
332 0 $temp->{$s} = 1;
333 0 my $len = 16 - length($s);
334 0 my $ts = $s;
335 0 ▶ if ($len < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($len < 0) 0 0 0
336 {
337 0 $len = 0;
338 0 $ts = substr($ts, 0, 16);
339 }
340 0 my $pad = "";
341 0 ▶ $pad = ' ' x $len if $len > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len > 0 0 0 0
342 0 $result .= $delim . $ts . $pad;
343 0 $delim = " ";
344 }
345 }
346  
347 0 return $result;
348 }
349  
350 sub verifySymbolNames
351 0 ▶{
Subroutine verifySymbolNames: not called CC=3 SCAR=24.8
Pod undocumented
352 0 my ($self, $rulename, $symbolnames ) = @_;
353  
354 0 my $symboltable = \%{$self->{symboltable}};
355  
356 0 for my $sn (@$symbolnames)
357 {
358 0 ▶ if (!exists $symboltable->{$sn})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $symboltable->{$sn}) 0 0 0
359 {
360 0 printf "[%-1s][%-45s][%-2s] missing from symbol table : %s\n", "", $rulename, "", $sn;
361 }
362 }
363 }
364  
365 sub validateSymbolTable
366 0 ▶{
Subroutine validateSymbolTable: not called CC=20 SCAR=60.4
Pod undocumented
367 0 my ($self) = @_;
368  
369 0 my $symboltable = \%{$self->{symboltable}};
370  
371 0 printf "===\n=== Composite Rules\n===\n\n";
372 0 printf <<'END_OF_SOURCE';
373 +-------------------------------------------------------- rule name
374 +--!-------------------------------------------------------- Fragment (F), Lexeme (L) or regular rule
375 ! ! +--------- redirected (->) or contributing rule
376 ! ! ! +----- number of rule references
377 ! ! ! ! +- list of rule references
378 ! ! ! ! !
379 V V V V V
380 END_OF_SOURCE
381  
382 0 for my $name (sort keys %$symboltable)
383 {
384 0 my $rule = $symboltable->{$name};
385  
386 SWITCH:
387 {
388 0 (exists $rule->{name}) && do
389 0 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
exists $rule->{"name"} and do { my $name = $rule->{"name"}; my $symbolreferences = walksubrule($name, $rule); if (scalar @$symbolreferences > 0) { my $strReferences = joinReferences($symbolreferences); my $type = ""; $type = "L" if exists $rule->{"isLexeme"} or exists $rule->{"grammarstate"} and $rule->{"grammarstate"} eq "lexer"; $type = "F" if exists $rule->{"type"} and $rule->{"type"} eq "fragment"; printf "[%-1s][%-45s][%-2s][%2d] %s\n", $type, $name, exists $rule->{"redirect"} ? "->" : "", scalar @$symbolreferences, $strReferences; $self->verifySymbolNames($name, $symbolreferences); } ; last SWITCH } 0 0 0
390 0 my $name = $rule->{name};
391 # if ($name eq "alter_table_properties")
392 # {
393 # printf "found!\n";
394 # }
395 0 my $symbolreferences = walksubrule($name, $rule);
396  
397 0 ▶ if (scalar @$symbolreferences > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$symbolreferences > 0) 0 0 0
398 {
399 0 my $strReferences = joinReferences($symbolreferences);
400 0 my $type = "";
401 0 ▶ $type = "L" if exists $rule->{isLexeme} || (exists $rule->{grammarstate} && $rule->{grammarstate} eq "lexer");
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{"isLexeme"} or exists $rule->{"grammarstate"} and $rule->{"grammarstate"} eq "lexer" 0 0 0
Condition0%0/6
exists $rule->{"isLexeme"} or exists $rule->{"grammarstate"} and $rule->{"grammarstate"} eq "lexer"
l!l&&r!l&&!r
000
exists $rule->{"grammarstate"} and $rule->{"grammarstate"} eq "lexer"
!ll&&!rl&&r
000
MC/DC0%0/3
exists $rule->{"isLexeme"} or exists $rule->{"grammarstate"} and $rule->{"grammarstate"} eq "lexer"
exists $rule->{"isLexeme"} exists $rule->{"grammarstate"} $rule->{"grammarstate"} eq "lexer"
Truth table0%0/4
A or B and C
A= exists $rule->{"isLexeme"}B= exists $rule->{"grammarstate"}C= $rule->{"grammarstate"} eq "lexer"
ABCresult
1XX1
0111
00X0
0100
402 0 ▶ $type = "F" if exists $rule->{type} && $rule->{type} eq "fragment";
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{"type"} and $rule->{"type"} eq "fragment" 0 0 0
Condition0%0/3
exists $rule->{"type"} and $rule->{"type"} eq "fragment"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $rule->{"type"} and $rule->{"type"} eq "fragment"
exists $rule->{"type"} $rule->{"type"} eq "fragment"
Truth table0%0/3
A and B
A= exists $rule->{"type"}B= $rule->{"type"} eq "fragment"
ABresult
0X0
100
111
403 0 ▶ printf "[%-1s][%-45s][%-2s][%2d] %s\n", $type, $name, (exists $rule->{redirect}) ? "->" : "", scalar @$symbolreferences, $strReferences;
Branch: 1 branch
BranchTrueFalseTotal
exists $rule->{'redirect'} ? : 0 0 0
404 0 $self->verifySymbolNames( $name, $symbolreferences );
405 }
406 0 last SWITCH;
407 };
408 do
409 0 {
410 0 die "can't process rule";
411 0 last SWITCH;
412 };
413 }
414 }
415  
416 0 printf "\n===\n=== Basic Rules\n===\n\n";
417 0 printf <<'END_OF_SOURCE';
418 +-------------------------------------------------------- rule name
419 +--!-------------------------------------------------------- Fragment (F), Lexeme (L) or regular rule
420 ! ! +--------- redirected (->) or contributing rule
421 ! ! ! +----- n/a
422 ! ! ! !
423 V V V V
424 END_OF_SOURCE
425  
426 0 for my $name (sort keys %$symboltable)
427 {
428 0 my $rule = $symboltable->{$name};
429  
430 SWITCH:
431 {
432 0 (exists $rule->{name}) && do
433 0 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
exists $rule->{"name"} and do { my $name = $rule->{"name"}; my $symbolreferences = walksubrule($name, $rule); if ($name eq "TILDE_OPERATOR_PART") { printf "found!\n"; } ; if (scalar @$symbolreferences == 0) { my $type = ""; $type = "L" if exists $rule->{"isLexeme"} or exists $rule->{"grammarstate"} and $rule->{"grammarstate"} eq "lexer"; $type = "F" if exists $rule->{"type"} and $rule->{"type"} eq "fragment"; printf "[%-1s][%-45s][%-2s][%2s] %s\n", $type, $name, exists $rule->{"redirect"} ? "->" : "", "", ""; } ; last SWITCH } 0 0 0
434 0 my $name = $rule->{name};
435 0 my $symbolreferences = walksubrule($name, $rule);
436  
437 0 ▶ if ($name eq "TILDE_OPERATOR_PART")
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "TILDE_OPERATOR_PART") 0 0 0
438 {
439 0 printf "found!\n";
440 }
441 0 ▶ if (scalar @$symbolreferences == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$symbolreferences == 0) 0 0 0
442 {
443 0 my $type = "";
444 0 ▶ $type = "L" if exists $rule->{isLexeme} || (exists $rule->{grammarstate} && $rule->{grammarstate} eq "lexer");
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{"isLexeme"} or exists $rule->{"grammarstate"} and $rule->{"grammarstate"} eq "lexer" 0 0 0
Condition0%0/6
exists $rule->{"isLexeme"} or exists $rule->{"grammarstate"} and $rule->{"grammarstate"} eq "lexer"
l!l&&r!l&&!r
000
exists $rule->{"grammarstate"} and $rule->{"grammarstate"} eq "lexer"
!ll&&!rl&&r
000
MC/DC0%0/3
exists $rule->{"isLexeme"} or exists $rule->{"grammarstate"} and $rule->{"grammarstate"} eq "lexer"
exists $rule->{"isLexeme"} exists $rule->{"grammarstate"} $rule->{"grammarstate"} eq "lexer"
Truth table0%0/4
A or B and C
A= exists $rule->{"isLexeme"}B= exists $rule->{"grammarstate"}C= $rule->{"grammarstate"} eq "lexer"
ABCresult
1XX1
0111
00X0
0100
445 0 ▶ $type = "F" if exists $rule->{type} && $rule->{type} eq "fragment";
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule->{"type"} and $rule->{"type"} eq "fragment" 0 0 0
Condition0%0/3
exists $rule->{"type"} and $rule->{"type"} eq "fragment"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $rule->{"type"} and $rule->{"type"} eq "fragment"
exists $rule->{"type"} $rule->{"type"} eq "fragment"
Truth table0%0/3
A and B
A= exists $rule->{"type"}B= $rule->{"type"} eq "fragment"
ABresult
0X0
100
111
446 0 ▶ printf "[%-1s][%-45s][%-2s][%2s] %s\n", $type, $name, (exists $rule->{redirect}) ? "->" : "", "", "";
Branch: 1 branch
BranchTrueFalseTotal
exists $rule->{'redirect'} ? : 0 0 0
447 }
448 0 last SWITCH;
449 };
450 do
451 0 {
452 0 die "can't process rule";
453 0 last SWITCH;
454 };
455 }
456 }
457  
458 0 printf "\n";
459 }
460  
461 2 1;
462  
463 # ABSTRACT: manage symbol table of rules parsed from antlr grammar
464  
465 =head1 SYNOPSIS
466 use MarpaX::G4::Symboltable;
467 my $symboltable = new MarpaX::G4::Symboltable;
468  
469 my $grammartext = readFile($infile);
470 my $data = MarpaX::G4::Parser::parse_rules($grammartext);
471 $symboltable->importParseTree($data);
472 $symboltable->validateSymbolTable();
473  
474 =head1 DESCRIPTION
475 Import the rules from the ANTLR4 parse tree into a symbol table.
476 'validateSymbolTable' does a depth-first tree traversal of the symbol table to produce a report of productions and terminal symbols.
477 =cut
478  
↑ Summary lib/MarpaX/G4/MarpaGen.pm »