| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Pegex::Cmd; | 
| 2 |  |  |  |  |  |  | our $VERSION = '0.26'; | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | #-----------------------------------------------------------------------------# | 
| 5 |  |  |  |  |  |  | package Pegex::Cmd::Command; | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 1 |  |  | 1 |  | 1352 | use App::Cmd::Setup -command; | 
|  | 1 |  |  |  |  | 32995 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 8 | 1 |  |  | 1 |  | 590 | use Mouse; | 
|  | 1 |  |  |  |  | 21487 |  | 
|  | 1 |  |  |  |  | 4 |  | 
| 9 |  |  |  |  |  |  | extends 'MouseX::App::Cmd::Command'; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | #-----------------------------------------------------------------------------# | 
| 12 |  |  |  |  |  |  | package Pegex::Cmd; | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 1 |  |  | 1 |  | 290 | use App::Cmd::Setup -app; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 10 |  | 
| 15 | 1 |  |  | 1 |  | 443 | use Mouse; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 3 |  | 
| 16 |  |  |  |  |  |  | extends 'MouseX::App::Cmd'; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | use Module::Pluggable | 
| 19 | 1 |  |  |  |  | 7 | require     => 1, | 
| 20 | 1 |  |  | 1 |  | 728 | search_path => [ 'Pegex::Cmd::Command' ]; | 
|  | 1 |  |  |  |  | 617 |  | 
| 21 |  |  |  |  |  |  | Pegex::Cmd->plugins; | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | #-----------------------------------------------------------------------------# | 
| 24 |  |  |  |  |  |  | package Pegex::Cmd::Command::compile; | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | Package->import( -command ); | 
| 27 | 1 |  |  | 1 |  | 84 | use Mouse; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 4 |  | 
| 28 |  |  |  |  |  |  | extends 'Pegex::Cmd::Command'; | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 1 |  |  |  |  | 56 | use constant abstract => | 
| 31 | 1 |  |  | 1 |  | 245 | 'Compile a Pegex grammar to some format.'; | 
|  | 1 |  |  |  |  | 2 |  | 
| 32 | 1 |  |  |  |  | 402 | use constant usage_desc => | 
| 33 | 1 |  |  | 1 |  | 3 | 'pegex compile --to= | 
|  | 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 |  |  | 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 |  |  | my ($self, $opt, $args) = @_; | 
| 82 | 0 |  |  |  |  |  | my $to = $self->to; | 
| 83 | 0 |  |  |  |  |  | my $regex = $self->regex; | 
| 84 | 0 | 0 |  |  |  |  | die "'$to' is an invalid --to= format" | 
| 85 |  |  |  |  |  |  | unless $formats{$to}; | 
| 86 | 0 | 0 |  |  |  |  | die "'$regex' is an invalid --regex= format" | 
| 87 |  |  |  |  |  |  | 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; |