| 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
| |||||||||||||||||||||||||||||
| 12 | 272 | ▶ | $self->set_max_parse if $self->parser->{maxparse}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | 272 | $self->{extra} = {}; | |||||||||||||||||||||||||||
| 14 | 272 | ▶ | while (my ($name, $node) = each %$tree) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 8977 | ▶ | next unless ref($node); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 32 | $node->{'+max'} = defined($min) ? 0 : 1 | ||||||||||||||||||||||||||||
| 33 | 31021 | ▶ | unless defined $node->{'+max'}; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 34 | $node->{'+asr'} = 0 | ||||||||||||||||||||||||||||
| 35 | 31021 | ▶ | unless defined $node->{'+asr'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 31021 | for my $kind (qw(ref rgx all any err code xxx)) { | |||||||||||||||||||||||||||
| 38 | 70562 | ▶ | return if $kind eq 'xxx'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 70562 | ▶ | if ($node->{rule} = $node->{".$kind"}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 31021 | delete $node->{".$kind"}; | |||||||||||||||||||||||||||
| 41 | 31021 | $node->{kind} = $kind; | |||||||||||||||||||||||||||
| 42 | 31021 | ▶ | if ($kind eq 'ref') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 11374 | ▶ | my $rule = $node->{rule} or die; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 11374 | ▶ | if (my $method = $self->grammar->can("rule_$rule")) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$node->{"method"} ||= $self->parser->can("match_$kind")
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")
| |||||||||||||||||||||||||||||
| 61 | 31021 | last; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 31021 | ▶ | if ($node->{kind} =~ /^(?:all|any)$/) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 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} = {})
MC/DC0%0/2
$rule ||= ($self->{"extra"}{$ref} = {})
$rule
($self->{"extra"}{$ref} = {})
Truth table66%2/3
A ||= B
A= $ruleB= ($self->{"extra"}{$ref} = {})
| |||||||||||||||||||||||||||||
| 72 | 11374 | ▶ | if (my $action = $self->receiver->can("got_$ref")) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 131 | if $counter++ >= $maxparse; | ||||||||||||||||||||||||||||
| 132 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 133 | 0 | $self->$method(@_); | |||||||||||||||||||||||||||
| 134 | 0 | }; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 18 | 1; | |||||||||||||||||||||||||||