| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Pegex::Regex; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 865 | use Pegex::Parser; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 22 |  | 
| 4 | 1 |  |  | 1 |  | 317 | use Pegex::Grammar; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 5 | 1 |  |  | 1 |  | 5 | use Pegex::Receiver; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 89 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | my @parsers; | 
| 8 |  |  |  |  |  |  | my $PASS = ''; | 
| 9 |  |  |  |  |  |  | my $FAIL = '(*FAIL)'; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | sub generate_regex { | 
| 12 | 0 |  |  | 0 | 0 | 0 | push @parsers, Pegex::Parser->new( | 
| 13 |  |  |  |  |  |  | grammar => Pegex::Grammar->new( text => shift ), | 
| 14 |  |  |  |  |  |  | receiver => Pegex::Receiver->new, | 
| 15 |  |  |  |  |  |  | throw_on_error => 0, | 
| 16 |  |  |  |  |  |  | ); | 
| 17 | 0 |  |  |  |  | 0 | my $index = $#parsers; | 
| 18 | 0 |  |  |  |  | 0 | my $regex = "(??{Pegex::Regex::parse($index, \$_)})"; | 
| 19 | 1 |  |  | 1 |  | 6 | use re 'eval'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 162 |  | 
| 20 | 0 |  |  |  |  | 0 | return qr{$regex}; | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub parse { | 
| 24 | 0 |  |  | 0 | 0 | 0 | my ($index, $input) = @_; | 
| 25 | 0 |  |  |  |  | 0 | undef %/; | 
| 26 | 0 | 0 |  |  |  | 0 | my $ast = $parsers[$index]->parse($input) or return $FAIL; | 
| 27 | 0 | 0 |  |  |  | 0 | %/ = %$ast if ref($ast) eq 'HASH'; | 
| 28 | 0 |  |  |  |  | 0 | return $PASS; | 
| 29 |  |  |  |  |  |  | }; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | # The following code was mutated from Damian Conway's Regexp::Grammars | 
| 32 |  |  |  |  |  |  | sub import { | 
| 33 |  |  |  |  |  |  | # Signal lexical scoping (active, unless something was exported)... | 
| 34 | 1 |  |  | 1 |  | 10 | $^H{'Pegex::Regex::active'} = 1; | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | # Process any regexes in module's active lexical scope... | 
| 37 | 1 |  |  | 1 |  | 6 | use overload; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 38 |  |  |  |  |  |  | overload::constant( | 
| 39 |  |  |  |  |  |  | qr => sub { | 
| 40 | 0 |  |  | 0 |  |  | my ($raw, $cooked, $type) = @_; | 
| 41 |  |  |  |  |  |  | # If active scope and really a regex... | 
| 42 | 0 | 0 | 0 |  |  |  | return generate_regex($raw) | 
| 43 |  |  |  |  |  |  | if _module_is_active() and $type =~ /qq?/; | 
| 44 |  |  |  |  |  |  | # Ignore everything else... | 
| 45 | 0 |  |  |  |  |  | return $cooked; | 
| 46 |  |  |  |  |  |  | } | 
| 47 | 1 |  |  |  |  | 6 | ); | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | # Deactivate module's regex effect when it is "anti-imported" with 'no'... | 
| 51 |  |  |  |  |  |  | sub unimport { | 
| 52 |  |  |  |  |  |  | # Signal lexical (non-)scoping... | 
| 53 | 0 |  |  | 0 |  |  | $^H{'Pegex::Regex::active'} = 0; | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | # Encapsulate the hoopy user-defined pragma interface... | 
| 57 |  |  |  |  |  |  | sub _module_is_active { | 
| 58 | 0 |  |  | 0 |  |  | return (caller 1)[10]->{'Pegex::Regex::active'}; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | 1; |