Repa/Lexer.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 33 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 250 SCAR 74.5
CC
41
Cov
0%
recognize
1406.0
init
20.0
dump_buffer
6.0
CRAP 1722.0
1 use 5.010;
2 use strict;
3 use warnings;
4  
5 package MarpaX::Repa::Lexer;
6 our $VERSION = '0.01';
7  
8 =head1 NAME
9  
10 MarpaX::Repa::Lexer - simplify lexing for Marpa parser
11  
12 =head1 DESCRIPTION
13  
14 Most details are in L<MarpaX::Repa>.
15  
16 =head1 METHODS
17  
18 =head2 new
19  
20 Returns a new lexer instance. Takes named arguments.
21  
22 my $lexer = MyLexer->new(
23 tokens => {
24 word => qr{\b\w+\b},
25 },
26 store => 'array',
27 debug => 1,
28 );
29  
30 Possible arguments:
31  
32 =over 4
33  
34 =item tokens
35  
36 Hash with names of terminals as keys and one of the
37 following as value:
38  
39 =over 4
40  
41 =item string
42  
43 Just a string to match.
44  
45 'a token' => "matches this long string",
46  
47 =item regular expression
48  
49 A C<qr{}> compiled regexp.
50  
51 'a token' => qr{"[^"]+"},
52  
53 Note that regexp MUST match at least one character. At this moment
54 look behind to look at chars before the current position is not
55 supported.
56  
57 =item hash
58  
59 With hash you can define token specific options. At this moment
60 'store' option only (see below). Use C<match> key to set what to
61 match (string or regular expression):
62  
63 'a token' => {
64 match => "a string",
65 store => 'hash',
66 },
67  
68 Per token options are:
69  
70 =over 4
71  
72 =item store
73  
74 What to store (pass as value to Marpa's recognizer). The following variants
75 are supported:
76  
77 =over 4
78  
79 =item hash (default)
80  
81 { token => 'a token', value => 'a value' }
82  
83 =item array
84  
85 [ 'a token', 'a value' ]
86  
87 =item scalar
88  
89 'a value'
90  
91 =item undef
92  
93 undef is stored so later Repa's actions will skip it.
94  
95 =item a callback
96  
97 A function will be called with token name and reference to its value.
98 Should return a reference or undef that will be passed to recognizer.
99  
100 =back
101  
102 =item check
103  
104 A callback that can check whether token is really match or not.
105  
106 =back
107  
108 =back
109  
110 =item debug
111  
112 If true then lexer prints debug log to STDERR.
113  
114 =item min_buffer
115  
116 Minimal size of the buffer (4*1024 by default).
117  
118 =back
119  
120 =cut
121  
122 sub new {
123 my $proto = shift;
124 my $self = bless { @_ }, ref $proto || $proto;
125 return $self->init;
126 }
127  
128 =head2 init
129  
130 Setups instance and returns C<$self>. Called from constructor.
131  
132 =cut
133  
134 sub init {
135 my $self = shift;
136  
137 my $tokens = $self->{'tokens'};
138 foreach my $token ( keys %$tokens ) {
139 my ($match, @rest);
140 if ( ref( $tokens->{ $token } ) eq 'HASH' ) {
141 $match = $tokens->{ $token }{'match'};
142 @rest = (@{ $tokens->{ $token } }{'store','check'});
143 } else {
144 $match = $tokens->{ $token };
145 }
146 $rest[0] ||= $self->{'store'} || 'hash';
147 my $type =
148 ref $match ? 'RE'
149 : length $match == 1 ? 'CHAR'
150 : 'STRING';
151 $tokens->{ $token } = [ $type, $match, @rest ];
152 }
153  
154 $self->{'min_buffer'} //= 4*1024;
155 $self->{'buffer'} //= '';
156  
157 return $self;
158 }
159  
160 =head2 recognize
161  
162 Takes a recognizer and a file handle. Parses input. Dies on critical errors, but
163 not when parser lost its way. Returns recognizer that was passed.
164  
165 =cut
166  
167 sub recognize {
168 my $self = shift;
169 my $rec = shift;
170 my $fh = shift;
171  
172  
173 my $buffer = $self->buffer;
174 my $buffer_can_grow = $self->grow_buffer( $fh );
175  
176 my $expected = $rec->terminals_expected;
177 return $rec unless @$expected;
178  
179 while ( length $$buffer ) {
180 say STDERR "Expect token(s): ". join(', ', map "'$_'", @$expected)
181 if $self->{'debug'};
182  
183 say STDERR "Buffer start: ". $self->dump_buffer .'...'
184 if $self->{'debug'};
185  
186 my $longest = 0;
187 my @alternatives;
188 my $first_char = substr $$buffer, 0, 1;
189 foreach my $token ( @$expected ) {
190 REDO:
191  
192 my ($matched, $match, $length);
193 my ($type, $what, $how, $check) = @{ $self->{'tokens'}{ $token } || [] };
194  
195 unless ( $type ) {
196 say STDERR "Unknown token: '$token'" if $self->{'debug'};
197 next;
198 }
199 elsif ( $type eq 'RE' ) {
200 if ( $$buffer =~ /^($what)/ ) {
201 ($matched, $match, $length) = (1, $1, length $1);
202 if ( $length == length $$buffer && $buffer_can_grow ) {
203 $buffer_can_grow = $self->grow_buffer( $fh );
204 goto REDO;
205 }
206 }
207 }
208 elsif ( $type eq 'STRING' ) {
209 $length = length $what;
210 ($matched, $match) = (1, $what)
211 if $what eq substr $$buffer, 0, $length;
212 }
213 elsif ( $type eq 'CHAR' ) {
214 ($matched, $match, $length) = (1, $first_char, 1)
215 if $what eq $first_char;
216 }
217 else {
218 die "Unknown type $type";
219 }
220  
221 unless ( $matched ) {
222 say STDERR "No '$token' in ". $self->dump_buffer if $self->{'debug'};
223 next;
224 }
225  
226 unless ( $length ) {
227 die "Token '$token' matched empty string. This is not supported.";
228 }
229 say STDERR "Token '$token' matched ". $self->dump_buffer( $length )
230 if $self->{'debug'};
231  
232 if ( $check && !$check->( $self, $token, $match, $length ) ) {
233 say STDERR "\tCheck failed for '$token', skipping"
234 if $self->{'debug'};
235 next;
236 }
237  
238 if ( $self->{longest_expected} ) {
239 if ( $length > $longest ) {
240 say STDERR "New longest token of length $length" if $self->{'debug'};
241 @alternatives = (); $longest = $length;
242 } elsif ( $length < $longest ) {
243 say STDERR "Skipping $token token as it's short" if $self->{'debug'};
244 next;
245 }
246 }
247 push @alternatives, [$token, $how, $match, $length];
248 }
249  
250 foreach my $e ( @alternatives ) {
251 my ($token, $how, $match, $length) = @$e;
252 say STDERR "Accepting $token of length $length" if $self->{'debug'};
253  
254 if ( ref $how ) {
255 $match = $how->( $token, \"$match" );
256 } elsif ( $how eq 'hash' ) {
257 $match = \{ token => $token, value => $match };
258 } elsif ( $how eq 'array' ) {
259 $match = \[$token, $match];
260 } elsif ( $how eq 'scalar' ) {
261 $match = \"$match";
262 } elsif ( $how eq 'undef' ) {
263 $match = \undef;
264 } else {
265 die "Unknown store variant - '$how'";
266 }
267  
268 $rec->alternative( $token, $match, $length );
269 }
270  
271 my $skip = 0;
272 while (1) {
273 # XXX: we are done, no way to advance further, we would love this
274 # to be improved in Marpa
275 if ( $rec->current_earleme == $rec->thin->furthest_earleme ) {
276 return $rec;
277 }
278  
279 $skip++;
280 local $@;
281 if ( defined (my $events = eval { $rec->earleme_complete }) ) {
282 if ( $events && $rec->exhausted ) {
283 substr $$buffer, 0, $skip, '';
284 return $rec;
285 }
286 $expected = $rec->terminals_expected;
287 last if @$expected;
288 } else {
289 say STDERR "Failed to parse: $@" if $self->{'debug'};
290 return $rec;
291 }
292 }
293 substr $$buffer, 0, $skip, '';
294 $buffer_can_grow = $self->grow_buffer( $fh )
295 if $buffer_can_grow && $self->{'min_buffer'} > length $$buffer;
296  
297 say STDERR '' if $self->{'debug'};
298 }
299 return $rec;
300 }
301  
302 =head2 buffer
303  
304 Returns reference to the current buffer.
305  
306 =cut
307  
308 sub buffer { \$_[0]->{'buffer'} }
309  
310 =head2 grow_buffer
311  
312 Called when L</buffer> needs a re-fill with a file handle as argument.
313 Returns true if there is still data to come from the handle.
314  
315 =cut
316  
317 sub grow_buffer {
318 my $self = shift;
319 local $/ = \($self->{'min_buffer'}*2);
320 $self->{'buffer'} .= readline($_[0]) // return 0;
321 return 1 && $self->{'min_buffer'};
322 }
323  
324 =head2 dump_buffer
325  
326 Returns first 20 chars of the buffer with everything besides ASCII encoded
327 with C<\x{####}>. Use argument to control size, zero to mean whole buffer.
328  
329 =cut
330  
331 sub dump_buffer {
332 my $self = shift;
333 my $show = shift // 20;
334 my $str = $show? substr( $self->{'buffer'}, 0, $show ) : $self->{'buffer'};
335 (my $res = $str) =~ s/([^\x20-\x7E])/'\\x{'. hex( ord $1 ) .'}' /ge;
336 return $res;
337 }
338  
339 1;