PPI/Token/HereDoc.pm

filter: Statementstmt 97.5% 78 / 80 Branchbran 97.7% 43 / 44 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 87.5% 7 / 8 Podpod 100% 2 / 2 totaltotal 95.8% 138 / 144 CC 28 SCAR 29.8
CC
28
Cov
97%
__TOKENIZER__on_char
26.0
heredoc
2.0
terminator
2.0
CRAP 19.7
« PPI/Token/Word.pm ↑ Summary PPI/Normal/Standard.pm »
1 package PPI::Token::HereDoc;
2  
3 =pod
4  
5 =head1 NAME
6  
7 PPI::Token::HereDoc - Token class for the here-doc
8  
9 =head1 INHERITANCE
10  
11 PPI::Token::HereDoc
12 isa PPI::Token
13 isa PPI::Element
14  
15 =head1 DESCRIPTION
16  
17 Here-docs are incredibly handy when writing Perl, but incredibly tricky
18 when parsing it, primarily because they don't follow the general flow of
19 input.
20  
21 They jump ahead and nab lines directly off the input buffer. Whitespace
22 and newlines may not matter in most Perl code, but they matter in here-docs.
23  
24 They are also tricky to store as an object. They look sort of like an
25 operator and a string, but they don't act like it. And they have a second
26 section that should be something like a separate token, but isn't because a
27 string can span from above the here-doc content to below it.
28  
29 So when parsing, this is what we do.
30  
31 Firstly, the PPI::Token::HereDoc object, does not represent the C<<< << >>>
32 operator, or the "END_FLAG", or the content, or even the terminator.
33  
34 It represents all of them at once.
35  
36 The token itself has only the declaration part as its "content".
37  
38 # This is what the content of a HereDoc token is
39 <<FOO
40
41 # Or this
42 <<"FOO"
43
44 # Or even this
45 << 'FOO'
46  
47 That is, the "operator", any whitespace separator, and the quoted or bare
48 terminator. So when you call the C<content> method on a HereDoc token, you
49 get '<< "FOO"'.
50  
51 As for the content and the terminator, when treated purely in "content" terms
52 they do not exist.
53  
54 The content is made available with the C<heredoc> method, and the name of
55 the terminator with the C<terminator> method.
56  
57 To make things work in the way you expect, PPI has to play some games
58 when doing line/column location calculation for tokens, and also during
59 the content parsing and generation processes.
60  
61 Documents cannot simply by recreated by stitching together the token
62 contents, and involve a somewhat more expensive procedure, but the extra
63 expense should be relatively negligible unless you are doing huge
64 quantities of them.
65  
66 Please note that due to the immature nature of PPI in general, we expect
67 C<HereDocs> to be a rich (bad) source of corner-case bugs for quite a while,
68 but for the most part they should more or less DWYM.
69  
70 =head2 Comparison to other string types
71  
72 Although technically it can be considered a quote, for the time being
73 C<HereDocs> are being treated as a completely separate C<Token> subclass,
74 and will not be found in a search for L<PPI::Token::Quote> or
75 L<PPI::Token::QuoteLike objects>.
76  
77 This may change in the future, with it most likely to end up under
78 QuoteLike.
79  
80 =head1 METHODS
81  
82 Although it has the standard set of C<Token> methods, C<HereDoc> objects
83 have a relatively large number of unique methods all of their own.
84  
85 =cut
86  
87 63 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 63 ▶use PPI::Token ();
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 63 our $VERSION = '1.260'; # VERSION
91  
92 63 our @ISA = "PPI::Token";
93  
94  
95  
96  
97  
98 #####################################################################
99 # PPI::Token::HereDoc Methods
100  
101 =pod
102  
103 =head2 heredoc
104  
105 The C<heredoc> method is the authoritative method for accessing the contents
106 of the C<HereDoc> object.
107  
108 It returns the contents of the here-doc as a list of newline-terminated
109 strings. If called in scalar context, it returns the number of lines in
110 the here-doc, B<excluding> the terminator line.
111  
112 =cut
113  
114 sub heredoc {
115 wantarray
116 211 ? @{shift->{_heredoc}}
117 237 ▶ : scalar @{shift->{_heredoc}};
Subroutine heredoc: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 211 26 237
118 }
119  
120 =pod
121  
122 =head2 terminator
123  
124 The C<terminator> method returns the name of the terminating string for the
125 here-doc.
126  
127 Returns the terminating string as an unescaped string (in the rare case
128 the terminator has an escaped quote in it).
129  
130 =cut
131  
132 sub terminator {
133 0 ▶ shift->{_terminator};
Subroutine terminator: not called CC=1 SCAR=6.9
Pod documented
134 }
135  
136 sub _is_terminator {
137 715 ▶ my ( $self, $terminator, $line, $indented ) = @_;
Subroutine _is_terminator: called CC=2 SCAR=6.9
138 715 ▶ if ( $indented ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indented) { } 51 664 715
139 51 return $line =~ /^\s*$terminator$/;
140 } else {
141 664 return $line eq $terminator;
142 }
143 }
144  
145 sub _indent {
146 15 ▶ my ( $self, $token ) = @_;
Subroutine _indent: called CC=1 SCAR=0.0
147 15 my ($indent) = $token->{_terminator_line} =~ /^(\s*)/;
148 15 return $indent;
149 }
150  
151 sub _is_match_indent {
152 15 ▶ my ( $self, $token, $indent ) = @_;
Subroutine _is_match_indent: called CC=1 SCAR=0.0
153 15 return (grep { /^$indent/ } @{$token->{_heredoc}}) == @{$token->{_heredoc}};
154 }
155  
156  
157  
158  
159 #####################################################################
160 # Tokenizer Methods
161  
162 # Parse in the entire here-doc in one call
163 sub __TOKENIZER__on_char {
164 216 ▶ my ( $self, $t ) = @_;
Subroutine __TOKENIZER__on_char: called CC=26 SCAR=32.6
165  
166 # We are currently located on the first char after the <<
167  
168 # Handle the most common form first for simplicity and speed reasons
169 ### FIXME - This regex, and this method in general, do not yet allow
170 ### for the null here-doc, which terminates at the first
171 ### empty line.
172 216 pos $t->{line} = $t->{line_cursor};
173  
174 216 ▶ if ( $t->{line} !~ m/\G( ~? \s* (?: "[^"]*" | '[^']*' | `[^`]*` | \\?\w+ ) )/gcx ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t->{"line"} =~ /\G( ~? \s* (?: "[^"]*" | '[^']*' | `[^`]*` | \\?\w+ ) )/cgx) 3 213 216
175 # Degenerate to a left-shift operation
176 3 $t->{token}->set_class('Operator');
177 3 return $t->_finalize_token->__TOKENIZER__on_char( $t );
178 }
179  
180 # Add the rest of the token, work out what type it is,
181 # and suck in the content until the end.
182 213 my $token = $t->{token};
183 213 $token->{content} .= $1;
184 213 $t->{line_cursor} += length $1;
185  
186 # Find the terminator, clean it up and determine
187 # the type of here-doc we are dealing with.
188 213 my $content = $token->{content};
189 213 ▶ if ( $content =~ /^\<\<(~?)(\w+)$/ ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($content =~ /^\<\<(~?)(\w+)$/) { } 133 80 213
elsif ($content =~ /^\<\<(~?)\s*\'(.*)\'$/) { } 37 43 80
elsif ($content =~ /^\<\<(~?)\s*\"(.*)\"$/) { } 14 29 43
elsif ($content =~ /^\<\<(~?)\s*\`(.*)\`$/) { } 9 20 29
elsif ($content =~ /^\<\<(~?)\\(\w+)$/) { } 20 0 20
190 # Bareword
191 133 $token->{_mode} = 'interpolate';
192 133 ▶ $token->{_indented} = 1 if $1 eq '~';
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "~" 4 129 133
193 133 $token->{_terminator} = $2;
194  
195 } elsif ( $content =~ /^\<\<(~?)\s*\'(.*)\'$/ ) {
196 # ''-quoted literal
197 37 $token->{_mode} = 'literal';
198 37 ▶ $token->{_indented} = 1 if $1 eq '~';
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "~" 3 34 37
199 37 $token->{_terminator} = $2;
200 37 $token->{_terminator} =~ s/\\'/'/g;
201  
202 } elsif ( $content =~ /^\<\<(~?)\s*\"(.*)\"$/ ) {
203 # ""-quoted literal
204 14 $token->{_mode} = 'interpolate';
205 14 ▶ $token->{_indented} = 1 if $1 eq '~';
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "~" 3 11 14
206 14 $token->{_terminator} = $2;
207 14 $token->{_terminator} =~ s/\\"/"/g;
208  
209 } elsif ( $content =~ /^\<\<(~?)\s*\`(.*)\`$/ ) {
210 # ``-quoted command
211 9 $token->{_mode} = 'command';
212 9 ▶ $token->{_indented} = 1 if $1 eq '~';
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "~" 3 6 9
213 9 $token->{_terminator} = $2;
214 9 $token->{_terminator} =~ s/\\`/`/g;
215  
216 } elsif ( $content =~ /^\<\<(~?)\\(\w+)$/ ) {
217 # Legacy forward-slashed bareword
218 20 $token->{_mode} = 'literal';
219 20 ▶ $token->{_indented} = 1 if $1 eq '~';
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "~" 2 18 20
220 20 $token->{_terminator} = $2;
221  
222 } else {
223 # WTF?
224 0 return undef;
225 }
226  
227 # Suck in the HEREDOC
228 213 $token->{_heredoc} = \my @heredoc;
229 213 my $terminator = $token->{_terminator} . "\n";
230 213 ▶ while ( defined( my $line = $t->_get_line ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $t->_get_line)) 676 68 744
231 676 ▶ if ( $self->_is_terminator( $terminator, $line, $token->{_indented} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_terminator($terminator, $line, $token->{"_indented"})) 145 531 676
232 # Keep the actual termination line for consistency
233 # when we are re-assembling the file
234 145 $token->{_terminator_line} = $line;
235  
236 145 ▶ if ( $token->{_indented} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"_indented"}) 9 136 145
237 9 my $indent = $self->_indent( $token );
238 # Indentation of here-doc doesn't match delimiter
239 9 ▶ unless ( $self->_is_match_indent( $token, $indent ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_match_indent($token, $indent)) 1 8 9
240 1 push @heredoc, $line;
241 1 last;
242 }
243  
244 8 s/^$indent// for @heredoc, $token->{_terminator_line};
245 }
246  
247 # The HereDoc is now fully parsed
248 144 return $t->_finalize_token->__TOKENIZER__on_char( $t );
249 }
250  
251 # Add the line
252 531 push @heredoc, $line;
253 }
254  
255 # End of file.
256 # Error: Didn't reach end of here-doc before end of file.
257  
258 # If the here-doc block is not empty, look at the last line to determine if
259 # the here-doc terminator is missing a newline (which Perl would fail to
260 # compile but is easy to detect) or if the here-doc block was just not
261 # terminated at all (which Perl would fail to compile as well).
262 69 $token->{_terminator_line} = undef;
263 69 ▶ if ( @heredoc and defined $heredoc[-1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@heredoc and defined $heredoc[-1]) 39 30 69
Condition66%2/3
@heredoc and defined $heredoc[-1]
!ll&&!rl&&r
30039
MC/DC50%1/2
@heredoc and defined $heredoc[-1]
@heredoc defined $heredoc[-1]
Truth table66%2/3
A and B
A= @heredocB= defined $heredoc[-1]
ABresult
0X0
100
111
264 # See PPI::Tokenizer, the algorithm there adds a space at the end of the
265 # document that we need to make sure we remove.
266 39 ▶ if ( $t->{source_eof_chop} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"source_eof_chop"}) 34 5 39
267 34 chop $heredoc[-1];
268 34 $t->{source_eof_chop} = '';
269 }
270  
271 # Check if the last line of the file matches the terminator without
272 # newline at the end. If so, remove it from the content and set it as
273 # the terminator line.
274 $token->{_terminator_line} = pop @heredoc
275 39 ▶ if $self->_is_terminator( $token->{_terminator}, $heredoc[-1], $token->{_indented} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_terminator($token->{"_terminator"}, $heredoc[-1], $token->{"_indented"}) 18 21 39
276 }
277  
278 69 ▶ if ( $token->{_indented} && $token->{_terminator_line} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"_indented"} and $token->{"_terminator_line"}) 6 63 69
Condition100%3/3
$token->{"_indented"} and $token->{"_terminator_line"}
!ll&&!rl&&r
6216
MC/DC100%2/2
$token->{"_indented"} and $token->{"_terminator_line"}
$token->{"_indented"} $token->{"_terminator_line"}
Truth table100%3/3
A and B
A= $token->{"_indented"}B= $token->{"_terminator_line"}
ABresult
0X0
100
111
279 6 my $indent = $self->_indent( $token );
280 6 ▶ if ( $self->_is_match_indent( $token, $indent ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_match_indent($token, $indent)) 5 1 6
281 # Remove indent from here-doc as much as possible
282 5 s/^$indent// for @heredoc;
283 }
284  
285 6 s/^$indent// for $token->{_terminator_line};
286 }
287  
288 # Set a hint for PPI::Document->serialize so it can
289 # inexpensively repair it if needed when writing back out.
290 69 $token->{_damaged} = 1;
291  
292 # The HereDoc is not fully parsed
293 69 $t->_finalize_token->__TOKENIZER__on_char( $t );
294 }
295  
296 63 1;
297  
298 =pod
299  
300 =head1 TO DO
301  
302 - Implement PPI::Token::Quote interface compatibility
303  
304 - Check CPAN for any use of the null here-doc or here-doc-in-s///e
305  
306 - Add support for the null here-doc
307  
308 - Add support for here-doc in s///e
309  
310 =head1 SUPPORT
311  
312 See the L<support section|PPI/SUPPORT> in the main module.
313  
314 =head1 AUTHOR
315  
316 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
317  
318 =head1 COPYRIGHT
319  
320 Copyright 2001 - 2011 Adam Kennedy.
321  
322 This program is free software; you can redistribute
323 it and/or modify it under the same terms as Perl itself.
324  
325 The full text of the license can be found in the
326 LICENSE file included with this module.
327  
328 =cut
« PPI/Token/Word.pm ↑ Summary PPI/Normal/Standard.pm »