AST.pm

filter: Statementstmt 96.0% 122 / 127 Branchbran 50.0% 13 / 26 Conditioncond 44.4% 16 / 36 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 100% 20 / 20 Podpod 100% 5 / 5 totaltotal 75.2% 176 / 234 CC 31 SCAR 28.0
CC
31
Cov
80%
generate
30.7
parse
15.7
BEGIN
1.0
CRAP 16.4
« AST/Util.pm ↑ Summary AST/Data/Scan/Impl/Perl5/_BaseTypes.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package MarpaX::Languages::IDL::AST;
5 2 ▶use MarpaX::Languages::IDL::AST::Value;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use MarpaX::Languages::IDL::AST::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Scalar::Util qw/blessed reftype refaddr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Template::Constants qw/:chomp :debug/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::ShareDir qw/dist_dir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Data::Scan;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # ABSTRACT: Translate an IDL source to an AST
16  
17 2 our $VERSION = '0.007'; # VERSION
18  
19  
20 2 ▶use Carp qw/carp croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Marpa::R2 qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use File::Basename qw/basename fileparse/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use File::Spec::Functions qw/case_tolerant rel2abs catdir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 our $BLESS_PACKAGE = 'IDL::AST';
26 2 our $DATA = do {local $/; <DATA>};
27 2 our $G = Marpa::R2::Scanless::G->new({source => \$DATA, bless_package => $BLESS_PACKAGE});
28 # Marpa follows Unicode recommendation, i.e. perl's \R, that cannot be in a character class
29 2 our $NEWLINE_REGEXP = qr/(?>\x0D\x0A|\v)/;
30  
31  
32 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
33 1 my ($class) = @_;
34  
35 1 my $self = {
36 };
37  
38 1 bless($self, $class);
39  
40 1 return $self;
41 }
42  
43  
44 1 ▶sub parse {
Subroutine parse: called CC=12 SCAR=27.5
Pod documented
45 1 my ($self, $input, $hashOptPtr) = @_;
46 #
47 # Parameters check
48 #
49 1 ▶ $hashOptPtr //= {};
Condition50%1/2
$hashOptPtr //= {}
l!l
01
MC/DC0%0/1
$hashOptPtr //= {}
$hashOptPtr
Truth table50%1/2
A
A= $hashOptPtr
Aresult
11
00
50 1 ▶ if (ref($hashOptPtr) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hashOptPtr ne "HASH") 0 1 1
51 0 croak '3rd argument must be a pointer to HASH containing any Marpa::R2::Scanles::R option, except the grammar option';
52 }
53 1 foreach (qw/grammar semantics_package/) {
54 2 ▶ if (exists($hashOptPtr->{$_})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hashOptPtr->{$_}) 0 2 2
55 0 delete($hashOptPtr->{$_});
56 }
57 }
58 1 my $datap;
59 1 ▶ if (! ref($input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $input) { } 1 0 1
60 #
61 # Assume this is a filename
62 # IDL wants the filename to end with .idl
63 #
64 1 my ($filename, $directories, $suffix) = fileparse($input, qr/\.[^.]*/);
65 1 ▶ if (( case_tolerant() && (lc($suffix) ne '.idl')) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($suffix ne ".idl") 0 1 1
66 (! case_tolerant() && ( $suffix ne '.idl'))) {
67 0 carp "$input does not end with .idl";
68 }
69 #
70 # Load data
71 #
72 1 ▶ open(my $fh, '<', $input) || croak "Failed to open $input, $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $input or croak("Failed to open $input, $!") 1 0 1
73 1 my $data = do { local $/; <$fh> };
74 1 ▶ close($fh) || warn "Failed to close $input, $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or warn "Failed to close $input, $!" 1 0 1
75 1 $datap = \$data;
76 } else {
77 #
78 # Assume this is the data to parse
79 #
80 0 $datap = $input;
81 }
82 #
83 # Recognizer
84 #
85 my $recce = Marpa::R2::Scanless::R->new({grammar => $G,
86 # trace_terminals => 1,
87 semantics_package => 'MarpaX::Languages::IDL::AST::Value',
88 1 %{$hashOptPtr}});
89 1 $recce->read($datap);
90 #
91 # AST value
92 #
93 1 my $value = $recce->value();
94 1 ▶ croak 'Undefined AST value' if (! defined($value) || ! defined(${$value}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and defined ${$value;} 0 1 1
Condition33%1/3
defined $value and defined ${$value;}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $value and defined ${$value;}
defined $value defined ${$value;}
Truth table33%1/3
A and B
A= defined $valueB= defined ${$value;}
ABresult
0X0
100
111
95 #
96 # We want a single value
97 #
98 1 my $nextValue = $recce->value();
99 1 ▶ croak 'Ambiguous AST' if (defined($nextValue));
Branch: 1 branch
BranchTrueFalseTotal
if defined $nextValue 0 1 1
100 #
101 # Let's remember the latest AST
102 #
103 1 $self->{_ast} = ${$value};
104  
105 1 return $self;
106 }
107  
108  
109 1 ▶sub ast {
Subroutine ast: called CC=1 SCAR=0.0
Pod documented
110 1 my ($self) = @_;
111  
112 1 return $self->{_ast};
113 }
114  
115  
116 1 ▶sub output {
Subroutine output: called CC=1 SCAR=0.0
Pod documented
117 1 my ($self) = @_;
118  
119 1 return $self->{_output};
120 }
121  
122  
123 1 ▶sub generate {
Subroutine generate: called CC=20 SCAR=34.2
Pod documented
124 1 my ($self, $ast, $template, $targetOptionHashp) = @_;
125  
126 1 ▶ $ast //= $self->ast();
Condition33%1/3
$ast //= $self->ast
l!l&&r!l&&!r
010
MC/DC0%0/2
$ast //= $self->ast
$ast $self->ast
Truth table33%1/3
A //= B
A= $astB= $self->ast
ABresult
1X1
011
000
127 1 ▶ $template //= 'perl5.tt2';
Condition50%1/2
$template //= "perl5.tt2"
l!l
01
MC/DC0%0/1
$template //= "perl5.tt2"
$template
Truth table50%1/2
A
A= $template
Aresult
11
00
128 1 ▶ $targetOptionHashp //= {};
Condition50%1/2
$targetOptionHashp //= {}
l!l
01
MC/DC0%0/1
$targetOptionHashp //= {}
$targetOptionHashp
Truth table50%1/2
A
A= $targetOptionHashp
Aresult
11
00
129 #
130 # We provide a default style only if this is a template we know about
131 #
132 1 my $style = $targetOptionHashp->{style};
133 1 my $addDefaultStyleInIncludePath = '';
134 1 my $packageDist = __PACKAGE__;
135 1 $packageDist =~ s/::/-/g;
136 1 my $distDir = dist_dir($packageDist);
137 1 ▶ if (! defined($style)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $style) 1 0 1
138 1 ▶ if ($template eq 'perl5.tt2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($template eq "perl5.tt2") 1 0 1
139 1 $style = 'Moose';
140 1 $addDefaultStyleInIncludePath = catdir($distDir, 'perl5', 'style', $style);
141 }
142 }
143  
144 1 ▶ if (ref($targetOptionHashp) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $targetOptionHashp ne "HASH") 0 1 1
145 0 croak '3rd argument must be a pointer to HASH';
146 }
147  
148 1 my $ttOptionHashp = $targetOptionHashp->{tt};
149 1 ▶ $ttOptionHashp->{STRICT} //= 1;
Condition50%1/2
$ttOptionHashp->{"STRICT"} //= 1
l!l
01
MC/DC0%0/1
$ttOptionHashp->{"STRICT"} //= 1
$ttOptionHashp->{"STRICT"}
Truth table50%1/2
A
A= $ttOptionHashp->{"STRICT"}
Aresult
11
00
150 1 ▶ $ttOptionHashp->{DELIMITER} //= $Config{path_sep};
Condition33%1/3
$ttOptionHashp->{"DELIMITER"} //= $MarpaX::Languages::IDL::AST::Config{"path_sep"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$ttOptionHashp->{"DELIMITER"} //= $MarpaX::Languages::IDL::AST::Config{"path_sep"}
$ttOptionHashp->{"DELIMITER"} $MarpaX::Languages::IDL::AST::Config{"path_sep"}
Truth table33%1/3
A //= B
A= $ttOptionHashp->{"DELIMITER"}B= $MarpaX::Languages::IDL::AST::Config{"path_sep"}
ABresult
1X1
011
000
151 1 ▶ $ttOptionHashp->{INCLUDE_PATH} //= '';
Condition50%1/2
$ttOptionHashp->{"INCLUDE_PATH"} //= ""
l!l
01
MC/DC0%0/1
$ttOptionHashp->{"INCLUDE_PATH"} //= ""
$ttOptionHashp->{"INCLUDE_PATH"}
Truth table50%1/2
A
A= $ttOptionHashp->{"INCLUDE_PATH"}
Aresult
11
00
152 1 $ttOptionHashp->{INCLUDE_PATH} .= $ttOptionHashp->{DELIMITER} . $distDir;
153 1 ▶ if ($addDefaultStyleInIncludePath) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addDefaultStyleInIncludePath) 1 0 1
154 1 $ttOptionHashp->{INCLUDE_PATH} .= $ttOptionHashp->{DELIMITER} . $addDefaultStyleInIncludePath;
155 }
156 1 ▶ $ttOptionHashp->{INTERPOLATE} //= 1;
Condition50%1/2
$ttOptionHashp->{"INTERPOLATE"} //= 1
l!l
01
MC/DC0%0/1
$ttOptionHashp->{"INTERPOLATE"} //= 1
$ttOptionHashp->{"INTERPOLATE"}
Truth table50%1/2
A
A= $ttOptionHashp->{"INTERPOLATE"}
Aresult
11
00
157 1 ▶ $ttOptionHashp->{EVAL_PERL} //= 1;
Condition50%1/2
$ttOptionHashp->{"EVAL_PERL"} //= 1
l!l
01
MC/DC0%0/1
$ttOptionHashp->{"EVAL_PERL"} //= 1
$ttOptionHashp->{"EVAL_PERL"}
Truth table50%1/2
A
A= $ttOptionHashp->{"EVAL_PERL"}
Aresult
11
00
158 1 ▶ $ttOptionHashp->{PRE_CHOMP} //= CHOMP_NONE;
Condition50%1/2
$ttOptionHashp->{"PRE_CHOMP"} //= 0
l!l
01
MC/DC0%0/1
$ttOptionHashp->{"PRE_CHOMP"} //= 0
$ttOptionHashp->{"PRE_CHOMP"}
Truth table50%1/2
A
A= $ttOptionHashp->{"PRE_CHOMP"}
Aresult
11
00
159 1 ▶ $ttOptionHashp->{POST_CHOMP} //= CHOMP_NONE;
Condition50%1/2
$ttOptionHashp->{"POST_CHOMP"} //= 0
l!l
01
MC/DC0%0/1
$ttOptionHashp->{"POST_CHOMP"} //= 0
$ttOptionHashp->{"POST_CHOMP"}
Truth table50%1/2
A
A= $ttOptionHashp->{"POST_CHOMP"}
Aresult
11
00
160 1 ▶ $ttOptionHashp->{RELATIVE} //= 1;
Condition50%1/2
$ttOptionHashp->{"RELATIVE"} //= 1
l!l
01
MC/DC0%0/1
$ttOptionHashp->{"RELATIVE"} //= 1
$ttOptionHashp->{"RELATIVE"}
Truth table50%1/2
A
A= $ttOptionHashp->{"RELATIVE"}
Aresult
11
00
161 1 local $Template::Directive::WHILE_MAX = 1000000000;
162 1 ▶ my $tt = Template->new($ttOptionHashp) || croak "$Template::ERROR";
Condition50%1/2
'Template'->new($ttOptionHashp) || croak("$Template::ERROR")
l!l
10
MC/DC0%0/1
'Template'->new($ttOptionHashp) || croak("$Template::ERROR")
'Template'->new($ttOptionHashp)
Truth table50%1/2
A
A= 'Template'->new($ttOptionHashp)
Aresult
11
00
163  
164 #
165 # The semantics for our TT templates is to provide a hash with
166 # a reference to a scratchpad hash (free to use) and the AST
167 #
168 1 my $ttVarsHashp = $targetOptionHashp->{vars};
169 #
170 # Our hooks
171 #
172 1 ▶ $ttVarsHashp->{ast} //= $ast;
Condition33%1/3
$ttVarsHashp->{"ast"} //= $ast
l!l&&r!l&&!r
010
MC/DC0%0/2
$ttVarsHashp->{"ast"} //= $ast
$ttVarsHashp->{"ast"} $ast
Truth table33%1/3
A //= B
A= $ttVarsHashp->{"ast"}B= $ast
ABresult
1X1
011
000
173 1 ▶ $ttVarsHashp->{nativeFloat} = $targetOptionHashp->{nativeFloat} // 1;
Condition50%1/2
$targetOptionHashp->{"nativeFloat"} // 1
l!l
01
MC/DC0%0/1
$targetOptionHashp->{"nativeFloat"} // 1
$targetOptionHashp->{"nativeFloat"}
Truth table50%1/2
A
A= $targetOptionHashp->{"nativeFloat"}
Aresult
11
00
174 1 $ttVarsHashp->{style} = $style;
175  
176 1 $self->{_output} = '';
177 1 ▶ $tt->process($template, $ttVarsHashp, \$self->{_output}) || croak $tt->error();
Branch: 1 branch
BranchTrueFalseTotal
$tt->process($template, $ttVarsHashp, \$self->{"_output"}) or croak($tt->error) 1 0 1
178  
179 1 return $self;
180 }
181  
182  
183  
184 2 1;
185  
186 =pod
187  
188 =encoding UTF-8
189  
190 =head1 NAME
191  
192 MarpaX::Languages::IDL::AST - Translate an IDL source to an AST
193  
194 =head1 VERSION
195  
196 version 0.007
197  
198 =head1 SYNOPSIS
199  
200 use MarpaX::Languages::IDL::AST;
201  
202 my $idlPath = 'source.idl';
203 my $ast = MarpaX::Languages::IDL::AST->new()->parse($idlPath)->validate();
204  
205 =head2 $class->new()
206  
207 Instantiate a new object. Returns a reference to it, denoted $self hereafter.
208  
209 =head2 $self->parse($path, $hashOptPtr)
210  
211 Parse the IDL and produce an AST out of it, then a meta-AST that is more useful representation for further processing. Takes as parameters:
212  
213 =over
214  
215 =item $path
216  
217 A required IDL pathname.
218  
219 =item $hashOptPtr
220  
221 An optional reference to a hash containing Marpa::R2::Scanless::R() parameters, except the grammar and semantics_package options.
222  
223 =back
224  
225 The AST is an exact representation of the parse tree value of the IDL grammar contained in this package, except:
226  
227 =over
228  
229 =item scopedName
230  
231 Original grammar rule is:
232  
233 <scopedName> ::= <identifier> | '::' <identifier> | <scopedName> '::' <identifier>
234  
235 and has been rewriten to:
236  
237 <scopedName> ::= <identifier>+ separator => <coloncolon>
238  
239 A dedicated action rule will concatenate all identifiers into a single string, giving the impression that scopedName is a token (accurate terminology is a lexeme). I.e. the scopedName value in the AST is in the form:
240  
241 bless([start,totalLength,concatenatedValue], 'IDL::AST::scopedName')
242  
243 alike the identifier.
244  
245 =back
246  
247 This method returns $self.
248  
249 =head2 $self->ast()
250  
251 Returns the latest AST produced by $self->parse().
252  
253 =head2 $self->output()
254  
255 Returns the latest output produced by $self->generate().
256  
257 =head2 $self->generate($ast, $template, $targetOptionHashp)
258  
259 Generate files for the given AST $ast.
260  
261 =over
262  
263 =item $ast
264  
265 AST as produced by the method $self->parse(). Default to $self->ast().
266  
267 =item $template
268  
269 Template-Toolkit template name. Default to 'perl5.tt2', available in this distribution.
270  
271 =item $targetOptionHashp
272  
273 Hash reference of options specific to target $target.
274  
275 =back
276  
277 This method returns $self.
278  
279 =head1 DESCRIPTION
280  
281 This module provide and manage an AST of an IDL file, as per OMG's IDL 3.5 grammar specification.
282  
283 =head1 NOTES
284  
285 IDL version is 3.5 as of L<OMG IDL3.5 Specification|http://www.omg.org/spec/IDL35/3.5/>.
286  
287 This specification imposes input to come from a filename, with suffix '.idl'.
288  
289 Any preprocessing feature is ignored, and eventual multi-line proprocessing directives are likely to cause failure. Since the most expected preprocessing tokens are #include, #ifdef, and al., the user is expected to have already run a preprocessor before using this package.
290  
291 =head1 SEE ALSO
292  
293 L<Marpa::R2>
294  
295 =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
296  
297 =head1 SUPPORT
298  
299 =head2 Bugs / Feature Requests
300  
301 Please report any bugs or feature requests through the issue tracker
302 at L<https://rt.cpan.org/Public/Dist/Display.html?Name=MarpaX-Languages-IDL-AST>.
303 You will be notified automatically of any progress on your issue.
304  
305 =head2 Source Code
306  
307 This is open source software. The code repository is available for
308 public review and contribution under the terms of the license.
309  
310 L<https://github.com/jddurand/marpax-languages-idl-ast>
311  
312 git clone git://github.com/jddurand/marpax-languages-idl-ast.git
313  
314 =head1 AUTHOR
315  
316 Jean-Damien Durand <jeandamiendurand@free.fr>
317  
318 =head1 COPYRIGHT AND LICENSE
319  
320 This software is copyright (c) 2014 by Jean-Damien Durand.
321  
322 This is free software; you can redistribute it and/or modify it under
323 the same terms as the Perl 5 programming language system itself.
324  
325 =cut
326  
327 __DATA__
« AST/Util.pm ↑ Summary AST/Data/Scan/Impl/Perl5/_BaseTypes.pm »