Repa/Lexer.pm untested

filter: Statementstmt 0.0% 0 / 125 Branchbran 0.0% 0 / 86 Conditioncond 0.0% 0 / 45 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 271 SCAR 75.9
CC
44
Cov
0%
recognize
1640.0
init
20.0
dump_buffer
6.0
CRAP 1980.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 complete
111  
112 If true then parse should complete in one go and consume whole input.
113  
114 =item debug
115  
116 If true then lexer prints debug log to STDERR.
117  
118 =item min_buffer
119  
120 Minimal size of the buffer (4*1024 by default).
121  
122 =back
123  
124 =cut
125  
126 sub new {
127 my $proto = shift;
128 my $self = bless { @_ }, ref $proto || $proto;
129 return $self->init;
130 }
131  
132 =head2 init
133  
134 Setups instance and returns C<$self>. No need to call, it's called
135 from the constructor.
136  
137 =cut
138  
139 sub init {
140 my $self = shift;
141  
142 my $tokens = $self->{'tokens'};
143 foreach my $token ( keys %$tokens ) {
144 my ($match, @rest);
145 if ( ref( $tokens->{ $token } ) eq 'HASH' ) {
146 $match = $tokens->{ $token }{'match'};
147 @rest = (@{ $tokens->{ $token } }{'store','check'});
148 } else {
149 $match = $tokens->{ $token };
150 }
151 $rest[0] ||= $self->{'store'} || 'hash';
152 my $type =
153 ref $match ? 'RE'
154 : length $match == 1 ? 'CHAR'
155 : 'STRING';
156 $tokens->{ $token } = [ $type, $match, @rest ];
157 }
158  
159 $self->{'min_buffer'} //= 4*1024;
160 $self->{'buffer'} //= '';
161  
162 return $self;
163 }
164  
165 =head2 recognize
166  
167 Takes a recognizer and a file handle. Parses input. Dies on critical errors, but
168 not when parser lost its way. Returns recognizer that was passed.
169  
170 =cut
171  
172 sub recognize {
173 my $self = shift;
174 my $rec = shift;
175 my $fh = shift;
176  
177  
178 my $buffer = $self->buffer;
179 my $buffer_can_grow = $self->grow_buffer( $fh );
180  
181 my $expected = $rec->terminals_expected;
182 return $rec unless @$expected;
183  
184 while ( length $$buffer ) {
185 say STDERR "Expect token(s): ". join(', ', map "'$_'", @$expected)
186 if $self->{'debug'};
187  
188 say STDERR "Buffer start: ". $self->dump_buffer .'...'
189 if $self->{'debug'};
190  
191 my $longest = 0;
192 my @alternatives;
193 my $first_char = substr $$buffer, 0, 1;
194 foreach my $token ( @$expected ) {
195 REDO:
196  
197 my ($matched, $match, $length);
198 my ($type, $what, $how, $check) = @{ $self->{'tokens'}{ $token } || [] };
199  
200 unless ( $type ) {
201 say STDERR "Unknown token: '$token'" if $self->{'debug'};
202 next;
203 }
204 elsif ( $type eq 'RE' ) {
205 if ( $$buffer =~ /^($what)/ ) {
206 ($matched, $match, $length) = (1, $1, length $1);
207 if ( $length == length $$buffer && $buffer_can_grow ) {
208 $buffer_can_grow = $self->grow_buffer( $fh );
209 goto REDO;
210 }
211 }
212 }
213 elsif ( $type eq 'STRING' ) {
214 $length = length $what;
215 ($matched, $match) = (1, $what)
216 if $what eq substr $$buffer, 0, $length;
217 }
218 elsif ( $type eq 'CHAR' ) {
219 ($matched, $match, $length) = (1, $first_char, 1)
220 if $what eq $first_char;
221 }
222 else {
223 die "Unknown type $type";
224 }
225  
226 unless ( $matched ) {
227 say STDERR "No '$token' in ". $self->dump_buffer if $self->{'debug'};
228 next;
229 }
230  
231 unless ( $length ) {
232 die "Token '$token' matched empty string. This is not supported.";
233 }
234 say STDERR "Token '$token' matched ". $self->dump_buffer( $length )
235 if $self->{'debug'};
236  
237 if ( $check && !$check->( $self, $token, $match, $length ) ) {
238 say STDERR "\tCheck failed for '$token', skipping"
239 if $self->{'debug'};
240 next;
241 }
242  
243 if ( $self->{longest_expected} ) {
244 if ( $length > $longest ) {
245 say STDERR "New longest token of length $length" if $self->{'debug'};
246 @alternatives = (); $longest = $length;
247 } elsif ( $length < $longest ) {
248 say STDERR "Skipping $token token as it's short" if $self->{'debug'};
249 next;
250 }
251 }
252 push @alternatives, [$token, $how, $match, $length];
253 }
254  
255 foreach my $e ( @alternatives ) {
256 my ($token, $how, $match, $length) = @$e;
257 say STDERR "Accepting $token of length $length" if $self->{'debug'};
258  
259 if ( ref $how ) {
260 $match = $how->( $token, \"$match" );
261 } elsif ( $how eq 'hash' ) {
262 $match = \{ token => $token, value => $match };
263 } elsif ( $how eq 'array' ) {
264 $match = \[$token, $match];
265 } elsif ( $how eq 'scalar' ) {
266 $match = \"$match";
267 } elsif ( $how eq 'undef' ) {
268 $match = \undef;
269 } else {
270 die "Unknown store variant - '$how'";
271 }
272  
273 $rec->alternative( $token, $match, $length );
274 }
275  
276 my $skip = 0;
277 while (1) {
278 # XXX: we are done, no way to advance further, we would love this
279 # to be improved in Marpa
280 if ( $rec->current_earleme == $rec->thin->furthest_earleme ) {
281 die "Failed to parse: parser reached furthest earleme, but buffer is not empty - '". $self->dump_buffer . "'"
282 if $self->{complete} && (length $$buffer || $buffer_can_grow);
283 return $rec;
284 }
285  
286 $skip++;
287 local $@;
288 if ( defined (my $events = eval { $rec->earleme_complete }) ) {
289 if ( $events && $rec->exhausted ) {
290 substr $$buffer, 0, $skip, '';
291 die "Failed to parse: parser is exhausted, but buffer is not empty - '". $self->dump_buffer . "'"
292 if $self->{complete} && (length $$buffer || $buffer_can_grow);
293 return $rec;
294 }
295 $expected = $rec->terminals_expected;
296 last if @$expected;
297 } else {
298 say STDERR "Failed to parse: $@" if $self->{'debug'};
299 die "Failed to parse: $@" if $self->{complete};
300 return $rec;
301 }
302 }
303 substr $$buffer, 0, $skip, '';
304 $buffer_can_grow = $self->grow_buffer( $fh )
305 if $buffer_can_grow && $self->{'min_buffer'} > length $$buffer;
306  
307 say STDERR '' if $self->{'debug'};
308 }
309 return $rec;
310 }
311  
312 =head2 buffer
313  
314 Returns reference to the current buffer.
315  
316 =cut
317  
318 sub buffer { \$_[0]->{'buffer'} }
319  
320 =head2 grow_buffer
321  
322 Called when L</buffer> needs a re-fill with a file handle as argument.
323 Returns true if there is still data to come from the handle.
324  
325 =cut
326  
327 sub grow_buffer {
328 my $self = shift;
329 local $/ = \($self->{'min_buffer'}*2);
330 $self->{'buffer'} .= readline($_[0]) // return 0;
331 return 1 && $self->{'min_buffer'};
332 }
333  
334 =head2 dump_buffer
335  
336 Returns first 20 chars of the buffer with everything besides ASCII encoded
337 with C<\x{####}>. Use argument to control size, zero to mean whole buffer.
338  
339 =cut
340  
341 sub dump_buffer {
342 my $self = shift;
343 my $show = shift // 20;
344 my $str = $show? substr( $self->{'buffer'}, 0, $show ) : $self->{'buffer'};
345 (my $res = $str) =~ s/([^\x20-\x7E])/'\\x{'. hex( ord $1 ) .'}' /ge;
346 return $res;
347 }
348  
349 1;