Xslate/Syntax/Handlebars.pm

filter: Statementstmt 94.7% 304 / 321 Branchbran 83.3% 95 / 114 Conditioncond 79.4% 31 / 39 MC/DCmcdc 56.5% 13 / 23 Subroutinesub 100% 36 / 36 Podpod 0.0% 0 / 23 totaltotal 86.1% 479 / 556 CC 79 SCAR 26.8
CC
79
Cov
91%
split_tags
36.9
std_block
11.5
preprocess
8.0
CRAP 14.5
↑ Summary Handlebars/Compiler.pm »
1 package Text::Xslate::Syntax::Handlebars;
2 13 our $AUTHORITY = 'cpan:DOY';
3 13 $Text::Xslate::Syntax::Handlebars::VERSION = '0.05';
4 13 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Text::Xslate::Util qw($DEBUG $NUMBER neat p);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 13 ▶use Text::Handlebars::Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 extends 'Text::Xslate::Parser';
12  
13 13 ▶use constant _DUMP_PROTO => scalar($DEBUG =~ /\b dump=proto \b/xmsi);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 13 my $nl = qr/\x0d?\x0a/;
16  
17 13 my $bracket_string = qr/\[ [^\]]* \]/xms;
18 13 my $STRING = qr/(?: $Text::Xslate::Util::STRING | $bracket_string )/xms;
19  
20 13 my $single_char = '[.#^/>&;=]';
21 my $OPERATOR_TOKEN = sprintf(
22 "(?:%s|$single_char)",
23 13 join('|', map{ quotemeta } qw(..))
24 );
25  
26 81 ▶sub _build_identity_pattern { qr/\@?[A-Za-z_][A-Za-z0-9_?-]*/ }
Subroutine _build_identity_pattern: called CC=1 SCAR=0.0
27 81 ▶sub _build_comment_pattern { qr/\![^;]*/ }
Subroutine _build_comment_pattern: called CC=1 SCAR=0.0
28  
29 81 ▶sub _build_line_start { undef }
Subroutine _build_line_start: called CC=1 SCAR=0.0
30 81 ▶sub _build_tag_start { '{{' }
Subroutine _build_tag_start: called CC=1 SCAR=0.0
31 81 ▶sub _build_tag_end { '}}' }
Subroutine _build_tag_end: called CC=1 SCAR=0.0
32  
33 81 ▶sub _build_shortcut_table { +{} }
Subroutine _build_shortcut_table: called CC=1 SCAR=0.0
34  
35 4623 ▶sub symbol_class { 'Text::Handlebars::Symbol' }
Subroutine symbol_class: called CC=1 SCAR=0.0
Pod undocumented
36  
37 145 ▶sub split_tags {
Subroutine split_tags: called CC=35 SCAR=36.1
Pod undocumented
38 145 my $self = shift;
39 145 my ($input) = @_;
40  
41 145 my $tag_start = $self->tag_start;
42 145 my $tag_end = $self->tag_end;
43  
44 145 my $lex_comment = $self->comment_pattern;
45 145 my $lex_code = qr/(?: $lex_comment | (?: $STRING | [^\['"] ) )/xms;
46  
47 145 my @chunks;
48  
49 my @raw_text;
50 145 my @delimiters;
51  
52 145 my $close_tag;
53 145 my $standalone = 1;
54 145 ▶ while ($input) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input) 1142 145 1287
55 1142 ▶ if ($close_tag) {
Branch: 3 branches
BranchTrueFalseTotal
if ($close_tag) { } 359 783 1142
elsif ($input =~ s/\A\Q$tag_start\E//) { } 359 424 783
elsif ($input =~ s/\A([^\n]*?(?:\n|(?=\Q$tag_start\E)|\z))//) { } 424 0 424
56 359 my $start = 0;
57 359 my $pos;
58 359 ▶ while(($pos = index $input, $close_tag, $start) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($pos = index($input, $close_tag, $start)) >= 0) 359 0 359
59 359 my $code = substr $input, 0, $pos;
60 359 $code =~ s/$lex_code//g;
61 359 ▶ if(length($code) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $code == 0) 359 0 359
62 359 last;
63 }
64 0 $start = $pos + 1;
65 }
66  
67 359 ▶ if ($pos >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos >= 0) { } 359 0 359
68 359 my $code = substr $input, 0, $pos, '';
69 359 ▶ $input =~ s/\A\Q$close_tag//
Branch: 1 branch
BranchTrueFalseTotal
$input =~ s/\A\Q$close_tag\E// or die "Oops!" 359 0 359
70 or die "Oops!";
71  
72 # XXX this is ugly, but i don't know how to get the parsing
73 # right otherwise if we also need to support ^foo
74 359 ▶ $code = 'else' if $code eq '^';
Branch: 1 branch
BranchTrueFalseTotal
if $code eq "^" 6 353 359
75  
76 359 my @extra;
77  
78 359 ▶ my $autochomp = $code =~ m{^[!#^/=>]} || $code eq 'else';
Condition100%3/3
$code =~ m[^[!#^/=>]] || $code eq 'else'
l!l&&r!l&&!r
15310196
MC/DC100%2/2
$code =~ m[^[!#^/=>]] || $code eq 'else'
$code =~ m[^[!#^/=>]] $code eq 'else'
Truth table100%3/3
A || B
A= $code =~ m[^[!#^/=>]]B= $code eq 'else'
ABresult
1X1
011
000
79  
80 359 ▶ if ($code =~ s/^=\s*([^\s]+)\s+([^\s]+)\s*=$//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($code =~ s/^=\s*([^\s]+)\s+([^\s]+)\s*=$//) { } 2 357 359
elsif ($code =~ /^=/) { } 0 357 357
81 2 ($tag_start, $tag_end) = ($1, $2);
82 }
83 elsif ($code =~ /^=/) {
84 0 die "Invalid delimiter tag: $code";
85 }
86  
87 359 ▶ if ($autochomp && $standalone) {
Branch: 1 branch
BranchTrueFalseTotal
if ($autochomp and $standalone) { } 122 237 359
Condition100%3/3
$autochomp and $standalone
!ll&&!rl&&r
19641122
MC/DC100%2/2
$autochomp and $standalone
$autochomp $standalone
Truth table100%3/3
A and B
A= $autochompB= $standalone
ABresult
0X0
100
111
88 122 ▶ if ($input =~ /\A\s*(?:\n|\z)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ /\A\s*(?:\n|\z)/) 94 28 122
89 94 $input =~ s/\A$nl//;
90 94 ▶ if (@chunks > 0 && $chunks[-1][0] eq 'text' && $code !~ m{^>}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chunks > 0 and $chunks[-1][0] eq "text" and not $code =~ /^>/) 73 21 94
Condition83%5/6
@chunks > 0 and $chunks[-1][0] eq "text" and not $code =~ /^>/
!ll&&!rl&&r
21073
@chunks > 0 and $chunks[-1][0] eq "text"
!ll&&!rl&&r
16573
MC/DC66%2/3
@chunks > 0 and $chunks[-1][0] eq "text" and not $code =~ /^>/
@chunks > 0 $chunks[-1][0] eq "text" not $code =~ /^>/
Truth table75%3/4
A and B and not(C)
A= @chunks > 0B= $chunks[-1][0] eq "text"C= not $code =~ /^>/
ABCresult
0XX0
10X0
1100
1111
91 73 $chunks[-1][1] =~ s/^(?:(?!\n)\s)*\z//m;
92 73 ▶ if (@raw_text) {
Branch: 1 branch
BranchTrueFalseTotal
if (@raw_text) 49 24 73
93 49 $raw_text[-1] =~ s/^(?:(?!\n)\s)*\z//m;
94 }
95 }
96 }
97 }
98 else {
99 237 $standalone = 0;
100 }
101  
102 359 ▶ if ($code =~ m{^/} || $code eq 'else') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ m[^/] or $code eq "else") 83 276 359
Condition100%3/3
$code =~ m[^/] or $code eq "else"
l!l&&r!l&&!r
7310276
MC/DC100%2/2
$code =~ m[^/] or $code eq "else"
$code =~ m[^/] $code eq "else"
Truth table100%3/3
A or B
A= $code =~ m[^/]B= $code eq "else"
ABresult
1X1
011
000
103 83 push @extra, pop @raw_text;
104 83 push @extra, pop @delimiters;
105 83 ▶ if (@raw_text) {
Branch: 1 branch
BranchTrueFalseTotal
if (@raw_text) 9 74 83
106 9 $raw_text[-1] .= $extra[0];
107 }
108 }
109 359 ▶ if (@raw_text) {
Branch: 1 branch
BranchTrueFalseTotal
if (@raw_text) 96 263 359
110 96 ▶ if ($close_tag eq '}}}') {
Branch: 1 branch
BranchTrueFalseTotal
if ($close_tag eq '}}}') { } 8 88 96
111 8 $raw_text[-1] .= '{{{' . $code . '}}}';
112 }
113 else {
114 88 $raw_text[-1] .= $tag_start . $code . $tag_end;
115 }
116 }
117 359 ▶ if ($code =~ m{^[#^]} || $code eq 'else') {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /^[#^]/ or $code eq "else") 83 276 359
Condition100%3/3
$code =~ /^[#^]/ or $code eq "else"
l!l&&r!l&&!r
7310276
MC/DC100%2/2
$code =~ /^[#^]/ or $code eq "else"
$code =~ /^[#^]/ $code eq "else"
Truth table100%3/3
A or B
A= $code =~ /^[#^]/B= $code eq "else"
ABresult
1X1
011
000
118 83 push @raw_text, '';
119 83 push @delimiters, [$tag_start, $tag_end];
120 }
121  
122 359 ▶ if (length($code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $code) 357 2 359
123 357 ▶ push @chunks, [
Branch: 1 branch
BranchTrueFalseTotal
$close_tag eq '}}}' ? : 25 332 357
124 ($close_tag eq '}}}' ? 'raw_code' : 'code'),
125 $code,
126 @extra,
127 ];
128 }
129  
130 359 undef $close_tag;
131 }
132 else {
133 0 last; # the end tag is not found
134 }
135 }
136 elsif ($input =~ s/\A\Q$tag_start//) {
137 359 ▶ if ($tag_start eq '{{' && $input =~ s/\A\{//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag_start eq '{{' and $input =~ s/\A\{//) { } 25 334 359
Condition100%3/3
$tag_start eq "{{" and $input =~ s/\A\{//
!ll&&!rl&&r
233225
MC/DC100%2/2
$tag_start eq "{{" and $input =~ s/\A\{//
$tag_start eq "{{" $input =~ s/\A\{//
Truth table100%3/3
A and B
A= $tag_start eq "{{"B= $input =~ s/\A\{//
ABresult
0X0
100
111
138 25 $close_tag = '}}}';
139 }
140 else {
141 334 $close_tag = $tag_end;
142 }
143 }
144 elsif ($input =~ s/\A([^\n]*?(?:\n|(?=\Q$tag_start\E)|\z))//) {
145 424 my $text = $1;
146 424 ▶ if (length($text)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $text) 424 0 424
147 424 push @chunks, [ text => $text ];
148  
149 424 ▶ if ($standalone) {
Branch: 1 branch
BranchTrueFalseTotal
if ($standalone) { } 243 181 424
150 243 $standalone = $text =~ /(?:^|\n)\s*$/;
151 }
152 else {
153 181 $standalone = $text =~ /\n\s*$/;
154 }
155  
156 424 ▶ if (@raw_text) {
Branch: 1 branch
BranchTrueFalseTotal
if (@raw_text) 148 276 424
157 148 $raw_text[-1] .= $text;
158 }
159 }
160 }
161 else {
162 0 confess "Oops: unreached code, near " . p($input);
163 }
164 }
165  
166 145 ▶ if ($close_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($close_tag) 0 145 145
167 # calculate line number
168 0 my $orig_src = $_[0];
169 0 substr $orig_src, -length($input), length($input), '';
170 0 my $line = ($orig_src =~ tr/\n/\n/);
171 0 $self->_error("Malformed templates detected",
172 neat((split /\n/, $input)[0]), ++$line,
173 );
174 }
175  
176 145 return @chunks;
177 }
178  
179 145 ▶sub preprocess {
Subroutine preprocess: called CC=8 SCAR=20.8
Pod undocumented
180 145 my $self = shift;
181 145 my ($input) = @_;
182  
183 145 my @chunks = $self->split_tags($input);
184  
185 145 my $code = '';
186 145 for my $chunk (@chunks) {
187 781 my ($type, $content, $raw_text, $delimiters) = @$chunk;
188 781 ▶ if ($type eq 'text') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'text') { } 424 357 781
elsif ($type eq 'code') { } 332 25 357
elsif ($type eq 'raw_code') { } 25 0 25
189 424 $content =~ s/(["\\])/\\$1/g;
190 424 ▶ $code .= qq{print_raw "$content";\n}
Branch: 1 branch
BranchTrueFalseTotal
if length $content 392 32 424
191 if length($content);
192 }
193 elsif ($type eq 'code') {
194 332 my $extra = '';
195 332 ▶ if ($content =~ s{^/}{}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($content =~ s[^/][]) { } 73 259 332
elsif ($content eq 'else') { } 10 249 259
196 73 $chunk->[2] =~ s/(["\\])/\\$1/g;
197 73 $chunk->[3][0] =~ s/(["\\])/\\$1/g;
198 73 $chunk->[3][1] =~ s/(["\\])/\\$1/g;
199  
200 $extra = '"'
201 73 . join('" "', $chunk->[2], @{ $chunk->[3] })
202 . '"';
203 73 $code .= qq{/$extra $content;\n};
204 }
205 elsif ($content eq 'else') {
206 # XXX fix duplication
207 10 $chunk->[2] =~ s/(["\\])/\\$1/g;
208 10 $chunk->[3][0] =~ s/(["\\])/\\$1/g;
209 10 $chunk->[3][1] =~ s/(["\\])/\\$1/g;
210  
211 $extra = '"'
212 10 . join('" "', $chunk->[2], @{ $chunk->[3] })
213 . '"';
214 10 $code .= qq{$content $extra;\n};
215 }
216 else {
217 249 $code .= qq{$content;\n};
218 }
219 }
220 elsif ($type eq 'raw_code') {
221 25 $code .= qq{&$content;\n};
222 }
223 else {
224 0 $self->_error("Oops: Unknown token: $content ($type)");
225 }
226 }
227  
228 145 print STDOUT $code, "\n" if _DUMP_PROTO;
229 145 return $code;
230 }
231  
232 # XXX advance has some syntax special cases in it, probably need to override
233 # it too eventually
234  
235 81 ▶sub init_symbols {
Subroutine init_symbols: called CC=6 SCAR=17.9
Pod undocumented
236 81 my $self = shift;
237  
238 81 for my $type (qw(name key literal)) {
239 243 my $symbol = $self->symbol("($type)");
240 243 $symbol->arity($type);
241 243 $symbol->set_nud($self->can("nud_$type"));
242 243 $symbol->lbp(10);
243 }
244  
245 81 for my $this (qw(. this)) {
246 162 my $symbol = $self->symbol($this);
247 162 $symbol->arity('key');
248 162 $symbol->id('.');
249 162 $symbol->lbp(10);
250 162 $symbol->set_nud($self->can('nud_key'));
251 }
252  
253 81 for my $field_access (qw(. /)) {
254 162 $self->infix($field_access, 256, $self->can('led_dot'));
255 }
256  
257 81 for my $block ('#', '^') {
258 162 $self->symbol($block)->set_std($self->can('std_block'));
259 }
260  
261 81 for my $else (qw(/ else)) {
262 162 $self->symbol($else)->is_block_end(1);
263 }
264  
265 81 $self->symbol('>')->set_std($self->can('std_partial'));
266  
267 81 $self->symbol('&')->set_nud($self->can('nud_mark_raw'));
268 81 $self->symbol('..')->set_nud($self->can('nud_uplevel'));
269 81 $self->symbol('..')->lbp(10);
270  
271 81 $self->infix('=', 20, $self->can('led_equals'));
272 }
273  
274 # copied from Text::Xslate::Parser, but using different definitions of
275 # $STRING and $OPERATOR_TOKEN
276 2608 ▶sub tokenize {
Subroutine tokenize: called CC=6 SCAR=17.9
Pod undocumented
277 2608 my($parser) = @_;
278  
279 2608 local *_ = \$parser->{input};
280  
281 2608 my $comment_rx = $parser->comment_pattern;
282 2608 my $id_rx = $parser->identity_pattern;
283 2608 my $count = 0;
284 TRY: {
285 2608 /\G (\s*) /xmsgc;
286 2610 $count += ( $1 =~ tr/\n/\n/);
287 2610 $parser->following_newline( $count );
288  
289 2610 ▶ if(/\G $comment_rx /xmsgc) {
Branch: 5 branches
BranchTrueFalseTotal
if (/\G $comment_rx /cgmsx) { } 2 2608 2610
elsif (/\G ($id_rx)/cgmsx) { } 806 1802 2608
elsif (/\G ($Text::Xslate::Syntax::Handlebars::NUMBER | $STRING)/cgmosx) { } 650 1152 1802
elsif (/\G ($OPERATOR_TOKEN)/cgmosx) { } 1007 145 1152
elsif (/\G (\S+)/cgmsx) { } 0 145 145
290 2 redo TRY; # retry
291 }
292 elsif(/\G ($id_rx)/xmsgc){
293 806 return [ name => $1 ];
294 }
295 elsif(/\G ($NUMBER | $STRING)/xmsogc){
296 650 return [ literal => $1 ];
297 }
298 elsif(/\G ($OPERATOR_TOKEN)/xmsogc){
299 1007 return [ operator => $1 ];
300 }
301 elsif(/\G (\S+)/xmsgc) {
302 0 Carp::confess("Oops: Unexpected token '$1'");
303 }
304 else { # empty
305 145 return [ special => '(end)' ];
306 }
307 }
308 }
309  
310 112 ▶sub nud_name {
Subroutine nud_name: called CC=2 SCAR=6.9
Pod undocumented
311 112 my $self = shift;
312 112 my ($symbol) = @_;
313  
314 112 my $name = $self->SUPER::nud_name($symbol);
315  
316 112 my $call = $self->call($name);
317  
318 112 ▶ if ($self->token->is_defined) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->token->is_defined) 3 109 112
319 3 push @{ $call->second }, $self->expression(0);
320 }
321  
322 112 return $call;
323 }
324  
325 287 ▶sub nud_key {
Subroutine nud_key: called CC=1 SCAR=0.0
Pod undocumented
326 287 my $self = shift;
327 287 my ($symbol) = @_;
328  
329 287 return $symbol->clone(arity => 'key');
330 }
331  
332 48 ▶sub led_dot {
Subroutine led_dot: called CC=3 SCAR=11.0
Pod undocumented
333 48 my $self = shift;
334 48 my ($symbol, $left) = @_;
335  
336 # XXX hack to make {{{.}}} work, but in general this syntax is ambiguous
337 # and i'm not going to deal with it
338 48 ▶ if ($left->arity eq 'call' && $left->first->id eq 'mark_raw') {
Branch: 1 branch
BranchTrueFalseTotal
if ($left->arity eq "call" and $left->first->id eq "mark_raw") 1 47 48
Condition66%2/3
$left->arity eq "call" and $left->first->id eq "mark_raw"
!ll&&!rl&&r
4701
MC/DC50%1/2
$left->arity eq "call" and $left->first->id eq "mark_raw"
$left->arity eq "call" $left->first->id eq "mark_raw"
Truth table66%2/3
A and B
A= $left->arity eq "call"B= $left->first->id eq "mark_raw"
ABresult
0X0
100
111
339 1 push @{ $left->second }, $symbol->nud($self);
340 1 return $left;
341 }
342  
343 47 my $dot = $self->make_field_lookup($left, $self->token, $symbol);
344  
345 47 $self->advance;
346  
347 47 return $dot;
348 }
349  
350 73 ▶sub std_block {
Subroutine std_block: called CC=11 SCAR=24.5
Pod undocumented
351 73 my $self = shift;
352 73 my ($symbol) = @_;
353  
354 73 my $inverted = $symbol->id eq '^';
355  
356 73 my $name = $self->expression(0);
357  
358 73 ▶ if ($name->arity ne 'key' && $name->arity ne 'key_field' && $name->arity ne 'call') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name->arity ne "key" and $name->arity ne "key_field" and $name->arity ne "call") 0 73 73
Condition66%4/6
$name->arity ne "key" and $name->arity ne "key_field" and $name->arity ne "call"
!ll&&!rl&&r
35380
$name->arity ne "key" and $name->arity ne "key_field"
!ll&&!rl&&r
35038
MC/DC0%0/3
$name->arity ne "key" and $name->arity ne "key_field" and $name->arity ne "call"
$name->arity ne "key" $name->arity ne "key_field" $name->arity ne "call"
Truth table50%2/4
A and B and C
A= $name->arity ne "key"B= $name->arity ne "key_field"C= $name->arity ne "call"
ABCresult
0XX0
10X0
1100
1111
359 0 $self->_unexpected("opening block name", $self->token);
360 }
361 73 my $name_string = $self->_field_to_string($name);
362  
363 73 $self->advance(';');
364  
365 73 my %block;
366 73 my $context = 'if';
367 73 $block{$context}{body} = $self->statements;
368  
369 73 ▶ if ($self->token->id eq 'else') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->token->id eq "else") 10 63 73
370 10 $self->advance;
371  
372 10 $block{$context}{raw_text} = $self->token;
373 10 $self->advance;
374 10 $block{$context}{open_tag} = $self->token;
375 10 $self->advance;
376 10 $block{$context}{close_tag} = $self->token;
377 10 $self->advance;
378  
379 10 $context = 'else';
380 10 $block{$context}{body} = $self->statements;
381 }
382  
383 73 $self->advance('/');
384  
385 73 $block{$context}{raw_text} = $self->token;
386 73 $self->advance;
387 73 $block{$context}{open_tag} = $self->token;
388 73 $self->advance;
389 73 $block{$context}{close_tag} = $self->token;
390 73 $self->advance;
391  
392 73 ▶ if ($inverted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inverted) 1 72 73
393 1 ($block{if}, $block{else}) = ($block{else}, $block{if});
394 1 ▶ if (!$block{if}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($block{"if"}) 1 0 1
395 1 $block{if}{body} = $self->literal('');
396 1 $block{if}{raw_text} = $self->literal('');
397 1 $block{if}{open_tag} = $block{else}{open_tag};
398 1 $block{if}{close_tag} = $block{else}{close_tag};
399 }
400 }
401  
402 73 my $closing_name = $self->expression(0);
403  
404 73 ▶ if ($closing_name->arity ne 'key' && $closing_name->arity ne 'key_field' && $closing_name->arity ne 'call') {
Branch: 1 branch
BranchTrueFalseTotal
if ($closing_name->arity ne "key" and $closing_name->arity ne "key_field" and $closing_name->arity ne "call") 0 73 73
Condition66%4/6
$closing_name->arity ne "key" and $closing_name->arity ne "key_field" and $closing_name->arity ne "call"
!ll&&!rl&&r
35380
$closing_name->arity ne "key" and $closing_name->arity ne "key_field"
!ll&&!rl&&r
35038
MC/DC0%0/3
$closing_name->arity ne "key" and $closing_name->arity ne "key_field" and $closing_name->arity ne "call"
$closing_name->arity ne "key" $closing_name->arity ne "key_field" $closing_name->arity ne "call"
Truth table50%2/4
A and B and C
A= $closing_name->arity ne "key"B= $closing_name->arity ne "key_field"C= $closing_name->arity ne "call"
ABCresult
0XX0
10X0
1100
1111
405 0 $self->_unexpected("closing block name", $self->token);
406 }
407 73 my $closing_name_string = $self->_field_to_string($closing_name);
408  
409 73 ▶ if ($name_string ne $closing_name_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name_string ne $closing_name_string) 0 73 73
410 0 $self->_unexpected('/' . $name_string, $self->token);
411 }
412  
413 73 $self->advance(';');
414  
415 73 return $self->print_raw(
416 $name->clone(
417 arity => 'block',
418 first => $name,
419 second => \%block,
420 ),
421 );
422 }
423  
424 26 ▶sub nud_mark_raw {
Subroutine nud_mark_raw: called CC=1 SCAR=0.0
Pod undocumented
425 26 my $self = shift;
426 26 my ($symbol) = @_;
427  
428 26 return $self->symbol('mark_raw')->clone(
429 line => $symbol->line,
430 )->nud($self);
431 }
432  
433 17 ▶sub nud_uplevel {
Subroutine nud_uplevel: called CC=1 SCAR=0.0
Pod undocumented
434 17 my $self = shift;
435 17 my ($symbol) = @_;
436  
437 17 return $symbol->clone(arity => 'variable');
438 }
439  
440 3 ▶sub std_partial {
Subroutine std_partial: called CC=3 SCAR=11.0
Pod undocumented
441 3 my $self = shift;
442 3 my ($symbol) = @_;
443  
444 3 my $partial = $self->token->clone(arity => 'literal');
445 3 $self->advance;
446 3 my $args;
447 3 ▶ if ($self->token->id ne ';') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->token->id ne ";") 1 2 3
448 1 $args = $self->expression(0);
449 }
450 3 $self->advance(';');
451  
452 3 ▶ return $symbol->clone(
Branch: 1 branch
BranchTrueFalseTotal
$partial->id =~ /\./ ? : 0 3 3
453 arity => 'partial',
454 first => ($partial->id =~ /\./ ? $partial : [ $partial ]),
455 second => $args,
456 );
457 }
458  
459 4 ▶sub led_equals {
Subroutine led_equals: called CC=1 SCAR=0.0
Pod undocumented
460 4 my $self = shift;
461 4 my ($symbol, $left) = @_;
462  
463 4 my $right = $self->expression($symbol->lbp);
464  
465 4 return $symbol->clone(
466 arity => 'pair',
467 first => $left->clone(arity => 'literal'),
468 second => $right,
469 );
470 }
471  
472 283 ▶sub undefined_name {
Subroutine undefined_name: called CC=1 SCAR=0.0
Pod undocumented
473 283 my $self = shift;
474 283 my ($name) = @_;
475  
476 283 return $self->symbol('(key)')->clone(id => $name);
477 }
478  
479 243 ▶sub define_function {
Subroutine define_function: called CC=2 SCAR=6.9
Pod undocumented
480 243 my $self = shift;
481 243 my (@names) = @_;
482  
483 243 $self->SUPER::define_function(@_);
484 243 for my $name (@names) {
485 1651 my $symbol = $self->symbol($name);
486 1651 $symbol->set_nud($self->can('nud_name'));
487 1651 $symbol->lbp(10);
488 }
489  
490 243 return;
491 }
492  
493 162 ▶sub define_helper {
Subroutine define_helper: called CC=2 SCAR=6.9
Pod undocumented
494 162 my $self = shift;
495 162 my (@names) = @_;
496  
497 162 $self->define_function(@names);
498 162 for my $name (@names) {
499 343 my $symbol = $self->symbol($name);
500 343 $symbol->is_helper(1);
501 }
502  
503 162 return;
504 }
505  
506 650 ▶sub parse_literal {
Subroutine parse_literal: called CC=2 SCAR=6.9
Pod undocumented
507 650 my $self = shift;
508 650 my ($literal) = @_;
509  
510 650 ▶ if ($literal =~ /\A\[(.*)\]\z/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($literal =~ /\A\[(.*)\]\z/ms) 4 646 650
511 4 $literal = $1;
512 4 $literal =~ s/(["\\])/\\$1/g;
513 4 $literal = '"' . $literal . '"';
514 }
515  
516 650 return $self->SUPER::parse_literal($literal);
517 }
518  
519 47 ▶sub is_valid_field {
Subroutine is_valid_field: called CC=4 SCAR=14.0
Pod undocumented
520 47 my $self = shift;
521 47 my ($field) = @_;
522  
523 # allow foo.[10]
524 47 ▶ return 1 if $field->arity eq 'literal';
Branch: 1 branch
BranchTrueFalseTotal
if $field->arity eq "literal" 3 44 47
525 # undefined symbols are all treated as keys - see undefined_name
526 44 ▶ return 1 if $field->arity eq 'key';
Branch: 1 branch
BranchTrueFalseTotal
if $field->arity eq "key" 33 11 44
527 # allow ../../foo
528 11 ▶ return 1 if $field->id eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $field->id eq ".." 11 0 11
529  
530 0 return;
531 }
532  
533 812 ▶sub expression {
Subroutine expression: called CC=4 SCAR=13.9
Pod undocumented
534 812 my $self = shift;
535 812 my ($rbp) = @_;
536  
537 812 my $token = $self->token;
538 812 $self->advance;
539 812 my $left = $token->nud($self);
540  
541 812 ▶ while ($rbp < $self->token->lbp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rbp < $self->token->lbp) 122 812 934
542 122 $token = $self->token;
543 122 ▶ if ($token->has_led) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->has_led) { } 52 70 122
544 52 $self->advance;
545 52 $left = $token->led($self, $left);
546 }
547 else {
548 70 ▶ if ($left->arity ne 'call') {
Branch: 1 branch
BranchTrueFalseTotal
if ($left->arity ne "call") 0 70 70
549 0 $self->_error("Unexpected " . $token->arity, $left);
550 }
551 70 push @{ $left->second }, $self->expression($token->lbp);
552 }
553 }
554  
555 812 return $left;
556 }
557  
558 932 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod undocumented
559 932 my $self = shift;
560  
561 932 my $call = $self->SUPER::call(@_);
562 932 $call->is_helper($call->first->is_helper);
563 932 return $call;
564 }
565  
566 47 ▶sub make_field_lookup {
Subroutine make_field_lookup: called CC=3 SCAR=12.3
Pod undocumented
567 47 my $self = shift;
568 47 my ($var, $field, $dot) = @_;
569  
570 47 ▶ if (!$self->is_valid_field($field)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_valid_field($field)) 0 47 47
571 0 $self->_unexpected("a field name", $field);
572 }
573  
574 47 ▶ $dot ||= $self->symbol('.');
Condition33%1/3
$dot ||= $self->symbol(".")
l!l&&r!l&&!r
4700
MC/DC0%0/2
$dot ||= $self->symbol(".")
$dot $self->symbol(".")
Truth table33%1/3
A ||= B
A= $dotB= $self->symbol(".")
ABresult
1X1
011
000
575  
576 47 return $dot->clone(
577 arity => 'key_field',
578 first => $var,
579 second => $field->clone(arity => 'literal'),
580 );
581 }
582  
583 73 ▶sub print_raw {
Subroutine print_raw: called CC=1 SCAR=0.0
Pod undocumented
584 73 my $self = shift;
585 73 return $self->print(@_)->clone(id => 'print_raw');
586 }
587  
588 384 ▶sub literal {
Subroutine literal: called CC=1 SCAR=0.0
Pod undocumented
589 384 my $self = shift;
590 384 my ($value) = @_;
591 384 return $self->symbol('(literal)')->clone(id => $value);
592 }
593  
594 146 ▶sub _field_to_string {
Subroutine _field_to_string: called CC=2 SCAR=7.6
595 146 my $self = shift;
596 146 my ($symbol) = @_;
597  
598 # name and key can just be returned
599 146 ▶ return $symbol->id
Branch: 1 branch
BranchTrueFalseTotal
unless $symbol->arity eq "field" 146 0 146
600 unless $symbol->arity eq 'field';
601  
602 # field accesses should recurse on the first and append the second
603 0 return $self->_field_to_string($symbol->first) . '.' . $symbol->second->id;
604 }
605  
606 13 __PACKAGE__->meta->make_immutable;
607 13 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
608  
609 =for Pod::Coverage
610 call
611 define_function
612 define_helper
613 expression
614 init_symbols
615 is_valid_field
616 led_dot
617 led_equals
618 literal
619 make_field_lookup
620 nud_key
621 nud_mark_raw
622 nud_name
623 nud_uplevel
624 parse_literal
625 preprocess
626 print_raw
627 split_tags
628 std_block
629 std_partial
630 symbol_class
631 tokenize
632 undefined_name
633  
634 =cut
635  
636 13 1;
↑ Summary Handlebars/Compiler.pm »