Perlito5/Emitter/Token.pm untested

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 44 Podpod 0.0% 0 / 43 totaltotal 0.0% 0 / 257 CC 18 SCAR 23.3
CC
18
Cov
0%
emit_perl5
42.0
emit_perl5
30.0
constant
20.0
CRAP 10.3
1 use v5;
2  
3  
4 package Perlito5::Rul;
5 sub new { my $class = shift; bless {@_}, $class }
6  
7 sub constant {
8 my $str = shift;
9 my $len = length $str;
10 if ( $len ) {
11 my @cond;
12 my $i = 0;
13 for my $char (split(//, $str)) {
14 if ($char eq '\\') {
15 $char = '\\\\';
16 }
17 if ($char eq '\'') {
18 $char = '\\\'';
19 }
20 push @cond, '(\'' . $char . '\' eq $str->[$MATCH->{to} + ' . $i . '])';
21 $i++;
22 }
23 return '('
24 . join ( ' && ',
25 @cond,
26 '($MATCH->{to} += ' . $len . ')'
27 )
28 . ')';
29 }
30 else {
31 return '1'
32 }
33 }
34  
35  
36  
37 package Perlito5::Rul::Quantifier;
38 sub new { my $class = shift; bless {@_}, $class }
39 sub term { $_[0]->{term} }
40 sub quant { $_[0]->{quant} }
41 sub emit_perl5 {
42 my $self = $_[0];
43  
44 if ($self->{quant} eq '') {
45 return $self->{term}->emit_perl5;
46 }
47 if ($self->{quant} eq '+') {
48 $self->{term}->set_captures_to_array;
49 return
50 '(do { '
51 . 'my $m = $MATCH; '
52 . 'my $to = $MATCH->{to}; '
53 . 'my $count = 0; '
54 . 'while (' . $self->{term}->emit_perl5() . ') '
55 . '{ '
56 . '$m = $MATCH; '
57 . '$to = $MATCH->{to}; '
58 . '$count = $count + 1; '
59 . '}; '
60 . '$MATCH = $m; '
61 . '$MATCH->{to} = $to; '
62 . '$count > 0; '
63 . '})';
64 }
65 if ($self->{quant} eq '*') {
66 $self->{term}->set_captures_to_array;
67 return
68 '(do { '
69 . 'my $m = $MATCH; '
70 . 'my $to = $MATCH->{to}; '
71 . 'while (' . $self->{term}->emit_perl5() . ') '
72 . '{ '
73 . '$m = $MATCH; '
74 . '$to = $MATCH->{to}; '
75 . '}; '
76 . '$MATCH = $m; '
77 . '$MATCH->{to} = $to; '
78 . '1 '
79 . '})';
80 }
81 if ($self->{quant} eq '?') {
82 $self->{term}->set_captures_to_array;
83 return
84 '(do { '
85 . 'my $m = $MATCH; '
86 . 'if (!' . $self->{term}->emit_perl5() . ') '
87 . '{ '
88 . '$MATCH = $m; '
89 . '}; '
90 . '1 '
91 . '})';
92 }
93  
94 die "Perlito5::Rul::Quantifier: not implemented";
95 }
96 sub set_captures_to_array {
97 my $self = $_[0];
98  
99 $self->{term}->set_captures_to_array;
100 }
101  
102  
103  
104 package Perlito5::Rul::Or;
105 sub new { my $class = shift; bless {@_}, $class }
106 sub or_list { $_[0]->{or_list} }
107 sub emit_perl5 {
108 my $self = $_[0];
109  
110 if ( scalar( @{$self->{or_list}} ) == 1 ) {
111 return $self->{or_list}[0]->emit_perl5;
112 }
113  
114 my $pos = '$' . Perlito5::get_label();
115  
116 '(do { '
117 . 'my ' . $pos . ' = $MATCH->{to}; ('
118 . join( ') || ($MATCH->{to} = ' . $pos . ', ',
119 map( $_->emit_perl5, @{$self->{or_list}} )
120 )
121 . ') })';
122 }
123 sub set_captures_to_array {
124 my $self = $_[0];
125  
126 map( $_->set_captures_to_array, @{$self->{or_list}} );
127 }
128  
129  
130  
131 package Perlito5::Rul::Concat;
132 sub new { my $class = shift; bless {@_}, $class }
133 sub concat { $_[0]->{concat} }
134 sub emit_perl5 {
135 my $self = $_[0];
136  
137 if ( scalar( @{$self->{concat}} ) == 1 ) {
138 return $self->{concat}[0]->emit_perl5;
139 }
140  
141 '('
142 . join( ' && ',
143 map( $_->emit_perl5, @{$self->{concat}} )
144 )
145 . ')';
146 }
147 sub set_captures_to_array {
148 my $self = $_[0];
149  
150 map( $_->set_captures_to_array, @{$self->{concat}} );
151 }
152  
153  
154  
155 package Perlito5::Rul::Subrule;
156 sub new { my $class = shift; bless {@_}, $class }
157 sub metasyntax { $_[0]->{metasyntax} }
158 sub captures { $_[0]->{captures} }
159 sub emit_perl5 {
160 my $self = $_[0];
161 my $meth = $self->{metasyntax};
162 my $code;
163 if ($self->{captures} == 1) {
164 $code = 'if ($m2) { $MATCH->{to} = $m2->{to}; $MATCH->{\'' . $self->{metasyntax} . '\'} = $m2; 1 } else { 0 }; '
165 }
166 elsif ($self->{captures} > 1) {
167 $code = 'if ($m2) { '
168 . '$MATCH->{to} = $m2->{to}; '
169 . 'push @{ $MATCH->{\'' . $self->{metasyntax} . '\'} }, $m2; '
170 . '1 '
171 . '} else { 0 }; '
172 }
173 else {
174 $code = 'if ($m2) { $MATCH->{to} = $m2->{to}; 1 } else { 0 }; '
175 }
176  
177 '(do { '
178 . 'my $m2 = ' . $meth . '($str, $MATCH->{to}); '
179 . $code
180 . '})'
181 }
182 sub set_captures_to_array {
183 my $self = $_[0];
184  
185 if ($self->{captures} > 0) {
186 $self->{captures} = $self->{captures} + 1;
187 }
188 }
189  
190  
191  
192 package Perlito5::Rul::Constant;
193 sub new { my $class = shift; bless {@_}, $class }
194 sub constant { $_[0]->{constant} }
195 sub emit_perl5 {
196 my $self = $_[0];
197  
198 my $str = $self->{constant};
199 Perlito5::Rul::constant( $str );
200 }
201 sub set_captures_to_array {
202 my $self = $_[0];
203 }
204  
205  
206  
207 package Perlito5::Rul::Dot;
208 sub new { my $class = shift; bless {@_}, $class }
209 sub emit_perl5 {
210 my $self = $_[0];
211  
212 '(\'\' ne $str->[$MATCH->{to}] ' .
213 '&& ++$MATCH->{to}' .
214 ')';
215 }
216 sub set_captures_to_array {
217 my $self = $_[0];
218 }
219  
220  
221  
222 package Perlito5::Rul::SpecialChar;
223 sub new { my $class = shift; bless {@_}, $class }
224 sub char { $_[0]->{char} }
225 sub emit_perl5 {
226 my $self = $_[0];
227  
228 my $char = $self->{char};
229 if ($char eq 'n') {
230 return Perlito5::Rul::Subrule->new( metasyntax => 'is_newline', captures => 0 )->emit_perl5;
231 }
232 if ($char eq 'N') {
233 return Perlito5::Rul::Subrule->new( metasyntax => 'not_newline', captures => 0 )->emit_perl5;
234 }
235 if ($char eq 'd') {
236 return Perlito5::Rul::Subrule->new( metasyntax => 'digit', captures => 0 )->emit_perl5;
237 }
238 if ($char eq 's') {
239 return Perlito5::Rul::Subrule->new( metasyntax => 'space', captures => 0 )->emit_perl5;
240 }
241 if ($char eq 't') {
242 return Perlito5::Rul::constant( chr(9) );
243 }
244 return Perlito5::Rul::constant( $char );
245 }
246 sub set_captures_to_array {
247 my $self = $_[0];
248 }
249  
250  
251  
252 package Perlito5::Rul::Block;
253 sub new { my $class = shift; bless {@_}, $class }
254 sub closure { $_[0]->{closure} }
255 sub emit_perl5 {
256 my $self = $_[0];
257  
258 '(do { '
259 . $self->{closure}
260 . '; 1 })'
261 }
262 sub set_captures_to_array {
263 my $self = $_[0];
264 }
265  
266  
267  
268 package Perlito5::Rul::Before;
269 sub new { my $class = shift; bless {@_}, $class }
270 sub rule_exp { $_[0]->{rule_exp} }
271 sub emit_perl5 {
272 my $self = $_[0];
273  
274 '(do { ' .
275 'my $tmp = $MATCH; ' .
276 '$MATCH = { \'from\' => $tmp->{to}, \'to\' => $tmp->{to} }; ' .
277 'my $res = ' .
278 $self->{rule_exp}->emit_perl5() .
279 '; ' .
280 '$MATCH = $tmp; ' .
281 '$res ? 1 : 0 ' .
282 '})'
283 }
284 sub set_captures_to_array {
285 my $self = $_[0];
286 }
287  
288  
289  
290 package Perlito5::Rul::NotBefore;
291 sub new { my $class = shift; bless {@_}, $class }
292 sub rule_exp { $_[0]->{rule_exp} }
293 sub emit_perl5 {
294 my $self = $_[0];
295  
296 '(do { ' .
297 'my $tmp = $MATCH; ' .
298 '$MATCH = { \'from\' => $tmp->{to}, \'to\' => $tmp->{to} }; ' .
299 'my $res = ' .
300 $self->{rule_exp}->emit_perl5() .
301 '; ' .
302 '$MATCH = $tmp; ' .
303 '$res ? 0 : 1 ' .
304 '})'
305 }
306 sub set_captures_to_array {
307 my $self = $_[0];
308 }
309  
310 1;
311  
312 =begin
313  
314 =head1 NAME
315  
316 Perlito5::Emitter::Token - Code generator for Perlito Perl 5 grammar
317  
318 =head1 SYNOPSIS
319  
320 my $match = $source.rule;
321 Perlito5::Match::flat($match)->emit_perl5; # generated Perlito source code
322  
323 =head1 DESCRIPTION
324  
325 This module generates Perl 5 code for the Regex6 compiler.
326  
327 =head1 AUTHORS
328  
329 Flavio Soibelmann Glock <fglock@gmail.com>.
330  
331 =head1 COPYRIGHT
332  
333 Copyright 2006, 2009, 2011, 2012 by Flavio Soibelmann Glock, Audrey Tang and others.
334  
335 This program is free software; you can redistribute it and/or modify it
336 under the same terms as Perl itself.
337  
338 See L<http://www.perl.com/perl/misc/Artistic.html>
339  
340 =end