PPI/Token/Whitespace.pm

filter: Statementstmt 98.1% 107 / 109 Branchbran 93.0% 80 / 86 Conditioncond 84.2% 91 / 108 MC/DCmcdc 46.9% 23 / 49 Subroutinesub 87.5% 7 / 8 Podpod 100% 2 / 2 totaltotal 85.6% 310 / 362 CC 80 SCAR 41.4
CC
80
Cov
92%
__TOKENIZER__on_char
74.6
__TOKENIZER__on_line_start
7.0
null
6.0
CRAP 62.6
↑ Summary PPI/Token/Unknown.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 63 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 63 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46 63 ▶use PPI::Token ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 63 our $VERSION = '1.260'; # VERSION
49  
50 63 our @ISA = "PPI::Token";
51  
52 =pod
53  
54 =head2 null
55  
56 Because L<PPI> sees documents as sitting on a sort of substrate made of
57 whitespace, there are a couple of corner cases that get particularly
58 nasty if they don't find whitespace in certain places.
59  
60 Imagine walking down the beach to go into the ocean, and then quite
61 unexpectedly falling off the side of the planet. Well it's somewhat
62 equivalent to that, including the whole screaming death bit.
63  
64 The C<null> method is a convenience provided to get some internals
65 out of some of these corner cases.
66  
67 Specifically it create a whitespace token that represents nothing,
68 or at least the null string C<''>. It's a handy way to have some
69 "whitespace" right where you need it, without having to have any
70 actual characters.
71  
72 =cut
73  
74 63 my $null;
75  
76 sub null {
77 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
78 0 Clone::clone($null);
79 }
80  
81 ### XS -> PPI/XS.xs:_PPI_Token_Whitespace__significant 0.900+
82 sub significant() { '' }
83  
84 =pod
85  
86 =head2 tidy
87  
88 C<tidy> is a convenience method for removing unneeded whitespace.
89  
90 Specifically, it removes any whitespace from the end of a line.
91  
92 Note that this B<doesn't> include POD, where you may well need
93 to keep certain types of whitespace. The entire POD chunk lives
94 in its own L<PPI::Token::Pod> object.
95  
96 =cut
97  
98 sub tidy {
99 2 ▶ $_[0]->{content} =~ s/^\s+?(?>\n)//;
Subroutine tidy: called CC=1 SCAR=0.0
Pod documented
100 2 1;
101 }
102  
103  
104  
105  
106  
107 #####################################################################
108 # Parsing Methods
109  
110 # Build the class and commit maps
111 my %COMMITMAP = (
112 3213 map( { ord $_ => 'PPI::Token::Word' } 'a' .. 'u', 'A' .. 'Z', qw" w y z _ " ), # no v or x
113 63 map( { ord $_ => 'PPI::Token::Structure' } qw" ; [ ] { } ) " ),
114 ord '#' => 'PPI::Token::Comment',
115 ord 'v' => 'PPI::Token::Number::Version',
116 );
117 my %CLASSMAP = (
118 630 map( { ord $_ => 'Number' } 0 .. 9 ),
119 567 map( { ord $_ => 'Operator' } qw" = ? | + > . ! ~ ^ " ),
120 63 map( { ord $_ => 'Unknown' } qw" * $ @ & : % " ),
121 ord ',' => 'PPI::Token::Operator',
122 ord "'" => 'Quote::Single',
123 ord '"' => 'Quote::Double',
124 ord '`' => 'QuoteLike::Backtick',
125 ord '\\' => 'Cast',
126 ord '_' => 'Word',
127 9 => 'Whitespace', # A horizontal tab
128 10 => 'Whitespace', # A newline
129 12 => 'Whitespace', # A form feed
130 13 => 'Whitespace', # A carriage return
131 32 => 'Whitespace', # A normal space
132 );
133  
134 # Words (functions and keywords) after which a following / is
135 # almost certainly going to be a regex
136 63 my %MATCHWORD = map { $_ => 1 } qw{
137 return
138 split
139 if
140 unless
141 grep
142 map
143 };
144  
145 sub __TOKENIZER__on_line_start {
146 49748 ▶ my $t = $_[1];
Subroutine __TOKENIZER__on_line_start: called CC=7 SCAR=19.5
147 49748 my $line = $t->{line};
148  
149 # Can we classify the entire line in one go
150 49748 ▶ if ( $line =~ /^\s*$/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($line =~ /^\s*$/) { } 6621 43127 49748
elsif ($line =~ /^\s*#/) { } 4287 38840 43127
elsif ($line =~ /^=(\w+)/) { } 737 38103 38840
elsif ($line =~ /^use v6\-alpha\;/) { } 2 38101 38103
151 # A whitespace line
152 6621 $t->_new_token( 'Whitespace', $line );
153 6621 return 0;
154  
155 } elsif ( $line =~ /^\s*#/ ) {
156 # A comment line
157 4287 $t->_new_token( 'Comment', $line );
158 4287 $t->_finalize_token;
159 4287 return 0;
160  
161 } elsif ( $line =~ /^=(\w+)/ ) {
162 # A Pod tag... change to pod mode
163 737 $t->_new_token( 'Pod', $line );
164 737 ▶ if ( $1 eq 'cut' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq 'cut') { } 0 737 737
165 # This is an error, but one we'll ignore
166 # Don't go into Pod mode, since =cut normally
167 # signals the end of Pod mode
168 } else {
169 737 $t->{class} = 'PPI::Token::Pod';
170 }
171 737 return 0;
172  
173 } elsif ( $line =~ /^use v6\-alpha\;/ ) {
174 # Indicates a Perl 6 block. Make the initial
175 # implementation just suck in the entire rest of the
176 # file.
177 2 my @perl6;
178 2 while ( 1 ) {
179 693 my $line6 = $t->_get_line;
180 693 ▶ last unless defined $line6;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line6 2 691 693
181 691 push @perl6, $line6;
182 }
183 2 push @{ $t->{perl6} }, join '', @perl6;
184  
185 # We only sucked in the block, we don't actually do
186 # anything to the "use v6..." line. So return as if
187 # we didn't find anything at all.
188 2 return 1;
189 }
190  
191 38101 1;
192 }
193  
194 sub __TOKENIZER__on_char {
195 364724 ▶ my $t = $_[1];
Subroutine __TOKENIZER__on_char: called CC=72 SCAR=43.1
196 364724 my $c = substr $t->{line}, $t->{line_cursor}, 1;
197 364724 my $char = ord $c;
198  
199 # Do we definitely know what something is?
200 364724 ▶ return $COMMITMAP{$char}->__TOKENIZER__commit($t) if $COMMITMAP{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $COMMITMAP{$char} 112630 252094 364724
201  
202 # Handle the simple option first
203 252094 ▶ return $CLASSMAP{$char} if $CLASSMAP{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $CLASSMAP{$char} 233969 18125 252094
204  
205 18125 ▶ if ( $char == 40 ) { # $char eq '('
Branch: 6 branches
BranchTrueFalseTotal
if ($char == 40) { } 8285 9840 18125
elsif ($char == 60) { } 1265 8575 9840
elsif ($char == 47) { } 1023 7552 8575
elsif ($char == 120) { } 811 6741 7552
elsif ($char == 45) { } 6735 6 6741
elsif ($char >= 128) { } 6 0 6
206 # Finalise any whitespace token...
207 8285 ▶ $t->_finalize_token if $t->{token};
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"token"} 2955 5330 8285
208  
209 # Is this the beginning of a sub prototype?
210 # We are a sub prototype IF
211 # 1. The previous significant token is a bareword.
212 # 2. The one before that is the word 'sub'.
213 # 3. The one before that is a 'structure'
214  
215 # Get the three previous significant tokens
216 8285 my @tokens = $t->_previous_significant_tokens(3);
217  
218 # A normal subroutine declaration
219 8285 my $p1 = $tokens[1];
220 8285 my $p2 = $tokens[2];
221 8285 ▶ 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 "" || $p2->isa("PPI::Token::Word") && $p2->content =~ /^(?:my|our|state)$/) 3 7913 7916
Condition80%24/30
$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 "" || $p2->isa("PPI::Token::Word") && $p2->content =~ /^(?:my|our|state)$/
!ll&&!rl&&r
79133657
$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
7433480372
$tokens[0] and $tokens[0]->isa("PPI::Token::Word") and $p1 and $p1->isa("PPI::Token::Word")
!ll&&!rl&&r
16755758852
$tokens[0] and $tokens[0]->isa("PPI::Token::Word") and $p1
!ll&&!rl&&r
15231526610
$tokens[0] and $tokens[0]->isa("PPI::Token::Word")
!ll&&!rl&&r
37911446762
not $p2 or $p2->isa("PPI::Token::Structure") or $p2->isa("PPI::Token::Whitespace") and $p2->content eq "" or $p2->isa("PPI::Token::Word") and $p2->content =~ /^(?:my|our|state)$/
l!l&&r!l&&!r
36930
not $p2 or $p2->isa("PPI::Token::Structure") or $p2->isa("PPI::Token::Whitespace") and $p2->content eq ""
l!l&&r!l&&!r
36903
not $p2 or $p2->isa("PPI::Token::Structure")
l!l&&r!l&&!r
2541153
$p2->isa("PPI::Token::Whitespace") and $p2->content eq ""
!ll&&!rl&&r
300
$p2->isa("PPI::Token::Word") and $p2->content =~ /^(?:my|our|state)$/
!ll&&!rl&&r
003
MC/DC45%5/11
$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 "" || $p2->isa("PPI::Token::Word") && $p2->content =~ /^(?:my|our|state)$/
$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 "" $p2->isa("PPI::Token::Word") $p2->content =~ /^(?:my|our|state)$/
Truth table42%6/14
A and B and C and D and E and not(F) or G or H and I or J and K
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 ""J= $p2->isa("PPI::Token::Word")K= $p2->content =~ /^(?:my|our|state)$/
ABCDEFGHIJKresult
0XXXXXXXXXX0
10XXXXXXXXX0
110XXXXXXXX0
1110XXXXXXX0
11110XXXXXX0
11111000X0X0
11111000X100
1111100100X0
111110010100
111111XXXXX1
1111101XXXX1
111110011XX1
11111000X111
111110010111
222 $tokens[0]
223 and
224 $tokens[0]->isa('PPI::Token::Word')
225 and
226 $p1
227 and
228 $p1->isa('PPI::Token::Word')
229 and
230 $p1->content eq 'sub'
231 and (
232 not $p2
233 or
234 $p2->isa('PPI::Token::Structure')
235 or (
236 $p2->isa('PPI::Token::Whitespace')
237 and
238 $p2->content eq ''
239 )
240 or (
241 # Lexical subroutine
242 $p2->isa('PPI::Token::Word')
243 and
244 $p2->content =~ /^(?:my|our|state)$/
245 )
246 )
247 ) {
248 # This is a sub prototype
249 372 return 'Prototype';
250 }
251  
252 # A prototyped anonymous subroutine
253 7913 my $p0 = $tokens[0];
254 7913 ▶ 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 7851 7913
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
7851062
$p0 and $p0->isa("PPI::Token::Word") and $p0->content eq "sub"
!ll&&!rl&&r
1523632862
$p0 and $p0->isa("PPI::Token::Word")
!ll&&!rl&&r
37911446390
$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
255 # Maybe it's invoking a method named 'sub'
256 and not ( $p1 and $p1->isa('PPI::Token::Operator') and $p1->content eq '->')
257 ) {
258 62 return 'Prototype';
259 }
260  
261 # This is a normal open bracket
262 7851 return 'Structure';
263  
264 } elsif ( $char == 60 ) { # $char eq '<'
265 # Finalise any whitespace token...
266 1265 ▶ $t->_finalize_token if $t->{token};
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"token"} 418 847 1265
267  
268 # This is either "less than" or "readline quote-like"
269 # Do some context stuff to guess which.
270 1265 my $prev = $t->_last_significant_token;
271  
272 # The most common group of less-thans are used like
273 # $foo < $bar
274 # 1 < $bar
275 # $#foo < $bar
276 1265 ▶ return 'Operator' if $prev and $prev->isa('PPI::Token::Symbol');
Branch: 1 branch
BranchTrueFalseTotal
if $prev and $prev->isa("PPI::Token::Symbol") 316 949 1265
Condition100%3/3
$prev and $prev->isa("PPI::Token::Symbol")
!ll&&!rl&&r
114835316
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
277 949 ▶ return 'Operator' if $prev and $prev->isa('PPI::Token::Magic');
Branch: 1 branch
BranchTrueFalseTotal
if $prev and $prev->isa("PPI::Token::Magic") 0 949 949
Condition66%2/3
$prev and $prev->isa("PPI::Token::Magic")
!ll&&!rl&&r
1148350
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
278 949 ▶ return 'Operator' if $prev and $prev->isa('PPI::Token::Number');
Branch: 1 branch
BranchTrueFalseTotal
if $prev and $prev->isa("PPI::Token::Number") 30 919 949
Condition100%3/3
$prev and $prev->isa("PPI::Token::Number")
!ll&&!rl&&r
11480530
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
279 919 ▶ return 'Operator' if $prev and $prev->isa('PPI::Token::ArrayIndex');
Branch: 1 branch
BranchTrueFalseTotal
if $prev and $prev->isa("PPI::Token::ArrayIndex") 0 919 919
Condition66%2/3
$prev and $prev->isa("PPI::Token::ArrayIndex")
!ll&&!rl&&r
1148050
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
280  
281 # If it is <<... it's a here-doc instead
282 919 my $next_char = substr( $t->{line}, $t->{line_cursor} + 1, 2 );
283 919 ▶ return 'Operator' if $next_char =~ /<[^>]/;
Branch: 1 branch
BranchTrueFalseTotal
if $next_char =~ /<[^>]/ 284 635 919
284  
285 635 ▶ return 'Operator' if not $prev;
Branch: 1 branch
BranchTrueFalseTotal
unless $prev 85 550 635
286  
287 # The most common group of readlines are used like
288 # while ( <...> )
289 # while <>;
290 550 my $prec = $prev->content;
291 550 ▶ return 'QuoteLike::Readline'
Branch: 1 branch
BranchTrueFalseTotal
if $prev->isa("PPI::Token::Structure") and $prec eq "(" or $prev->isa("PPI::Token::Structure") and $prec eq ";" or $prev->isa("PPI::Token::Word") and $prec eq "while" or $prev->isa("PPI::Token::Operator") and $prec eq "=" or $prev->isa("PPI::Token::Operator") and $prec eq "," 48 445 493
Condition100%27/27
$prev->isa("PPI::Token::Structure") and $prec eq "(" or $prev->isa("PPI::Token::Structure") and $prec eq ";" or $prev->isa("PPI::Token::Word") and $prec eq "while" or $prev->isa("PPI::Token::Operator") and $prec eq "=" or $prev->isa("PPI::Token::Operator") and $prec eq ","
l!l&&r!l&&!r
8337430
$prev->isa("PPI::Token::Structure") and $prec eq "(" or $prev->isa("PPI::Token::Structure") and $prec eq ";" or $prev->isa("PPI::Token::Word") and $prec eq "while" or $prev->isa("PPI::Token::Operator") and $prec eq "="
l!l&&r!l&&!r
5726467
$prev->isa("PPI::Token::Structure") and $prec eq "(" or $prev->isa("PPI::Token::Structure") and $prec eq ";" or $prev->isa("PPI::Token::Word") and $prec eq "while"
l!l&&r!l&&!r
516493
$prev->isa("PPI::Token::Structure") and $prec eq "(" or $prev->isa("PPI::Token::Structure") and $prec eq ";"
l!l&&r!l&&!r
3714499
$prev->isa("PPI::Token::Structure") and $prec eq "("
!ll&&!rl&&r
4427137
$prev->isa("PPI::Token::Structure") and $prec eq ";"
!ll&&!rl&&r
4425714
$prev->isa("PPI::Token::Word") and $prec eq "while"
!ll&&!rl&&r
3281656
$prev->isa("PPI::Token::Operator") and $prec eq "="
!ll&&!rl&&r
29317426
$prev->isa("PPI::Token::Operator") and $prec eq ","
!ll&&!rl&&r
29315222
MC/DC50%5/10
$prev->isa("PPI::Token::Structure") and $prec eq "(" or $prev->isa("PPI::Token::Structure") and $prec eq ";" or $prev->isa("PPI::Token::Word") and $prec eq "while" or $prev->isa("PPI::Token::Operator") and $prec eq "=" or $prev->isa("PPI::Token::Operator") and $prec eq ","
$prev->isa("PPI::Token::Structure") $prec eq "(" $prev->isa("PPI::Token::Structure") $prec eq ";" $prev->isa("PPI::Token::Word") $prec eq "while" $prev->isa("PPI::Token::Operator") $prec eq "=" $prev->isa("PPI::Token::Operator") $prec eq ","
Truth table9%6/63
A and B or C and D or E and F or G and H or I and J
A= $prev->isa("PPI::Token::Structure")B= $prec eq "("C= $prev->isa("PPI::Token::Structure")D= $prec eq ";"E= $prev->isa("PPI::Token::Word")F= $prec eq "while"G= $prev->isa("PPI::Token::Operator")H= $prec eq "="I= $prev->isa("PPI::Token::Operator")J= $prec eq ","
ABCDEFGHIJresult
11XXXXXXXX1
0X11XXXXXX1
1011XXXXXX1
0X0X11XXXX1
0X1011XXXX1
100X11XXXX1
101011XXXX1
0X0X0X11XX1
0X0X1011XX1
0X100X11XX1
0X101011XX1
100X0X11XX1
100X1011XX1
10100X11XX1
10101011XX1
0X0X0X0X111
0X0X0X10111
0X0X100X111
0X0X1010111
0X100X0X111
0X100X10111
0X10100X111
0X101010111
100X0X0X111
100X0X10111
100X100X111
100X1010111
10100X0X111
10100X10111
1010100X111
10101010111
0X0X0X0X0X0
0X0X0X0X100
0X0X0X100X0
0X0X0X10100
0X0X100X0X0
0X0X100X100
0X0X10100X0
0X0X1010100
0X100X0X0X0
0X100X0X100
0X100X100X0
0X100X10100
0X10100X0X0
0X10100X100
0X1010100X0
0X101010100
100X0X0X0X0
100X0X0X100
100X0X100X0
100X0X10100
100X100X0X0
100X100X100
100X10100X0
100X1010100
10100X0X0X0
10100X0X100
10100X100X0
10100X10100
1010100X0X0
1010100X100
101010100X0
10101010100
292 if ( $prev->isa('PPI::Token::Structure') and $prec eq '(' )
293 or ( $prev->isa('PPI::Token::Structure') and $prec eq ';' )
294 or ( $prev->isa('PPI::Token::Word') and $prec eq 'while' )
295 or ( $prev->isa('PPI::Token::Operator') and $prec eq '=' )
296 or ( $prev->isa('PPI::Token::Operator') and $prec eq ',' );
297  
298 445 ▶ if ( $prev->isa('PPI::Token::Structure') and $prec eq '}' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Token::Structure") and $prec eq "}") 11 434 445
Condition100%3/3
$prev->isa("PPI::Token::Structure") and $prec eq "}"
!ll&&!rl&&r
3884611
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
299 # Could go either way... do a regex check
300 # $foo->{bar} < 2;
301 # grep { .. } <foo>;
302 11 pos $t->{line} = $t->{line_cursor};
303 11 ▶ if ( $t->{line} =~ m/\G<(?!\d)\w+>/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"line"} =~ /\G<(?!\d)\w+>/cg) 3 8 11
304 # Almost definitely readline
305 3 return 'QuoteLike::Readline';
306 }
307 }
308  
309 # Otherwise, we guess operator, which has been the default up
310 # until this more comprehensive section was created.
311 442 return 'Operator';
312  
313 } elsif ( $char == 47 ) { # $char eq '/'
314 # Finalise any whitespace token...
315 1023 ▶ $t->_finalize_token if $t->{token};
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"token"} 354 669 1023
316  
317 # This is either a "divided by" or a "start regex"
318 # Do some context stuff to guess ( ack ) which.
319 # Hopefully the guess will be good enough.
320 1023 my $prev = $t->_last_significant_token;
321  
322 # Or as the very first thing in a file
323 1023 ▶ return 'Regexp::Match' if not $prev;
Branch: 1 branch
BranchTrueFalseTotal
unless $prev 97 926 1023
324  
325 926 my $prec = $prev->content;
326  
327 # Most times following an operator, we are a regex.
328 # This includes cases such as:
329 # , - As an argument in a list
330 # .. - The second condition in a flip flop
331 # =~ - A bound regex
332 # !~ - Ditto
333 926 ▶ return 'Regexp::Match' if $prev->isa('PPI::Token::Operator');
Branch: 1 branch
BranchTrueFalseTotal
if $prev->isa("PPI::Token::Operator") 460 466 926
334  
335 # After a symbol
336 466 ▶ return 'Operator' if $prev->isa('PPI::Token::Symbol');
Branch: 1 branch
BranchTrueFalseTotal
if $prev->isa("PPI::Token::Symbol") 71 395 466
337 395 ▶ if ( $prec eq ']' and $prev->isa('PPI::Token::Structure') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prec eq "]" and $prev->isa("PPI::Token::Structure")) 10 385 395
Condition66%2/3
$prec eq "]" and $prev->isa("PPI::Token::Structure")
!ll&&!rl&&r
385010
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
338 10 return 'Operator';
339 }
340  
341 # After another number
342 385 ▶ return 'Operator' if $prev->isa('PPI::Token::Number');
Branch: 1 branch
BranchTrueFalseTotal
if $prev->isa("PPI::Token::Number") 31 354 385
343  
344 # After going into scope/brackets
345 354 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->isa("PPI::Token::Structure") and $prec eq "(" || $prec eq "{" || $prec eq ";") 39 296 335
Condition100%9/9
$prev->isa("PPI::Token::Structure") and $prec eq "(" || $prec eq "{" || $prec eq ";"
!ll&&!rl&&r
2607420
$prec eq "(" or $prec eq "{" or $prec eq ";"
l!l&&r!l&&!r
441436
$prec eq "(" or $prec eq "{"
l!l&&r!l&&!r
192550
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
346 $prev->isa('PPI::Token::Structure')
347 and (
348 $prec eq '('
349 or
350 $prec eq '{'
351 or
352 $prec eq ';'
353 )
354 ) {
355 58 return 'Regexp::Match';
356 }
357  
358 # Functions and keywords
359 296 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($MATCHWORD{$prec} and $prev->isa("PPI::Token::Word")) 69 227 296
Condition66%2/3
$MATCHWORD{$prec} and $prev->isa("PPI::Token::Word")
!ll&&!rl&&r
227069
MC/DC50%1/2
$MATCHWORD{$prec} and $prev->isa("PPI::Token::Word")
$MATCHWORD{$prec} $prev->isa("PPI::Token::Word")
Truth table66%2/3
A and B
A= $MATCHWORD{$prec}B= $prev->isa("PPI::Token::Word")
ABresult
0X0
100
111
360 $MATCHWORD{$prec}
361 and
362 $prev->isa('PPI::Token::Word')
363 ) {
364 69 return 'Regexp::Match';
365 }
366  
367 # What about the char after the slash? There's some things
368 # that would be highly illogical to see if it's an operator.
369 227 my $next_char = substr $t->{line}, $t->{line_cursor} + 1, 1;
370 227 ▶ if ( defined $next_char and length $next_char ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next_char and length $next_char) 227 0 227
Condition33%1/3
defined $next_char and length $next_char
!ll&&!rl&&r
00227
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
371 227 ▶ if ( $next_char =~ /(?:\^|\[|\\)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_char =~ /(?:\^|\[|\\)/) 12 215 227
372 12 return 'Regexp::Match';
373 }
374 }
375  
376 # Otherwise... erm... assume operator?
377 # Add more tests here as potential cases come to light
378 215 return 'Operator';
379  
380 } elsif ( $char == 120 ) { # $char eq 'x'
381 # Could be a word, the x= operator, the x operator
382 # followed by whitespace, or the x operator without any
383 # space between itself and its operand, e.g.: '$a x3',
384 # which is the same as '$a x 3'. _current_x_is_operator
385 # assumes we have a complete 'x' token, but we don't
386 # yet. We may need to split this x character apart from
387 # what follows it.
388 811 ▶ if ( $t->_current_x_is_operator ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->_current_x_is_operator) 208 603 811
389 208 pos $t->{line} = $t->{line_cursor} + 1;
390 208 ▶ 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 120 88 208
391 \d # x op with no whitespace e.g. 'x3'
392 |
393 (?!( # negative lookahead
394 => # not on left of fat comma
395 |
396 \w # not a word like "xyzzy"
397 |
398 \s # not x op plus whitespace
399 ))
400 )/gcx;
401 }
402  
403 # Otherwise, commit like a normal bareword, including x
404 # operator followed by whitespace.
405 691 return PPI::Token::Word->__TOKENIZER__commit($t);
406  
407 } elsif ( $char == 45 ) { # $char eq '-'
408 # Look for an obvious operator operand context
409 6735 my $context = $t->_opcontext;
410 6735 ▶ if ( $context eq 'operator' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context eq 'operator') { } 4880 1855 6735
411 4880 return 'Operator';
412 } else {
413 # More logic needed
414 1855 return 'Unknown';
415 }
416  
417 } elsif ( $char >= 128 ) { # Outside ASCII
418 6 ▶ return 'PPI::Token::Word'->__TOKENIZER__commit($t) if $c =~ /\w/;
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /\w/ 5 1 6
419 1 ▶ return 'Whitespace' if $c =~ /\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $c =~ /\s/ 0 1 1
420 }
421  
422  
423 # All the whitespaces are covered, so what to do
424 ### For now, die
425 1 PPI::Exception->throw("Encountered unexpected character '$char'");
426 }
427  
428 sub __TOKENIZER__on_line_end {
429 37679 ▶ $_[1]->_finalize_token if $_[1]->{token};
Subroutine __TOKENIZER__on_line_end: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[1]{"token"} 36098 1581 37679
430 }
431  
432 63 1;
433  
434 =pod
435  
436 =head1 SUPPORT
437  
438 See the L<support section|PPI/SUPPORT> in the main module.
439  
440 =head1 AUTHOR
441  
442 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
443  
444 =head1 COPYRIGHT
445  
446 Copyright 2001 - 2011 Adam Kennedy.
447  
448 This program is free software; you can redistribute
449 it and/or modify it under the same terms as Perl itself.
450  
451 The full text of the license can be found in the
452 LICENSE file included with this module.
453  
454 =cut
↑ Summary PPI/Token/Unknown.pm »