| 1 | package Pegex::Grammar; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 18 | ▶ | use Pegex::Base; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | # Grammar can be in text or tree form. Tree will be compiled from text. | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # Grammar can also be stored in a file. | ||||||||||||||||||||||||||||||||||||||||||
| 6 | 18 | has file => (); | |||||||||||||||||||||||||||||||||||||||||
| 7 | 18 | has text => ( | |||||||||||||||||||||||||||||||||||||||||
| 8 | builder => 'make_text', | ||||||||||||||||||||||||||||||||||||||||||
| 9 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 10 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 18 | has tree => ( | |||||||||||||||||||||||||||||||||||||||||
| 12 | builder => 'make_tree', | ||||||||||||||||||||||||||||||||||||||||||
| 13 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 14 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 18 | has start_rules => []; | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub make_text { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
make_text: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | my $filename = $self->file | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | or return ''; | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | open TEXT, "<", $filename | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | or die "Can't open '$filename' for input\n:$!"; | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | return do {local $/; <TEXT>} | |||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 85 | ▶ | sub make_tree { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
make_tree: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 85 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 28 | # Turn off ENV debugging for grammar compile step: | ||||||||||||||||||||||||||||||||||||||||||
| 29 | local ( | ||||||||||||||||||||||||||||||||||||||||||
| 30 | $ENV{PERL_PEGEX_DEBUG}, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | $ENV{PERL_PEGEX_RECURSION_LIMIT}, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | $ENV{PERL_PEGEX_RECURSION_WARN_LIMIT}, | ||||||||||||||||||||||||||||||||||||||||||
| 33 | $ENV{PERL_PEGEX_ITERATION_LIMIT}, | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 85 | ); | |||||||||||||||||||||||||||||||||||||||||
| 35 | 85 | ▶ | my $text = $self->text | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | or die "Can't create a '" . ref($self) . | ||||||||||||||||||||||||||||||||||||||||||
| 37 | "' grammar. No tree or text or file."; | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 85 | require Pegex::Compiler; | |||||||||||||||||||||||||||||||||||||||||
| 39 | return Pegex::Compiler->new->compile( | ||||||||||||||||||||||||||||||||||||||||||
| 40 | $text, | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 85 | ▶ | @{$self->start_rules || []} | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->start_rules || []
MC/DC100%1/1
$self->start_rules || []
$self->start_rules
Truth table100%2/2
A
A= $self->start_rules
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | )->tree; | ||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | # This import is to support: perl -MPegex::Grammar::Module=compile | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 22 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=9 SCAR=39.5
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 22 | my ($package) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 22 | ▶ | if (((caller))[1] =~ /^-e?$/ and @_ == 2 and $_[1] eq 'compile') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
(caller)[1] =~ /^-e?$/ and @_ == 2 and $_[1] eq "compile"
(caller)[1] =~ /^-e?$/ and @_ == 2
MC/DC0%0/3
(caller)[1] =~ /^-e?$/ and @_ == 2 and $_[1] eq "compile"
(caller)[1] =~ /^-e?$/
@_ == 2
$_[1] eq "compile"
Truth table25%1/4
A and B and C
A= (caller)[1] =~ /^-e?$/B= @_ == 2C= $_[1] eq "compile"
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | $package->compile_into_module(); | |||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | exit; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 22 | ▶ | if (my $env = $ENV{PERL_PEGEX_AUTO_COMPILE}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my %modules = map {($_, 1)} split ',', $env; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ($modules{$package}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if (my $grammar_file = $package->file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if (-f $grammar_file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | my $module = $package; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | $module =~ s!::!/!g; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | $module .= '.pm'; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my $module_file = $INC{$module}; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if (-M $grammar_file < -M $module_file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | $package->compile_into_module(); | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | local $SIG{__WARN__}; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | delete $INC{$module}; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | require $module; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub compile_into_module { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
compile_into_module: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my ($package) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my $grammar_file = $package->file; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | open GRAMMAR, "<", $grammar_file | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | or die "Can't open $grammar_file for input"; | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | my $grammar_text = do {local $/; <GRAMMAR>}; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | close GRAMMAR; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $module = $package; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $module =~ s!::!/!g; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $module = "$module.pm"; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | my $file = $INC{$module} or return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my $perl; | |||||||||||||||||||||||||||||||||||||||||
| 85 | my @rules; | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | if ($package->can('start_rules')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | @rules = @{$package->start_rules || []}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$package->start_rules || []
MC/DC0%0/1
$package->start_rules || []
$package->start_rules
Truth table0%0/2
A
A= $package->start_rules
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ($module eq 'Pegex/Pegex/Grammar.pm') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | require Pegex::Bootstrap; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $perl = Pegex::Bootstrap->new->compile($grammar_text, @rules)->to_perl; | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | require Pegex::Compiler; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | $perl = Pegex::Compiler->new->compile($grammar_text, @rules)->to_perl; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | open IN, "<", $file or die $!; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | my $module_text = do {local $/; <IN>}; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | require Pegex; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | my $msg = " # Generated/Inlined by Pegex::Grammar ($Pegex::VERSION)"; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | close IN; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | $perl =~ s/^/ /gm; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $module_text =~ s/^(sub\s+make_tree\s*\{).*?(^\})/$1$msg\n$perl$2/ms; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | $module_text =~ s/^(sub\s+tree\s*\{).*?(^\})/$1$msg\n$perl$2/ms; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | chomp $grammar_text; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | $grammar_text = "<<'...';\n$grammar_text\n...\n"; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | $module_text =~ s/^(sub\s+text\s*\{).*?(^\})/$1$msg\n$grammar_text$2/ms; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | $grammar_text =~ s/^/# /gm; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | $module_text =~ s/^(# sub\s+text\s*\{).*?(^# \})/$1$msg\n$grammar_text$2/ms; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | open OUT, '>', $file or die $!; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | print OUT $module_text; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | close OUT; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | print "Compiled '$grammar_file' into '$file'.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 18 | 1; | |||||||||||||||||||||||||||||||||||||||||