1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
<?php
/*
* Template.php - expression template parser
*
* Copyright (c) 2013 Elmar Ludwig
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*/
namespace exTpl;
require_once 'Scanner.php';
require_once 'Node.php';
/**
* The Template class is the only externally visible API of this
* template implementation. It can be used to create and render
* template objects.
*/
class Template
{
private static $tag_start = '{';
private static $tag_end = '}';
private $template;
/**
* Sets the delimiter strings used for the template tags, the
* default delimiters are: $tag_start = '{', $tag_end = '}'.
*
* @param string $tag_start tag start marker
* @param string $tag_end tag end marker
*/
public static function setTagMarkers($tag_start, $tag_end)
{
self::$tag_start = $tag_start;
self::$tag_end = $tag_end;
}
/**
* Initializes a new Template instance from the given string.
*
* @param string $string template text
*/
public function __construct($string)
{
$this->template = new ArrayNode();
self::parseTemplate($this->template, $string, 0);
}
/**
* Renders the template to a string using the given array of
* bindings to resolve symbol references inside the template.
*
* @param array $bindings symbol table
*
* @return string string representation of the template
*/
public function render(array $bindings)
{
return $this->template->render(new Context($bindings));
}
/**
* Skips tokens until the end of the current tag is reached.
*
* @param string $string template text
* @param int $pos offset in string
*
* @return int new offset in the string
*/
private static function skipTokens($string, $pos)
{
for ($len = strlen($string); $pos < $len &&
substr_compare($string, self::$tag_end, $pos, strlen(self::$tag_end)); ++$pos) {
$chr = $string[$pos];
if ($chr === '"' || $chr === "'") {
while (++$pos < $len && $string[$pos] !== $chr) {
if ($string[$pos] === '\\') {
++$pos;
}
}
}
}
return $pos;
}
/**
* Parses a template string into a template node tree, starting
* at the specified offset. All created nodes are added to the
* given sequence node.
*
* @param ArrayNode $node template node to build
* @param string $string string to parse
* @param int $pos offset in string
*
* @return int new offset in the string
*/
private static function parseTemplate(ArrayNode $node, $string, $pos)
{
$len = strlen($string);
while ($pos < $len) {
$next_pos = strpos($string, self::$tag_start, $pos);
if ($next_pos === false) {
$child = new TextNode(substr($string, $pos));
$node->addChild($child);
break;
}
if ($next_pos > $pos) {
$child = new TextNode(substr($string, $pos, $next_pos - $pos));
$node->addChild($child);
}
$pos = $next_pos + strlen(self::$tag_start);
$next_pos = self::skipTokens($string, $pos);
$scanner = new Scanner(substr($string, $pos, $next_pos - $pos));
$pos = $next_pos + strlen(self::$tag_end);
switch ($scanner->nextToken()) {
case T_FOREACH:
$scanner->nextToken();
$child = new IteratorNode(self::parseExpr($scanner));
$pos = self::parseTemplate($child, $string, $pos);
$node->addChild($child);
break;
case T_ENDFOREACH:
return $pos;
case T_IF:
$scanner->nextToken();
$child = new ConditionNode(self::parseExpr($scanner));
$pos = self::parseTemplate($child, $string, $pos);
$node->addChild($child);
break;
case T_ELSEIF:
$scanner->nextToken();
$child = new ConditionNode(self::parseExpr($scanner));
$node->addElse();
$node->addChild($child);
return self::parseTemplate($child, $string, $pos);
case T_ELSE:
$scanner->nextToken();
$node->addElse();
break;
case T_ENDIF:
return $pos;
default:
$child = new ExpressionNode(self::parseExpr($scanner));
$node->addChild($child);
}
if ($scanner->tokenType() !== false) {
throw new TemplateParserException('syntax error', $scanner);
}
}
return $pos;
}
/**
* value: NUMBER | STRING | SYMBOL | '(' expr ')'
*/
private static function parseValue(Scanner $scanner)
{
switch ($scanner->tokenType()) {
case T_CONSTANT_ENCAPSED_STRING:
case T_DNUMBER:
case T_LNUMBER:
$result = new ConstantExpression($scanner->tokenValue());
break;
case T_STRING:
$result = new SymbolExpression($scanner->tokenValue());
break;
case '(':
$scanner->nextToken();
$result = self::parseExpr($scanner);
if ($scanner->tokenType() !== ')') {
throw new TemplateParserException('missing ")"', $scanner);
}
break;
default:
throw new TemplateParserException('syntax error', $scanner);
}
$scanner->nextToken();
return $result;
}
/**
* index: value | index '[' expr ']' | index '.' SYMBOL
*/
private static function parseIndex(Scanner $scanner)
{
$result = self::parseValue($scanner);
$type = $scanner->tokenType();
while ($type === '[' || $type === '.') {
$scanner->nextToken();
if ($type === '[') {
$expr = self::parseExpr($scanner);
if ($scanner->tokenType() !== ']') {
throw new TemplateParserException('missing "]"', $scanner);
}
} else if ($scanner->tokenType() === T_STRING) {
$expr = new ConstantExpression($scanner->tokenValue());
} else {
throw new TemplateParserException('symbol expected', $scanner);
}
$scanner->nextToken();
$result = new IndexExpression($result, $expr, $type);
$type = $scanner->tokenType();
}
return $result;
}
/**
* sign: '!' sign | '+' sign | '-' sign | index
*/
private static function parseSign(Scanner $scanner)
{
switch ($scanner->tokenType()) {
case '!':
$scanner->nextToken();
$result = new NotExpression(self::parseSign($scanner));
break;
case '+':
$scanner->nextToken();
$result = self::parseSign($scanner);
break;
case '-':
$scanner->nextToken();
$result = new MinusExpression(self::parseSign($scanner));
break;
default:
$result = self::parseIndex($scanner);
}
return $result;
}
/**
* product: sign | product '*' sign | product '/' sign | product '%' sign
*/
private static function parseProduct(Scanner $scanner)
{
$result = self::parseSign($scanner);
$type = $scanner->tokenType();
while ($type === '*' || $type === '/' || $type === '%') {
$scanner->nextToken();
$result = new ArithExpression($result, self::parseSign($scanner), $type);
$type = $scanner->tokenType();
}
return $result;
}
/**
* sum: product | sum '+' product | sum '-' product | sum '~' product
*/
private static function parseSum(Scanner $scanner)
{
$result = self::parseProduct($scanner);
$type = $scanner->tokenType();
while ($type === '+' || $type === '-' || $type === '~') {
$scanner->nextToken();
$result = new ArithExpression($result, self::parseProduct($scanner), $type);
$type = $scanner->tokenType();
}
return $result;
}
/**
* lt_gt: sum | lt_gt '<' concat | lt_gt IS_SMALLER_OR_EQUAL concat
* | lt_gt '>' concat | lt_gt IS_GREATER_OR_EQUAL concat
*/
private static function parseLtGt(Scanner $scanner)
{
$result = self::parseSum($scanner);
$type = $scanner->tokenType();
while ($type === '<' || $type === T_IS_SMALLER_OR_EQUAL ||
$type === '>' || $type === T_IS_GREATER_OR_EQUAL) {
$scanner->nextToken();
$result = new BooleanExpression($result, self::parseSum($scanner), $type);
$type = $scanner->tokenType();
}
return $result;
}
/**
* cmp: lt_gt | cmp IS_EQUAL lt_gt | cmp IS_NOT_EQUAL lt_gt
*/
private static function parseCmp(Scanner $scanner)
{
$result = self::parseLtGt($scanner);
$type = $scanner->tokenType();
while ($type === T_IS_EQUAL || $type === T_IS_NOT_EQUAL) {
$scanner->nextToken();
$result = new BooleanExpression($result, self::parseLtGt($scanner), $type);
$type = $scanner->tokenType();
}
return $result;
}
/**
* and: cmp | and BOOLEAN_AND cmp
*/
private static function parseAnd(Scanner $scanner)
{
$result = self::parseCmp($scanner);
$type = $scanner->tokenType();
while ($type === T_BOOLEAN_AND) {
$scanner->nextToken();
$result = new BooleanExpression($result, self::parseCmp($scanner), $type);
$type = $scanner->tokenType();
}
return $result;
}
/**
* or: and | or BOOLEAN_OR and
*/
private static function parseOr(Scanner $scanner)
{
$result = self::parseAnd($scanner);
$type = $scanner->tokenType();
while ($type === T_BOOLEAN_OR) {
$scanner->nextToken();
$result = new BooleanExpression($result, self::parseAnd($scanner), $type);
$type = $scanner->tokenType();
}
return $result;
}
/**
* expr: or | or '?' expr ':' expr
*/
private static function parseExpr(Scanner $scanner)
{
$result = self::parseOr($scanner);
if ($scanner->tokenType() === '?') {
$scanner->nextToken();
$expr = self::parseExpr($scanner);
if ($scanner->tokenType() !== ':') {
throw new TemplateParserException('missing ":"', $scanner);
}
$scanner->nextToken();
$result = new ConditionExpression($result, $expr, self::parseExpr($scanner));
}
return $result;
}
}
/**
* Exception class used to report template parse errors.
*/
class TemplateParserException extends \Exception
{
public function __construct($message, $scanner)
{
$type = $scanner->tokenType();
$value = is_int($type) ? $scanner->tokenValue() : $type;
return parent::__construct("$message at \"$value\"");
}
}
|