1Template::Alloy::Parse(U3s)er Contributed Perl DocumentatTieomnplate::Alloy::Parse(3)
2
3
4

NAME

6       Template::Alloy::Parse - Common parsing role for creating AST from tem‐
7       plates
8

DESCRIPTION

10       The Template::Alloy::Parse role is reponsible for storing the majority
11       of directive parsing code, as well as for delegating to the TT, HTE,
12       Tmpl, and Velocity roles for finding variables and directives.
13

ROLE METHODS

15       parse_tree
16           Used by load_tree.  This is the main grammar engine of the program.
17           It delegates to the syntax found in $self->{'SYNTAX'} (defaults to
18           'alloy') and calls the function found in the $SYNTAX hashref.  The
19           majority of these syntaxes use methods found in the $DIRECTIVES
20           hashref to parse different DIRECTIVE types for each particular syn‐
21           tax.
22
23           A template that looked like the following:
24
25               Foo
26               [%- GET foo -%]
27               [%- GET bar -%]
28               Bar
29
30           would parse to the following AST:
31
32               [
33                   'Foo',
34                   ['GET', 6, 15, ['foo', 0]],
35                   ['GET', 22, 31, ['bar', 0]],
36                   'Bar',
37               ]
38
39           The "GET" words represent the directive used.  The 6, 15 represent
40           the beginning and ending characters of the directive in the docu‐
41           ment.  The remaining items are the variables necessary for running
42           the particular directive.
43
44       parse_expr
45           Used to parse a variable, an expression, a literal string, or a
46           number.  It returns a parsed variable tree.  Samples of parsed
47           variables can be found in the VARIABLE PARSE TREE section.
48
49               my $str = "1 + 2 * 3";
50               my $ast = $self->parse_expr(\$str);
51               # $ast looks like [[undef, '+', 1, [[undef, '*', 2, 3], 0]], 0]
52
53       "parse_args"
54           Allow for the multitudinous ways that TT parses arguments.  This
55           allows for positional as well as named arguments.  Named arguments
56           can be separated with a "=" or "=>", and positional arguments
57           should be separated by " " or ",".  This only returns an array of
58           parsed variables.  To get the actual values, you must call
59           play_expr on each value.
60
61       "dump_parse_tree"
62           This method allows for returning a string of perl code representing
63           the AST of the parsed tree.
64
65           It is mainly used for testing.
66
67       "dump_parse_expr"
68           This method allows for returning a Data::Dumper dump of a parsed
69           variable.  It is mainly used for testing.
70
71       "parse_*"
72           Methods by these names are used by parse_tree to parse the tem‐
73           plate.  These are the grammar.  They are used by all of the various
74           template syntaxes Unless otherwise mentioned, these methods are not
75           exposed via the role.
76

VARIABLE PARSE TREE

78       Template::Alloy parses templates into an tree of operations (an AST or
79       abstract syntax tree).  Even variable access is parsed into a tree.
80       This is done in a manner somewhat similar to the way that TT operates
81       except that nested variables such as foo.bar⎪baz contain the '.' or '⎪'
82       in between each name level.  Operators are parsed and stored as part of
83       the variable (it may be more appropriate to say we are parsing a term
84       or an expression).
85
86       The following table shows a variable or expression and the correspond‐
87       ing parsed tree (this is what the parse_expr method would return).
88
89           one                [ 'one',  0 ]
90           one()              [ 'one',  [] ]
91           one.two            [ 'one',  0, '.', 'two',  0 ]
92           one⎪two            [ 'one',  0, '⎪', 'two',  0 ]
93           one.$two           [ 'one',  0, '.', ['two', 0 ], 0 ]
94           one(two)           [ 'one',  [ ['two', 0] ] ]
95           one.${two().three} [ 'one',  0, '.', ['two', [], '.', 'three', 0], 0]
96           2.34               2.34
97           "one"              "one"
98           1 + 2              [ [ undef, '+', 1, 2 ], 0]
99           a + b              [ [ undef, '+', ['a', 0], ['b', 0] ], 0 ]
100           "one"⎪length       [ [ undef, '~', "one" ], 0, '⎪', 'length', 0 ]
101           "one $a two"       [ [ undef, '~', 'one ', ['a', 0], ' two' ], 0 ]
102           [0, 1, 2]          [ [ undef, '[]', 0, 1, 2 ], 0 ]
103           [0, 1, 2].size     [ [ undef, '[]', 0, 1, 2 ], 0, '.', 'size', 0 ]
104           ['a', a, $a ]      [ [ undef, '[]', 'a', ['a', 0], [['a', 0], 0] ], 0]
105           {a  => 'b'}        [ [ undef, '{}', 'a', 'b' ], 0 ]
106           {a  => 'b'}.size   [ [ undef, '{}', 'a', 'b' ], 0, '.', 'size', 0 ]
107           {$a => b}          [ [ undef, '{}', ['a', 0], ['b', 0] ], 0 ]
108           a * (b + c)        [ [ undef, '*', ['a', 0], [ [undef, '+', ['b', 0], ['c', 0]], 0 ]], 0 ]
109           (a + b)            [ [ undef, '+', ['a', 0], ['b', 0] ]], 0 ]
110           (a + b) * c        [ [ undef, '*', [ [undef, '+', ['a', 0], ['b', 0] ], 0 ], ['c', 0] ], 0 ]
111           a ? b : c          [ [ undef, '?', ['a', 0], ['b', 0], ['c', 0] ], 0 ]
112           a ⎪⎪ b ⎪⎪ c        [ [ undef, '⎪⎪', ['a', 0], [ [undef, '⎪⎪', ['b', 0], ['c', 0] ], 0 ] ], 0 ]
113           ! a                [ [ undef, '!', ['a', 0] ], 0 ]
114
115       Some notes on the parsing.
116
117           Operators are parsed as part of the variable and become part of the variable tree.
118
119           Operators are stored in the variable tree using an operator identity array which
120           contains undef as the first value, the operator, and the operator arguments.  This
121           allows for quickly descending the parsed variable tree and determining that the next
122           node is an operator.
123
124           Parenthesis () can be used at any point in an expression to disambiguate precedence.
125
126           "Variables" that appear to be literal strings or literal numbers
127           are returned as the literal (no operator tree).
128
129       The following perl can be typed at the command line to view the parsed
130       variable tree:
131
132           perl -e 'use Template::Alloy; print Template::Alloy->dump_parse_expr("foo.bar + 2")."\n"'
133
134       Also the following can be included in a template to view the output in
135       a template:
136
137           [% USE cet = Template::Alloy %]
138           [%~ cet.dump_parse_expr('foo.bar + 2').replace('\s+', ' ') %]
139

AUTHOR

141       Paul Seamons <paul at seamons dot com>
142

LICENSE

144       This module may be distributed under the same terms as Perl itself.
145
146
147
148perl v5.8.8                       2007-07-24         Template::Alloy::Parse(3)
Impressum