| 1 | package MarpaX::Simple; | ||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||
| 3 | 1 | our $DATE = '2017-07-10'; # DATE | |||||||||||||||||||||||||
| 4 | 1 | our $VERSION = '0.07'; # VERSION | |||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||
| 6 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Log::ger; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Marpa::R2; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 12 | 1 | ▶ | use UUID::Random; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 15 | 1 | our @EXPORT_OK = qw(gen_parser); | |||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||
| 17 | 1 | our %SPEC; | |||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||
| 19 | $SPEC{gen_parser} = { | ||||||||||||||||||||||||||
| 20 | 1 | v => 1.1, | |||||||||||||||||||||||||
| 21 | summary => 'Generate Marpa-based parser', | ||||||||||||||||||||||||||
| 22 | description => <<'_', | ||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||
| 24 | One of `slif_grammar`, `grammar_class_file`, `grammar_module_file`, | ||||||||||||||||||||||||||
| 25 | `grammar_file`, `grammar` must be specified for the input grammar. The arguments | ||||||||||||||||||||||||||
| 26 | will be checked in that order. The simplest would be to use grammar text inlined | ||||||||||||||||||||||||||
| 27 | in your source code (`grammar`), but for larger grammar it might be more | ||||||||||||||||||||||||||
| 28 | convenient to put it in a separate file (the `grammar_*file` arguments) so you | ||||||||||||||||||||||||||
| 29 | can check line numbers from error messages more easily. The `slif_grammar` has | ||||||||||||||||||||||||||
| 30 | the lowest overhead since you specify SLIF grammar that are already | ||||||||||||||||||||||||||
| 31 | instantiated. See also the related Dist::Zilla plugin | ||||||||||||||||||||||||||
| 32 | `Dist::Zilla::Plugin::InsertMarpaSLIFGrammar`. | ||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||
| 35 | _ | ||||||||||||||||||||||||||
| 36 | args => { | ||||||||||||||||||||||||||
| 37 | slif_grammar => { | ||||||||||||||||||||||||||
| 38 | summary => 'SLIF input grammar', | ||||||||||||||||||||||||||
| 39 | schema => ['obj*', isa=>'Marpa::R2::Scanless::G'], | ||||||||||||||||||||||||||
| 40 | description => <<'_', | ||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||
| 42 | This input-grammar argument is checked first because it is the most ready-to-use | ||||||||||||||||||||||||||
| 43 | form and has the least overhead. The other input-grammar arguments require | ||||||||||||||||||||||||||
| 44 | creating the `Marpa::R2::Scanless::G` object first from the Marpa BNF DSL | ||||||||||||||||||||||||||
| 45 | grammar. | ||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||
| 47 | _ | ||||||||||||||||||||||||||
| 48 | tags => ['category:input-grammar'], | ||||||||||||||||||||||||||
| 49 | }, | ||||||||||||||||||||||||||
| 50 | grammar_class_file => { | ||||||||||||||||||||||||||
| 51 | summary => 'Marpa BNF input grammar file, '. | ||||||||||||||||||||||||||
| 52 | 'resolved using File::ShareDir\'s class_file()', | ||||||||||||||||||||||||||
| 53 | schema => 'str*', | ||||||||||||||||||||||||||
| 54 | tags => ['category:input-grammar'], | ||||||||||||||||||||||||||
| 55 | }, | ||||||||||||||||||||||||||
| 56 | grammar_module_file => { | ||||||||||||||||||||||||||
| 57 | summary => 'Marpa BNF input grammar file, '. | ||||||||||||||||||||||||||
| 58 | 'resolved using File::ShareDir\'s module_file()', | ||||||||||||||||||||||||||
| 59 | schema => 'str*', | ||||||||||||||||||||||||||
| 60 | tags => ['category:input-grammar'], | ||||||||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||||||||
| 62 | grammar_file => { | ||||||||||||||||||||||||||
| 63 | summary => 'Marpa BNF input grammar file', | ||||||||||||||||||||||||||
| 64 | schema => 'str*', | ||||||||||||||||||||||||||
| 65 | tags => ['category:input-grammar'], | ||||||||||||||||||||||||||
| 66 | }, | ||||||||||||||||||||||||||
| 67 | grammar => { | ||||||||||||||||||||||||||
| 68 | summary => 'Marpa BNF input grammar text', | ||||||||||||||||||||||||||
| 69 | schema => 'str*', | ||||||||||||||||||||||||||
| 70 | tags => ['category:input-grammar'], | ||||||||||||||||||||||||||
| 71 | }, | ||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||
| 73 | actions => { | ||||||||||||||||||||||||||
| 74 | summary => 'Supply code for actions specified in the grammar', | ||||||||||||||||||||||||||
| 75 | schema => ['hash*', each_value => 'code*'], | ||||||||||||||||||||||||||
| 76 | }, | ||||||||||||||||||||||||||
| 77 | too_many_earley_items => { | ||||||||||||||||||||||||||
| 78 | summary => "Will be passed to recognizer's constructor", | ||||||||||||||||||||||||||
| 79 | schema => ['int*'], | ||||||||||||||||||||||||||
| 80 | }, | ||||||||||||||||||||||||||
| 81 | trace_terminals => { | ||||||||||||||||||||||||||
| 82 | summary => "Will be passed to recognizer's constructor", | ||||||||||||||||||||||||||
| 83 | schema => ['bool'], | ||||||||||||||||||||||||||
| 84 | }, | ||||||||||||||||||||||||||
| 85 | trace_values => { | ||||||||||||||||||||||||||
| 86 | summary => "Will be passed to recognizer's constructor", | ||||||||||||||||||||||||||
| 87 | schema => ['bool'], | ||||||||||||||||||||||||||
| 88 | }, | ||||||||||||||||||||||||||
| 89 | }, | ||||||||||||||||||||||||||
| 90 | result_naked => 1, | ||||||||||||||||||||||||||
| 91 | result => { | ||||||||||||||||||||||||||
| 92 | schema => 'code*', | ||||||||||||||||||||||||||
| 93 | }, | ||||||||||||||||||||||||||
| 94 | }; | ||||||||||||||||||||||||||
| 95 | 0 | ▶ | sub gen_parser { | ||||||||||||||||||||||||
|
Subroutine
gen_parser: not called CC=7 SCAR=38.8
Pod
documented
| |||||||||||||||||||||||||||
| 96 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||
| 98 | 0 | my %args = @_; | |||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||
| 100 | 0 | my $slif; | |||||||||||||||||||||||||
| 101 | { | ||||||||||||||||||||||||||
| 102 | 0 | my $bnf; | |||||||||||||||||||||||||
| 103 | 0 | ▶ | if ($args{slif_grammar}) { | ||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||
| 104 | 0 | $slif = $args{slif_grammar}; | |||||||||||||||||||||||||
| 105 | 0 | last; | |||||||||||||||||||||||||
| 106 | } elsif ($args{grammar_class_file}) { | ||||||||||||||||||||||||||
| 107 | 0 | require File::ShareDir; | |||||||||||||||||||||||||
| 108 | 0 | require File::Slurper; | |||||||||||||||||||||||||
| 109 | $bnf = File::Slurper::read_text( | ||||||||||||||||||||||||||
| 110 | File::ShareDir::class_file(__PACKAGE__, | ||||||||||||||||||||||||||
| 111 | 0 | $args{grammar_class_file})); | |||||||||||||||||||||||||
| 112 | } elsif ($args{grammar_module_file}) { | ||||||||||||||||||||||||||
| 113 | 0 | require File::ShareDir; | |||||||||||||||||||||||||
| 114 | 0 | require File::Slurper; | |||||||||||||||||||||||||
| 115 | $bnf = File::Slurper::read_text( | ||||||||||||||||||||||||||
| 116 | File::ShareDir::module_file(__PACKAGE__, | ||||||||||||||||||||||||||
| 117 | 0 | $args{grammar_module_file})); | |||||||||||||||||||||||||
| 118 | } elsif ($args{grammar_file}) { | ||||||||||||||||||||||||||
| 119 | 0 | require File::Slurper; | |||||||||||||||||||||||||
| 120 | 0 | $bnf = File::Slurper::read_text($args{grammar_file}); | |||||||||||||||||||||||||
| 121 | } elsif ($args{grammar}) { | ||||||||||||||||||||||||||
| 122 | 0 | $bnf = $args{grammar}; | |||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||
| 124 | 0 | $slif = Marpa::R2::Scanless::G->new({source => \$bnf}); | |||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||
| 127 | 0 | my $pkg = __PACKAGE__ . '::gen' . substr(UUID::Random::generate(), 0, 8); | |||||||||||||||||||||||||
| 128 | 0 | my $acts = $args{actions}; | |||||||||||||||||||||||||
| 129 | 0 | for (keys %$acts) { | |||||||||||||||||||||||||
| 130 | 0 | *{"$pkg\::$_"} = $acts->{$_}; | |||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||
| 133 | 0 | ▶ | my $parser = sub { | ||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||
| 134 | 0 | my $input = shift; | |||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||
| 136 | my $rec_args = { | ||||||||||||||||||||||||||
| 137 | grammar => $slif, | ||||||||||||||||||||||||||
| 138 | semantics_package => $pkg, | ||||||||||||||||||||||||||
| 139 | trace_terminals => $args{trace_terminals} ? 1:0, | ||||||||||||||||||||||||||
| 140 | 0 | ▶ | trace_values => $args{trace_values} ? 1:0, | ||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||
| 141 | }; | ||||||||||||||||||||||||||
| 142 | $rec_args->{too_many_earley_items} = $args{too_many_earley_items} | ||||||||||||||||||||||||||
| 143 | 0 | ▶ | if $args{too_many_earley_items}; | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 144 | 0 | my $recce = Marpa::R2::Scanless::R->new($rec_args); | |||||||||||||||||||||||||
| 145 | 0 | $recce->read(\$input); | |||||||||||||||||||||||||
| 146 | 0 | my $valref = $recce->value; | |||||||||||||||||||||||||
| 147 | 0 | ▶ | if (!defined($valref)) { | ||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||
| 148 | 0 | die "No parse was found after reading the entire input\n"; | |||||||||||||||||||||||||
| 149 | # XXX show last expression | ||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||
| 151 | 0 | $$valref; | |||||||||||||||||||||||||
| 152 | 0 | }; | |||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||
| 154 | 0 | $parser; | |||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||
| 157 | 1 | 1; | |||||||||||||||||||||||||
| 158 | # ABSTRACT: Generate Marpa-based parser | ||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||
| 160 | __END__ | ||||||||||||||||||||||||||