blib/lib/ExtUtils/XSpp/Parser.pm

filter: Statementstmt 90.9% 101 / 111 Branchbran 75.0% 15 / 20 Conditioncond 47.6% 10 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 89.2% 25 / 28 Podpod 55.0% 11 / 20 totaltotal 76.7% 162 / 211 CC 23 SCAR 9.5
CC
23
Cov
83%
_handle_plugin
5.5
_add_plugin
4.2
load_plugin
4.0
CRAP 2.6
« blib/lib/ExtUtils/XSpp/Node/Member.pm ↑ Summary blib/lib/ExtUtils/XSpp/Exception/object.pm »
1 package ExtUtils::XSpp::Parser;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 21 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use ExtUtils::XSpp::Grammar;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 ExtUtils::XSpp::Parser - an XS++ parser
12  
13 =cut
14  
15 4 ▶sub _my_open {
Subroutine _my_open: called CC=2 SCAR=7.1
16 4 my $file = shift;
17  
18 4 ▶ open my $in, "<", $file
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<", $file or die "Failed to open '${file}' for reading: $!" 4 0 4
19 or die "Failed to open '$file' for reading: $!";
20  
21 4 return $in;
22 }
23  
24 =head2 ExtUtils::XSpp::Parser::new( file => path )
25  
26 Create a new XS++ parser.
27  
28 =cut
29  
30 86 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
31 86 my $class = shift;
32 86 my $this = bless {}, $class;
33 86 my %args = @_;
34  
35 86 $this->{FILE} = $args{file};
36 86 $this->{STRING} = $args{string};
37 86 $this->{PARSER} = ExtUtils::XSpp::Grammar->new;
38 86 $this->{PLUGINS} = {};
39  
40 86 return $this;
41 }
42  
43 =head2 ExtUtils::XSpp::Parser::parse
44  
45 Parse the file data; returns true on success, false otherwise,
46 on failure C<get_errors> will return the list of errors.
47  
48 =cut
49  
50 86 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.0
Pod documented
51 86 my $this = shift;
52 86 my $fh;
53 86 ▶ if( $this->{FILE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'FILE'}) { } 2 84 86
54 2 $fh = _my_open( $this->{FILE} );
55 } else {
56 open $fh, '<', \$this->{STRING}
57 84 ▶ or die "Failed to create file handle from in-memory string";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", \$this->{"STRING"} or die "Failed to create file handle from in-memory string" 84 0 84
58 }
59 86 my $buf = '';
60  
61 86 my $parser = $this->{PARSER};
62 86 $parser->YYData->{LEX}{FH} = $fh;
63 86 $parser->YYData->{LEX}{BUFFER} = \$buf;
64 86 $parser->YYData->{LEX}{FILE} = $this->{FILE};
65 86 local $parser->YYData->{PARSER} = $this;
66  
67 86 $this->{DATA} = $parser->YYParse( yylex => \&ExtUtils::XSpp::Grammar::yylex,
68 yyerror => \&ExtUtils::XSpp::Grammar::yyerror,
69 yydebug => 0x00,
70 );
71 }
72  
73 0 ▶sub parse_type {
Subroutine parse_type: not called CC=1 SCAR=6.9
Pod undocumented
74 0 my( $class, $type ) = @_;
75 0 my $this = $class->new( string => "%_type{$type}" );
76  
77 0 $this->parse;
78  
79 0 return $this->{DATA};
80 }
81  
82 2 ▶sub include_file {
Subroutine include_file: called CC=1 SCAR=0.0
Pod undocumented
83 2 my $this = shift;
84 2 my( $file ) = @_;
85 2 my $buf = '';
86 my $new_lex = { FH => _my_open( $file ),
87 FILE => $file,
88 BUFFER => \$buf,
89 NEXT => $this->{PARSER}->YYData->{LEX},
90 2 };
91  
92 2 $this->{PARSER}->YYData->{LEX} = $new_lex;
93 }
94  
95 =head2 ExtUtils::XSpp::Parser::get_data
96  
97 Returns a list containing the parsed data. Each item of the list is
98 a subclass of C<ExtUtils::XSpp::Node>
99  
100 =cut
101  
102 85 ▶sub get_data {
Subroutine get_data: called CC=2 SCAR=7.1
Pod documented
103 85 my $this = shift;
104 die "'parse' must be called before calling 'get_data'"
105 85 ▶ unless exists $this->{DATA};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{"DATA"} 0 85 85
106  
107 85 return $this->{DATA};
108 }
109  
110 =head2 ExtUtils::XSpp::Parser::get_errors
111  
112 Returns the parsing errors as an array.
113  
114 =cut
115  
116 0 ▶sub get_errors {
Subroutine get_errors: not called CC=1 SCAR=6.9
Pod documented
117 0 my $this = shift;
118  
119 0 return @{$this->{ERRORS}};
120 }
121  
122 =head2 ExtUtils::XSpp::Parser::load_plugin
123  
124 Loads the specified plugin and calls its C<register_plugin> method.
125  
126 =cut
127  
128 9 ▶sub load_plugin {
Subroutine load_plugin: called CC=4 SCAR=14.0
Pod documented
129 9 my( $this, $package ) = @_;
130  
131 9 ▶ if (eval "require ExtUtils::XSpp::Plugin::$package;") {
Branch: 2 branches
BranchTrueFalseTotal
if (eval "require ExtUtils::XSpp::Plugin::$package;") { } 7 2 9
elsif (not eval "require $package;") { } 0 2 2
132 7 $package = "ExtUtils::XSpp::Plugin::$package";
133 }
134 elsif (!eval "require $package;") {
135 0 die "Could not load XS++ plugin '$package' (neither via the namespace "
136 ."'ExtUtils::XSpp::Plugin::$package' nor via '$package'). Reason: $@";
137 }
138  
139 # only call register_plugin once
140 9 ▶ if (!$this->{PLUGINS}{$package}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"PLUGINS"}{$package}) 8 1 9
141 8 $package->register_plugin( $this );
142 8 $this->{PLUGINS}{$package} = 1;
143 }
144  
145 # TODO handle %load_plugin parameters
146  
147 9 return 1;
148 }
149  
150 =head2 ExtUtils::XSpp::Parser::add_post_process_plugin
151  
152 Adds the specified plugin to be called after parsing is complete to
153 modify the parse tree before it is emitted.
154  
155 =cut
156  
157 4 ▶sub add_post_process_plugin {
Subroutine add_post_process_plugin: called CC=1 SCAR=0.0
Pod documented
158 4 my( $this, %args ) = @_;
159  
160 4 _add_plugin( $this, 'POST_PROCESS', \%args, 'post_process' );
161 }
162  
163 85 ▶sub post_process_plugins { $_[0]->{PLUGINS}{POST_PROCESS} || [] }
Subroutine post_process_plugins: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"PLUGINS"}{"POST_PROCESS"} or [] 4 81 85
164  
165 =head2 ExtUtils::XSpp::Parser::add_class_tag_plugin
166  
167 Adds the specified plugin to the list of plugins that can handle custom
168 %foo annotations for a class.
169  
170 =cut
171  
172 4 ▶sub add_class_tag_plugin {
Subroutine add_class_tag_plugin: called CC=2 SCAR=7.1
Pod documented
173 4 my( $this, %args ) = @_;
174 4 ▶ my $tag = $args{tag} || '_any_';
Condition50%1/2
$args{'tag'} || '_any_'
l!l
40
MC/DC0%0/1
$args{'tag'} || '_any_'
$args{'tag'}
Truth table50%1/2
A
A= $args{'tag'}
Aresult
11
00
175  
176 4 _add_plugin( $this, 'CLASS_TAG', \%args, 'handle_class_tag' );
177 }
178  
179 2 ▶sub handle_class_tag_plugins {
Subroutine handle_class_tag_plugins: called CC=1 SCAR=0.0
Pod undocumented
180 2 my( $this, $class, @args ) = @_;
181  
182 2 _handle_plugin( $this, $this->{PLUGINS}{CLASS_TAG}, 'class',
183 [ $class, @args ] );
184 }
185  
186 =head2 ExtUtils::XSpp::Parser::add_function_tag_plugin
187  
188 Adds the specified plugin to the list of plugins that can handle custom
189 %foo annotations for a function.
190  
191 =cut
192  
193 4 ▶sub add_function_tag_plugin {
Subroutine add_function_tag_plugin: called CC=2 SCAR=7.1
Pod documented
194 4 my( $this, %args ) = @_;
195 4 ▶ my $tag = $args{tag} || '_any_';
Condition50%1/2
$args{'tag'} || '_any_'
l!l
40
MC/DC0%0/1
$args{'tag'} || '_any_'
$args{'tag'}
Truth table50%1/2
A
A= $args{'tag'}
Aresult
11
00
196  
197 4 _add_plugin( $this, 'FUNCTION_TAG', \%args, 'handle_function_tag' );
198 }
199  
200 44 ▶sub handle_function_tags_plugins {
Subroutine handle_function_tags_plugins: called CC=1 SCAR=0.0
Pod undocumented
201 44 my( $this, $function, $tags ) = @_;
202  
203 44 _handle_plugins( $this, $this->{PLUGINS}{FUNCTION_TAG}, 'function',
204 $tags, $function )
205 }
206  
207 =head2 ExtUtils::XSpp::Parser::add_method_tag_plugin
208  
209 Adds the specified plugin to the list of plugins that can handle custom
210 %foo annotations for a function.
211  
212 =cut
213  
214 5 ▶sub add_method_tag_plugin {
Subroutine add_method_tag_plugin: called CC=2 SCAR=7.1
Pod documented
215 5 my( $this, %args ) = @_;
216 5 ▶ my $tag = $args{tag} || '_any_';
Condition50%1/2
$args{'tag'} || '_any_'
l!l
50
MC/DC0%0/1
$args{'tag'} || '_any_'
$args{'tag'}
Truth table50%1/2
A
A= $args{'tag'}
Aresult
11
00
217  
218 5 _add_plugin( $this, 'METHOD_TAG', \%args, 'handle_method_tag' );
219 }
220  
221 51 ▶sub handle_method_tags_plugins {
Subroutine handle_method_tags_plugins: called CC=1 SCAR=0.0
Pod undocumented
222 51 my( $this, $method, $tags ) = @_;
223  
224 51 _handle_plugins( $this, $this->{PLUGINS}{METHOD_TAG}, 'method',
225 $tags, $method );
226 }
227  
228 =head2 ExtUtils::XSpp::Parser::add_argument_tag_plugin
229  
230 Adds the specified plugin to the list of plugins that can handle custom
231 %foo annotations for an arguments.
232  
233 =cut
234  
235 1 ▶sub add_argument_tag_plugin {
Subroutine add_argument_tag_plugin: called CC=2 SCAR=7.1
Pod documented
236 1 my( $this, %args ) = @_;
237 1 ▶ my $tag = $args{tag} || '_any_';
Condition50%1/2
$args{'tag'} || '_any_'
l!l
10
MC/DC0%0/1
$args{'tag'} || '_any_'
$args{'tag'}
Truth table50%1/2
A
A= $args{'tag'}
Aresult
11
00
238  
239 1 _add_plugin( $this, 'ARGUMENT_TAG', \%args, 'handle_argument_tag' );
240 }
241  
242 114 ▶sub handle_argument_tags_plugins {
Subroutine handle_argument_tags_plugins: called CC=1 SCAR=0.0
Pod undocumented
243 114 my( $this, $argument, $tags ) = @_;
244  
245 114 _handle_plugins( $this, $this->{PLUGINS}{ARGUMENT_TAG}, 'argument',
246 $tags, $argument );
247 }
248  
249 =head2 ExtUtils::XSpp::Parser::add_toplevel_tag_plugin
250  
251 Adds the specified plugin to the list of plugins that can handle custom
252 %foo top level directives.
253  
254 =cut
255  
256 4 ▶sub add_toplevel_tag_plugin {
Subroutine add_toplevel_tag_plugin: called CC=2 SCAR=7.1
Pod documented
257 4 my( $this, %args ) = @_;
258 4 ▶ my $tag = $args{tag} || '_any_';
Condition50%1/2
$args{'tag'} || '_any_'
l!l
40
MC/DC0%0/1
$args{'tag'} || '_any_'
$args{'tag'}
Truth table50%1/2
A
A= $args{'tag'}
Aresult
11
00
259  
260 4 _add_plugin( $this, 'TOPLEVEL_TAG', \%args, 'handle_toplevel_tag' );
261 }
262  
263 2 ▶sub handle_toplevel_tag_plugins {
Subroutine handle_toplevel_tag_plugins: called CC=1 SCAR=0.0
Pod undocumented
264 2 my( $this, @args ) = @_;
265  
266 2 _handle_plugin( $this, $this->{PLUGINS}{TOPLEVEL_TAG}, 'top-level',
267 [ undef, @args ] );
268 }
269  
270 22 ▶sub _add_plugin {
Subroutine _add_plugin: called CC=4 SCAR=14.2
271 22 my( $this, $kind, $args, $default_method ) = @_;
272 my $entry = { plugin => $args->{plugin},
273 22 ▶ method => $args->{method} || $default_method,
Condition33%1/3
$args->{'method'} || $default_method
l!l&&r!l&&!r
0220
MC/DC0%0/2
$args->{'method'} || $default_method
$args->{'method'} $default_method
Truth table33%1/3
A || B
A= $args->{'method'}B= $default_method
ABresult
1X1
011
000
274 };
275  
276 22 ▶ if( $kind eq 'POST_PROCESS' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq 'POST_PROCESS') { } 4 18 22
277 4 push @{$this->{PLUGINS}{$kind}}, $entry;
278 } else {
279 18 ▶ push @{$this->{PLUGINS}{$kind}{$args->{tag} || '_any_'}}, $entry;
Condition50%1/2
$args->{'tag'} || '_any_'
l!l
180
MC/DC0%0/1
$args->{'tag'} || '_any_'
$args->{'tag'}
Truth table50%1/2
A
A= $args->{'tag'}
Aresult
11
00
280 }
281 }
282  
283 209 ▶sub _handle_plugins {
Subroutine _handle_plugins: called CC=3 SCAR=11.0
284 209 my( $this, $plugins, $plugin_type, $tags, $arg ) = @_;
285 209 my @nodes;
286  
287 209 ▶ foreach my $tag ( @{$tags || []} ) {
Condition50%1/2
$tags || []
l!l
2090
MC/DC0%0/1
$tags || []
$tags
Truth table50%1/2
A
A= $tags
Aresult
11
00
288 my $nodes = _handle_plugin( $this, $plugins, $plugin_type,
289 [ $arg, $tag->{any},
290 named => $tag->{named},
291 positional => $tag->{positional},
292 any_named_arguments => $tag->{named},
293 any_positional_arguments => $tag->{positional},
294 8 ] );
295  
296 8 push @nodes, @$nodes;
297 }
298  
299 209 return \@nodes;
300 }
301  
302 12 ▶sub _handle_plugin {
Subroutine _handle_plugin: called CC=5 SCAR=17.0
303 12 my( $this, $plugins, $plugin_type, $plugin_args ) = @_;
304 12 my $tag = $plugin_args->[1];
305  
306 12 ▶ foreach my $plugin ( @{$plugins->{$tag} || []}, @{$plugins->{_any_} || []} ) {
Condition50%2/4
$plugins->{$tag} || []
l!l
120
$plugins->{'_any_'} || []
l!l
012
MC/DC0%0/1
$plugins->{$tag} || []
$plugins->{$tag}
MC/DC0%0/1
$plugins->{'_any_'} || []
$plugins->{'_any_'}
Truth table50%1/2
A
A= $plugins->{$tag}
Aresult
11
00
Truth table50%1/2
A
A= $plugins->{'_any_'}
Aresult
11
00
307 12 my $method = $plugin->{method};
308  
309 12 my( $handled, @nodes ) = $plugin->{plugin}->$method( @$plugin_args );
310 12 ▶ return \@nodes if $handled;
Branch: 1 branch
BranchTrueFalseTotal
if $handled 12 0 12
311 }
312  
313 0 die "Unhandled $plugin_type annotation '$tag'";
314 }
315  
316 0 ▶sub current_file { $_[0]->{PARSER}->YYData->{LEX}{FILE} }
Subroutine current_file: not called CC=1 SCAR=6.9
Pod undocumented
317  
318 21 1;
« blib/lib/ExtUtils/XSpp/Node/Member.pm ↑ Summary blib/lib/ExtUtils/XSpp/Exception/object.pm »