| 1 | package Jinja2::TT2::Emitter; | ||||||||||||||||||||
| 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 v5.20; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | ▶ | use Jinja2::TT2::Filters; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||
| 10 | 1 | my ($class, %opts) = @_; | |||||||||||||||||||
| 11 | return bless { | ||||||||||||||||||||
| 12 | indent_level => 0, | ||||||||||||||||||||
| 13 | 1 | ▶ | indent_str => $opts{indent_str} // ' ', | ||||||||||||||||||
|
Condition50%1/2
$opts{"indent_str"} // " "
MC/DC0%0/1
$opts{"indent_str"} // " "
$opts{"indent_str"}
Truth table50%1/2
A
A= $opts{"indent_str"}
| |||||||||||||||||||||
| 14 | filters => Jinja2::TT2::Filters->new(), | ||||||||||||||||||||
| 15 | }, $class; | ||||||||||||||||||||
| 16 | } | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 112 | ▶ | sub emit { | ||||||||||||||||||
|
Subroutine
emit: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||
| 19 | 112 | my ($self, $ast) = @_; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 112 | ▶ | if ($ast->{type} eq 'ROOT') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | 43 | return join('', map { $self->emit($_) } @{$ast->{body}}); | |||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 69 | my $method = '_emit_' . lc($ast->{type}); | |||||||||||||||||||
| 26 | 69 | ▶ | if ($self->can($method)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | 69 | return $self->$method($ast); | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 0 | die "Unknown AST node type: $ast->{type}"; | |||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 19 | ▶ | sub _emit_text { | ||||||||||||||||||
|
Subroutine
_emit_text: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | 19 | my ($self, $node) = @_; | |||||||||||||||||||
| 35 | 19 | return $node->{value}; | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 1 | ▶ | sub _emit_comment { | ||||||||||||||||||
|
Subroutine
_emit_comment: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 39 | 1 | my ($self, $node) = @_; | |||||||||||||||||||
| 40 | 1 | return "[%# $node->{value} %]"; | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 32 | ▶ | sub _emit_output { | ||||||||||||||||||
|
Subroutine
_emit_output: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 44 | 32 | my ($self, $node) = @_; | |||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 32 | ▶ | my $strip_before = $node->{strip_before} ? '-' : ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | 32 | ▶ | my $strip_after = $node->{strip_after} ? '-' : ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 32 | my $expr = $self->_emit_expr($node->{expr}); | |||||||||||||||||||
| 50 | 32 | return "[%$strip_before $expr $strip_after%]"; | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 10 | ▶ | sub _emit_if { | ||||||||||||||||||
|
Subroutine
_emit_if: called CC=9 SCAR=22.1
| |||||||||||||||||||||
| 54 | 10 | my ($self, $node) = @_; | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 10 | ▶ | my $strip_before = $node->{strip_before} ? '-' : ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 10 | ▶ | my $strip_after = $node->{strip_after} ? '-' : ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 10 | my $condition = $self->_emit_expr($node->{condition}); | |||||||||||||||||||
| 60 | 10 | my $output = "[%$strip_before IF $condition $strip_after%]"; | |||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 10 | for my $child (@{$node->{body}}) { | |||||||||||||||||||
| 63 | 6 | $output .= $self->emit($child); | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 10 | for my $branch (@{$node->{branches}}) { | |||||||||||||||||||
| 67 | 4 | ▶ | if ($branch->{type} eq 'ELIF') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 68 | 1 | my $elif_cond = $self->_emit_expr($branch->{condition}); | |||||||||||||||||||
| 69 | 1 | $output .= "[% ELSIF $elif_cond %]"; | |||||||||||||||||||
| 70 | 1 | for my $child (@{$branch->{body}}) { | |||||||||||||||||||
| 71 | 1 | $output .= $self->emit($child); | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | } elsif ($branch->{type} eq 'ELSE') { | ||||||||||||||||||||
| 74 | 3 | $output .= "[% ELSE %]"; | |||||||||||||||||||
| 75 | 3 | for my $child (@{$branch->{body}}) { | |||||||||||||||||||
| 76 | 3 | $output .= $self->emit($child); | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 10 | $output .= "[% END %]"; | |||||||||||||||||||
| 82 | 10 | return $output; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 3 | ▶ | sub _emit_for { | ||||||||||||||||||
|
Subroutine
_emit_for: called CC=5 SCAR=17.6
| |||||||||||||||||||||
| 86 | 3 | my ($self, $node) = @_; | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 3 | my $loop_var = join(', ', @{$node->{loop_vars}}); | |||||||||||||||||||
| 89 | 3 | my $iterable = $self->_emit_expr($node->{iterable}); | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 3 | my $output = "[% FOREACH $loop_var IN $iterable %]"; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 3 | for my $child (@{$node->{body}}) { | |||||||||||||||||||
| 94 | 7 | $output .= $self->emit($child); | |||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | # Handle else clause (TT2 doesn't have for/else, need WRAPPER or conditional) | ||||||||||||||||||||
| 98 | 3 | ▶ | if (@{$node->{else_body}}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 99 | # TT2 workaround: use IF for empty check | ||||||||||||||||||||
| 100 | 0 | $output = "[% IF $iterable.size %]" . $output . "[% ELSE %]"; | |||||||||||||||||||
| 101 | 0 | for my $child (@{$node->{else_body}}) { | |||||||||||||||||||
| 102 | 0 | $output .= $self->emit($child); | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | 0 | $output .= "[% END %]"; | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 3 | ▶ | $output .= "[% END %]" unless @{$node->{else_body}}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 3 | return $output; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 1 | ▶ | sub _emit_block { | ||||||||||||||||||
|
Subroutine
_emit_block: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 113 | 1 | my ($self, $node) = @_; | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 1 | my $output = "[% BLOCK $node->{name} %]"; | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 1 | for my $child (@{$node->{body}}) { | |||||||||||||||||||
| 118 | 1 | $output .= $self->emit($child); | |||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 1 | $output .= "[% END %]"; | |||||||||||||||||||
| 122 | 1 | return $output; | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 0 | ▶ | sub _emit_extends { | ||||||||||||||||||
|
Subroutine
_emit_extends: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 126 | 0 | my ($self, $node) = @_; | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 0 | my $template = $self->_emit_expr($node->{template}); | |||||||||||||||||||
| 129 | # Remove quotes for TT2 PROCESS/WRAPPER | ||||||||||||||||||||
| 130 | 0 | $template =~ s/^['"]|['"]$//g; | |||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | # TT2 doesn't have direct extends; use WRAPPER or PROCESS | ||||||||||||||||||||
| 133 | # For now, emit as a comment + PROCESS | ||||||||||||||||||||
| 134 | 0 | return "[%# EXTENDS: $template - use WRAPPER in TT2 %]\n[% PROCESS $template %]"; | |||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 1 | ▶ | sub _emit_include { | ||||||||||||||||||
|
Subroutine
_emit_include: called CC=3 SCAR=11.2
| |||||||||||||||||||||
| 138 | 1 | my ($self, $node) = @_; | |||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 1 | ▶ | my $strip_before = $node->{strip_before} ? '-' : ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | 1 | ▶ | my $strip_after = $node->{strip_after} ? '-' : ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 1 | my $template = $self->_emit_expr($node->{template}); | |||||||||||||||||||
| 144 | 1 | $template =~ s/^['"]|['"]$//g; | |||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 1 | return "[%$strip_before INCLUDE $template $strip_after%]"; | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 0 | ▶ | sub _emit_import { | ||||||||||||||||||
|
Subroutine
_emit_import: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 150 | 0 | my ($self, $node) = @_; | |||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 0 | my $template = $self->_emit_expr($node->{template}); | |||||||||||||||||||
| 153 | 0 | $template =~ s/^['"]|['"]$//g; | |||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | # TT2 uses PROCESS for imports; macros become available | ||||||||||||||||||||
| 156 | 0 | return "[%# IMPORT $template AS $node->{alias} %]\n[% USE $node->{alias} = $template %]"; | |||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 0 | ▶ | sub _emit_from { | ||||||||||||||||||
|
Subroutine
_emit_from: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 160 | 0 | my ($self, $node) = @_; | |||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 0 | my $template = $self->_emit_expr($node->{template}); | |||||||||||||||||||
| 163 | 0 | $template =~ s/^['"]|['"]$//g; | |||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 0 | ▶ | my @imports = map { $_->{name} . ($_->{alias} ne $_->{name} ? " AS $_->{alias}" : '') } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 166 | 0 | @{$node->{imports}}; | |||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 0 | return "[%# FROM $template IMPORT " . join(', ', @imports) . " %]"; | |||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 1 | ▶ | sub _emit_set { | ||||||||||||||||||
|
Subroutine
_emit_set: called CC=3 SCAR=14.2
| |||||||||||||||||||||
| 172 | 1 | my ($self, $node) = @_; | |||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 1 | my $names = join(', ', @{$node->{names}}); | |||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | 1 | ▶ | if ($node->{value}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 177 | # Inline set | ||||||||||||||||||||
| 178 | 1 | my $value = $self->_emit_expr($node->{value}); | |||||||||||||||||||
| 179 | 1 | return "[% $names = $value %]"; | |||||||||||||||||||
| 180 | } else { | ||||||||||||||||||||
| 181 | # Block set - use FILTER to capture content | ||||||||||||||||||||
| 182 | 0 | my $output = "[% FILTER \$set_$names %]"; | |||||||||||||||||||
| 183 | 0 | for my $child (@{$node->{body}}) { | |||||||||||||||||||
| 184 | 0 | $output .= $self->emit($child); | |||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | 0 | $output .= "[% END %][% $names = set_$names %]"; | |||||||||||||||||||
| 187 | 0 | return $output; | |||||||||||||||||||
| 188 | } | ||||||||||||||||||||
| 189 | } | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | 1 | ▶ | sub _emit_macro { | ||||||||||||||||||
|
Subroutine
_emit_macro: called CC=3 SCAR=11.1
| |||||||||||||||||||||
| 192 | 1 | my ($self, $node) = @_; | |||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | my @args = map { | ||||||||||||||||||||
| 195 | 1 | my $arg = $_->{name}; | |||||||||||||||||||
| 196 | 1 | ▶ | if (defined $_->{default}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 197 | 0 | $arg .= " = " . $self->_emit_expr($_->{default}); | |||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | 1 | $arg; | |||||||||||||||||||
| 200 | 1 | } @{$node->{args}}; | |||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | 1 | my $output = "[% MACRO $node->{name}(" . join(', ', @args) . ") BLOCK %]"; | |||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | 1 | for my $child (@{$node->{body}}) { | |||||||||||||||||||
| 205 | 2 | $output .= $self->emit($child); | |||||||||||||||||||
| 206 | } | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | 1 | $output .= "[% END %]"; | |||||||||||||||||||
| 209 | 1 | return $output; | |||||||||||||||||||
| 210 | } | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | 0 | ▶ | sub _emit_call { | ||||||||||||||||||
|
Subroutine
_emit_call: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 213 | 0 | my ($self, $node) = @_; | |||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | 0 | my $call = $self->_emit_expr($node->{call}); | |||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | # TT2 doesn't have direct call blocks; emit as WRAPPER | ||||||||||||||||||||
| 218 | 0 | my $output = "[%# CALL $call %]\n[% WRAPPER $call %]"; | |||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | 0 | for my $child (@{$node->{body}}) { | |||||||||||||||||||
| 221 | 0 | $output .= $self->emit($child); | |||||||||||||||||||
| 222 | } | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | 0 | $output .= "[% END %]"; | |||||||||||||||||||
| 225 | 0 | return $output; | |||||||||||||||||||
| 226 | } | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | 0 | ▶ | sub _emit_filter { | ||||||||||||||||||
|
Subroutine
_emit_filter: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 229 | 0 | my ($self, $node) = @_; | |||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | 0 | my $filter = $self->_emit_filter_single($node->{filter}); | |||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | 0 | my $output = "[% FILTER $filter %]"; | |||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | 0 | for my $child (@{$node->{body}}) { | |||||||||||||||||||
| 236 | 0 | $output .= $self->emit($child); | |||||||||||||||||||
| 237 | } | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | 0 | $output .= "[% END %]"; | |||||||||||||||||||
| 240 | 0 | return $output; | |||||||||||||||||||
| 241 | } | ||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | 0 | ▶ | sub _emit_raw { | ||||||||||||||||||
|
Subroutine
_emit_raw: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 244 | 0 | my ($self, $node) = @_; | |||||||||||||||||||
| 245 | # TT2 has no raw tag; content is already literal | ||||||||||||||||||||
| 246 | # Could use [% RAWPERL %] but that's different | ||||||||||||||||||||
| 247 | 0 | return $node->{value}; | |||||||||||||||||||
| 248 | } | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | 0 | ▶ | sub _emit_with { | ||||||||||||||||||
|
Subroutine
_emit_with: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 251 | 0 | my ($self, $node) = @_; | |||||||||||||||||||
| 252 | |||||||||||||||||||||
| 253 | # TT2 doesn't have 'with'; simulate with SET in a wrapper | ||||||||||||||||||||
| 254 | 0 | my $output = "[%# WITH scope %]"; | |||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | 0 | for my $assign (@{$node->{assignments}}) { | |||||||||||||||||||
| 257 | 0 | $output .= "[% SET $assign->{name} = " . $self->_emit_expr($assign->{value}) . " %]"; | |||||||||||||||||||
| 258 | } | ||||||||||||||||||||
| 259 | |||||||||||||||||||||
| 260 | 0 | for my $child (@{$node->{body}}) { | |||||||||||||||||||
| 261 | 0 | $output .= $self->emit($child); | |||||||||||||||||||
| 262 | } | ||||||||||||||||||||
| 263 | |||||||||||||||||||||
| 264 | 0 | return $output; | |||||||||||||||||||
| 265 | } | ||||||||||||||||||||
| 266 | |||||||||||||||||||||
| 267 | 0 | ▶ | sub _emit_autoescape { | ||||||||||||||||||
|
Subroutine
_emit_autoescape: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 268 | 0 | my ($self, $node) = @_; | |||||||||||||||||||
| 269 | |||||||||||||||||||||
| 270 | # TT2 handles escaping differently; emit as comment | ||||||||||||||||||||
| 271 | 0 | my $output = "[%# AUTOESCAPE " . $self->_emit_expr($node->{enabled}) . " %]"; | |||||||||||||||||||
| 272 | |||||||||||||||||||||
| 273 | 0 | for my $child (@{$node->{body}}) { | |||||||||||||||||||
| 274 | 0 | $output .= $self->emit($child); | |||||||||||||||||||
| 275 | } | ||||||||||||||||||||
| 276 | |||||||||||||||||||||
| 277 | 0 | $output .= "[%# END AUTOESCAPE %]"; | |||||||||||||||||||
| 278 | 0 | return $output; | |||||||||||||||||||
| 279 | } | ||||||||||||||||||||
| 280 | |||||||||||||||||||||
| 281 | # Expression emitters | ||||||||||||||||||||
| 282 | 91 | ▶ | sub _emit_expr { | ||||||||||||||||||
|
Subroutine
_emit_expr: called CC=32 SCAR=38.4
| |||||||||||||||||||||
| 283 | 91 | my ($self, $node) = @_; | |||||||||||||||||||
| 284 | |||||||||||||||||||||
| 285 | 91 | ▶ | return '' unless $node; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 286 | |||||||||||||||||||||
| 287 | 91 | my $type = $node->{type}; | |||||||||||||||||||
| 288 | |||||||||||||||||||||
| 289 | 91 | ▶ | if ($type eq 'NAME') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 290 | 48 | my $name = $node->{value}; | |||||||||||||||||||
| 291 | # Map Jinja2 loop variables to TT2 | ||||||||||||||||||||
| 292 | 48 | ▶ | if ($name eq 'loop') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 293 | 5 | return 'loop'; | |||||||||||||||||||
| 294 | } | ||||||||||||||||||||
| 295 | 43 | return $name; | |||||||||||||||||||
| 296 | } | ||||||||||||||||||||
| 297 | |||||||||||||||||||||
| 298 | 43 | ▶ | if ($type eq 'LITERAL') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 299 | 14 | ▶ | if ($node->{subtype} eq 'STRING') { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 300 | 3 | my $val = $node->{value}; | |||||||||||||||||||
| 301 | 3 | $val =~ s/'/\\'/g; | |||||||||||||||||||
| 302 | 3 | return "'$val'"; | |||||||||||||||||||
| 303 | } elsif ($node->{subtype} eq 'BOOL') { | ||||||||||||||||||||
| 304 | 4 | ▶ | return $node->{value} ? '1' : '0'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 305 | } elsif ($node->{subtype} eq 'NONE') { | ||||||||||||||||||||
| 306 | 0 | return 'undef'; | |||||||||||||||||||
| 307 | } else { | ||||||||||||||||||||
| 308 | 7 | return $node->{value}; | |||||||||||||||||||
| 309 | } | ||||||||||||||||||||
| 310 | } | ||||||||||||||||||||
| 311 | |||||||||||||||||||||
| 312 | 29 | ▶ | if ($type eq 'BINOP') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 313 | 6 | my $left = $self->_emit_expr($node->{left}); | |||||||||||||||||||
| 314 | 6 | my $right = $self->_emit_expr($node->{right}); | |||||||||||||||||||
| 315 | 6 | my $op = $node->{op}; | |||||||||||||||||||
| 316 | |||||||||||||||||||||
| 317 | # Map operators | ||||||||||||||||||||
| 318 | 6 | my %op_map = ( | |||||||||||||||||||
| 319 | 'and' => 'AND', | ||||||||||||||||||||
| 320 | 'or' => 'OR', | ||||||||||||||||||||
| 321 | '~' => '_', # String concatenation | ||||||||||||||||||||
| 322 | 'in' => 'IN', | ||||||||||||||||||||
| 323 | 'not in' => 'NOT IN', | ||||||||||||||||||||
| 324 | '==' => '==', | ||||||||||||||||||||
| 325 | '!=' => '!=', | ||||||||||||||||||||
| 326 | '//' => 'div', # Integer division | ||||||||||||||||||||
| 327 | ); | ||||||||||||||||||||
| 328 | |||||||||||||||||||||
| 329 | 6 | ▶ | $op = $op_map{$op} // $op; | ||||||||||||||||||
|
Condition66%2/3
$op_map{$op} // $op
MC/DC0%0/2
$op_map{$op} // $op
$op_map{$op}
$op
Truth table66%2/3
A // B
A= $op_map{$op}B= $op
| |||||||||||||||||||||
| 330 | |||||||||||||||||||||
| 331 | 6 | return "($left $op $right)"; | |||||||||||||||||||
| 332 | } | ||||||||||||||||||||
| 333 | |||||||||||||||||||||
| 334 | 23 | ▶ | if ($type eq 'UNARYOP') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 335 | 1 | my $operand = $self->_emit_expr($node->{operand}); | |||||||||||||||||||
| 336 | 1 | my $op = $node->{op}; | |||||||||||||||||||
| 337 | |||||||||||||||||||||
| 338 | 1 | ▶ | if ($op eq 'not') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 339 | 1 | return "NOT $operand"; | |||||||||||||||||||
| 340 | } | ||||||||||||||||||||
| 341 | |||||||||||||||||||||
| 342 | 0 | return "$op$operand"; | |||||||||||||||||||
| 343 | } | ||||||||||||||||||||
| 344 | |||||||||||||||||||||
| 345 | 22 | ▶ | if ($type eq 'TERNARY') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 346 | 1 | my $true_val = $self->_emit_expr($node->{true_val}); | |||||||||||||||||||
| 347 | 1 | my $condition = $self->_emit_expr($node->{condition}); | |||||||||||||||||||
| 348 | 1 | ▶ | my $false_val = $node->{false_val} ? $self->_emit_expr($node->{false_val}) : "''"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 349 | |||||||||||||||||||||
| 350 | 1 | return "($condition ? $true_val : $false_val)"; | |||||||||||||||||||
| 351 | } | ||||||||||||||||||||
| 352 | |||||||||||||||||||||
| 353 | 21 | ▶ | if ($type eq 'GETATTR') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 354 | 7 | my $expr = $self->_emit_expr($node->{expr}); | |||||||||||||||||||
| 355 | 7 | my $attr = $node->{attr}; | |||||||||||||||||||
| 356 | |||||||||||||||||||||
| 357 | # Map loop attributes | ||||||||||||||||||||
| 358 | 7 | ▶ | if ($expr eq 'loop') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 359 | 5 | my %loop_map = ( | |||||||||||||||||||
| 360 | 'index' => 'count', | ||||||||||||||||||||
| 361 | 'index0' => 'index', | ||||||||||||||||||||
| 362 | 'revindex' => 'max - loop.index + 1', | ||||||||||||||||||||
| 363 | 'first' => 'first', | ||||||||||||||||||||
| 364 | 'last' => 'last', | ||||||||||||||||||||
| 365 | 'length' => 'size', | ||||||||||||||||||||
| 366 | 'cycle' => 'cycle', | ||||||||||||||||||||
| 367 | ); | ||||||||||||||||||||
| 368 | 5 | ▶ | return 'loop.' . ($loop_map{$attr} // $attr); | ||||||||||||||||||
|
Condition33%1/3
$loop_map{$attr} // $attr
MC/DC0%0/2
$loop_map{$attr} // $attr
$loop_map{$attr}
$attr
Truth table33%1/3
A // B
A= $loop_map{$attr}B= $attr
| |||||||||||||||||||||
| 369 | } | ||||||||||||||||||||
| 370 | |||||||||||||||||||||
| 371 | 2 | return "$expr.$attr"; | |||||||||||||||||||
| 372 | } | ||||||||||||||||||||
| 373 | |||||||||||||||||||||
| 374 | 14 | ▶ | if ($type eq 'GETITEM') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 375 | 1 | my $expr = $self->_emit_expr($node->{expr}); | |||||||||||||||||||
| 376 | 1 | my $index = $self->_emit_expr($node->{index}); | |||||||||||||||||||
| 377 | |||||||||||||||||||||
| 378 | 1 | ▶ | return "$expr.\$index" if $index =~ /^[a-zA-Z]/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 379 | 1 | return "$expr.$index"; | |||||||||||||||||||
| 380 | } | ||||||||||||||||||||
| 381 | |||||||||||||||||||||
| 382 | 13 | ▶ | if ($type eq 'CALL') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 383 | 1 | my $expr = $self->_emit_expr($node->{expr}); | |||||||||||||||||||
| 384 | 1 | my @args = map { $self->_emit_expr($_) } @{$node->{args}}; | |||||||||||||||||||
| 385 | |||||||||||||||||||||
| 386 | 1 | for my $kwarg (@{$node->{kwargs}}) { | |||||||||||||||||||
| 387 | 0 | push @args, "$kwarg->{name} = " . $self->_emit_expr($kwarg->{value}); | |||||||||||||||||||
| 388 | } | ||||||||||||||||||||
| 389 | |||||||||||||||||||||
| 390 | # Handle built-in functions | ||||||||||||||||||||
| 391 | 1 | ▶ | if ($expr eq 'range') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 392 | 1 | ▶ | if (@args == 1) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 393 | 1 | return "[0 .. $args[0] - 1]"; | |||||||||||||||||||
| 394 | } elsif (@args == 2) { | ||||||||||||||||||||
| 395 | 0 | return "[$args[0] .. $args[1] - 1]"; | |||||||||||||||||||
| 396 | } elsif (@args == 3) { | ||||||||||||||||||||
| 397 | # range with step - complex in TT2 | ||||||||||||||||||||
| 398 | 0 | return "[% # range($args[0], $args[1], $args[2]) %]"; | |||||||||||||||||||
| 399 | } | ||||||||||||||||||||
| 400 | } | ||||||||||||||||||||
| 401 | |||||||||||||||||||||
| 402 | 0 | ▶ | if ($expr eq 'super') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 403 | 0 | return 'content'; # TT2's WRAPPER content | |||||||||||||||||||
| 404 | } | ||||||||||||||||||||
| 405 | |||||||||||||||||||||
| 406 | 0 | return "$expr(" . join(', ', @args) . ")"; | |||||||||||||||||||
| 407 | } | ||||||||||||||||||||
| 408 | |||||||||||||||||||||
| 409 | 12 | ▶ | if ($type eq 'FILTER') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 410 | 10 | my $base = $self->_emit_expr($node->{expr}); | |||||||||||||||||||
| 411 | 10 | return $self->_emit_filter_application($base, $node); | |||||||||||||||||||
| 412 | } | ||||||||||||||||||||
| 413 | |||||||||||||||||||||
| 414 | 2 | ▶ | if ($type eq 'LIST') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 415 | 1 | my @elements = map { $self->_emit_expr($_) } @{$node->{elements}}; | |||||||||||||||||||
| 416 | 1 | return '[' . join(', ', @elements) . ']'; | |||||||||||||||||||
| 417 | } | ||||||||||||||||||||
| 418 | |||||||||||||||||||||
| 419 | 1 | ▶ | if ($type eq 'TUPLE') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 420 | 0 | my @elements = map { $self->_emit_expr($_) } @{$node->{elements}}; | |||||||||||||||||||
| 421 | 0 | return '[' . join(', ', @elements) . ']'; # TT2 uses arrays | |||||||||||||||||||
| 422 | } | ||||||||||||||||||||
| 423 | |||||||||||||||||||||
| 424 | 1 | ▶ | if ($type eq 'DICT') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 425 | my @pairs = map { | ||||||||||||||||||||
| 426 | 1 | my $key = $self->_emit_expr($_->{key}); | |||||||||||||||||||
| 427 | 1 | my $val = $self->_emit_expr($_->{value}); | |||||||||||||||||||
| 428 | 1 | "$key => $val"; | |||||||||||||||||||
| 429 | 1 | } @{$node->{pairs}}; | |||||||||||||||||||
| 430 | 1 | return '{ ' . join(', ', @pairs) . ' }'; | |||||||||||||||||||
| 431 | } | ||||||||||||||||||||
| 432 | |||||||||||||||||||||
| 433 | 0 | ▶ | if ($type eq 'NAMED_ARG') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 434 | 0 | my $value = $self->_emit_expr($node->{value}); | |||||||||||||||||||
| 435 | 0 | return "$node->{name} = $value"; | |||||||||||||||||||
| 436 | } | ||||||||||||||||||||
| 437 | |||||||||||||||||||||
| 438 | 0 | die "Unknown expression type: $type"; | |||||||||||||||||||
| 439 | } | ||||||||||||||||||||
| 440 | |||||||||||||||||||||
| 441 | 10 | ▶ | sub _emit_filter_application { | ||||||||||||||||||
|
Subroutine
_emit_filter_application: called CC=7 SCAR=26.8
| |||||||||||||||||||||
| 442 | 10 | my ($self, $base, $node) = @_; | |||||||||||||||||||
| 443 | |||||||||||||||||||||
| 444 | 10 | my $filter_name = $node->{name}; | |||||||||||||||||||
| 445 | 10 | my @args = map { $self->_emit_expr($_) } @{$node->{args}}; | |||||||||||||||||||
| 446 | |||||||||||||||||||||
| 447 | # Get the TT2 equivalent | ||||||||||||||||||||
| 448 | 10 | my $tt2_filter = $self->{filters}->map_filter($filter_name, \@args); | |||||||||||||||||||
| 449 | |||||||||||||||||||||
| 450 | 10 | ▶ | if ($tt2_filter->{type} eq 'vmethod') { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 451 | 10 | ▶ | if ($tt2_filter->{args}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 452 | 1 | return "$base.$tt2_filter->{name}($tt2_filter->{args})"; | |||||||||||||||||||
| 453 | } else { | ||||||||||||||||||||
| 454 | 9 | return "$base.$tt2_filter->{name}"; | |||||||||||||||||||
| 455 | } | ||||||||||||||||||||
| 456 | } elsif ($tt2_filter->{type} eq 'filter') { | ||||||||||||||||||||
| 457 | 0 | ▶ | if ($tt2_filter->{args}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 458 | 0 | return "$base | $tt2_filter->{name}($tt2_filter->{args})"; | |||||||||||||||||||
| 459 | } else { | ||||||||||||||||||||
| 460 | 0 | return "$base | $tt2_filter->{name}"; | |||||||||||||||||||
| 461 | } | ||||||||||||||||||||
| 462 | } elsif ($tt2_filter->{type} eq 'custom') { | ||||||||||||||||||||
| 463 | 0 | return $tt2_filter->{code}->($base, @args); | |||||||||||||||||||
| 464 | } else { | ||||||||||||||||||||
| 465 | # Passthrough - keep original name | ||||||||||||||||||||
| 466 | 0 | ▶ | my $args_str = @args ? '(' . join(', ', @args) . ')' : ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 467 | 0 | return "$base | $filter_name$args_str"; | |||||||||||||||||||
| 468 | } | ||||||||||||||||||||
| 469 | } | ||||||||||||||||||||
| 470 | |||||||||||||||||||||
| 471 | 0 | ▶ | sub _emit_filter_single { | ||||||||||||||||||
|
Subroutine
_emit_filter_single: not called CC=6 SCAR=37.4
| |||||||||||||||||||||
| 472 | 0 | my ($self, $node) = @_; | |||||||||||||||||||
| 473 | |||||||||||||||||||||
| 474 | 0 | ▶ | if ($node->{type} eq 'FILTER') { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 475 | 0 | ▶ | my $inner = $node->{expr} ? $self->_emit_filter_single($node->{expr}) : ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 476 | 0 | my $filter_name = $node->{name}; | |||||||||||||||||||
| 477 | 0 | my @args = map { $self->_emit_expr($_) } @{$node->{args}}; | |||||||||||||||||||
| 478 | |||||||||||||||||||||
| 479 | 0 | ▶ | my $args_str = @args ? '(' . join(', ', @args) . ')' : ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 480 | 0 | my $this_filter = "$filter_name$args_str"; | |||||||||||||||||||
| 481 | |||||||||||||||||||||
| 482 | 0 | ▶ | return $inner ? "$inner | $this_filter" : $this_filter; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 483 | } elsif ($node->{type} eq 'NAME') { | ||||||||||||||||||||
| 484 | 0 | return $node->{value}; | |||||||||||||||||||
| 485 | } | ||||||||||||||||||||
| 486 | |||||||||||||||||||||
| 487 | 0 | die "Unexpected filter node type: $node->{type}"; | |||||||||||||||||||
| 488 | } | ||||||||||||||||||||
| 489 | |||||||||||||||||||||
| 490 | 1 | 1; | |||||||||||||||||||
| 491 | |||||||||||||||||||||
| 492 | __END__ | ||||||||||||||||||||