PPI/Token/_QuoteEngine/Full.pm

filter: Statementstmt 100% 176 / 176 Branchbran 84.0% 74 / 88 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 1 totaltotal 94.5% 262 / 277 CC 47 SCAR 22.2
CC
47
Cov
95%
_fill_braced
16.0
_fill
11.0
_fill_normal
8.0
CRAP 9.2
« PPI/Structure.pm ↑ Summary PPI/Statement.pm »
1 package PPI::Token::_QuoteEngine::Full;
2  
3 # Full quote engine
4  
5 63 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 63 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 63 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 63 ▶use PPI::Token::_QuoteEngine ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 63 our $VERSION = '1.260'; # VERSION
11  
12 63 our @ISA = 'PPI::Token::_QuoteEngine';
13  
14 # Prototypes for the different braced sections
15 63 my %SECTIONS = (
16 '(' => { type => '()', _close => ')' },
17 '<' => { type => '<>', _close => '>' },
18 '[' => { type => '[]', _close => ']' },
19 '{' => { type => '{}', _close => '}' },
20 );
21  
22 # For each quote type, the extra fields that should be set.
23 # This should give us faster initialization.
24 63 my %QUOTES = (
25 'q' => { operator => 'q', braced => undef, separator => undef, _sections => 1 },
26 'qq' => { operator => 'qq', braced => undef, separator => undef, _sections => 1 },
27 'qx' => { operator => 'qx', braced => undef, separator => undef, _sections => 1 },
28 'qw' => { operator => 'qw', braced => undef, separator => undef, _sections => 1 },
29 'qr' => { operator => 'qr', braced => undef, separator => undef, _sections => 1, modifiers => 1 },
30 'm' => { operator => 'm', braced => undef, separator => undef, _sections => 1, modifiers => 1 },
31 's' => { operator => 's', braced => undef, separator => undef, _sections => 2, modifiers => 1 },
32 'tr' => { operator => 'tr', braced => undef, separator => undef, _sections => 2, modifiers => 1 },
33  
34 # Y is the little-used variant of tr
35 'y' => { operator => 'y', braced => undef, separator => undef, _sections => 2, modifiers => 1 },
36  
37 '/' => { operator => undef, braced => 0, separator => '/', _sections => 1, modifiers => 1 },
38  
39 # Angle brackets quotes mean "readline(*FILEHANDLE)"
40 '<' => { operator => undef, braced => 1, separator => undef, _sections => 1, },
41  
42 # The final ( and kind of depreciated ) "first match only" one is not
43 # used yet, since I'm not sure on the context differences between
44 # this and the trinary operator, but it's here for completeness.
45 '?' => { operator => undef, braced => 0, separator => '?', _sections => 1, modifiers => 1 },
46 );
47  
48  
49 sub new {
50 3214 ▶ my $class = shift;
Subroutine new: called CC=7 SCAR=19.7
Pod undocumented
51 3214 ▶ my $init = defined $_[0]
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 3214 0 3214
52 ? shift
53 : Carp::croak("::Full->new called without init string");
54  
55 # Create the token
56 ### This manual SUPER'ing ONLY works because none of
57 ### Token::Quote, Token::QuoteLike and Token::Regexp
58 ### implement a new function of their own.
59 3214 ▶ my $self = PPI::Token::new( $class, $init ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $self = PPI::Token::new($class, $init) or return undef 0 3214 3214
60  
61 # Do we have a prototype for the initializer? If so, add the extra fields
62 3214 ▶ my $options = $QUOTES{$init} or return $self->_error(
Branch: 1 branch
BranchTrueFalseTotal
my $options = $QUOTES{$init} or return $self->_error("Unknown quote type '${init}'") 0 3214 3214
63 "Unknown quote type '$init'"
64 );
65 3214 foreach ( keys %$options ) {
66 14707 $self->{$_} = $options->{$_};
67 }
68  
69 # Set up the modifiers hash if needed
70 3214 ▶ $self->{modifiers} = {} if $self->{modifiers};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"modifiers"} 1851 1363 3214
71  
72 # Handle the special < base
73 3214 ▶ if ( $init eq '<' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($init eq "<") 108 3106 3214
74 108 $self->{sections}->[0] = Clone::clone( $SECTIONS{'<'} );
75 }
76  
77 3214 $self;
78 }
79  
80 sub _fill {
81 3214 ▶ my $class = shift;
Subroutine _fill: called CC=11 SCAR=24.0
82 3214 my $t = shift;
83 my $self = $t->{token}
84 3214 ▶ or Carp::croak("::Full->_fill called without current token");
Branch: 1 branch
BranchTrueFalseTotal
my $self = $t->{"token"} or Carp::croak("::Full->_fill called without current token") 0 3214 3214
85  
86 # Load in the operator stuff if needed
87 3214 ▶ if ( $self->{operator} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"operator"}) 2410 804 3214
88 # In an operator based quote-like, handle the gap between the
89 # operator and the opening separator.
90 2410 ▶ if ( substr( $t->{line}, $t->{line_cursor}, 1 ) =~ /\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($t->{"line"}, $t->{"line_cursor"}, 1) =~ /\s/) 523 1887 2410
91 # Go past the gap
92 523 my $gap = $self->_scan_quote_like_operator_gap( $t );
93 523 ▶ return undef unless defined $gap;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gap 0 523 523
94 523 ▶ if ( ref $gap ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $gap) 178 345 523
95 # End of file
96 178 $self->{content} .= $$gap;
97 178 return 0;
98 }
99 345 $self->{content} .= $gap;
100 }
101  
102 # The character we are now on is the separator. Capture,
103 # and advance into the first section.
104 2232 my $sep = substr( $t->{line}, $t->{line_cursor}++, 1 );
105 2232 $self->{content} .= $sep;
106  
107 # Determine if these are normal or braced type sections
108 2232 ▶ if ( my $section = $SECTIONS{$sep} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $section = $SECTIONS{$sep}) { } 1046 1186 2232
109 1046 $self->{braced} = 1;
110 1046 $self->{sections}->[0] = Clone::clone($section);
111 } else {
112 1186 $self->{braced} = 0;
113 1186 $self->{separator} = $sep;
114 }
115 }
116  
117 # Parse different based on whether we are normal or braced
118 my $rv = $self->{braced}
119 3036 ▶ ? $self->_fill_braced($t)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'braced'} ? : 1154 1882 3036
120 : $self->_fill_normal($t);
121 3036 ▶ return $rv if !$rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 668 2368 3036
122  
123 # Return now unless it has modifiers ( i.e. s/foo//eieio )
124 2368 ▶ return 1 unless $self->{modifiers};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"modifiers"} 1185 1183 2368
125  
126 # Check for modifiers
127 1183 my $char;
128 1183 my $len = 0;
129 1183 ▶ while ( ($char = substr( $t->{line}, $t->{line_cursor} + 1, 1 )) =~ /[^\W\d_]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($char = substr($t->{"line"}, $t->{"line_cursor"} + 1, 1)) =~ /[^\W\d_]/) 440 1183 1623
130 440 $len++;
131 440 $self->{content} .= $char;
132 440 $self->{modifiers}->{lc $char} = 1;
133 440 $t->{line_cursor}++;
134 }
135 }
136  
137 # Handle the content parsing path for normally separated
138 sub _fill_normal {
139 1882 ▶ my $self = shift;
Subroutine _fill_normal: called CC=8 SCAR=20.8
140 1882 my $t = shift;
141  
142 # Get the content up to the next separator
143 1882 my $string = $self->_scan_for_unescaped_character( $t, $self->{separator} );
144 1882 ▶ return undef unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 1882 1882
145 1882 ▶ if ( ref $string ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $string) 422 1460 1882
146 # End of file
147 422 ▶ if ( length($$string) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$string > 1) { } 278 144 422
148 # Complete the properties for the first section
149 278 my $str = $$string;
150 278 chop $str;
151 $self->{sections}->[0] = {
152 278 position => length($self->{content}),
153 size => length($$string) - 1,
154 type => "$self->{separator}$self->{separator}",
155 };
156 278 $self->{_sections} = 1;
157 } else {
158 # No sections at all
159 144 $self->{sections} = [ ];
160 144 $self->{_sections} = 0;
161 }
162 422 $self->{content} .= $$string;
163 422 return 0;
164 }
165  
166 # Complete the properties of the first section
167 $self->{sections}->[0] = {
168 position => length $self->{content},
169 1460 size => length($string) - 1,
170 type => "$self->{separator}$self->{separator}",
171 };
172 1460 $self->{content} .= $string;
173  
174 # We are done if there is only one section
175 1460 ▶ return 1 if $self->{_sections} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_sections"} == 1 1077 383 1460
176  
177 # There are two sections.
178  
179 # Advance into the next section
180 383 $t->{line_cursor}++;
181  
182 # Get the content up to the end separator
183 383 $string = $self->_scan_for_unescaped_character( $t, $self->{separator} );
184 383 ▶ return undef unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 383 383
185 383 ▶ if ( ref $string ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $string) 76 307 383
186 # End of file
187 76 ▶ if ( length($$string) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$string > 1) { } 72 4 76
188 # Complete the properties for the second section
189 72 my $str = $$string;
190 72 chop $str;
191 $self->{sections}->[1] = {
192 72 position => length($self->{content}),
193 size => length($$string) - 1,
194 type => "$self->{separator}$self->{separator}",
195 };
196 } else {
197 # No sections at all
198 4 $self->{_sections} = 1;
199 }
200 76 $self->{content} .= $$string;
201 76 return 0;
202 }
203  
204 # Complete the properties of the second section
205 $self->{sections}->[1] = {
206 307 position => length($self->{content}),
207 size => length($string) - 1
208 };
209 307 $self->{content} .= $string;
210  
211 307 1;
212 }
213  
214 # Handle content parsing for matching brace separated
215 sub _fill_braced {
216 1154 ▶ my $self = shift;
Subroutine _fill_braced: called CC=16 SCAR=27.7
217 1154 my $t = shift;
218  
219 # Get the content up to the close character
220 1154 my $section = $self->{sections}->[0];
221 1154 my $brace_str = $self->_scan_for_brace_character( $t, $section->{_close} );
222 1154 ▶ return undef unless defined $brace_str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $brace_str 0 1154 1154
223 1154 ▶ if ( ref $brace_str ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $brace_str) 128 1026 1154
224 # End of file
225 128 ▶ if ( length($$brace_str) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$brace_str > 1) { } 80 48 128
226 # Complete the properties for the first section
227 80 my $str = $$brace_str;
228 80 chop $str;
229 $self->{sections}->[0] = {
230 position => length($self->{content}),
231 size => length($$brace_str) - 1,
232 type => $section->{type},
233 80 };
234 80 $self->{_sections} = 1;
235 } else {
236 # No sections at all
237 48 $self->{sections} = [ ];
238 48 $self->{_sections} = 0;
239 }
240 128 $self->{content} .= $$brace_str;
241 128 return 0;
242 }
243  
244 # Complete the properties of the first section
245 1026 $section->{position} = length $self->{content};
246 1026 $section->{size} = length($brace_str) - 1;
247 1026 $self->{content} .= $brace_str;
248 1026 delete $section->{_close};
249  
250 # We are done if there is only one section
251 1026 ▶ return 1 if $self->{_sections} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_sections"} == 1 862 164 1026
252  
253 # There are two sections.
254  
255 # Is there a gap between the sections.
256 164 my $char = substr( $t->{line}, ++$t->{line_cursor}, 1 );
257 164 ▶ if ( $char =~ /\s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char =~ /\s/) 116 48 164
258 # Go past the gap
259 116 my $gap_str = $self->_scan_quote_like_operator_gap( $t );
260 116 ▶ return undef unless defined $gap_str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gap_str 0 116 116
261 116 ▶ if ( ref $gap_str ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $gap_str) 3 113 116
262 # End of file
263 3 $self->{content} .= $$gap_str;
264 3 return 0;
265 }
266 113 $self->{content} .= $gap_str;
267 113 $char = substr( $t->{line}, $t->{line_cursor}, 1 );
268 }
269  
270 161 $section = $SECTIONS{$char};
271  
272 161 ▶ if ( $section ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($section) { } 117 44 161
elsif ($char =~ / \A [^\w\s] \z /msx) { } 25 19 44
273 # It's a brace
274  
275 # Initialize the second section
276 117 $self->{content} .= $char;
277 117 $section = { %$section };
278  
279 # Advance into the second section
280 117 $t->{line_cursor}++;
281  
282 # Get the content up to the close character
283 117 $brace_str = $self->_scan_for_brace_character( $t, $section->{_close} );
284 117 ▶ return undef unless defined $brace_str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $brace_str 0 117 117
285 117 ▶ if ( ref $brace_str ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $brace_str) { } 7 110 117
286 # End of file
287 7 ▶ if ( length($$brace_str) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$brace_str > 1) { } 5 2 7
288 # Complete the properties for the second section
289 5 my $str = $$brace_str;
290 5 chop $str;
291 $self->{sections}->[1] = {
292 position => length($self->{content}),
293 size => length($$brace_str) - 1,
294 type => $section->{type},
295 5 };
296 5 $self->{_sections} = 2;
297 } else {
298 # No sections at all
299 2 $self->{_sections} = 1;
300 }
301 7 $self->{content} .= $$brace_str;
302 7 return 0;
303 } else {
304 # Complete the properties for the second section
305 $self->{sections}->[1] = {
306 position => length($self->{content}),
307 size => length($brace_str) - 1,
308 type => $section->{type},
309 110 };
310 110 $self->{content} .= $brace_str;
311 }
312 } elsif ( $char =~ m/ \A [^\w\s] \z /smx ) {
313 # It is some other delimiter (weird, but possible)
314  
315 # Add the delimiter to the content.
316 25 $self->{content} .= $char;
317  
318 # Advance into the next section
319 25 $t->{line_cursor}++;
320  
321 # Get the content up to the end separator
322 25 my $string = $self->_scan_for_unescaped_character( $t, $char );
323 25 ▶ return undef unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 25 25
324 25 ▶ if ( ref $string ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $string) 13 12 25
325 # End of file
326 13 ▶ if ( length($$string) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$string > 1) { } 5 8 13
327 # Complete the properties for the second section
328 5 my $str = $$string;
329 5 chop $str;
330 $self->{sections}->[1] = {
331 5 position => length($self->{content}),
332 size => length($$string) - 1,
333 type => "$char$char",
334 };
335 } else {
336 # Only the one section
337 8 $self->{_sections} = 1;
338 }
339 13 $self->{content} .= $$string;
340 13 return 0;
341 }
342  
343 # Complete the properties of the second section
344 $self->{sections}->[1] = {
345 12 position => length($self->{content}),
346 size => length($string) - 1,
347 type => "$char$char",
348 };
349 12 $self->{content} .= $string;
350  
351 } else {
352  
353 # Error, it has to be a delimiter of some sort.
354 # Although this will result in a REALLY illegal regexp,
355 # we allow it anyway.
356  
357 # Create a null second section
358 $self->{sections}->[1] = {
359 19 position => length($self->{content}),
360 size => 0,
361 type => '',
362 };
363  
364 # Attach an error to the token and move on
365 19 $self->{_error} = "No second section of regexp, or does not start with a balanced character";
366  
367 # Roll back the cursor one char and return signalling end of regexp
368 19 $t->{line_cursor}--;
369 19 return 0;
370 }
371  
372 122 1;
373 }
374  
375  
376  
377  
378  
379 #####################################################################
380 # Additional methods to find out about the quote
381  
382 # In a scalar context, get the number of sections
383 # In an array context, get the section information
384 sub _sections {
385 26 ▶ wantarray ? @{$_[0]->{sections}} : scalar @{$_[0]->{sections}}
Subroutine _sections: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 16 10 26
386 }
387  
388 # Get a section's content
389 sub _section_content {
390 504 ▶ my $self = shift;
Subroutine _section_content: called CC=3 SCAR=11.0
391 504 my $i = shift;
392 504 ▶ $self->{sections} or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"sections"} or return 0 504 504
393 504 ▶ my $section = $self->{sections}->[$i] or return;
Branch: 1 branch
BranchTrueFalseTotal
my $section = $self->{"sections"}[$i] or return 4 500 504
394 500 return substr( $self->content, $section->{position}, $section->{size} );
395 }
396  
397 # Get the modifiers if any.
398 # In list context, return the modifier hash.
399 # In scalar context, clone the hash and return a reference to it.
400 # If there are no modifiers, simply return.
401 sub _modifiers {
402 7 ▶ my $self = shift;
Subroutine _modifiers: called CC=3 SCAR=11.0
403 7 ▶ $self->{modifiers} or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"modifiers"} or return 0 7 7
404 7 ▶ wantarray and return %{ $self->{modifiers} };
Branch: 1 branch
BranchTrueFalseTotal
wantarray and return %{$self->{"modifiers"};} 5 2 7
405 2 return +{ %{ $self->{modifiers} } };
406 }
407  
408 # Get the delimiters, or at least give it a good try to get them.
409 sub _delimiters {
410 492 ▶ my $self = shift;
Subroutine _delimiters: called CC=4 SCAR=13.9
411 492 ▶ $self->{sections} or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"sections"} or return 0 492 492
412 492 my @delims;
413 492 foreach my $sect ( @{ $self->{sections} } ) {
414 494 ▶ if ( exists $sect->{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $sect->{'type'}) { } 493 1 494
415 493 push @delims, $sect->{type};
416 } else {
417 1 my $content = $self->content;
418 push @delims,
419 substr( $content, $sect->{position} - 1, 1 ) .
420 1 substr( $content, $sect->{position} + $sect->{size}, 1 );
421 }
422 }
423 492 return @delims;
424 }
425  
426 63 1;
427  
428 =pod
429  
430 =head1 SUPPORT
431  
432 See the L<support section|PPI/SUPPORT> in the main module.
433  
434 =head1 AUTHOR
435  
436 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
437  
438 =head1 COPYRIGHT
439  
440 Copyright 2001 - 2011 Adam Kennedy.
441  
442 This program is free software; you can redistribute
443 it and/or modify it under the same terms as Perl itself.
444  
445 The full text of the license can be found in the
446 LICENSE file included with this module.
447  
448 =cut
« PPI/Structure.pm ↑ Summary PPI/Statement.pm »