blib/lib/Pegex/Cmd.pm

filter: Statementstmt 88.4% 61 / 69 Branchbran 58.8% 20 / 34 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 85.7% 12 / 14 Podpod 0.0% 0 / 8 totaltotal 73.8% 96 / 130 CC 19 SCAR 20.2
CC
19
Cov
78%
compile
14.4
slurp
6.0
error
6.0
CRAP 7.5
↑ Summary blib/lib/Pegex/Cmd/Mo.pm »
1 package Pegex::Cmd;
2 7 our $VERSION = '0.28';
3  
4 #-----------------------------------------------------------------------------#
5 package Pegex::Cmd;
6  
7 7 ▶use Pegex::Cmd::Mo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶use constant abstract =>
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 'Compile a Pegex grammar to some format.';
13 7 ▶use constant usage_desc =>
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 'pegex compile --to=<output format> [grammar_file.pgx]';
15  
16 # Output format. One of: yaml, json, perl, perl6, python.
17 7 has to => ();
18  
19 # Regex format: raw, perl.
20 7 has regex => ();
21  
22 # Use the bootstrap compiler
23 7 has boot => ();
24  
25 # Starting rules to combinate
26 has rules => (
27 3 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
28 $ENV{PEGEX_COMBINATE_RULES}
29 3 ▶ ? [ split / +/, $ENV{PEGEX_COMBINATE_RULES} ]
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'PEGEX_COMBINATE_RULES'} ? : 0 3 3
30 : []
31 },
32 7 );
33  
34 7 my %formats = map {($_,1)} qw'yaml json perl';
35 7 my %regexes = map {($_,1)} qw'perl raw';
36 7 my %commands = map {($_,1)} qw'compile help version';
37 sub usage;
38 sub error;
39  
40 6 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod undocumented
41 6 my ($self, @argv) = @_;
42  
43 6 my ($command, $args) = $self->getopt(@argv);
44 5 @ARGV = ();
45  
46 5 $self->$command($args);
47 }
48  
49 1 ▶sub help {
Subroutine help: called CC=1 SCAR=0.0
Pod undocumented
50 1 print usage;
51 }
52  
53 3 ▶sub compile {
Subroutine compile: called CC=13 SCAR=26.6
Pod undocumented
54 3 my ($self, $args) = @_;
55  
56 3 ▶ my $to = $self->to or
Branch: 1 branch
BranchTrueFalseTotal
my $to = $self->to or error("--to=perl|yaml|json required") 3 0 3
57 error "--to=perl|yaml|json required";
58  
59 3 ▶ my $regex = $self->regex ||
Branch: 1 branch
BranchTrueFalseTotal
$self->regex || $to eq 'perl' ? : 1 2 3
Condition66%2/3
$self->regex or $to eq "perl"
l!l&&r!l&&!r
012
MC/DC50%1/2
$self->regex or $to eq "perl"
$self->regex $to eq "perl"
Truth table66%2/3
A or B
A= $self->regexB= $to eq "perl"
ABresult
1X1
011
000
60 $to eq 'perl' ? 'perl' : 'raw';
61  
62 die "'$to' is an invalid --to= format"
63 3 ▶ unless $formats{$to};
Branch: 1 branch
BranchTrueFalseTotal
unless $formats{$to} 0 3 3
64 die "'$regex' is an invalid --regex= format"
65 3 ▶ unless $regexes{$regex};
Branch: 1 branch
BranchTrueFalseTotal
unless $regexes{$regex} 0 3 3
66  
67 my $input = scalar(@$args)
68 ? $self->slurp($args->[0])
69 3 ▶ : do { local $/; <> };
Branch: 1 branch
BranchTrueFalseTotal
scalar @$args ? : 0 3 3
70 3 ▶ my $compiler_class = $self->boot
Branch: 1 branch
BranchTrueFalseTotal
$self->boot ? : 0 3 3
71 ? 'Pegex::Bootstrap'
72 : 'Pegex::Compiler';
73  
74 3 ▶ eval "use $compiler_class; 1" or die $@;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "use $compiler_class; 1" or die $@ 3 0 3
75 3 my $compiler = $compiler_class->new();
76 3 $compiler->parse($input)->combinate(@{$self->rules});
77 3 ▶ $compiler->native if $regex eq 'perl';
Branch: 1 branch
BranchTrueFalseTotal
if $regex eq "perl" 1 2 3
78  
79 my $output =
80 $to eq 'perl' ? $compiler->to_perl :
81 $to eq 'yaml' ? $compiler->to_yaml :
82 $to eq 'json' ? $compiler->to_json :
83 0 ▶ do { die "'$to' format not supported yet" };
Branch: 3 branches
BranchTrueFalseTotal
$to eq 'perl' ? : 1 2 3
$to eq 'yaml' ? : 1 1 2
$to eq 'json' ? : 1 0 1
84 3 print STDOUT $output;
85 }
86  
87 1 ▶sub version {
Subroutine version: called CC=1 SCAR=0.0
Pod undocumented
88 1 require Pegex;
89  
90 1 print <<"...";
91 The 'pegex' compiler command v$VERSION
92  
93 Using the Perl Pegex module v$Pegex::VERSION
94 ...
95 }
96  
97 6 ▶sub getopt {
Subroutine getopt: called CC=4 SCAR=14.0
Pod undocumented
98 6 my ($self, @argv) = @_;
99  
100 6 local @ARGV = @argv;
101  
102 GetOptions(
103 "to=s" => \$self->{to},
104 "boot" => \$self->{boot},
105 6 ▶ ) or error;
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("to=s", \$self->{"to"}, "boot", \$self->{"boot"}) or error() 6 0 6
106  
107 6 ▶ if (not @ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@ARGV) 1 5 6
108 1 print usage;
109 1 exit 0;
110 }
111  
112 5 my $command = shift @ARGV;
113 5 ▶ $commands{$command} or
Branch: 1 branch
BranchTrueFalseTotal
$commands{$command} or error("Invalid command '${command}'") 5 0 5
114 error "Invalid command '$command'";
115  
116 5 return $command, [@ARGV];
117 }
118  
119 0 ▶sub slurp {
Subroutine slurp: not called CC=2 SCAR=17.9
Pod undocumented
120 0 my ($self, $file) = @_;
121 0 ▶ open my $fh, $file or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $file or die "Can't open '${file}' for input" 0 0 0
122 die "Can't open '$file' for input";
123 0 local $/;
124 0 <$fh>;
125 }
126  
127 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod undocumented
128 0 my $msg = usage;
129  
130 0 ▶ $msg = "Error: $_[0]\n\n$msg" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
131  
132 0 die $msg;
133 }
134  
135 2 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
136 2 <<'...';
137 pegex <command> [<options>] [<input-file>]
138  
139 Commands:
140  
141 compile: Compile a Pegex grammar to some format
142 version: Show Pegex version
143 help: Show help
144  
145 Options:
146 -t,--to=: Output type: yaml, json, perl
147 -b, --boot: Use the Pegex Bootstrap compiler
148 -r, --rules=: List of starting rules
149  
150 ...
151 }
152  
153 7 1;
↑ Summary blib/lib/Pegex/Cmd/Mo.pm »