line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HOP::Parser; |
2
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
66284
|
use warnings; |
|
5
|
|
|
|
|
7
|
|
|
5
|
|
|
|
|
148
|
|
4
|
5
|
|
|
5
|
|
16
|
use strict; |
|
5
|
|
|
|
|
5
|
|
|
5
|
|
|
|
|
101
|
|
5
|
|
|
|
|
|
|
|
6
|
5
|
|
|
5
|
|
14
|
use base 'Exporter'; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
325
|
|
7
|
5
|
|
|
5
|
|
1996
|
use HOP::Stream qw/drop tail head node is_node/; |
|
5
|
|
|
|
|
5390
|
|
|
5
|
|
|
|
|
1758
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our %N; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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. |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Please note that this module should be considered B code. While |
75
|
|
|
|
|
|
|
everything works fairly well, the documentation is incomplete and some of the |
76
|
|
|
|
|
|
|
functions could stand to be better named (C, 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 is a special purpose parser which is used internally. It always |
141
|
|
|
|
|
|
|
succeeds and returns I for C<$parsed> and the C<$remainder> is the |
142
|
|
|
|
|
|
|
unaltered input C<$stream>. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=cut |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub nothing { |
147
|
53
|
|
|
53
|
1
|
32
|
my $input = shift; |
148
|
53
|
|
|
|
|
93
|
return ( undef, $input ); |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
############################################################################## |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head2 End_of_Input |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
if (End_of_Input($stream)) { |
156
|
|
|
|
|
|
|
... |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
C 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 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
|
|
|
16
|
1
|
1644
|
my $input = shift; |
175
|
16
|
100
|
|
|
|
35
|
return ( undef, undef ) unless defined($input); |
176
|
2
|
|
|
|
|
14
|
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. |
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 subroutine is supplied, that C 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 is |
214
|
|
|
|
|
|
|
used instead. |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=cut |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
my $is_node = sub { is_node($_[0]) || ref $_[0] eq 'ARRAY' }; |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
sub lookfor { |
221
|
171
|
|
|
171
|
1
|
11533
|
my $wanted = shift; |
222
|
171
|
|
100
|
331
|
|
665
|
my $value = shift || sub { $_[0][1] }; |
|
331
|
|
|
|
|
318
|
|
223
|
171
|
|
|
|
|
125
|
my $param = shift; |
224
|
|
|
|
|
|
|
|
225
|
171
|
100
|
|
|
|
270
|
$wanted = [$wanted] unless ref $wanted; |
226
|
|
|
|
|
|
|
my $parser = parser { |
227
|
697
|
|
|
697
|
|
7238
|
my $input = shift; |
228
|
697
|
100
|
|
|
|
1041
|
$input = node(@$input) if ref($input) eq 'ARRAY'; |
229
|
697
|
100
|
|
|
|
922
|
unless ( defined $input ) { |
230
|
64
|
|
|
|
|
172
|
die [ 'TOKEN', $input, $wanted ]; |
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
633
|
|
|
|
|
806
|
my $next = head($input); |
234
|
633
|
|
|
|
|
2782
|
for my $i ( 0 .. $#$wanted ) { |
235
|
758
|
50
|
|
|
|
891
|
next unless defined $wanted->[$i]; |
236
|
5
|
|
|
5
|
|
24
|
no warnings 'uninitialized'; |
|
5
|
|
|
|
|
5
|
|
|
5
|
|
|
|
|
2507
|
|
237
|
758
|
100
|
|
|
|
1070
|
unless ($wanted->[$i] eq $next->[$i]) { |
238
|
281
|
|
|
|
|
740
|
die [ 'TOKEN', $input, $wanted ]; |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
} |
241
|
352
|
|
|
|
|
391
|
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
|
|
|
|
|
525
|
my $tail = tail($input); |
249
|
352
|
100
|
100
|
|
|
14361
|
$tail = [$tail] |
250
|
|
|
|
|
|
|
if $is_node->($tail) and not $is_node->($tail->[0]); |
251
|
352
|
|
|
|
|
1958
|
return ( $wanted_value, $tail ); |
252
|
171
|
|
|
|
|
495
|
}; |
253
|
171
|
|
|
|
|
527
|
$N{$parser} = "[@$wanted]"; |
254
|
171
|
|
|
|
|
412
|
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 with the arguments as an array |
266
|
|
|
|
|
|
|
reference. See C for more information. |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
=cut |
269
|
|
|
|
|
|
|
|
270
|
126
|
|
|
126
|
1
|
61638
|
sub match { @_ = [@_]; goto &lookfor } |
|
126
|
|
|
|
|
233
|
|
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
|
|
|
562
|
1
|
13686
|
sub parser (&) { $_[0] } |
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 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
|
100
|
|
4
|
1
|
2256
|
my $p = ref $_[0] eq 'CODE' ? shift : lookfor @_; |
303
|
|
|
|
|
|
|
parser { |
304
|
8
|
50
|
|
8
|
|
153
|
my $input = shift or return; |
305
|
8
|
|
|
|
|
13
|
$p->($input); |
306
|
4
|
|
|
|
|
7
|
return (undef, $input); |
307
|
|
|
|
|
|
|
}, |
308
|
4
|
|
|
|
|
15
|
} |
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. |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
=cut |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
sub neg_lookahead { |
325
|
4
|
100
|
|
4
|
1
|
2668
|
my $p = ref $_[0] eq 'CODE' ? shift : lookfor @_; |
326
|
|
|
|
|
|
|
parser { |
327
|
8
|
50
|
|
8
|
|
99
|
my $input = shift or return; |
328
|
8
|
|
|
|
|
11
|
my @ret = eval { $p->($input) }; |
|
8
|
|
|
|
|
15
|
|
329
|
8
|
100
|
|
|
|
33
|
die [ 'TOKEN', $input, $p ] if @ret; |
330
|
4
|
|
|
|
|
9
|
return (undef, $input); |
331
|
|
|
|
|
|
|
}, |
332
|
4
|
|
|
|
|
15
|
} |
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 succeed sequentially. |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
C 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
|
100
|
|
139
|
1
|
2766
|
shift unless ref $_[0]; |
353
|
139
|
|
|
|
|
145
|
my @parsers = @_; |
354
|
139
|
100
|
|
|
|
220
|
return \¬hing if @parsers == 0; |
355
|
137
|
100
|
|
|
|
185
|
return $parsers[0] if @parsers == 1; |
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
my $parser = parser { |
358
|
719
|
|
|
719
|
|
17683
|
my $input = shift; |
359
|
719
|
|
|
|
|
464
|
my ( $v, @values ); |
360
|
719
|
|
|
|
|
530
|
for (@parsers) { |
361
|
1146
|
|
|
|
|
1148
|
( $v, $input ) = $_->($input); |
362
|
798
|
100
|
|
|
|
1373
|
push @values, $v if defined $v; # assumes we wish to discard undef |
363
|
|
|
|
|
|
|
} |
364
|
371
|
|
|
|
|
505
|
return ( \@values, $input ); |
365
|
135
|
|
|
|
|
367
|
}; |
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 but matches one of any tokens |
376
|
|
|
|
|
|
|
(rather than all tokens sequentially). |
377
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
=cut |
379
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
sub alternate { |
381
|
77
|
|
|
77
|
1
|
864
|
my @parsers = @_; |
382
|
2
|
|
|
2
|
|
6
|
return parser { return () } |
383
|
77
|
100
|
|
|
|
123
|
if @parsers == 0; |
384
|
75
|
50
|
|
|
|
108
|
return $parsers[0] if @parsers == 1; |
385
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
my $parser = parser { |
387
|
499
|
|
|
499
|
|
554
|
my $input = shift; |
388
|
499
|
|
|
|
|
281
|
my @failures; |
389
|
|
|
|
|
|
|
|
390
|
499
|
|
|
|
|
417
|
for (@parsers) { |
391
|
818
|
|
|
|
|
571
|
my ( $v, $newinput ) = eval { $_->($input) }; |
|
818
|
|
|
|
|
831
|
|
392
|
818
|
100
|
|
|
|
897
|
if ($@) { |
393
|
403
|
50
|
|
|
|
535
|
die unless ref $@; # not a parser failure |
394
|
403
|
|
|
|
|
402
|
push @failures, $@; |
395
|
|
|
|
|
|
|
} |
396
|
|
|
|
|
|
|
else { |
397
|
415
|
|
|
|
|
651
|
return ( $v, $newinput ); |
398
|
|
|
|
|
|
|
} |
399
|
|
|
|
|
|
|
} |
400
|
84
|
|
|
|
|
180
|
die [ 'ALT', $input, \@failures ]; |
401
|
75
|
|
|
|
|
220
|
}; |
402
|
|
|
|
|
|
|
{ |
403
|
5
|
|
|
5
|
|
23
|
no warnings 'uninitialized'; |
|
5
|
|
|
|
|
5
|
|
|
5
|
|
|
|
|
5239
|
|
|
75
|
|
|
|
|
57
|
|
404
|
75
|
|
|
|
|
387
|
$N{$parser} = "(" . join ( " | ", map $N{$_}, @parsers ) . ")"; |
405
|
|
|
|
|
|
|
} |
406
|
75
|
|
|
|
|
188
|
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
|
|
|
12
|
1
|
15
|
my ( $element, $separator ) = @_; |
423
|
12
|
100
|
|
|
|
30
|
$separator = lookfor('COMMA') unless defined $separator; |
424
|
|
|
|
|
|
|
|
425
|
|
|
|
|
|
|
return T( |
426
|
|
|
|
|
|
|
concatenate( $element, star( concatenate( $separator, $element ) ) ), |
427
|
12
|
50
|
|
12
|
|
20
|
sub {[ $_[0], $_[1] ? map { @$_ } @{ $_[1] } : () ] }, |
|
16
|
|
|
|
|
37
|
|
|
12
|
|
|
|
|
23
|
|
428
|
12
|
|
|
|
|
19
|
); |
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, this |
440
|
|
|
|
|
|
|
parser expects a leading C<$separator> in what it matches. |
441
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
=cut |
443
|
|
|
|
|
|
|
|
444
|
|
|
|
|
|
|
sub rlist_of { |
445
|
6
|
|
|
6
|
1
|
8
|
my ( $element, $separator ) = @_; |
446
|
6
|
100
|
|
|
|
16
|
$separator = lookfor('COMMA') unless defined $separator; |
447
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
return T( concatenate( $separator, list_of( $element, $separator ) ), |
449
|
6
|
|
|
6
|
|
10
|
sub { [ $_[0], @{ $_[1] } ] } ); |
|
6
|
|
|
|
|
7
|
|
|
6
|
|
|
|
|
9
|
|
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
|
|
|
12
|
1
|
15
|
my ( $element, $separator ) = @_; |
466
|
12
|
100
|
|
|
|
23
|
$separator = lookfor('COMMA') unless defined $separator; |
467
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
return T( |
469
|
|
|
|
|
|
|
concatenate( |
470
|
|
|
|
|
|
|
$element, star( concatenate( absorb($separator), $element ) ) |
471
|
|
|
|
|
|
|
), |
472
|
|
|
|
|
|
|
sub { |
473
|
12
|
|
|
12
|
|
16
|
my @matches = shift; |
474
|
12
|
50
|
|
|
|
22
|
if ( my $tail = shift ) { |
475
|
12
|
|
|
|
|
13
|
foreach my $match (@$tail) { |
476
|
16
|
|
|
|
|
23
|
push @matches, grep defined $_, @$match; |
477
|
|
|
|
|
|
|
} |
478
|
|
|
|
|
|
|
} |
479
|
12
|
|
|
|
|
14
|
return \@matches; |
480
|
|
|
|
|
|
|
} |
481
|
12
|
|
|
|
|
20
|
); |
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, it expects a separator at the beginning of the list. |
495
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
=cut |
497
|
|
|
|
|
|
|
|
498
|
|
|
|
|
|
|
sub rlist_values_of { |
499
|
6
|
|
|
6
|
1
|
6
|
my ( $element, $separator ) = @_; |
500
|
6
|
100
|
|
|
|
18
|
$separator = lookfor('COMMA') unless defined $separator; |
501
|
|
|
|
|
|
|
|
502
|
|
|
|
|
|
|
return T( concatenate( $separator, list_values_of( $element, $separator ) ), |
503
|
6
|
|
|
6
|
|
11
|
sub { $_[1] } ); |
|
6
|
|
|
|
|
7
|
|
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
|
|
|
12
|
1
|
19
|
my $parser = shift; |
521
|
12
|
|
|
24
|
|
28
|
return T( $parser, sub { () } ); |
|
24
|
|
|
|
|
18
|
|
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
|
|
|
115
|
1
|
94
|
my ( $parser, $transform ) = @_; |
537
|
|
|
|
|
|
|
return parser { |
538
|
720
|
|
|
720
|
|
630
|
my $input = shift; |
539
|
720
|
50
|
|
|
|
715
|
if ( my ( $value, $newinput ) = $parser->($input) ) { |
540
|
394
|
|
|
|
|
517
|
local $^W; # using this to suppress 'uninitialized' warnings |
541
|
394
|
100
|
|
|
|
543
|
$value = [$value] if !ref $value; |
542
|
394
|
|
|
|
|
464
|
$value = $transform->(@$value); |
543
|
394
|
|
|
|
|
906
|
return ( $value, $newinput ); |
544
|
|
|
|
|
|
|
} |
545
|
|
|
|
|
|
|
else { |
546
|
0
|
|
|
|
|
0
|
return; |
547
|
|
|
|
|
|
|
} |
548
|
115
|
|
|
|
|
280
|
}; |
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
|
|
|
120
|
1
|
493
|
my $input = shift; |
564
|
120
|
|
|
|
|
155
|
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. 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
|
|
|
|
|
|
|
my ( $first, $rest ) = @_; |
583
|
|
|
|
|
|
|
[ $first, @$rest ]; |
584
|
|
|
|
|
|
|
}; |
585
|
|
|
|
|
|
|
|
586
|
|
|
|
|
|
|
sub star { |
587
|
47
|
|
|
47
|
1
|
42
|
my $p = shift; |
588
|
47
|
|
|
|
|
39
|
my $p_star; |
589
|
|
|
|
|
|
|
$p_star = alternate( |
590
|
|
|
|
|
|
|
T( |
591
|
47
|
|
|
119
|
|
118
|
concatenate( $p, parser { $p_star->(@_) } ), |
|
119
|
|
|
|
|
217
|
|
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
|
|
|
8
|
1
|
8
|
my $p = shift; |
614
|
8
|
|
|
|
|
11
|
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
|
|
|
5
|
1
|
8
|
my $parser = shift; |
634
|
5
|
|
|
|
|
12
|
return alternate ( |
635
|
|
|
|
|
|
|
$parser, |
636
|
|
|
|
|
|
|
\&null_list, |
637
|
|
|
|
|
|
|
); |
638
|
|
|
|
|
|
|
} |
639
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
## Chapter 8 section 4.4 |
641
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
sub operator { |
643
|
2
|
|
|
2
|
1
|
3
|
my ( $subpart, @ops ) = @_; |
644
|
2
|
|
|
|
|
5
|
my (@alternatives); |
645
|
2
|
|
|
|
|
2
|
for my $operator (@ops) { |
646
|
4
|
|
|
|
|
5
|
my ( $op, $opfunc ) = @$operator; |
647
|
|
|
|
|
|
|
push @alternatives, T( |
648
|
|
|
|
|
|
|
concatenate( $op, $subpart ), |
649
|
|
|
|
|
|
|
sub { |
650
|
24
|
|
|
24
|
|
19
|
my $subpart_value = $_[1]; |
651
|
24
|
|
|
|
|
28
|
sub { $opfunc->( $_[0], $subpart_value ) } |
652
|
24
|
|
|
|
|
49
|
} |
653
|
4
|
|
|
|
|
3
|
); |
654
|
|
|
|
|
|
|
} |
655
|
|
|
|
|
|
|
my $result = T( |
656
|
|
|
|
|
|
|
concatenate( $subpart, star( alternate(@alternatives) ) ), |
657
|
|
|
|
|
|
|
sub { |
658
|
62
|
|
|
62
|
|
51
|
my ( $total, $funcs ) = @_; |
659
|
62
|
|
|
|
|
58
|
for my $f (@$funcs) { |
660
|
24
|
|
|
|
|
26
|
$total = $f->($total); |
661
|
|
|
|
|
|
|
} |
662
|
62
|
|
|
|
|
88
|
$total; |
663
|
|
|
|
|
|
|
} |
664
|
2
|
|
|
|
|
13
|
); |
665
|
|
|
|
|
|
|
} |
666
|
|
|
|
|
|
|
|
667
|
|
|
|
|
|
|
## Chapter 8 section 4.7.1 |
668
|
|
|
|
|
|
|
|
669
|
|
|
|
|
|
|
sub error { |
670
|
1
|
|
|
1
|
1
|
1
|
my ($try) = @_; |
671
|
|
|
|
|
|
|
return parser { |
672
|
12
|
|
|
12
|
|
9
|
my $input = shift; |
673
|
12
|
|
|
|
|
7
|
my @result = eval { $try->($input) }; |
|
12
|
|
|
|
|
13
|
|
674
|
12
|
50
|
|
|
|
17
|
if ($@) { |
675
|
12
|
50
|
|
|
|
24
|
die ref $@ ? $@ : "Internal error ($@)"; |
676
|
|
|
|
|
|
|
} |
677
|
0
|
|
|
|
|
|
return @result; |
678
|
1
|
|
|
|
|
2
|
}; |
679
|
|
|
|
|
|
|
} |
680
|
|
|
|
|
|
|
|
681
|
|
|
|
|
|
|
## Chapter 8 section 6 |
682
|
|
|
|
|
|
|
|
683
|
|
|
|
|
|
|
sub action { |
684
|
0
|
|
|
0
|
1
|
|
my $action = shift; |
685
|
|
|
|
|
|
|
return parser { |
686
|
0
|
|
|
0
|
|
|
my $input = shift; |
687
|
0
|
|
|
|
|
|
$action->($input); |
688
|
0
|
|
|
|
|
|
return ( undef, $input ); |
689
|
0
|
|
|
|
|
|
}; |
690
|
|
|
|
|
|
|
} |
691
|
|
|
|
|
|
|
|
692
|
|
|
|
|
|
|
sub test { |
693
|
0
|
|
|
0
|
1
|
|
my $action = shift; |
694
|
|
|
|
|
|
|
return parser { |
695
|
0
|
|
|
0
|
|
|
my $input = shift; |
696
|
0
|
|
|
|
|
|
my $result = $action->($input); |
697
|
0
|
0
|
|
|
|
|
return $result ? ( undef, $input ) : (); |
698
|
0
|
|
|
|
|
|
}; |
699
|
|
|
|
|
|
|
} |
700
|
|
|
|
|
|
|
|
701
|
0
|
|
|
0
|
1
|
|
sub debug { shift; @_ } # see Parser::Debug::debug |
|
0
|
|
|
|
|
|
|
702
|
|
|
|
|
|
|
|
703
|
|
|
|
|
|
|
my $error; |
704
|
|
|
|
|
|
|
|
705
|
|
|
|
|
|
|
sub fetch_error { |
706
|
0
|
|
|
0
|
1
|
|
my ( $fail, $depth ) = @_; |
707
|
|
|
|
|
|
|
|
708
|
|
|
|
|
|
|
# clear the error unless it's a recursive call |
709
|
0
|
0
|
|
|
|
|
$error = '' if __PACKAGE__ ne caller; |
710
|
0
|
|
0
|
|
|
|
$depth ||= 0; |
711
|
0
|
|
|
|
|
|
my $I = " " x $depth; |
712
|
0
|
0
|
|
|
|
|
return unless 'ARRAY' eq ref $fail; # XXX ? |
713
|
0
|
|
|
|
|
|
my ( $type, $position, $data ) = @$fail; |
714
|
0
|
|
|
|
|
|
my $pos_desc = ""; |
715
|
|
|
|
|
|
|
|
716
|
0
|
|
|
|
|
|
while ( length($pos_desc) < 40 ) { |
717
|
0
|
0
|
|
|
|
|
if ($position) { |
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
|
0
|
|
|
|
|
$pos_desc .= "..." if defined $position; |
729
|
|
|
|
|
|
|
|
730
|
0
|
0
|
|
|
|
|
if ( $type eq 'TOKEN' ) { |
|
|
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
|
0
|
|
|
|
|
my $any = $depth ? "Or any" : "Any"; |
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<< >> |
750
|
|
|
|
|
|
|
|
751
|
|
|
|
|
|
|
=head1 BUGS |
752
|
|
|
|
|
|
|
|
753
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
754
|
|
|
|
|
|
|
C, or through the web interface at |
755
|
|
|
|
|
|
|
L. |
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. 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
|
|
|
|
|
|
|
1; # End of HOP::Parser |