root/tlate/TemplateLanguage.txt

Revision 7d800a4197bd506752f27503b3c2aac839372310, 5.6 KB (checked in by Antti-Juhani Kaijanaho <antti-juhani@…>, 3 years ago)

[tlate::tlate] Support one-line comments.

Signed-off-by: Antti-Juhani Kaijanaho <antti-juhani@…>

  • Property mode set to 100644
Line 
1                         Template data model
2                         -------------------
3
4A template is always interpreted in the context of a data model,
5provided by the part of Alue that is using the template.
6
7A data model consists of a set of ATTRIBUTES.  An attribute has a name
8(which follows the lexical syntax of an identifier, as specified
9below) and a value.
10
11There are several kinds of attribute values:
12
13  – literal strings
14
15  - a list of values
16
17  - a structured value, which is simply a data (sub)model
18
19  - a definition closure (described later)
20
21An attribute value may also be specifically missing.
22
23Typically, the part of Alue that uses the template defines a data
24model schema, which describes the names and value kinds (including the
25schemas of any submodels) of attributes.
26
27
28                        Lexical considerations
29                        ----------------------
30
31The template language is modeful.  The two modes are LITERAL and
32DIRECTIVE.  All template files start in the LITERAL mode.
33
34In either mode, the dollar sign ($) may be used to switch modes.
35
36In either mode, a hash sign (#) starts a comment that extends to the
37end of the line.  The end-of-line is considered a part of the comment.
38
39In LITERAL mode, most characters (including whitespace) stand for
40themselves and are copied verbatim to the output.  The exceptions are
41the curly brackets ({}), which are used for grouping, and the
42mode-switching dollar sign.
43
44In the DIRECTIVE mode, input is tokenized in the manner customary for
45computer languages:
46
47  - Whitespace is ignored, except so far as it is used to separate tokens.
48
49  - Punctuation tokens are the ordinary parentheses (()), and the comma (,).
50
51  – Names are nonempty sequences of the ASCII letters ('a' to 'z' in
52    either case), ASCII decimal digits ('0' to '9'), the dash (-), the
53    period (.) and the underscore (_).
54
55  – The following names are reserved for use as syntactic keywords:
56    def, else, for, if, in, include.  Other names are called
57    identifiers.
58
59
60                           Template syntax
61                           ---------------
62
63Curly brackets (in LITERAL mode) are used to delimit a TEMPLATE LIST.
64A template list consists of literal text and directives.  When
65occurring alone, a template list is processed as if the surrounding
66curly brackets were missing, but in certain directives a template list
67is saved (uninterpreted) for later use.
68
69An ATTRIBUTE REFERENCE consists of an identifier by itself.  Since
70identifiers are only recognized in DIRECTIVE mode, an attribute
71reference is most commonly surrounded by dollar signs ($foo$).  If the
72value of the attribute is a literal string, that string is outputted.
73
74  For example, if there is an attribute called "foo" whose value is
75  the literal string "cat", then $foo$ will output "cat".
76
77A LIST ITERATION consists of an identifier followed by a template
78list.  If the value of the attribute is a list (or a structured value,
79which is treated as a one-element list), then the template list will
80be interpreted once for each element of the value list.  The element
81will be accessible inside the template list as the attribute named
82"cursor", and if the element is a structured value, all its attributes
83are also available inside the list.
84
85  For example, if the attribute called "nephews" contains as its value
86  a list of the literal strings "Huey", "Dewey", and "Louie", then
87  $nephews${$cursor$! } will output "Huey! Dewey! Louie! ".  Note that
88  there must be no whitespace in the "${" pair.
89
90A CONDITIONAL consists of the keyword "if" followed by an identifier,
91followed by a template list, and optionally followed by the keyword
92"else" and another template list.  If the identifier names an
93attribute that has a value, then the first template list is
94interpreted.  if there is no attribute named by the identifier, the
95the second template list (if any) is interpreted.
96
97  For example, consider $if foo${yes}$else${no}$.  If there is an
98  attribute called "foo", then "yes" is outputted; otherwise, "no" is
99  outputted.
100
101  Note that there must not be any spaces in the "${" and "}$" pairs.
102  However, there may be whitespace between the dollar signs and the
103  keywords (thus, "$ if" is allowed).
104
105A DEFINITION consists of
106    - the keyword "def", followed by
107    - an identifier (the definee), followed by
108    - an ordinary opening parenthesis "(", followed by
109    - a comma-separated list of identifiers (the parameters), followed by
110    - an ordinary closing parenthesis ")", followed by
111    - a template list (the body).
112The definition does not output anything.  However, the definee may
113subsequently be used in an invocation, discussed below.  A definition
114may be (and usually is) recursive.
115
116An INVOCATION consists of
117    - an identifier (the invokee), followed by
118    - an ordinary opening parentesis "(", followed by
119    - a comma-separated list of identifiers (the arguments), followed by
120    - an ordinary closing parenthesis ")".
121If there is no usable definee that matches the invokee, or the number
122of parameters disagrees with the number of arguments, the invocation
123outputs nothing.  Otherwise, the body of the corresponding definition
124will be interpreted, with each parameter standing for the
125corresponding argument.
126
127  For example, consider the following template:
128    $def twice(a)${$a$$a$} $twice(foo)$
129  Now, if "foo" is an attribute having as its value the literal string
130  "me", then this template outputs " meme".
131
132  Note: as usual, the sequences "${" and "}$" may not contain any
133  whitespace.  The one space before "meme" in the output results from
134  the space separating the definition and the invocation (which is not
135  mandatory by no means).
Note: See TracBrowser for help on using the browser.