File Coverage

blib/lib/Pegex/Cmd.pm
Criterion Covered Total %
statement 24 45 53.3
branch 0 20 0.0
condition n/a
subroutine 8 10 80.0
pod 1 2 50.0
total 33 77 42.8


line stmt bran cond sub pod time code
1             package Pegex::Cmd;
2             our $VERSION = '0.27';
3              
4             #-----------------------------------------------------------------------------#
5             package Pegex::Cmd::Command;
6              
7 1     1   1512 use App::Cmd::Setup -command;
  1         39559  
  1         7  
8 1     1   681 use Mouse;
  1         28147  
  1         3  
9             extends 'MouseX::App::Cmd::Command';
10              
11             #-----------------------------------------------------------------------------#
12             package Pegex::Cmd;
13              
14 1     1   310 use App::Cmd::Setup -app;
  1         2  
  1         5  
15 1     1   394 use Mouse;
  1         2  
  1         4  
16             extends 'MouseX::App::Cmd';
17              
18             use Module::Pluggable
19 1         7 require => 1,
20 1     1   807 search_path => [ 'Pegex::Cmd::Command' ];
  1         831  
21             Pegex::Cmd->plugins;
22              
23             #-----------------------------------------------------------------------------#
24             package Pegex::Cmd::Command::compile;
25              
26             Package->import( -command );
27 1     1   107 use Mouse;
  1         1  
  1         6  
28             extends 'Pegex::Cmd::Command';
29              
30 1         54 use constant abstract =>
31 1     1   405 'Compile a Pegex grammar to some format.';
  1         3  
32 1         579 use constant usage_desc =>
33 1     1   5 'pegex compile --to= [grammar_file.pgx]';
  1         2  
34              
35             has to => (
36             is => 'ro',
37             isa => 'Str',
38             required => 1,
39             documentation => 'Output format. One of: yaml, json, perl, perl6, python.',
40             );
41              
42             has regex => (
43             is => 'ro',
44             isa => 'Str',
45             lazy => 1,
46             default => sub {
47             my $self = shift;
48             $self->to eq 'perl' ? 'perl' : 'raw';
49             },
50             documentation => "Regex format: raw, perl.",
51             );
52              
53             has boot => (
54             is => 'ro',
55             isa => 'Bool',
56             default => sub { 0 },
57             documentation => 'Use the bootstrap compiler',
58             );
59              
60             has rules => (
61             is => 'ro',
62             default => sub {
63             $ENV{PEGEX_COMBINATE_RULES}
64             ? [ split / +/, $ENV{PEGEX_COMBINATE_RULES} ]
65             : []
66             },
67             documentation => 'Starting rules to combinate',
68             );
69              
70             my %formats = map {($_,1)} qw'yaml json perl perl6 python';
71             my %regexes = map {($_,1)} qw'perl raw';
72              
73             sub slurp {
74 0     0 0   my ($file_name) = @_;
75 0 0         open INPUT, $file_name or die "Can't open $file_name for input";
76 0           local $/;
77            
78 0           }
79              
80             sub execute {
81 0     0 1   my ($self, $opt, $args) = @_;
82 0           my $to = $self->to;
83 0           my $regex = $self->regex;
84             die "'$to' is an invalid --to= format"
85 0 0         unless $formats{$to};
86             die "'$regex' is an invalid --regex= format"
87 0 0         unless $regexes{$regex};
88             my $input = scalar(@$args)
89             ? slurp($args->[0])
90 0 0         : do { local $/; <> };
  0            
  0            
91 0 0         my $compiler_class = $self->boot
92             ? 'Pegex::Bootstrap'
93             : 'Pegex::Compiler';
94 0 0         eval "use $compiler_class; 1" or die $@;
95 0           my $compiler = $compiler_class->new();
96 0           $compiler->parse($input)->combinate(@{$self->rules});
  0            
97 0 0         $compiler->native if $regex eq 'perl';
98             my $output =
99             $to eq 'perl' ? $compiler->to_perl :
100             $to eq 'yaml' ? $compiler->to_yaml :
101             $to eq 'json' ? $compiler->to_json :
102 0 0         do { die "'$to' format not supported yet" };
  0 0          
    0          
103 0           print STDOUT $output;
104             }
105              
106             1;