blib/lib/HOP/Parser.pm

filter: Statementstmt 81.0% 163 / 201 Branchbran 63.4% 52 / 82 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 88.6% 47 / 53 Podpod 100% 25 / 25 totaltotal 78.6% 294 / 374 SCAR 45.1
CC
52
Cov
76%
fetch_error
156.0
__ANON__
8.0
__ANON__
6.0
CRAP 91.3
↑ Summary Build »
1 package HOP::Parser;
2  
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use HOP::Stream qw/drop tail head node is_node/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 our %N;
10  
11 6 our @EXPORT_OK = qw(
12 absorb
13 action
14 alternate
15 concatenate
16 debug
17 fetch_error
18 End_of_Input
19 error
20 list_of
21 list_values_of
22 lookfor
23 lookahead
24 neg_lookahead
25 match
26 nothing
27 null_list
28 operator
29 optional
30 parser
31 rlist_of
32 rlist_values_of
33 star
34 plus
35 T
36 test
37 );
38  
39 6 our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
40  
41 sub parser (&); # Forward declaration - see below
42  
43 =head1 NAME
44  
45 HOP::Parser - "Higher Order Perl" Parser
46  
47 =head1 VERSION
48  
49 Version 0.02_05
50  
51 =cut
52  
53 6 our $VERSION = '0.02_05';
54  
55 =head1 SYNOPSIS
56  
57 use HOP::Parser qw/:all/;
58  
59 # assemble a bunch of parsers according to a grammar
60  
61 =head1 DESCRIPTION
62  
63 This package is based on the Parser.pm code from the book "Higher Order Perl",
64 by Mark Jason Dominus.
65  
66 This module implements recursive-descent parsers by allowing programmers to
67 build a bunch of smaller parsers to represent grammar elements and assemble
68 them into a full parser. Pages 376 to 415 of the first and second editions
69 of HOP should be enough to get you up to speed :)
70  
71 The PDF for the second edition can be downloaded from MJD's site:
72 L<http://hop.perl.plover.com/book/pdf/HigherOrderPerl.pdf>.
73  
74 Please note that this module should be considered B<ALPHA> code. While
75 everything works fairly well, the documentation is incomplete and some of the
76 functions could stand to be better named (C<rlist_of>, for example).
77  
78 =head1 EXPORT
79  
80 =over 4
81  
82 =item * absorb
83  
84 =item * action
85  
86 =item * alternate
87  
88 =item * concatenate
89  
90 =item * debug
91  
92 =item * fetch_error
93  
94 =item * End_of_Input
95  
96 =item * error
97  
98 =item * list_of
99  
100 =item * list_values_of
101  
102 =item * lookfor
103  
104 =item * match
105  
106 =item * lookahead
107  
108 =item * neg_lookahead
109  
110 =item * nothing
111  
112 =item * null_list
113  
114 =item * operator
115  
116 =item * optional
117  
118 =item * parser
119  
120 =item * rlist_of
121  
122 =item * rlist_values_of
123  
124 =item * star
125  
126 =item * plus
127  
128 =item * T
129  
130 =item * test
131  
132 =back
133  
134 =head1 FUNCTIONS
135  
136 =head2 nothing
137  
138 my ($parsed, $remainder) = nothing($stream);
139  
140 C<nothing> is a special purpose parser which is used internally. It always
141 succeeds and returns I<undef> for C<$parsed> and the C<$remainder> is the
142 unaltered input C<$stream>.
143  
144 =cut
145  
146 sub nothing {
147 53 ▶ my $input = shift;
Subroutine nothing: called CC=1 SCAR=0.0
Pod documented
148 53 return ( undef, $input );
149 }
150  
151 ##############################################################################
152  
153 =head2 End_of_Input
154  
155 if (End_of_Input($stream)) {
156 ...
157 }
158  
159 C<End_of_Input> is another special purpose parser which only succeeds if there
160 is no input left in the stream. It's generally used in the I<start symbol> of
161 the grammar.
162  
163 # entire_input ::= statements 'End_Of_Input'
164  
165 my $entire_input = concatenate(
166 $statements,
167 \&End_of_Input
168 );
169  
170  
171 =cut
172  
173 sub End_of_Input {
174 16 ▶ my $input = shift;
Subroutine End_of_Input: called CC=2 SCAR=6.9
Pod documented
175 16 ▶ return ( undef, undef ) unless defined($input);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 14 2 16
176 2 die [ "End of input", $input ];
177 }
178  
179 ##############################################################################
180  
181 =head2 lookfor
182  
183 my $parser = lookfor($label, [\&get_value], [$param]); # or
184 my $parser = lookfor(\@label_and_optional_values, [\&get_value], [$param]);
185 my ($parsed, $remaining_stream) = $parser->($stream);
186  
187 The following details the arguments to C<lookfor>.
188  
189 =over 4
190  
191 =item * C<$label> or C<@label_and_optional_values>
192  
193 The first argument is either a scalar with the token label or an array
194 reference. The first element in the array reference should be the token label
195 and subsequent elements can be anything you need. Usually the second element
196 is the token value, but if you need more than this, that's OK.
197  
198 =item * C<\&get_value>
199  
200 If an optional C<get_value> subroutine is supplied, that C<get_value> will be
201 applied to the parsed value prior to it being returned. This is useful if
202 non-standard tokens are being passed in or if we wish to preprocess the
203 returned values.
204  
205 =item * C<$param>
206  
207 If needed, additional arguments besides the current matched token can be
208 passed to C<&get_value>. Supply them as the third argument (which can be any
209 data structure you wish, so long as it's a single scalar value).
210  
211 =back
212  
213 In practice, the full power of this function is rarely needed and C<match> is
214 used instead.
215  
216 =cut
217  
218 658 ▶my $is_node = sub { is_node($_[0]) || ref $_[0] eq 'ARRAY' };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
is_node($_[0]) or ref $_[0] eq "ARRAY" 269 389 658
219  
220 sub lookfor {
221 171 ▶ my $wanted = shift;
Subroutine lookfor: called CC=3 SCAR=11.0
Pod documented
222 171 ▶ my $value = shift || sub { $_[0][1] };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
shift() || sub { $_[0][1]; }
l!l&&r!l&&!r
51660
MC/DC0%0/2
shift() || sub { $_[0][1]; }
shift() sub { $_[0][1]; }
Truth table66%2/3
A || B
A= shift()B= sub { $_[0][1]; }
ABresult
1X1
011
000
223 171 my $param = shift;
224  
225 171 ▶ $wanted = [$wanted] unless ref $wanted;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $wanted 29 142 171
226 my $parser = parser {
227 697 ▶ my $input = shift;
Subroutine __ANON__: called CC=8 SCAR=20.8
228 697 ▶ $input = node(@$input) if ref($input) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $input eq "ARRAY" 41 656 697
229 697 ▶ unless ( defined $input ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $input) 64 633 697
230 64 die [ 'TOKEN', $input, $wanted ];
231 }
232  
233 633 my $next = head($input);
234 633 for my $i ( 0 .. $#$wanted ) {
235 758 ▶ next unless defined $wanted->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wanted->[$i] 0 758 758
236 6 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
237 758 ▶ unless ($wanted->[$i] eq $next->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wanted->[$i] eq $next->[$i]) 281 477 758
238 281 die [ 'TOKEN', $input, $wanted ];
239 }
240 }
241 352 my $wanted_value = $value->( $next, $param );
242  
243 # the following is unlikely to affect a stream with a promise
244 # for a tail as the promise tends to Do The Right Thing.
245 #
246 # Otherwise, the AoA stream might just return an aref for
247 # the tail instead of an AoA. This breaks things
248 352 my $tail = tail($input);
249 352 ▶ $tail = [$tail]
Branch: 1 branch
BranchTrueFalseTotal
if &$is_node($tail) and not &$is_node($tail->[0]) 14 338 352
Condition100%3/3
&$is_node($tail) and not &$is_node($tail->[0])
!ll&&!rl&&r
4629214
MC/DC100%2/2
&$is_node($tail) and not &$is_node($tail->[0])
&$is_node($tail) not &$is_node($tail->[0])
Truth table100%3/3
A and not(B)
A= &$is_node($tail)B= not &$is_node($tail->[0])
ABresult
0X0
100
111
250 if $is_node->($tail) and not $is_node->($tail->[0]);
251 352 return ( $wanted_value, $tail );
252 171 };
253 171 $N{$parser} = "[@$wanted]";
254 171 return $parser;
255 }
256  
257 ##############################################################################
258  
259 =head2 match
260  
261 my $parser = match($label, [$value]);
262 my ($parsed, $remainder) = $parser->($stream);
263  
264 This function takes a label and an optional value and builds a parser which
265 matches them by dispatching to C<lookfor> with the arguments as an array
266 reference. See C<lookfor> for more information.
267  
268 =cut
269  
270 126 ▶sub match { @_ = [@_]; goto &lookfor }
Subroutine match: called CC=1 SCAR=0.0
Pod documented
271  
272 ##############################################################################
273  
274 =head2 parser
275  
276 my $parser = parser { 'some code' };
277  
278 Currently, this is merely syntactic sugar that allows us to declare a naked
279 block as a subroutine (i.e., omit the "sub" keyword).
280  
281 =cut
282  
283 562 ▶sub parser (&) { $_[0] }
Subroutine parser: called CC=1 SCAR=0.0
Pod documented
284  
285 ##############################################################################
286  
287 =head2 lookahead
288  
289 my $parser = lookahead( $label );
290 $parser = lookahead( $parser );
291  
292 This function takes a parser argument or list of arguments supported by
293 C<lookfor()> and returns a parser that will return true if the parser matches,
294 but does not actually change the stream. This is so that you can write parsers
295 that match something and then look ahead to see if they match the next thing,
296 without actualy consuming that next thing. This is akin to a zero width
297 positive look-ahead in a regular expression.
298  
299 =cut
300  
301 sub lookahead {
302 4 ▶ my $p = ref $_[0] eq 'CODE' ? shift : lookfor @_;
Subroutine lookahead: called CC=2 SCAR=7.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'CODE' ? : 2 2 4
303 parser {
304 8 ▶ my $input = shift or return;
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
my $input = shift() or return 0 8 8
305 8 $p->($input);
306 4 return (undef, $input);
307 },
308 4 }
309  
310 ##############################################################################
311  
312 =head2 neg_lookahead
313  
314 my $parser = neg_lookahead( $label );
315 $parser = neg_lookahead( $parser );
316  
317 This function returns a parser that returns true if it looks ahead and does
318 not find a match for the specified parser. That is, it's akin to a zero width
319 negative look-ahead in a regular expression. The supported arguments are the
320 same as for C<lookahead()>.
321  
322 =cut
323  
324 sub neg_lookahead {
325 4 ▶ my $p = ref $_[0] eq 'CODE' ? shift : lookfor @_;
Subroutine neg_lookahead: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'CODE' ? : 2 2 4
326 parser {
327 8 ▶ my $input = shift or return;
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
my $input = shift() or return 0 8 8
328 8 my @ret = eval { $p->($input) };
329 8 ▶ die [ 'TOKEN', $input, $p ] if @ret;
Branch: 1 branch
BranchTrueFalseTotal
if @ret 4 4 8
330 4 return (undef, $input);
331 },
332 4 }
333  
334 ##############################################################################
335  
336 =head2 concatenate
337  
338 my $parser = concatenate(@parsers);
339 my ($values, $remainder) = $parser->($stream);
340  
341 This function takes a list of parsers and returns a new parser. The new parser
342 succeeds if all parsers passed to C<concatenate> succeed sequentially.
343  
344 C<concatenate> will discard undefined values. This allows us to do this and
345 only return the desired value(s):
346  
347 concatenate(absorb($lparen), $value, absorb($rparen))
348  
349 =cut
350  
351 sub concatenate {
352 139 ▶ shift unless ref $_[0];
Subroutine concatenate: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 2 137 139
353 139 my @parsers = @_;
354 139 ▶ return \&nothing if @parsers == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @parsers == 0 2 137 139
355 137 ▶ return $parsers[0] if @parsers == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @parsers == 1 2 135 137
356  
357 my $parser = parser {
358 719 ▶ my $input = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
359 719 my ( $v, @values );
360 719 for (@parsers) {
361 1146 ( $v, $input ) = $_->($input);
362 798 ▶ push @values, $v if defined $v; # assumes we wish to discard undef
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 762 36 798
363 }
364 371 return ( \@values, $input );
365 135 };
366 }
367  
368 ##############################################################################
369  
370 =head2 alternate
371  
372 my $parser = alternate(@parsers);
373 my ($parsed, $remainder) = $parser->stream;
374  
375 This function behaves like C<concatenate> but matches one of any tokens
376 (rather than all tokens sequentially).
377  
378 =cut
379  
380 sub alternate {
381 77 ▶ my @parsers = @_;
Subroutine alternate: called CC=3 SCAR=11.0
Pod documented
382 2 ▶ return parser { return () }
Subroutine __ANON__: called CC=1 SCAR=0.0
383 77 ▶ if @parsers == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @parsers == 0 2 75 77
384 75 ▶ return $parsers[0] if @parsers == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @parsers == 1 0 75 75
385  
386 my $parser = parser {
387 499 ▶ my $input = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
388 499 my @failures;
389  
390 499 for (@parsers) {
391 818 my ( $v, $newinput ) = eval { $_->($input) };
392 818 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 403 415 818
393 403 ▶ die unless ref $@; # not a parser failure
Branch: 1 branch
BranchTrueFalseTotal
unless ref $@ 0 403 403
394 403 push @failures, $@;
395 }
396 else {
397 415 return ( $v, $newinput );
398 }
399 }
400 84 die [ 'ALT', $input, \@failures ];
401 75 };
402 {
403 6 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
404 75 $N{$parser} = "(" . join ( " | ", map $N{$_}, @parsers ) . ")";
405 }
406 75 return $parser;
407 }
408  
409 ##############################################################################
410  
411 =head2 list_of
412  
413 my $parser = list_of( $element, $separator );
414 my ($parsed, $remainder) = $parser->($stream);
415  
416 This function takes two parsers and returns a new parser which matches a
417 C<$separator> delimited list of C<$element> items.
418  
419 =cut
420  
421 sub list_of {
422 12 ▶ my ( $element, $separator ) = @_;
Subroutine list_of: called CC=2 SCAR=7.0
Pod documented
423 12 ▶ $separator = lookfor('COMMA') unless defined $separator;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $separator 4 8 12
424  
425 return T(
426 concatenate( $element, star( concatenate( $separator, $element ) ) ),
427 12 ▶ sub {[ $_[0], $_[1] ? map { @$_ } @{ $_[1] } : () ] },
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 12 0 12
428 12 );
429 }
430  
431 ##############################################################################
432  
433 =head2 rlist_of
434  
435 my $parser = list_of( $element, $separator );
436 my ($parsed, $remainder) = $parser->($stream);
437  
438 This function takes two parsers and returns a new parser which matches a
439 C<$separator> delimited list of C<$element> items. Unlike C<list_of>, this
440 parser expects a leading C<$separator> in what it matches.
441  
442 =cut
443  
444 sub rlist_of {
445 6 ▶ my ( $element, $separator ) = @_;
Subroutine rlist_of: called CC=2 SCAR=6.9
Pod documented
446 6 ▶ $separator = lookfor('COMMA') unless defined $separator;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $separator 4 2 6
447  
448 return T( concatenate( $separator, list_of( $element, $separator ) ),
449 6 ▶ sub { [ $_[0], @{ $_[1] } ] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
450 }
451  
452 ##############################################################################
453  
454 =head2 list_values_of
455  
456 my $parser = list_of( $element, $separator );
457 my ($parsed, $remainder) = $parser->($stream);
458  
459 This parser generator is the same as C<&list_of>, but it only returns the
460 elements, not the separators.
461  
462 =cut
463  
464 sub list_values_of {
465 12 ▶ my ( $element, $separator ) = @_;
Subroutine list_values_of: called CC=2 SCAR=6.9
Pod documented
466 12 ▶ $separator = lookfor('COMMA') unless defined $separator;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $separator 4 8 12
467  
468 return T(
469 concatenate(
470 $element, star( concatenate( absorb($separator), $element ) )
471 ),
472 sub {
473 12 ▶ my @matches = shift;
Subroutine __ANON__: called CC=3 SCAR=11.1
474 12 ▶ if ( my $tail = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tail = shift()) 12 0 12
475 12 foreach my $match (@$tail) {
476 16 push @matches, grep defined $_, @$match;
477 }
478 }
479 12 return \@matches;
480 }
481 12 );
482 }
483  
484 ##############################################################################
485  
486 =head2 rlist_values_of
487  
488 my $parser = list_of( $element, $separator );
489 my ($parsed, $remainder) = $parser->($stream);
490  
491 This parser generator is the same as C<&list_values_of>, but it only returns
492 the elements, not the separators.
493  
494 List C<rlist_of>, it expects a separator at the beginning of the list.
495  
496 =cut
497  
498 sub rlist_values_of {
499 6 ▶ my ( $element, $separator ) = @_;
Subroutine rlist_values_of: called CC=2 SCAR=6.9
Pod documented
500 6 ▶ $separator = lookfor('COMMA') unless defined $separator;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $separator 4 2 6
501  
502 return T( concatenate( $separator, list_values_of( $element, $separator ) ),
503 6 ▶ sub { $_[1] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
504 }
505  
506 ##############################################################################
507  
508 =head2 absorb
509  
510 my $parser = absorb( $parser );
511 my ($parsed, $remainder) = $parser->($stream);
512  
513 This special-purpose parser will allow you to match a given item but not
514 actually return anything. This is very useful when matching commas in lists,
515 statement separators, etc.
516  
517 =cut
518  
519 sub absorb {
520 12 ▶ my $parser = shift;
Subroutine absorb: called CC=1 SCAR=0.0
Pod documented
521 12 ▶ return T( $parser, sub { () } );
Subroutine __ANON__: called CC=1 SCAR=0.0
522 }
523  
524 ##############################################################################
525  
526 =head2 T
527  
528 my @result = T( $parser, \&transform );
529  
530 Given a parser and a transformation sub, this function will apply the
531 tranformation to the values returned by the parser, if any.
532  
533 =cut
534  
535 sub T {
536 115 ▶ my ( $parser, $transform ) = @_;
Subroutine T: called CC=1 SCAR=0.0
Pod documented
537 return parser {
538 720 ▶ my $input = shift;
Subroutine __ANON__: called CC=3 SCAR=11.2
539 720 ▶ if ( my ( $value, $newinput ) = $parser->($input) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$parser($input)) { } 394 0 394
540 394 local $^W; # using this to suppress 'uninitialized' warnings
541 394 ▶ $value = [$value] if !ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 73 321 394
542 394 $value = $transform->(@$value);
543 394 return ( $value, $newinput );
544 }
545 else {
546 0 return;
547 }
548 115 };
549 }
550  
551 ##############################################################################
552  
553 =head2 null_list
554  
555 my ($parsed, $remainder) = null_list($stream);
556  
557 This special purpose parser always succeeds and returns an empty array
558 reference and the stream.
559  
560 =cut
561  
562 sub null_list {
563 120 ▶ my $input = shift;
Subroutine null_list: called CC=1 SCAR=0.0
Pod documented
564 120 return ( [], $input );
565 }
566  
567 ##############################################################################
568  
569 =head2 star
570  
571 my $parser = star($another_parser);
572 my ($parsed, $remainder) = $parser->($stream);
573  
574 This parser always succeeds and matches zero or more instances of
575 C<$another_parser>. It parallels the regular expression C<*> quantifier. If it
576 matches zero, it returns the same results as C<null_list>. Otherwise, it
577 returns and array ref of the matched values and the remainder of the stream.
578  
579 =cut
580  
581 my $star_plus_t = sub {
582 125 ▶ my ( $first, $rest ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
583 125 [ $first, @$rest ];
584 6 };
585  
586 sub star {
587 47 ▶ my $p = shift;
Subroutine star: called CC=1 SCAR=0.0
Pod documented
588 47 my $p_star;
589 $p_star = alternate(
590 T(
591 47 ▶ concatenate( $p, parser { $p_star->(@_) } ),
Subroutine __ANON__: called CC=1 SCAR=0.0
592 $star_plus_t,
593 ),
594 \&null_list
595 );
596 }
597  
598 ##############################################################################
599  
600 =head2 plus
601  
602 my $parser = plus($another_parser);
603 my ($parsed, $remainder) = $parser->($stream);
604  
605 This parser succeeds when it matches one or more instances of
606 C<$another_parser>. It parallels the regular expression C<+> quantifier. If it
607 matches one or more, it returns and array ref of the matched values and the
608 remainder of the stream.
609  
610 =cut
611  
612 sub plus {
613 8 ▶ my $p = shift;
Subroutine plus: called CC=1 SCAR=0.0
Pod documented
614 8 T(
615 concatenate( $p, star($p) ),
616 $star_plus_t,
617 );
618 }
619  
620 ##############################################################################
621  
622 =head2 optional
623  
624 my $parser = optional($another_parser);
625 my ($parser, $remainder) = $parser->(stream);
626  
627 This parser matches 0 or 1 of the given parser item. It parallels the regular
628 expression C<?> quantifier.
629  
630 =cut
631  
632 sub optional {
633 5 ▶ my $parser = shift;
Subroutine optional: called CC=1 SCAR=0.0
Pod documented
634 5 return alternate (
635 $parser,
636 \&null_list,
637 );
638 }
639  
640 ## Chapter 8 section 4.4
641  
642 sub operator {
643 2 ▶ my ( $subpart, @ops ) = @_;
Subroutine operator: called CC=2 SCAR=6.9
Pod documented
644 2 my (@alternatives);
645 2 for my $operator (@ops) {
646 4 my ( $op, $opfunc ) = @$operator;
647 push @alternatives, T(
648 concatenate( $op, $subpart ),
649 sub {
650 24 ▶ my $subpart_value = $_[1];
Subroutine __ANON__: called CC=1 SCAR=0.0
651 24 ▶ sub { $opfunc->( $_[0], $subpart_value ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
652 24 }
653 4 );
654 }
655 my $result = T(
656 concatenate( $subpart, star( alternate(@alternatives) ) ),
657 sub {
658 62 ▶ my ( $total, $funcs ) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
659 62 for my $f (@$funcs) {
660 24 $total = $f->($total);
661 }
662 62 $total;
663 }
664 2 );
665 }
666  
667 ## Chapter 8 section 4.7.1
668  
669 sub error {
670 1 ▶ my ($try) = @_;
Subroutine error: called CC=1 SCAR=0.2
Pod documented
671 return parser {
672 12 ▶ my $input = shift;
Subroutine __ANON__: called CC=3 SCAR=11.8
673 12 my @result = eval { $try->($input) };
674 12 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 12 0 12
675 12 ▶ die ref $@ ? $@ : "Internal error ($@)";
Branch: 1 branch
BranchTrueFalseTotal
ref $@ ? : 12 0 12
676 }
677 0 return @result;
678 1 };
679 }
680  
681 ## Chapter 8 section 6
682  
683 sub action {
684 0 ▶ my $action = shift;
Subroutine action: not called CC=1 SCAR=6.9
Pod documented
685 return parser {
686 0 ▶ my $input = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
687 0 $action->($input);
688 0 return ( undef, $input );
689 0 };
690 }
691  
692 sub test {
693 0 ▶ my $action = shift;
Subroutine test: not called CC=1 SCAR=6.9
Pod documented
694 return parser {
695 0 ▶ my $input = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
696 0 my $result = $action->($input);
697 0 ▶ return $result ? ( undef, $input ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 0 0 0
698 0 };
699 }
700  
701 0 ▶sub debug { shift; @_ } # see Parser::Debug::debug
Subroutine debug: not called CC=1 SCAR=6.9
Pod documented
702  
703 6 my $error;
704  
705 sub fetch_error {
706 0 ▶ my ( $fail, $depth ) = @_;
Subroutine fetch_error: not called CC=12 SCAR=50.5
Pod documented
707  
708 # clear the error unless it's a recursive call
709 0 ▶ $error = '' if __PACKAGE__ ne caller;
Branch: 1 branch
BranchTrueFalseTotal
if __PACKAGE__ ne caller 0 0 0
710 0 ▶ $depth ||= 0;
Condition0%0/2
$depth ||= 0
l!l
00
MC/DC0%0/1
$depth ||= 0
$depth
Truth table0%0/2
A
A= $depth
Aresult
11
00
711 0 my $I = " " x $depth;
712 0 ▶ return unless 'ARRAY' eq ref $fail; # XXX ?
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $fail 0 0 0
713 0 my ( $type, $position, $data ) = @$fail;
714 0 my $pos_desc = "";
715  
716 0 ▶ while ( length($pos_desc) < 40 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $pos_desc < 40) 0 0 0
717 0 ▶ if ($position) {
Branch: 1 branch
BranchTrueFalseTotal
if ($position) { } 0 0 0
718 0 my $h = head($position);
719 0 $pos_desc .= "[@$h] ";
720 }
721 else {
722 0 $pos_desc .= "End of input ";
723 0 last;
724 }
725 0 $position = tail($position);
726 }
727 0 chop $pos_desc;
728 0 ▶ $pos_desc .= "..." if defined $position;
Branch: 1 branch
BranchTrueFalseTotal
if defined $position 0 0 0
729  
730 0 ▶ if ( $type eq 'TOKEN' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'TOKEN') { } 0 0 0
elsif ($type eq 'End of input') { } 0 0 0
elsif ($type eq 'ALT') { } 0 0 0
731 0 $error .= "${I}Wanted [@$data] instead of '$pos_desc'\n";
732 }
733 elsif ( $type eq 'End of input' ) {
734 0 $error .= "${I}Wanted EOI instead of '$pos_desc'\n";
735 }
736 elsif ( $type eq 'ALT' ) {
737 0 ▶ my $any = $depth ? "Or any" : "Any";
Branch: 1 branch
BranchTrueFalseTotal
$depth ? : 0 0 0
738 0 $error .= "${I}$any of the following:\n";
739 0 for (@$data) {
740 0 fetch_error( $_, $depth + 1 );
741 }
742 }
743 0 return $error;
744 }
745  
746  
747 =head1 AUTHOR
748  
749 Mark Jason Dominus. Maintained by Curtis "Ovid" Poe, C<< <ovid@cpan.org> >>
750  
751 =head1 BUGS
752  
753 Please report any bugs or feature requests to
754 C<bug-hop-parser@rt.cpan.org>, or through the web interface at
755 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HOP-Parser>.
756 I will be notified, and then you'll automatically be notified of progress on
757 your bug as I make changes.
758  
759 =head1 ACKNOWLEDGEMENTS
760  
761 Many thanks to Mark Dominus and Elsevier, Inc. for allowing this work to be
762 republished.
763  
764 =head1 COPYRIGHT & LICENSE
765  
766 Code derived from the book "Higher-Order Perl" by Mark Dominus, published by
767 Morgan Kaufmann Publishers, Copyright 2005 by Elsevier Inc.
768  
769 =head1 ABOUT THE SOFTWARE
770  
771 All Software (code listings) presented in the book can be found on the
772 companion website for the book (http://perl.plover.com/hop/) and is
773 subject to the License agreements below.
774  
775 =head1 LATEST VERSION
776  
777 You can download the latest versions of these modules at
778 L<http://github.com/Ovid/hop/>. Feel free to fork and make changes.
779  
780 =head1 ELSEVIER SOFTWARE LICENSE AGREEMENT
781  
782 Please read the following agreement carefully before using this Software. This
783 Software is licensed under the terms contained in this Software license
784 agreement ("agreement"). By using this Software product, you, an individual,
785 or entity including employees, agents and representatives ("you" or "your"),
786 acknowledge that you have read this agreement, that you understand it, and
787 that you agree to be bound by the terms and conditions of this agreement.
788 Elsevier inc. ("Elsevier") expressly does not agree to license this Software
789 product to you unless you assent to this agreement. If you do not agree with
790 any of the following terms, do not use the Software.
791  
792 =head1 LIMITED WARRANTY AND LIMITATION OF LIABILITY
793  
794 YOUR USE OF THIS SOFTWARE IS AT YOUR OWN RISK. NEITHER ELSEVIER NOR ITS
795 LICENSORS REPRESENT OR WARRANT THAT THE SOFTWARE PRODUCT WILL MEET YOUR
796 REQUIREMENTS OR THAT ITS OPERATION WILL BE UNINTERRUPTED OR ERROR-FREE. WE
797 EXCLUDE AND EXPRESSLY DISCLAIM ALL EXPRESS AND IMPLIED WARRANTIES NOT STATED
798 HEREIN, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
799 PARTICULAR PURPOSE. IN ADDITION, NEITHER ELSEVIER NOR ITS LICENSORS MAKE ANY
800 REPRESENTATIONS OR WARRANTIES, EITHER EXPRESS OR IMPLIED, REGARDING THE
801 PERFORMANCE OF YOUR NETWORK OR COMPUTER SYSTEM WHEN USED IN CONJUNCTION WITH
802 THE SOFTWARE PRODUCT. WE SHALL NOT BE LIABLE FOR ANY DAMAGE OR LOSS OF ANY
803 KIND ARISING OUT OF OR RESULTING FROM YOUR POSSESSION OR USE OF THE SOFTWARE
804 PRODUCT CAUSED BY ERRORS OR OMISSIONS, DATA LOSS OR CORRUPTION, ERRORS OR
805 OMISSIONS IN THE PROPRIETARY MATERIAL, REGARDLESS OF WHETHER SUCH LIABILITY IS
806 BASED IN TORT, CONTRACT OR OTHERWISE AND INCLUDING, BUT NOT LIMITED TO,
807 ACTUAL, SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES. IF THE
808 FOREGOING LIMITATION IS HELD TO BE UNENFORCEABLE, OUR MAXIMUM LIABILITY TO YOU
809 SHALL NOT EXCEED THE AMOUNT OF THE PURCHASE PRICE PAID BY YOU FOR THE SOFTWARE
810 PRODUCT. THE REMEDIES AVAILABLE TO YOU AGAINST US AND THE LICENSORS OF
811 MATERIALS INCLUDED IN THE SOFTWARE PRODUCT ARE EXCLUSIVE.
812  
813 YOU UNDERSTAND THAT ELSEVIER, ITS AFFILIATES, LICENSORS, SUPPLIERS AND AGENTS,
814 MAKE NO WARRANTIES, EXPRESSED OR IMPLIED, WITH RESPECT TO THE SOFTWARE
815 PRODUCT, INCLUDING, WITHOUT LIMITATION THE PROPRIETARY MATERIAL, AND
816 SPECIFICALLY DISCLAIM ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A
817 PARTICULAR PURPOSE.
818  
819 IN NO EVENT WILL ELSEVIER, ITS AFFILIATES, LICENSORS, SUPPLIERS OR AGENTS, BE
820 LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, WITHOUT LIMITATION, ANY LOST
821 PROFITS, LOST SAVINGS OR OTHER INCIDENTAL OR CONSEQUENTIAL DAMAGES, ARISING
822 OUT OF YOUR USE OR INABILITY TO USE THE SOFTWARE PRODUCT REGARDLESS OF WHETHER
823 SUCH DAMAGES ARE FORESEEABLE OR WHETHER SUCH DAMAGES ARE DEEMED TO RESULT FROM
824 THE FAILURE OR INADEQUACY OF ANY EXCLUSIVE OR OTHER REMEDY.
825  
826 =head1 SOFTWARE LICENSE AGREEMENT
827  
828 This Software License Agreement is a legal agreement between the Author and
829 any person or legal entity using or accepting any Software governed by this
830 Agreement. The Software is available on the companion website
831 (http://perl.plover.com/hop/) for the Book, Higher-Order Perl, which is
832 published by Morgan Kaufmann Publishers. "The Software" is comprised of all
833 code (fragments and pseudocode) presented in the book.
834  
835 By installing, copying, or otherwise using the Software, you agree to be bound
836 by the terms of this Agreement.
837  
838 The parties agree as follows:
839  
840 =over 4
841  
842 =item 1 Grant of License
843  
844 We grant you a nonexclusive license to use the Software for any purpose,
845 commercial or non-commercial, as long as the following credit is included
846 identifying the original source of the Software: "from Higher-Order Perl by
847 Mark Dominus, published by Morgan Kaufmann Publishers, Copyright 2005 by
848 Elsevier Inc".
849  
850 =item 2 Disclaimer of Warranty.
851  
852 We make no warranties at all. The Software is transferred to you on an "as is"
853 basis. You use the Software at your own peril. You assume all risk of loss for
854 all claims or controversies, now existing or hereafter, arising out of use of
855 the Software. We shall have no liability based on a claim that your use or
856 combination of the Software with products or data not supplied by us infringes
857 any patent, copyright, or proprietary right. All other warranties, expressed
858 or implied, including, without limitation, any warranty of merchantability or
859 fitness for a particular purpose are hereby excluded.
860  
861 =item 3 Limitation of Liability.
862  
863 We will have no liability for special, incidental, or consequential damages
864 even if advised of the possibility of such damages. We will not be liable for
865 any other damages or loss in any way connected with the Software.
866  
867 =back
868  
869 =cut
870  
871 6 1; # End of HOP::Parser
↑ Summary Build »