PPI/Token/_QuoteEngine.pm

filter: Statementstmt 91.7% 67 / 73 Branchbran 71.0% 27 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 85.4% 100 / 117 CC 20 SCAR 18.0
CC
20
Cov
85%
_scan_for_brace_character
8.1
_scan_quote_like_operator_gap
6.3
_scan_for_unescaped_character
6.2
CRAP 6.0
« PPI/Structure/For.pm ↑ Summary PPI/Statement/Scheduled.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 63 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 63 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 63 our $VERSION = '1.260'; # VERSION
37  
38  
39  
40  
41  
42 # Hook for the __TOKENIZER__on_char token call
43 sub __TOKENIZER__on_char {
44 14153 ▶ my $class = shift;
Subroutine __TOKENIZER__on_char: called CC=3 SCAR=11.2
45 14153 ▶ my $t = $_[0]->{token} ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'token'} ? : 14153 0 14153
46  
47 # Call the fill method to process the quote
48 14153 my $rv = $t->{token}->_fill( $t );
49 14153 ▶ return undef unless defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 14153 14153
50  
51 ## Doesn't support "end of file" indicator
52  
53 # Finalize the token and return 0 to tell the tokenizer
54 # to go to the next character.
55 14153 $t->_finalize_token;
56  
57 14153 0;
58 }
59  
60  
61  
62  
63  
64 #####################################################################
65 # Optimised character processors, used for quotes
66 # and quote like stuff, and accessible to the child classes
67  
68 # An outright scan, raw and fast.
69 # Searches for a particular character, not escaped, loading in new
70 # lines as needed.
71 # When called, we start at the current position.
72 # When leaving, the position should be set to the position
73 # of the character, NOT the one after it.
74 sub _scan_for_unescaped_character {
75 13229 ▶ my $class = shift;
Subroutine _scan_for_unescaped_character: called CC=6 SCAR=18.3
76 13229 my $t = shift;
77 13229 ▶ my $char = (length $_[0] == 1) ? quotemeta shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
length $_[0] == 1 ? : 13229 0 13229
78  
79 # Create the search regex.
80 # Same as above but with a negative look-behind assertion.
81 13229 my $search = qr/(.*?(?<!\\)(?:\\\\)*$char)/;
82  
83 13229 my $string = '';
84 13229 ▶ while ( exists $t->{line} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t->{"line"}) 15560 0 15560
85 # Get the search area for the current line
86 15560 pos $t->{line} = $t->{line_cursor};
87  
88 # Can we find a match on this line
89 15560 ▶ if ( $t->{line} =~ m/\G$search/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"line"} =~ /\G$search/cg) 11990 3570 15560
90 # Found the character on this line
91 11990 $t->{line_cursor} += length($1) - 1;
92 11990 return $string . $1;
93 }
94  
95 # Load in the next line
96 3570 $string .= substr $t->{line}, $t->{line_cursor};
97 3570 my $rv = $t->_fill_line('inscan');
98 3570 ▶ if ( $rv ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rv) { } 2331 1239 3570
elsif (defined $rv) { } 1239 0 1239
99 # Push to first character
100 2331 $t->{line_cursor} = 0;
101 } elsif ( defined $rv ) {
102 # We hit the End of File
103 1239 return \$string;
104 } else {
105 # Unexpected error
106 0 return undef;
107 }
108 }
109  
110 # We shouldn't be able to get here
111 0 return undef;
112 }
113  
114 # Scan for a close braced, and take into account both escaping,
115 # and open close bracket pairs in the string. When complete, the
116 # method leaves the line cursor on the LAST character found.
117 sub _scan_for_brace_character {
118 1271 ▶ my $class = shift;
Subroutine _scan_for_brace_character: called CC=8 SCAR=20.9
119 1271 my $t = shift;
120 1271 ▶ my $close_brace = $_[0] =~ /^(?:\>|\)|\}|\])$/ ? shift : Carp::confess(''); # return undef;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^(?:\>|\)|\}|\])$/ ? : 1271 0 1271
121 1271 my $open_brace = $close_brace;
122 1271 $open_brace =~ tr/\>\)\}\]/\<\(\{\[/;
123  
124 # Create the search string
125 1271 $close_brace = quotemeta $close_brace;
126 1271 $open_brace = quotemeta $open_brace;
127 1271 my $search = qr/\G(.*?(?<!\\)(?:\\\\)*(?:$open_brace|$close_brace))/;
128  
129 # Loop as long as we can get new lines
130 1271 my $string = '';
131 1271 my $depth = 1;
132 1271 ▶ while ( exists $t->{line} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t->{"line"}) 2467 0 2467
133 # Get the search area
134 2467 pos $t->{line} = $t->{line_cursor};
135  
136 # Look for a match
137 2467 ▶ unless ( $t->{line} =~ /$search/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t->{"line"} =~ /$search/cg) 734 1733 2467
138 # Load in the next line
139 734 $string .= substr( $t->{line}, $t->{line_cursor} );
140 734 my $rv = $t->_fill_line('inscan');
141 734 ▶ if ( $rv ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 599 135 734
142 # Push to first character
143 599 $t->{line_cursor} = 0;
144 599 next;
145 }
146 135 ▶ if ( defined $rv ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv) 135 0 135
147 # We hit the End of File
148 135 return \$string;
149 }
150  
151 # Unexpected error
152 0 return undef;
153 }
154  
155 # Add to the string
156 1733 $string .= $1;
157 1733 $t->{line_cursor} += length $1;
158  
159 # Alter the depth and continue if we aren't at the end
160 1733 ▶ $depth += ($1 =~ /$open_brace$/) ? 1 : -1 and next;
Branch: 2 branches
BranchTrueFalseTotal
$depth += $1 =~ /$open_brace$/ ? 1 : -1 and next 597 1136 1733
$1 =~ /$open_brace$/ ? : 343 1390 1733
161  
162 # Rewind the cursor by one character ( cludgy hack )
163 1136 $t->{line_cursor} -= 1;
164 1136 return $string;
165 }
166  
167 # Returning the string as a reference indicates EOF
168 0 \$string;
169 }
170  
171 # Find all spaces and comments, up to, but not including
172 # the first non-whitespace character.
173 #
174 # Although it doesn't return it, it leaves the cursor
175 # on the character following the gap
176 sub _scan_quote_like_operator_gap {
177 639 ▶ my $t = $_[1];
Subroutine _scan_quote_like_operator_gap: called CC=6 SCAR=18.4
178  
179 639 my $string = '';
180 639 ▶ while ( exists $t->{line} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $t->{"line"}) 701 0 701
181 # Get the search area for the current line
182 701 pos $t->{line} = $t->{line_cursor};
183  
184 # Since this regex can match zero characters, it should always match
185 701 ▶ $t->{line} =~ /\G(\s*(?:\#.*)?)/gc or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$t->{"line"} =~ /\G(\s*(?:\#.*)?)/cg or return undef 0 701 701
186  
187 # Add the chars found to the string
188 701 $string .= $1;
189  
190 # Did we match the entire line?
191 701 ▶ unless ( $t->{line_cursor} + length $1 == length $t->{line} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t->{"line_cursor"} + length($1) == length $t->{"line"}) 458 243 701
192 # Partial line match, which means we are at
193 # the end of the gap. Fix the cursor and return
194 # the string.
195 458 $t->{line_cursor} += length $1;
196 458 return $string;
197 }
198  
199 # Load in the next line.
200 # If we reach the EOF, $t->{line} gets deleted,
201 # which is caught by the while.
202 243 my $rv = $t->_fill_line('inscan');
203 243 ▶ if ( $rv ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rv) { } 62 181 243
elsif (defined $rv) { } 181 0 181
204 # Set the cursor to the first character
205 62 $t->{line_cursor} = 0;
206 } elsif ( defined $rv ) {
207 # Returning the string as a reference indicates EOF
208 181 return \$string;
209 } else {
210 0 return undef;
211 }
212 }
213  
214 # Shouldn't be able to get here
215 0 return undef;
216 }
217  
218 63 1;
219  
220 =pod
221  
222 =head1 SUPPORT
223  
224 See the L<support section|PPI/SUPPORT> in the main module.
225  
226 =head1 AUTHOR
227  
228 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
229  
230 =head1 COPYRIGHT
231  
232 Copyright 2001 - 2011 Adam Kennedy.
233  
234 This program is free software; you can redistribute
235 it and/or modify it under the same terms as Perl itself.
236  
237 The full text of the license can be found in the
238 LICENSE file included with this module.
239  
240 =cut
« PPI/Structure/For.pm ↑ Summary PPI/Statement/Scheduled.pm »