lib/Template/Parser.pm

filter: Statementstmt 97.3% 336 / 345 Branchbran 87.5% 161 / 184 Conditioncond 75.0% 66 / 88 MC/DCmcdc 56.8% 29 / 51 Subroutinesub 100% 29 / 29 Podpod 11.7% 2 / 17 totaltotal 87.2% 623 / 714 CC 130 SCAR 29.6
CC
130
Cov
91%
split_text
39.9
_parse
27.2
tokenise_directive
18.0
CRAP 19.3
« lib/Template/Plugin/Date.pm ↑ Summary lib/Template/Context.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Parser
4 #
5 # DESCRIPTION
6 # This module implements a LALR(1) parser and associated support
7 # methods to parse template documents into the appropriate "compiled"
8 # format. Much of the parser DFA code (see _parse() method) is based
9 # on Francois Desarmenien's Parse::Yapp module. Kudos to him.
10 #
11 # AUTHOR
12 # Andy Wardley <abw@wardley.org>
13 #
14 # COPYRIGHT
15 # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved.
16 #
17 # This module is free software; you can redistribute it and/or
18 # modify it under the same terms as Perl itself.
19 #
20 # The following copyright notice appears in the Parse::Yapp
21 # documentation.
22 #
23 # The Parse::Yapp module and its related modules and shell
24 # scripts are copyright (c) 1998 Francois Desarmenien,
25 # France. All rights reserved.
26 #
27 # You may use and distribute them under the terms of either
28 # the GNU General Public License or the Artistic License, as
29 # specified in the Perl README file.
30 #
31 #============================================================================
32  
33 package Template::Parser;
34  
35 106 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 106 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 106 ▶use base 'Template::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 106 ▶use Template::Constants qw( :status :chomp );
Subroutine BEGIN: called CC=1 SCAR=0.0
40 106 ▶use Template::Directive;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 106 ▶use Template::Grammar;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # parser state constants
44 106 ▶use constant CONTINUE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 106 ▶use constant ACCEPT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 106 ▶use constant ERROR => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 106 ▶use constant ABORT => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 106 our $VERSION = '3.106';
50 106 ▶our $DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Template::Parser::DEBUG 106 0 106
51 106 our $ERROR = '';
52  
53 # The ANYCASE option can cause conflicts when reserved words are used as
54 # variable names, hash keys, template names, plugin names, etc. The
55 #
56 # $ANYCASE_BEFORE regex identifies where such a word precedes an assignment,
57 # either as a variable (C<wrapper = 'html'>) or hash key (C<{ wrapper => 'html' }).
58 # In that case it is treated as a simple words rather than being the lower case
59 # equivalent of the upper case keyword (e.g. WRAPPER).
60 #
61 # $ANYCASE_AFTER is used to identify when such a word follows a symbols that
62 # suggests it can't be a keyword, e.g. after BLOCK INCLUDE WRAPPER, USE, etc.
63 106 our $ANYCASE_BEFORE = qr/\G((?=\s*[=\.]))/;
64 our $ANYCASE_AFTER = {
65 106 map { $_ => 1 }
66 qw(
67 GET SET CALL DEFAULT INSERT INCLUDE PROCESS WRAPPER BLOCK USE
68 PLUGIN FILTER MACRO IN TO STEP AND OR NOT DIV MOD DOT
69 IF UNLESS ELSIF FOR WHILE SWITCH CASE META THROW CATCH VIEW
70 CMPOP BINOP COMMA
71 ),
72 '(', '[', '{'
73 # not sure about ASSIGN as it breaks C<header_html = include header>
74 };
75  
76  
77 #========================================================================
78 # -- COMMON TAG STYLES --
79 #========================================================================
80  
81 106 our $TAG_STYLE = {
82 'outline' => [ '\[%', '%\]', '%%' ], # NEW! Outline tag
83 'default' => [ '\[%', '%\]' ],
84 'template1' => [ '[\[%]%', '%[\]%]' ],
85 'metatext' => [ '%%', '%%' ],
86 'html' => [ '<!--', '-->' ],
87 'mason' => [ '<%', '>' ],
88 'asp' => [ '<%', '%>' ],
89 'php' => [ '<\?', '\?>' ],
90 'star' => [ '\[\*', '\*\]' ],
91 };
92 106 $TAG_STYLE->{ template } = $TAG_STYLE->{ tt2 } = $TAG_STYLE->{ default };
93  
94  
95 our $DEFAULT_STYLE = {
96 START_TAG => $TAG_STYLE->{ default }->[0],
97 END_TAG => $TAG_STYLE->{ default }->[1],
98 106 OUTLINE_TAG => $TAG_STYLE->{ default }->[2],
99 # TAG_STYLE => 'default',
100 ANYCASE => 0,
101 INTERPOLATE => 0,
102 PRE_CHOMP => 0,
103 POST_CHOMP => 0,
104 V1DOLLAR => 0,
105 EVAL_PERL => 0,
106 };
107  
108 106 our $QUOTED_ESCAPES = {
109 n => "\n",
110 r => "\r",
111 t => "\t",
112 };
113  
114 # note that '-' must come first so Perl doesn't think it denotes a range
115 106 our $CHOMP_FLAGS = qr/[-=~+]/;
116  
117  
118  
119 #========================================================================
120 # ----- PUBLIC METHODS -----
121 #========================================================================
122  
123 #------------------------------------------------------------------------
124 # new(\%config)
125 #
126 # Constructor method.
127 #------------------------------------------------------------------------
128  
129 236 ▶sub new {
Subroutine new: called CC=12 SCAR=25.3
Pod documented
130 236 my $class = shift;
131 236 ▶ my $config = $_[0] && ref($_[0]) eq 'HASH' ? shift(@_) : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && ref $_[0] eq 'HASH' ? : 234 2 236
Condition100%3/3
$_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
11234
MC/DC100%2/2
$_[0] and ref $_[0] eq "HASH"
$_[0] ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
132 236 my ($tagstyle, $debug, $start, $end, $defaults, $grammar, $hash, $key, $udef);
133  
134 my $self = bless {
135 START_TAG => undef,
136 END_TAG => undef,
137 OUTLINE_TAG => undef,
138 TAG_STYLE => 'default',
139 ANYCASE => 0,
140 INTERPOLATE => 0,
141 PRE_CHOMP => 0,
142 POST_CHOMP => 0,
143 V1DOLLAR => 0,
144 EVAL_PERL => 0,
145 FILE_INFO => 1,
146 GRAMMAR => undef,
147 _ERROR => '',
148 IN_BLOCK => [ ],
149 TRACE_VARS => $config->{ TRACE_VARS },
150 236 ▶ FACTORY => $config->{ FACTORY } || 'Template::Directive',
Condition100%2/2
$config->{'FACTORY'} || 'Template::Directive'
l!l
2234
MC/DC100%1/1
$config->{'FACTORY'} || 'Template::Directive'
$config->{'FACTORY'}
Truth table100%2/2
A
A= $config->{'FACTORY'}
Aresult
11
00
151 }, $class;
152  
153 # update self with any relevant keys in config
154 236 foreach $key (keys %$self) {
155 3776 ▶ $self->{ $key } = $config->{ $key } if defined $config->{ $key };
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{$key} 104 3672 3776
156 }
157 236 $self->{ FILEINFO } = [ ];
158  
159 # DEBUG config item can be a bitmask
160 236 ▶ if (defined ($debug = $config->{ DEBUG })) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined($debug = $config->{'DEBUG'})) { } 15 221 236
elsif ($Template::Parser::DEBUG == 1) { } 0 221 221
161 15 $self->{ DEBUG } = $debug & ( Template::Constants::DEBUG_PARSER
162 | Template::Constants::DEBUG_FLAGS );
163 15 $self->{ DEBUG_DIRS } = $debug & Template::Constants::DEBUG_DIRS;
164 }
165 # package variable can be set to 1 to support previous behaviour
166 elsif ($DEBUG == 1) {
167 0 $self->{ DEBUG } = Template::Constants::DEBUG_PARSER;
168 0 $self->{ DEBUG_DIRS } = 0;
169 }
170 # otherwise let $DEBUG be a bitmask
171 else {
172 221 $self->{ DEBUG } = $DEBUG & ( Template::Constants::DEBUG_PARSER
173 | Template::Constants::DEBUG_FLAGS );
174 221 $self->{ DEBUG_DIRS } = $DEBUG & Template::Constants::DEBUG_DIRS;
175 }
176  
177 236 ▶ $grammar = $self->{ GRAMMAR } ||= do {
Condition33%1/3
$self->{"GRAMMAR"} ||= do { require Template::Grammar; "Template::Grammar"->new }
l!l&&r!l&&!r
02360
MC/DC0%0/2
$self->{"GRAMMAR"} ||= do { require Template::Grammar; "Template::Grammar"->new }
$self->{"GRAMMAR"} do { require Template::Grammar; "Template::Grammar"->new }
Truth table33%1/3
A ||= B
A= $self->{"GRAMMAR"}B= do { require Template::Grammar; "Template::Grammar"->new }
ABresult
1X1
011
000
178 236 require Template::Grammar;
179 236 Template::Grammar->new();
180 };
181  
182 # instantiate a FACTORY object
183 236 ▶ unless (ref $self->{ FACTORY }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"FACTORY"}) 235 1 236
184 235 my $fclass = $self->{ FACTORY };
185 $self->{ FACTORY } = $self->{ FACTORY }->new(
186 NAMESPACE => $config->{ NAMESPACE }
187 )
188 235 ▶ || return $class->error($self->{ FACTORY }->error());
Condition50%1/2
$self->{'FACTORY'}->new('NAMESPACE', $config->{'NAMESPACE'}) || (return $class->error($self->{'FACTORY'}->error))
l!l
2350
MC/DC0%0/1
$self->{'FACTORY'}->new('NAMESPACE', $config->{'NAMESPACE'}) || (return $class->error($self->{'FACTORY'}->error))
$self->{'FACTORY'}->new('NAMESPACE', $config->{'NAMESPACE'})
Truth table50%1/2
A
A= $self->{'FACTORY'}->new('NAMESPACE', $config->{'NAMESPACE'})
Aresult
11
00
189 }
190  
191 # load grammar rules, states and lex table
192 @$self{ qw( LEXTABLE STATES RULES ) }
193 236 = @$grammar{ qw( LEXTABLE STATES RULES ) };
194  
195 236 ▶ $self->new_style($config)
Branch: 1 branch
BranchTrueFalseTotal
$self->new_style($config) or return $class->error($self->error) 236 0 236
196 || return $class->error($self->error());
197  
198 236 return $self;
199 }
200  
201 #-----------------------------------------------------------------------
202 # These methods are used to track nested IF and WHILE blocks. Each
203 # generated if/while block is given a label indicating the directive
204 # type and nesting depth, e.g. FOR0, WHILE1, FOR2, WHILE3, etc. The
205 # NEXT and LAST directives use the innermost label, e.g. last WHILE3;
206 #-----------------------------------------------------------------------
207  
208 232 ▶sub enter_block {
Subroutine enter_block: called CC=1 SCAR=0.0
Pod undocumented
209 232 my ($self, $name) = @_;
210 232 my $blocks = $self->{ IN_BLOCK };
211 232 push(@{ $self->{ IN_BLOCK } }, $name);
212 }
213  
214 232 ▶sub leave_block {
Subroutine leave_block: called CC=1 SCAR=0.0
Pod undocumented
215 232 my $self = shift;
216 232 my $label = $self->block_label;
217 232 pop(@{ $self->{ IN_BLOCK } });
218 232 return $label;
219 }
220  
221 17 ▶sub in_block {
Subroutine in_block: called CC=2 SCAR=6.9
Pod undocumented
222 17 my ($self, $name) = @_;
223 17 my $blocks = $self->{ IN_BLOCK };
224 17 ▶ return @$blocks && $blocks->[-1] eq $name;
Condition100%3/3
@$blocks && $blocks->[-1] eq $name
!ll&&!rl&&r
1610
MC/DC100%2/2
@$blocks && $blocks->[-1] eq $name
@$blocks $blocks->[-1] eq $name
Truth table100%3/3
A && B
A= @$blocksB= $blocks->[-1] eq $name
ABresult
0X0
100
111
225 }
226  
227 260 ▶sub block_label {
Subroutine block_label: called CC=2 SCAR=6.9
Pod undocumented
228 260 my ($self, $prefix, $suffix) = @_;
229 260 my $blocks = $self->{ IN_BLOCK };
230 260 ▶ my $name = @$blocks
Branch: 1 branch
BranchTrueFalseTotal
@$blocks ? : 257 3 260
231 ? $blocks->[-1] . scalar @$blocks
232 : undef;
233 260 return join('', grep { defined $_ } $prefix, $name, $suffix);
234 }
235  
236  
237  
238 #------------------------------------------------------------------------
239 # new_style(\%config)
240 #
241 # Install a new (stacked) parser style. This feature is currently
242 # experimental but should mimic the previous behaviour with regard to
243 # TAG_STYLE, START_TAG, END_TAG, etc.
244 #------------------------------------------------------------------------
245  
246 237 ▶sub new_style {
Subroutine new_style: called CC=10 SCAR=23.6
Pod undocumented
247 237 my ($self, $config) = @_;
248 237 ▶ my $styles = $self->{ STYLE } ||= [ ];
Condition100%2/2
$self->{"STYLE"} ||= []
l!l
1236
MC/DC100%1/1
$self->{"STYLE"} ||= []
$self->{"STYLE"}
Truth table100%2/2
A
A= $self->{"STYLE"}
Aresult
11
00
249 237 my ($tagstyle, $tags, $start, $end, $out, $key);
250  
251 # clone new style from previous or default style
252 237 ▶ my $style = { %{ $styles->[-1] || $DEFAULT_STYLE } };
Condition66%2/3
$styles->[-1] || $Template::Parser::DEFAULT_STYLE
l!l&&r!l&&!r
12360
MC/DC0%0/2
$styles->[-1] || $Template::Parser::DEFAULT_STYLE
$styles->[-1] $Template::Parser::DEFAULT_STYLE
Truth table66%2/3
A || B
A= $styles->[-1]B= $Template::Parser::DEFAULT_STYLE
ABresult
1X1
011
000
253  
254 # expand START_TAG and END_TAG from specified TAG_STYLE
255 237 ▶ if ($tagstyle = $config->{ TAG_STYLE }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagstyle = $config->{"TAG_STYLE"}) 5 232 237
256 return $self->error("Invalid tag style: $tagstyle")
257 5 ▶ unless defined ($tags = $TAG_STYLE->{ $tagstyle });
Branch: 1 branch
BranchTrueFalseTotal
unless defined($tags = $Template::Parser::TAG_STYLE->{$tagstyle}) 0 5 5
258 5 ($start, $end, $out) = @$tags;
259 5 ▶ $config->{ START_TAG } ||= $start;
Condition33%1/3
$config->{"START_TAG"} ||= $start
l!l&&r!l&&!r
050
MC/DC0%0/2
$config->{"START_TAG"} ||= $start
$config->{"START_TAG"} $start
Truth table33%1/3
A ||= B
A= $config->{"START_TAG"}B= $start
ABresult
1X1
011
000
260 5 ▶ $config->{ END_TAG } ||= $end;
Condition33%1/3
$config->{"END_TAG"} ||= $end
l!l&&r!l&&!r
050
MC/DC0%0/2
$config->{"END_TAG"} ||= $end
$config->{"END_TAG"} $end
Truth table33%1/3
A ||= B
A= $config->{"END_TAG"}B= $end
ABresult
1X1
011
000
261 5 ▶ $config->{ OUTLINE_TAG } ||= $out;
Condition66%2/3
$config->{"OUTLINE_TAG"} ||= $out
l!l&&r!l&&!r
023
MC/DC50%1/2
$config->{"OUTLINE_TAG"} ||= $out
$config->{"OUTLINE_TAG"} $out
Truth table66%2/3
A ||= B
A= $config->{"OUTLINE_TAG"}B= $out
ABresult
1X1
011
000
262 }
263  
264 237 foreach $key (keys %$DEFAULT_STYLE) {
265 2133 ▶ $style->{ $key } = $config->{ $key } if defined $config->{ $key };
Branch: 1 branch
BranchTrueFalseTotal
if defined $config->{$key} 110 2023 2133
266 }
267  
268 237 $start = $style->{ START_TAG };
269 237 $end = $style->{ END_TAG };
270 237 $out = $style->{ OUTLINE_TAG };
271 237 $style->{ TEXT_SPLIT } = $self->text_splitter($start, $end, $out);
272  
273 237 push(@$styles, $style);
274 237 return $style;
275 }
276  
277 256 ▶sub text_splitter {
Subroutine text_splitter: called CC=2 SCAR=6.9
Pod undocumented
278 256 my ($self, $start, $end, $out) = @_;
279  
280 256 ▶ if (defined $out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $out) { } 7 249 256
281 7 return qr/
282 \A(.*?) # $1 - start of line up to directive
283 (?:
284 (?:
285 ^$out # outline tag at start of line
286 (.*? # $2 - content of that line
287 (?:\n|$) # end of that line or file
288 )
289 )
290 |
291 (?:
292 $start # start of tag
293 (.*?) # $3 - tag contents
294 $end # end of tag
295 )
296 )
297 /msx;
298 }
299 else {
300 249 return qr/
301 ^(.*?) # $1 - start of line up to directive
302 (?:
303 $start # start of tag
304 (.*?) # $2 - tag contents
305 $end # end of tag
306 )
307 /sx;
308 }
309 }
310  
311 #------------------------------------------------------------------------
312 # old_style()
313 #
314 # Pop the current parser style and revert to the previous one. See
315 # new_style(). ** experimental **
316 #------------------------------------------------------------------------
317  
318 1 ▶sub old_style {
Subroutine old_style: called CC=2 SCAR=7.0
Pod undocumented
319 1 my $self = shift;
320 1 my $styles = $self->{ STYLE };
321 1 ▶ return $self->error('only 1 parser style remaining')
Branch: 1 branch
BranchTrueFalseTotal
unless @$styles > 1 0 1 1
322 unless (@$styles > 1);
323 1 pop @$styles;
324 1 return $styles->[-1];
325 }
326  
327  
328 #------------------------------------------------------------------------
329 # parse($text, $data)
330 #
331 # Parses the text string, $text and returns a hash array representing
332 # the compiled template block(s) as Perl code, in the format expected
333 # by Template::Document.
334 #------------------------------------------------------------------------
335  
336 1491 ▶sub parse {
Subroutine parse: called CC=5 SCAR=16.3
Pod documented
337 1491 my ($self, $text, $info) = @_;
338 1491 my ($tokens, $block);
339  
340 1491 ▶ $info->{ DEBUG } //= $self->{ DEBUG_DIRS };
Condition33%1/3
$info->{"DEBUG"} //= $self->{"DEBUG_DIRS"}
l!l&&r!l&&!r
014910
MC/DC0%0/2
$info->{"DEBUG"} //= $self->{"DEBUG_DIRS"}
$info->{"DEBUG"} $self->{"DEBUG_DIRS"}
Truth table33%1/3
A //= B
A= $info->{"DEBUG"}B= $self->{"DEBUG_DIRS"}
ABresult
1X1
011
000
341  
342 # print "info: { ", join(', ', map { "$_ => $info->{ $_ }" } keys %$info), " }\n";
343  
344 # store for blocks defined in the template (see define_block())
345 1491 my $defblock = $self->{ DEFBLOCK } = { };
346 1491 my $metadata = $self->{ METADATA } = [ ];
347 1491 my $variables = $self->{ VARIABLES } = { };
348 1491 $self->{ DEFBLOCKS } = [ ];
349  
350 1491 $self->{ _ERROR } = '';
351  
352 # split file into TEXT/DIRECTIVE chunks
353 1491 ▶ $tokens = $self->split_text($text)
Condition50%1/2
$self->split_text($text) || (return undef)
l!l
14910
MC/DC0%0/1
$self->split_text($text) || (return undef)
$self->split_text($text)
Truth table50%1/2
A
A= $self->split_text($text)
Aresult
11
00
354 || return undef; ## RETURN ##
355  
356 1491 push(@{ $self->{ FILEINFO } }, $info);
357  
358 # parse chunks
359 1491 $block = $self->_parse($tokens, $info);
360  
361 1491 pop(@{ $self->{ FILEINFO } });
362  
363 1491 ▶ return undef unless $block; ## RETURN ##
Branch: 1 branch
BranchTrueFalseTotal
unless $block 3 1488 1491
364  
365 $self->debug("compiled main template document block:\n$block")
366 1488 ▶ if $self->{ DEBUG } & Template::Constants::DEBUG_PARSER;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} & 64 0 1488 1488
367  
368 return {
369 1488 BLOCK => $block,
370 DEFBLOCKS => $defblock,
371 VARIABLES => $variables,
372 METADATA => { @$metadata },
373 };
374 }
375  
376  
377  
378 #------------------------------------------------------------------------
379 # split_text($text)
380 #
381 # Split input template text into directives and raw text chunks.
382 #------------------------------------------------------------------------
383  
384 1492 ▶sub split_text {
Subroutine split_text: called CC=39 SCAR=36.9
Pod undocumented
385 1492 my ($self, $text) = @_;
386 1492 my ($pre, $dir, $prelines, $dirlines, $postlines, $chomp, $tags, @tags);
387 1492 my $style = $self->{ STYLE }->[-1];
388 my ($start, $end, $out, $prechomp, $postchomp, $interp ) =
389 1492 @$style{ qw( START_TAG END_TAG OUTLINE_TAG PRE_CHOMP POST_CHOMP INTERPOLATE ) };
390 1492 ▶ my $tags_dir = $self->{ANYCASE} ? qr<TAGS>i : qr<TAGS>;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'ANYCASE'} ? : 117 1375 1492
391 1492 my $split = $style->{ TEXT_SPLIT };
392 1492 my $has_out = defined $out;
393  
394 1492 my @tokens = ();
395 1492 my $line = 1;
396  
397 return \@tokens ## RETURN ##
398 1492 ▶ unless defined $text && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 8 1484 1492
Condition66%2/3
defined $text and length $text
!ll&&!rl&&r
081484
MC/DC50%1/2
defined $text and length $text
defined $text length $text
Truth table66%2/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
399  
400 # extract all directives from the text
401 1484 ▶ while ($text =~ s/$split//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/$split//) 4309 1484 5793
402 4309 ▶ $pre = $1 // '';
Condition50%1/2
$1 // ""
l!l
43090
MC/DC0%0/1
$1 // ""
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
403 4309 ▶ $dir = $2 // $3 // '';
Condition60%3/5
($2 // $3) // ""
l!l
43090
$2 // $3
l!l&&r!l&&!r
4292017
MC/DC50%1/2
($2 // $3) // ""
$2 $3
Truth table66%2/3
A // B
A= $2B= $3
ABresult
1X1
011
000
404  
405 4309 $prelines = ($pre =~ tr/\n//); # newlines in preceding text
406 4309 $dirlines = ($dir =~ tr/\n//); # newlines in directive tag
407 4309 $postlines = 0; # newlines chomped after tag
408  
409 4309 for ($dir) {
410 4309 ▶ if (/^\#/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\#/) { } 15 4294 4309
411 # comment out entire directive except for any end chomp flag
412 #
413 # Handle comments containing nested tag pairs, e.g.:
414 # [%# [% foo %] %]
415 # The tokenizer splits at the first END_TAG, so the
416 # directive text may contain unbalanced START_TAGs.
417 # For each one, consume the matching END_TAG from
418 # the remaining template text.
419 15 my $nested = () = /$start/g;
420 15 ▶ while ($nested > 0 && $text =~ s/\A(.*?)$end//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nested > 0 and $text =~ s/\A(.*?)$end//s) 6 15 21
Condition66%2/3
$nested > 0 and $text =~ s/\A(.*?)$end//s
!ll&&!rl&&r
1506
MC/DC50%1/2
$nested > 0 and $text =~ s/\A(.*?)$end//s
$nested > 0 $text =~ s/\A(.*?)$end//s
Truth table66%2/3
A and B
A= $nested > 0B= $text =~ s/\A(.*?)$end//s
ABresult
0X0
100
111
421 6 my $extra = $1;
422 6 $dirlines += ($extra =~ tr/\n//);
423 6 $nested--;
424 6 $nested += () = $extra =~ /$start/g;
425 }
426 15 ▶ $dir = ($dir =~ /($CHOMP_FLAGS)$/o) ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$dir =~ /($Template::Parser::CHOMP_FLAGS)$/o ? : 4 11 15
427 }
428 else {
429  
430 4294 ▶ if(s/^($CHOMP_FLAGS)?(\s*)//so && $2) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^($Template::Parser::CHOMP_FLAGS)?(\s*)//os and $2) 4280 14 4294
Condition66%2/3
s/^($Template::Parser::CHOMP_FLAGS)?(\s*)//os and $2
!ll&&!rl&&r
0144280
MC/DC50%1/2
s/^($Template::Parser::CHOMP_FLAGS)?(\s*)//os and $2
s/^($Template::Parser::CHOMP_FLAGS)?(\s*)//os $2
Truth table66%2/3
A and B
A= s/^($Template::Parser::CHOMP_FLAGS)?(\s*)//osB= $2
ABresult
0X0
100
111
431 4280 my $chomped = $2;
432 4280 my $linecount = ($chomped =~ tr/\n//); # newlines in chomped whitespace
433 4280 ▶ $linecount ||= 0;
Condition100%2/2
$linecount ||= 0
l!l
164264
MC/DC100%1/1
$linecount ||= 0
$linecount
Truth table100%2/2
A
A= $linecount
Aresult
11
00
434 4280 $prelines += $linecount;
435 4280 $dirlines -= $linecount;
436 }
437 # PRE_CHOMP: process whitespace before tag
438 4294 ▶ $chomp = $1 ? $1 : $prechomp;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 122 4172 4294
439 4294 $chomp =~ tr/-=~+/1230/;
440 4294 ▶ if ($chomp && $pre) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chomp and $pre) 120 4174 4294
Condition100%3/3
$chomp and $pre
!ll&&!rl&&r
415222120
MC/DC100%2/2
$chomp and $pre
$chomp $pre
Truth table100%3/3
A and B
A= $chompB= $pre
ABresult
0X0
100
111
441 # chomp off whitespace and newline preceding directive
442 120 ▶ if ($chomp == CHOMP_ALL) {
Branch: 3 branches
BranchTrueFalseTotal
if ($chomp == 1) { } 111 9 120
elsif ($chomp == 2) { } 6 3 9
elsif ($chomp == 3) { } 3 0 3
443 111 $pre =~ s{ (\r?\n|^) [^\S\n]* \z }{}mx;
444 }
445 elsif ($chomp == CHOMP_COLLAPSE) {
446 6 $pre =~ s{ (\s+) \z }{ }x;
447 }
448 elsif ($chomp == CHOMP_GREEDY) {
449 3 $pre =~ s{ (\s+) \z }{}x;
450 }
451 }
452 }
453  
454 # POST_CHOMP: process whitespace after tag
455 4309 s/\s*($CHOMP_FLAGS)?\s*$//so;
456 4309 ▶ $chomp = $1 ? $1 : $postchomp;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 1254 3055 4309
457 4309 $chomp =~ tr/-=~+/1230/;
458 4309 ▶ if ($chomp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chomp) 2104 2205 4309
459 2104 ▶ if ($chomp == CHOMP_ALL) {
Branch: 3 branches
BranchTrueFalseTotal
if ($chomp == 1) { } 2095 9 2104
elsif ($chomp == 2) { } 6 3 9
elsif ($chomp == 3) { } 3 0 3
460 2095 ▶ $text =~ s{ ^ ([^\S\n]* \n) }{}x
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s/ ^ ([^\S\n]* \n) //x and ++$postlines 1943 152 2095
461 && $postlines++;
462 }
463 elsif ($chomp == CHOMP_COLLAPSE) {
464 6 ▶ $text =~ s{ ^ (\s+) }{ }x
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s/ ^ (\s+) / /x and $postlines += $1 =~ tr/\n// 6 0 6
465 && ($postlines += $1=~y/\n//);
466 }
467 # any trailing whitespace
468 elsif ($chomp == CHOMP_GREEDY) {
469 3 ▶ $text =~ s{ ^ (\s+) }{}x
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s/ ^ (\s+) //x and $postlines += $1 =~ tr/\n// 3 0 3
470 && ($postlines += $1=~y/\n//);
471 }
472 }
473 }
474  
475 # any text preceding the directive can now be added
476 4309 ▶ if (length $pre) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $pre) 2267 2042 4309
477 2267 ▶ push(@tokens, $interp
Branch: 1 branch
BranchTrueFalseTotal
$interp ? : 535 1732 2267
478 ? [ $pre, $line, 'ITEXT' ]
479 : ('TEXT', $pre) );
480 }
481 4309 $line += $prelines;
482  
483 # and now the directive, along with line number information
484 4309 ▶ if (length $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dir) 4290 19 4309
485 # the TAGS directive is a compile-time switch
486 4290 ▶ if ($dir =~ /^$tags_dir\s+(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir =~ /^$tags_dir\s+(.*)/) { } 19 4271 4290
487 19 my @tags = split(/\s+/, $1);
488 19 ▶ if (scalar @tags > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @tags > 1) { } 8 11 19
elsif ($tags = $$Template::Parser::TAG_STYLE{$tags[0]}) { } 11 0 11
489 8 ($start, $end, $out) = map { quotemeta($_) } @tags;
490 8 $split = $self->text_splitter($start, $end, $out);
491 }
492 elsif ($tags = $TAG_STYLE->{ $tags[0] }) {
493 11 ($start, $end, $out) = @$tags;
494 11 $split = $self->text_splitter($start, $end, $out);
495 }
496 else {
497 0 warn "invalid TAGS style: $tags[0]\n";
498 }
499 }
500 else {
501 # DIRECTIVE is pushed as:
502 # [ $dirtext, $line_no(s), \@tokens ]
503 4271 ▶ push(@tokens,
Branch: 1 branch
BranchTrueFalseTotal
$dirlines ? : 252 4019 4271
504 [ $dir,
505 ($dirlines
506 ? sprintf("%d-%d", $line, $line + $dirlines)
507 : $line),
508 $self->tokenise_directive($dir) ]);
509 }
510 }
511  
512 # update line counter to include directive lines and any extra
513 # newline chomped off the start of the following text
514 4309 $line += $dirlines + $postlines;
515 }
516  
517 # anything remaining in the string is plain text
518 1484 ▶ push(@tokens, $interp
Branch: 2 branches
BranchTrueFalseTotal
if length $text 1040 444 1484
$interp ? : 218 822 1040
519 ? [ $text, $line, 'ITEXT' ]
520 : ( 'TEXT', $text) )
521 if length $text;
522  
523 1484 return \@tokens; ## RETURN ##
524 }
525  
526  
527  
528 #------------------------------------------------------------------------
529 # interpolate_text($text, $line)
530 #
531 # Examines $text looking for any variable references embedded like
532 # $this or like ${ this }.
533 #------------------------------------------------------------------------
534  
535 911 ▶sub interpolate_text {
Subroutine interpolate_text: called CC=7 SCAR=19.6
Pod undocumented
536 911 my ($self, $text, $line) = @_;
537 911 my @tokens = ();
538 911 my ($pre, $var, $dir);
539  
540  
541 911 ▶ while ($text =~
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ / ( (?: \\. | [^\$] ){1,3000} ) # escaped or non-'$' character [$1] | ( \$ (?: # embedded variable [$2] (?: \{ ([^\}]*) \} ) # ${ ... } [$3] | ([\w\.]+) # $word [$4] ) ) /gx) 1415 911 2326
542 /
543 ( (?: \\. | [^\$] ){1,3000} ) # escaped or non-'$' character [$1]
544 |
545 ( \$ (?: # embedded variable [$2]
546 (?: \{ ([^\}]*) \} ) # ${ ... } [$3]
547 |
548 ([\w\.]+) # $word [$4]
549 )
550 )
551 /gx) {
552  
553 1415 ▶ ($pre, $var, $dir) = ($1, $3 || $4, $2);
Condition100%3/3
$3 || $4
l!l&&r!l&&!r
412701104
MC/DC100%2/2
$3 || $4
$3 $4
Truth table100%3/3
A || B
A= $3B= $4
ABresult
1X1
011
000
554  
555 # preceding text
556 1415 ▶ if (defined($pre) && length($pre)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pre and length $pre) 1104 311 1415
Condition66%2/3
defined $pre and length $pre
!ll&&!rl&&r
31101104
MC/DC50%1/2
defined $pre and length $pre
defined $pre length $pre
Truth table66%2/3
A and B
A= defined $preB= length $pre
ABresult
0X0
100
111
557 1104 $line += $pre =~ tr/\n//;
558 1104 $pre =~ s/\\\$/\$/g;
559 1104 push(@tokens, 'TEXT', $pre);
560 }
561 # $variable reference
562 1415 ▶ if ($var) {
Branch: 2 branches
BranchTrueFalseTotal
if ($var) { } 311 1104 1415
elsif ($dir) { } 0 1104 1104
563 311 $line += $dir =~ tr/\n/ /;
564 311 push(@tokens, [ $dir, $line, $self->tokenise_directive($var) ]);
565 }
566 # other '$' reference - treated as text
567 elsif ($dir) {
568 0 $line += $dir =~ tr/\n//;
569 0 push(@tokens, 'TEXT', $dir);
570 }
571 }
572  
573 911 return \@tokens;
574 }
575  
576  
577  
578 #------------------------------------------------------------------------
579 # tokenise_directive($text)
580 #
581 # Called by the private _parse() method when it encounters a DIRECTIVE
582 # token in the list provided by the split_text() or interpolate_text()
583 # methods. The directive text is passed by parameter.
584 #
585 # The method splits the directive into individual tokens as recognised
586 # by the parser grammar (see Template::Grammar for details). It
587 # constructs a list of tokens each represented by 2 elements, as per
588 # split_text() et al. The first element contains the token type, the
589 # second the token itself.
590 #
591 # The method tokenises the string using a complex (but fast) regex.
592 # For a deeper understanding of the regex magic at work here, see
593 # Jeffrey Friedl's excellent book "Mastering Regular Expressions",
594 # from O'Reilly, ISBN 1-56592-257-3
595 #
596 # Returns a reference to the list of chunks (each one being 2 elements)
597 # identified in the directive text. On error, the internal _ERROR string
598 # is set and undef is returned.
599 #------------------------------------------------------------------------
600  
601 4582 ▶sub tokenise_directive {
Subroutine tokenise_directive: called CC=18 SCAR=28.9
Pod undocumented
602 4582 my ($self, $text, $line) = @_;
603 4582 my ($token, $uctoken, $type, $lookup);
604 4582 my $lextable = $self->{ LEXTABLE };
605 4582 my $style = $self->{ STYLE }->[-1];
606 4582 my ($anycase, $start, $end) = @$style{ qw( ANYCASE START_TAG END_TAG ) };
607 4582 my @tokens = ( );
608  
609 4582 ▶ while ($text =~
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ m( # strip out any comments (\#[^\n]*) | # a quoted phrase matches in $3 (["']) # $2 - opening quote, ' or " ( # $3 - quoted text buffer (?: # repeat group (no backreference) \\\\ # an escaped backslash \\ | # ...or... \\\2 # an escaped quote \" or \' (match $1) | # ...or... . # any other character | \n )*? # non-greedy repeat ) # end of $3 \2 # match opening quote | # an unquoted number matches in $4 (-?\d+(?:\.\d+)?) # numbers | # filename matches in $5 ( /?\w+(?:(?:/|::?)\w*)+ | /\w+) | # an identifier matches in $6 (\w+) # variable identifier | # an unquoted word or symbol matches in $7 ( [(){}\[\]:;,/\\] # misc parenthesis and symbols # | \-> # arrow operator (for future?) | [+\-*] # math operations | \$\{? # dollar with option left brace | => # like '=' | [=!<>]?= | [!<>] # eqality tests | &&? | \|\|? # boolean ops | \.\.? # n..n sequence | \S+ # something unquoted ) # end of $7 )gmox) 19313 4582 23895
610 /
611 # strip out any comments
612 (\#[^\n]*)
613 |
614 # a quoted phrase matches in $3
615 (["']) # $2 - opening quote, ' or "
616 ( # $3 - quoted text buffer
617 (?: # repeat group (no backreference)
618 \\\\ # an escaped backslash \\
619 | # ...or...
620 \\\2 # an escaped quote \" or \' (match $1)
621 | # ...or...
622 . # any other character
623 | \n
624 )*? # non-greedy repeat
625 ) # end of $3
626 \2 # match opening quote
627 |
628 # an unquoted number matches in $4
629 (-?\d+(?:\.\d+)?) # numbers
630 |
631 # filename matches in $5
632 ( \/?\w+(?:(?:\/|::?)\w*)+ | \/\w+)
633 |
634 # an identifier matches in $6
635 (\w+) # variable identifier
636 |
637 # an unquoted word or symbol matches in $7
638 ( [(){}\[\]:;,\/\\] # misc parenthesis and symbols
639 # | \-> # arrow operator (for future?)
640 | [+\-*] # math operations
641 | \$\{? # dollar with option left brace
642 | => # like '='
643 | [=!<>]?= | [!<>] # eqality tests
644 | &&? | \|\|? # boolean ops
645 | \.\.? # n..n sequence
646 | \S+ # something unquoted
647 ) # end of $7
648 /gmxo) {
649  
650 # ignore comments to EOL
651 19313 ▶ next if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 25 19288 19313
652  
653 # quoted string
654 19288 ▶ if (defined ($token = $3)) {
Branch: 5 branches
BranchTrueFalseTotal
if (defined($token = $3)) { } 1490 17798 19288
elsif (defined($token = $4)) { } 826 16972 17798
elsif (defined($token = $5)) { } 26 16946 16972
elsif (defined($token = $6)) { } 10021 6925 16946
elsif (defined($token = $7)) { } 6925 0 6925
655 # double-quoted string may include $variable references
656 1490 ▶ if ($2 eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq '"') { } 353 1137 1490
657 353 ▶ if ($token =~ /[\$\\]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /[\$\\]/) { } 174 179 353
658 174 $type = 'QUOTED';
659 # unescape " and \ but leave \$ escaped so that
660 # interpolate_text() doesn't incorrectly treat it
661 # as a variable reference
662 # $token =~ s/\\([\\"])/$1/g;
663 174 for ($token) {
664 174 s/\\([^\$nrt])/$1/g;
665 174 s/\\([nrt])/$QUOTED_ESCAPES->{ $1 }/ge;
666 }
667 push(@tokens, ('"') x 2,
668 174 @{ $self->interpolate_text($token) },
669 ('"') x 2);
670 174 next;
671 }
672 else {
673 179 $type = 'LITERAL';
674 179 $token =~ s['][\\']g;
675 179 $token = "'$token'";
676 }
677 }
678 else {
679 1137 $type = 'LITERAL';
680 1137 $token = "'$token'";
681 }
682 }
683 # number
684 elsif (defined ($token = $4)) {
685 826 $type = 'NUMBER';
686 }
687 elsif (defined($token = $5)) {
688 26 $type = 'FILENAME';
689 }
690 elsif (defined($token = $6)) {
691 # Fold potential keywords to UPPER CASE if the ANYCASE option is
692 # set, unless (we've got some preceding tokens and) the previous
693 # token is a DOT op. This prevents the 'last' in 'data.last'
694 # from being interpreted as the LAST keyword.
695 10021 ▶ if ($anycase) {
Branch: 1 branch
BranchTrueFalseTotal
if ($anycase) { } 827 9194 10021
696 # if the token follows a dot or precedes an assignment then
697 # it's not for folding, e.g. the 'wrapper' in this:
698 # [% page = { wrapper='html' }; page.wrapper %]
699 827 ▶ if ((@tokens && $ANYCASE_AFTER->{ $tokens[-2] })
Branch: 1 branch
BranchTrueFalseTotal
if (@tokens and $$Template::Parser::ANYCASE_AFTER{$tokens[-2]} or $text =~ /$Template::Parser::ANYCASE_BEFORE/cg) { } 495 332 827
Condition100%6/6
@tokens and $$Template::Parser::ANYCASE_AFTER{$tokens[-2]} or $text =~ /$Template::Parser::ANYCASE_BEFORE/cg
l!l&&r!l&&!r
281214332
@tokens and $$Template::Parser::ANYCASE_AFTER{$tokens[-2]}
!ll&&!rl&&r
383163281
MC/DC100%3/3
@tokens and $$Template::Parser::ANYCASE_AFTER{$tokens[-2]} or $text =~ /$Template::Parser::ANYCASE_BEFORE/cg
@tokens $$Template::Parser::ANYCASE_AFTER{$tokens[-2]} $text =~ /$Template::Parser::ANYCASE_BEFORE/cg
Truth table100%5/5
A and B or C
A= @tokensB= $$Template::Parser::ANYCASE_AFTER{$tokens[-2]}C= $text =~ /$Template::Parser::ANYCASE_BEFORE/cg
ABCresult
11X1
0X11
1011
0X00
1000
700 || ($text =~ /$ANYCASE_BEFORE/gc)) {
701 # keep the token unmodified
702 495 $uctoken = $token;
703 }
704 else {
705 332 $uctoken = uc $token;
706 }
707 }
708 else {
709 9194 $uctoken = $token;
710 }
711 10021 ▶ if (defined ($type = $lextable->{ $uctoken })) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($type = $lextable->{$uctoken})) { } 2683 7338 10021
712 2683 $token = $uctoken;
713 }
714 else {
715 7338 $type = 'IDENT';
716 }
717 }
718 elsif (defined ($token = $7)) {
719 # reserved words may be in lower case unless case sensitive
720 6925 ▶ $uctoken = $anycase ? uc $token : $token;
Branch: 1 branch
BranchTrueFalseTotal
$anycase ? : 553 6372 6925
721 6925 ▶ unless (defined ($type = $lextable->{ $uctoken })) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($type = $lextable->{$uctoken})) 0 6925 6925
722 0 $type = 'UNQUOTED';
723 }
724 }
725  
726 19114 push(@tokens, $type, $token);
727  
728 # print(STDERR " +[ $type, $token ]\n")
729 # if $DEBUG;
730 }
731  
732 # print STDERR "tokenise directive() returning:\n [ @tokens ]\n"
733 # if $DEBUG;
734  
735 4582 return \@tokens; ## RETURN ##
736 }
737  
738  
739 #------------------------------------------------------------------------
740 # define_block($name, $block)
741 #
742 # Called by the parser 'defblock' rule when a BLOCK definition is
743 # encountered in the template. The name of the block is passed in the
744 # first parameter and a reference to the compiled block is passed in
745 # the second. This method stores the block in the $self->{ DEFBLOCK }
746 # hash which has been initialised by parse() and will later be used
747 # by the same method to call the store() method on the calling cache
748 # to define the block "externally".
749 #------------------------------------------------------------------------
750  
751 162 ▶sub define_block {
Subroutine define_block: called CC=4 SCAR=14.0
Pod undocumented
752 162 my ($self, $name, $block) = @_;
753 my $defblock = $self->{ DEFBLOCK }
754 162 ▶ || return undef;
Condition50%1/2
$self->{'DEFBLOCK'} || (return undef)
l!l
1620
MC/DC0%0/1
$self->{'DEFBLOCK'} || (return undef)
$self->{'DEFBLOCK'}
Truth table50%1/2
A
A= $self->{'DEFBLOCK'}
Aresult
11
00
755  
756 $self->debug("compiled block '$name':\n$block")
757 162 ▶ if $self->{ DEBUG } & Template::Constants::DEBUG_PARSER;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} & 64 0 162 162
758  
759 162 ▶ warn "Block redefined: $name\n" if exists $defblock->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
if exists $defblock->{$name} 1 161 162
760  
761 162 $defblock->{ $name } = $block;
762  
763 162 return undef;
764 }
765  
766 31 ▶sub push_defblock {
Subroutine push_defblock: called CC=2 SCAR=6.9
Pod undocumented
767 31 my $self = shift;
768 31 ▶ my $stack = $self->{ DEFBLOCK_STACK } ||= [];
Condition100%2/2
$self->{"DEFBLOCK_STACK"} ||= []
l!l
283
MC/DC100%1/1
$self->{"DEFBLOCK_STACK"} ||= []
$self->{"DEFBLOCK_STACK"}
Truth table100%2/2
A
A= $self->{"DEFBLOCK_STACK"}
Aresult
11
00
769 31 push(@$stack, $self->{ DEFBLOCK } );
770 31 $self->{ DEFBLOCK } = { };
771 }
772  
773 31 ▶sub pop_defblock {
Subroutine pop_defblock: called CC=3 SCAR=11.2
Pod undocumented
774 31 my $self = shift;
775 31 my $defs = $self->{ DEFBLOCK };
776 31 ▶ my $stack = $self->{ DEFBLOCK_STACK } || return $defs;
Condition50%1/2
$self->{'DEFBLOCK_STACK'} || (return $defs)
l!l
310
MC/DC0%0/1
$self->{'DEFBLOCK_STACK'} || (return $defs)
$self->{'DEFBLOCK_STACK'}
Truth table50%1/2
A
A= $self->{'DEFBLOCK_STACK'}
Aresult
11
00
777 31 ▶ return $defs unless @$stack;
Branch: 1 branch
BranchTrueFalseTotal
unless @$stack 0 31 31
778 31 $self->{ DEFBLOCK } = pop @$stack;
779 31 return $defs;
780 }
781  
782  
783 #------------------------------------------------------------------------
784 # add_metadata(\@setlist)
785 #------------------------------------------------------------------------
786  
787 26 ▶sub add_metadata {
Subroutine add_metadata: called CC=2 SCAR=7.0
Pod undocumented
788 26 my ($self, $setlist) = @_;
789 my $metadata = $self->{ METADATA }
790 26 ▶ || return undef;
Condition50%1/2
$self->{'METADATA'} || (return undef)
l!l
260
MC/DC0%0/1
$self->{'METADATA'} || (return undef)
$self->{'METADATA'}
Truth table50%1/2
A
A= $self->{'METADATA'}
Aresult
11
00
791  
792 26 push(@$metadata, @$setlist);
793  
794 26 return undef;
795 }
796  
797  
798 #------------------------------------------------------------------------
799 # location()
800 #
801 # Return Perl comment indicating current parser file and line
802 #------------------------------------------------------------------------
803  
804 3915 ▶sub location {
Subroutine location: called CC=5 SCAR=16.1
Pod undocumented
805 3915 my $self = shift;
806 3915 ▶ return "\n" unless $self->{ FILE_INFO };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"FILE_INFO"} 13 3902 3915
807 3902 my $line = ${ $self->{ LINE } };
808 3902 my $info = $self->{ FILEINFO }->[-1];
809 my $file = $info->{ path } || $info->{ name }
810 3902 ▶ || '(unknown template)';
Condition100%5/5
$info->{'path'} || $info->{'name'} || '(unknown template)'
l!l
38948
$info->{"path"} or $info->{"name"}
l!l&&r!l&&!r
389048
MC/DC100%2/2
$info->{'path'} || $info->{'name'} || '(unknown template)'
$info->{"path"} $info->{"name"}
Truth table100%3/3
A or B
A= $info->{"path"}B= $info->{"name"}
ABresult
1X1
011
000
811 3902 $line =~ s/\-.*$//; # might be 'n-n'
812 3902 ▶ $line ||= 1;
Condition100%2/2
$line ||= 1
l!l
3730172
MC/DC100%1/1
$line ||= 1
$line
Truth table100%2/2
A
A= $line
Aresult
11
00
813 3902 return "#line $line \"$file\"\n";
814 }
815  
816  
817 #========================================================================
818 # ----- PRIVATE METHODS -----
819 #========================================================================
820  
821 #------------------------------------------------------------------------
822 # _parse(\@tokens, \@info)
823 #
824 # Parses the list of input tokens passed by reference and returns a
825 # Template::Directive::Block object which contains the compiled
826 # representation of the template.
827 #
828 # This is the main parser DFA loop. See embedded comments for
829 # further details.
830 #
831 # On error, undef is returned and the internal _ERROR field is set to
832 # indicate the error. This can be retrieved by calling the error()
833 # method.
834 #------------------------------------------------------------------------
835  
836 1491 ▶sub _parse {
Subroutine _parse: called CC=27 SCAR=33.0
837 1491 my ($self, $tokens, $info) = @_;
838 1491 my ($token, $value, $text, $line, $inperl);
839 1491 my ($state, $stateno, $status, $action, $lookup, $coderet, @codevars);
840 1491 my ($lhs, $len, $code); # rule contents
841 1491 my $stack = [ [ 0, undef ] ]; # DFA stack
842  
843 # DEBUG
844 # local $" = ', ';
845  
846 # retrieve internal rule and state tables
847 1491 my ($states, $rules) = @$self{ qw( STATES RULES ) };
848  
849 # If we're tracing variable usage then we need to give the factory a
850 # reference to our $self->{ VARIABLES } for it to fill in. This is a
851 # bit of a hack to back-patch this functionality into TT2.
852 $self->{ FACTORY }->trace_vars($self->{ VARIABLES })
853 1491 ▶ if $self->{ TRACE_VARS };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"TRACE_VARS"} 1 1490 1491
854  
855 # call the grammar set_factory method to install emitter factory
856 1491 $self->{ GRAMMAR }->install_factory($self->{ FACTORY });
857  
858 1491 $line = $inperl = 0;
859 1491 $self->{ LINE } = \$line;
860 1491 $self->{ FILE } = $info->{ name };
861 1491 $self->{ INPERL } = \$inperl;
862  
863 1491 $status = CONTINUE;
864 1491 my $in_string = 0;
865  
866 1491 while(1) {
867 # get state number and state
868 103986 $stateno = $stack->[-1]->[0];
869 103986 $state = $states->[$stateno];
870  
871 # see if any lookaheads exist for the current state
872 103986 ▶ if (exists $state->{'ACTIONS'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $state->{'ACTIONS'}) { } 49701 54285 103986
873  
874 # get next token and expand any directives (i.e. token is an
875 # array ref) onto the front of the token list
876 49701 ▶ while (! defined $token && @$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $token and @$tokens) 33199 49701 82900
Condition100%3/3
not defined $token and @$tokens
!ll&&!rl&&r
48212148933199
MC/DC100%2/2
not defined $token and @$tokens
not defined $token @$tokens
Truth table100%3/3
not(A) and B
A= not defined $tokenB= @$tokens
ABresult
0X0
100
111
877 33199 $token = shift(@$tokens);
878 33199 ▶ if (ref $token) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $token) { } 5331 27868 33199
879 5331 ($text, $line, $token) = @$token;
880 5331 ▶ if (ref $token) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $token) { } 4579 752 5331
elsif ($token eq 'ITEXT') { } 752 0 752
881 4579 ▶ if ($info->{ DEBUG } && ! $in_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{'DEBUG'} and not $in_string) { } 15 4564 4579
Condition100%3/3
$info->{"DEBUG"} and not $in_string
!ll&&!rl&&r
4562215
MC/DC100%2/2
$info->{"DEBUG"} and not $in_string
$info->{"DEBUG"} not $in_string
Truth table100%3/3
A and not(B)
A= $info->{"DEBUG"}B= not $in_string
ABresult
0X0
100
111
882 # - - - - - - - - - - - - - - - - - - - - - - - - -
883 # This is gnarly. Look away now if you're easily
884 # frightened. We're pushing parse tokens onto the
885 # pending list to simulate a DEBUG directive like so:
886 # [% DEBUG msg line='20' text='INCLUDE foo' %]
887 # - - - - - - - - - - - - - - - - - - - - - - - - -
888 15 my $dtext = $text;
889 15 $dtext =~ s[(['\\])][\\$1]g;
890 15 unshift(@$tokens,
891 DEBUG => 'DEBUG',
892 IDENT => 'msg',
893 IDENT => 'line',
894 ASSIGN => '=',
895 LITERAL => "'$line'",
896 IDENT => 'text',
897 ASSIGN => '=',
898 LITERAL => "'$dtext'",
899 IDENT => 'file',
900 ASSIGN => '=',
901 LITERAL => "'$info->{ name }'",
902 (';') x 2,
903 @$token,
904 (';') x 2);
905 }
906 else {
907 4564 unshift(@$tokens, @$token, (';') x 2);
908 }
909 4579 $token = undef; # force redo
910 }
911 elsif ($token eq 'ITEXT') {
912 752 ▶ if ($inperl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inperl) { } 15 737 752
913 # don't perform interpolation in PERL blocks
914 15 $token = 'TEXT';
915 15 $value = $text;
916 }
917 else {
918 unshift(@$tokens,
919 737 @{ $self->interpolate_text($text, $line) });
920 737 $token = undef; # force redo
921 }
922 }
923 }
924 else {
925 # toggle string flag to indicate if we're crossing
926 # a string boundary
927 27868 ▶ $in_string = ! $in_string if $token eq '"';
Branch: 1 branch
BranchTrueFalseTotal
if $token eq "\"" 348 27520 27868
928 27868 $value = shift(@$tokens);
929 }
930 };
931 # clear undefined token to avoid 'undefined variable blah blah'
932 # warnings and let the parser logic pick it up in a minute
933 49701 ▶ $token //= '';
Condition100%2/2
$token //= ""
l!l
482121489
MC/DC100%1/1
$token //= ""
$token
Truth table100%2/2
A
A= $token
Aresult
11
00
934  
935 # get the next state for the current lookahead token
936 $action = defined ($lookup = $state->{'ACTIONS'}->{ $token })
937 ? $lookup
938 49701 ▶ : defined ($lookup = $state->{'DEFAULT'})
Branch: 2 branches
BranchTrueFalseTotal
defined($lookup = $state->{'ACTIONS'}{$token}) ? : 32640 17061 49701
defined($lookup = $state->{'DEFAULT'}) ? : 17058 3 17061
939 ? $lookup
940 : undef;
941 }
942 else {
943 # no lookahead actions
944 54285 $action = $state->{'DEFAULT'};
945 }
946  
947 # ERROR: no ACTION
948 103986 ▶ last unless defined $action;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action 3 103983 103986
949  
950 # - - - - - - - - - - - - - - - - - - - - - - - - - - - -
951 # shift (+ive ACTION)
952 # - - - - - - - - - - - - - - - - - - - - - - - - - - - -
953 103983 ▶ if ($action > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action > 0) 29369 74614 103983
954 29369 push(@$stack, [ $action, $value ]);
955 29369 $token = $value = undef;
956 29369 redo;
957 };
958  
959 # - - - - - - - - - - - - - - - - - - - - - - - - - - - -
960 # reduce (-ive ACTION)
961 # - - - - - - - - - - - - - - - - - - - - - - - - - - - -
962 74614 ($lhs, $len, $code) = @{ $rules->[ -$action ] };
963  
964 # no action imples ACCEPTance
965 74614 ▶ $action
Branch: 1 branch
BranchTrueFalseTotal
$action or $status = 1 73126 1488 74614
966 or $status = ACCEPT;
967  
968 # use dummy sub if code ref doesn't exist
969 74614 ▶ if ( !$code ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $code) { } 24176 50438 74614
970 24176 ▶ $coderet = $len ? $stack->[ -$len ]->[1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$len ? : 23939 237 24176
971 } else {
972 # $code = sub { $_[1] }
973 # unless $code;
974  
975 @codevars = $len
976 50438 ▶ ? map { $_->[1] } @$stack[ -$len .. -1 ]
Branch: 1 branch
BranchTrueFalseTotal
$len ? : 48251 2187 50438
977 : ();
978  
979 50438 eval {
980 50438 $coderet = &$code( $self, @codevars );
981 };
982 50438 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 50438 50438
983 0 my $err = $@;
984 0 chomp $err;
985 0 return $self->_parse_error($err);
986 }
987 }
988  
989  
990 # reduce stack by $len
991 74614 splice(@$stack, -$len, $len);
992  
993 # ACCEPT
994 74614 ▶ return $coderet ## RETURN ##
Branch: 1 branch
BranchTrueFalseTotal
if $status == 1 1488 73126 74614
995 if $status == ACCEPT;
996  
997 # ABORT
998 return undef ## RETURN ##
999 73126 ▶ if $status == ABORT;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 3 0 73126 73126
1000  
1001 # ERROR
1002 last
1003 73126 ▶ if $status == ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 2 0 73126 73126
1004 }
1005 continue {
1006 73126 push(@$stack, [ $states->[ $stack->[-1][0] ]->{'GOTOS'}->{ $lhs },
1007 $coderet ]),
1008 }
1009  
1010 # ERROR ## RETURN ##
1011 3 ▶ return $self->_parse_error('unexpected end of input')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 2 3
1012 unless defined $value;
1013  
1014 # munge text of last directive to make it readable
1015 # $text =~ s/\n/\\n/g;
1016  
1017 2 ▶ return $self->_parse_error("unexpected end of directive", $text)
Branch: 1 branch
BranchTrueFalseTotal
if $value eq ";" 0 2 2
1018 if $value eq ';'; # end of directive SEPARATOR
1019  
1020 2 return $self->_parse_error("unexpected token ($value)", $text);
1021 }
1022  
1023  
1024  
1025 #------------------------------------------------------------------------
1026 # _parse_error($msg, $dirtext)
1027 #
1028 # Method used to handle errors encountered during the parse process
1029 # in the _parse() method.
1030 #------------------------------------------------------------------------
1031  
1032 3 ▶sub _parse_error {
Subroutine _parse_error: called CC=4 SCAR=14.0
1033 3 my ($self, $msg, $text) = @_;
1034 3 my $line = $self->{ LINE };
1035 3 ▶ $line = ref($line) ? $$line : $line;
Branch: 1 branch
BranchTrueFalseTotal
ref $line ? : 3 0 3
1036 3 ▶ $line = 'unknown' unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 3 3
1037  
1038 3 ▶ $msg .= "\n [% $text %]"
Branch: 1 branch
BranchTrueFalseTotal
if defined $text 2 1 3
1039 if defined $text;
1040  
1041 3 return $self->error("line $line: $msg");
1042 }
1043  
1044 106 1;
1045  
1046 __END__
« lib/Template/Plugin/Date.pm ↑ Summary lib/Template/Context.pm »