Pegex/Pegex/AST.pm

filter: Statementstmt 95.4% 168 / 176 Branchbran 84.6% 83 / 98 Conditioncond 72.2% 13 / 18 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 100% 24 / 24 Podpod 0.0% 0 / 22 totaltotal 83.4% 292 / 350 CC 58 SCAR 18.5
CC
58
Cov
90%
set_separator
15.1
set_quantity
8.0
set_modifier
7.0
CRAP 6.4
« Pegex/Bootstrap.pm ↑ Summary Pegex/Regex.pm »
1 package Pegex::Pegex::AST;
2 17 ▶use Pegex::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 extends 'Pegex::Tree';
4  
5 17 ▶use Pegex::Grammar::Atoms;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 has atoms => Pegex::Grammar::Atoms->new->atoms;
8 17 has extra_rules => {};
9  
10 188 ▶sub got_grammar {
Subroutine got_grammar: called CC=2 SCAR=6.9
Pod undocumented
11 188 my ($self, $got) = @_;
12 188 my ($meta_section, $rule_section) = @$got;
13 my $grammar = {
14 '+toprule' => $self->{toprule},
15 188 %{$self->{extra_rules}},
16 %$meta_section,
17 };
18 188 for my $rule (@$rule_section) {
19 355 my ($key, $value) = %$rule;
20 355 $grammar->{$key} = $value;
21 }
22 188 return $grammar;
23 }
24  
25 202 ▶sub got_meta_section {
Subroutine got_meta_section: called CC=4 SCAR=13.9
Pod undocumented
26 202 my ($self, $got) = @_;
27 202 my $meta = {};
28 202 for my $next (@$got) {
29 9 my ($key, $val) = @$next;
30 9 $key = "+$key";
31 9 my $old = $meta->{$key};
32 9 ▶ if (defined $old) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old) { } 1 8 9
33 1 ▶ if (ref $old) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $old) { } 0 1 1
34 0 push @$old, $val;
35 }
36 else {
37 1 $meta->{$key} = [ $old, $val ];
38 }
39 }
40 else {
41 8 $meta->{$key} = $val;
42 }
43 }
44 202 return $meta;
45 }
46  
47 366 ▶sub got_rule_definition {
Subroutine got_rule_definition: called CC=3 SCAR=11.0
Pod undocumented
48 366 my ($self, $got) = @_;
49 366 my ($name, $value) = @$got;
50 366 $name =~ s/-/_/g;
51 366 ▶ $self->{toprule} = $name if $name eq 'TOP';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "TOP" 4 362 366
52 366 ▶ $self->{toprule} ||= $name;
Condition66%2/3
$self->{"toprule"} ||= $name
l!l&&r!l&&!r
1741920
MC/DC0%0/2
$self->{"toprule"} ||= $name
$self->{"toprule"} $name
Truth table66%2/3
A ||= B
A= $self->{"toprule"}B= $name
ABresult
1X1
011
000
53 366 return +{ $name => $value };
54 }
55  
56 65 ▶sub got_bracketed_group {
Subroutine got_bracketed_group: called CC=1 SCAR=0.0
Pod undocumented
57 65 my ($self, $got) = @_;
58 65 my ($prefix, $group, $suffix) = @$got;
59 65 set_modifier($group, $prefix);
60 65 set_quantity($group, $suffix);
61 65 return $group;
62 }
63  
64 490 ▶sub got_all_group {
Subroutine got_all_group: called CC=3 SCAR=11.0
Pod undocumented
65 490 my ($self, $got) = @_;
66 490 my $list = $self->get_group($got);
67 490 ▶ die unless @$list;
Branch: 1 branch
BranchTrueFalseTotal
unless @$list 0 490 490
68 490 ▶ return $list->[0] if @$list == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 1 354 136 490
69 136 return { '.all' => $list };
70 }
71  
72 440 ▶sub got_any_group {
Subroutine got_any_group: called CC=3 SCAR=11.0
Pod undocumented
73 440 my ($self, $got) = @_;
74 440 my $list = $self->get_group($got);
75 440 ▶ die unless @$list;
Branch: 1 branch
BranchTrueFalseTotal
unless @$list 0 440 440
76 440 ▶ return $list->[0] if @$list == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$list == 1 401 39 440
77 39 return { '.any' => $list };
78 }
79  
80 930 ▶sub get_group {
Subroutine get_group: called CC=1 SCAR=0.3
Pod undocumented
81 930 my ($self, $group) = @_;
82 4069 ▶ sub get {
Subroutine get: called CC=5 SCAR=17.8
Pod undocumented
83 4069 my $it = shift;
84 4069 ▶ my $ref = ref($it) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ref = ref $it or return 4069 0 4069
85 4069 ▶ if ($ref eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 1192 2877 4069
elsif ($ref eq 'ARRAY') { } 2877 0 2877
86 1192 ▶ return($it || ());
Condition33%1/3
$it || ()
l!l&&r!l&&!r
119200
MC/DC0%0/2
$it || ()
$it ()
Truth table33%1/3
A || B
A= $itB= ()
ABresult
1X1
011
000
87 }
88 elsif ($ref eq 'ARRAY') {
89 2877 return map get($_), @$it;
90 }
91 else {
92 0 die;
93 }
94 };
95 930 return [ get($group) ];
96 }
97  
98 702 ▶sub got_rule_part {
Subroutine got_rule_part: called CC=2 SCAR=6.9
Pod undocumented
99 702 my ($self, $got) = @_;
100 702 my ($rule, $sep) = @$got;
101 702 ▶ $rule = set_separator($rule, @$sep) if @$sep;
Branch: 1 branch
BranchTrueFalseTotal
if @$sep 40 662 702
102 702 return $rule;
103 }
104  
105 446 ▶sub got_rule_reference {
Subroutine got_rule_reference: called CC=3 SCAR=11.0
Pod undocumented
106 446 my ($self, $got) = @_;
107 446 my ($prefix, $ref1, $ref2, $suffix) = @$got;
108 446 ▶ my $ref = $ref1 || $ref2;
Condition66%2/3
$ref1 || $ref2
l!l&&r!l&&!r
347990
MC/DC0%0/2
$ref1 || $ref2
$ref1 $ref2
Truth table66%2/3
A || B
A= $ref1B= $ref2
ABresult
1X1
011
000
109 446 $ref =~ s/-/_/g;
110 446 my $node = +{ '.ref' => $ref };
111 446 ▶ if (my $regex = $self->atoms->{$ref}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $regex = $self->atoms->{$ref}) 20 426 446
112 20 $self->{extra_rules}{$ref} = +{ '.rgx' => $regex };
113 }
114 446 set_modifier($node, $prefix);
115 446 set_quantity($node, $suffix);
116 446 return $node;
117 }
118  
119 15 ▶sub got_quoted_regex {
Subroutine got_quoted_regex: called CC=1 SCAR=0.0
Pod undocumented
120 15 my ($self, $got) = @_;
121 15 $got =~ s/([^\w\`\%\:\<\/\,\=\;])/\\$1/g;
122 15 return +{ '.rgx' => $got };
123 }
124  
125 75 ▶sub got_regex_rule_reference {
Subroutine got_regex_rule_reference: called CC=2 SCAR=7.0
Pod undocumented
126 75 my ($self, $got) = @_;
127 75 ▶ my $ref = $got->[0] || $got->[1];
Condition66%2/3
$got->[0] || $got->[1]
l!l&&r!l&&!r
36390
MC/DC0%0/2
$got->[0] || $got->[1]
$got->[0] $got->[1]
Truth table66%2/3
A || B
A= $got->[0]B= $got->[1]
ABresult
1X1
011
000
128 75 return +{ '.ref' => $ref };
129 }
130  
131 9 ▶sub got_whitespace_maybe {
Subroutine got_whitespace_maybe: called CC=1 SCAR=0.0
Pod undocumented
132 9 my ($self) = @_;
133 9 return +{ '.rgx' => '<_>'};
134 }
135  
136 7 ▶sub got_whitespace_must {
Subroutine got_whitespace_must: called CC=1 SCAR=0.0
Pod undocumented
137 7 my ($self) = @_;
138 7 return +{ '.rgx' => '<__>'};
139 }
140  
141 9 ▶sub got_whitespace_start {
Subroutine got_whitespace_start: called CC=2 SCAR=6.9
Pod undocumented
142 9 my ($self, $got) = @_;
143 9 ▶ my $rule = $got eq '+' ? '__' : '_';
Branch: 1 branch
BranchTrueFalseTotal
$got eq '+' ? : 2 7 9
144 9 return +{ '.rgx' => "<$rule>"};
145 }
146  
147 215 ▶sub got_regular_expression {
Subroutine got_regular_expression: called CC=6 SCAR=17.9
Pod undocumented
148 215 my ($self, $got) = @_;
149 215 my $modifier = shift @$got;
150 215 ▶ if (@$got == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$got == 2) 9 206 215
151 9 my $part = shift @$got;
152 9 unshift @{$got->[0]}, $part;
153 }
154  
155 my $regex = join '', map {
156 334 ▶ if (ref($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 109 225 334
157 109 my $part;
158 109 ▶ if (defined($part = $_->{'.rgx'})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined($part = $_->{'.rgx'})) { } 34 75 109
elsif (defined($part = $_->{'.ref'})) { } 75 0 75
159 34 $part;
160 }
161 elsif (defined($part = $_->{'.ref'})) {
162 75 "<$part>";
163 }
164 else {
165 0 XXX $_;
166 }
167 }
168 else {
169 225 $_;
170 }
171 215 } @{$got->[0]};
172 # $regex =~ s!\(([ism]?\:|\=|\!)!(?$1!g;
173 215 $regex =~ s{\(([ism]?\:|\=|\!|<[=!])}{(?$1}g;
174 215 my $rgx = { '.rgx' => $regex };
175 215 ▶ set_modifier($rgx, $modifier) if $modifier;
Branch: 1 branch
BranchTrueFalseTotal
if $modifier 1 214 215
176 215 return $rgx;
177 }
178  
179 14 ▶sub got_whitespace_token {
Subroutine got_whitespace_token: called CC=4 SCAR=14.8
Pod undocumented
180 14 my ($self, $got) = @_;
181 14 my $token;
182 14 ▶ if ($got =~ /^\~{1,2}$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($got =~ /^\~{1,2}$/) { } 0 14 14
elsif ($got =~ /^\-{1,2}$/) { } 9 5 14
elsif ($got eq '+') { } 5 0 5
183 0 $token = +{ '.ref' => ('_' x length($got)) };
184 }
185 elsif ($got =~ /^\-{1,2}$/) {
186 9 $token = +{ '.ref' => ('_' x length($got)) };
187 }
188 elsif ($got eq '+') {
189 5 $token = +{ '.ref' => '__' };
190 }
191 else {
192 0 die;
193 }
194 14 return $token;
195 }
196  
197 8 ▶sub got_error_message {
Subroutine got_error_message: called CC=1 SCAR=0.0
Pod undocumented
198 8 my ($self, $got) = @_;
199 8 return +{ '.err' => $got };
200 }
201  
202 862 ▶sub set_modifier {
Subroutine set_modifier: called CC=7 SCAR=19.5
Pod undocumented
203 862 my ($object, $modifier) = @_;
204 862 ▶ return unless $modifier;
Branch: 1 branch
BranchTrueFalseTotal
unless $modifier 773 89 862
205 89 ▶ if ($modifier eq '=') {
Branch: 5 branches
BranchTrueFalseTotal
if ($modifier eq '=') { } 18 71 89
elsif ($modifier eq '!') { } 20 51 71
elsif ($modifier eq '.') { } 19 32 51
elsif ($modifier eq '+') { } 4 28 32
elsif ($modifier eq '-') { } 28 0 28
206 18 $object->{'+asr'} = 1;
207 }
208 elsif ($modifier eq '!') {
209 20 $object->{'+asr'} = -1;
210 }
211 elsif ($modifier eq '.') {
212 19 $object->{'-skip'} = 1;
213 }
214 elsif ($modifier eq '+') {
215 4 $object->{'-wrap'} = 1;
216 }
217 elsif ($modifier eq '-') {
218 28 $object->{'-flat'} = 1;
219 }
220 else {
221 0 die "Invalid modifier: '$modifier'";
222 }
223 }
224  
225 860 ▶sub set_quantity {
Subroutine set_quantity: called CC=8 SCAR=20.8
Pod undocumented
226 860 my ($object, $quantity) = @_;
227 860 ▶ return unless $quantity;
Branch: 1 branch
BranchTrueFalseTotal
unless $quantity 664 196 860
228 196 ▶ if ($quantity eq '?') {
Branch: 6 branches
BranchTrueFalseTotal
if ($quantity eq '?') { } 37 159 196
elsif ($quantity eq '*') { } 66 93 159
elsif ($quantity eq '+') { } 48 45 93
elsif ($quantity =~ /^(\d+)$/) { } 12 33 45
elsif ($quantity =~ /^(\d+)-(\d+)$/) { } 21 12 33
elsif ($quantity =~ /^(\d+)\+$/) { } 12 0 12
229 37 $object->{'+max'} = 1;
230 }
231 elsif ($quantity eq '*') {
232 66 $object->{'+min'} = 0;
233 }
234 elsif ($quantity eq '+') {
235 48 $object->{'+min'} = 1;
236 }
237 elsif ($quantity =~ /^(\d+)$/) {
238 12 $object->{'+min'} = int $1 + 0;
239 12 $object->{'+max'} = int $1 + 0;
240 }
241 elsif ($quantity =~ /^(\d+)-(\d+)$/) {
242 21 $object->{'+min'} = int $1 + 0;
243 21 $object->{'+max'} = int $2 + 0;
244 }
245 elsif ($quantity =~ /^(\d+)\+$/) {
246 12 $object->{'+min'} = int $1 + 0;
247 }
248 else {
249 0 die "Invalid quantifier: '$quantity'";
250 }
251 }
252  
253 66 ▶sub set_separator {
Subroutine set_separator: called CC=15 SCAR=27.1
Pod undocumented
254 66 my ($rule, $op, $sep) = @_;
255 66 my $extra = ($op eq '%%');
256 66 ▶ if (not defined $rule->{'+max'} and not defined $rule->{'+min'}) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $rule->{'+max'} || defined $rule->{'+min'}) { } 16 50 66
elsif (defined $rule->{'+max'} and defined $rule->{'+min'}) { } 14 36 50
elsif (not defined $rule->{'+max'}) { } 32 4 36
Condition100%3/3
defined $rule->{'+max'} || defined $rule->{'+min'}
l!l&&r!l&&!r
183216
MC/DC100%2/2
defined $rule->{'+max'} || defined $rule->{'+min'}
defined $rule->{'+max'} defined $rule->{'+min'}
Truth table100%3/3
A || B
A= defined $rule->{'+max'}B= defined $rule->{'+min'}
ABresult
1X1
011
000
257 16 ▶ $rule = {'.all' => [ $rule, {%{clone($sep)}, '+max' => 1}, ] }
Branch: 1 branch
BranchTrueFalseTotal
if $extra 6 10 16
258 if $extra;
259 16 return $rule;
260 }
261 ▶ elsif (defined $rule->{'+max'} and defined $rule->{'+min'}) {
Condition100%3/3
defined $rule->{"+max"} and defined $rule->{"+min"}
!ll&&!rl&&r
32414
MC/DC100%2/2
defined $rule->{"+max"} and defined $rule->{"+min"}
defined $rule->{"+max"} defined $rule->{"+min"}
Truth table100%3/3
A and B
A= defined $rule->{"+max"}B= defined $rule->{"+min"}
ABresult
0X0
100
111
262 14 my ($min, $max) = delete @{$rule}{qw(+min +max)};
263 14 ▶ $min-- if $min > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $min > 0 14 0 14
264 14 ▶ $max-- if $max > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $max > 0 14 0 14
265 14 $rule = {
266 '.all' => [
267 $rule,
268 {
269 '+min' => $min,
270 '+max' => $max,
271 '-flat' => 1,
272 '.all' => [
273 $sep,
274 clone($rule),
275 ],
276 },
277 ],
278 };
279 }
280 elsif (not defined $rule->{'+max'}) {
281 32 my $copy = clone($rule);
282 32 my $min = delete $copy->{'+min'};
283 32 my $new = {
284 '.all' => [
285 $copy,
286 {
287 '+min' => 0,
288 '-flat' => 1,
289 '.all' => [
290 $sep,
291 clone($copy),
292 ],
293 },
294 ],
295 };
296 32 ▶ if ($rule->{'+min'} == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rule->{'+min'} == 0) { } 14 18 32
elsif ($rule->{'+min'} == 1) { } 12 6 18
297 14 $rule = $new;
298 14 $rule->{'+max'} = 1;
299 }
300 elsif ($rule->{'+min'} == 1) {
301 12 $rule = $new;
302 }
303 else {
304 6 $rule = $new;
305 6 ▶ $min-- if $min > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $min > 0 6 0 6
306 6 $rule->{'.all'}[-1]{'+min'} = $min;
307 }
308 }
309 else {
310 4 ▶ if ($rule->{'+max'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{'+max'} == 1) { } 4 0 4
311 4 delete $rule->{'+min'};
312 $rule = {
313 4 %{clone($rule)},
314 '+max' => 1,
315 };
316 4 ▶ if ($extra) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra) 2 2 4
317 2 $rule = { '.all' => [$rule, {%{clone($sep)}, '+max' => 1}] };
318 }
319 4 return $rule;
320 }
321 else {
322 0 XXX 'FAIL', $rule, $op, $sep;
323 }
324 }
325 46 ▶ if ($extra) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra) 23 23 46
326 23 push @{$rule->{'.all'}}, {%{clone($sep)}, '+max' => 1};
327 }
328 46 return $rule;
329 }
330  
331 122 ▶sub clone {
Subroutine clone: called CC=4 SCAR=13.9
Pod undocumented
332 122 my ($o) = @_;
333 return ref($o) eq 'HASH'
334 161 ▶ ? { map { my $v = $o->{$_}; ($_, (ref($v) ? clone($v) : $v)) } keys %$o }
Branch: 1 branch
BranchTrueFalseTotal
ref $v ? : 3 158 161
335 122 ▶ : [ map { ref($_) ? clone($_) : $_ } @$o ];
Branch: 2 branches
BranchTrueFalseTotal
ref $o eq 'HASH' ? : 119 3 122
ref $_ ? : 6 0 6
336 }
337  
338 17 1;
« Pegex/Bootstrap.pm ↑ Summary Pegex/Regex.pm »