lib/MarpaX/G4.pm

filter: Statementstmt 61.1% 52 / 85 Branchbran 34.0% 15 / 44 Conditioncond 18.1% 2 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 75.0% 9 / 12 Podpod 0.0% 0 / 6 totaltotal 47.5% 78 / 164 CC 26 SCAR 28.5
CC
26
Cov
49%
readFile
30.0
translatefiles
30.0
processSymboltable
22.2
CRAP 17.4
« script/testmarpagrammar.pl ↑ Summary script/testmarpagrammar_from_file.pl »
1 # ----------------------------------------------------------------------------------------------------- #
2 # MarpaX::MarpaGen #
3 # #
4 # translate the parsed antlr4 rules to Marpa::R2 syntax. #
5 # #
6 # V 0.9.6 #
7 # ----------------------------------------------------------------------------------------------------- #
8  
9 package MarpaX::G4;
10  
11 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use MarpaX::G4::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use MarpaX::G4::Symboltable;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use MarpaX::G4::MarpaGen;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 our $VERSION = '0.9.6';
21 2 our $optstr = 'cdefghiko:prs:tuv';
22  
23 sub new
24 1 ▶{
Subroutine new: called CC=2 SCAR=7.2
Pod undocumented
25 1 my $invocant = shift;
26 1 ▶ my $class = ref($invocant) || $invocant; # Object or class name
Condition33%1/3
ref $invocant || $invocant
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table33%1/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
27 1 my $self = {}; # initiate our handy hashref
28 1 bless($self,$class); # make it usable
29  
30 1 return $self;
31 }
32  
33 sub printHelpScreen
34 0 ▶{
Subroutine printHelpScreen: not called CC=1 SCAR=6.9
Pod undocumented
35 0 my ($scriptName) = @_;
36  
37 0 my $usage = "Usage: $scriptName [-cdefghikprtuv] [-s <startsymbol>] [-o <outputfile>] <file1>[ <file2> ...]";
38  
39 0 my @helptext = (
40 "$scriptName - Antlr4 to MarpaX converter"
41 ,""
42 ,"$usage"
43 ,""
44 ,"-c strip all comments and actions (except inline actions)"
45 ,"-d dump the parse tree"
46 ,"-e embed the g4 inline actions into the marpa grammar"
47 ," (default : prefix as comments ahead of the rule)"
48 ,"-f convert fragments to classes where applicable"
49 ,"-g convert lazy to greedy quantifiers"
50 ," (CAVEAT: this might change the grammar semantics)"
51 ,"-h print this help and exit"
52 ,"-i ignore redirects (don't discard redirected rules)"
53 ,"-k build case-insensitive keywords from single-letter fragments"
54 ,"-o <outputfile> specify the output file. default is stdout"
55 ,"-p strip inline comments and actions"
56 ,"-r verify the consistency of the symbol table"
57 ,"-s <startsymbol> specify the start rule of the grammar"
58 ," (default: 1st rule of the 1st input file)"
59 ,"-t trace the grammar generation"
60 ,"-u make literals and classes case-insensitive"
61 ,"-v dump the symbol table"
62 );
63  
64 0 map { print $_ . "\n"; } @helptext;
65  
66 0 exit 0;
67 }
68  
69 ##
70 # readFile: swallow an entire input file into a string
71 ##
72 sub readFile
73 0 ▶{
Subroutine readFile: not called CC=5 SCAR=34.0
Pod undocumented
74 0 my ($infile) = @_;
75  
76 0 ▶ my $inph = *STDIN if $infile eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $infile eq "-" 0 0 0
77 0 ▶ open($inph, "< $infile") || die "can't open input file $infile : $!" if $infile ne '-';
Branch: 1 branch
BranchTrueFalseTotal
if $infile ne "-" 0 0 0
Condition0%0/5
$infile ne '-' && (open($inph, "< $infile") || die("can't open input file $infile : $!"))
!ll&&!rl&&r
000
open $inph, "< $infile" or die "can't open input file $infile : $!"
l!l
00
MC/DC0%0/2
$infile ne '-' && (open($inph, "< $infile") || die("can't open input file $infile : $!"))
$infile ne '-' open $inph, "< $infile"
Truth table0%0/3
A && B
A= $infile ne '-'B= open $inph, "< $infile"
ABresult
0X0
100
111
78  
79 my $file_text = do
80 0 {
81 0 local $/;
82 0 <$inph>;
83 };
84  
85 0 ▶ close($inph) if $infile ne '-';
Branch: 1 branch
BranchTrueFalseTotal
if $infile ne "-" 0 0 0
86  
87 0 return $file_text;
88 }
89  
90 ##
91 # processSymboltable: generate the output grammar from the symbol table
92 ##
93 sub processSymboltable
94 1 ▶{
Subroutine processSymboltable: called CC=14 SCAR=31.0
Pod undocumented
95 1 my ($self, $symboltable, $options ) = @_;
96  
97 1 ▶ if ( exists $options->{v} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"v"}) 0 1 1
98 {
99 0 $Data::Dumper::Indent = 1;
100 0 print Dumper($symboltable);
101 }
102  
103 1 ▶ $symboltable->setStartRule($options->{s}) if exists $options->{s};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"s"} 0 1 1
104 1 ▶ $symboltable->validateSymbolTable() if exists $options->{r};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"r"} 0 1 1
105  
106 1 my $generator = new MarpaX::G4::MarpaGen;
107  
108 1 ▶ $generator->stripallcomments if exists $options->{c};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"c"} 0 1 1
109 1 ▶ $generator->embedactions if exists $options->{e};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"e"} 0 1 1
110 1 ▶ $generator->fragment2class if exists $options->{f};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"f"} 1 0 1
111 1 ▶ $generator->shiftlazytogreedy if exists $options->{g};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"g"} 0 1 1
112 1 ▶ $generator->buildkeywords if exists $options->{k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"k"} 1 0 1
113 1 ▶ $generator->stripactions if exists $options->{p};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"p"} 0 1 1
114 1 ▶ $generator->setVerbosity(2) if exists $options->{t};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"t"} 0 1 1
115 1 ▶ $generator->matchcaseinsensitive if exists $options->{u} || exists $options->{k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"u"} or exists $options->{"k"} 1 0 1
Condition33%1/3
exists $options->{"u"} or exists $options->{"k"}
l!l&&r!l&&!r
100
MC/DC0%0/2
exists $options->{"u"} or exists $options->{"k"}
exists $options->{"u"} exists $options->{"k"}
Truth table33%1/3
A or B
A= exists $options->{"u"}B= exists $options->{"k"}
ABresult
1X1
011
000
116  
117 1 my $outputfile = '-';
118 1 ▶ $outputfile = $options->{o} if exists $options->{o};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"o"} 1 0 1
119 1 $generator->setoutputfile($outputfile);
120  
121 1 $generator->generate($symboltable);
122 }
123  
124 ##
125 # translatestring: parse the antlr4 input grammar from '$grammartext',
126 # generate the Marpa::R2 output grammar.
127 ##
128 sub translatestring
129 1 ▶{
Subroutine translatestring: called CC=4 SCAR=15.2
Pod undocumented
130 1 my ( $self, $grammartext, $options ) = @_;
131  
132 1 my $parser = new MarpaX::G4::Parser;
133 1 ▶ $parser->enabletrace if exists $options->{t};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"t"} 0 1 1
134 1 ▶ $parser->ignoreredirect if exists $options->{i};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"i"} 0 1 1
135  
136 1 my $symboltable = new MarpaX::G4::Symboltable;
137  
138 1 my $data = $parser->parse($grammartext);
139 1 ▶ if ( exists $options->{d} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"d"}) 0 1 1
140 {
141 0 printf "===\n=== Parsed Hashtable\n===\n";
142 0 $Data::Dumper::Indent = 2;
143 0 print Dumper($data);
144 }
145 1 $symboltable->importParseTree($data);
146  
147 1 $self->processSymboltable($symboltable, $options);
148 }
149  
150 ##
151 # translatefiles: parse the antlr4 input grammar from the input file(s),
152 # generate the Marpa::R2 output grammar.
153 ##
154 sub translatefiles
155 0 ▶{
Subroutine translatefiles: not called CC=5 SCAR=34.0
Pod undocumented
156 0 my ( $self, $inputfiles, $options ) = @_;
157  
158 0 my $parser = new MarpaX::G4::Parser;
159 0 ▶ $parser->enabletrace if exists $options->{t};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"t"} 0 0 0
160 0 ▶ $parser->ignoreredirect if exists $options->{i};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"i"} 0 0 0
161  
162 0 my $symboltable = new MarpaX::G4::Symboltable;
163  
164 0 ▶ while (scalar @$inputfiles)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$inputfiles) 0 0 0
165 {
166 0 my $infile = shift @$inputfiles;
167 0 my $grammartext = readFile($infile);
168 0 my $data = $parser->parse($grammartext);
169 0 ▶ if ( exists $options->{d} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"d"}) 0 0 0
170 {
171 0 $Data::Dumper::Indent = 2;
172 0 print Dumper($data);
173 }
174 0 $symboltable->importParseTree($data);
175 }
176  
177 0 $self->processSymboltable($symboltable, $options);
178 }
179  
180 2 1;
181  
182 # ABSTRACT: translate parsed antlr4 rules to Marpa2 syntax
183  
184 =head1 SYNOPSIS
185 use MarpaX::G4;
186  
187 =head1 DESCRIPTION
188 Translate the rules from the symbol table created from the imported ANTLR4 grammar
189 into Marpa syntax and write them to output.
190  
191 =cut
« script/testmarpagrammar.pl ↑ Summary script/testmarpagrammar_from_file.pl »