Template::Alloy::Parse(3pm) | User Contributed Perl Documentation | Template::Alloy::Parse(3pm) |
Template::Alloy::Parse - Common parsing role for creating AST from templates
The Template::Alloy::Parse role is responsible for storing the majority of directive parsing code, as well as for delegating to the TT, HTE, Tmpl, and Velocity roles for finding variables and directives.
A template that looked like the following:
Foo [%- GET foo -%] [%- GET bar -%] Bar
would parse to the following AST:
[ 'Foo', ['GET', 6, 15, ['foo', 0]], ['GET', 22, 31, ['bar', 0]], 'Bar', ]
The "GET" words represent the directive used. The 6, 15 represent the beginning and ending characters of the directive in the document. The remaining items are the variables necessary for running the particular directive.
my $str = "1 + 2 * 3"; my $ast = $self->parse_expr(\$str); # $ast looks like [[undef, '+', 1, [[undef, '*', 2, 3], 0]], 0]
It is mainly used for testing.
Template::Alloy parses templates into an tree of operations (an AST or abstract syntax tree). Even variable access is parsed into a tree. This is done in a manner somewhat similar to the way that TT operates except that nested variables such as foo.bar|baz contain the '.' or '|' in between each name level. Operators are parsed and stored as part of the variable (it may be more appropriate to say we are parsing a term or an expression).
The following table shows a variable or expression and the corresponding parsed tree (this is what the parse_expr method would return).
one [ 'one', 0 ] one() [ 'one', [] ] one.two [ 'one', 0, '.', 'two', 0 ] one|two [ 'one', 0, '|', 'two', 0 ] one.$two [ 'one', 0, '.', ['two', 0 ], 0 ] one(two) [ 'one', [ ['two', 0] ] ] one.${two().three} [ 'one', 0, '.', ['two', [], '.', 'three', 0], 0] 2.34 2.34 "one" "one" 1 + 2 [ [ undef, '+', 1, 2 ], 0] a + b [ [ undef, '+', ['a', 0], ['b', 0] ], 0 ] "one"|length [ [ undef, '~', "one" ], 0, '|', 'length', 0 ] "one $a two" [ [ undef, '~', 'one ', ['a', 0], ' two' ], 0 ] [0, 1, 2] [ [ undef, '[]', 0, 1, 2 ], 0 ] [0, 1, 2].size [ [ undef, '[]', 0, 1, 2 ], 0, '.', 'size', 0 ] ['a', a, $a ] [ [ undef, '[]', 'a', ['a', 0], [['a', 0], 0] ], 0] {a => 'b'} [ [ undef, '{}', 'a', 'b' ], 0 ] {a => 'b'}.size [ [ undef, '{}', 'a', 'b' ], 0, '.', 'size', 0 ] {$a => b} [ [ undef, '{}', ['a', 0], ['b', 0] ], 0 ] a * (b + c) [ [ undef, '*', ['a', 0], [ [undef, '+', ['b', 0], ['c', 0]], 0 ]], 0 ] (a + b) [ [ undef, '+', ['a', 0], ['b', 0] ]], 0 ] (a + b) * c [ [ undef, '*', [ [undef, '+', ['a', 0], ['b', 0] ], 0 ], ['c', 0] ], 0 ] a ? b : c [ [ undef, '?', ['a', 0], ['b', 0], ['c', 0] ], 0 ] a || b || c [ [ undef, '||', ['a', 0], [ [undef, '||', ['b', 0], ['c', 0] ], 0 ] ], 0 ] ! a [ [ undef, '!', ['a', 0] ], 0 ]
Some notes on the parsing.
Operators are parsed as part of the variable and become part of the variable tree. Operators are stored in the variable tree using an operator identity array which contains undef as the first value, the operator, and the operator arguments. This allows for quickly descending the parsed variable tree and determining that the next node is an operator. Parenthesis () can be used at any point in an expression to disambiguate precedence. "Variables" that appear to be literal strings or literal numbers are returned as the literal (no operator tree).
The following perl can be typed at the command line to view the parsed variable tree:
perl -e 'use Template::Alloy; print Template::Alloy->dump_parse_expr("foo.bar + 2")."\n"'
Also the following can be included in a template to view the output in a template:
[% USE cet = Template::Alloy %] [%~ cet.dump_parse_expr('foo.bar + 2').replace('\s+', ' ') %]
Paul Seamons <paul@seamons.com>
This module may be distributed under the same terms as Perl itself.
2022-10-16 | perl v5.36.0 |