Pegex/Grammar.pm

filter: Statementstmt 21.7% 17 / 78 Branchbran 10.0% 3 / 30 Conditioncond 40.0% 4 / 10 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 60.0% 3 / 5 Podpod 33.3% 1 / 3 totaltotal 22.1% 29 / 131 CC 20 SCAR 38.2
CC
20
Cov
20%
compile_into_module
72.0
import
52.2
make_text
12.0
CRAP 45.5
↑ Summary Pegex/Parser.pm »
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
BranchTrueFalseTotal
my $filename = $self->file or return "" 0 0 0
20 or return '';
21 0 ▶ open TEXT, "<", $filename
Branch: 1 branch
BranchTrueFalseTotal
open TEXT, "<", $filename or die "Can't open '${filename}' for input\n:$!" 0 0 0
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
BranchTrueFalseTotal
my $text = $self->text or die q[Can't create a '] . ref($self) . q[' grammar. No tree or text or file.] 85 0 85
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 || []
l!l
841
MC/DC100%1/1
$self->start_rules || []
$self->start_rules
Truth table100%2/2
A
A= $self->start_rules
Aresult
11
00
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
BranchTrueFalseTotal
if ((caller)[1] =~ /^-e?$/ and @_ == 2 and $_[1] eq "compile") 0 22 22
Condition33%2/6
(caller)[1] =~ /^-e?$/ and @_ == 2 and $_[1] eq "compile"
!ll&&!rl&&r
2200
(caller)[1] =~ /^-e?$/ and @_ == 2
!ll&&!rl&&r
2200
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"
ABCresult
0XX0
10X0
1100
1111
49 0 $package->compile_into_module();
50 0 exit;
51 }
52 22 ▶ if (my $env = $ENV{PERL_PEGEX_AUTO_COMPILE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $env = $ENV{"PERL_PEGEX_AUTO_COMPILE"}) 0 22 22
53 0 my %modules = map {($_, 1)} split ',', $env;
54 0 ▶ if ($modules{$package}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modules{$package}) 0 0 0
55 0 ▶ if (my $grammar_file = $package->file) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $grammar_file = $package->file) 0 0 0
56 0 ▶ if (-f $grammar_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $grammar_file) 0 0 0
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
BranchTrueFalseTotal
if (-M $grammar_file < -M $module_file) 0 0 0
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
BranchTrueFalseTotal
open GRAMMAR, "<", $grammar_file or die "Can't open $grammar_file for input" 0 0 0
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
BranchTrueFalseTotal
my $file = $INC{$module} or return 0 0 0
84 0 my $perl;
85 my @rules;
86 0 ▶ if ($package->can('start_rules')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package->can("start_rules")) 0 0 0
87 0 ▶ @rules = @{$package->start_rules || []};
Condition0%0/2
$package->start_rules || []
l!l
00
MC/DC0%0/1
$package->start_rules || []
$package->start_rules
Truth table0%0/2
A
A= $package->start_rules
Aresult
11
00
88 }
89 0 ▶ if ($module eq 'Pegex/Pegex/Grammar.pm') {
Branch: 1 branch
BranchTrueFalseTotal
if ($module eq 'Pegex/Pegex/Grammar.pm') { } 0 0 0
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
BranchTrueFalseTotal
open IN, "<", $file or die $! 0 0 0
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
BranchTrueFalseTotal
open OUT, ">", $file or die $! 0 0 0
111 0 print OUT $module_text;
112 0 close OUT;
113 0 print "Compiled '$grammar_file' into '$file'.\n";
114 }
115  
116 18 1;
↑ Summary Pegex/Parser.pm »