| 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 | use strict; | ||
| 36 | use warnings; | ||
| 37 | use base 'Template::Base'; | ||
| 38 | |||
| 39 | use Template::Constants qw( :status :chomp ); | ||
| 40 | use Template::Directive; | ||
| 41 | use Template::Grammar; | ||
| 42 | |||
| 43 | # parser state constants | ||
| 44 | use constant CONTINUE => 0; | ||
| 45 | use constant ACCEPT => 1; | ||
| 46 | use constant ERROR => 2; | ||
| 47 | use constant ABORT => 3; | ||
| 48 | |||
| 49 | our $VERSION = '3.100'; | ||
| 50 | our $DEBUG = 0 unless defined $DEBUG; | ||
| 51 | 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 | our $ANYCASE_BEFORE = qr/\G((?=\s*[=\.]))/; | ||
| 64 | our $ANYCASE_AFTER = { | ||
| 65 | 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 | 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 | $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 | 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 | 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 | our $CHOMP_FLAGS = qr/[-=~+]/; | ||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | #======================================================================== | ||
| 120 | # ----- PUBLIC METHODS ----- | ||
| 121 | #======================================================================== | ||
| 122 | |||
| 123 | #------------------------------------------------------------------------ | ||
| 124 | # new(\%config) | ||
| 125 | # | ||
| 126 | # Constructor method. | ||
| 127 | #------------------------------------------------------------------------ | ||
| 128 | |||
| 129 | sub new { | ||
| 130 | my $class = shift; | ||
| 131 | my $config = $_[0] && ref($_[0]) eq 'HASH' ? shift(@_) : { @_ }; | ||
| 132 | 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 | FACTORY => $config->{ FACTORY } || 'Template::Directive', | ||
| 151 | }, $class; | ||
| 152 | |||
| 153 | # update self with any relevant keys in config | ||
| 154 | foreach $key (keys %$self) { | ||
| 155 | $self->{ $key } = $config->{ $key } if defined $config->{ $key }; | ||
| 156 | } | ||
| 157 | $self->{ FILEINFO } = [ ]; | ||
| 158 | |||
| 159 | # DEBUG config item can be a bitmask | ||
| 160 | if (defined ($debug = $config->{ DEBUG })) { | ||
| 161 | $self->{ DEBUG } = $debug & ( Template::Constants::DEBUG_PARSER | ||
| 162 | | Template::Constants::DEBUG_FLAGS ); | ||
| 163 | $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 | $self->{ DEBUG } = Template::Constants::DEBUG_PARSER; | ||
| 168 | $self->{ DEBUG_DIRS } = 0; | ||
| 169 | } | ||
| 170 | # otherwise let $DEBUG be a bitmask | ||
| 171 | else { | ||
| 172 | $self->{ DEBUG } = $DEBUG & ( Template::Constants::DEBUG_PARSER | ||
| 173 | | Template::Constants::DEBUG_FLAGS ); | ||
| 174 | $self->{ DEBUG_DIRS } = $DEBUG & Template::Constants::DEBUG_DIRS; | ||
| 175 | } | ||
| 176 | |||
| 177 | $grammar = $self->{ GRAMMAR } ||= do { | ||
| 178 | require Template::Grammar; | ||
| 179 | Template::Grammar->new(); | ||
| 180 | }; | ||
| 181 | |||
| 182 | # instantiate a FACTORY object | ||
| 183 | unless (ref $self->{ FACTORY }) { | ||
| 184 | my $fclass = $self->{ FACTORY }; | ||
| 185 | $self->{ FACTORY } = $self->{ FACTORY }->new( | ||
| 186 | NAMESPACE => $config->{ NAMESPACE } | ||
| 187 | ) | ||
| 188 | || return $class->error($self->{ FACTORY }->error()); | ||
| 189 | } | ||
| 190 | |||
| 191 | # load grammar rules, states and lex table | ||
| 192 | @$self{ qw( LEXTABLE STATES RULES ) } | ||
| 193 | = @$grammar{ qw( LEXTABLE STATES RULES ) }; | ||
| 194 | |||
| 195 | $self->new_style($config) | ||
| 196 | || return $class->error($self->error()); | ||
| 197 | |||
| 198 | 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 | sub enter_block { | ||
| 209 | my ($self, $name) = @_; | ||
| 210 | my $blocks = $self->{ IN_BLOCK }; | ||
| 211 | push(@{ $self->{ IN_BLOCK } }, $name); | ||
| 212 | } | ||
| 213 | |||
| 214 | sub leave_block { | ||
| 215 | my $self = shift; | ||
| 216 | my $label = $self->block_label; | ||
| 217 | pop(@{ $self->{ IN_BLOCK } }); | ||
| 218 | return $label; | ||
| 219 | } | ||
| 220 | |||
| 221 | sub in_block { | ||
| 222 | my ($self, $name) = @_; | ||
| 223 | my $blocks = $self->{ IN_BLOCK }; | ||
| 224 | return @$blocks && $blocks->[-1] eq $name; | ||
| 225 | } | ||
| 226 | |||
| 227 | sub block_label { | ||
| 228 | my ($self, $prefix, $suffix) = @_; | ||
| 229 | my $blocks = $self->{ IN_BLOCK }; | ||
| 230 | my $name = @$blocks | ||
| 231 | ? $blocks->[-1] . scalar @$blocks | ||
| 232 | : undef; | ||
| 233 | 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 | sub new_style { | ||
| 247 | my ($self, $config) = @_; | ||
| 248 | my $styles = $self->{ STYLE } ||= [ ]; | ||
| 249 | my ($tagstyle, $tags, $start, $end, $out, $key); | ||
| 250 | |||
| 251 | # clone new style from previous or default style | ||
| 252 | my $style = { %{ $styles->[-1] || $DEFAULT_STYLE } }; | ||
| 253 | |||
| 254 | # expand START_TAG and END_TAG from specified TAG_STYLE | ||
| 255 | if ($tagstyle = $config->{ TAG_STYLE }) { | ||
| 256 | return $self->error("Invalid tag style: $tagstyle") | ||
| 257 | unless defined ($tags = $TAG_STYLE->{ $tagstyle }); | ||
| 258 | ($start, $end, $out) = @$tags; | ||
| 259 | $config->{ START_TAG } ||= $start; | ||
| 260 | $config->{ END_TAG } ||= $end; | ||
| 261 | $config->{ OUTLINE_TAG } ||= $out; | ||
| 262 | } | ||
| 263 | |||
| 264 | foreach $key (keys %$DEFAULT_STYLE) { | ||
| 265 | $style->{ $key } = $config->{ $key } if defined $config->{ $key }; | ||
| 266 | } | ||
| 267 | |||
| 268 | $start = $style->{ START_TAG }; | ||
| 269 | $end = $style->{ END_TAG }; | ||
| 270 | $out = $style->{ OUTLINE_TAG }; | ||
| 271 | $style->{ TEXT_SPLIT } = $self->text_splitter($start, $end, $out); | ||
| 272 | |||
| 273 | push(@$styles, $style); | ||
| 274 | return $style; | ||
| 275 | } | ||
| 276 | |||
| 277 | sub text_splitter { | ||
| 278 | my ($self, $start, $end, $out) = @_; | ||
| 279 | |||
| 280 | if (defined $out) { | ||
| 281 | 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 | 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 | sub old_style { | ||
| 319 | my $self = shift; | ||
| 320 | my $styles = $self->{ STYLE }; | ||
| 321 | return $self->error('only 1 parser style remaining') | ||
| 322 | unless (@$styles > 1); | ||
| 323 | pop @$styles; | ||
| 324 | 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 | sub parse { | ||
| 337 | my ($self, $text, $info) = @_; | ||
| 338 | my ($tokens, $block); | ||
| 339 | |||
| 340 | $info->{ DEBUG } = $self->{ DEBUG_DIRS } | ||
| 341 | unless defined $info->{ DEBUG }; | ||
| 342 | |||
| 343 | # print "info: { ", join(', ', map { "$_ => $info->{ $_ }" } keys %$info), " }\n"; | ||
| 344 | |||
| 345 | # store for blocks defined in the template (see define_block()) | ||
| 346 | my $defblock = $self->{ DEFBLOCK } = { }; | ||
| 347 | my $metadata = $self->{ METADATA } = [ ]; | ||
| 348 | my $variables = $self->{ VARIABLES } = { }; | ||
| 349 | $self->{ DEFBLOCKS } = [ ]; | ||
| 350 | |||
| 351 | $self->{ _ERROR } = ''; | ||
| 352 | |||
| 353 | # split file into TEXT/DIRECTIVE chunks | ||
| 354 | $tokens = $self->split_text($text) | ||
| 355 | || return undef; ## RETURN ## | ||
| 356 | |||
| 357 | push(@{ $self->{ FILEINFO } }, $info); | ||
| 358 | |||
| 359 | # parse chunks | ||
| 360 | $block = $self->_parse($tokens, $info); | ||
| 361 | |||
| 362 | pop(@{ $self->{ FILEINFO } }); | ||
| 363 | |||
| 364 | return undef unless $block; ## RETURN ## | ||
| 365 | |||
| 366 | $self->debug("compiled main template document block:\n$block") | ||
| 367 | if $self->{ DEBUG } & Template::Constants::DEBUG_PARSER; | ||
| 368 | |||
| 369 | return { | ||
| 370 | BLOCK => $block, | ||
| 371 | DEFBLOCKS => $defblock, | ||
| 372 | VARIABLES => $variables, | ||
| 373 | METADATA => { @$metadata }, | ||
| 374 | }; | ||
| 375 | } | ||
| 376 | |||
| 377 | |||
| 378 | |||
| 379 | #------------------------------------------------------------------------ | ||
| 380 | # split_text($text) | ||
| 381 | # | ||
| 382 | # Split input template text into directives and raw text chunks. | ||
| 383 | #------------------------------------------------------------------------ | ||
| 384 | |||
| 385 | sub split_text { | ||
| 386 | my ($self, $text) = @_; | ||
| 387 | my ($pre, $dir, $prelines, $dirlines, $postlines, $chomp, $tags, @tags); | ||
| 388 | my $style = $self->{ STYLE }->[-1]; | ||
| 389 | my ($start, $end, $out, $prechomp, $postchomp, $interp ) = | ||
| 390 | @$style{ qw( START_TAG END_TAG OUTLINE_TAG PRE_CHOMP POST_CHOMP INTERPOLATE ) }; | ||
| 391 | my $tags_dir = $self->{ANYCASE} ? qr<TAGS>i : qr<TAGS>; | ||
| 392 | my $split = $style->{ TEXT_SPLIT }; | ||
| 393 | my $has_out = defined $out; | ||
| 394 | |||
| 395 | my @tokens = (); | ||
| 396 | my $line = 1; | ||
| 397 | |||
| 398 | return \@tokens ## RETURN ## | ||
| 399 | unless defined $text && length $text; | ||
| 400 | |||
| 401 | # extract all directives from the text | ||
| 402 | while ($text =~ s/$split//) { | ||
| 403 | $pre = $1; | ||
| 404 | $dir = defined($2) ? $2 : $3; | ||
| 405 | $pre = '' unless defined $pre; | ||
| 406 | $dir = '' unless defined $dir; | ||
| 407 | |||
| 408 | $prelines = ($pre =~ tr/\n//); # newlines in preceding text | ||
| 409 | $dirlines = ($dir =~ tr/\n//); # newlines in directive tag | ||
| 410 | $postlines = 0; # newlines chomped after tag | ||
| 411 | |||
| 412 | for ($dir) { | ||
| 413 | if (/^\#/) { | ||
| 414 | # comment out entire directive except for any end chomp flag | ||
| 415 | $dir = ($dir =~ /($CHOMP_FLAGS)$/o) ? $1 : ''; | ||
| 416 | } | ||
| 417 | else { | ||
| 418 | |||
| 419 | if(s/^($CHOMP_FLAGS)?(\s*)//so && $2) { | ||
| 420 | my $chomped = $2; | ||
| 421 | my $linecount = ($chomped =~ tr/\n//); # newlines in chomped whitespace | ||
| 422 | $linecount ||= 0; | ||
| 423 | $prelines += $linecount; | ||
| 424 | $dirlines -= $linecount; | ||
| 425 | } | ||
| 426 | # PRE_CHOMP: process whitespace before tag | ||
| 427 | $chomp = $1 ? $1 : $prechomp; | ||
| 428 | $chomp =~ tr/-=~+/1230/; | ||
| 429 | if ($chomp && $pre) { | ||
| 430 | # chomp off whitespace and newline preceding directive | ||
| 431 | if ($chomp == CHOMP_ALL) { | ||
| 432 | $pre =~ s{ (\r?\n|^) [^\S\n]* \z }{}mx; | ||
| 433 | } | ||
| 434 | elsif ($chomp == CHOMP_COLLAPSE) { | ||
| 435 | $pre =~ s{ (\s+) \z }{ }x; | ||
| 436 | } | ||
| 437 | elsif ($chomp == CHOMP_GREEDY) { | ||
| 438 | $pre =~ s{ (\s+) \z }{}x; | ||
| 439 | } | ||
| 440 | } | ||
| 441 | } | ||
| 442 | |||
| 443 | # POST_CHOMP: process whitespace after tag | ||
| 444 | s/\s*($CHOMP_FLAGS)?\s*$//so; | ||
| 445 | $chomp = $1 ? $1 : $postchomp; | ||
| 446 | $chomp =~ tr/-=~+/1230/; | ||
| 447 | if ($chomp) { | ||
| 448 | if ($chomp == CHOMP_ALL) { | ||
| 449 | $text =~ s{ ^ ([^\S\n]* \n) }{}x | ||
| 450 | && $postlines++; | ||
| 451 | } | ||
| 452 | elsif ($chomp == CHOMP_COLLAPSE) { | ||
| 453 | $text =~ s{ ^ (\s+) }{ }x | ||
| 454 | && ($postlines += $1=~y/\n//); | ||
| 455 | } | ||
| 456 | # any trailing whitespace | ||
| 457 | elsif ($chomp == CHOMP_GREEDY) { | ||
| 458 | $text =~ s{ ^ (\s+) }{}x | ||
| 459 | && ($postlines += $1=~y/\n//); | ||
| 460 | } | ||
| 461 | } | ||
| 462 | } | ||
| 463 | |||
| 464 | # any text preceding the directive can now be added | ||
| 465 | if (length $pre) { | ||
| 466 | push(@tokens, $interp | ||
| 467 | ? [ $pre, $line, 'ITEXT' ] | ||
| 468 | : ('TEXT', $pre) ); | ||
| 469 | } | ||
| 470 | $line += $prelines; | ||
| 471 | |||
| 472 | # and now the directive, along with line number information | ||
| 473 | if (length $dir) { | ||
| 474 | # the TAGS directive is a compile-time switch | ||
| 475 | if ($dir =~ /^$tags_dir\s+(.*)/) { | ||
| 476 | my @tags = split(/\s+/, $1); | ||
| 477 | if (scalar @tags > 1) { | ||
| 478 | ($start, $end, $out) = map { quotemeta($_) } @tags; | ||
| 479 | $split = $self->text_splitter($start, $end, $out); | ||
| 480 | } | ||
| 481 | elsif ($tags = $TAG_STYLE->{ $tags[0] }) { | ||
| 482 | ($start, $end, $out) = @$tags; | ||
| 483 | $split = $self->text_splitter($start, $end, $out); | ||
| 484 | } | ||
| 485 | else { | ||
| 486 | warn "invalid TAGS style: $tags[0]\n"; | ||
| 487 | } | ||
| 488 | } | ||
| 489 | else { | ||
| 490 | # DIRECTIVE is pushed as: | ||
| 491 | # [ $dirtext, $line_no(s), \@tokens ] | ||
| 492 | push(@tokens, | ||
| 493 | [ $dir, | ||
| 494 | ($dirlines | ||
| 495 | ? sprintf("%d-%d", $line, $line + $dirlines) | ||
| 496 | : $line), | ||
| 497 | $self->tokenise_directive($dir) ]); | ||
| 498 | } | ||
| 499 | } | ||
| 500 | |||
| 501 | # update line counter to include directive lines and any extra | ||
| 502 | # newline chomped off the start of the following text | ||
| 503 | $line += $dirlines + $postlines; | ||
| 504 | } | ||
| 505 | |||
| 506 | # anything remaining in the string is plain text | ||
| 507 | push(@tokens, $interp | ||
| 508 | ? [ $text, $line, 'ITEXT' ] | ||
| 509 | : ( 'TEXT', $text) ) | ||
| 510 | if length $text; | ||
| 511 | |||
| 512 | return \@tokens; ## RETURN ## | ||
| 513 | } | ||
| 514 | |||
| 515 | |||
| 516 | |||
| 517 | #------------------------------------------------------------------------ | ||
| 518 | # interpolate_text($text, $line) | ||
| 519 | # | ||
| 520 | # Examines $text looking for any variable references embedded like | ||
| 521 | # $this or like ${ this }. | ||
| 522 | #------------------------------------------------------------------------ | ||
| 523 | |||
| 524 | sub interpolate_text { | ||
| 525 | my ($self, $text, $line) = @_; | ||
| 526 | my @tokens = (); | ||
| 527 | my ($pre, $var, $dir); | ||
| 528 | |||
| 529 | |||
| 530 | while ($text =~ | ||
| 531 | / | ||
| 532 | ( (?: \\. | [^\$] ){1,3000} ) # escaped or non-'$' character [$1] | ||
| 533 | | | ||
| 534 | ( \$ (?: # embedded variable [$2] | ||
| 535 | (?: \{ ([^\}]*) \} ) # ${ ... } [$3] | ||
| 536 | | | ||
| 537 | ([\w\.]+) # $word [$4] | ||
| 538 | ) | ||
| 539 | ) | ||
| 540 | /gx) { | ||
| 541 | |||
| 542 | ($pre, $var, $dir) = ($1, $3 || $4, $2); | ||
| 543 | |||
| 544 | # preceding text | ||
| 545 | if (defined($pre) && length($pre)) { | ||
| 546 | $line += $pre =~ tr/\n//; | ||
| 547 | $pre =~ s/\\\$/\$/g; | ||
| 548 | push(@tokens, 'TEXT', $pre); | ||
| 549 | } | ||
| 550 | # $variable reference | ||
| 551 | if ($var) { | ||
| 552 | $line += $dir =~ tr/\n/ /; | ||
| 553 | push(@tokens, [ $dir, $line, $self->tokenise_directive($var) ]); | ||
| 554 | } | ||
| 555 | # other '$' reference - treated as text | ||
| 556 | elsif ($dir) { | ||
| 557 | $line += $dir =~ tr/\n//; | ||
| 558 | push(@tokens, 'TEXT', $dir); | ||
| 559 | } | ||
| 560 | } | ||
| 561 | |||
| 562 | return \@tokens; | ||
| 563 | } | ||
| 564 | |||
| 565 | |||
| 566 | |||
| 567 | #------------------------------------------------------------------------ | ||
| 568 | # tokenise_directive($text) | ||
| 569 | # | ||
| 570 | # Called by the private _parse() method when it encounters a DIRECTIVE | ||
| 571 | # token in the list provided by the split_text() or interpolate_text() | ||
| 572 | # methods. The directive text is passed by parameter. | ||
| 573 | # | ||
| 574 | # The method splits the directive into individual tokens as recognised | ||
| 575 | # by the parser grammar (see Template::Grammar for details). It | ||
| 576 | # constructs a list of tokens each represented by 2 elements, as per | ||
| 577 | # split_text() et al. The first element contains the token type, the | ||
| 578 | # second the token itself. | ||
| 579 | # | ||
| 580 | # The method tokenises the string using a complex (but fast) regex. | ||
| 581 | # For a deeper understanding of the regex magic at work here, see | ||
| 582 | # Jeffrey Friedl's excellent book "Mastering Regular Expressions", | ||
| 583 | # from O'Reilly, ISBN 1-56592-257-3 | ||
| 584 | # | ||
| 585 | # Returns a reference to the list of chunks (each one being 2 elements) | ||
| 586 | # identified in the directive text. On error, the internal _ERROR string | ||
| 587 | # is set and undef is returned. | ||
| 588 | #------------------------------------------------------------------------ | ||
| 589 | |||
| 590 | sub tokenise_directive { | ||
| 591 | my ($self, $text, $line) = @_; | ||
| 592 | my ($token, $uctoken, $type, $lookup); | ||
| 593 | my $lextable = $self->{ LEXTABLE }; | ||
| 594 | my $style = $self->{ STYLE }->[-1]; | ||
| 595 | my ($anycase, $start, $end) = @$style{ qw( ANYCASE START_TAG END_TAG ) }; | ||
| 596 | my @tokens = ( ); | ||
| 597 | |||
| 598 | while ($text =~ | ||
| 599 | / | ||
| 600 | # strip out any comments | ||
| 601 | (\#[^\n]*) | ||
| 602 | | | ||
| 603 | # a quoted phrase matches in $3 | ||
| 604 | (["']) # $2 - opening quote, ' or " | ||
| 605 | ( # $3 - quoted text buffer | ||
| 606 | (?: # repeat group (no backreference) | ||
| 607 | \\\\ # an escaped backslash \\ | ||
| 608 | | # ...or... | ||
| 609 | \\\2 # an escaped quote \" or \' (match $1) | ||
| 610 | | # ...or... | ||
| 611 | . # any other character | ||
| 612 | | \n | ||
| 613 | )*? # non-greedy repeat | ||
| 614 | ) # end of $3 | ||
| 615 | \2 # match opening quote | ||
| 616 | | | ||
| 617 | # an unquoted number matches in $4 | ||
| 618 | (-?\d+(?:\.\d+)?) # numbers | ||
| 619 | | | ||
| 620 | # filename matches in $5 | ||
| 621 | ( \/?\w+(?:(?:\/|::?)\w*)+ | \/\w+) | ||
| 622 | | | ||
| 623 | # an identifier matches in $6 | ||
| 624 | (\w+) # variable identifier | ||
| 625 | | | ||
| 626 | # an unquoted word or symbol matches in $7 | ||
| 627 | ( [(){}\[\]:;,\/\\] # misc parenthesis and symbols | ||
| 628 | # | \-> # arrow operator (for future?) | ||
| 629 | | [+\-*] # math operations | ||
| 630 | | \$\{? # dollar with option left brace | ||
| 631 | | => # like '=' | ||
| 632 | | [=!<>]?= | [!<>] # eqality tests | ||
| 633 | | &&? | \|\|? # boolean ops | ||
| 634 | | \.\.? # n..n sequence | ||
| 635 | | \S+ # something unquoted | ||
| 636 | ) # end of $7 | ||
| 637 | /gmxo) { | ||
| 638 | |||
| 639 | # ignore comments to EOL | ||
| 640 | next if $1; | ||
| 641 | |||
| 642 | # quoted string | ||
| 643 | if (defined ($token = $3)) { | ||
| 644 | # double-quoted string may include $variable references | ||
| 645 | if ($2 eq '"') { | ||
| 646 | if ($token =~ /[\$\\]/) { | ||
| 647 | $type = 'QUOTED'; | ||
| 648 | # unescape " and \ but leave \$ escaped so that | ||
| 649 | # interpolate_text() doesn't incorrectly treat it | ||
| 650 | # as a variable reference | ||
| 651 | # $token =~ s/\\([\\"])/$1/g; | ||
| 652 | for ($token) { | ||
| 653 | s/\\([^\$nrt])/$1/g; | ||
| 654 | s/\\([nrt])/$QUOTED_ESCAPES->{ $1 }/ge; | ||
| 655 | } | ||
| 656 | push(@tokens, ('"') x 2, | ||
| 657 | @{ $self->interpolate_text($token) }, | ||
| 658 | ('"') x 2); | ||
| 659 | next; | ||
| 660 | } | ||
| 661 | else { | ||
| 662 | $type = 'LITERAL'; | ||
| 663 | $token =~ s['][\\']g; | ||
| 664 | $token = "'$token'"; | ||
| 665 | } | ||
| 666 | } | ||
| 667 | else { | ||
| 668 | $type = 'LITERAL'; | ||
| 669 | $token = "'$token'"; | ||
| 670 | } | ||
| 671 | } | ||
| 672 | # number | ||
| 673 | elsif (defined ($token = $4)) { | ||
| 674 | $type = 'NUMBER'; | ||
| 675 | } | ||
| 676 | elsif (defined($token = $5)) { | ||
| 677 | $type = 'FILENAME'; | ||
| 678 | } | ||
| 679 | elsif (defined($token = $6)) { | ||
| 680 | # Fold potential keywords to UPPER CASE if the ANYCASE option is | ||
| 681 | # set, unless (we've got some preceding tokens and) the previous | ||
| 682 | # token is a DOT op. This prevents the 'last' in 'data.last' | ||
| 683 | # from being interpreted as the LAST keyword. | ||
| 684 | if ($anycase) { | ||
| 685 | # if the token follows a dot or precedes an assignment then | ||
| 686 | # it's not for folding, e.g. the 'wrapper' in this: | ||
| 687 | # [% page = { wrapper='html' }; page.wrapper %] | ||
| 688 | if ((@tokens && $ANYCASE_AFTER->{ $tokens[-2] }) | ||
| 689 | || ($text =~ /$ANYCASE_BEFORE/gc)) { | ||
| 690 | # keep the token unmodified | ||
| 691 | $uctoken = $token; | ||
| 692 | } | ||
| 693 | else { | ||
| 694 | $uctoken = uc $token; | ||
| 695 | } | ||
| 696 | } | ||
| 697 | else { | ||
| 698 | $uctoken = $token; | ||
| 699 | } | ||
| 700 | if (defined ($type = $lextable->{ $uctoken })) { | ||
| 701 | $token = $uctoken; | ||
| 702 | } | ||
| 703 | else { | ||
| 704 | $type = 'IDENT'; | ||
| 705 | } | ||
| 706 | } | ||
| 707 | elsif (defined ($token = $7)) { | ||
| 708 | # reserved words may be in lower case unless case sensitive | ||
| 709 | $uctoken = $anycase ? uc $token : $token; | ||
| 710 | unless (defined ($type = $lextable->{ $uctoken })) { | ||
| 711 | $type = 'UNQUOTED'; | ||
| 712 | } | ||
| 713 | } | ||
| 714 | |||
| 715 | push(@tokens, $type, $token); | ||
| 716 | |||
| 717 | # print(STDERR " +[ $type, $token ]\n") | ||
| 718 | # if $DEBUG; | ||
| 719 | } | ||
| 720 | |||
| 721 | # print STDERR "tokenise directive() returning:\n [ @tokens ]\n" | ||
| 722 | # if $DEBUG; | ||
| 723 | |||
| 724 | return \@tokens; ## RETURN ## | ||
| 725 | } | ||
| 726 | |||
| 727 | |||
| 728 | #------------------------------------------------------------------------ | ||
| 729 | # define_block($name, $block) | ||
| 730 | # | ||
| 731 | # Called by the parser 'defblock' rule when a BLOCK definition is | ||
| 732 | # encountered in the template. The name of the block is passed in the | ||
| 733 | # first parameter and a reference to the compiled block is passed in | ||
| 734 | # the second. This method stores the block in the $self->{ DEFBLOCK } | ||
| 735 | # hash which has been initialised by parse() and will later be used | ||
| 736 | # by the same method to call the store() method on the calling cache | ||
| 737 | # to define the block "externally". | ||
| 738 | #------------------------------------------------------------------------ | ||
| 739 | |||
| 740 | sub define_block { | ||
| 741 | my ($self, $name, $block) = @_; | ||
| 742 | my $defblock = $self->{ DEFBLOCK } | ||
| 743 | || return undef; | ||
| 744 | |||
| 745 | $self->debug("compiled block '$name':\n$block") | ||
| 746 | if $self->{ DEBUG } & Template::Constants::DEBUG_PARSER; | ||
| 747 | |||
| 748 | warn "Block redefined: $name\n" if exists $defblock->{ $name }; | ||
| 749 | |||
| 750 | $defblock->{ $name } = $block; | ||
| 751 | |||
| 752 | return undef; | ||
| 753 | } | ||
| 754 | |||
| 755 | sub push_defblock { | ||
| 756 | my $self = shift; | ||
| 757 | my $stack = $self->{ DEFBLOCK_STACK } ||= []; | ||
| 758 | push(@$stack, $self->{ DEFBLOCK } ); | ||
| 759 | $self->{ DEFBLOCK } = { }; | ||
| 760 | } | ||
| 761 | |||
| 762 | sub pop_defblock { | ||
| 763 | my $self = shift; | ||
| 764 | my $defs = $self->{ DEFBLOCK }; | ||
| 765 | my $stack = $self->{ DEFBLOCK_STACK } || return $defs; | ||
| 766 | return $defs unless @$stack; | ||
| 767 | $self->{ DEFBLOCK } = pop @$stack; | ||
| 768 | return $defs; | ||
| 769 | } | ||
| 770 | |||
| 771 | |||
| 772 | #------------------------------------------------------------------------ | ||
| 773 | # add_metadata(\@setlist) | ||
| 774 | #------------------------------------------------------------------------ | ||
| 775 | |||
| 776 | sub add_metadata { | ||
| 777 | my ($self, $setlist) = @_; | ||
| 778 | my $metadata = $self->{ METADATA } | ||
| 779 | || return undef; | ||
| 780 | |||
| 781 | push(@$metadata, @$setlist); | ||
| 782 | |||
| 783 | return undef; | ||
| 784 | } | ||
| 785 | |||
| 786 | |||
| 787 | #------------------------------------------------------------------------ | ||
| 788 | # location() | ||
| 789 | # | ||
| 790 | # Return Perl comment indicating current parser file and line | ||
| 791 | #------------------------------------------------------------------------ | ||
| 792 | |||
| 793 | sub location { | ||
| 794 | my $self = shift; | ||
| 795 | return "\n" unless $self->{ FILE_INFO }; | ||
| 796 | my $line = ${ $self->{ LINE } }; | ||
| 797 | my $info = $self->{ FILEINFO }->[-1]; | ||
| 798 | my $file = $info->{ path } || $info->{ name } | ||
| 799 | || '(unknown template)'; | ||
| 800 | $line =~ s/\-.*$//; # might be 'n-n' | ||
| 801 | $line ||= 1; | ||
| 802 | return "#line $line \"$file\"\n"; | ||
| 803 | } | ||
| 804 | |||
| 805 | |||
| 806 | #======================================================================== | ||
| 807 | # ----- PRIVATE METHODS ----- | ||
| 808 | #======================================================================== | ||
| 809 | |||
| 810 | #------------------------------------------------------------------------ | ||
| 811 | # _parse(\@tokens, \@info) | ||
| 812 | # | ||
| 813 | # Parses the list of input tokens passed by reference and returns a | ||
| 814 | # Template::Directive::Block object which contains the compiled | ||
| 815 | # representation of the template. | ||
| 816 | # | ||
| 817 | # This is the main parser DFA loop. See embedded comments for | ||
| 818 | # further details. | ||
| 819 | # | ||
| 820 | # On error, undef is returned and the internal _ERROR field is set to | ||
| 821 | # indicate the error. This can be retrieved by calling the error() | ||
| 822 | # method. | ||
| 823 | #------------------------------------------------------------------------ | ||
| 824 | |||
| 825 | sub _parse { | ||
| 826 | my ($self, $tokens, $info) = @_; | ||
| 827 | my ($token, $value, $text, $line, $inperl); | ||
| 828 | my ($state, $stateno, $status, $action, $lookup, $coderet, @codevars); | ||
| 829 | my ($lhs, $len, $code); # rule contents | ||
| 830 | my $stack = [ [ 0, undef ] ]; # DFA stack | ||
| 831 | |||
| 832 | # DEBUG | ||
| 833 | # local $" = ', '; | ||
| 834 | |||
| 835 | # retrieve internal rule and state tables | ||
| 836 | my ($states, $rules) = @$self{ qw( STATES RULES ) }; | ||
| 837 | |||
| 838 | # If we're tracing variable usage then we need to give the factory a | ||
| 839 | # reference to our $self->{ VARIABLES } for it to fill in. This is a | ||
| 840 | # bit of a hack to back-patch this functionality into TT2. | ||
| 841 | $self->{ FACTORY }->trace_vars($self->{ VARIABLES }) | ||
| 842 | if $self->{ TRACE_VARS }; | ||
| 843 | |||
| 844 | # call the grammar set_factory method to install emitter factory | ||
| 845 | $self->{ GRAMMAR }->install_factory($self->{ FACTORY }); | ||
| 846 | |||
| 847 | $line = $inperl = 0; | ||
| 848 | $self->{ LINE } = \$line; | ||
| 849 | $self->{ FILE } = $info->{ name }; | ||
| 850 | $self->{ INPERL } = \$inperl; | ||
| 851 | |||
| 852 | $status = CONTINUE; | ||
| 853 | my $in_string = 0; | ||
| 854 | |||
| 855 | while(1) { | ||
| 856 | # get state number and state | ||
| 857 | $stateno = $stack->[-1]->[0]; | ||
| 858 | $state = $states->[$stateno]; | ||
| 859 | |||
| 860 | # see if any lookaheads exist for the current state | ||
| 861 | if (exists $state->{'ACTIONS'}) { | ||
| 862 | |||
| 863 | # get next token and expand any directives (i.e. token is an | ||
| 864 | # array ref) onto the front of the token list | ||
| 865 | while (! defined $token && @$tokens) { | ||
| 866 | $token = shift(@$tokens); | ||
| 867 | if (ref $token) { | ||
| 868 | ($text, $line, $token) = @$token; | ||
| 869 | if (ref $token) { | ||
| 870 | if ($info->{ DEBUG } && ! $in_string) { | ||
| 871 | # - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 872 | # This is gnarly. Look away now if you're easily | ||
| 873 | # frightened. We're pushing parse tokens onto the | ||
| 874 | # pending list to simulate a DEBUG directive like so: | ||
| 875 | # [% DEBUG msg line='20' text='INCLUDE foo' %] | ||
| 876 | # - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 877 | my $dtext = $text; | ||
| 878 | $dtext =~ s[(['\\])][\\$1]g; | ||
| 879 | unshift(@$tokens, | ||
| 880 | DEBUG => 'DEBUG', | ||
| 881 | IDENT => 'msg', | ||
| 882 | IDENT => 'line', | ||
| 883 | ASSIGN => '=', | ||
| 884 | LITERAL => "'$line'", | ||
| 885 | IDENT => 'text', | ||
| 886 | ASSIGN => '=', | ||
| 887 | LITERAL => "'$dtext'", | ||
| 888 | IDENT => 'file', | ||
| 889 | ASSIGN => '=', | ||
| 890 | LITERAL => "'$info->{ name }'", | ||
| 891 | (';') x 2, | ||
| 892 | @$token, | ||
| 893 | (';') x 2); | ||
| 894 | } | ||
| 895 | else { | ||
| 896 | unshift(@$tokens, @$token, (';') x 2); | ||
| 897 | } | ||
| 898 | $token = undef; # force redo | ||
| 899 | } | ||
| 900 | elsif ($token eq 'ITEXT') { | ||
| 901 | if ($inperl) { | ||
| 902 | # don't perform interpolation in PERL blocks | ||
| 903 | $token = 'TEXT'; | ||
| 904 | $value = $text; | ||
| 905 | } | ||
| 906 | else { | ||
| 907 | unshift(@$tokens, | ||
| 908 | @{ $self->interpolate_text($text, $line) }); | ||
| 909 | $token = undef; # force redo | ||
| 910 | } | ||
| 911 | } | ||
| 912 | } | ||
| 913 | else { | ||
| 914 | # toggle string flag to indicate if we're crossing | ||
| 915 | # a string boundary | ||
| 916 | $in_string = ! $in_string if $token eq '"'; | ||
| 917 | $value = shift(@$tokens); | ||
| 918 | } | ||
| 919 | }; | ||
| 920 | # clear undefined token to avoid 'undefined variable blah blah' | ||
| 921 | # warnings and let the parser logic pick it up in a minute | ||
| 922 | $token = '' unless defined $token; | ||
| 923 | |||
| 924 | # get the next state for the current lookahead token | ||
| 925 | $action = defined ($lookup = $state->{'ACTIONS'}->{ $token }) | ||
| 926 | ? $lookup | ||
| 927 | : defined ($lookup = $state->{'DEFAULT'}) | ||
| 928 | ? $lookup | ||
| 929 | : undef; | ||
| 930 | } | ||
| 931 | else { | ||
| 932 | # no lookahead actions | ||
| 933 | $action = $state->{'DEFAULT'}; | ||
| 934 | } | ||
| 935 | |||
| 936 | # ERROR: no ACTION | ||
| 937 | last unless defined $action; | ||
| 938 | |||
| 939 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 940 | # shift (+ive ACTION) | ||
| 941 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 942 | if ($action > 0) { | ||
| 943 | push(@$stack, [ $action, $value ]); | ||
| 944 | $token = $value = undef; | ||
| 945 | redo; | ||
| 946 | }; | ||
| 947 | |||
| 948 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 949 | # reduce (-ive ACTION) | ||
| 950 | # - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 951 | ($lhs, $len, $code) = @{ $rules->[ -$action ] }; | ||
| 952 | |||
| 953 | # no action imples ACCEPTance | ||
| 954 | $action | ||
| 955 | or $status = ACCEPT; | ||
| 956 | |||
| 957 | # use dummy sub if code ref doesn't exist | ||
| 958 | if ( !$code ) { | ||
| 959 | $coderet = $len ? $stack->[ -$len ]->[1] : undef; | ||
| 960 | } else { | ||
| 961 | # $code = sub { $_[1] } | ||
| 962 | # unless $code; | ||
| 963 | |||
| 964 | @codevars = $len | ||
| 965 | ? map { $_->[1] } @$stack[ -$len .. -1 ] | ||
| 966 | : (); | ||
| 967 | |||
| 968 | eval { | ||
| 969 | $coderet = &$code( $self, @codevars ); | ||
| 970 | }; | ||
| 971 | if ($@) { | ||
| 972 | my $err = $@; | ||
| 973 | chomp $err; | ||
| 974 | return $self->_parse_error($err); | ||
| 975 | } | ||
| 976 | } | ||
| 977 | |||
| 978 | |||
| 979 | # reduce stack by $len | ||
| 980 | splice(@$stack, -$len, $len); | ||
| 981 | |||
| 982 | # ACCEPT | ||
| 983 | return $coderet ## RETURN ## | ||
| 984 | if $status == ACCEPT; | ||
| 985 | |||
| 986 | # ABORT | ||
| 987 | return undef ## RETURN ## | ||
| 988 | if $status == ABORT; | ||
| 989 | |||
| 990 | # ERROR | ||
| 991 | last | ||
| 992 | if $status == ERROR; | ||
| 993 | } | ||
| 994 | continue { | ||
| 995 | push(@$stack, [ $states->[ $stack->[-1][0] ]->{'GOTOS'}->{ $lhs }, | ||
| 996 | $coderet ]), | ||
| 997 | } | ||
| 998 | |||
| 999 | # ERROR ## RETURN ## | ||
| 1000 | return $self->_parse_error('unexpected end of input') | ||
| 1001 | unless defined $value; | ||
| 1002 | |||
| 1003 | # munge text of last directive to make it readable | ||
| 1004 | # $text =~ s/\n/\\n/g; | ||
| 1005 | |||
| 1006 | return $self->_parse_error("unexpected end of directive", $text) | ||
| 1007 | if $value eq ';'; # end of directive SEPARATOR | ||
| 1008 | |||
| 1009 | return $self->_parse_error("unexpected token ($value)", $text); | ||
| 1010 | } | ||
| 1011 | |||
| 1012 | |||
| 1013 | |||
| 1014 | #------------------------------------------------------------------------ | ||
| 1015 | # _parse_error($msg, $dirtext) | ||
| 1016 | # | ||
| 1017 | # Method used to handle errors encountered during the parse process | ||
| 1018 | # in the _parse() method. | ||
| 1019 | #------------------------------------------------------------------------ | ||
| 1020 | |||
| 1021 | sub _parse_error { | ||
| 1022 | my ($self, $msg, $text) = @_; | ||
| 1023 | my $line = $self->{ LINE }; | ||
| 1024 | $line = ref($line) ? $$line : $line; | ||
| 1025 | $line = 'unknown' unless $line; | ||
| 1026 | |||
| 1027 | $msg .= "\n [% $text %]" | ||
| 1028 | if defined $text; | ||
| 1029 | |||
| 1030 | return $self->error("line $line: $msg"); | ||
| 1031 | } | ||
| 1032 | |||
| 1033 | 1; | ||
| 1034 | |||
| 1035 | __END__ |