PPI/Token/_QuoteEngine.pm

filter: Statementstmt 92.1% 70 / 76 Branchbran 71.0% 27 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 86.0% 105 / 122 CC 20 SCAR 17.4
CC
20
Cov
85%
_scan_for_brace_character
8.1
_scan_quote_like_operator_gap
6.3
_scan_for_unescaped_character
6.2
CRAP 5.7
« PPI/Statement/When.pm ↑ Summary PPI/Structure/For.pm »
1 package PPI::Token::_QuoteEngine;
2  
3 =pod
4  
5 =head1 NAME
6  
7 PPI::Token::_QuoteEngine - The PPI Quote Engine
8  
9 =head1 DESCRIPTION
10  
11 The C<PPI::Token::_QuoteEngine> package is designed hold functionality
12 for processing quotes and quote like operators, including regexes.
13 These have special requirements in parsing.
14  
15 The C<PPI::Token::_QuoteEngine> package itself provides various parsing
16 methods, which the L<PPI::Token::Quote>, L<PPI::Token::QuoteLike> and
17 L<PPI::Token::Regexp> can inherit from. In this sense, it serves
18 as a base class.
19  
20 =head2 Using this class
21  
22 I<(Refers only to internal uses. This class does not provide a
23 public interface)>
24  
25 To use these, you should initialize them as normal C<'$Class-E<gt>new'>,
26 and then call the 'fill' method, which will cause the specialised
27 parser to scan forwards and parse the quote to its end point.
28  
29 If -E<gt>fill returns true, finalise the token.
30  
31 =cut
32  
33 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 62 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 62 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
37 BEGIN {
38 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 }
40  
41  
42  
43  
44  
45 # Hook for the __TOKENIZER__on_char token call
46 sub __TOKENIZER__on_char {
47 13779 ▶ my $class = shift;
Subroutine __TOKENIZER__on_char: called CC=3 SCAR=11.2
48 13779 ▶ my $t = $_[0]->{token} ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'token'} ? : 13779 0 13779
49  
50 # Call the fill method to process the quote
51 13779 my $rv = $t->{token}->_fill( $t );
52 13779 ▶ return undef unless defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 13779 13779
53  
54 ## Doesn't support "end of file" indicator
55  
56 # Finalize the token and return 0 to tell the tokenizer
57 # to go to the next character.
58 13779 $t->_finalize_token;
59  
60 13779 0;
61 }
62  
63  
64  
65  
66  
67 #####################################################################
68 # Optimised character processors, used for quotes
69 # and quote like stuff, and accessible to the child classes
70  
71 # An outright scan, raw and fast.
72 # Searches for a particular character, not escaped, loading in new
73 # lines as needed.
74 # When called, we start at the current position.
75 # When leaving, the position should be set to the position
76 # of the character, NOT the one after it.
77 sub _scan_for_unescaped_character {
78 12662 ▶ my $class = shift;
Subroutine _scan_for_unescaped_character: called CC=6 SCAR=18.3
79 12662 my $t = shift;
80 12662 ▶ my $char = (length $_[0] == 1) ? quotemeta shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
length $_[0] == 1 ? : 12662 0 12662
81  
82 # Create the search regex.
83 # Same as above but with a negative look-behind assertion.
84 12662 my $search = qr/(.*?(?<!\\)(?:\\\\)*$char)/;
85  
86 12662 my $string = '';
87 12662 ▶ while ( exists $t->{line} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t->{"line"}) 14953 0 14953
88 # Get the search area for the current line
89 14953 pos $t->{line} = $t->{line_cursor};
90  
91 # Can we find a match on this line
92 14953 ▶ if ( $t->{line} =~ m/\G$search/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"line"} =~ /\G$search/cg) 11403 3550 14953
93 # Found the character on this line
94 11403 $t->{line_cursor} += length($1) - 1;
95 11403 return $string . $1;
96 }
97  
98 # Load in the next line
99 3550 $string .= substr $t->{line}, $t->{line_cursor};
100 3550 my $rv = $t->_fill_line('inscan');
101 3550 ▶ if ( $rv ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rv) { } 2291 1259 3550
elsif (defined $rv) { } 1259 0 1259
102 # Push to first character
103 2291 $t->{line_cursor} = 0;
104 } elsif ( defined $rv ) {
105 # We hit the End of File
106 1259 return \$string;
107 } else {
108 # Unexpected error
109 0 return undef;
110 }
111 }
112  
113 # We shouldn't be able to get here
114 0 return undef;
115 }
116  
117 # Scan for a close braced, and take into account both escaping,
118 # and open close bracket pairs in the string. When complete, the
119 # method leaves the line cursor on the LAST character found.
120 sub _scan_for_brace_character {
121 1446 ▶ my $class = shift;
Subroutine _scan_for_brace_character: called CC=8 SCAR=20.9
122 1446 my $t = shift;
123 1446 ▶ my $close_brace = $_[0] =~ /^(?:\>|\)|\}|\])$/ ? shift : Carp::confess(''); # return undef;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^(?:\>|\)|\}|\])$/ ? : 1446 0 1446
124 1446 my $open_brace = $close_brace;
125 1446 $open_brace =~ tr/\>\)\}\]/\<\(\{\[/;
126  
127 # Create the search string
128 1446 $close_brace = quotemeta $close_brace;
129 1446 $open_brace = quotemeta $open_brace;
130 1446 my $search = qr/\G(.*?(?<!\\)(?:\\\\)*(?:$open_brace|$close_brace))/;
131  
132 # Loop as long as we can get new lines
133 1446 my $string = '';
134 1446 my $depth = 1;
135 1446 ▶ while ( exists $t->{line} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t->{"line"}) 2605 0 2605
136 # Get the search area
137 2605 pos $t->{line} = $t->{line_cursor};
138  
139 # Look for a match
140 2605 ▶ unless ( $t->{line} =~ /$search/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t->{"line"} =~ /$search/cg) 726 1879 2605
141 # Load in the next line
142 726 $string .= substr( $t->{line}, $t->{line_cursor} );
143 726 my $rv = $t->_fill_line('inscan');
144 726 ▶ if ( $rv ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 593 133 726
145 # Push to first character
146 593 $t->{line_cursor} = 0;
147 593 next;
148 }
149 133 ▶ if ( defined $rv ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv) 133 0 133
150 # We hit the End of File
151 133 return \$string;
152 }
153  
154 # Unexpected error
155 0 return undef;
156 }
157  
158 # Add to the string
159 1879 $string .= $1;
160 1879 $t->{line_cursor} += length $1;
161  
162 # Alter the depth and continue if we aren't at the end
163 1879 ▶ $depth += ($1 =~ /$open_brace$/) ? 1 : -1 and next;
Branch: 2 branches
BranchTrueFalseTotal
$depth += $1 =~ /$open_brace$/ ? 1 : -1 and next 566 1313 1879
$1 =~ /$open_brace$/ ? : 319 1560 1879
164  
165 # Rewind the cursor by one character ( cludgy hack )
166 1313 $t->{line_cursor} -= 1;
167 1313 return $string;
168 }
169  
170 # Returning the string as a reference indicates EOF
171 0 \$string;
172 }
173  
174 # Find all spaces and comments, up to, but not including
175 # the first non-whitespace character.
176 #
177 # Although it doesn't return it, it leaves the cursor
178 # on the character following the gap
179 sub _scan_quote_like_operator_gap {
180 650 ▶ my $t = $_[1];
Subroutine _scan_quote_like_operator_gap: called CC=6 SCAR=18.4
181  
182 650 my $string = '';
183 650 ▶ while ( exists $t->{line} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t->{"line"}) 715 0 715
184 # Get the search area for the current line
185 715 pos $t->{line} = $t->{line_cursor};
186  
187 # Since this regex can match zero characters, it should always match
188 715 ▶ $t->{line} =~ /\G(\s*(?:\#.*)?)/gc or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$t->{"line"} =~ /\G(\s*(?:\#.*)?)/cg or return undef 0 715 715
189  
190 # Add the chars found to the string
191 715 $string .= $1;
192  
193 # Did we match the entire line?
194 715 ▶ unless ( $t->{line_cursor} + length $1 == length $t->{line} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t->{"line_cursor"} + length($1) == length $t->{"line"}) 465 250 715
195 # Partial line match, which means we are at
196 # the end of the gap. Fix the cursor and return
197 # the string.
198 465 $t->{line_cursor} += length $1;
199 465 return $string;
200 }
201  
202 # Load in the next line.
203 # If we reach the EOF, $t->{line} gets deleted,
204 # which is caught by the while.
205 250 my $rv = $t->_fill_line('inscan');
206 250 ▶ if ( $rv ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rv) { } 65 185 250
elsif (defined $rv) { } 185 0 185
207 # Set the cursor to the first character
208 65 $t->{line_cursor} = 0;
209 } elsif ( defined $rv ) {
210 # Returning the string as a reference indicates EOF
211 185 return \$string;
212 } else {
213 0 return undef;
214 }
215 }
216  
217 # Shouldn't be able to get here
218 0 return undef;
219 }
220  
221 62 1;
222  
223 =pod
224  
225 =head1 SUPPORT
226  
227 See the L<support section|PPI/SUPPORT> in the main module.
228  
229 =head1 AUTHOR
230  
231 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
232  
233 =head1 COPYRIGHT
234  
235 Copyright 2001 - 2011 Adam Kennedy.
236  
237 This program is free software; you can redistribute
238 it and/or modify it under the same terms as Perl itself.
239  
240 The full text of the license can be found in the
241 LICENSE file included with this module.
242  
243 =cut
« PPI/Statement/When.pm ↑ Summary PPI/Structure/For.pm »