blib/lib/HOP/Lexer.pm

filter: Statementstmt 100% 51 / 51 Branchbran 100% 20 / 20 Conditioncond 93.3% 14 / 15 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 97.2% 105 / 108 CC 16 SCAR 21.0
CC
16
Cov
99%
__ANON__
14.0
_tokens
2.0
make_lexer
2.0
CRAP 8.2
↑ Summary
1 package HOP::Lexer;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 our @EXPORT_OK = qw/ make_lexer string_lexer /;
8 3 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
9  
10 3 ▶use HOP::Stream 'node';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 HOP::Lexer - "Higher Order Perl" Lexer
15  
16 =head1 VERSION
17  
18 Version 0.032
19  
20 =cut
21  
22 3 our $VERSION = '0.032';
23  
24 =head1 SYNOPSIS
25  
26 use HOP::Lexer 'string_lexer';
27
28 my @input_tokens = (
29 [ 'VAR', qr/[[:alpha:]]+/ ],
30 [ 'NUM', qr/\d+/ ],
31 [ 'OP', qr/[+=]/ ],
32 [ 'SPACE', qr/\s*/, sub { () } ],
33 );
34
35 my $text = 'x = 3 + 4';
36 my $lexer = string_lexer( $text, @input_tokens );
37
38 my @tokens;
39 while ( my $token = $lexer->() ) {
40 push @tokens, $token;
41 }
42  
43 =head1 EXPORT
44  
45 Two functions may be exported, C<make_lexer> and C<string_lexer>.
46  
47 =head1 FUNCTIONS
48  
49 =head2 make_lexer
50  
51 my $lexer = make_lexer( $input_iterator, @tokens );
52  
53 The C<make_lexer> function expects an input data iterator as the first
54 argument and a series of tokens as subsequent arguments. It returns a stream
55 of lexed tokens. The output tokens are two element arrays:
56  
57 [ $label, $matched_text ]
58  
59 The iterator should be a subroutine reference that returns the next value
60 merely by calling the subroutine with no arguments. If you have a single
61 block of text in a scalar that you want lexed, see the C<string_lexer>
62 function.
63  
64 The input C<@tokens> array passed into C<make_lexer> is expected to be a list
65 of array references with two mandatory items and one optional one:
66  
67 [ $label, qr/$match/, &transform ]
68  
69 =over 4
70  
71 =item * C<$label>
72  
73 The C<$label> is the name used for the first item in an output token.
74  
75 =item * C<$match>
76  
77 The C<$match> is either an exact string or regular expression which matches
78 the text the label is to identify.
79  
80 =item * C<&transform>
81  
82 The C<&transform> subroutine reference is optional. If supplied, this will
83 take the matched text and should return a token matching an output token or
84 an empty list if the token is to be discarded. For example, to discard
85 whitespace (the label is actually irrelevant, but it helps to document the
86 code):
87  
88 [ 'WHITESPACE', /\s+/, sub {()} ]
89  
90 The two arguments supplied to the transformation subroutine are the label and
91 value. Thus, if we wish to force all non-negative integers to have a unary
92 plus, we might do something like this:
93  
94 [
95 'REVERSED INT', # the label
96 /[+-]?\d+/, # integers with an optional unary plus or minus
97 sub {
98 my ($label, $value) = @_;
99 $value = "+$value" unless $value =~ /^[-+]/;
100 [ $label, $value ]
101 }
102 ]
103  
104 =back
105  
106 For example, let's say we want to convert the string "x = 3 + 4" to the
107 following tokens:
108  
109 [ 'VAR', 'x' ]
110 [ 'OP', '=' ]
111 [ 'NUM', 3 ]
112 [ 'OP', '+' ]
113 [ 'NUM', 4 ]
114  
115 One way to do this would be with the following code:
116  
117 my $text = 'x = 3 + 4';
118 my @text = ($text);
119 my $iter = sub { shift @text };
120
121 my @input_tokens = (
122 [ 'VAR', qr/[[:alpha:]]+/ ],
123 [ 'NUM', qr/\d+/ ],
124 [ 'OP', qr/[+=]/ ],
125 [ 'SPACE', qr/\s*/, sub { () } ],
126 );
127
128 my $lexer = make_lexer( $iter, @input_tokens );
129
130 my @tokens;
131 while ( my $token = $lexer->() ) {
132 push @tokens, $token;
133 }
134  
135 C<@tokens> would contain the desired tokens.
136  
137 Note that the order in which the input tokens are passed in might cause input
138 to be lexed in different ways, thus the order is significant (C</\w+/> might
139 slurp up numbers before C</\b\d+\b/> can read them).
140  
141 =head2 string_lexer
142  
143 my $lexer = string_lexer( $string, @tokens );
144  
145 This function is identical to C<make_lexer>, but takes a string as the first
146 argument. This is merely syntactic sugar for the common case where we have
147 our data in a string but don't want to create an iterator. The following are
148 equivalent.
149  
150 my $lexer = string_lexer( $text, @input_tokens );
151  
152 Versus:
153  
154 my @text = ($text);
155 my $iter = sub { shift @text };
156 my $lexer = make_lexer( $iter, @input_tokens );
157
158 =cut
159  
160 1 ▶sub string_lexer {
Subroutine string_lexer: called CC=1 SCAR=0.0
Pod documented
161 1 my $text = shift;
162 1 my @text = $text;
163 1 ▶ return make_lexer( sub { shift @text }, @_ );
Subroutine __ANON__: called CC=1 SCAR=0.0
164 }
165  
166 2 ▶sub make_lexer {
Subroutine make_lexer: called CC=2 SCAR=6.9
Pod documented
167 2 my $lexer = shift;
168 2 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 8 2 10
169 8 my $args = shift;
170 8 $lexer = _tokens( $lexer, @$args );
171 }
172 2 return $lexer;
173 }
174  
175 8 ▶sub _tokens {
Subroutine _tokens: called CC=2 SCAR=6.9
176 8 my ( $input, $label, $pattern, $maketoken ) = @_;
177 8 ▶ $maketoken ||= sub { [ $_[0] => $_[1] ] };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$maketoken ||= sub { [$_[0], $_[1]]; }
l!l&&r!l&&!r
260
MC/DC0%0/2
$maketoken ||= sub { [$_[0], $_[1]]; }
$maketoken sub { [$_[0], $_[1]]; }
Truth table66%2/3
A ||= B
A= $maketokenB= sub { [$_[0], $_[1]]; }
ABresult
1X1
011
000
178 8 my @tokens;
179 8 my $buf = ""; # set to undef when input is exhausted
180 8 ▶ my $split = sub { split /($pattern)/ => $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
181  
182 51 ▶ return sub {
Subroutine __ANON__: called CC=14 SCAR=26.4
183 51 ▶ while ( 0 == @tokens && defined $buf ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == @tokens and defined $buf) 42 33 75
Condition100%3/3
0 == @tokens and defined $buf
!ll&&!rl&&r
25842
MC/DC100%2/2
0 == @tokens and defined $buf
0 == @tokens defined $buf
Truth table100%3/3
A and B
A= 0 == @tokensB= defined $buf
ABresult
0X0
100
111
184 42 my $i = $input->();
185 42 ▶ if ( ref $i ) { # input is a token
Branch: 1 branch
BranchTrueFalseTotal
if (ref $i) 18 24 42
186 18 my ( $sep, $tok ) = $split->($buf);
187 18 ▶ $tok = $maketoken->( $label, $tok ) if defined $tok;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tok 8 10 18
188 18 ▶ push @tokens => grep defined && $_ ne "" => $sep, $tok, $i;
Condition100%3/3
defined $_ && $_ ne ''
!ll&&!rl&&r
24822
MC/DC100%2/2
defined $_ && $_ ne ''
defined $_ $_ ne ''
Truth table100%3/3
A && B
A= defined $_B= $_ ne ''
ABresult
0X0
100
111
189 18 $buf = "";
190 18 last;
191 }
192 24 ▶ $buf .= $i if defined $i; # append new input to buffer
Branch: 1 branch
BranchTrueFalseTotal
if defined $i 16 8 24
193 24 my @newtoks = $split->($buf);
194 24 ▶ while ( @newtoks > 2 || @newtoks && !defined $i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@newtoks > 2 or @newtoks and not defined $i) 12 24 36
Condition100%6/6
@newtoks > 2 or @newtoks and not defined $i
l!l&&r!l&&!r
8424
@newtoks and not defined $i
!ll&&!rl&&r
8164
MC/DC100%3/3
@newtoks > 2 or @newtoks and not defined $i
@newtoks > 2 @newtoks not defined $i
Truth table100%4/4
A or B and not(C)
A= @newtoks > 2B= @newtoksC= not defined $i
ABCresult
1XX1
0111
00X0
0100
195  
196 # buffer contains complete separator plus combined token
197 # OR we've reached the end of input
198 12 push @tokens => shift @newtoks;
199 12 ▶ push @tokens => $maketoken->( $label, shift @newtoks )
Branch: 1 branch
BranchTrueFalseTotal
if @newtoks 10 2 12
200 if @newtoks;
201 }
202  
203 # reassemble remaining contents of buffer
204 24 $buf = join "" => @newtoks;
205 24 ▶ undef $buf unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 8 16 24
206 24 @tokens = grep $_ ne "" => @tokens;
207 }
208 51 ▶ $_[0] = '' unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 50 1 51
209 51 ▶ return 'peek' eq $_[0] ? $tokens[0] : shift @tokens;
Branch: 1 branch
BranchTrueFalseTotal
'peek' eq $_[0] ? : 1 50 51
210 8 };
211 }
212  
213 =head1 DEBUGGING
214  
215 The following caveats (or pitfalls, if you prefer), should be kept in mind
216 while lexing data.
217  
218 =over 4
219  
220 =item * Unlexed data
221  
222 The tokens returned by the lexer are array references. If any data cannot be
223 lexed, it will be returned as a string, unchanged.
224  
225 =item * Capturing parens
226  
227 Internally, L<Hop::Lexer> uses capturing parentheses to extract the data from
228 the provided regular expressions. If you need to group data in regular
229 expressions, use the non-capturing parentheses C<(?:...)>. Otherwise, your
230 code will break.
231  
232 =item * Precedence
233  
234 It's important to note that the order of the described tokens is important.
235 If you have keywords such as "while", "if", "unless", and so on, and any text
236 which matches C<qr/[[:word:]]+/> is considered a variable, the following fails:
237  
238 my @input_tokens = (
239 [ 'VAR', qr/[[:word:]]+/ ],
240 [ 'KEYWORD', qr/(?:while|if|unless)/ ],
241 );
242  
243 This is because the potential keywords will be matched as C<VAR>. To deal
244 with this, place the higher precedence tokens first:
245  
246 my @input_tokens = (
247 [ 'KEYWORD', qr/(?:while|if|unless)/ ],
248 [ 'VAR', qr/[[:word:]]+/ ],
249 );
250  
251 =back
252  
253 =head1 AUTHOR
254  
255 Mark Jason Dominus. Maintained by Curtis "Ovid" Poe, C<< <ovid@cpan.org> >>
256  
257 =head1 BUGS
258  
259 Please report any bugs or feature requests to
260 C<bug-hop-lexer@rt.cpan.org>, or through the web interface at
261 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=HOP-Lexer>.
262 I will be notified, and then you'll automatically be notified of progress on
263 your bug as I make changes.
264  
265 =head1 FURTHER READING
266  
267 See L<http://www.perl.com/pub/a/2006/01/05/parsing.html> for a detailed
268 article about using this module, along with a comprehensive example.
269  
270 This has now been included in the distribution as L<HOP::Lexer::Article>.
271  
272 =head1 ACKNOWLEDGEMENTS
273  
274 Many thanks to Mark Dominus and Elsevier, Inc. for allowing this work to be
275 republished.
276  
277 =head1 COPYRIGHT & LICENSE
278  
279 Code derived from the book "Higher-Order Perl" by Mark Dominus, published by
280 Morgan Kaufmann Publishers, Copyright 2005 by Elsevier Inc.
281  
282 =head1 ABOUT THE SOFTWARE
283  
284 All Software (code listings) presented in the book can be found on the
285 companion website for the book (http://perl.plover.com/hop/) and is
286 subject to the License agreements below.
287  
288 =head1 ELSEVIER SOFTWARE LICENSE AGREEMENT
289  
290 Please read the following agreement carefully before using this Software. This
291 Software is licensed under the terms contained in this Software license
292 agreement ("agreement"). By using this Software product, you, an individual,
293 or entity including employees, agents and representatives ("you" or "your"),
294 acknowledge that you have read this agreement, that you understand it, and
295 that you agree to be bound by the terms and conditions of this agreement.
296 Elsevier inc. ("Elsevier") expressly does not agree to license this Software
297 product to you unless you assent to this agreement. If you do not agree with
298 any of the following terms, do not use the Software.
299  
300 =head1 LIMITED WARRANTY AND LIMITATION OF LIABILITY
301  
302 YOUR USE OF THIS SOFTWARE IS AT YOUR OWN RISK. NEITHER ELSEVIER NOR ITS
303 LICENSORS REPRESENT OR WARRANT THAT THE SOFTWARE PRODUCT WILL MEET YOUR
304 REQUIREMENTS OR THAT ITS OPERATION WILL BE UNINTERRUPTED OR ERROR-FREE. WE
305 EXCLUDE AND EXPRESSLY DISCLAIM ALL EXPRESS AND IMPLIED WARRANTIES NOT STATED
306 HEREIN, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
307 PARTICULAR PURPOSE. IN ADDITION, NEITHER ELSEVIER NOR ITS LICENSORS MAKE ANY
308 REPRESENTATIONS OR WARRANTIES, EITHER EXPRESS OR IMPLIED, REGARDING THE
309 PERFORMANCE OF YOUR NETWORK OR COMPUTER SYSTEM WHEN USED IN CONJUNCTION WITH
310 THE SOFTWARE PRODUCT. WE SHALL NOT BE LIABLE FOR ANY DAMAGE OR LOSS OF ANY
311 KIND ARISING OUT OF OR RESULTING FROM YOUR POSSESSION OR USE OF THE SOFTWARE
312 PRODUCT CAUSED BY ERRORS OR OMISSIONS, DATA LOSS OR CORRUPTION, ERRORS OR
313 OMISSIONS IN THE PROPRIETARY MATERIAL, REGARDLESS OF WHETHER SUCH LIABILITY IS
314 BASED IN TORT, CONTRACT OR OTHERWISE AND INCLUDING, BUT NOT LIMITED TO,
315 ACTUAL, SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES. IF THE
316 FOREGOING LIMITATION IS HELD TO BE UNENFORCEABLE, OUR MAXIMUM LIABILITY TO YOU
317 SHALL NOT EXCEED THE AMOUNT OF THE PURCHASE PRICE PAID BY YOU FOR THE SOFTWARE
318 PRODUCT. THE REMEDIES AVAILABLE TO YOU AGAINST US AND THE LICENSORS OF
319 MATERIALS INCLUDED IN THE SOFTWARE PRODUCT ARE EXCLUSIVE.
320  
321 YOU UNDERSTAND THAT ELSEVIER, ITS AFFILIATES, LICENSORS, SUPPLIERS AND AGENTS,
322 MAKE NO WARRANTIES, EXPRESSED OR IMPLIED, WITH RESPECT TO THE SOFTWARE
323 PRODUCT, INCLUDING, WITHOUT LIMITATION THE PROPRIETARY MATERIAL, AND
324 SPECIFICALLY DISCLAIM ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A
325 PARTICULAR PURPOSE.
326  
327 IN NO EVENT WILL ELSEVIER, ITS AFFILIATES, LICENSORS, SUPPLIERS OR AGENTS, BE
328 LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, WITHOUT LIMITATION, ANY LOST
329 PROFITS, LOST SAVINGS OR OTHER INCIDENTAL OR CONSEQUENTIAL DAMAGES, ARISING
330 OUT OF YOUR USE OR INABILITY TO USE THE SOFTWARE PRODUCT REGARDLESS OF WHETHER
331 SUCH DAMAGES ARE FORESEEABLE OR WHETHER SUCH DAMAGES ARE DEEMED TO RESULT FROM
332 THE FAILURE OR INADEQUACY OF ANY EXCLUSIVE OR OTHER REMEDY.
333  
334 =head1 SOFTWARE LICENSE AGREEMENT
335  
336 This Software License Agreement is a legal agreement between the Author and
337 any person or legal entity using or accepting any Software governed by this
338 Agreement. The Software is available on the companion website
339 (http://perl.plover.com/hop/) for the Book, Higher-Order Perl, which is
340 published by Morgan Kaufmann Publishers. "The Software" is comprised of all
341 code (fragments and pseudocode) presented in the book.
342  
343 By installing, copying, or otherwise using the Software, you agree to be bound
344 by the terms of this Agreement.
345  
346 The parties agree as follows:
347  
348 =over 4
349  
350 =item 1 Grant of License
351  
352 We grant you a nonexclusive license to use the Software for any purpose,
353 commercial or non-commercial, as long as the following credit is included
354 identifying the original source of the Software: "from Higher-Order Perl by
355 Mark Dominus, published by Morgan Kaufmann Publishers, Copyright 2005 by
356 Elsevier Inc".
357  
358 =item 2 Disclaimer of Warranty.
359  
360 We make no warranties at all. The Software is transferred to you on an "as is"
361 basis. You use the Software at your own peril. You assume all risk of loss for
362 all claims or controversies, now existing or hereafter, arising out of use of
363 the Software. We shall have no liability based on a claim that your use or
364 combination of the Software with products or data not supplied by us infringes
365 any patent, copyright, or proprietary right. All other warranties, expressed
366 or implied, including, without limitation, any warranty of merchantability or
367 fitness for a particular purpose are hereby excluded.
368  
369 =item 3 Limitation of Liability.
370  
371 We will have no liability for special, incidental, or consequential damages
372 even if advised of the possibility of such damages. We will not be liable for
373 any other damages or loss in any way connected with the Software.
374  
375 =back
376  
377 =cut
378  
379 3 1; # End of HOP::Lexer
↑ Summary