MustacheTemplate/Parser.pm

filter: Statementstmt 100% 160 / 164 (4 uncoverable) Branchbran 98.8% 82 / 86 (3 uncoverable) Conditioncond 96.5% 27 / 29 (1 uncoverable) MC/DCmcdc 93.7% 14 / 16 (1 uncoverable) Subroutinesub 100% 15 / 15 Podpod 100% 1 / 1 totaltotal 99.0% 299 / 311 (9 uncoverable) CC 56 SCAR 35.2
CC
56
Cov
99%
_parse
48.0
_error
6.0
_needs_trim_around_whitespaces
4.0
CRAP 33.8
↑ Summary MustacheTemplate/Compiler.pm »
1 package Text::MustacheTemplate::Parser;
2 14 ▶use 5.022000;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 14 ▶use Text::MustacheTemplate::Lexer qw/:types/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use Text::MustacheTemplate::Generator;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 14 our %EXPORT_TAGS = (
13 syntaxes => [qw/SYNTAX_RAW_TEXT SYNTAX_VARIABLE SYNTAX_BOX SYNTAX_COMMENT SYNTAX_PARTIAL SYNTAX_DELIMITER/],
14 variables => [qw/VARIABLE_HTML_ESCAPE VARIABLE_RAW/],
15 boxes => [qw/BOX_SECTION BOX_INVERTED_SECTION BOX_BLOCK BOX_PARENT/],
16 references => [qw/REFERENCE_STATIC REFERENCE_DYNAMIC/],
17 );
18 14 our @EXPORT_OK = map @$_, values %EXPORT_TAGS;
19  
20 14 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
21 # enum
22 14 SYNTAX_RAW_TEXT => 0,
23 SYNTAX_VARIABLE => 1,
24 SYNTAX_BOX => 2,
25 SYNTAX_COMMENT => 3,
26 SYNTAX_PARTIAL => 4,
27 SYNTAX_DELIMITER => 5,
28 14 };
29  
30 14 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
31 # enum
32 14 VARIABLE_HTML_ESCAPE => 0,
33 VARIABLE_RAW => 1,
34 14 };
35  
36 14 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
37 # enum
38 14 BOX_SECTION => 0,
39 BOX_INVERTED_SECTION => 1,
40 BOX_BLOCK => 2,
41 BOX_PARENT => 3,
42 14 };
43  
44 14 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
45 # enum
46 14 REFERENCE_STATIC => 0,
47 REFERENCE_DYNAMIC => 1,
48 14 };
49  
50 14 my %TRIM_INNER_WHITESPACE_TYPES = map { $_ => 1 } (qw({ & ^ $ < >), '#', '/'); # set
51 14 my %TRIM_AROUND_WHITESPACE_TYPES = map { $_ => 1 } (qw(! ^ $ < >), '#', '/');
52  
53 14 our $SOURCE; # optional for error reporting and optimized lambda support
54  
55 546 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
56 546 my $class = shift;
57 546 my $ast = _parse(@_);
58 536 return $ast;
59 }
60  
61 546 ▶sub _parse {
Subroutine _parse: called CC=48 SCAR=38.7
62 546 my @tokens = @_;
63  
64 546 my $last_delimiter_token;
65 my @root;
66 546 my @stack;
67 546 my $ast = \@root;
68 546 for my $i (0..$#tokens) {
69 3156 my $token = $tokens[$i];
70  
71 3156 my ($type, $pos) = @$token;
72 3156 ▶ if ($type == TOKEN_RAW_TEXT) { # uncoverable branch false count:4
Branch: 4 branches
BranchTrueFalseTotal
if ($type == 0) { } 1226 1930 3156
elsif ($type == 1) { } 91 1839 1930
elsif ($type == 2) { } 1256 583 1839
elsif ($type == 3) { } 583 0 583
73 1226 my (undef, undef, $text) = @$token;
74 1226 ▶ if ($i != 1) { # optimized $i >= 2: comes delimiter token when $i == 0 always, so the first raw text token should be $i == 1.
Branch: 1 branch
BranchTrueFalseTotal
if ($i != 1) 850 376 1226
75 850 my $prev = $i-1; # $prev >= 1
76 850 ▶ if (_needs_trim_around_whitespaces($tokens[$prev])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_needs_trim_around_whitespaces($tokens[$prev])) 621 229 850
77 621 ▶ while ($prev != 1 && _needs_trim_around_whitespaces($tokens[$prev-1])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev != 1 and _needs_trim_around_whitespaces($tokens[$prev - 1])) 54 621 675
Condition100%3/3
$prev != 1 and _needs_trim_around_whitespaces($tokens[$prev - 1])
!ll&&!rl&&r
9852354
MC/DC100%2/2
$prev != 1 and _needs_trim_around_whitespaces($tokens[$prev - 1])
$prev != 1 _needs_trim_around_whitespaces($tokens[$prev - 1])
Truth table100%3/3
A and B
A= $prev != 1B= _needs_trim_around_whitespaces($tokens[$prev - 1])
ABresult
0X0
100
111
78 54 $prev--;
79 }
80 621 ▶ my $before_prev_token = $prev != 1 ? $tokens[$prev-1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$prev != 1 ? : 523 98 621
81 621 ▶ my $is_empty_before_prev = !defined $before_prev_token || $before_prev_token->[0] == TOKEN_PADDING || (
Condition100%9/9
!defined($before_prev_token) || $before_prev_token->[0] == 1 || $before_prev_token->[0] == 0 && $before_prev_token->[2] =~ /[\r\n]\z/amno
l!l&&r!l&&!r
158112351
not defined $before_prev_token or $before_prev_token->[0] == 1
l!l&&r!l&&!r
9860463
$before_prev_token->[0] == 0 and $before_prev_token->[2] =~ /[\r\n]\z/amno
!ll&&!rl&&r
28323112
MC/DC100%4/4
!defined($before_prev_token) || $before_prev_token->[0] == 1 || $before_prev_token->[0] == 0 && $before_prev_token->[2] =~ /[\r\n]\z/amno
not defined $before_prev_token $before_prev_token->[0] == 1 $before_prev_token->[0] == 0 $before_prev_token->[2] =~ /[\r\n]\z/amno
Truth table100%5/5
not(A) or B || C and D
A= not defined $before_prev_tokenB= $before_prev_token->[0] == 1C= $before_prev_token->[0] == 0D= $before_prev_token->[2] =~ /[\r\n]\z/amno
ABCDresult
1XXX1
01XX1
00111
000X0
00100
82 $before_prev_token->[0] == TOKEN_RAW_TEXT && $before_prev_token->[2] =~ /[\r\n]\z/mano
83 );
84 621 ▶ if ($is_empty_before_prev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_empty_before_prev) 270 351 621
85 270 $text =~ s/\A(\r\n|[\r\n])//mano;
86 }
87 }
88 }
89 1226 ▶ push @$ast => [SYNTAX_RAW_TEXT, $text] if $text;
Branch: 1 branch
BranchTrueFalseTotal
if $text 1167 59 1226
90 } elsif ($type == TOKEN_PADDING) {
91 91 my (undef, undef, $padding) = @$token;
92 91 my $needs_padding = 1;
93 91 ▶ if ($i == $#tokens) { # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $#tokens) 0 91 91
94 0 _error($token, 'Syntax Error: Padding token should not be last'); # uncoverable statement
95 }
96  
97 91 my $next = $i+1;
98 91 ▶ if (_needs_trim_around_whitespaces($tokens[$next])) {
Branch: 1 branch
BranchTrueFalseTotal
if (_needs_trim_around_whitespaces($tokens[$next])) 73 18 91
99 73 ▶ while ($next != $#tokens && _needs_trim_around_whitespaces($tokens[$next+1])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next != $#tokens and _needs_trim_around_whitespaces($tokens[$next + 1])) 1 73 74
Condition100%3/3
$next != $#tokens and _needs_trim_around_whitespaces($tokens[$next + 1])
!ll&&!rl&&r
12611
MC/DC100%2/2
$next != $#tokens and _needs_trim_around_whitespaces($tokens[$next + 1])
$next != $#tokens _needs_trim_around_whitespaces($tokens[$next + 1])
Truth table100%3/3
A and B
A= $next != $#tokensB= _needs_trim_around_whitespaces($tokens[$next + 1])
ABresult
0X0
100
111
100 1 $next++;
101 }
102 73 ▶ my $after_next_token = $next != $#tokens ? $tokens[$next+1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$next != $#tokens ? : 61 12 73
103 73 ▶ my $is_empty_after_next = !defined $after_next_token || (
Condition83%5/6
!defined($after_next_token) || $after_next_token->[0] == 0 && $after_next_token->[2] =~ /\A[\r\n]/amno
l!l&&r!l&&!r
124318
$after_next_token->[0] == 0 and $after_next_token->[2] =~ /\A[\r\n]/amno
!ll&&!rl&&r
01843
MC/DC66%2/3
!defined($after_next_token) || $after_next_token->[0] == 0 && $after_next_token->[2] =~ /\A[\r\n]/amno
!defined($after_next_token) $after_next_token->[0] == 0 $after_next_token->[2] =~ /\A[\r\n]/amno
Truth table75%3/4
not(A) || B and C
A= !defined($after_next_token)B= $after_next_token->[0] == 0C= $after_next_token->[2] =~ /\A[\r\n]/amno
ABCresult
1XX1
0111
00X0
0100
104 $after_next_token->[0] == TOKEN_RAW_TEXT && $after_next_token->[2] =~ /\A[\r\n]/mano
105 );
106 73 $needs_padding = !$is_empty_after_next;
107 }
108 91 ▶ if ($needs_padding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needs_padding) 36 55 91
109 36 push @$ast => [SYNTAX_RAW_TEXT, $padding];
110 }
111 } elsif ($type == TOKEN_TAG) {
112 1256 ▶ if (@$token == 3) { # uncoverable branch false count:2
Branch: 2 branches
BranchTrueFalseTotal
if (@$token == 3) { } 325 931 1256
elsif (@$token == 4) { } 931 0 931
113 325 my (undef, undef, $tag_body) = @$token;
114 325 ▶ _error($token, 'Syntax Error: Must not contain newlines') if $tag_body =~ /[\r\n]/mo;
Branch: 1 branch
BranchTrueFalseTotal
if $tag_body =~ /[\r\n]/mou 2 323 325
115 323 $tag_body =~ s/^\s+//ano;
116 323 $tag_body =~ s/\s+$//ano;
117 323 push @$ast => [SYNTAX_VARIABLE, VARIABLE_HTML_ESCAPE, $tag_body];
118 } elsif (@$token == 4) {
119 931 my (undef, undef, $tag_type, $tag_body) = @$token;
120 931 ▶ if ($TRIM_INNER_WHITESPACE_TYPES{$tag_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($TRIM_INNER_WHITESPACE_TYPES{$tag_type}) 897 34 931
121 897 ▶ _error($token, 'Syntax Error: Must not contain newlines') if $tag_body =~ /[\r\n]/mo;
Branch: 1 branch
BranchTrueFalseTotal
if $tag_body =~ /[\r\n]/mou 2 895 897
122 895 $tag_body =~ s/^\s+//ano;
123 895 $tag_body =~ s/\s+$//ano;
124 }
125  
126 929 ▶ if ($tag_type eq '{' || $tag_type eq '&') { # uncoverable branch false count:8
Branch: 8 branches
BranchTrueFalseTotal
if ($tag_type eq '{' or $tag_type eq '&') { } 65 864 929
elsif ($tag_type eq '!') { } 34 830 864
elsif ($tag_type eq '#') { } 154 676 830
elsif ($tag_type eq '^') { } 64 612 676
elsif ($tag_type eq '$') { } 105 507 612
elsif ($tag_type eq '<') { } 53 454 507
elsif ($tag_type eq '/') { } 372 82 454
elsif ($tag_type eq '>') { } 82 0 82
Condition100%3/3
$tag_type eq "{" or $tag_type eq "&"
l!l&&r!l&&!r
3926864
MC/DC100%2/2
$tag_type eq "{" or $tag_type eq "&"
$tag_type eq "{" $tag_type eq "&"
Truth table100%3/3
A or B
A= $tag_type eq "{"B= $tag_type eq "&"
ABresult
1X1
011
000
127 65 push @$ast => [SYNTAX_VARIABLE, VARIABLE_RAW, $tag_body];
128 } elsif ($tag_type eq '!') {
129 34 push @$ast => [SYNTAX_COMMENT, $tag_body];
130 } elsif ($tag_type eq '#') {
131 154 push @stack => [$i, $tag_body, [SYNTAX_BOX, BOX_SECTION, $tag_body], $ast];
132 154 $ast = [];
133 } elsif ($tag_type eq '^') {
134 64 push @stack => [$i, $tag_body, [SYNTAX_BOX, BOX_INVERTED_SECTION, $tag_body], $ast];
135 64 $ast = [];
136 } elsif ($tag_type eq '$') {
137 105 push @stack => [$i, $tag_body, [SYNTAX_BOX, BOX_BLOCK, $tag_body], $ast];
138 105 $ast = [];
139 } elsif ($tag_type eq '<') {
140 53 my $name = $tag_body;
141 53 my $syntax = do {
142 53 my $is_dynamic = substr($name,0,1) eq '*';
143 53 ▶ if ($is_dynamic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_dynamic) { } 3 50 53
144 3 $name = substr $name, 1;
145 3 [SYNTAX_BOX, BOX_PARENT, REFERENCE_DYNAMIC, $name];
146 } else {
147 50 [SYNTAX_BOX, BOX_PARENT, REFERENCE_STATIC, $name];
148 }
149 };
150 53 push @stack => [$i, $tag_body, $syntax, $ast];
151 53 $ast = [];
152 } elsif ($tag_type eq '/') {
153 372 ▶ _error($token, 'Syntax Error: Unbalanced Section') unless @stack;
Branch: 1 branch
BranchTrueFalseTotal
unless @stack 1 371 372
154 371 my $item = pop @stack;
155 371 my ($open_idx, $open_tag_body, $syntax, $parent) = @$item;
156 371 s/\s*//go for ($open_tag_body, $tag_body);
157 371 ▶ if ($open_tag_body ne $tag_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($open_tag_body ne $tag_body) 2 369 371
158 2 _error($token, "Syntax Error: Unbalanced Section: open=$open_tag_body close=$tag_body");
159 }
160 369 ▶ if ($syntax->[1] == BOX_SECTION) {
Branch: 1 branch
BranchTrueFalseTotal
if ($syntax->[1] == 0) 147 222 369
161 # keep inner template to support lambda
162 147 ▶ my $inner_template = defined $SOURCE
Branch: 1 branch
BranchTrueFalseTotal
defined $Text::MustacheTemplate::Parser::SOURCE ? : 143 4 147
163 ? substr $SOURCE, $tokens[$open_idx+1][1], $pos-$tokens[$open_idx+1][1]
164 : Text::MustacheTemplate::Generator->generate_from_tokens($last_delimiter_token, @tokens[$open_idx+1..$i-1]);
165 147 push @$syntax => $inner_template;
166 }
167 369 push @$syntax => $ast;
168 369 $ast = $parent;
169 369 push @$ast => $syntax;
170 } elsif ($tag_type eq '>') {
171 82 my $name = $tag_body;
172 82 $name =~ s/\s*//go;
173  
174 # comes delimiter token when $i == 0 always, so the first tag token should be $i == 1.
175 82 my $padding;
176 82 ▶ if ($i != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i != 1) 75 7 82
177 75 my $prev = $tokens[$i-1];
178 75 ▶ if ($prev->[0] == TOKEN_PADDING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->[0] == 1) 14 61 75
179 14 $padding = $prev->[2] ;
180 14 push @$ast => [SYNTAX_RAW_TEXT, $padding];
181 }
182 }
183  
184 82 my $is_dynamic = substr($name,0,1) eq '*';
185 82 ▶ if ($is_dynamic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_dynamic) { } 48 34 82
186 48 $name = substr $name, 1;
187 48 push @$ast => [SYNTAX_PARTIAL, REFERENCE_DYNAMIC, $name, $padding];
188 } else {
189 34 push @$ast => [SYNTAX_PARTIAL, REFERENCE_STATIC, $name, $padding];
190 }
191 } else {
192 0 _error($token, "Syntax Error: Unknown Tag Type: '$tag_type'"); # uncoverable statement
193 }
194 } else {
195 0 _error($token, 'Syntax Error: Unknown Token'); # uncoverable statement
196 }
197 } elsif ($type == TOKEN_DELIMITER) {
198 583 my @delimiters = @$token[3,4];
199 583 $last_delimiter_token = $token;
200 583 push @$ast => [SYNTAX_DELIMITER, @delimiters];
201 } else {
202 0 _error($token, 'Syntax Error: Unknown Token'); # uncoverable statement
203 }
204 }
205 539 ▶ if (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 3 536 539
206 3 my $item = pop @stack;
207 3 my (undef, undef, $open_token) = @$item;
208 3 _error($open_token, 'Syntax Error: Unbalanced Section');
209 }
210 536 return \@root;
211 }
212  
213 1580 ▶sub _needs_trim_around_whitespaces {
Subroutine _needs_trim_around_whitespaces: called CC=4 SCAR=13.9
214 1580 my $token = shift;
215 1580 my ($type) = @$token;
216  
217 1580 ▶ if ($type == TOKEN_DELIMITER) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 3) { } 40 1540 1580
elsif ($type == 2) { } 984 556 1540
218 40 my (undef, undef, $tag_body) = @$token;
219 40 return defined $tag_body; ## tag body is undef when first implicit TOKEN_DELIMITER
220 } elsif ($type == TOKEN_TAG) {
221 984 ▶ if (@$token == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$token == 4) { } 771 213 984
222 771 my (undef, undef, $tag_type, undef) = @$token;
223 771 return !!$TRIM_AROUND_WHITESPACE_TYPES{$tag_type};
224 } else {
225 213 return !!0;
226 }
227 }
228 556 return !!0;
229 }
230  
231 10 ▶sub _error {
Subroutine _error: called CC=6 SCAR=17.9
232 10 my ($token, $msg) = @_;
233 10 ▶ croak $msg unless $SOURCE;
Branch: 1 branch
BranchTrueFalseTotal
unless $Text::MustacheTemplate::Parser::SOURCE 1 9 10
234  
235 9 my $src = $SOURCE;
236 9 my $curr = $token->[1];
237 9 my $line = 1;
238 9 my $start = 0;
239 9 ▶ while ($src =~ /$/smgco and pos $src <= $curr) {# uncoverable condition left
Branch: 1 branch
BranchTrueFalseTotal
if ($src =~ /$/cgmosu and pos $src <= $curr) 2 9 11
Condition100%2/3
$src =~ /$/cgmosu and pos $src <= $curr
!ll&&!rl&&r
092
MC/DC100%1/2
$src =~ /$/cgmosu and pos $src <= $curr
$src =~ /$/cgmosu pos $src <= $curr
Truth table100%2/3
A and B
A= $src =~ /$/cgmosuB= pos $src <= $curr
ABresult
0X0
100
111
240 2 $start = pos $src;
241 2 $line++;
242 }
243 9 my $end = pos $src;
244 9 my $len = $curr - $start;
245 9 ▶ $len-- if $len > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len > 0 2 7 9
246  
247 9 ▶ my $trace = join "\n",
Condition100%2/2
$start || 0
l!l
18
MC/DC100%1/1
$start || 0
$start
Truth table100%2/2
A
A= $start
Aresult
11
00
248 "${msg}: line:$line",
249 substr($src, $start || 0, $end - $start),
250 (' ' x $len) . '^';
251 9 croak $trace, "\n";
252 }
253  
254 14 1;
255 __END__
↑ Summary MustacheTemplate/Compiler.pm »