blib/lib/Parse/ABNF.pm

filter: Statementstmt 15.7% 27 / 172 Branchbran 0.0% 0 / 104 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 58.3% 7 / 12 Podpod 100% 4 / 4 totaltotal 12.8% 38 / 295 CC 61 SCAR 59.4
CC
61
Cov
10%
_abnf2g
552.0
_to_jet
420.0
parse_to_grammar_formal
342.0
CRAP 379.1
↑ Summary
1 package Parse::ABNF;
2 4 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Parse::RecDescent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use List::Util qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 our $VERSION = '0.30';
9 4 our $Grammar = q{
10  
11 {
12 sub Make {
13 my $class = shift;
14 my %opts = @_;
15  
16 # Unfold single item groups and choices
17 return $opts{value}->[0] if $class eq 'Group' and @{$opts{value}} == 1;
18 return $opts{value}->[0] if $class eq 'Choice' and @{$opts{value}} == 1;
19  
20 # These aswell
21 return $opts{value}->[0] if $class eq 'Intersection' and @{$opts{value}} == 1;
22 return $opts{value}->[0] if $class eq 'Subtraction' and @{$opts{value}} == 1;
23
24 return { class => $class, %opts };
25 }
26 }
27  
28 parse: rulelist {
29 $return = $item[1];
30 }
31  
32 empty_line: c_wsp(s?) c_nl
33  
34 # The /^\Z/ ensures that we don't leave unparsed trailing content
35 # if there are errors in the grammar (Parse::RecDescent's default)
36  
37 rulelist: empty_line(s?) rule(s) /^\Z/ {
38 $return = $item[2];
39 }
40  
41 rule: rulename c_wsp(s?) "=" c_wsp(s?) elements c_nl empty_line(s?) {
42 $return = Make(Rule => name => $item[1], value => $item[5]);
43 }
44  
45 rule: rulename c_wsp(s?) "=/" c_wsp(s?) elements c_nl empty_line(s?) {
46 $return = Make(Rule => name => $item[1], value => $item[5], combine => 'choice');
47
48 }
49  
50 # Generate an error message if the rule production is not matched.
51 rule: <error>
52  
53 rulename: /[a-zA-Z][a-zA-Z0-9-]*/ {
54 $return = $item[1];
55 }
56  
57 # n exactly
58 repetition: /\d+/ element {
59 $return = Make(Repetition => min => $item[1], max => $item[1], value => $item[2]);
60 }
61  
62 # n to m
63 repetition: /\d+/ "*" /\d+/ element {
64 $return = Make(Repetition => min => $item[1], max => $item[3], value => $item[4]);
65 }
66  
67 # 0 to n
68 repetition: "*" /\d+/ element {
69 $return = Make(Repetition => min => 0, max => $item[2], value => $item[3]);
70 }
71  
72 # n or more
73 repetition: /\d+/ "*" element {
74 $return = Make(Repetition => min => $item[1], max => undef, value => $item[3]);
75 }
76  
77 # zero or more
78 repetition: "*" element {
79 $return = Make(Repetition => min => 0, max => undef, value => $item[2]);
80 }
81  
82 # exactly one
83 repetition: element {
84 $return = $item[1];
85 }
86  
87 #
88 elements: alternation c_wsp(s?) {
89 $return = $item[1];
90 }
91  
92 alt_op: "/"
93  
94 #
95 alternation: concatenation (c_wsp(s?) alt_op c_wsp(s?) concatenation)(s?) {
96 $return = Make(Choice => value => [$item[1], @{$item[2]}]);
97 }
98  
99 #
100 intersection: subtraction (c_wsp(s?) "&" c_wsp(s?) subtraction)(s?) {
101 $return = Make(Intersection => value => [$item[1], @{$item[2]}]);
102 }
103  
104 #
105 subtraction: repetition (c_wsp(s) "-" c_wsp(s) repetition)(?) {
106 $return = Make(Subtraction => value => [$item[1], @{$item[2]}]);
107 }
108
109 #
110 concatenation_: intersection (c_wsp(s) intersection)(s?) {
111 $return = Make(Group => value => [$item[1], @{$item[2]}]);
112 }
113  
114 #
115 concatenation: repetition (c_wsp(s) repetition)(s?) {
116 $return = Make(Group => value => [$item[1], @{$item[2]}]);
117 }
118  
119 #
120 element: ref_val | group | option | char_val | num_val | prose_val {
121 $return = $item[1];
122 }
123  
124 ref_val: rulename {
125 $return = Make(Reference => name => $item[1]);
126 }
127  
128 #
129 group: "(" c_wsp(s?) alternation c_wsp(s?) ")" {
130 $return = $item[3];
131 }
132  
133 #
134 option: "[" c_wsp(s?) alternation c_wsp(s?) "]" {
135 $return = Make(Repetition => min => 0, max => 1, value => $item[3]);
136 }
137  
138 c_wsp: /[ \t]/
139  
140 c_wsp: c_nl /[ \t]/
141  
142 newline: "\n"
143  
144 c_nl: newline
145  
146 c_nl: comment
147  
148 comment: /;[ \t\x21-\x7e]*/ newline
149  
150 char_val: '"' /[\x20-\x21\x23-\x7E]*/ '"' {
151 $return = Make(Literal => value => $item[2]);
152 }
153  
154 num_val: bin_val | dec_val | hex_val {
155 $return = $item[1];
156 }
157  
158 bin_val: "%b" /[01]+/ "-" /[01]+/ {
159 $return = Make(Range => type => 'binary', min => $item[2], max => $item[4]);
160 }
161  
162 dec_val: "%d" /\d+/ "-" /\d+/ {
163 $return = Make(Range => type => 'decimal', min => $item[2], max => $item[4]);
164 }
165  
166 hex_val: "%x" /[0-9a-fA-F]+/ "-" /[0-9a-fA-F]+/ {
167 $return = Make(Range => type => 'hex', min => $item[2], max => $item[4]);
168 }
169  
170 bin_val: "%b" /[01]+/ /(?:\.[01]+)*/ {
171 $return = Make(String => type => 'binary', value => [split/\./, "$item[2]$item[3]"]);
172 }
173  
174 dec_val: "%d" /\d+/ /(?:\.\d+)*/ {
175 $return = Make(String => type => 'decimal', value => [split/\./, "$item[2]$item[3]"]);
176 }
177  
178 hex_val: "%x" /[0-9a-fA-F]+/ /(?:\.[0-9a-fA-F]+)*/ {
179 $return = Make(String => type => 'hex', value => [split/\./, "$item[2]$item[3]"]);
180 }
181  
182 prose_val: "<" /[\x20-\x3d\x3f-\x7e]*/ ">" {
183 $return = Make(ProseValue => value => $item[2]);
184 }
185  
186 };
187  
188 4 our $CoreRulesGrammar = q{
189  
190 ALPHA = %x41-5A / %x61-7A
191 BIT = "0" / "1"
192 CHAR = %x01-7F
193 CR = %x0D
194 CRLF = CR LF
195 CTL = %x00-1F / %x7F
196 DIGIT = %x30-39
197 DQUOTE = %x22
198 HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
199 HTAB = %x09
200 LF = %x0A
201 LWSP = *(WSP / CRLF WSP)
202 OCTET = %x00-FF
203 SP = %x20
204 VCHAR = %x21-7E
205 WSP = SP / HTAB
206  
207 };
208  
209 # TODO: Perhaps this is not such a good idea, users may attempt to
210 # modify the data and thus affect simultaneously running modules.
211 4 our $CoreRules = do {
212 4 __PACKAGE__->new->parse( $CoreRulesGrammar );
213 };
214  
215 6 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
216 6 my $class = shift;
217 6 local $Parse::RecDescent::skip = '';
218 6 bless { _p => Parse::RecDescent->new($Grammar) }, $class;
219 }
220  
221 6 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
222 6 my $self = shift;
223 6 my $string = shift;
224 6 my $result = $self->{_p}->parse($string);
225 6 return $result;
226 }
227  
228 0 ▶sub _to_jet {
Subroutine _to_jet: not called CC=20 SCAR=60.4
229 0 my ($p) = @_;
230  
231 0 for ($p->{class}) {
232 0 ▶ if ($_ eq "Group") {
Branch: 11 branches
BranchTrueFalseTotal
if ($_ eq 'Group') { } 0 0 0
elsif ($_ eq 'Choice') { } 0 0 0
elsif ($_ eq 'Intersection') { } 0 0 0
elsif ($_ eq 'Subtraction') { } 0 0 0
elsif ($_ eq 'Repetition') { } 0 0 0
elsif ($_ eq 'Rule') { } 0 0 0
elsif ($_ eq 'Reference') { } 0 0 0
elsif ($_ eq 'Literal') { } 0 0 0
elsif ($_ eq 'ProseValue') { } 0 0 0
elsif ($_ eq 'String') { } 0 0 0
elsif ($_ eq 'Range') { } 0 0 0
233 0 my @values = map { _to_jet($_) } @{ $p->{value} };
234 0 return ["group", {}, \@values];
235 }
236 elsif ($_ eq "Choice") {
237 0 my @values = map { _to_jet($_) } @{ $p->{value} };
238 0 return ["choice", {}, \@values];
239 }
240 elsif ($_ eq "Intersection") {
241 0 my @values = map { _to_jet($_) } @{ $p->{value} };
242 0 return ["conjunction", {}, \@values];
243 }
244 elsif ($_ eq "Subtraction") {
245 0 my @values = map { _to_jet($_) } @{ $p->{value} };
246 0 return ["exclusion", {}, \@values];
247 }
248 elsif ($_ eq "Repetition") {
249 0 my @values = map { _to_jet($_) } $p->{value};
250 return ["repetition", {
251 min => $p->{min},
252 max => $p->{max},
253 0 }, \@values];
254 }
255 elsif ($_ eq "Rule") {
256 0 my @values = map { _to_jet($_) } $p->{value};
257 0 ▶ if (exists $p->{combine}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $p->{"combine"}) 0 0 0
258 ...
259 0 }
260 return ["rule", {
261 name => $p->{name},
262 0 }, \@values];
263 }
264 elsif ($_ eq "Reference") {
265 return ["ref", {
266 name => $p->{name},
267 0 }, []];
268 }
269 elsif ($_ eq "Literal") {
270 return ["asciiInsensitiveString", {
271 text => $p->{value}
272 0 }, []];
273 }
274 elsif ($_ eq "ProseValue") {
275 return ["prose", {
276 text => $p->{value},
277 0 }, []];
278 }
279 elsif ($_ eq "String") {
280 0 my @items;
281 0 for ($p->{type}) {
282 0 ▶ if ($_ eq "decimal") {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq 'decimal') { } 0 0 0
elsif ($_ eq 'hex') { } 0 0 0
283 0 @items = map { $_ } @{ $p->{value} };
284 }
285 elsif ($_ eq "hex") {
286 0 @items = map { hex $_ } @{ $p->{value} };
287 }
288 else {
289 ...
290 0 }
291 }
292  
293 my @values = map {
294 0 ["range", {
295 first => $_,
296 last => $_,
297 }, [] ]
298 } @items;
299  
300 0 ▶ return $values[0] if @values == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @values == 1 0 0 0
301  
302 0 return ["group", {}, \@values];
303 }
304 elsif ($_ eq "Range") {
305 return ["range", {
306 first => hex $p->{min},
307 last => hex $p->{max},
308 0 ▶ }, []] if $p->{type} eq 'hex';
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"type"} eq "hex" 0 0 0
309  
310 return ["range", {
311 first => $p->{min},
312 last => $p->{max},
313 0 ▶ }, []] if $p->{type} eq 'decimal';
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"type"} eq "decimal" 0 0 0
314  
315 ...
316 0 }
317 }
318  
319 ...
320 0 }
321  
322 0 ▶sub parse_to_jet {
Subroutine parse_to_jet: not called CC=3 SCAR=24.8
Pod documented
323 0 my ($self, $string, %options) = @_;
324  
325 0 my $result = $self->{_p}->parse($string);
326  
327 0 my @core_rules = map { _to_jet($_) } @$CoreRules;
328 0 $_->[1]{combine} = 'fallback' for @core_rules;
329  
330 my $g = [
331 'grammar',
332 {},
333 [
334 ($options{core} ? @core_rules : ()),
335 0 ▶ map { _to_jet($_) } @$result
Branch: 1 branch
BranchTrueFalseTotal
$options{'core'} ? : 0 0 0
336 ]
337 ];
338  
339 # _make_jet_binary($g);
340  
341 0 return $g;
342 }
343  
344 0 ▶sub parse_to_grammar_formal {
Subroutine parse_to_grammar_formal: not called CC=18 SCAR=58.3
Pod documented
345 0 my ($self, $string, %options) = @_;
346 0 my $result = $self->{_p}->parse($string);
347
348 0 require Grammar::Formal;
349 0 my $g = Grammar::Formal->new;
350 0 my $o = {};
351  
352 0 my @rules = map { _abnf2g($_, $g, $o) } @$result;
353  
354 ###################################################################
355 # Install all rules in the grammar
356 ###################################################################
357 0 for my $rule (@rules) {
358 0 ▶ if ($g->rules->{$rule->name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->rules->{$rule->name}) { } 0 0 0
359 0 my $old = $g->rules->{$rule->name};
360 0 my $new = Grammar::Formal::Rule->new(
361 name => $rule->name,
362 p => $g->Choice($old->p, $rule->p),
363 );
364 0 $g->set_rule($rule->name, $new);
365 } else {
366 0 $g->set_rule($rule->name, $rule);
367 }
368 }
369
370 ###################################################################
371 # Add missing Core rules if requested
372 ###################################################################
373 0 ▶ if ($options{core}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"core"}) 0 0 0
374 0 my %referenced;
375 0 my @todo = values %{ $g->{rules} };
376 0 ▶ while (my $c = pop @todo) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = pop @todo) 0 0 0
377 0 ▶ if ($c->isa('Grammar::Formal::Reference')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($c->isa('Grammar::Formal::Reference')) { } 0 0 0
elsif ($c->isa('Grammar::Formal::Unary')) { } 0 0 0
elsif ($c->isa('Grammar::Formal::Binary')) { } 0 0 0
378 0 $referenced{$c->name}++;
379 } elsif ($c->isa('Grammar::Formal::Unary')) {
380 0 push @todo, $c->p;
381 } elsif ($c->isa('Grammar::Formal::Binary')) {
382 0 push @todo, $c->p1, $c->p2;
383 }
384 }
385
386 # TODO: might be more sensible to convert the Core rules first
387 # and then simply throw them into the todo list above.
388
389 0 ▶ $referenced{WSP}++ if $referenced{LWSP};
Branch: 1 branch
BranchTrueFalseTotal
if $referenced{"LWSP"} 0 0 0
390 0 ▶ $referenced{CRLF}++ if $referenced{LWSP};
Branch: 1 branch
BranchTrueFalseTotal
if $referenced{"LWSP"} 0 0 0
391 0 ▶ $referenced{HTAB}++ if $referenced{WSP};
Branch: 1 branch
BranchTrueFalseTotal
if $referenced{"WSP"} 0 0 0
392 0 ▶ $referenced{SP}++ if $referenced{WSP};
Branch: 1 branch
BranchTrueFalseTotal
if $referenced{"WSP"} 0 0 0
393 0 ▶ $referenced{CR}++ if $referenced{CRLF};
Branch: 1 branch
BranchTrueFalseTotal
if $referenced{"CRLF"} 0 0 0
394 0 ▶ $referenced{LF}++ if $referenced{CRLF};
Branch: 1 branch
BranchTrueFalseTotal
if $referenced{"CRLF"} 0 0 0
395 0 ▶ $referenced{DIGIT}++ if $referenced{HEXDIG};
Branch: 1 branch
BranchTrueFalseTotal
if $referenced{"HEXDIG"} 0 0 0
396
397 0 my @core_rules = map { _abnf2g($_, $g, $o) } @$CoreRules;
398  
399 0 for my $rule (@core_rules) {
400 0 ▶ next if $g->rules->{$rule->name};
Branch: 1 branch
BranchTrueFalseTotal
if $g->rules->{$rule->name} 0 0 0
401 0 ▶ next unless $referenced{$rule->name};
Branch: 1 branch
BranchTrueFalseTotal
unless $referenced{$rule->name} 0 0 0
402 0 $g->set_rule($rule->name, $rule);
403 }
404 }
405
406 0 return $g;
407 }
408  
409 0 ▶sub _abnf2g {
Subroutine _abnf2g: not called CC=23 SCAR=63.1
410 0 my ($p, $g, $options) = @_;
411
412 0 ▶ $options->{pos} //= 1;
Condition0%0/2
$options->{"pos"} //= 1
l!l
00
MC/DC0%0/1
$options->{"pos"} //= 1
$options->{"pos"}
Truth table0%0/2
A
A= $options->{"pos"}
Aresult
11
00
413 0 my $pos = $options->{pos}++;
414
415 0 for ($p->{class}) {
416 0 ▶ if ($_ eq "Group") {
Branch: 11 branches
BranchTrueFalseTotal
if ($_ eq 'Group') { } 0 0 0
elsif ($_ eq 'Choice') { } 0 0 0
elsif ($_ eq 'Repetition') { } 0 0 0
elsif ($_ eq 'Rule') { } 0 0 0
elsif ($_ eq 'Reference') { } 0 0 0
elsif ($_ eq 'Literal') { } 0 0 0
elsif ($_ eq 'ProseValue') { } 0 0 0
elsif ($_ eq 'String') { } 0 0 0
elsif ($_ eq 'Range') { } 0 0 0
elsif ($_ eq 'Intersection') { } 0 0 0
elsif ($_ eq 'Subtraction') { } 0 0 0
417 0 my @values = map { _abnf2g($_, $g, $options) } @{ $p->{value} };
418 0 my $group = $g->Empty;
419 0 ▶ while (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) 0 0 0
420 0 $group = $g->Group(pop(@values), $group, position => $pos);
421 }
422 0 return $group;
423 }
424 elsif ($_ eq "Choice") {
425 0 my @values = map { _abnf2g($_, $g, $options) } @{ $p->{value} };
426 0 my $choice = $g->NotAllowed;
427 0 ▶ while (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) 0 0 0
428 0 $choice = $g->Choice(pop(@values), $choice, position => $pos);
429 }
430 0 return $choice;
431 }
432 elsif ($_ eq "Repetition") {
433 0 ▶ if (defined $p->{max}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p->{'max'}) { } 0 0 0
434 return Grammar::Formal::BoundedRepetition->new(
435 min => $p->{min},
436 max => $p->{max},
437 0 p => _abnf2g($p->{value}, $g, $options),
438 position => $pos,
439 );
440 } else {
441 return Grammar::Formal::SomeOrMore->new(
442 min => $p->{min},
443 0 p => _abnf2g($p->{value}, $g, $options),
444 position => $pos,
445 );
446 }
447 }
448 elsif ($_ eq "Rule") {
449 return Grammar::Formal::Rule->new(
450 name => $p->{name},
451 0 p => _abnf2g($p->{value}, $g, $options),
452 position => $pos,
453 );
454 }
455 elsif ($_ eq "Reference") {
456 return Grammar::Formal::Reference->new(
457 name => $p->{name},
458 0 position => $pos,
459 );
460 }
461 elsif ($_ eq "Literal") {
462 return Grammar::Formal::AsciiInsensitiveString->new(
463 value => $p->{value},
464 0 position => $pos,
465 );
466 }
467 elsif ($_ eq "ProseValue") {
468 return Grammar::Formal::ProseValue->new(
469 value => $p->{value},
470 0 position => $pos,
471 );
472 }
473 elsif ($_ eq "String") {
474 0 my @items;
475 0 for ($p->{type}) {
476 0 ▶ if ($_ eq "decimal") {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq 'decimal') { } 0 0 0
elsif ($_ eq 'hex') { } 0 0 0
477 0 @items = map { $_ } @{ $p->{value} };
478 }
479 elsif ($_ eq "hex") {
480 0 @items = map { hex $_ } @{ $p->{value} };
481 }
482 else {
483 ...
484 0 }
485 }
486 my @values = map {
487 0 Grammar::Formal::Range->new(
488 min => $_,
489 max => $_,
490 position => $pos,
491 )
492 } @items;
493
494 0 my $group = $g->Empty;
495 0 ▶ while (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) 0 0 0
496 0 $group = $g->Group(pop(@values), $group, position => $pos);
497 }
498  
499 0 return $group;
500 }
501 elsif ($_ eq "Range") {
502 return Grammar::Formal::Range->new(
503 min => hex $p->{min},
504 max => hex $p->{max},
505 position => $pos,
506 0 ▶ ) if $p->{type} eq 'hex';
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"type"} eq "hex" 0 0 0
507 return Grammar::Formal::Range->new(
508 min => $p->{min},
509 max => $p->{max},
510 position => $pos,
511 0 ▶ ) if $p->{type} eq 'decimal';
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"type"} eq "decimal" 0 0 0
512 0 ...;
513 }
514 elsif ($_ eq "Intersection") {
515 0 my @values = map { _abnf2g($_, $g, $options) } @{ $p->{value} };
516 0 ▶ return List::Util::reduce { Grammar::Formal::Intersection->new(
Subroutine __ANON__: not called CC=1 SCAR=6.9
517 p1 => $a,
518 p2 => $b,
519 position => $pos,
520 0 ) } @values;
521 ...
522 0 }
523 elsif ($_ eq "Subtraction") {
524 return Grammar::Formal::Subtraction->new(
525 p1 => _abnf2g($p->{value}[0], $g, $options),
526 0 p2 => _abnf2g($p->{value}[1], $g, $options),
527 position => $pos,
528 );
529 ...
530 0 }
531 }
532 }
533  
534 4 1;
535  
536 __END__
↑ Summary