PPI/Token/Whitespace.pm

filter: Statementstmt 98.5% 139 / 141 Branchbran 93.4% 86 / 92 Conditioncond 79.3% 69 / 87 MC/DCmcdc 46.6% 21 / 45 Subroutinesub 91.6% 11 / 12 Podpod 100% 2 / 2 totaltotal 86.5% 328 / 379 CC 81 SCAR 39.8
CC
81
Cov
92%
__TOKENIZER__on_char
71.0
__TOKENIZER__on_line_start
7.0
null
6.0
CRAP 53.7
↑ Summary PPI/Lexer.pm »
1 package PPI::Token::Whitespace;
2  
3 =pod
4  
5 =head1 NAME
6  
7 PPI::Token::Whitespace - Tokens representing ordinary white space
8  
9 =head1 INHERITANCE
10  
11 PPI::Token::Whitespace
12 isa PPI::Token
13 isa PPI::Element
14  
15 =head1 DESCRIPTION
16  
17 As a full "round-trip" parser, PPI records every last byte in a
18 file and ensure that it is included in the L<PPI::Document> object.
19  
20 This even includes whitespace. In fact, Perl documents are seen
21 as "floating in a sea of whitespace", and thus any document will
22 contain vast quantities of C<PPI::Token::Whitespace> objects.
23  
24 For the most part, you shouldn't notice them. Or at least, you
25 shouldn't B<have> to notice them.
26  
27 This means doing things like consistently using the "S for significant"
28 series of L<PPI::Node> and L<PPI::Element> methods to do things.
29  
30 If you want the nth child element, you should be using C<schild> rather
31 than C<child>, and likewise C<snext_sibling>, C<sprevious_sibling>, and
32 so on and so forth.
33  
34 =head1 METHODS
35  
36 Again, for the most part you should really B<not> need to do anything
37 very significant with whitespace.
38  
39 But there are a couple of convenience methods provided, beyond those
40 provided by the parent L<PPI::Token> and L<PPI::Element> classes.
41  
42 =cut
43  
44 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 62 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46 62 ▶use PPI::Token ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 62 ▶use vars qw{$VERSION @ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
49 BEGIN {
50 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 62 @ISA = 'PPI::Token';
52 }
53  
54 =pod
55  
56 =head2 null
57  
58 Because L<PPI> sees documents as sitting on a sort of substrate made of
59 whitespace, there are a couple of corner cases that get particularly
60 nasty if they don't find whitespace in certain places.
61  
62 Imagine walking down the beach to go into the ocean, and then quite
63 unexpectedly falling off the side of the planet. Well it's somewhat
64 equivalent to that, including the whole screaming death bit.
65  
66 The C<null> method is a convenience provided to get some internals
67 out of some of these corner cases.
68  
69 Specifically it create a whitespace token that represents nothing,
70 or at least the null string C<''>. It's a handy way to have some
71 "whitespace" right where you need it, without having to have any
72 actual characters.
73  
74 =cut
75  
76 62 my $null;
77  
78 sub null {
79 0 ▶ $null ||= $_[0]->new('');
Subroutine null: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/3
$null ||= $_[0]->new("")
l!l&&r!l&&!r
000
MC/DC0%0/2
$null ||= $_[0]->new("")
$null $_[0]->new("")
Truth table0%0/3
A ||= B
A= $nullB= $_[0]->new("")
ABresult
1X1
011
000
80 0 Clone::clone($null);
81 }
82  
83 ### XS -> PPI/XS.xs:_PPI_Token_Whitespace__significant 0.900+
84 sub significant() { '' }
85  
86 =pod
87  
88 =head2 tidy
89  
90 C<tidy> is a convenience method for removing unneeded whitespace.
91  
92 Specifically, it removes any whitespace from the end of a line.
93  
94 Note that this B<doesn't> include POD, where you may well need
95 to keep certain types of whitespace. The entire POD chunk lives
96 in its own L<PPI::Token::Pod> object.
97  
98 =cut
99  
100 sub tidy {
101 2 ▶ $_[0]->{content} =~ s/^\s+?(?>\n)//;
Subroutine tidy: called CC=1 SCAR=0.0
Pod documented
102 2 1;
103 }
104  
105  
106  
107  
108  
109 #####################################################################
110 # Parsing Methods
111  
112 # Build the class and commit maps
113 62 ▶use vars qw{ @CLASSMAP @COMMITMAP %MATCHWORD };
Subroutine BEGIN: called CC=1 SCAR=0.0
114 BEGIN {
115 62 ▶ @CLASSMAP = ();
Subroutine BEGIN: called CC=6 SCAR=17.9
116 62 @COMMITMAP = ();
117 62 foreach (
118 'a' .. 'u', 'w', 'y', 'z', 'A' .. 'Z', '_'
119 ) {
120 3162 $COMMITMAP[ord $_] = 'PPI::Token::Word';
121 }
122 62 foreach ( qw!; [ ] { } )! ) { $COMMITMAP[ord $_] = 'PPI::Token::Structure' }
123 62 foreach ( 0 .. 9 ) { $CLASSMAP[ord $_] = 'Number' }
124 62 foreach ( qw{= ? | + > . ! ~ ^} ) { $CLASSMAP[ord $_] = 'Operator' }
125 62 foreach ( qw{* $ @ & : %} ) { $CLASSMAP[ord $_] = 'Unknown' }
126  
127 # Miscellaneous remainder
128 62 $COMMITMAP[ord '#'] = 'PPI::Token::Comment';
129 62 $COMMITMAP[ord 'v'] = 'PPI::Token::Number::Version';
130 62 $CLASSMAP[ord ','] = 'PPI::Token::Operator';
131 62 $CLASSMAP[ord "'"] = 'Quote::Single';
132 62 $CLASSMAP[ord '"'] = 'Quote::Double';
133 62 $CLASSMAP[ord '`'] = 'QuoteLike::Backtick';
134 62 $CLASSMAP[ord '\\'] = 'Cast';
135 62 $CLASSMAP[ord '_'] = 'Word';
136 62 $CLASSMAP[9] = 'Whitespace'; # A horizontal tab
137 62 $CLASSMAP[10] = 'Whitespace'; # A newline
138 62 $CLASSMAP[12] = 'Whitespace'; # A form feed
139 62 $CLASSMAP[13] = 'Whitespace'; # A carriage return
140 62 $CLASSMAP[32] = 'Whitespace'; # A normal space
141  
142 # Words (functions and keywords) after which a following / is
143 # almost certainly going to be a regex
144 62 %MATCHWORD = map { $_ => 1 } qw{
145 return
146 split
147 if
148 unless
149 grep
150 map
151 };
152 }
153  
154  
155 sub __TOKENIZER__on_line_start {
156 48202 ▶ my $t = $_[1];
Subroutine __TOKENIZER__on_line_start: called CC=7 SCAR=19.5
157 48202 my $line = $t->{line};
158  
159 # Can we classify the entire line in one go
160 48202 ▶ if ( $line =~ /^\s*$/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($line =~ /^\s*$/) { } 6337 41865 48202
elsif ($line =~ /^\s*#/) { } 4239 37626 41865
elsif ($line =~ /^=(\w+)/) { } 743 36883 37626
elsif ($line =~ /^use v6\-alpha\;/) { } 2 36881 36883
161 # A whitespace line
162 6337 $t->_new_token( 'Whitespace', $line );
163 6337 return 0;
164  
165 } elsif ( $line =~ /^\s*#/ ) {
166 # A comment line
167 4239 $t->_new_token( 'Comment', $line );
168 4239 $t->_finalize_token;
169 4239 return 0;
170  
171 } elsif ( $line =~ /^=(\w+)/ ) {
172 # A Pod tag... change to pod mode
173 743 $t->_new_token( 'Pod', $line );
174 743 ▶ if ( $1 eq 'cut' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq 'cut') { } 0 743 743
175 # This is an error, but one we'll ignore
176 # Don't go into Pod mode, since =cut normally
177 # signals the end of Pod mode
178 } else {
179 743 $t->{class} = 'PPI::Token::Pod';
180 }
181 743 return 0;
182  
183 } elsif ( $line =~ /^use v6\-alpha\;/ ) {
184 # Indicates a Perl 6 block. Make the initial
185 # implementation just suck in the entire rest of the
186 # file.
187 2 my @perl6;
188 2 while ( 1 ) {
189 693 my $line6 = $t->_get_line;
190 693 ▶ last unless defined $line6;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line6 2 691 693
191 691 push @perl6, $line6;
192 }
193 2 push @{ $t->{perl6} }, join '', @perl6;
194  
195 # We only sucked in the block, we don't actually do
196 # anything to the "use v6..." line. So return as if
197 # we didn't find anything at all.
198 2 return 1;
199 }
200  
201 36881 1;
202 }
203  
204 sub __TOKENIZER__on_char {
205 351884 ▶ my $t = $_[1];
Subroutine __TOKENIZER__on_char: called CC=68 SCAR=42.6
206 351884 my $c = substr $t->{line}, $t->{line_cursor}, 1;
207 351884 my $char = ord $c;
208  
209 # Do we definitely know what something is?
210 351884 ▶ return $COMMITMAP[$char]->__TOKENIZER__commit($t) if $COMMITMAP[$char];
Branch: 1 branch
BranchTrueFalseTotal
if $PPI::Token::Whitespace::COMMITMAP[$char] 109495 242389 351884
211  
212 # Handle the simple option first
213 242389 ▶ return $CLASSMAP[$char] if $CLASSMAP[$char];
Branch: 1 branch
BranchTrueFalseTotal
if $PPI::Token::Whitespace::CLASSMAP[$char] 224863 17526 242389
214  
215 17526 ▶ if ( $char == 40 ) { # $char eq '('
Branch: 6 branches
BranchTrueFalseTotal
if ($char == 40) { } 7983 9543 17526
elsif ($char == 60) { } 1244 8299 9543
elsif ($char == 47) { } 998 7301 8299
elsif ($char == 120) { } 818 6483 7301
elsif ($char == 45) { } 6477 6 6483
elsif ($char >= 128) { } 6 0 6
216 # Finalise any whitespace token...
217 7983 ▶ $t->_finalize_token if $t->{token};
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"token"} 2836 5147 7983
218  
219 # Is this the beginning of a sub prototype?
220 # We are a sub prototype IF
221 # 1. The previous significant token is a bareword.
222 # 2. The one before that is the word 'sub'.
223 # 3. The one before that is a 'structure'
224  
225 # Get the three previous significant tokens
226 7983 my @tokens = $t->_previous_significant_tokens(3);
227  
228 # A normal subroutine declaration
229 7983 my $p1 = $tokens[1];
230 7983 my $p2 = $tokens[2];
231 7983 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens[0] and $tokens[0]->isa("PPI::Token::Word") and $p1 and $p1->isa("PPI::Token::Word") and $p1->content eq "sub" and !$p2 || $p2->isa("PPI::Token::Structure") || $p2->isa("PPI::Token::Whitespace") && $p2->content eq "") 115 7614 7729
Condition70%17/24
$tokens[0] and $tokens[0]->isa("PPI::Token::Word") and $p1 and $p1->isa("PPI::Token::Word") and $p1->content eq "sub" and !$p2 || $p2->isa("PPI::Token::Structure") || $p2->isa("PPI::Token::Whitespace") && $p2->content eq ""
!ll&&!rl&&r
76143690
$tokens[0] and $tokens[0]->isa("PPI::Token::Word") and $p1 and $p1->isa("PPI::Token::Word") and $p1->content eq "sub"
!ll&&!rl&&r
7140474369
$tokens[0] and $tokens[0]->isa("PPI::Token::Word") and $p1 and $p1->isa("PPI::Token::Word")
!ll&&!rl&&r
16265514843
$tokens[0] and $tokens[0]->isa("PPI::Token::Word") and $p1
!ll&&!rl&&r
14811456357
$tokens[0] and $tokens[0]->isa("PPI::Token::Word")
!ll&&!rl&&r
36711146502
not $p2 or $p2->isa("PPI::Token::Structure") or $p2->isa("PPI::Token::Whitespace") and $p2->content eq ""
l!l&&r!l&&!r
36900
not $p2 or $p2->isa("PPI::Token::Structure")
l!l&&r!l&&!r
2541150
$p2->isa("PPI::Token::Whitespace") and $p2->content eq ""
!ll&&!rl&&r
000
MC/DC0%0/9
$tokens[0] and $tokens[0]->isa("PPI::Token::Word") and $p1 and $p1->isa("PPI::Token::Word") and $p1->content eq "sub" and !$p2 || $p2->isa("PPI::Token::Structure") || $p2->isa("PPI::Token::Whitespace") && $p2->content eq ""
$tokens[0] $tokens[0]->isa("PPI::Token::Word") $p1 $p1->isa("PPI::Token::Word") $p1->content eq "sub" not $p2 $p2->isa("PPI::Token::Structure") $p2->isa("PPI::Token::Whitespace") $p2->content eq ""
Truth table50%5/10
A and B and C and D and E and not(F) or G or H and I
A= $tokens[0]B= $tokens[0]->isa("PPI::Token::Word")C= $p1D= $p1->isa("PPI::Token::Word")E= $p1->content eq "sub"F= not $p2G= $p2->isa("PPI::Token::Structure")H= $p2->isa("PPI::Token::Whitespace")I= $p2->content eq ""
ABCDEFGHIresult
0XXXXXXXX0
10XXXXXXX0
110XXXXXX0
1110XXXXX0
11110XXXX0
11111000X0
1111100100
111111XXX1
1111101XX1
1111100111
232 $tokens[0]
233 and
234 $tokens[0]->isa('PPI::Token::Word')
235 and
236 $p1
237 and
238 $p1->isa('PPI::Token::Word')
239 and
240 $p1->content eq 'sub'
241 and (
242 not $p2
243 or
244 $p2->isa('PPI::Token::Structure')
245 or (
246 $p2->isa('PPI::Token::Whitespace')
247 and
248 $p2->content eq ''
249 )
250 )
251 ) {
252 # This is a sub prototype
253 369 return 'Prototype';
254 }
255  
256 # A prototyped anonymous subroutine
257 7614 my $p0 = $tokens[0];
258 7614 ▶ if ( $p0 and $p0->isa('PPI::Token::Word') and $p0->content eq 'sub'
Branch: 1 branch
BranchTrueFalseTotal
if ($p0 and $p0->isa("PPI::Token::Word") and $p0->content eq "sub" and not $p1 && $p1->isa("PPI::Token::Operator") && $p1->content eq "->") 62 7552 7614
Condition86%13/15
$p0 and $p0->isa("PPI::Token::Word") and $p0->content eq "sub" and not $p1 && $p1->isa("PPI::Token::Operator") && $p1->content eq "->"
!ll&&!rl&&r
7552062
$p0 and $p0->isa("PPI::Token::Word") and $p0->content eq "sub"
!ll&&!rl&&r
1481607162
$p0 and $p0->isa("PPI::Token::Word")
!ll&&!rl&&r
36711146133
$p1 && $p1->isa('PPI::Token::Operator') && $p1->content eq '->'
!ll&&!rl&&r
5570
$p1 and $p1->isa("PPI::Token::Operator")
!ll&&!rl&&r
5237
MC/DC50%3/6
$p0 and $p0->isa("PPI::Token::Word") and $p0->content eq "sub" and not $p1 && $p1->isa("PPI::Token::Operator") && $p1->content eq "->"
$p0 $p0->isa("PPI::Token::Word") $p0->content eq "sub" $p1 $p1->isa("PPI::Token::Operator") $p1->content eq '->'
Truth table85%6/7
A and B and C and not(D and E && F)
A= $p0B= $p0->isa("PPI::Token::Word")C= $p0->content eq "sub"D= $p1E= $p1->isa("PPI::Token::Operator")F= $p1->content eq '->'
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1111110
1110XX1
11110X1
1111101
259 # Maybe it's invoking a method named 'sub'
260 and not ( $p1 and $p1->isa('PPI::Token::Operator') and $p1->content eq '->')
261 ) {
262 62 return 'Prototype';
263 }
264  
265 # This is a normal open bracket
266 7552 return 'Structure';
267  
268 } elsif ( $char == 60 ) { # $char eq '<'
269 # Finalise any whitespace token...
270 1244 ▶ $t->_finalize_token if $t->{token};
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"token"} 403 841 1244
271  
272 # This is either "less than" or "readline quote-like"
273 # Do some context stuff to guess which.
274 1244 my $prev = $t->_last_significant_token;
275  
276 # The most common group of less-thans are used like
277 # $foo < $bar
278 # 1 < $bar
279 # $#foo < $bar
280 1244 ▶ return 'Operator' if $prev and $prev->isa('PPI::Token::Symbol');
Branch: 1 branch
BranchTrueFalseTotal
if $prev and $prev->isa("PPI::Token::Symbol") 317 927 1244
Condition100%3/3
$prev and $prev->isa("PPI::Token::Symbol")
!ll&&!rl&&r
108819317
MC/DC100%2/2
$prev and $prev->isa("PPI::Token::Symbol")
$prev $prev->isa("PPI::Token::Symbol")
Truth table100%3/3
A and B
A= $prevB= $prev->isa("PPI::Token::Symbol")
ABresult
0X0
100
111
281 927 ▶ return 'Operator' if $prev and $prev->isa('PPI::Token::Magic');
Branch: 1 branch
BranchTrueFalseTotal
if $prev and $prev->isa("PPI::Token::Magic") 0 927 927
Condition66%2/3
$prev and $prev->isa("PPI::Token::Magic")
!ll&&!rl&&r
1088190
MC/DC0%0/2
$prev and $prev->isa("PPI::Token::Magic")
$prev $prev->isa("PPI::Token::Magic")
Truth table66%2/3
A and B
A= $prevB= $prev->isa("PPI::Token::Magic")
ABresult
0X0
100
111
282 927 ▶ return 'Operator' if $prev and $prev->isa('PPI::Token::Number');
Branch: 1 branch
BranchTrueFalseTotal
if $prev and $prev->isa("PPI::Token::Number") 32 895 927
Condition100%3/3
$prev and $prev->isa("PPI::Token::Number")
!ll&&!rl&&r
10878732
MC/DC100%2/2
$prev and $prev->isa("PPI::Token::Number")
$prev $prev->isa("PPI::Token::Number")
Truth table100%3/3
A and B
A= $prevB= $prev->isa("PPI::Token::Number")
ABresult
0X0
100
111
283 895 ▶ return 'Operator' if $prev and $prev->isa('PPI::Token::ArrayIndex');
Branch: 1 branch
BranchTrueFalseTotal
if $prev and $prev->isa("PPI::Token::ArrayIndex") 0 895 895
Condition66%2/3
$prev and $prev->isa("PPI::Token::ArrayIndex")
!ll&&!rl&&r
1087870
MC/DC0%0/2
$prev and $prev->isa("PPI::Token::ArrayIndex")
$prev $prev->isa("PPI::Token::ArrayIndex")
Truth table66%2/3
A and B
A= $prevB= $prev->isa("PPI::Token::ArrayIndex")
ABresult
0X0
100
111
284  
285 # If it is <<... it's a here-doc instead
286 895 my $next_char = substr( $t->{line}, $t->{line_cursor} + 1, 1 );
287 895 ▶ if ( $next_char eq '<' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_char eq "<") 269 626 895
288 269 return 'Operator';
289 }
290  
291 626 ▶ return 'Operator' if not $prev;
Branch: 1 branch
BranchTrueFalseTotal
unless $prev 79 547 626
292  
293 # The most common group of readlines are used like
294 # while ( <...> )
295 # while <>;
296 547 my $prec = $prev->content;
297 547 ▶ if ( $prev->isa('PPI::Token::Structure') and $prec eq '(' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Token::Structure") and $prec eq "(") 38 509 547
Condition100%3/3
$prev->isa("PPI::Token::Structure") and $prec eq "("
!ll&&!rl&&r
4248538
MC/DC100%2/2
$prev->isa("PPI::Token::Structure") and $prec eq "("
$prev->isa("PPI::Token::Structure") $prec eq "("
Truth table100%3/3
A and B
A= $prev->isa("PPI::Token::Structure")B= $prec eq "("
ABresult
0X0
100
111
298 38 return 'QuoteLike::Readline';
299 }
300 509 ▶ if ( $prev->isa('PPI::Token::Word') and $prec eq 'while' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Token::Word") and $prec eq "while") 3 506 509
Condition100%3/3
$prev->isa("PPI::Token::Word") and $prec eq "while"
!ll&&!rl&&r
3551513
MC/DC100%2/2
$prev->isa("PPI::Token::Word") and $prec eq "while"
$prev->isa("PPI::Token::Word") $prec eq "while"
Truth table100%3/3
A and B
A= $prev->isa("PPI::Token::Word")B= $prec eq "while"
ABresult
0X0
100
111
301 3 return 'QuoteLike::Readline';
302 }
303 506 ▶ if ( $prev->isa('PPI::Token::Operator') and $prec eq '=' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Token::Operator") and $prec eq "=") 27 479 506
Condition100%3/3
$prev->isa("PPI::Token::Operator") and $prec eq "="
!ll&&!rl&&r
30217727
MC/DC100%2/2
$prev->isa("PPI::Token::Operator") and $prec eq "="
$prev->isa("PPI::Token::Operator") $prec eq "="
Truth table100%3/3
A and B
A= $prev->isa("PPI::Token::Operator")B= $prec eq "="
ABresult
0X0
100
111
304 27 return 'QuoteLike::Readline';
305 }
306 479 ▶ if ( $prev->isa('PPI::Token::Operator') and $prec eq ',' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Token::Operator") and $prec eq ",") 24 455 479
Condition100%3/3
$prev->isa("PPI::Token::Operator") and $prec eq ","
!ll&&!rl&&r
30215324
MC/DC100%2/2
$prev->isa("PPI::Token::Operator") and $prec eq ","
$prev->isa("PPI::Token::Operator") $prec eq ","
Truth table100%3/3
A and B
A= $prev->isa("PPI::Token::Operator")B= $prec eq ","
ABresult
0X0
100
111
307 24 return 'QuoteLike::Readline';
308 }
309  
310 455 ▶ if ( $prev->isa('PPI::Token::Structure') and $prec eq '}' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Token::Structure") and $prec eq "}") 15 440 455
Condition100%3/3
$prev->isa("PPI::Token::Structure") and $prec eq "}"
!ll&&!rl&&r
3707015
MC/DC100%2/2
$prev->isa("PPI::Token::Structure") and $prec eq "}"
$prev->isa("PPI::Token::Structure") $prec eq "}"
Truth table100%3/3
A and B
A= $prev->isa("PPI::Token::Structure")B= $prec eq "}"
ABresult
0X0
100
111
311 # Could go either way... do a regex check
312 # $foo->{bar} < 2;
313 # grep { .. } <foo>;
314 15 pos $t->{line} = $t->{line_cursor};
315 15 ▶ if ( $t->{line} =~ m/\G<(?!\d)\w+>/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"line"} =~ /\G<(?!\d)\w+>/cg) 3 12 15
316 # Almost definitely readline
317 3 return 'QuoteLike::Readline';
318 }
319 }
320  
321 # Otherwise, we guess operator, which has been the default up
322 # until this more comprehensive section was created.
323 452 return 'Operator';
324  
325 } elsif ( $char == 47 ) { # $char eq '/'
326 # Finalise any whitespace token...
327 998 ▶ $t->_finalize_token if $t->{token};
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"token"} 321 677 998
328  
329 # This is either a "divided by" or a "start regex"
330 # Do some context stuff to guess ( ack ) which.
331 # Hopefully the guess will be good enough.
332 998 my $prev = $t->_last_significant_token;
333  
334 # Or as the very first thing in a file
335 998 ▶ return 'Regexp::Match' if not $prev;
Branch: 1 branch
BranchTrueFalseTotal
unless $prev 103 895 998
336  
337 895 my $prec = $prev->content;
338  
339 # Most times following an operator, we are a regex.
340 # This includes cases such as:
341 # , - As an argument in a list
342 # .. - The second condition in a flip flop
343 # =~ - A bound regex
344 # !~ - Ditto
345 895 ▶ return 'Regexp::Match' if $prev->isa('PPI::Token::Operator');
Branch: 1 branch
BranchTrueFalseTotal
if $prev->isa("PPI::Token::Operator") 413 482 895
346  
347 # After a symbol
348 482 ▶ return 'Operator' if $prev->isa('PPI::Token::Symbol');
Branch: 1 branch
BranchTrueFalseTotal
if $prev->isa("PPI::Token::Symbol") 55 427 482
349 427 ▶ if ( $prec eq ']' and $prev->isa('PPI::Token::Structure') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prec eq "]" and $prev->isa("PPI::Token::Structure")) 12 415 427
Condition66%2/3
$prec eq "]" and $prev->isa("PPI::Token::Structure")
!ll&&!rl&&r
415012
MC/DC50%1/2
$prec eq "]" and $prev->isa("PPI::Token::Structure")
$prec eq "]" $prev->isa("PPI::Token::Structure")
Truth table66%2/3
A and B
A= $prec eq "]"B= $prev->isa("PPI::Token::Structure")
ABresult
0X0
100
111
350 12 return 'Operator';
351 }
352  
353 # After another number
354 415 ▶ return 'Operator' if $prev->isa('PPI::Token::Number');
Branch: 1 branch
BranchTrueFalseTotal
if $prev->isa("PPI::Token::Number") 32 383 415
355  
356 # After going into scope/brackets
357 383 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Token::Structure") and $prec eq "(" || $prec eq "{" || $prec eq ";") 37 335 372
Condition100%9/9
$prev->isa("PPI::Token::Structure") and $prec eq "(" || $prec eq "{" || $prec eq ";"
!ll&&!rl&&r
3105122
$prec eq "(" or $prec eq "{" or $prec eq ";"
l!l&&r!l&&!r
341425
$prec eq "(" or $prec eq "{"
l!l&&r!l&&!r
112339
MC/DC50%2/4
$prev->isa("PPI::Token::Structure") and $prec eq "(" || $prec eq "{" || $prec eq ";"
$prev->isa("PPI::Token::Structure") $prec eq "(" $prec eq "{" $prec eq ";"
Truth table60%3/5
A and B or C or D
A= $prev->isa("PPI::Token::Structure")B= $prec eq "("C= $prec eq "{"D= $prec eq ";"
ABCDresult
0XXX0
10000
11XX1
101X1
10011
358 $prev->isa('PPI::Token::Structure')
359 and (
360 $prec eq '('
361 or
362 $prec eq '{'
363 or
364 $prec eq ';'
365 )
366 ) {
367 48 return 'Regexp::Match';
368 }
369  
370 # Functions and keywords
371 335 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($PPI::Token::Whitespace::MATCHWORD{$prec} and $prev->isa("PPI::Token::Word")) 67 268 335
Condition66%2/3
$PPI::Token::Whitespace::MATCHWORD{$prec} and $prev->isa("PPI::Token::Word")
!ll&&!rl&&r
268067
MC/DC50%1/2
$PPI::Token::Whitespace::MATCHWORD{$prec} and $prev->isa("PPI::Token::Word")
$PPI::Token::Whitespace::MATCHWORD{$prec} $prev->isa("PPI::Token::Word")
Truth table66%2/3
A and B
A= $PPI::Token::Whitespace::MATCHWORD{$prec}B= $prev->isa("PPI::Token::Word")
ABresult
0X0
100
111
372 $MATCHWORD{$prec}
373 and
374 $prev->isa('PPI::Token::Word')
375 ) {
376 67 return 'Regexp::Match';
377 }
378  
379 # What about the char after the slash? There's some things
380 # that would be highly illogical to see if it's an operator.
381 268 my $next_char = substr $t->{line}, $t->{line_cursor} + 1, 1;
382 268 ▶ if ( defined $next_char and length $next_char ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next_char and length $next_char) 268 0 268
Condition33%1/3
defined $next_char and length $next_char
!ll&&!rl&&r
00268
MC/DC0%0/2
defined $next_char and length $next_char
defined $next_char length $next_char
Truth table33%1/3
A and B
A= defined $next_charB= length $next_char
ABresult
0X0
100
111
383 268 ▶ if ( $next_char =~ /(?:\^|\[|\\)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_char =~ /(?:\^|\[|\\)/) 14 254 268
384 14 return 'Regexp::Match';
385 }
386 }
387  
388 # Otherwise... erm... assume operator?
389 # Add more tests here as potential cases come to light
390 254 return 'Operator';
391  
392 } elsif ( $char == 120 ) { # $char eq 'x'
393 # Could be a word, the x= operator, the x operator
394 # followed by whitespace, or the x operator without any
395 # space between itself and its operand, e.g.: '$a x3',
396 # which is the same as '$a x 3'. _current_x_is_operator
397 # assumes we have a complete 'x' token, but we don't
398 # yet. We may need to split this x character apart from
399 # what follows it.
400 818 ▶ if ( $t->_current_x_is_operator ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->_current_x_is_operator) 199 619 818
401 199 pos $t->{line} = $t->{line_cursor} + 1;
402 199 ▶ return 'Operator' if $t->{line} =~ m/\G(?:
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"line"} =~ /\G(?: \d # x op with no whitespace e.g. 'x3' | (?!( # negative lookahead => # not on left of fat comma | \w # not a word like "xyzzy" | \s # not x op plus whitespace )) )/cgx 126 73 199
403 \d # x op with no whitespace e.g. 'x3'
404 |
405 (?!( # negative lookahead
406 => # not on left of fat comma
407 |
408 \w # not a word like "xyzzy"
409 |
410 \s # not x op plus whitespace
411 ))
412 )/gcx;
413 }
414  
415 # Otherwise, commit like a normal bareword, including x
416 # operator followed by whitespace.
417 692 return PPI::Token::Word->__TOKENIZER__commit($t);
418  
419 } elsif ( $char == 45 ) { # $char eq '-'
420 # Look for an obvious operator operand context
421 6477 my $context = $t->_opcontext;
422 6477 ▶ if ( $context eq 'operator' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context eq 'operator') { } 4627 1850 6477
423 4627 return 'Operator';
424 } else {
425 # More logic needed
426 1850 return 'Unknown';
427 }
428  
429 } elsif ( $char >= 128 ) { # Outside ASCII
430 6 ▶ return 'PPI::Token::Word'->__TOKENIZER__commit($t) if $c =~ /\w/;
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /\w/ 5 1 6
431 1 ▶ return 'Whitespace' if $c =~ /\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /\s/ 0 1 1
432 }
433  
434  
435 # All the whitespaces are covered, so what to do
436 ### For now, die
437 1 PPI::Exception->throw("Encountered unexpected character '$char'");
438 }
439  
440 sub __TOKENIZER__on_line_end {
441 36698 ▶ $_[1]->_finalize_token if $_[1]->{token};
Subroutine __TOKENIZER__on_line_end: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[1]{"token"} 35094 1604 36698
442 }
443  
444 62 1;
445  
446 =pod
447  
448 =head1 SUPPORT
449  
450 See the L<support section|PPI/SUPPORT> in the main module.
451  
452 =head1 AUTHOR
453  
454 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
455  
456 =head1 COPYRIGHT
457  
458 Copyright 2001 - 2011 Adam Kennedy.
459  
460 This program is free software; you can redistribute
461 it and/or modify it under the same terms as Perl itself.
462  
463 The full text of the license can be found in the
464 LICENSE file included with this module.
465  
466 =cut
↑ Summary PPI/Lexer.pm »