blib/script/testmarpagrammar_from_file.pl untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 105 CC 7 SCAR 20.5
CC
7
Cov
0%
readFile
20.0
flattenArray
12.0
default_action
6.0
CRAP 7.8
1 #!/usr/local/bin/perl -w
2 # #################################################################################### #
3 # Program testmarpagrammarfromfile #
4 # #
5 # Author Axel Zuber #
6 # Created 29.05.2022 #
7 # #
8 # Description read a Marpa grammar and an input file #
9 # apply the grammar to the input and dump the parse tree #
10 # #
11 # ------------------------------------------------------------------------------------ #
12 # #
13 # Input ANTLR4 grammar #
14 # input file #
15 # #
16 # ------------------------------------------------------------------------------------ #
17 # #
18 # Output Parse tree #
19 # #
20 # ------------------------------------------------------------------------------------ #
21 # #
22 # Exit code #
23 # 0 : Successful #
24 # 4 : Warnings #
25 # 8 : Errors #
26 # #
27 # ------------------------------------------------------------------------------------ #
28 # #
29 # History #
30 # Ver Date Name Description #
31 # 0.9 29.05.2022 A. Zuber Initial version #
32 # #
33 # #################################################################################### #
34  
35  
36 # ------------------------------------------------------------------------------------ #
37 # Subroutines #
38 # ------------------------------------------------------------------------------------ #
39  
40 package MarpaX::Test;
41 use strict;
42 use warnings FATAL => 'all';
43  
44 use strict;
45  
46 use lib 'lib';
47 use Data::Dumper;
48 use Getopt::Std;
49 use Marpa::R2;
50 use Data::Dump;
51  
52 # ------------------------------------------------------------------------------------ #
53 # Subroutines #
54 # ------------------------------------------------------------------------------------ #
55  
56 sub readFile
57 {
58 my ($infile) = @_;
59  
60 my $inph = *STDIN if $infile eq '-';
61 open($inph, "< $infile") || die "can't open input file $infile : $!" if $infile ne '-';
62  
63 my $file_text = do
64 {
65 local $/;
66 <$inph>;
67 };
68  
69 close($inph) if $infile ne '-';
70  
71 return $file_text;
72 }
73  
74 # ------------------------------------------------------------------------------------ #
75 # MAIN #
76 # ------------------------------------------------------------------------------------ #
77  
78 my $usage = "testmarpagrammar_from_file -g grammarfile <inputfile>";
79 my $optStr = 'g:v';
80 my $options = {};
81  
82 die 'Invalid option(s) given' if !getopts( "$optStr", $options );
83  
84 die "$usage" if !exists $options->{g} || scalar @ARGV < 1;
85  
86 my $grammartext = readFile($options->{g});
87  
88 my $dsl = <<"END_OF_DSL";
89 ${grammartext}
90 END_OF_DSL
91  
92 my $grammar = Marpa::R2::Scanless::G->new({
93 action_object => 'MarpaX::Test::Actions',
94 # default_action => '[name, values]',
95 default_action => 'default_action',
96 # default_action => 'flattenArray',
97 source => \$dsl
98 });
99  
100 my $parser = Marpa::R2::Scanless::R->new({ grammar => $grammar });
101 my $input = readFile($ARGV[0]);
102  
103 $parser->read(\$input);
104 $input = undef;
105  
106 my $value_ref = $parser->value();
107  
108 print Data::Dump::dump($value_ref);
109  
110 exit 0;
111  
112 # ------------------------------------------------------------------------------------ #
113 # Example grammar actions #
114 # ------------------------------------------------------------------------------------ #
115  
116 package MarpaX::Test::Actions;
117 use strict;
118  
119 sub new
120 {
121 my ($class) = @_;
122 return bless {}, $class;
123 }
124  
125 sub flattenArray
126 {
127 my ($self, $items) = @_;
128 my $result = $items;
129 if (ref $items eq "ARRAY")
130 {
131 if (scalar @$items != 1)
132 {
133 die "\$items must hold exactly 1 items";
134 }
135 $result = @{$items}[0];
136 }
137 return $result;
138 }
139  
140 sub default_action
141 {
142 my ($self, @items ) = @_;
143  
144 my $result = \@items;
145 if (scalar @items == 1)
146 {
147 $result = $items[0];
148 }
149  
150 return $result;
151 }
152  
153 1;
154  
155 # ABSTRACT: create a parse tree from an input file by applying a Marpa grammar
156  
157 =head1 SYNOPSIS
158  
159 testmarpagrammar_from_file.pl -g ./plsql.mx ./ddl/create_package.sql
160  
161 =head1 DESCRIPTION
162 Create the Marpa grammar from option '-g' and apply it to the text from the input file,
163 then dump the parse tree.
164 =cut