File Coverage

blib/lib/Pegex/Parser.pm
Criterion Covered Total %
statement 146 207 70.5
branch 91 156 58.3
condition 25 56 44.6
subroutine 16 23 69.5
pod 0 16 0.0
total 278 458 60.7


line stmt bran cond sub pod time code
1             package Pegex::Parser;
2 11     11   109535 use Pegex::Base;
  11         20  
  11         50  
3 11     11   62 no warnings qw( recursion );
  11         14  
  11         346  
4              
5 11     11   3553 use Pegex::Input;
  11         24  
  11         289  
6 11     11   3667 use Pegex::Optimizer;
  11         24  
  11         268  
7 11     11   65 use Scalar::Util;
  11         17  
  11         8219  
8              
9             has grammar => (required => 1);
10             has receiver => ();
11             has input => ();
12              
13             has recursion_count => 0;
14             has iteration_count => 0;
15              
16             has debug => ();
17             has debug_indent => ();
18             has debug_color => ();
19             has debug_got_color => ();
20             has debug_not_color => ();
21              
22             has recursion_limit => ();
23             has recursion_warn_limit => ();
24             has iteration_limit => ();
25              
26             sub BUILD {
27 21     21 0 45 my ($self) = @_;
28              
29 21   50     112 $self->{throw_on_error} ||= 1;
30              
31             $self->{debug} =
32             defined($ENV{PERL_PEGEX_DEBUG}) ? $ENV{PERL_PEGEX_DEBUG} :
33             defined($Pegex::Parser::Debug) ? $Pegex::Parser::Debug : 0
34 21 50       119 unless defined($self->{debug});
    50          
    100          
35              
36             $self->{debug_indent} =
37             defined($ENV{PERL_PEGEX_DEBUG_INDENT}) ? $ENV{PERL_PEGEX_DEBUG_INDENT} :
38             defined($Pegex::Parser::DebugIndent) ? $Pegex::Parser::DebugIndent : 1
39 21 50       104 unless defined($self->{debug_indent});
    50          
    50          
40             $self->{debug_indent} = 1 if (
41             not length $self->{debug_indent}
42             or $self->{debug_indent} =~ tr/0-9//c
43 21 50 33     192 or $self->{debug_indent} < 0
      33        
44             );
45              
46 21 100       66 if ($self->{debug}) {
47             $self->{debug_color} =
48             defined($ENV{PERL_PEGEX_DEBUG_COLOR}) ? $ENV{PERL_PEGEX_DEBUG_COLOR} :
49             defined($Pegex::Parser::DebugColor) ? $Pegex::Parser::DebugColor : 1
50 2 50       17 unless defined($self->{debug_color});
    50          
    50          
51 2         4 my ($got, $not);
52             ($self->{debug_color}, $got, $not) =
53 2         9 split / *, */, $self->{debug_color};
54 2   50     11 $got ||= 'bright_green';
55 2   50     8 $not ||= 'bright_red';
56 2         8 $_ = [split ' ', $_] for ($got, $not);
57 2         5 $self->{debug_got_color} = $got;
58 2         4 $self->{debug_not_color} = $not;
59 2 50       8 my $c = defined($self->{debug_color}) ? $self->{debug_color} : 1;
60             $self->{debug_color} =
61 2 0       20 $c eq 'always' ? 1 :
    50          
    50          
    50          
    50          
62             $c eq 'auto' ? (-t STDERR ? 1 : 0) :
63             $c eq 'never' ? 0 :
64             $c =~ /^\d+$/ ? $c : 0;
65 2 50       13 if ($self->{debug_color}) {
66 2         1241 require Term::ANSIColor;
67 2 50       14545 if ($Term::ANSIColor::VERSION < 3.00) {
68 0         0 s/^bright_// for
69 0         0 @{$self->{debug_got_color}},
70 0         0 @{$self->{debug_not_color}};
71             }
72             }
73             }
74             $self->{recursion_limit} =
75             defined($ENV{PERL_PEGEX_RECURSION_LIMIT}) ? $ENV{PERL_PEGEX_RECURSION_LIMIT} :
76             defined($Pegex::Parser::RecursionLimit) ? $Pegex::Parser::RecursionLimit : 0
77 21 50       132 unless defined($self->{recursion_limit});
    50          
    50          
78             $self->{recursion_warn_limit} =
79             defined($ENV{PERL_PEGEX_RECURSION_WARN_LIMIT}) ? $ENV{PERL_PEGEX_RECURSION_WARN_LIMIT} :
80             defined($Pegex::Parser::RecursionWarnLimit) ? $Pegex::Parser::RecursionWarnLimit : 0
81 21 50       103 unless defined($self->{recursion_warn_limit});
    50          
    50          
82             $self->{iteration_limit} =
83             defined($ENV{PERL_PEGEX_ITERATION_LIMIT}) ? $ENV{PERL_PEGEX_ITERATION_LIMIT} :
84             defined($Pegex::Parser::IterationLimit) ? $Pegex::Parser::IterationLimit : 0
85 21 50       133 unless defined($self->{iteration_limit});
    50          
    50          
86             }
87              
88             # XXX Add an optional $position argument. Default to 0. This is the position
89             # to start parsing. Set position and farthest below to this value. Allows for
90             # sub-parsing. Need to somehow return the finishing position of a subparse.
91             # Maybe this all goes in a subparse() method.
92             sub parse {
93 19     19 0 69 my ($self, $input, $start) = @_;
94              
95 19 50       44 $start =~ s/-/_/g if $start;
96              
97 19         47 $self->{position} = 0;
98 19         33 $self->{farthest} = 0;
99              
100 19 100       100 $self->{input} = (not ref $input)
101             ? Pegex::Input->new(string => $input)
102             : $input;
103              
104             $self->{input}->open
105 19 50       105 unless $self->{input}{_is_open};
106 19         74 $self->{buffer} = $self->{input}->read;
107 19         33 $self->{last_line_pos} = 0;
108 19         55 $self->{last_line} = 1;
109              
110 19   66     194 $self->{grammar}{tree} ||= $self->{grammar}->make_tree;
111              
112             my $start_rule_ref = $start ||
113             $self->{grammar}{tree}{'+toprule'} ||
114 19 50 33     193 $self->{grammar}{tree}{'TOP'} & 'TOP' or
115             die "No starting rule for Pegex::Parser::parse";
116              
117             die "No 'receiver'. Can't parse"
118 19 50       72 unless $self->{receiver};
119              
120             my $optimizer = Pegex::Optimizer->new(
121             parser => $self,
122             grammar => $self->{grammar},
123             receiver => $self->{receiver},
124 19         104 );
125              
126 19         77 $optimizer->optimize_grammar($start_rule_ref);
127              
128             # Add circular ref and weaken it.
129 19         34 $self->{receiver}{parser} = $self;
130 19         97 Scalar::Util::weaken($self->{receiver}{parser});
131              
132 19 50       134 if ($self->{receiver}->can("initial")) {
133 0         0 $self->{rule} = $start_rule_ref;
134 0         0 $self->{parent} = {};
135 0         0 $self->{receiver}->initial();
136             }
137              
138 19         45 local *match_next;
139             {
140 11     11   72 no warnings 'redefine';
  11         22  
  11         9068  
  19         24  
141             *match_next = (
142             $self->{recursion_warn_limit} or
143             $self->{recursion_limit} or
144             $self->{iteration_limit}
145 19 50 33     183 ) ? \&match_next_with_limit :
146             \&match_next_normal;
147             }
148              
149 19 100       69 my $match = $self->debug ? do {
150 1         3 my $method = $optimizer->make_trace_wrapper(\&match_ref);
151 1         3 $self->$method($start_rule_ref, {'+asr' => 0});
152             } : $self->match_ref($start_rule_ref, {});
153              
154 19         88 $self->{input}->close;
155              
156 19 50 33     102 if (not $match or $self->{position} < length ${$self->{buffer}}) {
  19         63  
157 0         0 $self->throw_error("Parse document failed for some reason");
158 0         0 return; # In case $self->throw_on_error is off
159             }
160              
161 19 100       141 if ($self->{receiver}->can("final")) {
162 17         42 $self->{rule} = $start_rule_ref;
163 17         38 $self->{parent} = {};
164 17         56 $match = [ $self->{receiver}->final(@$match) ];
165             }
166              
167 19         176 $match->[0];
168             }
169              
170             sub match_next_normal {
171 3442     3442 0 3858 my ($self, $next) = @_;
172              
173             my ($rule, $method, $kind, $min, $max, $assertion) =
174 3442         3425 @{$next}{'rule', 'method', 'kind', '+min', '+max', '+asr'};
  3442         5522  
175              
176             my ($position, $match, $count) =
177 3442         4838 ($self->{position}, [], 0);
178              
179 3442         4650 while (my $return = $method->($self, $rule, $next)) {
180 1376 50       2075 $position = $self->{position} unless $assertion;
181 1376         1330 $count++;
182 1376         1679 push @$match, @$return;
183 1376 100       2095 last if $max == 1;
184             }
185 3442 100 100     7368 if (not $count and $min == 0 and $kind eq 'all') {
      100        
186 125         197 $match = [[]];
187             }
188 3442 100       4372 if ($max != 1) {
189 116 50       186 if ($next->{-flat}) {
190 0 0       0 $match = [ map { (ref($_) eq 'ARRAY') ? (@$_) : ($_) } @$match ];
  0         0  
191             }
192             else {
193 116         169 $match = [$match]
194             }
195             }
196 3442   66     6556 my $result = ($count >= $min and (not $max or $count <= $max))
197             ^ ($assertion == -1);
198 3442 100 100     5595 if (not($result) or $assertion) {
199             $self->{farthest} = $position
200 1985 50       2883 if ($self->{position} = $position) > $self->{farthest};
201             }
202              
203 3442 50       7685 ($result ? $next->{'-skip'} ? [] : $match : 0);
    100          
204             }
205              
206             sub match_next_with_limit {
207 0     0 0 0 my ($self, $next) = @_;
208              
209             sub limit_msg {
210 0     0 0 0 "Deep recursion ($_[0] levels) on Pegex::Parser::match_next\n";
211             }
212              
213 0         0 $self->{iteration_count}++;
214 0         0 $self->{recursion_count}++;
215              
216 0 0 0     0 if (
    0 0        
    0 0        
217             $self->{recursion_limit} and
218             $self->{recursion_count} >= $self->{recursion_limit}
219 0         0 ) { die limit_msg $self->{recursion_count} }
220             elsif (
221             $self->{recursion_warn_limit} and
222             not ($self->{recursion_count} % $self->{recursion_warn_limit})
223 0         0 ) { warn limit_msg $self->{recursion_count} }
224             elsif (
225             $self->{iteration_limit} and
226             $self->{iteration_count} > $self->{iteration_limit}
227 0         0 ) { die "Pegex iteration limit of $self->{iteration_limit} reached." }
228              
229 0         0 my $result = $self->match_next_normal($next);
230              
231 0         0 $self->{recursion_count}--;
232              
233 0         0 return $result;
234             }
235              
236             sub match_rule {
237 1     1 0 8 my ($self, $position, $match) = (@_, []);
238 1         2 $self->{position} = $position;
239             $self->{farthest} = $position
240 1 50       3 if $position > $self->{farthest};
241 1 50       3 $match = [ $match ] if @$match > 1;
242 1         3 my ($ref, $parent) = @{$self}{'rule', 'parent'};
  1         2  
243 1 50       3 my $rule = $self->{grammar}{tree}{$ref}
244             or die "No rule defined for '$ref'";
245              
246 1         4 [ $rule->{action}->($self->{receiver}, @$match) ];
247             }
248              
249             sub match_ref {
250 1474     1474 0 1890 my ($self, $ref, $parent) = @_;
251 1474 50       2449 my $rule = $self->{grammar}{tree}{$ref}
252             or die "No rule defined for '$ref'";
253 1474 100       2220 my $match = $self->match_next($rule) or return;
254 595 100       978 return $Pegex::Constant::Dummy unless $rule->{action};
255 572         659 @{$self}{'rule', 'parent'} = ($ref, $parent);
  572         826  
256              
257             # XXX Possible API mismatch.
258             # Not sure if we should "splat" the $match.
259 572         1273 [ $rule->{action}->($self->{receiver}, @$match) ];
260             }
261              
262             sub match_rgx {
263 1287     1287 0 1506 my ($self, $regexp) = @_;
264 1287         1407 my $buffer = $self->{buffer};
265              
266 1287         2171 pos($$buffer) = $self->{position};
267 1287 100       5844 $$buffer =~ /$regexp/g or return;
268              
269 401         647 $self->{position} = pos($$buffer);
270              
271             $self->{farthest} = $self->{position}
272 401 100       678 if $self->{position} > $self->{farthest};
273              
274 11     11   76 no strict 'refs';
  11         22  
  11         10164  
275 401         1493 my $captures = [ map $$_, 1..$#+ ];
276 401 100       855 $captures = [ $captures ] if $#+ > 1;
277              
278 401         777 return $captures;
279             }
280              
281             sub match_all {
282 571     571 0 715 my ($self, $list) = @_;
283 571         624 my $position = $self->{position};
284 571         644 my $set = [];
285 571         586 my $len = 0;
286 571         683 for my $elem (@$list) {
287 1036 100       1705 if (my $match = $self->match_next($elem)) {
288 701 50 66     1740 if (not ($elem->{'+asr'} or $elem->{'-skip'})) {
289 700         855 push @$set, @$match;
290 700         1067 $len++;
291             }
292             }
293             else {
294             $self->{farthest} = $position
295 335 50       531 if ($self->{position} = $position) > $self->{farthest};
296 335         583 return;
297             }
298             }
299 236 50       440 $set = [ $set ] if $len > 1;
300 236         399 return $set;
301             }
302              
303             sub match_any {
304 241     241 0 331 my ($self, $list) = @_;
305 241         311 for my $elem (@$list) {
306 932 100       1274 if (my $match = $self->match_next($elem)) {
307 162         324 return $match;
308             }
309             }
310 79         135 return;
311             }
312              
313             sub match_err {
314 0     0 0 0 my ($self, $error) = @_;
315 0         0 $self->throw_error($error);
316             }
317              
318             sub trace {
319 46     46 0 69 my ($self, $action) = @_;
320 46 100       113 my $indent = ($action =~ /^try_/) ? 1 : 0;
321 46   100     77 $self->{indent} ||= 0;
322 46 100       65 $self->{indent}-- unless $indent;
323              
324             $action = (
325             $action =~ m/got_/ ?
326             Term::ANSIColor::colored($self->{debug_got_color}, $action) :
327             $action =~ m/not_/ ?
328             Term::ANSIColor::colored($self->{debug_not_color}, $action) :
329             $action
330 46 50       153 ) if $self->{debug_color};
    100          
    50          
331              
332 46         2522 print STDERR ' ' x ($self->{indent} * $self->{debug_indent});
333 46 100       157 $self->{indent}++ if $indent;
334 46         51 my $snippet = substr(${$self->{buffer}}, $self->{position});
  46         143  
335 46 100       107 $snippet = substr($snippet, 0, 30) . "..."
336             if length $snippet > 30;
337 46         174 $snippet =~ s/\n/\\n/g;
338 46 100       1764 print STDERR sprintf("%-30s", $action) .
339             ($indent ? " >$snippet<\n" : "\n");
340             }
341              
342             sub throw_error {
343 0     0 0   my ($self, $msg) = @_;
344 0           $@ = $self->{error} = $self->format_error($msg);
345 0 0         return undef unless $self->{throw_on_error};
346 0           require Carp;
347 0           Carp::croak($self->{error});
348             }
349              
350             sub format_error {
351 0     0 0   my ($self, $msg) = @_;
352 0           my $buffer = $self->{buffer};
353 0           my $position = $self->{farthest};
354 0           my $real_pos = $self->{position};
355              
356 0           my $line = $self->line($position);
357 0           my $column = $position - rindex($$buffer, "\n", $position);
358              
359 0 0         my $pretext = substr(
    0          
360             $$buffer,
361             $position < 50 ? 0 : $position - 50,
362             $position < 50 ? $position : 50
363             );
364 0           my $context = substr($$buffer, $position, 50);
365 0           $pretext =~ s/.*\n//gs;
366 0           $context =~ s/\n/\\n/g;
367              
368 0           return <<"...";
369             Error parsing Pegex document:
370             msg: $msg
371             line: $line
372             column: $column
373             context: $pretext$context
374 0           ${\ (' ' x (length($pretext) + 10) . '^')}
375             position: $position ($real_pos pre-lookahead)
376             ...
377             }
378              
379             # TODO Move this to a Parser helper role/subclass
380             sub line_column {
381 0     0 0   my ($self, $position) = @_;
382 0   0       $position ||= $self->{position};
383 0           my $buffer = $self->{buffer};
384 0           my $line = $self->line($position);
385 0           my $column = $position - rindex($$buffer, "\n", $position);
386 0           return [$line, $column];
387             }
388              
389             sub line {
390 0     0 0   my ($self, $position) = @_;
391 0   0       $position ||= $self->{position};
392 0           my $buffer = $self->{buffer};
393 0           my $last_line = $self->{last_line};
394 0           my $last_line_pos = $self->{last_line_pos};
395 0           my $len = $position - $last_line_pos;
396 0 0         if ($len == 0) {
397 0           return $last_line;
398             }
399 0           my $line;
400 0 0         if ($len < 0) {
401 0           $line = $last_line - scalar substr($$buffer, $position, -$len) =~ tr/\n//;
402             } else {
403 0           $line = $last_line + scalar substr($$buffer, $last_line_pos, $len) =~ tr/\n//;
404             }
405 0           $self->{last_line} = $line;
406 0           $self->{last_line_pos} = $position;
407 0           return $line;
408             }
409              
410             # XXX Need to figure out what uses this. (sample.t)
411             {
412             package Pegex::Constant;
413             our $Null = [];
414             our $Dummy = [];
415             }
416              
417             1;