Pegex/Optimizer.pm

filter: Statementstmt 83.7% 67 / 80 Branchbran 77.7% 42 / 54 Conditioncond 66.6% 4 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 80.0% 8 / 10 Podpod 0.0% 0 / 5 totaltotal 76.1% 121 / 159 CC 33 SCAR 28.0
CC
33
Cov
81%
optimize_node
25.8
optimize_grammar
6.0
__ANON__
6.0
CRAP 16.5
« Pegex/Parser.pm ↑ Summary Pegex/Input.pm »
1 package Pegex::Optimizer;
2 18 ▶use Pegex::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 18 has parser => (required => 1);
5 18 has grammar => (required => 1);
6 18 has receiver => (required => 1);
7  
8 273 ▶sub optimize_grammar {
Subroutine optimize_grammar: called CC=6 SCAR=17.9
Pod undocumented
9 273 my ($self, $start) = @_;
10 273 my $tree = $self->grammar->{tree};
11 273 ▶ return if $tree->{'+optimized'};
Branch: 1 branch
BranchTrueFalseTotal
if $tree->{"+optimized"} 1 272 273
12 272 ▶ $self->set_max_parse if $self->parser->{maxparse};
Branch: 1 branch
BranchTrueFalseTotal
if $self->parser->{"maxparse"} 0 272 272
13 272 $self->{extra} = {};
14 272 ▶ while (my ($name, $node) = each %$tree) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$tree) 8977 272 9249
15 8977 ▶ next unless ref($node);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 878 8099 8977
16 8099 $self->optimize_node($node);
17 }
18 272 $self->optimize_node({'.ref' => $start});
19 272 my $extra = delete $self->{extra};
20 272 for my $key (%$extra) {
21 4 $tree->{$key} = $extra->{$key};
22 }
23 272 $tree->{'+optimized'} = 1;
24 }
25  
26 31021 ▶sub optimize_node {
Subroutine optimize_node: called CC=24 SCAR=32.5
Pod undocumented
27 31021 my ($self, $node) = @_;
28  
29 31021 my ($min, $max) = @{$node}{'+min', '+max'};
30 $node->{'+min'} = defined($max) ? 0 : 1
31 31021 ▶ unless defined $node->{'+min'};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $node->{"+min"} 29147 1874 31021
defined $max ? : 212 28935 29147
32 $node->{'+max'} = defined($min) ? 0 : 1
33 31021 ▶ unless defined $node->{'+max'};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $node->{"+max"} 30395 626 31021
defined $min ? : 1460 28935 30395
34 $node->{'+asr'} = 0
35 31021 ▶ unless defined $node->{'+asr'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node->{"+asr"} 30610 411 31021
36  
37 31021 for my $kind (qw(ref rgx all any err code xxx)) {
38 70562 ▶ return if $kind eq 'xxx';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "xxx" 0 70562 70562
39 70562 ▶ if ($node->{rule} = $node->{".$kind"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"rule"} = $$node{".$kind"}) 31021 39541 70562
40 31021 delete $node->{".$kind"};
41 31021 $node->{kind} = $kind;
42 31021 ▶ if ($kind eq 'ref') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "ref") 11374 19647 31021
43 11374 ▶ my $rule = $node->{rule} or die;
Branch: 1 branch
BranchTrueFalseTotal
my $rule = $node->{"rule"} or die 11374 0 11374
44 11374 ▶ if (my $method = $self->grammar->can("rule_$rule")) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $method = $self->grammar->can("rule_$rule")) { } 2 11372 11374
elsif (not $self->grammar->{'tree'}{$rule}) { } 0 11372 11372
45 2 $node->{method} = $self->make_method_wrapper($method);
46 }
47 elsif (not $self->grammar->{tree}{$rule}) {
48 0 ▶ if (my $method = $self->grammar->can("$rule")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $self->grammar->can("$rule")) 0 0 0
49 0 warn <<"...";
50 Warning:
51  
52 You have a method called '$rule' in your grammar.
53 It should probably be called 'rule_$rule'.
54  
55 ...
56 }
57 0 die "No rule '$rule' defined in grammar";
58 }
59 }
60 31021 ▶ $node->{method} ||= $self->parser->can("match_$kind") or die;
Branch: 1 branch
BranchTrueFalseTotal
$node->{"method"} ||= $self->parser->can("match_$kind") or die 31021 0 31021
Condition66%2/3
$node->{"method"} ||= $self->parser->can("match_$kind")
l!l&&r!l&&!r
2310190
MC/DC0%0/2
$node->{"method"} ||= $self->parser->can("match_$kind")
$node->{"method"} $self->parser->can("match_$kind")
Truth table66%2/3
A ||= B
A= $node->{"method"}B= $self->parser->can("match_$kind")
ABresult
1X1
011
000
61 31021 last;
62 }
63 }
64  
65 31021 ▶ if ($node->{kind} =~ /^(?:all|any)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($node->{'kind'} =~ /^(?:all|any)$/) { } 8776 22245 31021
elsif ($node->{'kind'} eq 'ref') { } 11374 10871 22245
elsif ($node->{'kind'} eq 'rgx') { } 8043 2828 10871
66 8776 $self->optimize_node($_) for @{$node->{rule}};
67 }
68 elsif ($node->{kind} eq 'ref') {
69 11374 my $ref = $node->{rule};
70 11374 my $rule = $self->grammar->{tree}{$ref};
71 11374 ▶ $rule ||= $self->{extra}{$ref} = {};
Condition66%2/3
$rule ||= ($self->{"extra"}{$ref} = {})
l!l&&r!l&&!r
1137220
MC/DC0%0/2
$rule ||= ($self->{"extra"}{$ref} = {})
$rule ($self->{"extra"}{$ref} = {})
Truth table66%2/3
A ||= B
A= $ruleB= ($self->{"extra"}{$ref} = {})
ABresult
1X1
011
000
72 11374 ▶ if (my $action = $self->receiver->can("got_$ref")) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $action = $self->receiver->can("got_$ref")) { } 3845 7529 11374
elsif (my $gotrule = $self->receiver->can('gotrule')) { } 7505 24 7529
73 3845 $rule->{action} = $action;
74 }
75 elsif (my $gotrule = $self->receiver->can("gotrule")) {
76 7505 $rule->{action} = $gotrule;
77 }
78 11374 ▶ if ($self->parser->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parser->{"debug"}) 23 11351 11374
79 23 $node->{method} = $self->make_trace_wrapper($node->{method});
80 }
81 }
82 elsif ($node->{kind} eq 'rgx') {
83 # XXX $node;
84 }
85 }
86  
87 2 ▶sub make_method_wrapper {
Subroutine make_method_wrapper: called CC=1 SCAR=0.0
Pod undocumented
88 2 my ($self, $method) = @_;
89 2 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
90 2 my ($parser, $ref, $parent) = @_;
91 2 @{$parser}{'rule', 'parent'} = ($ref, $parent);
92 $method->(
93 $parser->{grammar},
94 $parser,
95 $parser->{buffer},
96 $parser->{position},
97 2 );
98 }
99 2 }
100  
101 24 ▶sub make_trace_wrapper {
Subroutine make_trace_wrapper: called CC=1 SCAR=0.1
Pod undocumented
102 24 my ($self, $method) = @_;
103 23 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=14.6
104 23 my ($self, $ref, $parent) = @_;
105 23 my $asr = $parent->{'+asr'};
106 23 ▶ my $note =
Branch: 2 branches
BranchTrueFalseTotal
$asr == -1 ? : 0 23 23
$asr == 1 ? : 0 23 23
107 $asr == -1 ? '(!)' :
108 $asr == 1 ? '(=)' :
109 '';
110 23 $self->trace("try_$ref$note");
111 23 my $result;
112 23 ▶ if ($result = $self->$method($ref, $parent)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result = $self->$method($ref, $parent)) { } 23 0 23
113 23 $self->trace("got_$ref$note");
114 }
115 else {
116 0 $self->trace("not_$ref$note");
117 }
118 23 return $result;
119 }
120 24 }
121  
122 0 ▶sub set_max_parse {
Subroutine set_max_parse: not called CC=1 SCAR=4.0
Pod undocumented
123 0 require Pegex::Parser;
124 0 my ($self) = @_;
125 0 my $maxparse = $self->parser->{maxparse};
126 18 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
127 0 my $method = \&Pegex::Parser::match_ref;
128 0 my $counter = 0;
129 0 ▶ *Pegex::Parser::match_ref = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
130 0 ▶ die "Maximum parsing rules reached ($maxparse)\n"
Branch: 1 branch
BranchTrueFalseTotal
if $counter++ >= $maxparse 0 0 0
131 if $counter++ >= $maxparse;
132 0 my $self = shift;
133 0 $self->$method(@_);
134 0 };
135 }
136  
137 18 1;
« Pegex/Parser.pm ↑ Summary Pegex/Input.pm »