« blib/lib/Pugs/Emitter/Rule/Perl5/Ratchet.pm
↑ Summary
blib/lib/Pugs/Emitter/Rule/Perl6/Ratchet.pm »
| 1 | package Pugs::Emitter::Grammar::Perl5; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 15 | our $VERSION = '0.28'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | #use Smart::Comments; | ||||||||||||||||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use Pugs::Emitter::Rule::Perl5::Ratchet; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | # for safe mode | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub _prune_actions { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_prune_actions: not called CC=15 SCAR=54.8
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 0 | my ($ast) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 13 | 0 | ▶ | while (my ($key, $node) = each %$ast) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 0 | ▶ | next if $key =~ /^_/ or !ref $node; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$key =~ /^_/ or not ref $node
MC/DC0%0/2
$key =~ /^_/ or not ref $node
$key =~ /^_/
not ref $node
Truth table0%0/3
A or not(B)
A= $key =~ /^_/B= not ref $node
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | #warn $key; | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | if ($key eq 'closure') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | #die "Found closures!"; | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | next if ref $node ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | my $code = $node->{closure}; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | if ($code and !ref $code and $code =~ /\w+/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$code and not ref $code and $code =~ /\w+/
$code and not ref $code
MC/DC0%0/3
$code and not ref $code and $code =~ /\w+/
$code
not ref $code
$code =~ /\w+/
Truth table0%0/4
A and not(B) and C
A= $codeB= not ref $codeC= $code =~ /\w+/
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | die "ERROR: code blocks not allowed in safe mode: \"$code\"\n"; | |||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | ▶ | if (ref $node) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | my $ref = ref $node; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | if ($ref eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | _prune_actions($node); | |||||||||||||||||||||||||||||||||||||||||
| 28 | } elsif ($ref eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | for my $child (@$node) { | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | if (ref $child and ref $child eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $child and ref $child eq "HASH"
MC/DC0%0/2
ref $child and ref $child eq "HASH"
ref $child
ref $child eq "HASH"
Truth table0%0/3
A and B
A= ref $childB= ref $child eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | _prune_actions($child); | |||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub emit { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
emit: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my $ast = shift; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | my $opts = shift; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $opts ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts ||= {}
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | ## $ast | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | my ($name, $stmts) = each %$ast; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | my $p5_methods = ''; | |||||||||||||||||||||||||||||||||||||||||
| 46 | ### $name | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | for my $stmt (@$stmts) { | |||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | my $regex = $stmt->(); | |||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | my $type = $regex->{type}; | |||||||||||||||||||||||||||||||||||||||||
| 50 | ## $regex | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | if ($type eq 'block') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my $code = $regex->{value}; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | if ($opts->{safe_mode} && $code =~ /\w+/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opts->{"safe_mode"} and $code =~ /\w+/
MC/DC0%0/2
$opts->{"safe_mode"} and $code =~ /\w+/
$opts->{"safe_mode"}
$code =~ /\w+/
Truth table0%0/3
A and B
A= $opts->{"safe_mode"}B= $code =~ /\w+/
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | die "ERROR: verbatim Perl 5 blocks not allowed in safe mode: \"$code\"\n"; | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | $p5_methods .= <<"_EOC_"; | |||||||||||||||||||||||||||||||||||||||||
| 57 | # Code block from grammar spec | ||||||||||||||||||||||||||||||||||||||||||
| 58 | $code | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | _EOC_ | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | ### struct: $regex->{name} | ||||||||||||||||||||||||||||||||||||||||||
| 64 | ## regex AST: $regex->{ast} | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | my $params = {}; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if ($type eq 'rule') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | $params->{sigspace} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | my $body; | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my $ast = $regex->{ast}; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ($opts->{safe_mode}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | _prune_actions($ast); | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($type eq 'regex') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | $body = Pugs::Emitter::Rule::Perl5::emit( | |||||||||||||||||||||||||||||||||||||||||
| 78 | 'Pugs::Grammar::Rule', | ||||||||||||||||||||||||||||||||||||||||||
| 79 | $ast, | ||||||||||||||||||||||||||||||||||||||||||
| 80 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 81 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $body = Pugs::Emitter::Rule::Perl5::Ratchet::emit( | |||||||||||||||||||||||||||||||||||||||||
| 83 | 'Pugs::Grammar::Rule', | ||||||||||||||||||||||||||||||||||||||||||
| 84 | $ast, | ||||||||||||||||||||||||||||||||||||||||||
| 85 | $params, | ||||||||||||||||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | $body =~ s/^/ /gm; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $p5_methods .= <<_EOC_; | |||||||||||||||||||||||||||||||||||||||||
| 90 | # $regex->{type} $regex->{name} | ||||||||||||||||||||||||||||||||||||||||||
| 91 | *$regex->{name} = | ||||||||||||||||||||||||||||||||||||||||||
| 92 | $body; | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | _EOC_ | ||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | # bootstrap the regex parser itself: | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | my $prefix = $name eq 'Pugs::Grammar::Rule' ? | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | "#" : ''; | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | return <<"_EOC_"; | |||||||||||||||||||||||||||||||||||||||||
| 100 | package $name; | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | ${prefix}use base 'Pugs::Grammar::Base'; | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | use Pugs::Runtime::Match; | ||||||||||||||||||||||||||||||||||||||||||
| 105 | use Pugs::Runtime::Regex; | ||||||||||||||||||||||||||||||||||||||||||
| 106 | use Pugs::Runtime::Tracer (); | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | $p5_methods | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 111 | _EOC_ | ||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 15 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||||||||||||||||