| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 57 | error "--to=perl|yaml|json required"; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 3 | ▶ | my $regex = $self->regex || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->regex or $to eq "perl"
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"
| |||||||||||||||||||||||||||||
| 60 | $to eq 'perl' ? 'perl' : 'raw'; | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | die "'$to' is an invalid --to= format" | ||||||||||||||||||||||||||||
| 63 | 3 | ▶ | unless $formats{$to}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | die "'$regex' is an invalid --regex= format" | ||||||||||||||||||||||||||||
| 65 | 3 | ▶ | unless $regexes{$regex}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | my $input = scalar(@$args) | ||||||||||||||||||||||||||||
| 68 | ? $self->slurp($args->[0]) | ||||||||||||||||||||||||||||
| 69 | 3 | ▶ | : do { local $/; <> }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 3 | ▶ | my $compiler_class = $self->boot | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 6 | ▶ | if (not @ARGV) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 1 | print usage; | |||||||||||||||||||||||||||
| 109 | 1 | exit 0; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 5 | my $command = shift @ARGV; | |||||||||||||||||||||||||||
| 113 | 5 | ▶ | $commands{$command} or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||