X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fp5-CGI-Ex;a=blobdiff_plain;f=lib%2FCGI%2FEx%2FTemplate.pm;h=37dda33bd88b25f24ce7607b666bc0f03190dbfa;hp=f326155b16742ec42deae3d8febca4ea158ff190;hb=6c57b3331d84010b9e2031f8e3c8937c3117e8fc;hpb=a8620142ba0dcda3f0c5f102f791df944ed2245e diff --git a/lib/CGI/Ex/Template.pm b/lib/CGI/Ex/Template.pm index f326155..37dda33 100644 --- a/lib/CGI/Ex/Template.pm +++ b/lib/CGI/Ex/Template.pm @@ -1,8 +1,11 @@ package CGI::Ex::Template; +#STAT_TTL +#memory leak in USE + ###----------------------------------------------------------------### # See the perldoc in CGI/Ex/Template.pod -# Copyright 2006 - Paul Seamons # +# Copyright 2007 - Paul Seamons # # Distributed under the Perl Artistic License without warranty # ###----------------------------------------------------------------### @@ -10,125 +13,166 @@ use strict; use constant trace => $ENV{'CET_TRACE'} || 0; # enable for low level tracing use vars qw($VERSION $TAGS - $SCALAR_OPS $HASH_OPS $LIST_OPS $FILTER_OPS + $SCALAR_OPS $HASH_OPS $LIST_OPS $FILTER_OPS $VOBJS $DIRECTIVES $QR_DIRECTIVE $OPERATORS - $OP_UNARY - $OP_BINARY - $OP_TRINARY $OP_DISPATCH + $OP_ASSIGN + $OP + $OP_PREFIX + $OP_POSTFIX + $OP_TERNARY $QR_OP - $QR_OP_UNARY - $QR_OP_PARENED + $QR_OP_PREFIX + $QR_OP_ASSIGN $QR_COMMENTS $QR_FILENAME - $QR_AQ_NOTDOT + $QR_NUM $QR_AQ_SPACE $QR_PRIVATE $PACKAGE_EXCEPTION $PACKAGE_ITERATOR $PACKAGE_CONTEXT $PACKAGE_STASH $PACKAGE_PERL_HANDLE + $MAX_EVAL_RECURSE $MAX_MACRO_RECURSE $WHILE_MAX $EXTRA_COMPILE_EXT $DEBUG + + @CONFIG_COMPILETIME + @CONFIG_RUNTIME ); BEGIN { - $VERSION = '2.01'; + $VERSION = '2.11'; $PACKAGE_EXCEPTION = 'CGI::Ex::Template::Exception'; $PACKAGE_ITERATOR = 'CGI::Ex::Template::Iterator'; $PACKAGE_CONTEXT = 'CGI::Ex::Template::_Context'; $PACKAGE_STASH = 'CGI::Ex::Template::_Stash'; $PACKAGE_PERL_HANDLE = 'CGI::Ex::Template::EvalPerlHandle'; - - $TAGS ||= { - default => ['[%', '%]'], # default - template => ['[%', '%]'], # default - metatext => ['%%', '%%'], # Text::MetaText - star => ['[*', '*]'], # TT alternate - php => [''], # PHP - asp => ['<%', '%>'], # ASP - mason => ['<%', '>' ], # HTML::Mason - html => [''], # HTML comments + $MAX_EVAL_RECURSE = 50; + $MAX_MACRO_RECURSE = 50; + + $TAGS = { + asp => ['<%', '%>' ], # ASP + default => ['\[%', '%\]' ], # default + html => ['' ], # HTML comments + mason => ['<%', '>' ], # HTML::Mason + metatext => ['%%', '%%' ], # Text::MetaText + php => ['<\?', '\?>' ], # PHP + star => ['\[\*', '\*\]' ], # TT alternate + template => ['\[%', '%\]' ], # Normal Template Toolkit + template1 => ['[\[%]%', '%[%\]]'], # allow TT1 style + tt2 => ['\[%', '%\]' ], # TT2 }; - $SCALAR_OPS ||= { + $SCALAR_OPS = { + '0' => sub { $_[0] }, chunk => \&vmethod_chunk, collapse => sub { local $_ = $_[0]; s/^\s+//; s/\s+$//; s/\s+/ /g; $_ }, - defined => sub { 1 }, + defined => sub { defined $_[0] ? 1 : '' }, indent => \&vmethod_indent, + int => sub { local $^W; int $_[0] }, + fmt => \&vmethod_fmt_scalar, 'format' => \&vmethod_format, hash => sub { {value => $_[0]} }, - html => sub { local $_ = $_[0]; s/&/&/g; s//>/g; s/\"/"/g; $_ }, + html => sub { local $_ = $_[0]; s/&/&/g; s//>/g; s/\"/"/g; s/\'/'/g; $_ }, + item => sub { $_[0] }, lcfirst => sub { lcfirst $_[0] }, length => sub { defined($_[0]) ? length($_[0]) : 0 }, + list => sub { [$_[0]] }, lower => sub { lc $_[0] }, match => \&vmethod_match, + new => sub { defined $_[0] ? $_[0] : '' }, null => sub { '' }, + rand => sub { local $^W; rand shift }, remove => sub { vmethod_replace(shift, shift, '', 1) }, repeat => \&vmethod_repeat, replace => \&vmethod_replace, - search => sub { my ($str, $pat) = @_; return $str if ! defined $str || ! defined $pat; return scalar $str =~ /$pat/ }, + search => sub { my ($str, $pat) = @_; return $str if ! defined $str || ! defined $pat; return $str =~ /$pat/ }, size => sub { 1 }, split => \&vmethod_split, stderr => sub { print STDERR $_[0]; '' }, - substr => sub { my ($str, $i, $len) = @_; defined($len) ? substr($str, $i, $len) : substr($str, $i) }, + substr => \&vmethod_substr, trim => sub { local $_ = $_[0]; s/^\s+//; s/\s+$//; $_ }, ucfirst => sub { ucfirst $_[0] }, upper => sub { uc $_[0] }, uri => \&vmethod_uri, + url => \&vmethod_url, }; - $FILTER_OPS ||= { # generally - non-dynamic filters belong in scalar ops + $FILTER_OPS = { # generally - non-dynamic filters belong in scalar ops eval => [\&filter_eval, 1], evaltt => [\&filter_eval, 1], file => [\&filter_redirect, 1], redirect => [\&filter_redirect, 1], }; - $LIST_OPS ||= { + $LIST_OPS = { + defined => sub { return 1 if @_ == 1; defined $_[0]->[ defined($_[1]) ? $_[1] : 0 ] }, first => sub { my ($ref, $i) = @_; return $ref->[0] if ! $i; return [@{$ref}[0 .. $i - 1]]}, - grep => sub { my ($ref, $pat) = @_; [grep {/$pat/} @$ref] }, - hash => sub { my ($list, $i) = @_; defined($i) ? {map {$i++ => $_} @$list} : {@$list} }, + fmt => \&vmethod_fmt_list, + grep => sub { local $^W; my ($ref, $pat) = @_; [grep {/$pat/} @$ref] }, + hash => sub { local $^W; my $list = shift; return {@$list} if ! @_; my $i = shift || 0; return {map {$i++ => $_} @$list} }, + import => sub { my $ref = shift; push @$ref, grep {defined} map {ref eq 'ARRAY' ? @$_ : undef} @_; '' }, + item => sub { $_[0]->[ $_[1] || 0 ] }, join => sub { my ($ref, $join) = @_; $join = ' ' if ! defined $join; local $^W; return join $join, @$ref }, last => sub { my ($ref, $i) = @_; return $ref->[-1] if ! $i; return [@{$ref}[-$i .. -1]]}, list => sub { $_[0] }, - max => sub { $#{ $_[0] } }, + max => sub { local $^W; $#{ $_[0] } }, merge => sub { my $ref = shift; return [ @$ref, grep {defined} map {ref eq 'ARRAY' ? @$_ : undef} @_ ] }, + new => sub { local $^W; return [@_] }, + null => sub { '' }, nsort => \&vmethod_nsort, + pick => \&vmethod_pick, pop => sub { pop @{ $_[0] } }, push => sub { my $ref = shift; push @$ref, @_; return '' }, reverse => sub { [ reverse @{ $_[0] } ] }, shift => sub { shift @{ $_[0] } }, - size => sub { scalar @{ $_[0] } }, + size => sub { local $^W; scalar @{ $_[0] } }, slice => sub { my ($ref, $a, $b) = @_; $a ||= 0; $b = $#$ref if ! defined $b; return [@{$ref}[$a .. $b]] }, sort => \&vmethod_sort, splice => \&vmethod_splice, - unique => sub { my %u; return [ grep { ! $u{$_} ++ } @{ $_[0] } ] }, + unique => sub { my %u; return [ grep { ! $u{$_}++ } @{ $_[0] } ] }, unshift => sub { my $ref = shift; unshift @$ref, @_; return '' }, }; - $HASH_OPS ||= { - defined => sub { return '' if ! defined $_[1]; defined $_[0]->{ $_[1] } }, - delete => sub { return '' if ! defined $_[1]; delete $_[0]->{ $_[1] } }, + $HASH_OPS = { + defined => sub { return 1 if @_ == 1; defined $_[0]->{ defined($_[1]) ? $_[1] : '' } }, + delete => sub { my $h = shift; delete @{ $h }{map {defined($_) ? $_ : ''} @_}; '' }, each => sub { [%{ $_[0] }] }, - exists => sub { return '' if ! defined $_[1]; exists $_[0]->{ $_[1] } }, + exists => sub { exists $_[0]->{ defined($_[1]) ? $_[1] : '' } }, + fmt => \&vmethod_fmt_hash, hash => sub { $_[0] }, - import => sub { my ($a, $b) = @_; return '' if ref($b) ne 'HASH'; @{$a}{keys %$b} = values %$b; '' }, + import => sub { my ($a, $b) = @_; @{$a}{keys %$b} = values %$b if ref($b) eq 'HASH'; '' }, + item => sub { my ($h, $k) = @_; $k = '' if ! defined $k; $k =~ $QR_PRIVATE ? undef : $h->{$k} }, + items => sub { [ %{ $_[0] } ] }, keys => sub { [keys %{ $_[0] }] }, - list => sub { [$_[0]] }, - pairs => sub { [map { {key => $_, value => $_[0]->{$_}} } keys %{ $_[0] } ] }, - nsort => sub { my $ref = shift; [sort {$ref->{$a} <=> $ref->{$b} } keys %$ref] }, + list => \&vmethod_list_hash, + new => sub { local $^W; return (@_ == 1 && ref $_[-1] eq 'HASH') ? $_[-1] : {@_} }, + null => sub { '' }, + nsort => sub { my $ref = shift; [sort { $ref->{$a} <=> $ref->{$b}} keys %$ref] }, + pairs => sub { [map { {key => $_, value => $_[0]->{$_}} } sort keys %{ $_[0] } ] }, size => sub { scalar keys %{ $_[0] } }, sort => sub { my $ref = shift; [sort {lc $ref->{$a} cmp lc $ref->{$b}} keys %$ref] }, values => sub { [values %{ $_[0] }] }, }; + $VOBJS = { + Text => $SCALAR_OPS, + List => $LIST_OPS, + Hash => $HASH_OPS, + }; + foreach (values %$VOBJS) { + $_->{'Text'} = $_->{'as'}; + $_->{'Hash'} = $_->{'hash'}; + $_->{'List'} = $_->{'list'}; + } + $DIRECTIVES = { - #name #parse_sub #play_sub #block #postdir #continue #move_to_front + #name parse_sub play_sub block postdir continue move_to_front BLOCK => [\&parse_BLOCK, \&play_BLOCK, 1, 0, 0, 1], BREAK => [sub {}, \&play_control], CALL => [\&parse_CALL, \&play_CALL], @@ -136,6 +180,7 @@ BEGIN { CATCH => [\&parse_CATCH, undef, 0, 0, {TRY => 1, CATCH => 1}], CLEAR => [sub {}, \&play_CLEAR], '#' => [sub {}, sub {}], + CONFIG => [\&parse_CONFIG, \&play_CONFIG], DEBUG => [\&parse_DEBUG, \&play_DEBUG], DEFAULT => [\&parse_DEFAULT, \&play_DEFAULT], DUMP => [\&parse_DUMP, \&play_DUMP], @@ -153,8 +198,7 @@ BEGIN { INSERT => [\&parse_INSERT, \&play_INSERT], LAST => [sub {}, \&play_control], MACRO => [\&parse_MACRO, \&play_MACRO], - META => [undef, sub {}], - METADEF => [undef, \&play_METADEF], + META => [undef, \&play_META], NEXT => [sub {}, \&play_control], PERL => [\&parse_PERL, \&play_PERL, 1], PROCESS => [\&parse_PROCESS, \&play_PROCESS], @@ -168,75 +212,95 @@ BEGIN { TRY => [sub {}, \&play_TRY, 1], UNLESS => [\&parse_UNLESS, \&play_UNLESS, 1, 1], USE => [\&parse_USE, \&play_USE], - WHILE => [\&parse_IF, \&play_WHILE, 1, 1], + VIEW => [\&parse_VIEW, \&play_VIEW, 1], + WHILE => [\&parse_WHILE, \&play_WHILE, 1, 1], WRAPPER => [\&parse_WRAPPER, \&play_WRAPPER, 1, 1], #name #parse_sub #play_sub #block #postdir #continue #move_to_front }; - $QR_DIRECTIVE = qr{ ^ (\w+|\|) (?= $|[\s;\#]) }x; ### setup the operator parsing - $OPERATORS ||= [ - # name => # order, precedence, symbols, only_in_parens, sub to create - [2, 96, ['**', '^', 'pow'], 0, sub { $_[0] ** $_[1] } ], - [1, 93, ['!'], 0, sub { ! $_[0] } ], - [1, 93, ['-'], 0, sub { @_ == 1 ? 0 - $_[0] : $_[0] - $_[1] } ], - [2, 90, ['*'], 0, sub { $_[0] * $_[1] } ], - [2, 90, ['/'], 0, sub { $_[0] / $_[1] } ], - [2, 90, ['div', 'DIV'], 0, sub { int($_[0] / $_[1]) } ], - [2, 90, ['%', 'mod', 'MOD'], 0, sub { $_[0] % $_[1] } ], - [2, 85, ['+'], 0, sub { $_[0] + $_[1] } ], - [2, 85, ['-'], 0, sub { @_ == 1 ? 0 - $_[0] : $_[0] - $_[1] } ], - [2, 85, ['_', '~'], 0, sub { join "", @_ } ], - [2, 80, ['<'], 0, sub { $_[0] < $_[1] } ], - [2, 80, ['>'], 0, sub { $_[0] > $_[1] } ], - [2, 80, ['<='], 0, sub { $_[0] <= $_[1] } ], - [2, 80, ['>='], 0, sub { $_[0] >= $_[1] } ], - [2, 80, ['lt'], 0, sub { $_[0] lt $_[1] } ], - [2, 80, ['gt'], 0, sub { $_[0] gt $_[1] } ], - [2, 80, ['le'], 0, sub { $_[0] le $_[1] } ], - [2, 80, ['ge'], 0, sub { $_[0] ge $_[1] } ], - [2, 75, ['==', 'eq'], 0, sub { $_[0] eq $_[1] } ], - [2, 75, ['!=', 'ne'], 0, sub { $_[0] ne $_[1] } ], - [2, 70, ['&&'], 0, undef ], - [2, 65, ['||'], 0, undef ], - [2, 60, ['..'], 0, sub { $_[0] .. $_[1] } ], - [3, 55, ['?', ':'], 0, undef ], - [2, 52, ['='], 1, undef ], - [1, 50, ['not', 'NOT'], 0, sub { ! $_[0] } ], - [2, 45, ['and', 'AND'], 0, undef ], - [2, 40, ['or', 'OR'], 0, undef ], - [0, 0, ['hash'], 0, sub { return {@_}; } ], - [0, 0, ['array'], 0, sub { return [@_] } ], + $OPERATORS = [ + # type precedence symbols action (undef means play_operator will handle) + ['prefix', 99, ['\\'], undef ], + ['postfix', 98, ['++'], undef ], + ['postfix', 98, ['--'], undef ], + ['prefix', 97, ['++'], undef ], + ['prefix', 97, ['--'], undef ], + ['right', 96, ['**', 'pow'], sub { $_[0] ** $_[1] } ], + ['prefix', 93, ['!'], sub { ! $_[0] } ], + ['prefix', 93, ['-'], sub { @_ == 1 ? 0 - $_[0] : $_[0] - $_[1] } ], + ['left', 90, ['*'], sub { $_[0] * $_[1] } ], + ['left', 90, ['/'], sub { $_[0] / $_[1] } ], + ['left', 90, ['div', 'DIV'], sub { int($_[0] / $_[1]) } ], + ['left', 90, ['%', 'mod', 'MOD'], sub { $_[0] % $_[1] } ], + ['left', 85, ['+'], sub { $_[0] + $_[1] } ], + ['left', 85, ['-'], sub { @_ == 1 ? 0 - $_[0] : $_[0] - $_[1] } ], + ['left', 85, ['~', '_'], undef ], + ['none', 80, ['<'], sub { $_[0] < $_[1] } ], + ['none', 80, ['>'], sub { $_[0] > $_[1] } ], + ['none', 80, ['<='], sub { $_[0] <= $_[1] } ], + ['none', 80, ['>='], sub { $_[0] >= $_[1] } ], + ['none', 80, ['lt'], sub { $_[0] lt $_[1] } ], + ['none', 80, ['gt'], sub { $_[0] gt $_[1] } ], + ['none', 80, ['le'], sub { $_[0] le $_[1] } ], + ['none', 80, ['ge'], sub { $_[0] ge $_[1] } ], + ['none', 75, ['==', 'eq'], sub { $_[0] eq $_[1] } ], + ['none', 75, ['!=', 'ne'], sub { $_[0] ne $_[1] } ], + ['left', 70, ['&&'], undef ], + ['right', 65, ['||'], undef ], + ['none', 60, ['..'], sub { $_[0] .. $_[1] } ], + ['ternary', 55, ['?', ':'], undef ], + ['assign', 53, ['+='], sub { $_[0] + $_[1] } ], + ['assign', 53, ['-='], sub { $_[0] - $_[1] } ], + ['assign', 53, ['*='], sub { $_[0] * $_[1] } ], + ['assign', 53, ['/='], sub { $_[0] / $_[1] } ], + ['assign', 53, ['%='], sub { $_[0] % $_[1] } ], + ['assign', 53, ['**='], sub { $_[0] ** $_[1] } ], + ['assign', 53, ['~=', '_='], sub { $_[0] . $_[1] } ], + ['assign', 52, ['='], undef ], + ['prefix', 50, ['not', 'NOT'], sub { ! $_[0] } ], + ['left', 45, ['and', 'AND'], undef ], + ['right', 40, ['or', 'OR'], undef ], +# ['', 0, ['{}'], undef ], +# ['', 0, ['[]'], undef ], +# ['', 0, ['qr'], undef ], ]; - $OP_DISPATCH ||= {map {my $ref = $_; map {$_ => $ref->[4]} @{$ref->[2]}} @$OPERATORS}; - $OP_UNARY ||= {map {my $ref = $_; map {$_ => $ref} @{$ref->[2]}} grep {$_->[0] == 1} @$OPERATORS}; - $OP_BINARY ||= {map {my $ref = $_; map {$_ => $ref} @{$ref->[2]}} grep {$_->[0] == 2} @$OPERATORS}; - $OP_TRINARY ||= {map {my $ref = $_; map {$_ => $ref} @{$ref->[2]}} grep {$_->[0] == 3} @$OPERATORS}; + $OP = {map {my $ref = $_; map {$_ => $ref} @{$ref->[2]}} grep {$_->[0] ne 'prefix' } @$OPERATORS}; # all non-prefix + $OP_PREFIX = {map {my $ref = $_; map {$_ => $ref} @{$ref->[2]}} grep {$_->[0] eq 'prefix' } @$OPERATORS}; + $OP_DISPATCH = {map {my $ref = $_; map {$_ => $ref->[3]} @{$ref->[2]}} grep {$_->[3] } @$OPERATORS}; + $OP_ASSIGN = {map {my $ref = $_; map {$_ => 1} @{$ref->[2]}} grep {$_->[0] eq 'assign' } @$OPERATORS}; + $OP_POSTFIX = {map {my $ref = $_; map {$_ => 1} @{$ref->[2]}} grep {$_->[0] eq 'postfix'} @$OPERATORS}; # bool is postfix + $OP_TERNARY = {map {my $ref = $_; map {$_ => 1} @{$ref->[2]}} grep {$_->[0] eq 'ternary'} @$OPERATORS}; # bool is ternary sub _op_qr { # no mixed \w\W operators my %used; - my $chrs = join '|', map {quotemeta $_} grep {++$used{$_} < 2} grep {/^\W{2,}$/} @_; - my $chr = join '', map {quotemeta $_} grep {++$used{$_} < 2} grep {/^\W$/} @_; - my $word = join '|', grep {++$used{$_} < 2} grep {/^\w+$/} @_; + my $chrs = join '|', reverse sort map {quotemeta $_} grep {++$used{$_} < 2} grep {! /\{\}|\[\]/} grep {/^\W{2,}$/} @_; + my $chr = join '', sort map {quotemeta $_} grep {++$used{$_} < 2} grep {/^\W$/} @_; + my $word = join '|', reverse sort grep {++$used{$_} < 2} grep {/^\w+$/} @_; $chr = "[$chr]" if $chr; $word = "\\b(?:$word)\\b" if $word; return join('|', grep {length} $chrs, $chr, $word) || die "Missing operator regex"; } - sub _build_op_qr { _op_qr(sort map {@{ $_->[2] }} grep {$_->[0] > 1 && ! $_->[3]} @$OPERATORS) } # all binary, trinary, non-parened ops - sub _build_op_qr_unary { _op_qr(sort map {@{ $_->[2] }} grep {$_->[0] == 1 } @$OPERATORS) } # unary operators - sub _build_op_qr_paren { _op_qr(sort map {@{ $_->[2] }} grep { $_->[3]} @$OPERATORS) } # paren - $QR_OP ||= _build_op_qr(); - $QR_OP_UNARY ||= _build_op_qr_unary(); - $QR_OP_PARENED ||= _build_op_qr_paren(); - + sub _build_op_qr { _op_qr(map {@{ $_->[2] }} grep {$_->[0] ne 'prefix'} @$OPERATORS) } + sub _build_op_qr_prefix { _op_qr(map {@{ $_->[2] }} grep {$_->[0] eq 'prefix'} @$OPERATORS) } + sub _build_op_qr_assign { _op_qr(map {@{ $_->[2] }} grep {$_->[0] eq 'assign'} @$OPERATORS) } + $QR_OP = _build_op_qr(); + $QR_OP_PREFIX = _build_op_qr_prefix(); + $QR_OP_ASSIGN = _build_op_qr_assign(); + $QR_DIRECTIVE = '( [a-zA-Z]+\b | \| )'; $QR_COMMENTS = '(?-s: \# .* \s*)*'; - $QR_FILENAME = '([a-zA-Z]]:/|/)? [\w\-\.]+ (?:/[\w\-\.]+)*'; - $QR_AQ_NOTDOT = "(?! \\s* $QR_COMMENTS \\.)"; - $QR_AQ_SPACE = '(?: \\s+ | \$ | (?=[;+]) )'; # the + comes into play on filenames - $QR_PRIVATE ||= qr/^_/; + $QR_FILENAME = '([a-zA-Z]]:/|/)? [\w\.][\w\-\.]* (?:/[\w\-\.]+)*'; + $QR_NUM = '(?:\d*\.\d+ | \d+) (?: [eE][+-]\d+ )?'; + $QR_AQ_SPACE = '(?: \\s+ | \$ | (?=;) )'; + $QR_PRIVATE = qr/^[_.]/; + + $WHILE_MAX = 1000; + $EXTRA_COMPILE_EXT = '.sto2'; - $WHILE_MAX ||= 1000; - $EXTRA_COMPILE_EXT = '.sto'; + @CONFIG_COMPILETIME = qw(ANYCASE INTERPOLATE PRE_CHOMP POST_CHOMP V1DOLLAR V2PIPE); + @CONFIG_RUNTIME = qw(DUMP); + + eval {require Scalar::Util}; }; ###----------------------------------------------------------------### @@ -244,6 +308,12 @@ BEGIN { sub new { my $class = shift; my $args = ref($_[0]) ? { %{ shift() } } : {@_}; + + ### allow for lowercase args + if (my @keys = grep {/^[a-z][a-z_]+$/} keys %$args) { + @{ $args }{ map { uc $_ } @keys } = delete @{ $args }{ @keys }; + } + my $self = bless $args, $class; ### "enable" debugging - we only support DEBUG_DIRS and DEBUG_UNDEF @@ -268,8 +338,14 @@ sub _process { ### parse and execute my $doc; eval { + ### handed us a precompiled document + if (ref($file) eq 'HASH' && $file->{'_tree'}) { + $doc = $file; + ### load the document - $doc = $self->load_parsed_tree($file) || $self->throw('undef', "Zero length content");; + } else { + $doc = $self->load_parsed_tree($file) || $self->throw('undef', "Zero length content");; + } ### prevent recursion $self->throw('file', "recursion into '$doc->{name}'") @@ -280,18 +356,18 @@ sub _process { if (! @{ $doc->{'_tree'} }) { # no tags found - just return the content $$out_ref = ${ $doc->{'_content'} }; } else { - local $self->{'_vars'}->{'component'} = $doc; - $self->{'_vars'}->{'template'} = $doc if $self->{'_top_level'}; + local $self->{'_component'} = $doc; + local $self->{'_template'} = $self->{'_top_level'} ? $doc : $self->{'_template'}; + local @{ $self }{@CONFIG_RUNTIME} = @{ $self }{@CONFIG_RUNTIME}; $self->execute_tree($doc->{'_tree'}, $out_ref); - delete $self->{'_vars'}->{'template'} if $self->{'_top_level'}; } - }; - ### trim whitespace from the beginning and the end of a block or template - if ($self->{'TRIM'}) { - substr($$out_ref, $i, length($$out_ref) - $i) =~ s{ \s+ $ }{}x; # tail first - substr($$out_ref, $i, length($$out_ref) - $i) =~ s{ ^ \s+ }{}x; - } + ### trim whitespace from the beginning and the end of a block or template + if ($self->{'TRIM'}) { + substr($$out_ref, $i, length($$out_ref) - $i) =~ s{ \s+ $ }{}x; # tail first + substr($$out_ref, $i, length($$out_ref) - $i) =~ s{ ^ \s+ }{}x; + } + }; ### handle exceptions if (my $err = $@) { @@ -314,9 +390,9 @@ sub load_parsed_tree { ### looks like a string reference if (ref $file) { - $doc->{'_content'} = $file; - $doc->{'name'} = 'input text'; - $doc->{'is_str_ref'} = 1; + $doc->{'_content'} = $file; + $doc->{'name'} = 'input text'; + $doc->{'_is_str_ref'} = 1; ### looks like a previously cached-in-memory document } elsif ($self->{'_documents'}->{$file} @@ -360,7 +436,7 @@ sub load_parsed_tree { my $_tree = $ref->{'_tree'}; foreach my $node (@$_tree) { next if ! ref $node; - next if $node->[0] eq 'METADEF'; + next if $node->[0] eq 'META'; last if $node->[0] ne 'BLOCK'; next if $block_name ne $node->[3]; $doc->{'_content'} = $ref->{'_content'}; @@ -413,12 +489,13 @@ sub load_parsed_tree { $self->{'NAMESPACE'}->{$key} ||= $self->{'CONSTANTS'}; } - local $self->{'_vars'}->{'component'} = $doc; - $doc->{'_tree'} = $self->parse_tree($doc->{'_content'}); # errors die + local $self->{'_component'} = $doc; + $doc->{'_tree'} = eval { $self->parse_tree($doc->{'_content'}) } + || do { my $e = $@; $e->doc($doc) if UNIVERSAL::can($e, 'doc') && ! $e->doc; die $e }; # errors die } ### cache parsed_tree in memory unless asked not to do so - if (! $doc->{'is_str_ref'} && (! defined($self->{'CACHE_SIZE'}) || $self->{'CACHE_SIZE'})) { + if (! $doc->{'_is_str_ref'} && (! defined($self->{'CACHE_SIZE'}) || $self->{'CACHE_SIZE'})) { $self->{'_documents'}->{$file} ||= $doc; $doc->{'_cache_time'} = time; @@ -450,6 +527,8 @@ sub load_parsed_tree { return $doc; } +###----------------------------------------------------------------### + sub parse_tree { my $self = shift; my $str_ref = shift; @@ -460,39 +539,47 @@ sub parse_tree { my $STYLE = $self->{'TAG_STYLE'} || 'default'; my $START = $self->{'START_TAG'} || $TAGS->{$STYLE}->[0]; my $END = $self->{'END_TAG'} || $TAGS->{$STYLE}->[1]; - my $len_s = length $START; - my $len_e = length $END; + local $self->{'_end_tag'} = $END; + + local @{ $self }{@CONFIG_COMPILETIME} = @{ $self }{@CONFIG_COMPILETIME}; my @tree; # the parsed tree my $pointer = \@tree; # pointer to current tree to handle nested blocks my @state; # maintain block levels local $self->{'_state'} = \@state; # allow for items to introspect (usually BLOCKS) local $self->{'_in_perl'}; # no interpolation in perl + my @in_view; # let us know if we are in a view my @move_to_front; # items that need to be declared first (usually BLOCKS) - my @meta; # place to store any found meta information (to go into METADEF) - my $i = 0; # start index - my $j = 0; # end index - my $last = 0; # previous end index + my @meta; # place to store any found meta information (to go into META) my $post_chomp = 0; # previous post_chomp setting - my $continue; # multiple directives in the same tag + my $continue = 0; # flag for multiple directives in the same tag my $post_op; # found a post-operative DIRECTIVE my $capture; # flag to start capture my $func; my $node; - my $tag; + my $mark; + local pos $$str_ref = 0; + while (1) { ### continue looking for information in a semi-colon delimited tag if ($continue) { - $i = $continue; - $node = [undef, $i, $j]; + $node = [undef, pos($$str_ref), undef]; ### look through the string using index } else { - $i = index($$str_ref, $START, $last); - last if $i == -1; # no start tag found - we are done - if ($last != $i) { # found a text portion - chomp it, interpolate it and store it - my $text = substr($$str_ref, $last, $i - $last); - my $_last = $last; + $$str_ref =~ m{ \G (.*?) $START }gcxs + || last; + + ### found a text portion - chomp it, interpolate it and store it + if (length $1) { + my $text = $1; + + if ($text =~ m{ ($END) }xs) { + my $char = pos($$str_ref) + $-[1] + 1; + $self->throw('parse', "Found unmatched closing tag \"$1\"", undef, $char); + } + + my $_last = pos $$str_ref; if ($post_chomp) { if ($post_chomp == 1) { $_last += length($1) if $text =~ s{ ^ ([^\S\n]* \n) }{}x } elsif ($post_chomp == 2) { $_last += length($1) + 1 if $text =~ s{ ^ (\s+) }{ }x } @@ -503,45 +590,41 @@ sub parse_tree { $self->interpolate_node($pointer, $_last) if $self->{'INTERPOLATE'}; } } - $j = index($$str_ref, $END, $i + $len_s); - $last = $j + $len_e; - if ($j == -1) { # missing closing tag - $last = length($$str_ref); - last; - } - $tag = substr($$str_ref, $i + $len_s, $j - ($i + $len_s)); - $node = [undef, $i + $len_s, $j]; + + $node = [undef, pos($$str_ref), undef]; ### take care of whitespace and comments flags - my $pre_chomp = $tag =~ s{ ^ ([+=~-]) }{}x ? $1 : $self->{'PRE_CHOMP'}; - $post_chomp = $tag =~ s{ ([+=~-]) $ }{}x ? $1 : $self->{'POST_CHOMP'}; + my $pre_chomp = $$str_ref =~ m{ \G ([+=~-]) }gcx ? $1 : $self->{'PRE_CHOMP'}; $pre_chomp =~ y/-=~+/1230/ if $pre_chomp; - $post_chomp =~ y/-=~+/1230/ if $post_chomp; if ($pre_chomp && $pointer->[-1] && ! ref $pointer->[-1]) { if ($pre_chomp == 1) { $pointer->[-1] =~ s{ (?:\n|^) [^\S\n]* \z }{}x } elsif ($pre_chomp == 2) { $pointer->[-1] =~ s{ (\s+) \z }{ }x } elsif ($pre_chomp == 3) { $pointer->[-1] =~ s{ (\s+) \z }{}x } splice(@$pointer, -1, 1, ()) if ! length $pointer->[-1]; # remove the node if it is zero length } - if ($tag =~ /^\#/) { # leading # means to comment the entire section + if ($$str_ref =~ m{ \G \# }gcx) { # leading # means to comment the entire section + $$str_ref =~ m{ \G (.*?) ([+~=-]?) ($END) }gcxs # brute force - can't comment tags with nested %] + || $self->throw('parse', "Missing closing tag", undef, pos($$str_ref)); $node->[0] = '#'; + $node->[2] = pos($$str_ref) - length($3); push @$pointer, $node; + + $post_chomp = $2; + $post_chomp ||= $self->{'POST_CHOMP'}; + $post_chomp =~ y/-=~+/1230/ if $post_chomp; next; } - $tag =~ s{ ^ \s+ $QR_COMMENTS }{}ox; - } - - if (! length $tag) { - undef $continue; - undef $post_op; - next; + $$str_ref =~ m{ \G \s* $QR_COMMENTS }gcxo; } ### look for DIRECTIVES - if ($tag =~ $QR_DIRECTIVE # find a word - && $DIRECTIVES->{$1} ) { # is it a directive - $node->[0] = $func = $1; - $tag =~ s{ ^ (\w+ | \|) \s* $QR_COMMENTS }{}ox; + if ($$str_ref =~ m{ \G $QR_DIRECTIVE }gcxo # find a word + && ($func = $self->{'ANYCASE'} ? uc($1) : $1) + && ($DIRECTIVES->{$func} + || ((pos($$str_ref) -= length $1) && 0)) + ) { # is it a directive + $node->[0] = $func; + $$str_ref =~ m{ \G \s* $QR_COMMENTS }gcx; ### store out this current node level if ($post_op) { # on a post operator - replace the original node with the new one - store the old in the new @@ -558,7 +641,7 @@ sub parse_tree { ### anything that behaves as a block ending if ($func eq 'END' || $DIRECTIVES->{$func}->[4]) { # [4] means it is a continuation block (ELSE, CATCH, etc) if (! @state) { - $self->throw('parse', "Found an $func tag while not in a block", $node); + $self->throw('parse', "Found an $func tag while not in a block", $node, pos($$str_ref)); } my $parent_node = pop @state; @@ -567,7 +650,7 @@ sub parse_tree { $parent_node->[5] = $node; my $parent_type = $parent_node->[0]; if (! $DIRECTIVES->{$func}->[4]->{$parent_type}) { - $self->throw('parse', "Found unmatched nested block", $node, 0); + $self->throw('parse', "Found unmatched nested block", $node, pos($$str_ref)); } } @@ -577,17 +660,26 @@ sub parse_tree { ### normal end block if ($func eq 'END') { if ($DIRECTIVES->{$parent_node->[0]}->[5]) { # move things like BLOCKS to front - push @move_to_front, $parent_node; + if ($parent_node->[0] eq 'BLOCK' + && defined($parent_node->[3]) + && @in_view) { + push @{ $in_view[-1] }, $parent_node; + } else { + push @move_to_front, $parent_node; + } if ($pointer->[-1] && ! $pointer->[-1]->[6]) { # capturing doesn't remove the var splice(@$pointer, -1, 1, ()); } } elsif ($parent_node->[0] =~ /PERL$/) { delete $self->{'_in_perl'}; + } elsif ($parent_node->[0] eq 'VIEW') { + my $ref = { map {($_->[3] => $_->[4])} @{ pop @in_view }}; + unshift @{ $parent_node->[3] }, $ref; } ### continuation block - such as an elsif } else { - $node->[3] = eval { $DIRECTIVES->{$func}->[0]->($self, \$tag, $node) }; + $node->[3] = eval { $DIRECTIVES->{$func}->[0]->($self, $str_ref, $node) }; if (my $err = $@) { $err->node($node) if UNIVERSAL::can($err, 'node') && ! $err->node; die $err; @@ -597,26 +689,52 @@ sub parse_tree { } } elsif ($func eq 'TAGS') { - if ($tag =~ / ^ (\w+) /x && $TAGS->{$1}) { - $tag =~ s{ ^ (\w+) \s* $QR_COMMENTS }{}ox; - ($START, $END) = @{ $TAGS->{$1} }; - } elsif ($tag =~ s{ ^ (\S+) \s+ (\S+) \s* $QR_COMMENTS }{}ox) { - ($START, $END) = ($1, $2); + my $end; + if ($$str_ref =~ m{ \G (\w+) \s* $QR_COMMENTS }gcxs) { + my $ref = $TAGS->{lc $1} || $self->throw('parse', "Invalid TAGS name \"$1\"", undef, pos($$str_ref)); + ($START, $END) = @$ref; + + } else { + local $self->{'_operator_precedence'} = 1; # prevent operator matching + $START = $$str_ref =~ m{ \G (?= [\'\"\/]) }gcx + ? $self->parse_expr($str_ref) + : $self->parse_expr($str_ref, {auto_quote => "(\\S+) \\s+ $QR_COMMENTS"}) + || $self->throw('parse', "Invalid opening tag in TAGS", undef, pos($$str_ref)); + $END = $$str_ref =~ m{ \G (?= [\'\"\/]) }gcx + ? $self->parse_expr($str_ref) + : $self->parse_expr($str_ref, {auto_quote => "(\\S+) \\s* $QR_COMMENTS"}) + || $self->throw('parse', "Invalid closing tag in TAGS", undef, pos($$str_ref)); + for my $tag ($START, $END) { + $tag = $self->play_expr($tag); + $tag = quotemeta($tag) if ! ref $tag; + } } - $len_s = length $START; - $len_e = length $END; + + $node->[2] = pos $$str_ref; + + ### allow for one more closing tag of the old style + if ($$str_ref =~ m{ \G ([+~=-]?) $self->{'_end_tag'} }gcxs) { + $post_chomp = $1 || $self->{'POST_CHOMP'}; + $post_chomp =~ y/-=~+/1230/ if $post_chomp; + $continue = 0; + $post_op = undef; + $self->{'_end_tag'} = $END; # need to keep track so parse_expr knows when to stop + next; + } + + $self->{'_end_tag'} = $END; } elsif ($func eq 'META') { - my $args = $self->parse_args(\$tag); + my $args = $self->parse_args($str_ref, {named_at_front => 1}); my $hash; - if (($hash = $self->vivify_args($args)->[-1]) + if (($hash = $self->play_expr($args->[0])) && UNIVERSAL::isa($hash, 'HASH')) { unshift @meta, %$hash; # first defined win } ### all other "normal" tags } else { - $node->[3] = eval { $DIRECTIVES->{$func}->[0]->($self, \$tag, $node) }; + $node->[3] = eval { $DIRECTIVES->{$func}->[0]->($self, $str_ref, $node) }; if (my $err = $@) { $err->node($node) if UNIVERSAL::can($err, 'node') && ! $err->node; die $err; @@ -625,14 +743,15 @@ sub parse_tree { push @state, $node; $pointer = $node->[4] ||= []; } + push @in_view, [] if $func eq 'VIEW'; } ### allow for bare variable getting and setting - } elsif (defined(my $var = $self->parse_variable(\$tag))) { + } elsif (defined(my $var = $self->parse_expr($str_ref))) { push @$pointer, $node; - if ($tag =~ s{ ^ = >? \s* $QR_COMMENTS }{}ox) { + if ($$str_ref =~ m{ \G ($QR_OP_ASSIGN) >? (?! [+=~-]? $END) \s* $QR_COMMENTS }gcx) { $node->[0] = 'SET'; - $node->[3] = eval { $DIRECTIVES->{'SET'}->[0]->($self, \$tag, $node, $var) }; + $node->[3] = eval { $DIRECTIVES->{'SET'}->[0]->($self, $str_ref, $node, $1, $var) }; if (my $err = $@) { $err->node($node) if UNIVERSAL::can($err, 'node') && ! $err->node; die $err; @@ -642,11 +761,18 @@ sub parse_tree { $node->[3] = $var; } + ### handle empty tags [% %] + } elsif ($$str_ref =~ m{ \G (?: ; \s* $QR_COMMENTS)? ([+=~-]?) ($END) }gcxs) { + my $end = $2; + $post_chomp = $1 || $self->{'POST_CHOMP'}; + $post_chomp =~ y/-=~+/1230/ if $post_chomp; + $node->[2] = pos($$str_ref) - length($end); + $continue = 0; + $post_op = undef; + next; + } else { # error - my $all = substr($$str_ref, $i + $len_s, $j - ($i + $len_s)); - $all =~ s/^\s+//; - $all =~ s/\s+$//; - $self->throw('parse', "Not sure how to handle tag \"$all\"", $node); + $self->throw('parse', "Not sure how to handle tag", $node, pos($$str_ref)); } ### we now have the directive to capture for an item like "SET foo = BLOCK" - store it @@ -656,31 +782,46 @@ sub parse_tree { undef $capture; } + ### look for the closing tag again + if ($$str_ref =~ m{ \G (?: ; \s* $QR_COMMENTS)? ([+=~-]?) ($END) }gcxs) { + my $end = $2; + $post_chomp = $1 || $self->{'POST_CHOMP'}; + $post_chomp =~ y/-=~+/1230/ if $post_chomp; + + $node->[2] = pos($$str_ref) - length($end); + $continue = 0; + $post_op = undef; + next; + } + + ### we always continue - and always record our position now + $continue = 1; + $node->[2] = pos $$str_ref; + ### we are flagged to start capturing the output of the next directive - set it up if ($node->[6]) { - $continue = $j - length $tag; - $node->[2] = $continue; $post_op = undef; $capture = $node; ### semi-colon = end of statement - we will need to continue parsing this tag - } elsif ($tag =~ s{ ^ ; \s* $QR_COMMENTS }{}ox) { - $continue = $j - length $tag; - $node->[2] = $continue; + } elsif ($$str_ref =~ m{ \G ; \s* $QR_COMMENTS }gcxo) { $post_op = undef; - ### looking at a post operator ([% u FOREACH u IN [1..3] %]) - } elsif ($tag =~ $QR_DIRECTIVE # find a word - && $DIRECTIVES->{$1} # is it a directive - && $DIRECTIVES->{$1}->[3]) { # it is a post operative directive - $continue = $j - length $tag; - $node->[2] = $continue; - $post_op = $node; + } else { + ### looking at a post operator ([% u FOREACH u IN [1..3] %]) + $mark = pos $$str_ref; + if ($$str_ref =~ m{ \G $QR_DIRECTIVE }gcxo # find a word without advancing position + && ($func = $self->{'ANYCASE'} ? uc($1) : $1) + && (($DIRECTIVES->{$func} # and its a directive + && $DIRECTIVES->{$func}->[3]) # that can be post operative + || ((pos($$str_ref) = $mark) && 0)) # otherwise rollback + ) { + $post_op = $node; # store flag so next loop puts items in this node + pos($$str_ref) = $mark; - } else { # error - $self->throw('parse', "Found trailing info \"$tag\"", $node) if length $tag; - $continue = undef; - $post_op = undef; + } else { + $post_op = undef; + } } } @@ -688,7 +829,7 @@ sub parse_tree { unshift @tree, @move_to_front; } if (@meta) { - unshift @tree, ['METADEF', 0, 0, {@meta}]; + unshift @tree, ['META', 0, 0, {@meta}]; } if ($#state > -1) { @@ -696,9 +837,15 @@ sub parse_tree { } ### pull off the last text portion - if any - if ($last != length($$str_ref)) { - my $text = substr($$str_ref, $last, length($$str_ref) - $last); - my $_last = $last; + if (pos($$str_ref) != length($$str_ref)) { + my $text = substr $$str_ref, pos($$str_ref); + + if ($text =~ m{ ($END) }xs) { + my $char = pos($$str_ref) + $-[1] + 1; + $self->throw('parse', "Found unmatched closing tag \"$1\"", undef, $char); + } + + my $_last = pos($$str_ref); if ($post_chomp) { if ($post_chomp == 1) { $_last += length($1) if $text =~ s{ ^ ([^\S\n]* \n) }{}x } elsif ($post_chomp == 2) { $_last += length($1) + 1 if $text =~ s{ ^ (\s+) }{ }x } @@ -713,102 +860,125 @@ sub parse_tree { return \@tree; } -sub execute_tree { - my ($self, $tree, $out_ref) = @_; - - # node contains (0: DIRECTIVE, - # 1: start_index, - # 2: end_index, - # 3: parsed tag details, - # 4: sub tree for block types - # 5: continuation sub trees for sub continuation block types (elsif, else, etc) - # 6: flag to capture next directive - for my $node (@$tree) { - ### text nodes are just the bare text - if (! ref $node) { - warn "NODE: TEXT\n" if trace; - $$out_ref .= $node if defined $node; - next; - } - - warn "NODE: $node->[0] (char $node->[1])\n" if trace; - $$out_ref .= $self->debug_node($node) if $self->{'_debug_dirs'} && ! $self->{'_debug_off'}; - - my $val = $DIRECTIVES->{$node->[0]}->[1]->($self, $node->[3], $node, $out_ref); - $$out_ref .= $val if defined $val; - } -} - -###----------------------------------------------------------------### - -sub parse_variable { +sub parse_expr { my $self = shift; my $str_ref = shift; my $ARGS = shift || {}; + my $is_aq = $ARGS->{'auto_quote'} ? 1 : 0; + my $mark = pos $$str_ref; ### allow for custom auto_quoting (such as hash constructors) - if ($ARGS->{'auto_quote'}) { - if ($$str_ref =~ $ARGS->{'auto_quote'}) { - my $str = $1; - substr($$str_ref, 0, length($str), ''); - $$str_ref =~ s{ ^ \s* $QR_COMMENTS }{}ox; - return $str; - ### allow for auto-quoted $foo or ${foo.bar} type constructs - } elsif ($$str_ref =~ s{ ^ \$ (\w+ (?:\.\w+)*) \b \s* $QR_COMMENTS }{}ox - || $$str_ref =~ s{ ^ \$\{ \s* ([^\}]+) \} \s* $QR_COMMENTS }{}ox) { + if ($is_aq) { + if ($$str_ref =~ m{ \G $ARGS->{'auto_quote'} }gcx) { + return $1; + + ### allow for auto-quoted $foo + } elsif ($$str_ref =~ m{ \G \$ (\w+\b (?:\.\w+\b)*) \s* $QR_COMMENTS }gcxo) { my $name = $1; - return $self->parse_variable(\$name); + if ($$str_ref !~ m{ \G \( }gcx || $name =~ /^(?:qw|m|\d)/) { + return $self->parse_expr(\$name); + } + ### this is a little cryptic/odd - but TT allows items in + ### autoquote position to only be prefixed by a $ - gross + ### so we will defer to the regular parsing - but after the $ + pos($$str_ref) = $mark + 1; + $is_aq = undef; # but don't allow operators - false flag handed down + + ### allow for ${foo.bar} type constructs + } elsif ($$str_ref =~ m{ \G \$\{ \s* }gcx) { + my $var = $self->parse_expr($str_ref); + $$str_ref =~ m{ \G \s* \} \s* $QR_COMMENTS }gcxo + || $self->throw('parse', 'Missing close "}" from "${"', undef, pos($$str_ref)); + return $var; } } - my $copy = $$str_ref; # copy while parsing to allow for errors - ### test for leading unary operators - my $has_unary; - if ($copy =~ s{ ^ ($QR_OP_UNARY) \s* $QR_COMMENTS }{}ox) { - return if $ARGS->{'auto_quote'}; # auto_quoted thing was too complicated - $has_unary = $1; + ### test for leading prefix operators + my $has_prefix; + while (! $is_aq && $$str_ref =~ m{ \G ($QR_OP_PREFIX) }gcxo) { + push @{ $has_prefix }, $1; + $$str_ref =~ m{ \G \s* $QR_COMMENTS }gcxo; } my @var; my $is_literal; my $is_namespace; + my $already_parsed_args; + + ### allow hex + if ($$str_ref =~ m{ \G 0x ( [a-fA-F0-9]+ ) \s* $QR_COMMENTS }gcxo) { + my $number = eval { hex $1 } || 0; + push @var, \ $number; + $is_literal = 1; ### allow for numbers - if ($copy =~ s{ ^ ( (?:\d*\.\d+ | \d+) ) \s* $QR_COMMENTS }{}ox) { + } elsif ($$str_ref =~ m{ \G ( $QR_NUM ) \s* $QR_COMMENTS }gcxo) { my $number = $1; push @var, \ $number; $is_literal = 1; + ### allow for quoted array constructor + } elsif (! $is_aq && $$str_ref =~ m{ \G qw ([^\w\s]) \s* }gcxo) { + my $quote = $1; + $quote =~ y|([{<|)]}>|; + $$str_ref =~ m{ \G (.*?) (?throw('parse.missing.array_close', "Missing close \"$quote\"", undef, pos($$str_ref)); + my $str = $1; + $str =~ s{ ^ \s+ }{}x; + $str =~ s{ \s+ $ }{}x; + $str =~ s{ \\ \Q$quote\E }{$quote}gx; + push @var, [undef, '[]', split /\s+/, $str]; + + ### allow for regex constructor + } elsif (! $is_aq && $$str_ref =~ m{ \G / }gcx) { + $$str_ref =~ m{ \G (.*?) (?throw('parse', 'Unclosed regex tag "/"', undef, pos($$str_ref)); + my ($str, $opts) = ($1, $2); + $self->throw('parse', 'e option not allowed on regex', undef, pos($$str_ref)) if $opts =~ /e/; + $self->throw('parse', 'g option not supported on regex', undef, pos($$str_ref)) if $opts =~ /g/; + $str =~ s|\\n|\n|g; + $str =~ s|\\t|\t|g; + $str =~ s|\\r|\r|g; + $str =~ s|\\\/|\/|g; + $str =~ s|\\\$|\$|g; + $self->throw('parse', "Invalid regex: $@", undef, pos($$str_ref)) if ! eval { "" =~ /$str/; 1 }; + push @var, [undef, 'qr', $str, $opts]; + ### looks like a normal variable start - } elsif ($copy =~ s{ ^ (\w+) \s* $QR_COMMENTS }{}ox) { + } elsif ($$str_ref =~ m{ \G (\w+) \s* $QR_COMMENTS }gcxo) { push @var, $1; $is_namespace = 1 if $self->{'NAMESPACE'} && $self->{'NAMESPACE'}->{$1}; ### allow for literal strings - } elsif ($copy =~ s{ ^ ([\"\']) (|.*?[^\\]) \1 \s* $QR_COMMENTS }{}sox) { - if ($1 eq "'") { # no interpolation on single quoted strings - my $str = $2; + } elsif ($$str_ref =~ m{ \G ([\"\']) }gcx) { + my $quote = $1; + $$str_ref =~ m{ \G (.*?) (?throw('parse', "Unclosed quoted string ($1)", undef, pos($$str_ref)); + my $str = $1; + if ($quote eq "'") { # no interpolation on single quoted strings $str =~ s{ \\\' }{\'}xg; push @var, \ $str; $is_literal = 1; } else { - my $str = $2; $str =~ s/\\n/\n/g; $str =~ s/\\t/\t/g; $str =~ s/\\r/\r/g; - $str =~ s/\\([\"\$])/$1/g; - my @pieces = $ARGS->{'auto_quote'} - ? split(m{ (\$\w+ | \$\{ [^\}]+ \}) }x, $str) # autoquoted items get a single $\w+ - no nesting - : split(m{ (\$\w+ (?:\.\w+)* | \$\{ [^\}]+ \}) }x, $str); + $str =~ s/\\"/"/g; + my @pieces = $is_aq + ? split(m{ (?: ^ | (?parse_variable(\$name); - } + $name =~ s/\\\}/\}/g; + $piece = $self->parse_expr(\$name); + } @pieces = grep {defined && length} @pieces; if (@pieces == 1 && ! ref $pieces[0]) { push @var, \ $pieces[0]; @@ -817,97 +987,138 @@ sub parse_variable { push @var, \ ''; $is_literal = 1; } else { - push @var, \ ['~', @pieces]; + push @var, [undef, '~', @pieces]; } } - if ($ARGS->{'auto_quote'}){ - $$str_ref = $copy; + if ($is_aq) { return ${ $var[0] } if $is_literal; push @var, 0; return \@var; } - ### allow for leading $foo or ${foo.bar} type constructs - } elsif ($copy =~ s{ ^ \$ (\w+) \b \s* $QR_COMMENTS }{}ox - || $copy =~ s{ ^ \$\{ \s* ([^\}]+) \} \s* $QR_COMMENTS }{}ox) { - my $name = $1; - push @var, $self->parse_variable(\$name); + ### allow for leading $foo type constructs + } elsif ($$str_ref =~ m{ \G \$ (\w+) \b \s* $QR_COMMENTS }gcxo) { + if ($self->{'V1DOLLAR'}) { + push @var, $1; + $is_namespace = 1 if $self->{'NAMESPACE'} && $self->{'NAMESPACE'}->{$1}; + } else { + push @var, [$1, 0]; + } + + ### allow for ${foo.bar} type constructs + } elsif ($$str_ref =~ m{ \G \$\{ \s* }gcx) { + push @var, $self->parse_expr($str_ref); + $$str_ref =~ m{ \G \s* \} \s* $QR_COMMENTS }gcxo + || $self->throw('parse', 'Missing close "}" from "${"', undef, pos($$str_ref)); ### looks like an array constructor - } elsif ($copy =~ s{ ^ \[ \s* $QR_COMMENTS }{}ox) { + } elsif (! $is_aq && $$str_ref =~ m{ \G \[ \s* $QR_COMMENTS }gcxo) { local $self->{'_operator_precedence'} = 0; # reset presedence - my $arrayref = ['array']; - while (defined(my $var = $self->parse_variable(\$copy))) { + my $arrayref = [undef, '[]']; + while (defined(my $var = $self->parse_expr($str_ref))) { push @$arrayref, $var; - $copy =~ s{ ^ , \s* $QR_COMMENTS }{}ox; + $$str_ref =~ m{ \G , \s* $QR_COMMENTS }gcxo; } - $copy =~ s{ ^ \] \s* $QR_COMMENTS }{}ox - || $self->throw('parse.missing.square_bracket', "Missing close \]", undef, length($$str_ref) - length($copy)); - push @var, \ $arrayref; + $$str_ref =~ m{ \G \] \s* $QR_COMMENTS }gcxo + || $self->throw('parse.missing.square_bracket', "Missing close \]", undef, pos($$str_ref)); + push @var, $arrayref; ### looks like a hash constructor - } elsif ($copy =~ s{ ^ \{ \s* $QR_COMMENTS }{}ox) { + } elsif (! $is_aq && $$str_ref =~ m{ \G \{ \s* $QR_COMMENTS }gcxo) { local $self->{'_operator_precedence'} = 0; # reset precedence - my $hashref = ['hash']; - while (defined(my $key = $self->parse_variable(\$copy, {auto_quote => qr{ ^ (\w+) $QR_AQ_NOTDOT }xo}))) { - $copy =~ s{ ^ = >? \s* $QR_COMMENTS }{}ox; - my $val = $self->parse_variable(\$copy); + my $hashref = [undef, '{}']; + while (defined(my $key = $self->parse_expr($str_ref, {auto_quote => "(\\w+\\b) (?! \\.) \\s* $QR_COMMENTS"}))) { + $$str_ref =~ m{ \G = >? \s* $QR_COMMENTS }gcxo; + my $val = $self->parse_expr($str_ref); push @$hashref, $key, $val; - $copy =~ s{ ^ , \s* $QR_COMMENTS }{}ox; + $$str_ref =~ m{ \G , \s* $QR_COMMENTS }gcxo; } - $copy =~ s{ ^ \} \s* $QR_COMMENTS }{}ox - || $self->throw('parse.missing.curly_bracket', "Missing close \} ($copy)", undef, length($$str_ref) - length($copy)); - push @var, \ $hashref; + $$str_ref =~ m{ \G \} \s* $QR_COMMENTS }gcxo + || $self->throw('parse.missing.curly_bracket', "Missing close \}", undef, pos($$str_ref)); + push @var, $hashref; ### looks like a paren grouper - } elsif ($copy =~ s{ ^ \( \s* $QR_COMMENTS }{}ox) { + } elsif (! $is_aq && $$str_ref =~ m{ \G \( \s* $QR_COMMENTS }gcxo) { local $self->{'_operator_precedence'} = 0; # reset precedence - my $var = $self->parse_variable(\$copy, {allow_parened_ops => 1}); - $copy =~ s{ ^ \) \s* $QR_COMMENTS }{}ox - || $self->throw('parse.missing.paren', "Missing close \)", undef, length($$str_ref) - length($copy)); - @var = @$var; - pop(@var); # pull off the trailing args of the paren group + my $var = $self->parse_expr($str_ref, {allow_parened_ops => 1}); + + $$str_ref =~ m{ \G \) \s* $QR_COMMENTS }gcxo + || $self->throw('parse.missing.paren', "Missing close \)", undef, pos($$str_ref)); + + $self->throw('parse', 'Paren group cannot be followed by an open paren', undef, pos($$str_ref)) + if $$str_ref =~ m{ \G \( }gcx; + + $already_parsed_args = 1; + if (! ref $var) { + push @var, \$var, 0; + $is_literal = 1; + } elsif (! defined $var->[0]) { + push @var, $var, 0; + } else { + push @var, @$var; + } ### nothing to find - return failure } else { + pos($$str_ref) = $mark if $is_aq || $has_prefix; return; } - return if $ARGS->{'auto_quote'}; # auto_quoted thing was too complicated + # auto_quoted thing was too complicated + if ($is_aq) { + pos($$str_ref) = $mark; + return; + } ### looks for args for the initial - if ($copy =~ s{ ^ \( \s* $QR_COMMENTS }{}ox) { + if ($already_parsed_args) { + # do nothing + } elsif ($$str_ref =~ m{ \G \( \s* $QR_COMMENTS }gcxo) { local $self->{'_operator_precedence'} = 0; # reset precedence - my $args = $self->parse_args(\$copy); - $copy =~ s{ ^ \) \s* $QR_COMMENTS }{}ox - || $self->throw('parse.missing.paren', "Missing close \)", undef, length($$str_ref) - length($copy)); + my $args = $self->parse_args($str_ref, {is_parened => 1}); + $$str_ref =~ m{ \G \) \s* $QR_COMMENTS }gcxo + || $self->throw('parse.missing.paren', "Missing close \)", undef, pos($$str_ref)); push @var, $args; } else { push @var, 0; } + ### allow for nested items - while ($copy =~ s{ ^ ( \.(?!\.) | \|(?!\|) ) \s* $QR_COMMENTS }{}ox) { + while ($$str_ref =~ m{ \G ( \.(?!\.) | \|(?!\|) ) }gcx) { + if ($1 eq '|' && $self->{'V2PIPE'}) { + pos($$str_ref) -= 1; + last; + } + push(@var, $1) if ! $ARGS->{'no_dots'}; - ### allow for interpolated variables in the middle - one.$foo.two or one.${foo.bar}.two - if ($copy =~ s{ ^ \$(\w+) \s* $QR_COMMENTS }{}ox - || $copy =~ s{ ^ \$\{ \s* ([^\}]+)\} \s* $QR_COMMENTS }{}ox) { - my $name = $1; - my $var = $self->parse_variable(\$name); - push @var, $var; - } elsif ($copy =~ s{ ^ (\w+) \s* $QR_COMMENTS }{}ox) { + $$str_ref =~ m{ \G \s* $QR_COMMENTS }gcxo; + + ### allow for interpolated variables in the middle - one.$foo.two + if ($$str_ref =~ m{ \G \$ (\w+) \b \s* $QR_COMMENTS }gcxo) { + push @var, $self->{'V1DOLLAR'} ? $1 : [$1, 0]; + + ### or one.${foo.bar}.two + } elsif ($$str_ref =~ m{ \G \$\{ \s* }gcx) { + push @var, $self->parse_expr($str_ref); + $$str_ref =~ m{ \G \s* \} \s* $QR_COMMENTS }gcxo + || $self->throw('parse', 'Missing close "}" from "${"', undef, pos($$str_ref)); + + ### allow for names + } elsif ($$str_ref =~ m{ \G (-? \w+) \s* $QR_COMMENTS }gcxo) { push @var, $1; + } else { - $self->throw('parse', "Not sure how to continue parsing on \"$copy\" ($$str_ref)"); + $self->throw('parse', "Not sure how to continue parsing", undef, pos($$str_ref)); } ### looks for args for the nested item - if ($copy =~ s{ ^ \( \s* $QR_COMMENTS }{}ox) { + if ($$str_ref =~ m{ \G \( \s* $QR_COMMENTS }gcxo) { local $self->{'_operator_precedence'} = 0; # reset precedence - my $args = $self->parse_args(\$copy); - $copy =~ s{ ^ \) \s* $QR_COMMENTS }{}ox - || $self->throw('parse.missing.paren', "Missing close \)", undef, length($$str_ref) - length($copy)); + my $args = $self->parse_args($str_ref, {is_parened => 1}); + $$str_ref =~ m{ \G \) \s* $QR_COMMENTS }gcxo + || $self->throw('parse.missing.paren', "Missing close \)", undef, pos($$str_ref)); push @var, $args; } else { push @var, 0; @@ -916,26 +1127,50 @@ sub parse_variable { } ### flatten literals and constants as much as possible - my $var = ($is_literal && $#var == 1) ? ${ $var[0] } - : $is_namespace ? $self->get_variable(\@var, {is_namespace_during_compile => 1}) - : \@var; + my $var; + if ($is_literal) { + $var = ${ $var[0] }; + if ($#var != 1) { + $var[0] = [undef, '~', $var]; + $var = \@var; + } + } elsif ($is_namespace) { + $var = $self->play_expr(\@var, {is_namespace_during_compile => 1}); + } else { + $var = \@var; + } ### allow for all "operators" - if (! $self->{'_operator_precedence'}) { + if (! $self->{'_operator_precedence'} && defined $is_aq) { my $tree; my $found; - while ($copy =~ s{ ^ ($QR_OP) \s* $QR_COMMENTS }{}ox ## look for operators - then move along - || ($ARGS->{'allow_parened_ops'} - && $copy =~ s{ ^ ($QR_OP_PARENED) \s* $QR_COMMENTS }{}ox) ) { + while (1) { + my $mark = pos $$str_ref; + if ($self->{'_end_tag'} && $$str_ref =~ m{ \G [+=~-]? $self->{'_end_tag'} }gcx) { + pos($$str_ref) = $mark; + last; + } elsif ($$str_ref !~ m{ \G ($QR_OP) }gcxo) { + pos($$str_ref) = $mark; + last; + } + if ($OP_ASSIGN->{$1} && ! $ARGS->{'allow_parened_ops'}) { # only allow assignment in parens + pos($$str_ref) = $mark; + last; + } local $self->{'_operator_precedence'} = 1; - my $op = $1; - my $var2 = $self->parse_variable(\$copy); + my $op = $1; + $$str_ref =~ m{ \G \s* $QR_COMMENTS }gcxo; - ### allow for unary operator precedence - if ($has_unary && (($OP_BINARY->{$op} || $OP_TRINARY->{$op})->[1] < $OP_UNARY->{$has_unary}->[1])) { + ### allow for postfix - doesn't check precedence - someday we might change - but not today (only affects post ++ and --) + if ($OP_POSTFIX->{$op}) { + $var = [[undef, $op, $var, 1], 0]; # cheat - give a "second value" to postfix ops + next; + + ### allow for prefix operator precedence + } elsif ($has_prefix && $OP->{$op}->[1] < $OP_PREFIX->{$has_prefix->[-1]}->[1]) { if ($tree) { if ($#$tree == 1) { # only one operator - keep simple things fast - $var = [\ [$tree->[0], $var, $tree->[1]], 0]; + $var = [[undef, $tree->[0], $var, $tree->[1]], 0]; } else { unshift @$tree, $var; $var = $self->apply_precedence($tree, $found); @@ -943,20 +1178,21 @@ sub parse_variable { undef $tree; undef $found; } - $var = [ \ [ $has_unary, $var ], 0 ]; - undef $has_unary; + $var = [[undef, $has_prefix->[-1], $var ], 0]; + if (! @$has_prefix) { undef $has_prefix } else { pop @$has_prefix } } ### add the operator to the tree + my $var2 = $self->parse_expr($str_ref, {from_here => 1}); + $self->throw('parse', 'Missing variable after "'.$op.'"', undef, pos($$str_ref)) if ! defined $var2; push (@{ $tree ||= [] }, $op, $var2); - my $ref = $OP_BINARY->{$op} || $OP_TRINARY->{$op}; - $found->{$op} = $ref->[1]; + $found->{$OP->{$op}->[1]}->{$op} = 1; # found->{precedence}->{op} } ### if we found operators - tree the nodes by operator precedence if ($tree) { if (@$tree == 2) { # only one operator - keep simple things fast - $var = [\ [$tree->[0], $var, $tree->[1]], 0]; + $var = [[undef, $tree->[0], $var, $tree->[1]], 0]; } else { unshift @$tree, $var; $var = $self->apply_precedence($tree, $found); @@ -964,12 +1200,11 @@ sub parse_variable { } } - ### allow for unary on non-chained variables - if ($has_unary) { - $var = [ \ [ $has_unary, $var ], 0 ]; + ### allow for prefix on non-chained variables + if ($has_prefix) { + $var = [[undef, $_, $var], 0] for reverse @$has_prefix; } - $$str_ref = $copy; # commit the changes return $var; } @@ -980,59 +1215,77 @@ sub apply_precedence { my @var; my $trees; ### look at the operators we found in the order we found them - for my $op (sort {$found->{$a} <=> $found->{$b}} keys %$found) { - local $found->{$op}; - delete $found->{$op}; - my @trees; - my @trinary; - - ### split the array on the current operator - for (my $i = 0; $i <= $#$tree; $i ++) { - my $is_trinary = $OP_TRINARY->{$op} && grep {$_ eq $tree->[$i]} @{ $OP_TRINARY->{$op}->[2] }; - next if $tree->[$i] ne $op && ! $is_trinary; - push @trees, [splice @$tree, 0, $i, ()]; # everything up to the operator - push @trinary, $tree->[0] if $is_trinary; - shift @$tree; # pull off the operator + for my $prec (sort keys %$found) { + my $ops = $found->{$prec}; + local $found->{$prec}; + delete $found->{$prec}; + + ### split the array on the current operators for this level + my @ops; + my @exprs; + for (my $i = 1; $i <= $#$tree; $i += 2) { + next if ! $ops->{ $tree->[$i] }; + push @ops, $tree->[$i]; + push @exprs, [splice @$tree, 0, $i, ()]; + shift @$tree; $i = -1; } - next if ! @trees; # this iteration didn't have the current operator - push @trees, $tree if scalar @$tree; # elements after last operator + next if ! @exprs; # this iteration didn't have the current operator + push @exprs, $tree if scalar @$tree; # add on any remaining items - ### now - for this level split on remaining operators, or add the variable to the tree - for my $node (@trees) { + ### simplify sub expressions + for my $node (@exprs) { if (@$node == 1) { $node = $node->[0]; # single item - its not a tree } elsif (@$node == 3) { - $node = [ \ [ $node->[1], $node->[0], $node->[2] ], 0 ]; # single operator - put it straight on + $node = [[undef, $node->[1], $node->[0], $node->[2]], 0]; # single operator - put it straight on } else { $node = $self->apply_precedence($node, $found); # more complicated - recurse } } - ### return binary - if ($OP_BINARY->{$op}) { - my $val = $trees[-1]; - $val = [ \ [ $op, $trees[$_], $val ], 0 ] for reverse (0 .. $#trees - 1); # reverse order - helps out || - return $val; - } + ### assemble this current level - ### return simple trinary - if (@trinary == 2) { - return [ \ [ $op, @trees ], 0 ]; - } + ### some rules: + # 1) items at the same precedence level must all be either right or left or ternary associative + # 2) ternary items cannot share precedence with anybody else. + # 3) there really shouldn't be another operator at the same level as a postfix + my $type = $OP->{$ops[0]}->[0]; + + if ($type eq 'ternary') { + my $op = $OP->{$ops[0]}->[2]->[0]; # use the first op as what we are using - ### reorder complex trinary - rare case - while ($#trinary >= 1) { - ### if we look starting from the back - the first lead trinary op will always be next to its matching op - for (my $i = $#trinary; $i >= 0; $i --) { - next if $OP_TRINARY->{$trinary[$i]}->[2]->[1] eq $trinary[$i]; - my ($op, $op2) = splice @trinary, $i, 2, (); # remove the pair of operators - my $node = [ \ [$op, @trees[$i .. $i + 2] ], 0 ]; - splice @trees, $i, 3, $node; + ### return simple ternary + if (@exprs == 3) { + $self->throw('parse', "Ternary operator mismatch") if $ops[0] ne $op; + $self->throw('parse', "Ternary operator mismatch") if ! $ops[1] || $ops[1] eq $op; + return [[undef, $op, @exprs], 0]; } - } - return $trees[0]; # at this point the trinary has been reduced to a single operator + + ### reorder complex ternary - rare case + while ($#ops >= 1) { + ### if we look starting from the back - the first lead ternary op will always be next to its matching op + for (my $i = $#ops; $i >= 0; $i --) { + next if $OP->{$ops[$i]}->[2]->[1] eq $ops[$i]; + my ($op, $op2) = splice @ops, $i, 2, (); # remove the pair of operators + my $node = [[undef, $op, @exprs[$i .. $i + 2]], 0]; + splice @exprs, $i, 3, $node; + } + } + return $exprs[0]; # at this point the ternary has been reduced to a single operator + + } elsif ($type eq 'right' || $type eq 'assign') { + my $val = $exprs[-1]; + $val = [[undef, $ops[$_ - 1], $exprs[$_], $val], 0] for reverse (0 .. $#exprs - 1); + return $val; + + } else { + my $val = $exprs[0]; + $val = [[undef, $ops[$_ - 1], $val, $exprs[$_]], 0] for (1 .. $#exprs); + return $val; + + } } $self->throw('parse', "Couldn't apply precedence"); @@ -1043,29 +1296,78 @@ sub parse_args { my $self = shift; my $str_ref = shift; my $ARGS = shift || {}; - my $copy = $$str_ref; my @args; my @named; - while (length $$str_ref) { - my $copy = $$str_ref; - if (defined(my $name = $self->parse_variable(\$copy, {auto_quote => qr{ ^ (\w+) $QR_AQ_NOTDOT }xo})) - && $copy =~ s{ ^ = >? \s* $QR_COMMENTS }{}ox) { - $self->throw('parse', 'Named arguments not allowed') if $ARGS->{'positional_only'}; - my $val = $self->parse_variable(\$copy); - $copy =~ s{ ^ , \s* $QR_COMMENTS }{}ox; + my $name; + my $end = $self->{'_end_tag'} || '(?!)'; + while (1) { + my $mark = pos $$str_ref; + + ### look to see if the next thing is a directive or a closing tag + if (! $ARGS->{'is_parened'} + && ! $ARGS->{'require_arg'} + && $$str_ref =~ m{ \G $QR_DIRECTIVE (?: \s+ | (?: \s* $QR_COMMENTS (?: ;|[+=~-]?$end))) }gcxo + && ((pos($$str_ref) = $mark) || 1) # always revert + && $DIRECTIVES->{$self->{'ANYCASE'} ? uc($1) : $1} # looks like a directive - we are done + ) { + last; + } + if ($$str_ref =~ m{ \G [+=~-]? $end }gcx) { + pos($$str_ref) = $mark; + last; + } + + ### find the initial arg + my $name; + if ($ARGS->{'allow_bare_filenames'}) { + $name = $self->parse_expr($str_ref, {auto_quote => " + ($QR_FILENAME # file name + | \\w+\\b (?: :\\w+\\b)* ) # or block + (?= [+=~-]? $end # an end tag + | \\s*[+,;] # followed by explicit + , or ; + | \\s+ (?! [\\s=]) # or space not before an = + ) \\s* $QR_COMMENTS"}); + # filenames can be separated with a "+" - why a "+" ? + if ($$str_ref =~ m{ \G \+ (?! [+=~-]? $end) \s* $QR_COMMENTS }gcxo) { + push @args, $name; + $ARGS->{'require_arg'} = 1; + next; + } + } + if (! defined $name) { + $name = $self->parse_expr($str_ref); + if (! defined $name) { + if ($ARGS->{'require_arg'} && ! @args && ! $ARGS->{'positional_only'} && ! @named) { + $self->throw('parse', 'Argument required', undef, pos($$str_ref)); + } else { + last; + } + } + } + + $$str_ref =~ m{ \G \s* $QR_COMMENTS }gcxo; + + ### see if it is named or positional + if ($$str_ref =~ m{ \G = >? \s* $QR_COMMENTS }gcxo) { + $self->throw('parse', 'Named arguments not allowed', undef, $mark) if $ARGS->{'positional_only'}; + my $val = $self->parse_expr($str_ref); + $name = $name->[0] if ref($name) && @$name == 2 && ! $name->[1]; # strip a level of indirection on named arguments push @named, $name, $val; - $$str_ref = $copy; - } elsif (defined(my $arg = $self->parse_variable($str_ref))) { - push @args, $arg; - $$str_ref =~ s{ ^ , \s* $QR_COMMENTS }{}ox; } else { - last; + push @args, $name; } + + ### look for trailing comma + $ARGS->{'require_arg'} = ($$str_ref =~ m{ \G , \s* $QR_COMMENTS }gcxo) || 0; } - ### allow for named arguments to be added also - push @args, [\ ['hash', @named], 0] if scalar @named; + ### allow for named arguments to be added at the front (if asked) + if ($ARGS->{'named_at_front'}) { + unshift @args, [[undef, '{}', @named], 0]; + } elsif (scalar @named) { # only add at end - if there are some + push @args, [[undef, '{}', @named], 0] + } return \@args; } @@ -1076,7 +1378,7 @@ sub interpolate_node { return if $self->{'_in_perl'}; ### split on variables while keeping the variables - my @pieces = split m{ (?: ^ | (?[-1]; + my @pieces = split m{ (?: ^ | (?[-1]; if ($#pieces <= 0) { $tree->[-1] =~ s{ \\ ([\"\$]) }{$1}xg; return; @@ -1086,14 +1388,15 @@ sub interpolate_node { my $n = 0; foreach my $piece (@pieces) { $offset += length $piece; # we track the offset to make sure DEBUG has the right location + $piece =~ s{ \\ ([\"\$]) }{$1}xg; if (! ($n++ % 2)) { # odds will always be text chunks next if ! length $piece; - $piece =~ s{ \\ ([\"\$]) }{$1}xg; push @sub_tree, $piece; } elsif ($piece =~ m{ ^ \$ (\w+ (?:\.\w+)*) $ }x - || $piece =~ m{ ^ \$\{ \s* ([^\}]+) \} $ }x) { + || $piece =~ m{ ^ \$\{ \s* (.*?) (?parse_variable(\$name)]; + $name =~ s/\\\}/\}/g; + push @sub_tree, ['GET', $offset - length($piece), $offset, $self->parse_expr(\$name)]; } else { $self->throw('parse', "Parse error during interpolate node"); } @@ -1105,7 +1408,33 @@ sub interpolate_node { ###----------------------------------------------------------------### -sub get_variable { +sub execute_tree { + my ($self, $tree, $out_ref) = @_; + + # node contains (0: DIRECTIVE, + # 1: start_index, + # 2: end_index, + # 3: parsed tag details, + # 4: sub tree for block types + # 5: continuation sub trees for sub continuation block types (elsif, else, etc) + # 6: flag to capture next directive + for my $node (@$tree) { + ### text nodes are just the bare text + if (! ref $node) { + warn "NODE: TEXT\n" if trace; + $$out_ref .= $node if defined $node; + next; + } + + warn "NODE: $node->[0] (char $node->[1])\n" if trace; + $$out_ref .= $self->debug_node($node) if $self->{'_debug_dirs'} && ! $self->{'_debug_off'}; + + my $val = $DIRECTIVES->{$node->[0]}->[1]->($self, $node->[3], $node, $out_ref); + $$out_ref .= $val if defined $val; + } +} + +sub play_expr { ### allow for the parse tree to store literals return $_[1] if ! ref $_[1]; @@ -1115,28 +1444,30 @@ sub get_variable { my $i = 0; ### determine the top level of this particular variable access - my $ref = $var->[$i++]; + my $ref; + my $name = $var->[$i++]; my $args = $var->[$i++]; - warn "get_variable: begin \"$ref\"\n" if trace; - if (ref $ref) { - if (ref($ref) eq 'SCALAR') { # a scalar literal - $ref = $$ref; - } elsif (ref($ref) eq 'REF') { # operator - return $self->play_operator($$ref) if ${ $ref }->[0] eq '..'; - $ref = $self->play_operator($$ref); + warn "play_expr: begin \"$name\"\n" if trace; + if (ref $name) { + if (! defined $name->[0]) { # operator + return $self->play_operator($name) if wantarray && $name->[1] eq '..'; + $ref = $self->play_operator($name); } else { # a named variable access (ie via $name.foo) - $ref = $self->get_variable($ref); - if (defined $ref) { - return if $ref =~ $QR_PRIVATE; # don't allow vars that begin with _ - $ref = $self->{'_vars'}->{$ref}; + $name = $self->play_expr($name); + if (defined $name) { + return if $name =~ $QR_PRIVATE; # don't allow vars that begin with _ + return \$self->{'_vars'}->{$name} if $i >= $#$var && $ARGS->{'return_ref'} && ! ref $self->{'_vars'}->{$name}; + $ref = $self->{'_vars'}->{$name}; } } - } elsif (defined $ref) { + } elsif (defined $name) { if ($ARGS->{'is_namespace_during_compile'}) { - $ref = $self->{'NAMESPACE'}->{$ref}; + $ref = $self->{'NAMESPACE'}->{$name}; } else { - return if $ref =~ $QR_PRIVATE; # don't allow vars that begin with _ - $ref = $self->{'_vars'}->{$ref}; + return if $name =~ $QR_PRIVATE; # don't allow vars that begin with _ + return \$self->{'_vars'}->{$name} if $i >= $#$var && $ARGS->{'return_ref'} && ! ref $self->{'_vars'}->{$name}; + $ref = $self->{'_vars'}->{$name}; + $ref = ($name eq 'template' || $name eq 'component') ? $self->{"_$name"} : $VOBJS->{$name} if ! defined $ref; } } @@ -1146,7 +1477,8 @@ sub get_variable { ### check at each point if the rurned thing was a code if (UNIVERSAL::isa($ref, 'CODE')) { - my @results = $ref->($args ? @{ $self->vivify_args($args) } : ()); + return $ref if $i >= $#$var && $ARGS->{'return_ref'}; + my @results = $ref->($args ? map { $self->play_expr($_) } @$args : ()); if (defined $results[0]) { $ref = ($#results > 0) ? \@results : $results[0]; } elsif (defined $results[1]) { @@ -1160,14 +1492,14 @@ sub get_variable { ### descend one chained level last if $i >= $#$var; my $was_dot_call = $ARGS->{'no_dots'} ? 1 : $var->[$i++] eq '.'; - my $name = $var->[$i++]; - my $args = $var->[$i++]; - warn "get_variable: nested \"$name\"\n" if trace; + $name = $var->[$i++]; + $args = $var->[$i++]; + warn "play_expr: nested \"$name\"\n" if trace; ### allow for named portions of a variable name (foo.$name.bar) if (ref $name) { if (ref($name) eq 'ARRAY') { - $name = $self->get_variable($name); + $name = $self->play_expr($name); if (! defined($name) || $name =~ $QR_PRIVATE || $name =~ /^\./) { $ref = undef; last; @@ -1184,10 +1516,10 @@ sub get_variable { ### allow for scalar and filter access (this happens for every non virtual method call) if (! ref $ref) { if ($SCALAR_OPS->{$name}) { # normal scalar op - $ref = $SCALAR_OPS->{$name}->($ref, $args ? @{ $self->vivify_args($args) } : ()); + $ref = $SCALAR_OPS->{$name}->($ref, $args ? map { $self->play_expr($_) } @$args : ()); } elsif ($LIST_OPS->{$name}) { # auto-promote to list and use list op - $ref = $LIST_OPS->{$name}->([$ref], $args ? @{ $self->vivify_args($args) } : ()); + $ref = $LIST_OPS->{$name}->([$ref], $args ? map { $self->play_expr($_) } @$args : ()); } elsif (my $filter = $self->{'FILTERS'}->{$name} # filter configured in Template args || $FILTER_OPS->{$name} # predefined filters in CET @@ -1207,7 +1539,7 @@ sub get_variable { eval { my $sub = $filter->[0]; if ($filter->[1]) { # it is a "dynamic filter" that will return a sub - ($sub, my $err) = $sub->($self->context, $args ? @{ $self->vivify_args($args) } : ()); + ($sub, my $err) = $sub->($self->context, $args ? map { $self->play_expr($_) } @$args : ()); if (! $sub && $err) { $self->throw('filter', $err) if ref($err) !~ /Template::Exception$/; die $err; @@ -1240,7 +1572,8 @@ sub get_variable { ### method calls on objects if ($was_dot_call && UNIVERSAL::can($ref, 'can')) { - my @args = $args ? @{ $self->vivify_args($args) } : (); + return $ref if $i >= $#$var && $ARGS->{'return_ref'}; + my @args = $args ? map { $self->play_expr($_) } @$args : (); my @results = eval { $ref->$name(@args) }; if ($@) { my $class = ref $ref; @@ -1260,21 +1593,26 @@ sub get_variable { ### hash member access if (UNIVERSAL::isa($ref, 'HASH')) { if ($was_dot_call && exists($ref->{$name}) ) { + return \ $ref->{$name} if $i >= $#$var && $ARGS->{'return_ref'} && ! ref $ref->{$name}; $ref = $ref->{$name}; } elsif ($HASH_OPS->{$name}) { - $ref = $HASH_OPS->{$name}->($ref, $args ? @{ $self->vivify_args($args) } : ()); + $ref = $HASH_OPS->{$name}->($ref, $args ? map { $self->play_expr($_) } @$args : ()); } elsif ($ARGS->{'is_namespace_during_compile'}) { return $var; # abort - can't fold namespace variable } else { + return \ $ref->{$name} if $i >= $#$var && $ARGS->{'return_ref'}; $ref = undef; } ### array access } elsif (UNIVERSAL::isa($ref, 'ARRAY')) { - if ($name =~ /^\d+$/) { - $ref = ($name > $#$ref) ? undef : $ref->[$name]; + if ($name =~ m{ ^ -? $QR_NUM $ }ox) { + return \ $ref->[$name] if $i >= $#$var && $ARGS->{'return_ref'} && ! ref $ref->[$name]; + $ref = $ref->[$name]; + } elsif ($LIST_OPS->{$name}) { + $ref = $LIST_OPS->{$name}->($ref, $args ? map { $self->play_expr($_) } @$args : ()); } else { - $ref = (! $LIST_OPS->{$name}) ? undef : $LIST_OPS->{$name}->($ref, $args ? @{ $self->vivify_args($args) } : ()); + $ref = undef; } } } @@ -1285,7 +1623,7 @@ sub get_variable { if (! defined $ref) { if ($self->{'_debug_undef'}) { my $chunk = $var->[$i - 2]; - $chunk = $self->get_variable($chunk) if ref($chunk) eq 'ARRAY'; + $chunk = $self->play_expr($chunk) if ref($chunk) eq 'ARRAY'; die "$chunk is undefined\n"; } else { $ref = $self->undefined_any($var); @@ -1295,6 +1633,12 @@ sub get_variable { return $ref; } +sub is_empty_named_args { + my ($self, $hash_ident) = @_; + # [[undef, '{}', 'key1', 'val1', 'key2, 'val2'], 0] + return @{ $hash_ident->[0] } <= 2; +} + sub set_variable { my ($self, $var, $val, $ARGS) = @_; $ARGS ||= {}; @@ -1307,38 +1651,45 @@ sub set_variable { my $ref = $var->[$i++]; my $args = $var->[$i++]; if (ref $ref) { - if (ref($ref) eq 'ARRAY') { # named access (ie via $name.foo) - $ref = $self->get_variable($ref); - if (defined $ref && $ref !~ $QR_PRIVATE) { # don't allow vars that begin with _ - if ($#$var <= $i) { - $self->{'_vars'}->{$ref} = $val; - return; - } else { - $ref = $self->{'_vars'}->{$ref} ||= {}; - } + ### non-named types can't be set + return if ref($ref) ne 'ARRAY' || ! defined $ref->[0]; + + # named access (ie via $name.foo) + $ref = $self->play_expr($ref); + if (defined $ref && $ref !~ $QR_PRIVATE) { # don't allow vars that begin with _ + if ($#$var <= $i) { + return $self->{'_vars'}->{$ref} = $val; } else { - return; + $ref = $self->{'_vars'}->{$ref} ||= {}; } - } else { # all other types can't be set + } else { return; } } elsif (defined $ref) { return if $ref =~ $QR_PRIVATE; # don't allow vars that begin with _ if ($#$var <= $i) { - $self->{'_vars'}->{$ref} = $val; - return; + return $self->{'_vars'}->{$ref} = $val; } else { $ref = $self->{'_vars'}->{$ref} ||= {}; } } - ### let the top level thing be a code block - if (UNIVERSAL::isa($ref, 'CODE')) { - return; - } + while (defined $ref) { - ### vivify the chained levels - while (defined $ref && $#$var > $i) { + ### check at each point if the returned thing was a code + if (UNIVERSAL::isa($ref, 'CODE')) { + my @results = $ref->($args ? map { $self->play_expr($_) } @$args : ()); + if (defined $results[0]) { + $ref = ($#results > 0) ? \@results : $results[0]; + } elsif (defined $results[1]) { + die $results[1]; # TT behavior - why not just throw ? + } else { + return; + } + } + + ### descend one chained level + last if $i >= $#$var; my $was_dot_call = $ARGS->{'no_dots'} ? 1 : $var->[$i++] eq '.'; my $name = $var->[$i++]; my $args = $var->[$i++]; @@ -1346,10 +1697,9 @@ sub set_variable { ### allow for named portions of a variable name (foo.$name.bar) if (ref $name) { if (ref($name) eq 'ARRAY') { - $name = $self->get_variable($name); + $name = $self->play_expr($name); if (! defined($name) || $name =~ /^[_.]/) { - $ref = undef; - next; + return; } } else { die "Shouldn't get a ".ref($name)." during a vivify on chain"; @@ -1359,10 +1709,14 @@ sub set_variable { return; } + ### scalar access + if (! ref $ref) { + return; + ### method calls on objects - if (UNIVERSAL::can($ref, 'can')) { + } elsif (UNIVERSAL::can($ref, 'can')) { my $lvalueish; - my @args = $args ? @{ $self->vivify_args($args) } : (); + my @args = $args ? map { $self->play_expr($_) } @$args : (); if ($i >= $#$var) { $lvalueish = 1; push @args, $val; @@ -1374,20 +1728,20 @@ sub set_variable { } elsif (defined $results[1]) { die $results[1]; # TT behavior - why not just throw ? } else { - $ref = undef; + return; } return if $lvalueish; next; } - die $@ if ref $@ || $@ !~ /Can\'t locate object method/; + my $class = ref $ref; + die $@ if ref $@ || $@ !~ /Can\'t locate object method "\Q$name\E" via package "\Q$class\E"/; # fall on down to "normal" accessors } ### hash member access if (UNIVERSAL::isa($ref, 'HASH')) { if ($#$var <= $i) { - $ref->{$name} = $val; - return; + return $ref->{$name} = $val; } else { $ref = $ref->{$name} ||= {}; next; @@ -1395,10 +1749,9 @@ sub set_variable { ### array access } elsif (UNIVERSAL::isa($ref, 'ARRAY')) { - if ($name =~ /^\d+$/) { + if ($name =~ m{ ^ -? $QR_NUM $ }ox) { if ($#$var <= $i) { - $ref->[$name] = $val; - return; + return $ref->[$name] = $val; } else { $ref = $ref->[$name] ||= {}; next; @@ -1407,64 +1760,94 @@ sub set_variable { return; } - ### scalar access - } elsif (! ref($ref) && defined($ref)) { - return; - } - - ### check at each point if the returned thing was a code - if (defined($ref) && UNIVERSAL::isa($ref, 'CODE')) { - my @results = $ref->($args ? @{ $self->vivify_args($args) } : ()); - if (defined $results[0]) { - $ref = ($#results > 0) ? \@results : $results[0]; - } elsif (defined $results[1]) { - die $results[1]; # TT behavior - why not just throw ? - } else { - return; - } } } - return $ref; -} - -sub vivify_args { - my $self = shift; - my $vars = shift; - return [map {$self->get_variable($_)} @$vars]; + return; } ###----------------------------------------------------------------### sub play_operator { - my $self = shift; - my $tree = shift; + my ($self, $tree) = @_; + ### $tree looks like [undef, '+', 4, 5] - if ($OP_DISPATCH->{$tree->[0]}) { - my @args = map { $self->get_variable($tree->[$_]) } 1 .. $#$tree; + if ($OP_DISPATCH->{$tree->[1]}) { local $^W; - return $OP_DISPATCH->{$tree->[0]}->(@args); + if ($OP_ASSIGN->{$tree->[1]}) { + my $val = $OP_DISPATCH->{$tree->[1]}->($self->play_expr($tree->[2]), $self->play_expr($tree->[3])); + $self->set_variable($tree->[2], $val); + return $val; + } else { + return $OP_DISPATCH->{$tree->[1]}->(@$tree == 3 ? $self->play_expr($tree->[2]) : ($self->play_expr($tree->[2]), $self->play_expr($tree->[3]))); + } } - my $op = $tree->[0]; + my $op = $tree->[1]; ### do custom and short-circuitable operators if ($op eq '=') { - my $val = $self->get_variable($tree->[2]); - $self->set_variable($tree->[1], $val); + my $val = $self->play_expr($tree->[3]); + $self->set_variable($tree->[2], $val); return $val; } elsif ($op eq '||' || $op eq 'or' || $op eq 'OR') { - return $self->get_variable($tree->[1]) || $self->get_variable($tree->[2]) || ''; + return $self->play_expr($tree->[2]) || $self->play_expr($tree->[3]) || ''; } elsif ($op eq '&&' || $op eq 'and' || $op eq 'AND') { - my $var = $self->get_variable($tree->[1]) && $self->get_variable($tree->[2]); + my $var = $self->play_expr($tree->[2]) && $self->play_expr($tree->[3]); return $var ? $var : 0; } elsif ($op eq '?') { local $^W; - return $self->get_variable($tree->[1]) ? $self->get_variable($tree->[2]) : $self->get_variable($tree->[3]); + return $self->play_expr($tree->[2]) ? $self->play_expr($tree->[3]) : $self->play_expr($tree->[4]); + + } elsif ($op eq '~' || $op eq '_') { + local $^W; + my $s = ''; + $s .= $self->play_expr($tree->[$_]) for 2 .. $#$tree; + return $s; + + } elsif ($op eq '[]') { + return [map {$self->play_expr($tree->[$_])} 2 .. $#$tree]; + + } elsif ($op eq '{}') { + local $^W; + my @e; + push @e, $self->play_expr($tree->[$_]) for 2 .. $#$tree; + return {@e}; + + } elsif ($op eq '++') { + local $^W; + my $val = 0 + $self->play_expr($tree->[2]); + $self->set_variable($tree->[2], $val + 1); + return $tree->[3] ? $val : $val + 1; # ->[3] is set to 1 during parsing of postfix ops + + } elsif ($op eq '--') { + local $^W; + my $val = 0 + $self->play_expr($tree->[2]); + $self->set_variable($tree->[2], $val - 1); + return $tree->[3] ? $val : $val - 1; # ->[3] is set to 1 during parsing of postfix ops + + } elsif ($op eq '\\') { + my $var = $tree->[2]; + + my $ref = $self->play_expr($var, {return_ref => 1}); + return $ref if ! ref $ref; + return sub { sub { $$ref } } if ref $ref eq 'SCALAR' || ref $ref eq 'REF'; + + my $self_copy = $self; + eval {require Scalar::Util; Scalar::Util::weaken($self_copy)}; + + my $last = ['temp deref key', $var->[-1] ? [@{ $var->[-1] }] : 0]; + return sub { sub { # return a double sub so that the current play_expr will return a coderef + local $self_copy->{'_vars'}->{'temp deref key'} = $ref; + $last->[-1] = (ref $last->[-1] ? [@{ $last->[-1] }, @_] : [@_]) if @_; + return $self->play_expr($last); + } }; + } elsif ($op eq 'qr') { + return $tree->[3] ? qr{(?$tree->[3]:$tree->[2])} : qr{$tree->[2]}; } $self->throw('operator', "Un-implemented operation $op"); @@ -1473,20 +1856,21 @@ sub play_operator { ###----------------------------------------------------------------### sub parse_BLOCK { - my ($self, $tag_ref, $node) = @_; + my ($self, $str_ref, $node) = @_; - my $block_name = ''; - if ($$tag_ref =~ s{ ^ (\w+ (?: :\w+)*) \s* (?! [\.\|]) }{}x - || $$tag_ref =~ s{ ^ '(|.*?[^\\])' \s* (?! [\.\|]) }{}x - || $$tag_ref =~ s{ ^ "(|.*?[^\\])" \s* (?! [\.\|]) }{}x - ) { - $block_name = $1; - ### allow for nested blocks to have nested names - my @names = map {$_->[3]} grep {$_->[0] eq 'BLOCK'} @{ $self->{'_state'} }; - $block_name = join("/", @names, $block_name) if scalar @names; - } + my $end = $self->{'_end_tag'} || '(?!)'; + my $block_name = $self->parse_expr($str_ref, {auto_quote => " + ($QR_FILENAME # file name + | \\w+\\b (?: :\\w+\\b)* ) # or block + (?= [+=~-]? $end # an end tag + | \\s*[+,;] # followed by explicit + , or ; + | \\s+ (?! [\\s=]) # or space not before an = + ) \\s* $QR_COMMENTS"}); - return $block_name; + return '' if ! defined $block_name; + + my $prepend = join "/", map {$_->[3]} grep {ref($_) && $_->[0] eq 'BLOCK'} @{ $self->{'_state'} || {} }; + return $prepend ? "$prepend/$block_name" : $block_name; } sub play_BLOCK { @@ -1495,7 +1879,7 @@ sub play_BLOCK { ### store a named reference - but do nothing until something processes it $self->{'BLOCKS'}->{$block_name} = { _tree => $node->[4], - name => $self->{'_vars'}->{'component'}->{'name'} .'/'. $block_name, + name => $self->{'_component'}->{'name'} .'/'. $block_name, }; return; @@ -1506,14 +1890,14 @@ sub parse_CALL { $DIRECTIVES->{'GET'}->[0]->(@_) } sub play_CALL { $DIRECTIVES->{'GET'}->[1]->(@_); return } sub parse_CASE { - my ($self, $tag_ref) = @_; - return if $$tag_ref =~ s{ ^ DEFAULT \s* }{}x; - return $self->parse_variable($tag_ref); + my ($self, $str_ref) = @_; + return if $$str_ref =~ m{ \G DEFAULT \s* }gcx; + return $self->parse_expr($str_ref); } sub parse_CATCH { - my ($self, $tag_ref) = @_; - return $self->parse_variable($tag_ref, {auto_quote => qr{ ^ (\w+ (?: \.\w+)*) $QR_AQ_SPACE }xo}); + my ($self, $str_ref) = @_; + return $self->parse_expr($str_ref, {auto_quote => "(\\w+\\b (?: \\.\\w+\\b)*) $QR_AQ_SPACE \\s* $QR_COMMENTS"}); } sub play_control { @@ -1526,12 +1910,58 @@ sub play_CLEAR { $$out_ref = ''; } +sub parse_CONFIG { + my ($self, $str_ref) = @_; + + my %ctime = map {$_ => 1} @CONFIG_COMPILETIME; + my %rtime = map {$_ => 1} @CONFIG_RUNTIME; + + my $config = $self->parse_args($str_ref, {named_at_front => 1, is_parened => 1}); + my $ref = $config->[0]->[0]; + for (my $i = 2; $i < @$ref; $i += 2) { + my $key = $ref->[$i] = uc $ref->[$i]; + my $val = $ref->[$i + 1]; + if ($ctime{$key}) { + splice @$ref, $i, 2, (); # remove the options + $self->{$key} = $self->play_expr($val); + $i -= 2; + } elsif (! $rtime{$key}) { + $self->throw('parse', "Unknown CONFIG option \"$key\"", undef, pos($$str_ref)); + } + } + for (my $i = 1; $i < @$config; $i++) { + my $key = $config->[$i] = uc $config->[$i]->[0]; + if ($ctime{$key}) { + $config->[$i] = "CONFIG $key = ".(defined($self->{$key}) ? $self->{$key} : 'undef'); + } elsif (! $rtime{$key}) { + $self->throw('parse', "Unknown CONFIG option \"$key\"", undef, pos($$str_ref)); + } + } + return $config; +} + +sub play_CONFIG { + my ($self, $config) = @_; + + my %rtime = map {$_ => 1} @CONFIG_RUNTIME; + + ### do runtime config - not many options get these + my ($named, @the_rest) = @$config; + $named = $self->play_expr($named); + @{ $self }{keys %$named} = @{ $named }{keys %$named}; + + ### show what current values are + return join("\n", map { $rtime{$_} ? ("CONFIG $_ = ".(defined($self->{$_}) ? $self->{$_} : 'undef')) : $_ } @the_rest); +} + sub parse_DEBUG { - my ($self, $tag_ref) = @_; - $$tag_ref =~ s{ ^ (on | off | format) \s* }{}xi || $self->throw('parse', "Unknown DEBUG option"); + my ($self, $str_ref) = @_; + $$str_ref =~ m{ \G ([Oo][Nn] | [Oo][Ff][Ff] | [Ff][Oo][Rr][Mm][Aa][Tt]) \s* }gcx + || $self->throw('parse', "Unknown DEBUG option", undef, pos($$str_ref)); my $ret = [lc($1)]; if ($ret->[0] eq 'format') { - $$tag_ref =~ s{ ^ ([\"\']) (|.*?[^\\]) \1 \s* }{}xs || $self->throw('parse', "Missing format string"); + $$str_ref =~ m{ \G ([\"\']) (|.*?[^\\]) \1 \s* }gcxs + || $self->throw('parse', "Missing format string", undef, pos($$str_ref)); $ret->[1] = $2; } return $ret; @@ -1553,11 +1983,11 @@ sub parse_DEFAULT { $DIRECTIVES->{'SET'}->[0]->(@_) } sub play_DEFAULT { my ($self, $set) = @_; foreach (@$set) { - my ($set, $default) = @$_; + my ($op, $set, $default) = @$_; next if ! defined $set; - my $val = $self->get_variable($set); + my $val = $self->play_expr($set); if (! $val) { - $default = defined($default) ? $self->get_variable($default) : ''; + $default = defined($default) ? $self->play_expr($default) : ''; $self->set_variable($set, $default); } } @@ -1565,49 +1995,68 @@ sub play_DEFAULT { } sub parse_DUMP { - my ($self, $tag_ref) = @_; - my $ref = $self->parse_variable($tag_ref); - return $ref; + my ($self, $str_ref) = @_; + return $self->parse_args($str_ref, {named_at_front => 1}); } sub play_DUMP { - my ($self, $ident, $node) = @_; - require Data::Dumper; + my ($self, $dump, $node) = @_; + + my $conf = $self->{'DUMP'}; + return if ! $conf && defined $conf; # DUMP => 0 + $conf = {} if ref $conf ne 'HASH'; + + ### allow for handler override + my $handler = $conf->{'handler'}; + if (! $handler) { + require Data::Dumper; + my $obj = Data::Dumper->new([]); + my $meth; + foreach my $prop (keys %$conf) { $obj->$prop($conf->{$prop}) if $prop =~ /^\w+$/ && ($meth = $obj->can($prop)) } + my $sort = defined($conf->{'Sortkeys'}) ? $obj->Sortkeys : 1; + $obj->Sortkeys(sub { my $h = shift; [grep {$_ !~ $QR_PRIVATE} ($sort ? sort keys %$h : keys %$h)] }); + $handler = sub { $obj->Values([@_]); $obj->Dump } + } + + my ($named, @dump) = @$dump; + push @dump, $named if ! $self->is_empty_named_args($named); # add named args back on at end - if there are some + $_ = $self->play_expr($_) foreach @dump; + + ### look for the text describing what to dump my $info = $self->node_info($node); my $out; - my $var; - if ($ident) { - $out = Data::Dumper::Dumper($self->get_variable($ident)); - $var = $info->{'text'}; - $var =~ s/^[+\-~=]?\s*DUMP\s+//; - $var =~ s/\s*[+\-~=]?$//; + if (@dump) { + $out = $handler->(@dump && @dump == 1 ? $dump[0] : \@dump); + my $name = $info->{'text'}; + $name =~ s/^[+=~-]?\s*DUMP\s+//; + $name =~ s/\s*[+=~-]?$//; + $out =~ s/\$VAR1/$name/; + } elsif (defined($conf->{'EntireStash'}) && ! $conf->{'EntireStash'}) { + $out = ''; } else { - my @were_never_here = (qw(template component), grep {$_ =~ $QR_PRIVATE} keys %{ $self->{'_vars'} }); - local @{ $self->{'_vars'} }{ @were_never_here }; - delete @{ $self->{'_vars'} }{ @were_never_here }; - $out = Data::Dumper::Dumper($self->{'_vars'}); - $var = 'EntireStash'; - } - if ($ENV{'REQUEST_METHOD'}) { - $out =~ s/($self->{'_vars'}); + $out =~ s/\$VAR1/EntireStash/g; + } + + if ($conf->{'html'} || (! defined($conf->{'html'}) && $ENV{'REQUEST_METHOD'})) { + $out = $SCALAR_OPS->{'html'}->($out); $out = "
$out
"; - $out =~ s/\$VAR1/$var/; - $out = "DUMP: File \"$info->{file}\" line $info->{line}$out"; + $out = "DUMP: File \"$info->{file}\" line $info->{line}$out" if $conf->{'header'} || ! defined $conf->{'header'}; } else { - $out =~ s/\$VAR1/$var/; + $out = "DUMP: File \"$info->{file}\" line $info->{line}\n $out" if $conf->{'header'} || ! defined $conf->{'header'}; } return $out; } sub parse_FILTER { - my ($self, $tag_ref) = @_; + my ($self, $str_ref) = @_; my $name = ''; - if ($$tag_ref =~ s{ ^ ([^\W\d]\w*) \s* = \s* }{}x) { + if ($$str_ref =~ m{ \G ([^\W\d]\w*) \s* = \s* }gcx) { $name = $1; } - my $filter = $self->parse_variable($tag_ref); + my $filter = $self->parse_expr($str_ref); $filter = '' if ! defined $filter; return [$name, $filter]; @@ -1628,19 +2077,19 @@ sub play_FILTER { eval { $self->execute_tree($sub_tree, \$out) }; die $@ if $@ && ref($@) !~ /Template::Exception$/; - my $var = [\$out, 0, '|', @$filter]; # make a temporary var out of it + my $var = [[undef, '~', $out], 0, '|', @$filter]; # make a temporary var out of it return $DIRECTIVES->{'GET'}->[1]->($self, $var, $node, $out_ref); } sub parse_FOREACH { - my ($self, $tag_ref) = @_; - my $items = $self->parse_variable($tag_ref); + my ($self, $str_ref) = @_; + my $items = $self->parse_expr($str_ref); my $var; - if ($$tag_ref =~ s{ ^ (= | [Ii][Nn]\b) \s* }{}x) { + if ($$str_ref =~ m{ \G (= | [Ii][Nn]\b) \s* }gcx) { $var = [@$items]; - $items = $self->parse_variable($tag_ref); + $items = $self->parse_expr($str_ref); } return [$var, $items]; } @@ -1651,7 +2100,7 @@ sub play_FOREACH { ### get the items - make sure it is an arrayref my ($var, $items) = @$ref; - $items = $self->get_variable($items); + $items = $self->play_expr($items); return '' if ! defined $items; if (ref($items) !~ /Iterator$/) { @@ -1723,27 +2172,27 @@ sub play_FOREACH { } sub parse_GET { - my ($self, $tag_ref) = @_; - my $ref = $self->parse_variable($tag_ref); - $self->throw('parse', "Missing variable name") if ! defined $ref; + my ($self, $str_ref) = @_; + my $ref = $self->parse_expr($str_ref); + $self->throw('parse', "Missing variable name", undef, pos($$str_ref)) if ! defined $ref; return $ref; } sub play_GET { my ($self, $ident, $node) = @_; - my $var = $self->get_variable($ident); + my $var = $self->play_expr($ident); return (! defined $var) ? $self->undefined_get($ident, $node) : $var; } sub parse_IF { - my ($self, $tag_ref) = @_; - return $self->parse_variable($tag_ref); + my ($self, $str_ref) = @_; + return $self->parse_expr($str_ref); } sub play_IF { my ($self, $var, $node, $out_ref) = @_; - my $val = $self->get_variable($var); + my $val = $self->play_expr($var); if ($val) { my $body_ref = $node->[4] ||= []; $self->execute_tree($body_ref, $out_ref); @@ -1757,7 +2206,7 @@ sub play_IF { return; } my $var = $node->[3]; - my $val = $self->get_variable($var); + my $val = $self->play_expr($var); if ($val) { my $body_ref = $node->[4] ||= []; $self->execute_tree($body_ref, $out_ref); @@ -1770,7 +2219,7 @@ sub play_IF { sub parse_INCLUDE { $DIRECTIVES->{'PROCESS'}->[0]->(@_) } sub play_INCLUDE { - my ($self, $tag_ref, $node, $out_ref) = @_; + my ($self, $str_ref, $node, $out_ref) = @_; ### localize the swap my $swap = $self->{'_vars'}; @@ -1780,7 +2229,7 @@ sub play_INCLUDE { my $blocks = $self->{'BLOCKS'}; local $self->{'BLOCKS'} = {%$blocks}; - my $str = $DIRECTIVES->{'PROCESS'}->[1]->($self, $tag_ref, $node, $out_ref); + my $str = $DIRECTIVES->{'PROCESS'}->[1]->($self, $str_ref, $node, $out_ref); return $str; } @@ -1788,11 +2237,12 @@ sub play_INCLUDE { sub parse_INSERT { $DIRECTIVES->{'PROCESS'}->[0]->(@_) } sub play_INSERT { - my ($self, $var, $node, $out_ref) = @_; - my ($names, $args) = @$var; + my ($self, $args, $node, $out_ref) = @_; - foreach my $name (@$names) { - my $filename = $self->get_variable($name); + my ($named, @files) = @$args; + + foreach my $name (@files) { + my $filename = $self->play_expr($name); $$out_ref .= $self->_insert($filename); } @@ -1800,23 +2250,21 @@ sub play_INSERT { } sub parse_MACRO { - my ($self, $tag_ref, $node) = @_; - my $copy = $$tag_ref; + my ($self, $str_ref, $node) = @_; - my $name = $self->parse_variable(\$copy, {auto_quote => qr{ ^ (\w+) $QR_AQ_NOTDOT }xo}); - $self->throw('parse', "Missing macro name") if ! defined $name; + my $name = $self->parse_expr($str_ref, {auto_quote => "(\\w+\\b) (?! \\.) \\s* $QR_COMMENTS"}); + $self->throw('parse', "Missing macro name", undef, pos($$str_ref)) if ! defined $name; if (! ref $name) { $name = [ $name, 0 ]; } my $args; - if ($copy =~ s{ ^ \( \s* }{}x) { - $args = $self->parse_args(\$copy, {positional_only => 1}); - $copy =~ s { ^ \) \s* }{}x || $self->throw('parse.missing', "Missing close ')'"); + if ($$str_ref =~ m{ \G \( \s* }gcx) { + $args = $self->parse_args($str_ref, {positional_only => 1}); + $$str_ref =~ m{ \G \) \s* }gcx || $self->throw('parse.missing', "Missing close ')'", undef, pos($$str_ref)); } $node->[6] = 1; # set a flag to keep parsing - $$tag_ref = $copy; return [$name, $args]; } @@ -1833,7 +2281,8 @@ sub play_MACRO { $sub_tree = $sub_tree->[0]->[4]; } - my $self_copy = $self->weak_copy; + my $self_copy = $self; + eval {require Scalar::Util; Scalar::Util::weaken($self_copy)}; ### install a closure in the stash that will handle the macro $self->set_variable($name, sub { @@ -1841,6 +2290,12 @@ sub play_MACRO { my $copy = $self_copy->{'_vars'}; local $self_copy->{'_vars'}= {%$copy}; + ### prevent recursion + local $self_copy->{'_macro_recurse'} = $self_copy->{'_macro_recurse'} || 0; + $self_copy->throw('macro_recurse', "MAX_MACRO_RECURSE $MAX_MACRO_RECURSE reached") + if ++$self_copy->{'_macro_recurse'} > ($self_copy->{'MAX_MACRO_RECURSE'} || $MAX_MACRO_RECURSE); + + ### set arguments my $named = pop(@_) if $_[-1] && UNIVERSAL::isa($_[-1],'HASH') && $#_ > $#$args; my @positional = @_; @@ -1860,18 +2315,19 @@ sub play_MACRO { return; } -sub play_METADEF { +sub play_META { my ($self, $hash) = @_; + + my @keys = keys %$hash; + my $ref; if ($self->{'_top_level'}) { - $ref = $self->{'_vars'}->{'template'} ||= {}; + $ref = $self->{'_template'} ||= {}; } else { - $ref = $self->{'_vars'}->{'component'} ||= {}; - } - foreach my $key (keys %$hash) { - next if $key eq 'name' || $key eq 'modtime'; - $ref->{$key} = $hash->{$key}; + $ref = $self->{'_component'} ||= {}; } + + @{ $ref }{ @keys } = @{ $hash }{ @keys }; return; } @@ -1919,42 +2375,26 @@ sub play_PERL { } sub parse_PROCESS { - my ($self, $tag_ref) = @_; - my $info = [[], []]; - while (defined(my $filename = $self->parse_variable($tag_ref, { - auto_quote => qr{ ^ ($QR_FILENAME | \w+ (?: :\w+)* ) $QR_AQ_SPACE }xo, - }))) { - push @{$info->[0]}, $filename; - last if $$tag_ref !~ s{ ^ \+ \s* }{}x; - } - - ### allow for post process variables - while (length $$tag_ref) { - last if $$tag_ref =~ / ^ (\w+) (?: ;|$|\s)/x && $DIRECTIVES->{$1}; ### looks like a directive - we are done - - my $var = $self->parse_variable($tag_ref); - last if ! defined $var; - if ($$tag_ref !~ s{ ^ = >? \s* }{}x) { - $self->throw('parse.missing.equals', 'Missing equals while parsing args'); - } - - my $val = $self->parse_variable($tag_ref); - push @{$info->[1]}, [$var, $val]; - $$tag_ref =~ s{ ^ , \s* $QR_COMMENTS }{}ox if $val; - } + my ($self, $str_ref) = @_; - return $info; + return $self->parse_args($str_ref, { + named_at_front => 1, + allow_bare_filenames => 1, + require_arg => 1, + }); } sub play_PROCESS { my ($self, $info, $node, $out_ref) = @_; - my ($files, $args) = @$info; + my ($args, @files) = @$info; ### set passed args - foreach (@$args) { - my ($key, $val) = @$_; - $val = $self->get_variable($val); + # [[undef, '{}', 'key1', 'val1', 'key2', 'val2'], 0] + $args = $args->[0]; + foreach (my $i = 2; $i < @$args; $i+=2) { + my $key = $args->[$i]; + my $val = $self->play_expr($args->[$i+1]); if (ref($key) && @$key == 2 && $key->[0] eq 'import' && UNIVERSAL::isa($val, 'HASH')) { # import ?! - whatever foreach my $key (keys %$val) { $self->set_variable([$key,0], $val->{$key}); @@ -1965,9 +2405,9 @@ sub play_PROCESS { } ### iterate on any passed block or filename - foreach my $ref (@$files) { + foreach my $ref (@files) { next if ! defined $ref; - my $filename = $self->get_variable($ref); + my $filename = $self->play_expr($ref); my $out = ''; # have temp item to allow clear to correctly clear ### normal blocks or filenames @@ -1976,10 +2416,18 @@ sub play_PROCESS { ### allow for $template which is used in some odd instances } else { - $self->throw('process', "Unable to process document $filename") if $ref->[0] ne 'template'; + my $doc; + if ($ref->[0] eq 'template') { + $doc = $filename; + } else { + $doc = $self->play_expr($ref); + if (ref($doc) ne 'HASH' || ! $doc->{'_tree'}) { + $self->throw('process', "Passed item doesn't appear to be a valid document"); + } + } $self->throw('process', "Recursion detected in $node->[0] \$template") if $self->{'_process_dollar_template'}; local $self->{'_process_dollar_template'} = 1; - local $self->{'_vars'}->{'component'} = my $doc = $filename; + local $self->{'_component'} = $filename; return if ! $doc->{'_tree'}; ### execute and trim @@ -2042,43 +2490,50 @@ sub play_RAWPERL { } sub parse_SET { - my ($self, $tag_ref, $node, $initial_var) = @_; + my ($self, $str_ref, $node, $initial_op, $initial_var) = @_; my @SET; - my $copy = $$tag_ref; my $func; - while (length $$tag_ref) { - my $set; - my $get_val; - my $val; - if ($initial_var) { - $set = $initial_var; - undef $initial_var; - $get_val = 1; - } else { - $set = $self->parse_variable($tag_ref); - last if ! defined $set; - $get_val = $$tag_ref =~ s{ ^ = >? \s* }{}x; - } - if (! $get_val) { # no next val - $val = undef; - } elsif ($$tag_ref =~ $QR_DIRECTIVE # find a word - && $DIRECTIVES->{$1}) { # is it a directive - if so set up capturing - $node->[6] = 1; # set a flag to keep parsing - $val = $node->[4] ||= []; # setup storage - push @SET, [$set, $val]; - last; + + if ($initial_op) { + if ($$str_ref =~ m{ \G $QR_DIRECTIVE }gcx # find a word + && ((pos($$str_ref) -= length($1)) || 1) # always revert + && $DIRECTIVES->{$self->{'ANYCASE'} ? uc $1 : $1}) { # make sure its a directive - if so set up capturing + $node->[6] = 1; # set a flag to keep parsing + my $val = $node->[4] ||= []; # setup storage + return [[$initial_op, $initial_var, $val]]; } else { # get a normal variable - $val = $self->parse_variable($tag_ref); + return [[$initial_op, $initial_var, $self->parse_expr($str_ref)]]; } - push @SET, [$set, $val]; } + + while (1) { + my $set = $self->parse_expr($str_ref); + last if ! defined $set; + + if ($$str_ref =~ m{ \G ($QR_OP_ASSIGN) >? \s* }gcx) { + my $op = $1; + if ($$str_ref =~ m{ \G $QR_DIRECTIVE }gcx # find a word + && ((pos($$str_ref) -= length($1)) || 1) # always revert + && $DIRECTIVES->{$self->{'ANYCASE'} ? uc $1 : $1}) { # make sure its a directive - if so set up capturing + $node->[6] = 1; # set a flag to keep parsing + my $val = $node->[4] ||= []; # setup storage + push @SET, [$op, $set, $val]; + last; + } else { # get a normal variable + push @SET, [$op, $set, $self->parse_expr($str_ref)]; + } + } else { + push @SET, ['=', $set, undef]; + } + } + return \@SET; } sub play_SET { my ($self, $set, $node) = @_; foreach (@$set) { - my ($set, $val) = @$_; + my ($op, $set, $val) = @$_; if (! defined $val) { # not defined $val = ''; } elsif ($node->[4] && $val == $node->[4]) { # a captured directive @@ -2087,7 +2542,12 @@ sub play_SET { $val = ''; $self->execute_tree($sub_tree, \$val); } else { # normal var - $val = $self->get_variable($val); + $val = $self->play_expr($val); + } + + if ($OP_DISPATCH->{$op}) { + local $^W; + $val = $OP_DISPATCH->{$op}->($self->play_expr($set), $val); } $self->set_variable($set, $val); @@ -2100,7 +2560,7 @@ sub parse_SWITCH { $DIRECTIVES->{'GET'}->[0]->(@_) } sub play_SWITCH { my ($self, $var, $node, $out_ref) = @_; - my $val = $self->get_variable($var); + my $val = $self->play_expr($var); $val = '' if ! defined $val; ### $node->[4] is thrown away @@ -2112,14 +2572,14 @@ sub play_SWITCH { next; } - my $val2 = $self->get_variable($var); + my $val2 = $self->play_expr($var); $val2 = [$val2] if ! UNIVERSAL::isa($val2, 'ARRAY'); for my $test (@$val2) { # find matching values next if ! defined $val && defined $test; next if defined $val && ! defined $test; if ($val ne $test) { # check string-wise first - then numerical - next if $val !~ /^ -? (?: \d*\.\d+ | \d+) $/x; - next if $test !~ /^ -? (?: \d*\.\d+ | \d+) $/x; + next if $val !~ m{ ^ -? $QR_NUM $ }ox; + next if $test !~ m{ ^ -? $QR_NUM $ }ox; next if $val != $test; } @@ -2137,18 +2597,23 @@ sub play_SWITCH { } sub parse_THROW { - my ($self, $tag_ref, $node) = @_; - my $name = $self->parse_variable($tag_ref, {auto_quote => qr{ ^ (\w+ (?: \.\w+)*) $QR_AQ_SPACE }xo}); - $self->throw('parse.missing', "Missing name in THROW", $node) if ! $name; - my $args = $self->parse_args($tag_ref); + my ($self, $str_ref, $node) = @_; + my $name = $self->parse_expr($str_ref, {auto_quote => "(\\w+\\b (?: \\.\\w+\\b)*) $QR_AQ_SPACE \\s* $QR_COMMENTS"}); + $self->throw('parse.missing', "Missing name in THROW", $node, pos($$str_ref)) if ! $name; + my $args = $self->parse_args($str_ref, {named_at_front => 1}); return [$name, $args]; } sub play_THROW { my ($self, $ref, $node) = @_; my ($name, $args) = @$ref; - $name = $self->get_variable($name); - my @args = $args ? @{ $self->vivify_args($args) } : (); + + $name = $self->play_expr($name); + + my $named = shift @$args; + push @$args, $named if ! $self->is_empty_named_args($named); # add named args back on at end - if there are some + + my @args = $args ? map { $self->play_expr($_) } @$args : (); $self->throw($name, \@args, $node); } @@ -2186,7 +2651,7 @@ sub play_TRY { next; } next if ! $err; - my $name = $self->get_variable($node->[3]); + my $name = $self->play_expr($node->[3]); $name = '' if ! defined $name || lc($name) eq 'default'; if ($type =~ / ^ \Q$name\E \b /x && (! defined($last_found) || length($last_found) < length($name))) { # more specific wins @@ -2220,36 +2685,35 @@ sub play_TRY { sub parse_UNLESS { my $ref = $DIRECTIVES->{'IF'}->[0]->(@_); - return [ \ [ '!', $ref ], 0 ]; + return [[undef, '!', $ref], 0]; } sub play_UNLESS { return $DIRECTIVES->{'IF'}->[1]->(@_) } sub parse_USE { - my ($self, $tag_ref) = @_; + my ($self, $str_ref) = @_; my $var; - my $copy = $$tag_ref; - if (defined(my $_var = $self->parse_variable(\$copy, {auto_quote => qr{ ^ (\w+) $QR_AQ_NOTDOT }xo})) - && $copy =~ s{ ^ = >? \s* $QR_COMMENTS }{}ox) { + my $mark = pos $$str_ref; + if (defined(my $_var = $self->parse_expr($str_ref, {auto_quote => "(\\w+\\b) (?! \\.) \\s* $QR_COMMENTS"})) + && ($$str_ref =~ m{ \G = >? \s* $QR_COMMENTS }gcxo # make sure there is assignment + || ((pos($$str_ref) = $mark) && 0)) # otherwise we need to rollback + ) { $var = $_var; - $$tag_ref = $copy; } - $copy = $$tag_ref; - my $module = $self->parse_variable(\$copy, {auto_quote => qr{ ^ (\w+ (?: (?:\.|::) \w+)*) $QR_AQ_NOTDOT }xo}); - $self->throw('parse', "Missing plugin name while parsing $$tag_ref") if ! defined $module; + my $module = $self->parse_expr($str_ref, {auto_quote => "(\\w+\\b (?: (?:\\.|::) \\w+\\b)*) (?! \\.) \\s* $QR_COMMENTS"}); + $self->throw('parse', "Missing plugin name while parsing $$str_ref", undef, pos($$str_ref)) if ! defined $module; $module =~ s/\./::/g; my $args; - my $open = $copy =~ s{ ^ \( \s* $QR_COMMENTS }{}ox; - $args = $self->parse_args(\$copy); + my $open = $$str_ref =~ m{ \G \( \s* $QR_COMMENTS }gcxo; + $args = $self->parse_args($str_ref, {is_parened => $open, named_at_front => 1}); if ($open) { - $copy =~ s { ^ \) \s* $QR_COMMENTS }{}ox || $self->throw('parse.missing', "Missing close ')'"); + $$str_ref =~ m{ \G \) \s* $QR_COMMENTS }gcxo || $self->throw('parse.missing', "Missing close ')'", undef, pos($$str_ref)); } - $$tag_ref = $copy; return [$var, $module, $args]; } @@ -2262,39 +2726,45 @@ sub play_USE { my @var = map {($_, 0, '.')} split /(?:\.|::)/, $var; pop @var; # remove the trailing '.' + my $named = shift @$args; + push @$args, $named if ! $self->is_empty_named_args($named); # add named args back on at end - if there are some + ### look for a plugin_base - my $base = $self->{'PLUGIN_BASE'} || 'Template::Plugin'; # I'm not maintaining plugins - leave that to TT - my $package = $self->{'PLUGINS'}->{$module} ? $self->{'PLUGINS'}->{$module} - : $self->{'PLUGIN_FACTORY'}->{$module} ? $self->{'PLUGIN_FACTORY'}->{$module} - : "${base}::${module}"; - my $require = "$package.pm"; - $require =~ s|::|/|g; - - ### try and load the module - fall back to bare module if allowed + my $BASE = $self->{'PLUGIN_BASE'} || 'Template::Plugin'; # I'm not maintaining plugins - leave that to TT my $obj; - if ($self->{'PLUGIN_FACTORY'}->{$module} || eval {require $require}) { - my $shape = $package->load; - my $context = $self->context; - my @args = $args ? @{ $self->vivify_args($args) } : (); - $obj = $shape->new($context, @args); - } elsif (lc($module) eq 'iterator') { # use our iterator if none found (TT's works just fine) - $obj = $PACKAGE_ITERATOR->new($args ? $self->get_variable($args->[0]) : []); - } elsif (my @packages = grep {lc($package) eq lc($_)} @{ $self->list_plugins({base => $base}) }) { - foreach my $package (@packages) { - my $require = "$package.pm"; - $require =~ s|::|/|g; - eval {require $require} || next; + + foreach my $base (ref($BASE) eq 'ARRAY' ? @$BASE : $BASE) { + my $package = $self->{'PLUGINS'}->{$module} ? $self->{'PLUGINS'}->{$module} + : $self->{'PLUGIN_FACTORY'}->{$module} ? $self->{'PLUGIN_FACTORY'}->{$module} + : "${base}::${module}"; + my $require = "$package.pm"; + $require =~ s|::|/|g; + + ### try and load the module - fall back to bare module if allowed + if ($self->{'PLUGIN_FACTORY'}->{$module} || eval {require $require}) { my $shape = $package->load; my $context = $self->context; - my @args = $args ? @{ $self->vivify_args($args) } : (); + my @args = $args ? map { $self->play_expr($_) } @$args : (); $obj = $shape->new($context, @args); - } - } elsif ($self->{'LOAD_PERL'}) { - my $require = "$module.pm"; - $require =~ s|::|/|g; - if (eval {require $require}) { - my @args = $args ? @{ $self->vivify_args($args) } : (); - $obj = $module->new(@args); + } elsif (lc($module) eq 'iterator') { # use our iterator if none found (TT's works just fine) + $obj = $PACKAGE_ITERATOR->new($args ? $self->play_expr($args->[0]) : []); + } elsif (my @packages = grep {lc($package) eq lc($_)} @{ $self->list_plugins({base => $base}) }) { + foreach my $package (@packages) { + my $require = "$package.pm"; + $require =~ s|::|/|g; + eval {require $require} || next; + my $shape = $package->load; + my $context = $self->context; + my @args = $args ? map { $self->play_expr($_) } @$args : (); + $obj = $shape->new($context, @args); + } + } elsif ($self->{'LOAD_PERL'}) { + my $require = "$module.pm"; + $require =~ s|::|/|g; + if (eval {require $require}) { + my @args = $args ? map { $self->play_expr($_) } @$args : (); + $obj = $module->new(@args); + } } } if (! defined $obj) { @@ -2308,6 +2778,74 @@ sub play_USE { return; } +sub parse_VIEW { + my ($self, $str_ref) = @_; + + my $ref = $self->parse_args($str_ref, { + named_at_front => 1, + require_arg => 1, + }); + + return $ref; +} +#sub parse_VIEW { $DIRECTIVES->{'PROCESS'}->[0]->(@_) } + +sub play_VIEW { + my ($self, $ref, $node, $out_ref) = @_; + + my ($blocks, $args, $name) = @$ref; + + ### get args ready + # [[undef, '{}', 'key1', 'val1', 'key2', 'val2'], 0] + $args = $args->[0]; + my $hash = {}; + foreach (my $i = 2; $i < @$args; $i+=2) { + my $key = $args->[$i]; + my $val = $self->play_expr($args->[$i+1]); + if (ref $key) { + if (@$key == 2 && ! ref($key->[0]) && ! $key->[1]) { + $key = $key->[0]; + } else { + $self->set_variable($key, $val); + next; # what TT does + } + } + $hash->{$key} = $val; + } + + ### prepare the blocks + my $prefix = $hash->{'prefix'} || (ref($name) && @$name == 2 && ! $name->[1] && ! ref($name->[0])) ? "$name->[0]/" : ''; + foreach my $key (keys %$blocks) { + $blocks->{$key} = {name => "${prefix}${key}", _tree => $blocks->{$key}}; + } + $hash->{'blocks'} = $blocks; + + ### get the view + if (! eval { require Template::View }) { + $self->throw('view', 'Could not load Template::View library'); + } + my $view = Template::View->new($self->context, $hash) + || $self->throw('view', $Template::View::ERROR); + + ### 'play it' + my $old_view = $self->play_expr(['view', 0]); + $self->set_variable($name, $view); + $self->set_variable(['view', 0], $view); + + if ($node->[4]) { + my $out = ''; + $self->execute_tree($node->[4], \$out); + # throw away $out + } + + $self->set_variable(['view', 0], $old_view); + $view->seal; + + return ''; +} + +sub parse_WHILE { $DIRECTIVES->{'IF'}->[0]->(@_) } + sub play_WHILE { my ($self, $var, $node, $out_ref) = @_; return '' if ! defined $var; @@ -2318,7 +2856,7 @@ sub play_WHILE { my $count = $WHILE_MAX; while (--$count > 0) { - $self->get_variable($var) || last; + $self->play_expr($var) || last; ### execute the sub tree eval { $self->execute_tree($sub_tree, $out_ref) }; @@ -2335,21 +2873,21 @@ sub play_WHILE { return undef; } -sub parse_WRAPPER { $DIRECTIVES->{'INCLUDE'}->[0]->(@_) } +sub parse_WRAPPER { $DIRECTIVES->{'PROCESS'}->[0]->(@_) } sub play_WRAPPER { - my ($self, $var, $node, $out_ref) = @_; + my ($self, $args, $node, $out_ref) = @_; my $sub_tree = $node->[4] || return; - my ($names, $args) = @$var; + my ($named, @files) = @$args; my $out = ''; $self->execute_tree($sub_tree, \$out); - foreach my $name (reverse @$names) { + foreach my $name (reverse @files) { local $self->{'_vars'}->{'content'} = $out; $out = ''; - $DIRECTIVES->{'INCLUDE'}->[1]->($self, [[$name], $args], $node, \$out); + $DIRECTIVES->{'INCLUDE'}->[1]->($self, [$named, $name], $node, \$out); } $$out_ref .= $out; @@ -2376,7 +2914,7 @@ sub include_filename { my $paths = $self->{'INCLUDE_PATHS'} ||= do { # TT does this everytime a file is looked up - we are going to do it just in time - the first time - my $paths = $self->{'INCLUDE_PATH'} || $self->throw('file', "INCLUDE_PATH not set"); + my $paths = $self->{'INCLUDE_PATH'} || []; $paths = $paths->() if UNIVERSAL::isa($paths, 'CODE'); $paths = $self->split_paths($paths) if ! UNIVERSAL::isa($paths, 'ARRAY'); $paths; # return of the do @@ -2472,7 +3010,6 @@ sub process { my $var2 = $self->{'VARIABLES'} || $self->{'PRE_DEFINE'} || {}; $var1->{'global'} ||= {}; # allow for the "global" namespace - that continues in between processing my $copy = {%$var2, %$var1, %$swap}; - local $copy->{'template'}; local $self->{'BLOCKS'} = $blocks = {%$blocks}; # localize blocks - but save a copy to possibly restore @@ -2492,10 +3029,10 @@ sub process { if (exists $self->{'PROCESS'}) { ### load the meta data for the top document my $doc = $self->load_parsed_tree($content) || {}; - my $meta = ($doc->{'_tree'} && ref($doc->{'_tree'}->[0]) && $doc->{'_tree'}->[0]->[0] eq 'METADEF') + my $meta = ($doc->{'_tree'} && ref($doc->{'_tree'}->[0]) && $doc->{'_tree'}->[0]->[0] eq 'META') ? $doc->{'_tree'}->[0]->[3] : {}; - $copy->{'template'} = $doc; + local $self->{'_template'} = $doc; @{ $doc }{keys %$meta} = values %$meta; ### process any other templates @@ -2598,7 +3135,9 @@ sub DEBUG { ###----------------------------------------------------------------### sub exception { - my ($self, $type, $info, $node) = @_; + my $self = shift; + my $type = shift; + my $info = shift; return $type if ref($type) =~ /Template::Exception$/; if (ref($info) eq 'ARRAY') { my $hash = ref($info->[-1]) eq 'HASH' ? pop(@$info) : {}; @@ -2614,7 +3153,7 @@ sub exception { $type = 'undef'; } } - return $PACKAGE_EXCEPTION->new($type, $info, $node); + return $PACKAGE_EXCEPTION->new($type, $info, @_); } sub throw { die shift->exception(@_) } @@ -2665,20 +3204,6 @@ sub list_plugins { }; } -### get a copy of self without circular refs for use in closures -sub weak_copy { - my $self = shift; - my $self_copy; - if (eval { require Scalar::Util } - && defined &Scalar::Util::weaken) { - $self_copy = $self; - Scalar::Util::weaken($self_copy); - } else { - $self_copy = bless {%$self}, ref($self); # hackish way to avoid circular refs on old perls (pre 5.8) - } - return $self_copy; -} - sub debug_node { my ($self, $node) = @_; my $info = $self->node_info($node); @@ -2689,9 +3214,9 @@ sub debug_node { sub node_info { my ($self, $node) = @_; - my $doc = $self->{'_vars'}->{'component'}; + my $doc = $self->{'_component'}; my $i = $node->[1]; - my $j = $node->[2] || return ''; # METADEF can be 0 + my $j = $node->[2] || return ''; # META can be 0 $doc->{'_content'} ||= do { my $s = $self->slurp($doc->{'_filename'}) ; \$s }; my $s = substr(${ $doc->{'_content'} }, $i, $j - $i); $s =~ s/^\s+//; @@ -2704,9 +3229,11 @@ sub node_info { } sub get_line_number_by_index { - my ($self, $doc, $index) = @_; + my ($self, $doc, $index, $include_char) = @_; + return 1 if $index <= 0; + ### get the line offsets for the doc - my $lines = $doc->{'line_offsets'} ||= do { + my $lines = $doc->{'_line_offsets'} ||= do { $doc->{'_content'} ||= do { my $s = $self->slurp($doc->{'_filename'}) ; \$s }; my $i = 0; my @lines = (0); @@ -2717,15 +3244,20 @@ sub get_line_number_by_index { } \@lines; }; + ### binary search them (this is fast even on big docs) - return $#$lines + 1 if $index > $lines->[-1]; my ($i, $j) = (0, $#$lines); - while (1) { - return $i + 1 if abs($i - $j) <= 1; - my $k = int(($i + $j) / 2); - $j = $k if $lines->[$k] >= $index; - $i = $k if $lines->[$k] <= $index; + if ($index > $lines->[-1]) { + $i = $j; + } else { + while (1) { + last if abs($i - $j) <= 1; + my $k = int(($i + $j) / 2); + $j = $k if $lines->[$k] >= $index; + $i = $k if $lines->[$k] <= $index; + } } + return $include_char ? ($i + 1, $index - $lines->[$i]) : $i + 1; } ###----------------------------------------------------------------### @@ -2745,6 +3277,33 @@ sub define_vmethod { return 1; } +sub vmethod_fmt_scalar { + my $str = shift; $str = '' if ! defined $str; + my $pat = shift; $pat = '%s' if ! defined $pat; + local $^W; + return @_ ? sprintf($pat, $_[0], $str) + : sprintf($pat, $str); +} + +sub vmethod_fmt_list { + my $ref = shift || return ''; + my $pat = shift; $pat = '%s' if ! defined $pat; + my $sep = shift; $sep = ' ' if ! defined $sep; + local $^W; + return @_ ? join($sep, map {sprintf $pat, $_[0], $_} @$ref) + : join($sep, map {sprintf $pat, $_} @$ref); +} + +sub vmethod_fmt_hash { + my $ref = shift || return ''; + my $pat = shift; $pat = "%s\t%s" if ! defined $pat; + my $sep = shift; $sep = "\n" if ! defined $sep; + local $^W; + return ! @_ ? join($sep, map {sprintf $pat, $_, $ref->{$_}} sort keys %$ref) + : @_ == 1 ? join($sep, map {sprintf $pat, $_[0], $_, $ref->{$_}} sort keys %$ref) # don't get to pick - it applies to the key + : join($sep, map {sprintf $pat, $_[0], $_, $_[1], $ref->{$_}} sort keys %$ref); +} + sub vmethod_chunk { my $str = shift; my $size = shift || 1; @@ -2770,14 +3329,25 @@ sub vmethod_indent { sub vmethod_format { my $str = shift; $str = '' if ! defined $str; my $pat = shift; $pat = '%s' if ! defined $pat; - return join "\n", map{ sprintf $pat, $_ } split(/\n/, $str); + if (@_) { + return join "\n", map{ sprintf $pat, $_[0], $_ } split(/\n/, $str); + } else { + return join "\n", map{ sprintf $pat, $_ } split(/\n/, $str); + } +} + +sub vmethod_list_hash { + my ($hash, $what) = @_; + $what = 'pairs' if ! $what || $what !~ /^(keys|values|each|pairs)$/; + return $HASH_OPS->{$what}->($hash); } + sub vmethod_match { my ($str, $pat, $global) = @_; return [] if ! defined $str || ! defined $pat; my @res = $global ? ($str =~ /$pat/g) : ($str =~ /$pat/); - return (@res >= 2) ? \@res : (@res == 1) ? $res[0] : ''; + return @res ? \@res : ''; } sub vmethod_nsort { @@ -2789,9 +3359,18 @@ sub vmethod_nsort { : [sort {$a <=> $b} @$list]; } +sub vmethod_pick { + my $ref = shift; + no warnings; + my $n = int(shift); + $n = 1 if $n < 1; + my @ind = map { $ref->[ rand @$ref ] } 1 .. $n; + return $n == 1 ? $ind[0] : \@ind; +} + sub vmethod_repeat { my ($str, $n, $join) = @_; - return if ! length $str; + return '' if ! defined $str || ! length $str; $n = 1 if ! defined($n) || ! length $n; $join = '' if ! defined $join; return join $join, ($str) x $n; @@ -2836,15 +3415,27 @@ sub vmethod_splice { @replace = @{ $replace[0] } if @replace == 1 && ref $replace[0] eq 'ARRAY'; if (defined $len) { return [splice @$ref, $i || 0, $len, @replace]; + } elsif (defined $i) { + return [splice @$ref, $i]; } else { - return [splice @$ref, $i || 0]; + return [splice @$ref]; } } sub vmethod_split { - my ($str, $pat, @args) = @_; + my ($str, $pat, $lim) = @_; $str = '' if ! defined $str; - return defined $pat ? [split $pat, $str, @args] : [split ' ', $str, @args]; + if (defined $lim) { return defined $pat ? [split $pat, $str, $lim] : [split ' ', $str, $lim] } + else { return defined $pat ? [split $pat, $str ] : [split ' ', $str ] } +} + +sub vmethod_substr { + my ($str, $i, $len, $replace) = @_; + $i ||= 0; + return substr($str, $i) if ! defined $len; + return substr($str, $i, $len) if ! defined $replace; + substr($str, $i, $len, $replace); + return $str; } sub vmethod_uri { @@ -2854,9 +3445,24 @@ sub vmethod_uri { return $str; } +sub vmethod_url { + my $str = shift; + utf8::encode($str) if defined &utf8::encode; + $str =~ s/([^;\/?:@&=+\$,A-Za-z0-9\-_.!~*\'()])/sprintf('%%%02X', ord($1))/eg; + return $str; +} + sub filter_eval { my $context = shift; + return sub { + ### prevent recursion + my $t = $context->_template; + local $t->{'_eval_recurse'} = $t->{'_eval_recurse'} || 0; + $context->throw('eval_recurse', "MAX_EVAL_RECURSE $MAX_EVAL_RECURSE reached") + if ++$t->{'_eval_recurse'} > ($t->{'MAX_EVAL_RECURSE'} || $MAX_EVAL_RECURSE); + + my $text = shift; return $context->process(\$text); }; @@ -2892,7 +3498,14 @@ sub dump_parse { my $obj = UNIVERSAL::isa($_[0], __PACKAGE__) ? shift : __PACKAGE__->new; my $str = shift; require Data::Dumper; - return Data::Dumper::Dumper($obj->parse_variable(\$str)); + return Data::Dumper::Dumper($obj->parse_tree(\$str)); +} + +sub dump_parse_expr { + my $obj = UNIVERSAL::isa($_[0], __PACKAGE__) ? shift : __PACKAGE__->new; + my $str = shift; + require Data::Dumper; + return Data::Dumper::Dumper($obj->parse_expr(\$str)); } ###----------------------------------------------------------------### @@ -2905,8 +3518,8 @@ use overload fallback => 1; sub new { - my ($class, $type, $info, $node, $pos, $str_ref) = @_; - return bless [$type, $info, $node, $pos, $str_ref], $class; + my ($class, $type, $info, $node, $pos, $doc) = @_; + return bless [$type, $info, $node, $pos, $doc], $class; } sub type { shift->[0] } @@ -2915,25 +3528,34 @@ sub info { shift->[1] } sub node { my $self = shift; - $self->[2] = shift if $#_ == 0; + $self->[2] = shift if @_; $self->[2]; } -sub offset { shift->[3] || 0 } +sub offset { + my $self = shift; + $self->[3] = shift if @_; + $self->[3]; +} sub doc { my $self = shift; - $self->[4] = shift if $#_ == 0; + $self->[4] = shift if @_; $self->[4]; } sub as_string { my $self = shift; - my $msg = $self->type .' error - '. $self->info; - if (my $node = $self->node) { -# $msg .= " (In tag $node->[0] starting at char ".($node->[1] + $self->offset).")"; + if ($self->type =~ /^parse/) { + if (my $doc = $self->doc) { + my ($line, $char) = CGI::Ex::Template->get_line_number_by_index($doc, $self->offset, 'include_char'); + return $self->type ." error - $doc->{'name'} line $line char $char: ". $self->info; + } else { + return $self->type .' error - '. $self->info .' (At char '. $self->offset .')'; + } + } else { + return $self->type .' error - '. $self->info; } - return $msg; } ###----------------------------------------------------------------### @@ -3001,6 +3623,11 @@ use vars qw($AUTOLOAD); sub _template { shift->{'_template'} || die "Missing _template" } +sub template { + my ($self, $name) = @_; + return $self->_template->{'BLOCKS'}->{$name} || $self->_template->load_parsed_tree($name); +} + sub config { shift->_template } sub stash { @@ -3015,21 +3642,29 @@ sub eval_perl { shift->_template->{'EVAL_PERL'} } sub process { my $self = shift; my $ref = shift; - my $vars = $self->_template->_vars; + my $args = shift || {}; + + $self->_template->set_variable($_, $args->{$_}) for keys %$args; + my $out = ''; - $self->_template->_process($ref, $vars, \$out); + $self->_template->_process($ref, $self->_template->_vars, \$out); return $out; } sub include { my $self = shift; - my $file = shift; + my $ref = shift; my $args = shift || {}; - $self->_template->set_variable($_, $args->{$_}) for keys %$args; + my $t = $self->_template; + + my $swap = $t->{'_vars'}; + local $t->{'_vars'} = {%$swap}; + + $t->set_variable($_, $args->{$_}) for keys %$args; my $out = ''; # have temp item to allow clear to correctly clear - eval { $self->_template->_process($file, $self->{'_vars'}, \$out) }; + eval { $t->_process($ref, $t->_vars, \$out) }; if (my $err = $@) { die $err if ref($err) !~ /Template::Exception$/ || $err->type !~ /return/; } @@ -3101,16 +3736,16 @@ sub get { my ($self, $var) = @_; if (! ref $var) { if ($var =~ /^\w+$/) { $var = [$var, 0] } - else { $var = $self->_template->parse_variable(\$var, {no_dots => 1}) } + else { $var = $self->_template->parse_expr(\$var, {no_dots => 1}) } } - return $self->_template->get_variable($var, {no_dots => 1}); + return $self->_template->play_expr($var, {no_dots => 1}); } sub set { my ($self, $var, $val) = @_; if (! ref $var) { if ($var =~ /^\w+$/) { $var = [$var, 0] } - else { $var = $self->_template->parse_variable(\$var, {no_dots => 1}) } + else { $var = $self->_template->parse_expr(\$var, {no_dots => 1}) } } $self->_template->set_variable($var, $val, {no_dots => 1}); return $val;