Pugs/Grammar/Perl6.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 75 CC 3 SCAR 14.6
CC
3
Cov
0%
perl6_expression
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 4.3
1 package Pugs::Grammar::Perl6;
2 use strict;
3 use warnings;
4 use base qw(Pugs::Grammar::BaseCategory);
5 use Pugs::Grammar::StatementControl;
6 use Pugs::Grammar::Expression;
7 use Pugs::Grammar::Pod;
8  
9 use Pugs::Compiler::Rule;
10 use Pugs::Runtime::Match;
11 use Pugs::Grammar::P6Rule; # our local version of Grammar::Rule.pm
12  
13 use Data::Dumper;
14  
15 # TODO - redefine <ws> to test Pod.pm after each \n
16 # *ws = &Pugs::Grammar::BaseCategory::ws;
17  
18 sub perl6_expression {
19 my $class = shift;
20 my $src = shift;
21 my $param = shift;
22  
23 #warn "perl6_expression param: ", Dumper @_;
24  
25 my ( $ast, $tail ) = Pugs::Grammar::Expression::ast( $src, $param );
26 if ( length( $tail ) ) {
27 $src = substr( $src, 0, - length( $tail ) );
28 }
29 return Pugs::Runtime::Match->new( {
30 bool => ( $ast ? 1 : 0 ),
31 match => $src,
32 tail => $tail,
33 capture => $ast,
34 } )
35 };
36  
37 *block = Pugs::Compiler::Regex->compile( q(
38 \{ : <?ws>? <statements_or_null> <?ws>? \}
39 { return {
40 bare_block => $_[0]{statements_or_null}->(),
41 } }
42 |
43 \-\> :
44 [
45 <?ws>? <perl6_expression('no_blocks',0)> <?ws>?
46 \{ <?ws>? <statements_or_null> <?ws>? \}
47 { return {
48 pointy_block => $_[0]{statements_or_null}->(),
49 signature => $_[0]{perl6_expression}->(),
50 } }
51 |
52 <?ws>?
53 \{ <?ws>? <statements_or_null> <?ws>? \}
54 { return {
55 pointy_block => $_[0]{statements_or_null}->(),
56 signature => undef,
57 } }
58 ]
59 ),
60 { grammar => __PACKAGE__ }
61 )->code;
62  
63  
64 *if = Pugs::Compiler::Regex->compile( q(
65 (if|unless) : <?ws>?
66 $<exp1> := <perl6_expression('no_blocks',0)> <?ws>?
67 $<exp2> := <block>
68 [
69 <?ws>? else <?ws>?
70 $<exp3> := <block>
71 { return {
72 statement => $_[0][0]->(),
73 exp1 => $_[0]{exp1}->(),
74 exp2 => $_[0]{exp2}->(),
75 exp3 => $_[0]{exp3}->(),
76 } }
77 |
78 <?ws>? elsif <?ws>?
79 $<exp3> := <perl6_expression('no_blocks',0)> <?ws>?
80 $<exp4> := <block>
81 [
82 <?ws>? else <?ws>?
83 $<exp5> := <block>
84 { return {
85 statement => $_[0][0]->(),
86 exp1 => $_[0]{exp1}->(),
87 exp2 => $_[0]{exp2}->(),
88 exp3 => $_[0]{exp3}->(),
89 exp4 => $_[0]{exp4}->(),
90 exp5 => $_[0]{exp5}->(),
91 } }
92
93 # TODO: elsif ...
94 |
95 { return {
96 statement => $_[0][0]->(),
97 exp1 => $_[0]{exp1}->(),
98 exp2 => $_[0]{exp2}->(),
99 exp3 => $_[0]{exp3}->(),
100 exp4 => $_[0]{exp4}->(),
101 } }
102 ]
103 |
104 { return {
105 statement => $_[0][0]->(),
106 exp1 => $_[0]{exp1}->(),
107 exp2 => $_[0]{exp2}->(),
108 } }
109 ]
110 ),
111 { grammar => __PACKAGE__ }
112 )->code;
113  
114  
115 *for = Pugs::Compiler::Regex->compile( q(
116 (for|while) : <?ws>?
117 $<exp1> := <perl6_expression('no_blocks',0)> <?ws>?
118 $<exp2> := <block>
119 { return {
120 statement => $_[0][0]->(),
121 exp1 => $_[0]{exp1}->(),
122 exp2 => $_[0]{exp2}->(),
123 } }
124 ),
125 { grammar => __PACKAGE__ }
126 )->code;
127  
128  
129 *try = Pugs::Compiler::Regex->compile( q(
130 (try) : <?ws>? <block>
131 { return {
132 fixity => 'prefix',
133 op1 => { op => 'try' },
134 exp1 => $_[0]{block}->(),
135 } }
136 ),
137 { grammar => __PACKAGE__ }
138 )->code;
139  
140  
141 *attribute = Pugs::Compiler::Regex->compile( q(
142 (<alnum>+) <?ws> (<alnum>+)
143 [
144 <?ws> <attribute>
145 { return [
146 [ { bareword => $_[0][0]->() }, { bareword => $_[0][1]->() } ],
147 @{$<attribute>->()},
148 ] }
149 |
150 { return [[ { bareword => $_[0][0]->() }, { bareword => $_[0][1]->() } ]] }
151 ]
152 |
153 (\\:<alnum>+)
154 [
155 <?ws>? <attribute>
156 { return [
157 [ { bareword => $_[0][0]->() }, { num => 1 } ],
158 @{$<attribute>->()},
159 ] }
160 |
161 { return [[ { bareword => $_[0][0]->() }, { num => 1 } ]] }
162 ]
163 |
164 { return [] }
165 ),
166 { grammar => __PACKAGE__ }
167 )->code;
168  
169  
170 # Str|Num
171 *signature_term_type = Pugs::Compiler::Regex->compile( q(
172 (<alnum>+)
173 [
174 <?ws>? \| <?ws>? <signature_term_type>
175 { return {
176 op1 => '|',
177 exp1 => $_[0][0]->(),
178 exp2 => $<signature_term_type>->(),
179 } }
180 |
181 { return $_[0][0]->() }
182 ]
183 |
184 { return undef }
185 ),
186 { grammar => __PACKAGE__ }
187 )->code;
188  
189  
190 *signature_term_ident = Pugs::Compiler::Regex->compile( q(
191 ( ( <'$'>|<'%'>|<'@'>|<'&'> ) <alpha> [<alnum>|_]* )
192 { return $_[0][0]->() }
193 ),
194 { grammar => __PACKAGE__ }
195 )->code;
196  
197  
198 *signature_term = Pugs::Compiler::Regex->compile( q(
199 <signature_term_type> <?ws>?
200 (<':'>?)
201 (<'*'>?)
202 <signature_term_ident>
203 (<'?'>?)
204 <?ws>? <attribute>
205 { return {
206 type => $_[0]{signature_term_type}->(),
207 name => $_[0]{signature_term_ident}->(),
208 attribute => $_[0]{attribute}->(),
209
210 named_only => $_[0][0]->(),
211 splat => $_[0][1]->(),
212 optional => $_[0][2]->(),
213 } }
214 #|
215 # { return undef }
216 ),
217 { grammar => __PACKAGE__ }
218 )->code;
219  
220  
221 *signature_no_invocant = Pugs::Compiler::Regex->compile( q(
222 <signature_term>
223 [
224 <?ws>? <','> <?ws>? <signature_no_invocant>
225 { return [
226 $_[0]{signature_term}->(),
227 @{$_[0]{signature_no_invocant}->()},
228 ] }
229 |
230 { return [ $_[0]{signature_term}->() ] }
231 ]
232 |
233 { return [] }
234 ),
235 { grammar => __PACKAGE__ }
236 )->code;
237  
238  
239 *signature = Pugs::Compiler::Regex->compile( q(
240 <signature_term> <?ws>? <':'>
241 [
242 <?ws>? <signature_no_invocant>
243 { return [
244 {
245 %{$_[0]{signature_term}->()},
246 invocant => 1,
247 },
248 @{$_[0]{signature_no_invocant}->()},
249 ] }
250 |
251 { return [
252 {
253 %{$_[0]{signature_term}->()},
254 invocant => 1,
255 },
256 ] }
257 ]
258 |
259 <signature_no_invocant>
260 { return $_[0]{signature_no_invocant}->()
261 }
262 ),
263 { grammar => __PACKAGE__ }
264 )->code;
265  
266 *sub_decl_name = Pugs::Compiler::Regex->compile( q(
267 ( multi | <''> ) <?ws>?
268 ( submethod | method | sub ) <?ws>?
269 ( <?Pugs::Grammar::Term.ident>? )
270 { return {
271 multi => $_[0][0]->(),
272 statement => $_[0][1]->(),
273 name => $_[0][2]->(),
274 } }
275 |
276 ( multi ) <?ws>?
277 ( <?Pugs::Grammar::Term.ident>? )
278 { return {
279 multi => $_[0][0]->(),
280 statement => 'sub',
281 name => $_[0][1]->(),
282 } }
283  
284 ),
285 { grammar => __PACKAGE__ }
286 )->code;
287  
288 *sub_signature = Pugs::Compiler::Regex->compile( q(
289 # (sig)
290 <'('> : <?ws>? <signature> <?ws>? <')'>
291 { return $_[0]{signature}->() }
292 |
293 { return [] }
294 ),
295 { grammar => __PACKAGE__ }
296 )->code;
297  
298  
299 *sub_decl = Pugs::Compiler::Regex->compile( q(
300 <sub_decl_name> <?ws>?
301 # (sig)
302 <sub_signature> <?ws>?
303 # attr
304 <attribute> <?ws>?
305 <block>
306 { return {
307 multi => $_[0]{sub_decl_name}->()->{multi},
308 statement => $_[0]{sub_decl_name}->()->{statement},
309 name => $_[0]{sub_decl_name}->()->{name},
310
311 attribute => $_[0]{attribute}->(),
312 signature => $_[0]{sub_signature}->(),
313 block => $_[0]{block}->(),
314 } }
315 ),
316 { grammar => __PACKAGE__ }
317 )->code;
318  
319  
320 *rule_decl_name = Pugs::Compiler::Regex->compile( q(
321 ( multi | <''> ) <?ws>?
322 ( rule | regex | token ) <?ws>?
323 ( <?Pugs::Grammar::Term.ident>? )
324 { return {
325 multi => $_[0][0]->(),
326 statement => $_[0][1]->(),
327 name => $_[0][2]->(),
328 } }
329 ),
330 { grammar => __PACKAGE__ }
331 )->code;
332  
333  
334 *rule_decl = Pugs::Compiler::Regex->compile( q(
335 <rule_decl_name> : <?ws>?
336 # (sig)
337 <sub_signature> <?ws>?
338 # attr
339 <attribute> <?ws>?
340 <'{'>
341 <?ws>?
342 [
343 <Pugs::Grammar::P6Rule.rule>
344 <?ws>?
345 <'}'>
346 { return {
347 multi => $_[0]{rule_decl_name}->()->{multi},
348 statement => $_[0]{rule_decl_name}->()->{statement},
349 name => $_[0]{rule_decl_name}->()->{name},
350
351 attribute => $_[0]{attribute}->(),
352 signature => $_[0]{sub_signature}->(),
353  
354 # pass the match tree to the emitter
355 block => $_[0]{'Pugs::Grammar::P6Rule.rule'}->(),
356 } }
357
358 |
359 # XXX better error messages
360 { return { die "invalid rule syntax" } }
361 ]
362 ),
363 { grammar => __PACKAGE__ }
364 )->code;
365  
366  
367 *begin_block = Pugs::Compiler::Regex->compile( q(
368 (
369 BEGIN
370 | CHECK
371 | INIT
372 | END
373 | FIRST
374 | ENTER
375 | LEAVE
376 | KEEP
377 | UNDO
378 | NEXT
379 | LAST
380 | PRE
381 | POST
382 | CATCH
383 | CONTROL
384 ) : <?ws>? <block>
385 { return {
386 trait => $_[0][0]->(),
387 %{ $_[0]{block}->() },
388 } }
389 ),
390 { grammar => __PACKAGE__ }
391 )->code;
392  
393  
394 *statement = Pugs::Compiler::Regex->compile( q(
395 <begin_block>
396 { return $_[0]{begin_block}->();
397 }
398 |
399 <rule_decl>
400 { return $_[0]{rule_decl}->();
401 }
402 |
403 <sub_decl>
404 { return $_[0]{sub_decl}->();
405 }
406 |
407 <block>
408 { return $_[0]{block}->();
409 }
410 |
411 <if>
412 { return $_[0]{if}->();
413 }
414 |
415 <for>
416 { return $_[0]{for}->();
417 }
418 |
419 <try> # this actually don't belong here
420 { return $_[0]{try}->();
421 }
422 |
423 <perl6_expression>
424 [
425 <?ws>? (if|unless|for) <?ws>?
426 $<exp1> := <perl6_expression>
427 { return {
428 statement => $_[0][0]->(),
429 exp2 => $_[0]{perl6_expression}->(),
430 exp1 => $_[0]{exp1}->(),
431 } }
432 |
433 { return $_[0]{perl6_expression}->();
434 }
435 ]
436  
437 ),
438 { grammar => __PACKAGE__ }
439 )->code;
440  
441 *statements = Pugs::Compiler::Regex->compile( q(
442 [ ; <?ws>? ]*
443 [
444 <statement> :
445 [
446 <?ws>? [ ; <?ws>? ]*
447 <statements>
448 { return {
449 statements => [
450 $_[0]{statement}->(),
451 @{ $_[0]{statements}->()->{statements} },
452 ]
453 }
454 }
455 |
456 { return {
457 statements => [ $_[0]{statement}->() ],
458 } }
459 ]
460 |
461 { return {
462 statements => [],
463 } }
464 ]
465 ),
466 { grammar => __PACKAGE__ }
467 )->code;
468  
469 *statements_or_null = Pugs::Compiler::Regex->compile( q(
470 <statements>
471 { return $_[0]{statements}->() }
472 |
473 { return {
474 statements => [],
475 } }
476 ),
477 { grammar => __PACKAGE__ }
478 )->code;
479  
480 *parse = Pugs::Compiler::Regex->compile( q(
481 <?ws>?
482 <statements_or_null>
483 <?ws>?
484 { return $_[0]{statements_or_null}->() }
485 ),
486 { grammar => __PACKAGE__ }
487 )->code;
488  
489 1;
490 __END__