| 1 | package Filter::Indent::HereDoc; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Filter::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | our $VERSION = '1.01'; | |||||||||||||||||||||||||||
| 8 | 1 | our %options = (); | |||||||||||||||||||||||||||
| 9 | 1 | our @buffer; # Temporary storage of current here document | |||||||||||||||||||||||||||
| 10 | 1 | our @termstring; # FIFO list of here document terminating strings | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 13 | 1 | %options = (); | |||||||||||||||||||||||||||
| 14 | 1 | $options{$_}++ foreach (@_); | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | FILTER_ONLY | ||||||||||||||||||||||||||||
| 18 | 1 | ▶ | executable => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | my @code = split /\n/; | |||||||||||||||||||||||||||
| 20 | 1 | $_ = join '',(map &process_line($_),@code); | |||||||||||||||||||||||||||
| 21 | 1 | }; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 11 | ▶ | sub process_line { | ||||||||||||||||||||||||||
|
Subroutine
process_line: called CC=18 SCAR=39.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 11 | my $line = shift; | |||||||||||||||||||||||||||
| 25 | 11 | ▶ | if (@termstring) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | # At this point we are in a here document, so all lines of code | ||||||||||||||||||||||||||||
| 27 | # are buffered until the end of the heredoc is detected | ||||||||||||||||||||||||||||
| 28 | 2 | push @buffer,$line; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # 2 scenarios - terminator is a blank line, or terminator contains non- | ||||||||||||||||||||||||||||
| 31 | # whitespace. If blank line, then look for same whitespace at start of | ||||||||||||||||||||||||||||
| 32 | # each line in buffer. Otherwise take the whitespace that precedes the | ||||||||||||||||||||||||||||
| 33 | # terminator and match this against each line in the buffer. | ||||||||||||||||||||||||||||
| 34 | # | ||||||||||||||||||||||||||||
| 35 | # By default, we accept terminator strings in the Perl6 RFC111 format, | ||||||||||||||||||||||||||||
| 36 | # i.e. whitespace, ';', and comments following the terminator are | ||||||||||||||||||||||||||||
| 37 | # allowed. The only exception is if the terminator is a blank line, | ||||||||||||||||||||||||||||
| 38 | # in this case then only whitespace is allowed. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 2 | my $termregex; | |||||||||||||||||||||||||||
| 41 | 2 | ▶ | unless ($options{strict_terminators}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 2 | ▶ | if ($termstring[0] =~ /\S/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 2 | $termregex = qr/^(\s*)($termstring[0])(\s*;{0,1}\s*(?:#.*){0,1})$/; | |||||||||||||||||||||||||||
| 44 | } else { | ||||||||||||||||||||||||||||
| 45 | 0 | $termregex = qr/^\s*$/; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } else { | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if ($termstring[0] =~ /\S/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | $termregex = qr/^(\s*)($termstring[0])$/; | |||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||
| 51 | 0 | $termregex = qr/^$/; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 2 | my ($whitespace,$terminator,$extras); | |||||||||||||||||||||||||||
| 56 | 2 | ▶ | if ($line =~ $termregex) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 1 | ($whitespace,$terminator,$extras) = ($1,$2,$3); | |||||||||||||||||||||||||||
| 58 | 1 | ▶ | if ($termstring[0] =~ /\S/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 1 | foreach (@buffer) { | |||||||||||||||||||||||||||
| 60 | 2 | ▶ | return unless (/^$whitespace/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||||||||||
| 63 | # Terminator string is a blank line | ||||||||||||||||||||||||||||
| 64 | 0 | undef $whitespace; | |||||||||||||||||||||||||||
| 65 | 0 | foreach (@buffer) { | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | if (/^(\s+)\S/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | $whitespace = $1 unless ($whitespace and /^$whitespace\s*/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$whitespace and /^$whitespace\s*/
MC/DC0%0/2
$whitespace and /^$whitespace\s*/
$whitespace
/^$whitespace\s*/
Truth table0%0/3
A and B
A= $whitespaceB= /^$whitespace\s*/
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | # End of heredoc - strip the required amount of whitespace | ||||||||||||||||||||||||||||
| 72 | 1 | map s/^$whitespace//,@buffer; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # If we found extra characters after the terminator (Perl6 RFC111 | ||||||||||||||||||||||||||||
| 75 | # style), move them onto a new line to be compatible with Perl5 | ||||||||||||||||||||||||||||
| 76 | 1 | ▶ | if ($extras) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | pop @buffer; | |||||||||||||||||||||||||||
| 78 | 0 | push @buffer,$terminator; | |||||||||||||||||||||||||||
| 79 | 0 | push @buffer,$extras; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Return captured heredoc back to Perl and reset the buffer | ||||||||||||||||||||||||||||
| 83 | 1 | $line = join "\n",@buffer; | |||||||||||||||||||||||||||
| 84 | 1 | @buffer = (); | |||||||||||||||||||||||||||
| 85 | 1 | shift @termstring; | |||||||||||||||||||||||||||
| 86 | 1 | return "$line\n"; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||
| 89 | # Perl6 RFC111 states that whitespace after the terminator | ||||||||||||||||||||||||||||
| 90 | # should be ingored | ||||||||||||||||||||||||||||
| 91 | 9 | ▶ | unless ($options{strict_terminators}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 9 | $line =~ s/(?<!<)<<(?!<)\s+/<</g; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # Can we find the start of any here documents? | ||||||||||||||||||||||||||||
| 96 | 9 | ▶ | MATCH: while ($line =~ m/(?<!<)<<(?!<)/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 1 | ▶ | if ($line =~ m/\G(\w+)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 1 | push @termstring,$1; | |||||||||||||||||||||||||||
| 99 | 1 | next MATCH; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if ($line =~ m/\G(?:\s*(['"`]))(.*?)(?<!\\)\1/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | my ($quote,$string) = ($1,$2); | |||||||||||||||||||||||||||
| 104 | 0 | $string =~ s/\\$quote/$quote/g; | |||||||||||||||||||||||||||
| 105 | 0 | push @termstring,$string; | |||||||||||||||||||||||||||
| 106 | 0 | next MATCH; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # Use of bare << to mean <<"" is depreciated | ||||||||||||||||||||||||||||
| 110 | # ...but still works so the module needs to support it! | ||||||||||||||||||||||||||||
| 111 | 0 | push @termstring,''; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 9 | return "$line\n"; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | 1; | |||||||||||||||||||||||||||
| 118 | __END__ | ||||||||||||||||||||||||||||