Perl6/Rule/Parser.pm untested

filter: Statementstmt 0.0% 0 / 685 Branchbran 0.0% 0 / 260 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 984 CC 131 SCAR 97.1
CC
131
Cov
0%
init
17292.0
nextchar
2.0
match_scalar
2.0
CRAP 16415.0
1 package Perl6::Rule::Parser;
2  
3 use strict;
4 use warnings;
5  
6 use parent qw{Regexp::Parser};
7  
8 my ($nest_eval, $nest_logical);
9 $nest_eval = qr[ (?> [^\\{}]+ | \\. | { (??{ $nest_eval }) } )* ]x;
10 $nest_logical = qr[ (?> [^\\{}]+ | \\. | { (??{ $nest_logical }) } )* ]x;
11  
12  
13 our $non_nest_brack = qr{
14 \{ [^\}]* \} |
15 \< [^\>]* \> |
16 \( [^\)]* \) |
17 \[ [^\]]* \]
18 }x;
19  
20  
21 sub nextchar {
22 my ($self) = @_;
23  
24 ${&Rx} =~ m{ \G (?: \s+ | \# .* )+ }xgc;
25 }
26  
27  
28 sub match_scalar { }
29  
30  
31 sub match_array { }
32  
33  
34 sub match_hash { }
35  
36  
37 sub init {
38 my ($self) = @_;
39  
40 $self->add_handler('atom' => sub {
41 my ($S) = @_;
42 $S->nextchar;
43  
44 ${&Rx} =~ m{ \G (.) }xgcs or return;
45 my $c = $1;
46  
47 push @{ $S->{next} }, qw< atom >;
48 return $S->$c if $S->can($c);
49 return $S->object(exact => $c);
50 });
51  
52 ### BACKSLASHED THINGS
53  
54 $self->add_handler('\\' => sub {
55 my ($S, $cc) = @_;
56 my $c = '\\';
57  
58 if (${&Rx} =~ m{ \G (.) }xgcs) {
59 $c .= (my $n = $1);
60  
61 return $S->$c($cc) if $S->can($c);
62 --&RxPOS;
63  
64 $S->warn($S->RPe_BADESC, $c = $n, "") if $n =~ /[a-zA-Z]/;
65  
66 return $S->object(exact => $n, $c);
67 }
68  
69 $S->error($S->RPe_ESLASH);
70 });
71  
72 $self->add_handler('\b' => sub {
73 my ($S, $cc) = @_;
74 return $S->force_object(anyof_char => "\b", '\b') if $cc;
75 return $S->object(bound => bound => '\b');
76 });
77  
78 $self->add_handler('\B' => sub {
79 my ($S, $cc) = @_;
80 $S->warn($S->RPe_BADESC, "B", " in character class") if $cc;
81 return $S->force_object(anyof_char => 'B') if $cc;
82 return $S->object(bound => nbound => '\B');
83 });
84  
85 $self->add_handler('\c' => sub {
86 my ($S, $cc) = @_;
87  
88 if (${&Rx} =~ m{ \G ($non_nest_brack) }xgc) {
89 my $n = substr($1, 1, -1);
90 my @names = split /;/, $n;
91  
92 if ($cc) {
93 $S->error(0, "\\c[A;B] in character class") if @names > 1;
94 return $S->force_object(anyof_char => $S->nchar($n), "\\c[$n]");
95 }
96  
97 return $S->object(exact => join("", $S->nchar(@names)), "\\c[$n]");
98 }
99  
100 $S->error(0, "Missing right %s on \\%s", 'brace', "c$1") if ${&Rx} =~ m{ \G ([\[(\{<]) }xgc;
101 $S->error(0, "Missing brackets on \\%s", 'c');
102 });
103  
104 $self->add_handler('\C' => sub {
105 my ($S, $cc) = @_;
106  
107 if (${&Rx} =~ m{ \G ($non_nest_brack) }xgc) {
108 my $n = substr($1, 1, -1);
109 my @names = split /;/, $n;
110  
111 if ($cc) {
112 $S->error(0, "\\C[A;B] in character class") if @names > 1;
113 return $S->force_object(anyof_char_comp => $S->nchar($n), "\\C[$n]");
114 }
115  
116 return $S->object(exact_comp => join("", $S->nchar(@names)), "\\C[$n]");
117 }
118  
119 $S->error(0, "Missing right %s on \\%s", 'brace', "C$1") if ${&Rx} =~ m{ \G ([\[(\{<]) }xgc;
120 $S->error(0, "Missing brackets on \\%s", 'C');
121 });
122  
123 $self->add_handler('\d' => sub {
124 my ($S, $cc) = @_;
125 return $S->force_object(anyof_class => $S->force_object(digit => 0)) if $cc;
126 return $S->object(digit => 0);
127 });
128  
129 $self->add_handler('\D' => sub {
130 my ($S, $cc) = @_;
131 return $S->force_object(anyof_class => $S->force_object(digit => 1)) if $cc;
132 return $S->object(digit => 1);
133 });
134  
135 $self->add_handler('\e' => sub {
136 my ($S, $cc) = @_;
137 return $S->force_object(anyof_char => "\e", '\e') if $cc;
138 return $S->object(exact => "\e", '\e');
139 });
140  
141 $self->add_handler('\E' => sub {
142 my ($S, $cc) = @_;
143 return $S->force_object(anyof_char_comp => "\e", '\E') if $cc;
144 return $S->object(exact_comp => "\e", '\E');
145 });
146  
147 $self->add_handler('\f' => sub {
148 my ($S, $cc) = @_;
149 return $S->force_object(anyof_char => "\f", '\f') if $cc;
150 return $S->object(exact => "\f", '\f');
151 });
152  
153 $self->add_handler('\F' => sub {
154 my ($S, $cc) = @_;
155 return $S->force_object(anyof_char_comp => "\f", '\F') if $cc;
156 return $S->object(exact_comp => "\f", '\F');
157 });
158  
159 $self->add_handler('\h' => sub {
160 my ($S, $cc) = @_;
161 return $S->force_object(anyof_class => $S->force_object(horiz => 0)) if $cc;
162 return $S->object(horiz => 0);
163 });
164  
165 $self->add_handler('\H' => sub {
166 my ($S, $cc) = @_;
167 return $S->force_object(anyof_class => $S->force_object(horiz => 1)) if $cc;
168 return $S->object(horiz => 1);
169 });
170  
171 $self->add_handler('\n' => sub {
172 my ($S, $cc) = @_;
173 return $S->force_object(anyof_char => "\n", '\n') if $cc;
174 return $S->object(exact => "\n", '\n');
175 });
176  
177 $self->add_handler('\N' => sub {
178 my ($S, $cc) = @_;
179 return $S->force_object(anyof_char_comp => "\n", '\N') if $cc;
180 return $S->object(exact_comp => "\n", '\N');
181 });
182  
183 $self->add_handler('\r' => sub {
184 my ($S, $cc) = @_;
185 return $S->force_object(anyof_char => "\r", '\r') if $cc;
186 return $S->object(exact => "\r", '\r');
187 });
188  
189 $self->add_handler('\R' => sub {
190 my ($S, $cc) = @_;
191 return $S->force_object(anyof_char_comp => "\r", '\R') if $cc;
192 return $S->object(exact_comp => "\r", '\R');
193 });
194  
195 $self->add_handler('\s' => sub {
196 my ($S, $cc) = @_;
197 return $S->force_object(anyof_class => $S->force_object(space => 0)) if $cc;
198 return $S->object(space => 0);
199 });
200  
201 $self->add_handler('\S' => sub {
202 my ($S, $cc) = @_;
203 return $S->force_object(anyof_class => $S->force_object(space => 1)) if $cc;
204 return $S->object(space => 1);
205 });
206  
207 $self->add_handler('\t' => sub {
208 my ($S, $cc) = @_;
209 return $S->force_object(anyof_char => "\t", '\t') if $cc;
210 return $S->object(exact => "\t", '\t');
211 });
212  
213 $self->add_handler('\T' => sub {
214 my ($S, $cc) = @_;
215 return $S->force_object(anyof_char_comp => "\t", '\T') if $cc;
216 return $S->object(exact_comp => "\t", '\T');
217 });
218  
219 $self->add_handler('\v' => sub {
220 my ($S, $cc) = @_;
221 return $S->force_object(anyof_class => $S->force_object(vert => 0)) if $cc;
222 return $S->object(vert => 0);
223 });
224  
225 $self->add_handler('\V' => sub {
226 my ($S, $cc) = @_;
227 return $S->force_object(anyof_class => $S->force_object(vert => 1)) if $cc;
228 return $S->object(vert => 1);
229 });
230  
231 $self->add_handler('\w' => sub {
232 my ($S, $cc) = @_;
233 return $S->force_object(anyof_class => $S->force_object(alnum => 0)) if $cc;
234 return $S->object(alnum => 0);
235 });
236  
237 $self->add_handler('\W' => sub {
238 my ($S, $cc) = @_;
239 return $S->force_object(anyof_class => $S->force_object(alnum => 1)) if $cc;
240 return $S->object(alnum => 1);
241 });
242  
243 $self->add_handler('\x' => sub {
244 my ($S, $cc) = @_;
245  
246 if (${&Rx} =~ m{ \G ($non_nest_brack) }xgc) {
247 my $h = substr($1, 1, -1);
248 my @hex = split /;/, $h;
249  
250 $S->warn(0, "Illegal hexadecimal digit '%s' ignored", $1) if $h =~ /([^a-fA-F0-9;])/;
251  
252 if ($cc) {
253 $S->error(0, "\\x[A;B] in character class") if @hex > 1;
254 return $S->force_object(anyof_char => chr(hex $h), "\\x[$h]");
255 }
256  
257 return $S->object(exact => join("", map chr(hex), @hex), "\\x[$h]");
258 }
259  
260 $S->error(0, "Missing right %s on \\%s", 'brace', "x$1") if ${&Rx} =~ m{ \G ([\{\[(<]) }xgc;
261  
262 if (${&Rx} =~ m{ \G ( [a-fA-F0-9]+ ) }xgc) {
263 return $S->force_object(anyof_char => chr(hex $1), "\\x$1") if $cc;
264 return $S->object(exact => chr(hex $1), "\\x$1");
265 }
266  
267 $S->warn(0, "Illegal hexadecimal digit '%s' ignored", substr(${&Rx}, &RxPOS, 1));
268 return $S->force_object(anyof_char => "\0", "\\x[0]") if $cc;
269 return $S->object(exact => "\0", "\\x[0]");
270 });
271  
272 $self->add_handler('\X' => sub {
273 my ($S, $cc) = @_;
274  
275 if (${&Rx} =~ m{ \G ($non_nest_brack) }xgc) {
276 my $h = substr($1, 1, -1);
277 my @hex = split /;/, $h;
278  
279 $S->warn(0, "Illegal hexadecimal digit '%s' ignored", $1) if $h =~ /([^a-fA-F0-9;])/;
280  
281 if ($cc) {
282 $S->error(0, "\\X[A;B] in character class") if @hex > 1;
283 return $S->force_object(anyof_char_comp => chr(hex $h), "\\X[$h]");
284 }
285  
286 return $S->object(exact_comp => join("", map chr(hex), @hex), "\\X[$h]");
287 }
288  
289 $S->error(0, "Missing right %s on \\%s", 'brace', "X$1") if ${&Rx} =~ m{ \G ([\[(\{<]) }xgc;
290  
291 if (${&Rx} =~ m{ \G ( [a-fA-F0-9]+ ) }xgc) {
292 return $S->force_object(anyof_char_comp => chr(hex $1), "\\X$1") if $cc;
293 return $S->object(exact_comp => chr(hex $1), "\\X$1");
294 }
295  
296 $S->warn(0, "Illegal hexadecimal digit '%s' ignored", substr(${&Rx}, &RxPOS, 1));
297 return $S->force_object(anyof_char_comp => "\0", "\\X[0]") if $cc;
298 return $S->object(exact_comp => "\0", "\\X[0]");
299 });
300  
301 $self->add_handler('\0' => sub {
302 my ($S, $cc) = @_;
303  
304 if (${&Rx} =~ m{ \G ($non_nest_brack) }xgc) {
305 my $o = substr($1, 1, -1);
306 my @oct = split /;/, $o;
307  
308 $S->warn(0, "Illegal octal digit '%s' ignored", $1) if $o =~ /([^0-7;])/;
309  
310 if ($cc) {
311 $S->error(0, "\\0[A;B] in character class") if @oct > 1;
312 return $S->force_object(anyof_char => chr(oct $o), "\\0[$o]");
313 }
314  
315 return $S->object(exact => join("", map chr(oct), @oct), "\\0[$o]");
316 }
317  
318 $S->error(0, "Missing right %s on \\%s", 'brace', "0$1") if ${&Rx} =~ m{ \G ([\[(\{<]) }xgc;
319  
320 if (${&Rx} =~ m{ \G ( [0-7]+ ) }xgc) {
321 return $S->force_object(anyof_char => chr(oct $1), "\\0$1") if $cc;
322 return $S->object(exact => chr(oct $1), "\\0$1");
323 }
324  
325 $S->warn(0, "Illegal octal digit '%s' ignored", substr(${&Rx}, &RxPOS, 1));
326 return $S->force_object(anyof_char => "\0", "\\0[0]") if $cc;
327 return $S->object(exact => "\0", "\\0[0]");
328 });
329  
330  
331 ### ':'
332  
333 $self->add_flag('i' => sub { 0x01 });
334  
335 $self->add_handler(':' => sub {
336 my ($S) = @_;
337  
338 if (${&Rx} =~ m{ \G \: }xgc) {
339 my $n = '::';
340 return $S->$n;
341 }
342  
343 if (${&Rx} =~ m{ \G ([a-z]+) }xgc) {
344 }
345  
346 });
347  
348 $self->add_handler('::' => sub {
349 my ($S) = @_;
350  
351 if (${&Rx} =~ m{ \G \: }xgc) {
352 my $n = ':::';
353 return $S->$n;
354 }
355 });
356  
357 $self->add_handler(':::' => sub {
358 my ($S) = @_;
359 });
360  
361  
362 ### '#'
363  
364 # I don't think I need to handle this because the
365 # nextchar() method skips whitespace and comments
366  
367  
368 ### '$'
369  
370 $self->add_handler('$' => sub {
371 my ($S) = @_;
372  
373 if (${&Rx} =~ m{ \G \$ }xgc) {
374 my $n = '$$';
375 return $S->$n;
376 }
377  
378 if ($S->match_scalar) {
379 }
380  
381 return $S->object(eol => eos => '$');
382 });
383  
384 $self->add_handler('$$' => sub {
385 my ($S) = @_;
386  
387 if ($S->match_scalar) {
388 }
389  
390 return $S->object(eol => eol => '$$');
391 });
392  
393  
394 ### '@'
395  
396  
397 ### '%'
398  
399  
400 ### '^'
401  
402 $self->add_handler('^' => sub {
403 my ($S) = @_;
404  
405 if (${&Rx} =~ m{ \G \^ }xgc) {
406 my $n = '^^';
407 return $S->$n;
408 }
409  
410 return $S->object(bol => bos => '^');
411 });
412  
413 $self->add_handler('^^' => sub {
414 my ($S) = @_;
415 return $S->object(bol => bol => '^^');
416 });
417  
418  
419 ### '&'
420  
421  
422 ### '*'
423  
424 $self->add_handler('*' => sub {
425 my ($S) = @_;
426 push @{ $S->{next} }, qw< minmod >;
427 return $S->object(quant => 0, '');
428 });
429  
430  
431 ### '+'
432  
433 $self->add_handler('+' => sub {
434 my ($S) = @_;
435 push @{ $S->{next} }, qw< minmod >;
436 return $S->object(quant => 1, '');
437 });
438  
439  
440 ### '?'
441  
442 $self->add_handler('?' => sub {
443 my ($S) = @_;
444 push @{ $S->{next} }, qw< minmod >;
445 return $S->object(quant => 0, 1);
446 });
447  
448  
449 ### '('
450  
451 $self->add_handler('(' => sub {
452 my ($S) = @_;
453 $S->nextchar;
454  
455 push @{ $S->{next} }, qw< c) atom >;
456 &SIZE_ONLY ? ++$S->{maxpar} : ++$S->{nparen};
457 push @{ $S->{flags} }, &Rf;
458 return $S->object(open => $S->{nparen});
459 });
460  
461  
462 ### ')'
463  
464  
465 ### '{'
466  
467  
468 ### '}'
469  
470  
471 ### '['
472  
473  
474 ### ']'
475  
476  
477 ### '<'
478  
479  
480 ### '>'
481  
482  
483 ### '.'
484  
485 $self->add_handler('.' => sub {
486 my ($S) = @_;
487 return $S->object(reg_any => sany => '.');
488 });
489  
490  
491 ### '|'
492  
493 $self->add_handler('|' => sub {
494 my ($S) = @_;
495 return $S->object(branch =>);
496 });
497  
498  
499 return;
500  
501 # control character
502 $self->add_handler('\c' => sub {
503 my ($S, $cc) = @_;
504 ${&Rx} =~ m{ \G (.?) }xgc;
505 my $c = $1;
506 return $S->force_object(anyof_char => chr(64 ^ ord $c), "\\c$c") if $cc;
507 return $S->object(exact => chr(64 ^ ord $c), "\\c$c");
508 });
509  
510 # nprop (not a unicode property)
511 $self->add_handler('\P' => sub {
512 my ($S, $cc) = @_;
513 $S->error($self->RPe_EMPTYB, 'P') if ${&Rx} !~ m{ \G (.) }xgcs;
514  
515 my $name = $1;
516 if ($name eq '{') {
517 $S->error($self->RPe_RBRACE, 'P') if ${&Rx} !~ m{ \G ([^\}]*) \} }xgc;
518 $name = $1;
519 }
520  
521 return $S->force_object(anyof_class => $S->force_object(prop => $name, 1)) if $cc;
522 return $S->object(prop => $name, 1);
523 });
524  
525 # prop (a unicode property)
526 $self->add_handler('\p' => sub {
527 my ($S, $cc) = @_;
528 $S->error($self->RPe_EMPTYB, 'p') if ${&Rx} !~ m{ \G (.) }xgcs;
529  
530 my $name = $1;
531 if ($name eq '{') {
532 $S->error($self->RPe_RBRACE, 'p') if ${&Rx} !~ m{ \G ([^\}]*) \} }xgc;
533 $name = $1;
534 }
535  
536 return $S->force_object(anyof_class => $S->force_object(prop => $name, 0)) if $cc;
537 return $S->object(prop => $name, 0);
538 });
539  
540 # clump (a unicode clump)
541 $self->add_handler('\X' => sub {
542 my ($S, $cc) = @_;
543 $S->warn($S->RPe_BADESC, 'X', ' in character class') if $cc;
544 return $S->force_object(anyof_char => 'X') if $cc;
545 return $S->object(clump => '\X');
546 });
547  
548 # hex character
549 $self->add_handler('\x' => sub {
550 my ($S, $cc) = @_;
551 ${&Rx} =~ m{ \G ( \{ | .{0,2} ) }sxgc;
552 my $brace = 0;
553 my $num = $1;
554  
555 if ($num eq '{') {
556 $S->error($self->RPe_RBRACE, 'x') if ${&Rx} !~ m{ \G ( [^\}]* ) \} }xgc;
557 $num = $1;
558 $brace = 1;
559 }
560 else {
561 my $good = ($num =~ s/^([a-fA-F0-9]*)// and $1);
562 &RxPOS -= length $num;
563 $num = $good;
564 }
565  
566 my $rep = $brace ? "\\x{$num}" : sprintf("\\x%02s", $num);
567 return $S->force_object(anyof_char => chr hex $num, $rep) if $cc;
568 return $S->object(exact => chr hex $num, $rep);
569 });
570  
571 # alpha POSIX class
572 $self->add_handler('POSIX_alpha' => sub {
573 my ($S, $neg, $how) = @_;
574 return $S->force_object(anyof_class => alpha => $neg, \$how);
575 });
576  
577 # alnum POSIX class
578 $self->add_handler('POSIX_alnum' => sub {
579 my ($S, $neg, $how) = @_;
580 return $S->force_object(anyof_class => alnum => $neg, \$how);
581 });
582  
583 # ascii POSIX class
584 $self->add_handler('POSIX_ascii' => sub {
585 my ($S, $neg, $how) = @_;
586 return $S->force_object(anyof_class => ascii => $neg, \$how);
587 });
588  
589 # cntrl POSIX class
590 $self->add_handler('POSIX_cntrl' => sub {
591 my ($S, $neg, $how) = @_;
592 return $S->force_object(anyof_class => cntrl => $neg, \$how);
593 });
594  
595 # digit POSIX class
596 $self->add_handler('POSIX_digit' => sub {
597 my ($S, $neg, $how) = @_;
598 return $S->force_object(anyof_class => digit => $neg, \$how);
599 });
600  
601 # graph POSIX class
602 $self->add_handler('POSIX_graph' => sub {
603 my ($S, $neg, $how) = @_;
604 return $S->force_object(anyof_class => graph => $neg, \$how);
605 });
606  
607 # lower POSIX class
608 $self->add_handler('POSIX_lower' => sub {
609 my ($S, $neg, $how) = @_;
610 return $S->force_object(anyof_class => lower => $neg, \$how);
611 });
612  
613 # print POSIX class
614 $self->add_handler('POSIX_print' => sub {
615 my ($S, $neg, $how) = @_;
616 return $S->force_object(anyof_class => print => $neg, \$how);
617 });
618  
619 # punct POSIX class
620 $self->add_handler('POSIX_punct' => sub {
621 my ($S, $neg, $how) = @_;
622 return $S->force_object(anyof_class => punct => $neg, \$how);
623 });
624  
625 # space POSIX class
626 $self->add_handler('POSIX_space' => sub {
627 my ($S, $neg, $how) = @_;
628 return $S->force_object(anyof_class => space => $neg, \$how);
629 });
630  
631 # upper POSIX class
632 $self->add_handler('POSIX_upper' => sub {
633 my ($S, $neg, $how) = @_;
634 return $S->force_object(anyof_class => upper => $neg, \$how);
635 });
636  
637 # word POSIX class
638 $self->add_handler('POSIX_word' => sub {
639 my ($S, $neg, $how) = @_;
640 return $S->force_object(anyof_class => word => $neg, \$how);
641 });
642  
643 # xdigit POSIX class
644 $self->add_handler('POSIX_xdigit' => sub {
645 my ($S, $neg, $how) = @_;
646 return $S->force_object(anyof_class => xdigit => $neg, \$how);
647 });
648  
649 $self->add_handler('{' => sub {
650 my ($S) = @_;
651 if (${&Rx} =~ m{ \G (\d+) (,?) (\d*) \} }xgc) {
652 my ($min, $range, $max) = ($1, $2, $3);
653 $max = $min unless $range;
654 push @{ $S->{next} }, qw< minmod >;
655 $S->error($self->RPe_BCURLY) if length($max) and $min > $max;
656 return $S->object(quant => $min, $max);
657 }
658 return $S->object(exact => '{');
659 });
660  
661 $self->add_handler('minmod' => sub {
662 my ($S) = @_;
663 $S->nextchar;
664 return $S->object(minmod =>) if ${&Rx} =~ m{ \G \? }xgc;
665 return;
666 });
667  
668 # alternation branch
669  
670 # opening parenthesis (maybe capturing paren)
671  
672 # any character
673  
674 # backslash
675  
676 # start of char class (and possible negation)
677 $self->add_handler('[' => sub {
678 my ($S) = @_;
679 push @{ $S->{next} }, qw< cce] cc cc] >;
680 my $neg = ${&Rx} =~ m{ \G \^ }xgc;
681  
682 my $pos = &RxPOS;
683 if (${&Rx} =~ m{ \G ([:.=]) .*? \1 ] }xgc) {
684 $S->warn($S->RPe_OUTPOS, $1, $1);
685 &RxPOS = $pos;
686 }
687  
688 return $S->object(anyof => $neg);
689 });
690  
691 # char class ] at beginning
692 $self->add_handler('cc]' => sub {
693 my ($S) = @_;
694 return unless ${&Rx} =~ m{ \G ] }xgc;
695 return $S->object(anyof_char => "]");
696 });
697  
698 # start of char class range (or maybe just char)
699 $self->add_handler('cc' => sub {
700 my ($S) = @_;
701 return if ${&Rx} =~ m{ \G (?= ] | \z ) }xgc;
702 push @{ $S->{next} }, qw< cc >;
703 my ($lhs, $rhs, $before_range);
704 my $ret = \$lhs;
705  
706 {
707 if (${&Rx} =~ m{ \G ( \\ ) }xgcs) {
708 my $c = $1;
709 $$ret = $S->$c(1);
710 }
711 elsif (${&Rx} =~ m{ \G \[ ([.=:]) (\^?) (.*?) \1 \] }xgcs) {
712 my ($how, $neg, $name) = ($1, $2, $3);
713 my $posix = "POSIX_$name";
714 if ($S->can($posix)) { $$ret = $S->$posix($neg, $how) }
715 else { $S->error($S->RPe_BADPOS, "$how$neg$name$how") }
716 }
717 elsif (${&Rx} =~ m{ \G (.) }xgcs) {
718 $$ret = $S->force_object(anyof_char => $1);
719 }
720  
721 if ($ret == \$lhs) {
722 if (${&Rx} =~ m{ \G (?= - ) }xgc) {
723 if ($lhs->visual =~ /^(?:\[\[:.=]|\\[dDsSwWpP])/) {
724 $S->warn($S->RPe_FRANGE, $lhs->visual, "");
725 $ret = $lhs;
726 last;
727 }
728 $before_range = &RxPOS++;
729 $ret = \$rhs;
730 redo;
731 }
732 $ret = $lhs;
733 }
734 elsif ($ret == \$rhs) {
735 if ($rhs->visual =~ /^(?:\[[:.=]|\\[dDsSwWpP])/) {
736 $S->warn($S->RPe_FRANGE, $lhs->visual, $rhs->visual);
737 &RxPOS = $before_range;
738 $ret = $lhs;
739 }
740 elsif ($lhs->visual gt $rhs->visual) {
741 $S->error($S->RPe_IRANGE, $lhs->visual, $rhs->visual);
742 }
743 else {
744 $ret = $S->object(anyof_range => $lhs, $rhs);
745 }
746 }
747 }
748  
749 return if &SIZE_ONLY;
750 return $ret;
751 });
752  
753 # end of char class
754 $self->add_handler('cce]' => sub {
755 my ($S) = @_;
756 $S->error($S->RPe_LBRACK) if ${&Rx} !~ m{ \G ] }xgc;
757 return $S->object(anyof_close => "]");
758 });
759  
760 # closing paren coming from 'atom'
761 $self->add_handler(')' => sub {
762 my ($S) = @_;
763 pop @{ $S->{next} };
764 &RxPOS--;
765 return;
766 });
767  
768 # closing paren coming from an opening paren
769 $self->add_handler('c)' => sub {
770 my ($S) = @_;
771 $S->error($S->RPe_LPAREN) if ${&Rx} !~ m{ \G \) }xgc;
772 pop @{ $S->{flags} };
773 return $S->object(close =>);
774 });
775  
776 # some kind of assertion...
777 $self->add_handler('(?' => sub {
778 my ($S) = @_;
779 my $c = '(?';
780  
781 if (${&Rx} =~ m{ \G (.) }xgcs) {
782 my $n = "$c$1";
783 return $S->$n if $S->can($n);
784 &RxPOS--;
785 }
786 else {
787 $S->error($S->RPe_SEQINC);
788 }
789  
790 # flag assertion or non-capturing group
791 ${&Rx} =~ m{ \G ([a-zA-Z]*) (-? [a-zA-Z]*) }xgc;
792 my ($on, $off) = ($1, $2);
793 my ($r_on, $r_off) = ("", "");
794 my ($f_on, $f_off) = (0,0);
795  
796 &RxPOS -= length($on.$off);
797 my $old = &RxPOS;
798  
799 for (split //, $on) {
800 &RxPOS++;
801 if (my $f = $S->can("FLAG_$_")) {
802 my $v = $S->$f(1) and $r_on .= $_;
803 $f_on |= $v;
804 next;
805 }
806 my $bad = substr ${&Rx}, $old;
807 $S->error($S->RPe_NOTREC, &RxPOS - $old, $bad);
808 }
809  
810 &RxPOS++ if $off =~ s/^-//;
811  
812 for (split //, $off) {
813 &RxPOS++;
814 if (my $f = $S->can("FLAG_$_")) {
815 my $v = $S->$f(0) and $r_off .= $_;
816 $f_off |= $v;
817 next;
818 }
819 my $bad = substr ${&Rx}, $old;
820 $S->error($S->RPe_NOTREC, &RxPOS - $old, $bad);
821 }
822  
823 if (${&Rx} =~ m{ \G ([:)]) }xgc) {
824 my $type = $1 eq ':' ? 'group' : 'flags';
825 if ($type eq 'group') {
826 push @{ $S->{flags} }, &Rf;
827 push @{ $S->{next} }, qw< c) atom >;
828 }
829 &Rf |= $f_on;
830 &Rf &= ~$f_off;
831 return $S->object($type => $r_on, $r_off);
832 }
833  
834 &RxPOS++;
835 my $l = length($on.$off) + 2;
836 $S->error($S->RPe_NOTREC, $l, substr(${&Rx}, $old));
837 });
838  
839 # comment
840 $self->add_handler('(?#' => sub {
841 my ($S) = @_;
842 ${&Rx} =~ m{ \G [^)]* }xgc;
843 $S->error($S->RPe_NOTERM) unless ${&Rx} =~ m{ \G \) }xgc;
844 return;
845 });
846  
847 # not implemented (?$...)
848 $self->add_handler('(?$' => sub {
849 my ($S) = @_;
850 $S->error($S->RPe_NOTREC, 1, substr(${&Rx}, &RxPOS - 1));
851 });
852  
853 # not implemented (?@...)
854 $self->add_handler('(?@' => sub {
855 my ($S) = @_;
856 $S->error($S->RPe_NOTREC, 1, substr(${&Rx}, &RxPOS - 1));
857 });
858  
859 # look-ahead
860 $self->add_handler('(?=' => sub {
861 my ($S) = @_;
862 push @{ $S->{next} }, qw< c) atom >;
863 push @{ $S->{flags} }, &Rf;
864 return $S->object(ifmatch => 1);
865 });
866  
867 # look-ahead (neg)
868 $self->add_handler('(?!' => sub {
869 my ($S) = @_;
870 push @{ $S->{next} }, qw< c) atom >;
871 push @{ $S->{flags} }, &Rf;
872 return $S->object(unlessm => 1);
873 });
874  
875 # look-behind prefix
876 $self->add_handler('(?<' => sub {
877 my ($S) = @_;
878 my $c = '(?<';
879  
880 if (${&Rx} =~ m{ \G (.) }xgcs) {
881 my $n = "$c$1";
882 return $S->$n if $S->can($n);
883 }
884  
885 $S->error($S->RPe_NOTREC, 2, substr(${&Rx}, &RxPOS - 2));
886 });
887  
888 # look-behind
889 $self->add_handler('(?<=' => sub {
890 my ($S) = @_;
891 push @{ $S->{next} }, qw< c) atom >;
892 push @{ $S->{flags} }, &Rf;
893 return $S->object(ifmatch => -1);
894 });
895  
896 # look-behind (neg)
897 $self->add_handler('(?<!' => sub {
898 my ($S) = @_;
899 push @{ $S->{next} }, qw< c) atom >;
900 push @{ $S->{flags} }, &Rf;
901 return $S->object(unlessm => -1);
902 });
903  
904 # suspend
905 $self->add_handler('(?>' => sub {
906 my ($S) = @_;
907 push @{ $S->{next} }, qw< c) atom >;
908 push @{ $S->{flags} }, &Rf;
909 return $S->object(suspend =>);
910 });
911  
912 # eval
913 $self->add_handler('(?{' => sub {
914 my ($S) = @_;
915 if (${&Rx} =~ m{ \G ($nest_eval) \} \) }xgc) {
916 push @{ $S->{flags} }, &Rf;
917 return $S->object(eval => $1);
918 }
919 $S->error($S->RPe_NOTBAL);
920 });
921  
922 # logical prefix
923 $self->add_handler('(??' => sub {
924 my ($S) = @_;
925 my $c = '(??';
926  
927 if (${&Rx} =~ m{ \G (.) }xgcs) {
928 my $n = "$c$1";
929 return $S->$n if $S->can($n);
930 }
931  
932 $S->error($S->RPe_NOTREC, 2, substr(${&Rx}, &RxPOS - 2));
933 });
934  
935 # logical
936 $self->add_handler('(??{' => sub {
937 my ($S) = @_;
938 if (${&Rx} =~ m{ \G ($nest_logical) \} \) }xgc) {
939 push @{ $S->{flags} }, &Rf;
940 return $S->object(logical => $1);
941 }
942 $S->error($S->RPe_NOTBAL);
943 });
944  
945 # logical prefix
946 $self->add_handler('(?p' => sub {
947 my ($S) = @_;
948 my $c = '(?p';
949  
950 if (${&Rx} =~ m{ \G (.) }xgcs) {
951 my $n = "$c$1";
952 return $S->$n if $S->can($n);
953 }
954  
955 $S->error($S->RPe_NOTREC, 2, substr(${&Rx}, &RxPOS - 2));
956 });
957  
958 # logical
959 $self->add_handler('(?p{' => sub {
960 my ($S) = @_;
961 $S->warn($S->RPe_LOGDEP);
962 my $c = "(??{";
963 return $S->$c;
964 });
965  
966 $self->add_handler('(?(' => sub {
967 my ($S) = @_;
968 my $c = '(?(';
969  
970 if (${&Rx} =~ m{ \G (.) }xgcs) {
971 my $n = "$c$1";
972 return $S->$n if $S->can($n);
973 &RxPOS--;
974 }
975  
976 push @{ $S->{next} }, qw< ifthen( >;
977 push @{ $S->{flags} }, &Rf;
978 return $S->object(ifthen =>);
979 });
980  
981 # (?(...)t|f) condition
982 $self->add_handler('ifthen(' => sub {
983 my ($S) = @_;
984 my $c = 'ifthen(';
985  
986 push @{ $S->{next} }, qw< c) atom >;
987  
988 if (${&Rx} =~ m{ \G (.) }xgcs) {
989 my $n = "$c$1";
990 return $S->$n if $S->can($n);
991 &RxPOS--;
992 }
993  
994 if (${&Rx} =~ m{ \G ( [1-9]\d* ) }xgc) {
995 my $n = $1;
996 $S->error($S->RPe_SWNREC) if ${&Rx} !~ m{ \G \) }xgc;
997 push @{ $S->{next} }, qw< ifthen|2 ifthen| ifthen_atom >;
998 return $S->object(groupp => $n);
999 }
1000  
1001 $S->error($S->RPe_SWUNKN, &RxCUR);
1002 });
1003  
1004 # atom inside an ifthen
1005 $self->add_handler('ifthen_atom' => sub {
1006 my ($S) = @_;
1007 $S->nextchar;
1008 ${&Rx} =~ m{ \G ([^|]) }xgcs or return;
1009 my $c = $1;
1010  
1011 push @{ $S->{next} }, qw< ifthen_atom >;
1012 return $S->$c if $S->can($c);
1013 return $S->object(exact => $c);
1014 });
1015  
1016 # alternation branch inside ifthen
1017 $self->add_handler('ifthen|' => sub {
1018 my ($S) = @_;
1019 return if ${&Rx} !~ m{ \G \| }xgc;
1020 push @{ $S->{next} }, qw< ifthen_atom >;
1021 return $S->object(branch =>);
1022 });
1023  
1024 # illegal 2nd alternation branch inside ifthen
1025 $self->add_handler('ifthen|2' => sub {
1026 my ($S) = @_;
1027 return if ${&Rx} !~ m{ \G \| }xgc;
1028 $S->error($S->RPe_SWBRAN);
1029 });
1030  
1031 $self->add_handler('ifthen(?' => sub {
1032 my ($S) = @_;
1033 my $c = '(?';
1034  
1035 push @{ $S->{next} }, qw< ifthen|2 ifthen| ifthen_atom >;
1036  
1037 if (${&Rx} =~ m{ \G ( (?: <? [!=] | \{ ) ) }xgc) {
1038 my $n = "$c$1";
1039 return $S->$n if $S->can($n);
1040 &RxPOS -= length $1;
1041 }
1042  
1043 $S->error($S->RPe_SEQINC);
1044 });
1045 }
1046  
1047  
1048 1;
1049  
1050 __END__