line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Pegex::Optimizer; |
2
|
1
|
|
|
1
|
|
5
|
use Pegex::Base; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
has parser => (required => 1); |
5
|
|
|
|
|
|
|
has grammar => (required => 1); |
6
|
|
|
|
|
|
|
has receiver => (required => 1); |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub optimize_grammar { |
9
|
2
|
|
|
2
|
0
|
4
|
my ($self, $start) = @_; |
10
|
2
|
|
|
|
|
6
|
my $tree = $self->grammar->{tree}; |
11
|
2
|
100
|
|
|
|
13
|
return if $tree->{'+optimized'}; |
12
|
1
|
50
|
|
|
|
5
|
$self->set_max_parse if $self->parser->{maxparse}; |
13
|
1
|
|
|
|
|
3
|
$self->{extra} = {}; |
14
|
1
|
|
|
|
|
7
|
while (my ($name, $node) = each %$tree) { |
15
|
60
|
100
|
|
|
|
109
|
next unless ref($node); |
16
|
56
|
|
|
|
|
91
|
$self->optimize_node($node); |
17
|
|
|
|
|
|
|
} |
18
|
1
|
|
|
|
|
6
|
$self->optimize_node({'.ref' => $start}); |
19
|
1
|
|
|
|
|
5
|
my $extra = delete $self->{extra}; |
20
|
1
|
|
|
|
|
3
|
for my $key (%$extra) { |
21
|
0
|
|
|
|
|
0
|
$tree->{$key} = $extra->{$key}; |
22
|
|
|
|
|
|
|
} |
23
|
1
|
|
|
|
|
12
|
$tree->{'+optimized'} = 1; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub optimize_node { |
27
|
149
|
|
|
149
|
0
|
163
|
my ($self, $node) = @_; |
28
|
|
|
|
|
|
|
|
29
|
149
|
|
|
|
|
148
|
my ($min, $max) = @{$node}{'+min', '+max'}; |
|
149
|
|
|
|
|
267
|
|
30
|
149
|
100
|
|
|
|
418
|
$node->{'+min'} = defined($max) ? 0 : 1 |
|
|
100
|
|
|
|
|
|
31
|
|
|
|
|
|
|
unless defined $node->{'+min'}; |
32
|
149
|
100
|
|
|
|
324
|
$node->{'+max'} = defined($min) ? 0 : 1 |
|
|
100
|
|
|
|
|
|
33
|
|
|
|
|
|
|
unless defined $node->{'+max'}; |
34
|
149
|
100
|
|
|
|
275
|
$node->{'+asr'} = 0 |
35
|
|
|
|
|
|
|
unless defined $node->{'+asr'}; |
36
|
|
|
|
|
|
|
|
37
|
149
|
|
|
|
|
171
|
for my $kind (qw(ref rgx all any err code xxx)) { |
38
|
266
|
50
|
|
|
|
457
|
return if $kind eq 'xxx'; |
39
|
266
|
100
|
|
|
|
759
|
if ($node->{rule} = $node->{".$kind"}) { |
40
|
149
|
|
|
|
|
208
|
delete $node->{".$kind"}; |
41
|
149
|
|
|
|
|
193
|
$node->{kind} = $kind; |
42
|
149
|
100
|
|
|
|
257
|
if ($kind eq 'ref') { |
43
|
76
|
50
|
|
|
|
141
|
my $rule = $node->{rule} or die; |
44
|
76
|
50
|
|
|
|
142
|
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
|
149
|
50
|
33
|
|
|
776
|
$node->{method} ||= $self->parser->can("match_$kind") or die; |
61
|
149
|
|
|
|
|
228
|
last; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
149
|
100
|
|
|
|
627
|
if ($node->{kind} =~ /^(?:all|any)$/) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
66
|
33
|
|
|
|
|
30
|
$self->optimize_node($_) for @{$node->{rule}}; |
|
33
|
|
|
|
|
107
|
|
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
elsif ($node->{kind} eq 'ref') { |
69
|
76
|
|
|
|
|
94
|
my $ref = $node->{rule}; |
70
|
76
|
|
|
|
|
138
|
my $rule = $self->grammar->{tree}{$ref}; |
71
|
76
|
|
33
|
|
|
138
|
$rule ||= $self->{extra}{$ref} = {}; |
72
|
76
|
100
|
|
|
|
141
|
if (my $action = $self->receiver->can("got_$ref")) { |
|
|
50
|
|
|
|
|
|
73
|
28
|
|
|
|
|
55
|
$rule->{action} = $action; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
elsif (my $gotrule = $self->receiver->can("gotrule")) { |
76
|
48
|
|
|
|
|
72
|
$rule->{action} = $gotrule; |
77
|
|
|
|
|
|
|
} |
78
|
76
|
50
|
|
|
|
192
|
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
|
1
|
|
|
1
|
|
10
|
no warnings 'redefine'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
310
|
|
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; |