| 1 | package Filter::QuasiQuote; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | #use Smart::Comments; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | our $VERSION = '0.07'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Filter::Util::Call qw(filter_read); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | our $Debug; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | my ($type, @arguments) = @_ ; | |||||||||||||||||||||||||||
| 15 | #warn $type; | ||||||||||||||||||||||||||||
| 16 | 4 | my ($package, $filename, $line) = caller; | |||||||||||||||||||||||||||
| 17 | #warn "$package"; | ||||||||||||||||||||||||||||
| 18 | 4 | my $self = bless { | |||||||||||||||||||||||||||
| 19 | file => $filename, | ||||||||||||||||||||||||||||
| 20 | line => $line, | ||||||||||||||||||||||||||||
| 21 | quoted => undef, | ||||||||||||||||||||||||||||
| 22 | method => undef, | ||||||||||||||||||||||||||||
| 23 | ignore_once => undef, | ||||||||||||||||||||||||||||
| 24 | pos_diff => 0, | ||||||||||||||||||||||||||||
| 25 | }, $type; | ||||||||||||||||||||||||||||
| 26 | 4 | Filter::Util::Call::real_import($self, $package, 0) ; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 115 | ▶ | sub filter { | ||||||||||||||||||||||||||
|
Subroutine
filter: called CC=19 SCAR=29.6
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 115 | my ($self) = @_ ; | |||||||||||||||||||||||||||
| 31 | #warn "SELF: $self"; | ||||||||||||||||||||||||||||
| 32 | 115 | my($status) ; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 115 | $status = filter_read; | |||||||||||||||||||||||||||
| 35 | #warn scalar(s/\r//g); | ||||||||||||||||||||||||||||
| 36 | #warn "Last char: ", ord(substr($_, -1, 1)); | ||||||||||||||||||||||||||||
| 37 | 115 | my $changed; | |||||||||||||||||||||||||||
| 38 | 115 | ▶ | if ($status > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 111 | $self->{pos_diff} = 0; | |||||||||||||||||||||||||||
| 40 | 111 | $self->{line}++; | |||||||||||||||||||||||||||
| 41 | 111 | my ($i, $buf); | |||||||||||||||||||||||||||
| 42 | 111 | while (1) { | |||||||||||||||||||||||||||
| 43 | 335 | $i++; | |||||||||||||||||||||||||||
| 44 | 335 | $self->debug("Pos ", pos, ", Pass $i, Line $self->{line}"); | |||||||||||||||||||||||||||
| 45 | 335 | ▶ | if (/\G\s+/gc) { $buf .= $& } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 335 | ▶ | if (/\G\[:(\w+)\|(.*?)\|\]/gc) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 47 | #warn "$1 => $2"; | ||||||||||||||||||||||||||||
| 48 | 51 | my ($meth, $s) = ($1, $2); | |||||||||||||||||||||||||||
| 49 | 51 | my $len = length($&); | |||||||||||||||||||||||||||
| 50 | 51 | my $to = pos; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 51 | ▶ | if (defined $self->{method}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | die "Syntax error at $self->{file}, line $self->{line}: Quasiquotes cannot be nested.\n"; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | #warn "to: $to\n"; | ||||||||||||||||||||||||||||
| 57 | #warn "len: $len\n"; | ||||||||||||||||||||||||||||
| 58 | 51 | ▶ | if ($self->can($meth)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | #warn "POS diff: $self->{pos_diff}"; | ||||||||||||||||||||||||||||
| 60 | 40 | my $col = $to - $self->{pos_diff} - $len + 1; | |||||||||||||||||||||||||||
| 61 | 40 | my $res = $self->$meth($s, $self->{file}, $self->{line}, $col); | |||||||||||||||||||||||||||
| 62 | #$self->debug("Pos BEFORE change \$_: ", pos($_)); | ||||||||||||||||||||||||||||
| 63 | 40 | substr($_, $to - $len, $len, $res); | |||||||||||||||||||||||||||
| 64 | 40 | $changed = 1; pos($_) = $to - $len + length($res); | |||||||||||||||||||||||||||
| 65 | 40 | $self->{pos_diff} = length($res) - $len; | |||||||||||||||||||||||||||
| 66 | #$self->debug("Pos AFTER change \$_: ", pos($_)); | ||||||||||||||||||||||||||||
| 67 | ### $_ | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | elsif (/\G\[:(\w+)\|(.*)/gc) { | ||||||||||||||||||||||||||||
| 71 | 16 | my ($meth, $s) = ($1, $2); | |||||||||||||||||||||||||||
| 72 | 16 | my $len = length($&); | |||||||||||||||||||||||||||
| 73 | 16 | my $to = pos $_; | |||||||||||||||||||||||||||
| 74 | #warn "len: $len to: $to match: $&\n"; | ||||||||||||||||||||||||||||
| 75 | 16 | ▶ | if (!$self->can($meth)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 4 | $self->debug("Ignoring starting $meth at $self->{line} (pos $to, pass $i)"); | |||||||||||||||||||||||||||
| 77 | 4 | $self->{ignore_once} = 1; | |||||||||||||||||||||||||||
| 78 | #$self->{method} = $meth; | ||||||||||||||||||||||||||||
| 79 | 4 | last; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 12 | substr($_, $to - $len, $len, ' '); | |||||||||||||||||||||||||||
| 83 | 12 | $changed = 1; | |||||||||||||||||||||||||||
| 84 | 12 | my $col = $to - $self->{pos_diff} - $len + 1; | |||||||||||||||||||||||||||
| 85 | 12 | $self->{saved_pos} = [$self->{line}, $col]; | |||||||||||||||||||||||||||
| 86 | ### $_ | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 12 | ▶ | if (!defined $self->{method}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 12 | $self->{quoted} = $s; | |||||||||||||||||||||||||||
| 90 | 12 | $self->{method} = $meth; | |||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||
| 92 | 0 | die "Syntax error at $self->{file}, line $self->{line}: Quasiquotes cannot be nested.\n"; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 12 | last; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | elsif (/\G\|\]/gc) { | ||||||||||||||||||||||||||||
| 97 | 16 | my $s = $buf; | |||||||||||||||||||||||||||
| 98 | 16 | my $len = length($buf . $&); | |||||||||||||||||||||||||||
| 99 | 16 | my $to = pos; | |||||||||||||||||||||||||||
| 100 | 16 | $self->debug("Found closing tag: ", ref $self, " (pos $to, pass $i, line $self->{line})"); | |||||||||||||||||||||||||||
| 101 | 16 | ▶ | if ($self->{ignore_once}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 4 | ▶ | $self->debug("Ignoring closing $self->{method} at $self->{line} (pos $to, pass $i)") if $self->{method}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 4 | undef $self->{ignore_once}; | |||||||||||||||||||||||||||
| 104 | 4 | undef $self->{method}; | |||||||||||||||||||||||||||
| 105 | 4 | undef $self->{quoted}; | |||||||||||||||||||||||||||
| 106 | 4 | next; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 12 | my $meth = $self->{method}; | |||||||||||||||||||||||||||
| 110 | 12 | ▶ | if (!defined $meth) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | #warn $self; | ||||||||||||||||||||||||||||
| 112 | #warn "POS: ", pos; | ||||||||||||||||||||||||||||
| 113 | 0 | die ref $self, ": Syntax error at $self->{file}, line $self->{line}: Pending closing quasiquote. (pos $to, pass $i)\n"; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | #warn "POS diff: $self->{pos_diff}"; | ||||||||||||||||||||||||||||
| 116 | 12 | my $pos = $self->{saved_pos}; | |||||||||||||||||||||||||||
| 117 | 12 | my ($line, $col); | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | if (!$pos) { $line = $self->{line}; $col = 0 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 12 | else { ($line, $col) = @$pos } | |||||||||||||||||||||||||||
| 120 | 12 | my $res = $self->$meth($self->{quoted} . $s, $self->{file}, $line, $col); | |||||||||||||||||||||||||||
| 121 | 12 | undef $self->{method}; | |||||||||||||||||||||||||||
| 122 | 12 | undef $self->{quoted}; | |||||||||||||||||||||||||||
| 123 | 12 | substr($_, $to - $len, $len, $res); | |||||||||||||||||||||||||||
| 124 | 12 | $changed = 1; pos($_) = $to - $len + length($res); | |||||||||||||||||||||||||||
| 125 | 12 | $self->{pos_diff} = length($res) - $len; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | #$changed = 1; | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | elsif (/\G[^\|\[]+|\G./gc) { | ||||||||||||||||||||||||||||
| 130 | #print "Ignored: $_"; | ||||||||||||||||||||||||||||
| 131 | #last; | ||||||||||||||||||||||||||||
| 132 | #warn $&; | ||||||||||||||||||||||||||||
| 133 | 157 | $buf .= $&; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | else { | ||||||||||||||||||||||||||||
| 136 | 95 | last; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 111 | ▶ | if (!$changed && defined $self->{method}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $changed and defined $self->{"method"}
MC/DC100%2/2
not $changed and defined $self->{"method"}
not $changed
defined $self->{"method"}
Truth table100%3/3
not(A) and B
A= not $changedB= defined $self->{"method"}
| |||||||||||||||||||||||||||||
| 140 | 6 | $self->{quoted} .= $_; | |||||||||||||||||||||||||||
| 141 | 6 | $_ = "\n"; $changed = 1; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | #warn "$self->{file}: line $self->{line}: $_"; | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 115 | ▶ | $self->debug("Processed: (line $self->{line}): $_") if $changed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 115 | s/\n//gs; | |||||||||||||||||||||||||||
| 147 | 115 | ▶ | $_ .= "\n" unless substr($_, -1, 1) eq "\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | #warn $status; | ||||||||||||||||||||||||||||
| 149 | 115 | $status ; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 407 | ▶ | sub debug { | ||||||||||||||||||||||||||
|
Subroutine
debug: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | 407 | my $self = shift; | |||||||||||||||||||||||||||
| 154 | 407 | ▶ | warn ref $self, ": ", join('', @_), "\n" if $Debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 4 | 1; | |||||||||||||||||||||||||||
| 158 | __END__ | ||||||||||||||||||||||||||||