PPI/Token/_QuoteEngine/Full.pm

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