line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Pegex::Optimizer; |
2
|
4
|
|
|
4
|
|
19
|
use Pegex::Base; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
25
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
has parser => (required => 1); |
5
|
|
|
|
|
|
|
has grammar => (required => 1); |
6
|
|
|
|
|
|
|
has receiver => (required => 1); |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub optimize_grammar { |
9
|
8
|
|
|
8
|
0
|
19
|
my ($self, $start) = @_; |
10
|
8
|
|
|
|
|
29
|
my $tree = $self->grammar->{tree}; |
11
|
8
|
100
|
|
|
|
34
|
return if $tree->{'+optimized'}; |
12
|
4
|
50
|
|
|
|
19
|
$self->set_max_parse if $self->parser->{maxparse}; |
13
|
4
|
|
|
|
|
12
|
$self->{extra} = {}; |
14
|
4
|
|
|
|
|
27
|
while (my ($name, $node) = each %$tree) { |
15
|
240
|
100
|
|
|
|
443
|
next unless ref($node); |
16
|
224
|
|
|
|
|
348
|
$self->optimize_node($node); |
17
|
|
|
|
|
|
|
} |
18
|
4
|
|
|
|
|
38
|
$self->optimize_node({'.ref' => $start}); |
19
|
4
|
|
|
|
|
20
|
my $extra = delete $self->{extra}; |
20
|
4
|
|
|
|
|
19
|
for my $key (%$extra) { |
21
|
0
|
|
|
|
|
0
|
$tree->{$key} = $extra->{$key}; |
22
|
|
|
|
|
|
|
} |
23
|
4
|
|
|
|
|
20
|
$tree->{'+optimized'} = 1; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub optimize_node { |
27
|
596
|
|
|
596
|
0
|
671
|
my ($self, $node) = @_; |
28
|
|
|
|
|
|
|
|
29
|
596
|
|
|
|
|
527
|
my ($min, $max) = @{$node}{'+min', '+max'}; |
|
596
|
|
|
|
|
937
|
|
30
|
596
|
100
|
|
|
|
1622
|
$node->{'+min'} = defined($max) ? 0 : 1 |
|
|
100
|
|
|
|
|
|
31
|
|
|
|
|
|
|
unless defined $node->{'+min'}; |
32
|
596
|
100
|
|
|
|
1377
|
$node->{'+max'} = defined($min) ? 0 : 1 |
|
|
100
|
|
|
|
|
|
33
|
|
|
|
|
|
|
unless defined $node->{'+max'}; |
34
|
596
|
100
|
|
|
|
1101
|
$node->{'+asr'} = 0 |
35
|
|
|
|
|
|
|
unless defined $node->{'+asr'}; |
36
|
|
|
|
|
|
|
|
37
|
596
|
|
|
|
|
686
|
for my $kind (qw(ref rgx all any err code xxx)) { |
38
|
1064
|
50
|
|
|
|
1578
|
return if $kind eq 'xxx'; |
39
|
1064
|
100
|
|
|
|
2418
|
if ($node->{rule} = $node->{".$kind"}) { |
40
|
596
|
|
|
|
|
841
|
delete $node->{".$kind"}; |
41
|
596
|
|
|
|
|
686
|
$node->{kind} = $kind; |
42
|
596
|
100
|
|
|
|
864
|
if ($kind eq 'ref') { |
43
|
304
|
50
|
|
|
|
531
|
my $rule = $node->{rule} or die; |
44
|
304
|
50
|
|
|
|
598
|
if (my $method = $self->grammar->can("rule_$rule")) { |
|
|
50
|
|
|
|
|
|
45
|
0
|
|
|
|
|
0
|
$node->{method} = $self->make_method_wrapper($method); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
elsif (not $self->grammar->{tree}{$rule}) { |
48
|
0
|
0
|
|
|
|
0
|
if (my $method = $self->grammar->can("$rule")) { |
49
|
0
|
|
|
|
|
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
|
|
|
|
|
0
|
die "No rule '$rule' defined in grammar"; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
} |
60
|
596
|
50
|
33
|
|
|
1881
|
$node->{method} ||= $self->parser->can("match_$kind") or die; |
61
|
596
|
|
|
|
|
895
|
last; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
596
|
100
|
|
|
|
2549
|
if ($node->{kind} =~ /^(?:all|any)$/) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
66
|
132
|
|
|
|
|
109
|
$self->optimize_node($_) for @{$node->{rule}}; |
|
132
|
|
|
|
|
494
|
|
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
elsif ($node->{kind} eq 'ref') { |
69
|
304
|
|
|
|
|
340
|
my $ref = $node->{rule}; |
70
|
304
|
|
|
|
|
520
|
my $rule = $self->grammar->{tree}{$ref}; |
71
|
304
|
|
33
|
|
|
492
|
$rule ||= $self->{extra}{$ref} = {}; |
72
|
304
|
100
|
|
|
|
486
|
if (my $action = $self->receiver->can("got_$ref")) { |
|
|
50
|
|
|
|
|
|
73
|
112
|
|
|
|
|
169
|
$rule->{action} = $action; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
elsif (my $gotrule = $self->receiver->can("gotrule")) { |
76
|
192
|
|
|
|
|
278
|
$rule->{action} = $gotrule; |
77
|
|
|
|
|
|
|
} |
78
|
304
|
50
|
|
|
|
612
|
if ($self->parser->{debug}) { |
79
|
0
|
|
|
|
|
|
$node->{method} = $self->make_trace_wrapper($node->{method}); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
elsif ($node->{kind} eq 'rgx') { |
83
|
|
|
|
|
|
|
# XXX $node; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub make_method_wrapper { |
88
|
0
|
|
|
0
|
0
|
|
my ($self, $method) = @_; |
89
|
|
|
|
|
|
|
return sub { |
90
|
0
|
|
|
0
|
|
|
my ($parser, $ref, $parent) = @_; |
91
|
0
|
|
|
|
|
|
@{$parser}{'rule', 'parent'} = ($ref, $parent); |
|
0
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
$method->( |
93
|
|
|
|
|
|
|
$parser->{grammar}, |
94
|
|
|
|
|
|
|
$parser, |
95
|
|
|
|
|
|
|
$parser->{buffer}, |
96
|
|
|
|
|
|
|
$parser->{position}, |
97
|
|
|
|
|
|
|
); |
98
|
|
|
|
|
|
|
} |
99
|
0
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub make_trace_wrapper { |
102
|
0
|
|
|
0
|
0
|
|
my ($self, $method) = @_; |
103
|
|
|
|
|
|
|
return sub { |
104
|
0
|
|
|
0
|
|
|
my ($self, $ref, $parent) = @_; |
105
|
0
|
|
|
|
|
|
my $asr = $parent->{'+asr'}; |
106
|
0
|
0
|
|
|
|
|
my $note = |
|
|
0
|
|
|
|
|
|
107
|
|
|
|
|
|
|
$asr == -1 ? '(!)' : |
108
|
|
|
|
|
|
|
$asr == 1 ? '(=)' : |
109
|
|
|
|
|
|
|
''; |
110
|
0
|
|
|
|
|
|
$self->trace("try_$ref$note"); |
111
|
0
|
|
|
|
|
|
my $result; |
112
|
0
|
0
|
|
|
|
|
if ($result = $self->$method($ref, $parent)) { |
113
|
0
|
|
|
|
|
|
$self->trace("got_$ref$note"); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
else { |
116
|
0
|
|
|
|
|
|
$self->trace("not_$ref$note"); |
117
|
|
|
|
|
|
|
} |
118
|
0
|
|
|
|
|
|
return $result; |
119
|
|
|
|
|
|
|
} |
120
|
0
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub set_max_parse { |
123
|
0
|
|
|
0
|
0
|
|
require Pegex::Parser; |
124
|
0
|
|
|
|
|
|
my ($self) = @_; |
125
|
0
|
|
|
|
|
|
my $maxparse = $self->parser->{maxparse}; |
126
|
4
|
|
|
4
|
|
35
|
no warnings 'redefine'; |
|
4
|
|
|
|
|
24
|
|
|
4
|
|
|
|
|
650
|
|
127
|
0
|
|
|
|
|
|
my $method = \&Pegex::Parser::match_ref; |
128
|
0
|
|
|
|
|
|
my $counter = 0; |
129
|
|
|
|
|
|
|
*Pegex::Parser::match_ref = sub { |
130
|
0
|
0
|
|
0
|
|
|
die "Maximum parsing rules reached ($maxparse)\n" |
131
|
|
|
|
|
|
|
if $counter++ >= $maxparse; |
132
|
0
|
|
|
|
|
|
my $self = shift; |
133
|
0
|
|
|
|
|
|
$self->$method(@_); |
134
|
0
|
|
|
|
|
|
}; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
1; |