| 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 | 62 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 88 | 62 | ▶ | use PPI::Token (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 62 | ▶ | use vars qw{$VERSION @ISA}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | BEGIN { | ||||||||||||||||||||||||||||
| 92 | 62 | ▶ | $VERSION = '1.236'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 93 | 62 | @ISA = 'PPI::Token'; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | ##################################################################### | ||||||||||||||||||||||||||||
| 101 | # PPI::Token::HereDoc Methods | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =pod | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head2 heredoc | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | The C<heredoc> method is the authoritative method for accessing the contents | ||||||||||||||||||||||||||||
| 108 | of the C<HereDoc> object. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | It returns the contents of the here-doc as a list of newline-terminated | ||||||||||||||||||||||||||||
| 111 | strings. If called in scalar context, it returns the number of lines in | ||||||||||||||||||||||||||||
| 112 | the here-doc, B<excluding> the terminator line. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub heredoc { | ||||||||||||||||||||||||||||
| 117 | wantarray | ||||||||||||||||||||||||||||
| 118 | 193 | ? @{shift->{_heredoc}} | |||||||||||||||||||||||||||
| 119 | 219 | ▶ | : scalar @{shift->{_heredoc}}; | ||||||||||||||||||||||||||
|
Subroutine
heredoc: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =pod | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head2 terminator | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | The C<terminator> method returns the name of the terminating string for the | ||||||||||||||||||||||||||||
| 127 | here-doc. | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Returns the terminating string as an unescaped string (in the rare case | ||||||||||||||||||||||||||||
| 130 | the terminator has an escaped quote in it). | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | sub terminator { | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | shift->{_terminator}; | ||||||||||||||||||||||||||
|
Subroutine
terminator: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | ##################################################################### | ||||||||||||||||||||||||||||
| 143 | # Tokenizer Methods | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # Parse in the entire here-doc in one call | ||||||||||||||||||||||||||||
| 146 | sub __TOKENIZER__on_char { | ||||||||||||||||||||||||||||
| 147 | 198 | ▶ | my $t = $_[1]; | ||||||||||||||||||||||||||
|
Subroutine
__TOKENIZER__on_char: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # We are currently located on the first char after the << | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | # Handle the most common form first for simplicity and speed reasons | ||||||||||||||||||||||||||||
| 152 | ### FIXME - This regex, and this method in general, do not yet allow | ||||||||||||||||||||||||||||
| 153 | ### for the null here-doc, which terminates at the first | ||||||||||||||||||||||||||||
| 154 | ### empty line. | ||||||||||||||||||||||||||||
| 155 | 198 | pos $t->{line} = $t->{line_cursor}; | |||||||||||||||||||||||||||
| 156 | 198 | ▶ | if ( $t->{line} !~ m/\G( \s* (?: "[^"]*" | '[^']*' | `[^`]*` | \\?\w+ ) )/gcx ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | # Degenerate to a left-shift operation | ||||||||||||||||||||||||||||
| 158 | 3 | $t->{token}->set_class('Operator'); | |||||||||||||||||||||||||||
| 159 | 3 | return $t->_finalize_token->__TOKENIZER__on_char( $t ); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # Add the rest of the token, work out what type it is, | ||||||||||||||||||||||||||||
| 163 | # and suck in the content until the end. | ||||||||||||||||||||||||||||
| 164 | 195 | my $token = $t->{token}; | |||||||||||||||||||||||||||
| 165 | 195 | $token->{content} .= $1; | |||||||||||||||||||||||||||
| 166 | 195 | $t->{line_cursor} += length $1; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # Find the terminator, clean it up and determine | ||||||||||||||||||||||||||||
| 169 | # the type of here-doc we are dealing with. | ||||||||||||||||||||||||||||
| 170 | 195 | my $content = $token->{content}; | |||||||||||||||||||||||||||
| 171 | 195 | ▶ | if ( $content =~ /^\<\<(\w+)$/ ) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 172 | # Bareword | ||||||||||||||||||||||||||||
| 173 | 129 | $token->{_mode} = 'interpolate'; | |||||||||||||||||||||||||||
| 174 | 129 | $token->{_terminator} = $1; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | } elsif ( $content =~ /^\<\<\s*\'(.*)\'$/ ) { | ||||||||||||||||||||||||||||
| 177 | # ''-quoted literal | ||||||||||||||||||||||||||||
| 178 | 33 | $token->{_mode} = 'literal'; | |||||||||||||||||||||||||||
| 179 | 33 | $token->{_terminator} = $1; | |||||||||||||||||||||||||||
| 180 | 33 | $token->{_terminator} =~ s/\\'/'/g; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | } elsif ( $content =~ /^\<\<\s*\"(.*)\"$/ ) { | ||||||||||||||||||||||||||||
| 183 | # ""-quoted literal | ||||||||||||||||||||||||||||
| 184 | 10 | $token->{_mode} = 'interpolate'; | |||||||||||||||||||||||||||
| 185 | 10 | $token->{_terminator} = $1; | |||||||||||||||||||||||||||
| 186 | 10 | $token->{_terminator} =~ s/\\"/"/g; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | } elsif ( $content =~ /^\<\<\s*\`(.*)\`$/ ) { | ||||||||||||||||||||||||||||
| 189 | # ``-quoted command | ||||||||||||||||||||||||||||
| 190 | 5 | $token->{_mode} = 'command'; | |||||||||||||||||||||||||||
| 191 | 5 | $token->{_terminator} = $1; | |||||||||||||||||||||||||||
| 192 | 5 | $token->{_terminator} =~ s/\\`/`/g; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | } elsif ( $content =~ /^\<\<\\(\w+)$/ ) { | ||||||||||||||||||||||||||||
| 195 | # Legacy forward-slashed bareword | ||||||||||||||||||||||||||||
| 196 | 18 | $token->{_mode} = 'literal'; | |||||||||||||||||||||||||||
| 197 | 18 | $token->{_terminator} = $1; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | } else { | ||||||||||||||||||||||||||||
| 200 | # WTF? | ||||||||||||||||||||||||||||
| 201 | 0 | return undef; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | # Suck in the HEREDOC | ||||||||||||||||||||||||||||
| 205 | 195 | $token->{_heredoc} = \my @heredoc; | |||||||||||||||||||||||||||
| 206 | 195 | my $terminator = $token->{_terminator} . "\n"; | |||||||||||||||||||||||||||
| 207 | 195 | ▶ | while ( defined( my $line = $t->_get_line ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | 625 | ▶ | if ( $line eq $terminator ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | # Keep the actual termination line for consistency | ||||||||||||||||||||||||||||
| 210 | # when we are re-assembling the file | ||||||||||||||||||||||||||||
| 211 | 133 | $token->{_terminator_line} = $line; | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | # The HereDoc is now fully parsed | ||||||||||||||||||||||||||||
| 214 | 133 | return $t->_finalize_token->__TOKENIZER__on_char( $t ); | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | # Add the line | ||||||||||||||||||||||||||||
| 218 | 492 | push @heredoc, $line; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | # End of file. | ||||||||||||||||||||||||||||
| 222 | # Error: Didn't reach end of here-doc before end of file. | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | # If the here-doc block is not empty, look at the last line to determine if | ||||||||||||||||||||||||||||
| 225 | # the here-doc terminator is missing a newline (which Perl would fail to | ||||||||||||||||||||||||||||
| 226 | # compile but is easy to detect) or if the here-doc block was just not | ||||||||||||||||||||||||||||
| 227 | # terminated at all (which Perl would fail to compile as well). | ||||||||||||||||||||||||||||
| 228 | 62 | $token->{_terminator_line} = undef; | |||||||||||||||||||||||||||
| 229 | 62 | ▶ | if ( @heredoc and defined $heredoc[-1] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@heredoc and defined $heredoc[-1]
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]
| |||||||||||||||||||||||||||||
| 230 | # See PPI::Tokenizer, the algorithm there adds a space at the end of the | ||||||||||||||||||||||||||||
| 231 | # document that we need to make sure we remove. | ||||||||||||||||||||||||||||
| 232 | 32 | ▶ | if ( $t->{source_eof_chop} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | 29 | chop $heredoc[-1]; | |||||||||||||||||||||||||||
| 234 | 29 | $t->{source_eof_chop} = ''; | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | # Check if the last line of the file matches the terminator without | ||||||||||||||||||||||||||||
| 238 | # newline at the end. If so, remove it from the content and set it as | ||||||||||||||||||||||||||||
| 239 | # the terminator line. | ||||||||||||||||||||||||||||
| 240 | $token->{_terminator_line} = pop @heredoc | ||||||||||||||||||||||||||||
| 241 | 32 | ▶ | if $heredoc[-1] eq $token->{_terminator}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | # Set a hint for PPI::Document->serialize so it can | ||||||||||||||||||||||||||||
| 245 | # inexpensively repair it if needed when writing back out. | ||||||||||||||||||||||||||||
| 246 | 62 | $token->{_damaged} = 1; | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | # The HereDoc is not fully parsed | ||||||||||||||||||||||||||||
| 249 | 62 | $t->_finalize_token->__TOKENIZER__on_char( $t ); | |||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 62 | 1; | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | =pod | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | =head1 TO DO | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | - Implement PPI::Token::Quote interface compatibility | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | - Check CPAN for any use of the null here-doc or here-doc-in-s///e | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | - Add support for the null here-doc | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | - Add support for here-doc in s///e | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | See the L<support section|PPI/SUPPORT> in the main module. | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | Copyright 2001 - 2011 Adam Kennedy. | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||
| 279 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 282 | LICENSE file included with this module. | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | =cut | ||||||||||||||||||||||||||||