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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
|
#![allow(deprecated)]
/// The compiler module houses the code which parses and compiles templates. TinyTemplate implements
/// a simple bytecode interpreter (see the [instruction] module for more details) to render templates.
/// The [`TemplateCompiler`](struct.TemplateCompiler.html) struct is responsible for parsing the
/// template strings and generating the appropriate bytecode instructions.
use error::Error::*;
use error::{get_offset, Error, Result};
use instruction::{Instruction, Path, PathStep};
/// The end point of a branch or goto instruction is not known.
const UNKNOWN: usize = ::std::usize::MAX;
/// The compiler keeps a stack of the open blocks so that it can ensure that blocks are closed in
/// the right order. The Block type is a simple enumeration of the kinds of blocks that could be
/// open. It may contain the instruction index corresponding to the start of the block.
enum Block {
Branch(usize),
For(usize),
With,
}
/// List of the known @-keywords so that we can error if the user spells them wrong.
static KNOWN_KEYWORDS: [&str; 4] = ["@index", "@first", "@last", "@root"];
/// The TemplateCompiler struct is responsible for parsing a template string and generating bytecode
/// instructions based on it. The parser is a simple hand-written pattern-matching parser with no
/// recursion, which makes it relatively easy to read.
pub(crate) struct TemplateCompiler<'template> {
original_text: &'template str,
remaining_text: &'template str,
instructions: Vec<Instruction<'template>>,
block_stack: Vec<(&'template str, Block)>,
/// When we see a `{foo -}` or similar, we need to remember to left-trim the next text block we
/// encounter.
trim_next: bool,
}
impl<'template> TemplateCompiler<'template> {
/// Create a new template compiler to parse and compile the given template.
pub fn new(text: &'template str) -> TemplateCompiler<'template> {
TemplateCompiler {
original_text: text,
remaining_text: text,
instructions: vec![],
block_stack: vec![],
trim_next: false,
}
}
/// Consume the template compiler to parse the template and return the generated bytecode.
pub fn compile(mut self) -> Result<Vec<Instruction<'template>>> {
while !self.remaining_text.is_empty() {
// Comment, denoted by {# comment text #}
if self.remaining_text.starts_with("{#") {
self.trim_next = false;
let tag = self.consume_tag("#}")?;
let comment = tag[2..(tag.len() - 2)].trim();
if comment.starts_with('-') {
self.trim_last_whitespace();
}
if comment.ends_with('-') {
self.trim_next_whitespace();
}
// Block tag. Block tags are wrapped in {{ }} and always have one word at the start
// to identify which kind of tag it is. Depending on the tag type there may be more.
} else if self.remaining_text.starts_with("{{") {
self.trim_next = false;
let (discriminant, rest) = self.consume_block()?;
match discriminant {
"if" => {
let (path, negated) = if rest.starts_with("not") {
(self.parse_path(&rest[4..])?, true)
} else {
(self.parse_path(rest)?, false)
};
self.block_stack
.push((discriminant, Block::Branch(self.instructions.len())));
self.instructions
.push(Instruction::Branch(path, !negated, UNKNOWN));
}
"else" => {
self.expect_empty(rest)?;
let num_instructions = self.instructions.len() + 1;
self.close_branch(num_instructions, discriminant)?;
self.block_stack
.push((discriminant, Block::Branch(self.instructions.len())));
self.instructions.push(Instruction::Goto(UNKNOWN))
}
"endif" => {
self.expect_empty(rest)?;
let num_instructions = self.instructions.len();
self.close_branch(num_instructions, discriminant)?;
}
"with" => {
let (path, name) = self.parse_with(rest)?;
let instruction = Instruction::PushNamedContext(path, name);
self.instructions.push(instruction);
self.block_stack.push((discriminant, Block::With));
}
"endwith" => {
self.expect_empty(rest)?;
if let Some((_, Block::With)) = self.block_stack.pop() {
self.instructions.push(Instruction::PopContext)
} else {
return Err(self.parse_error(
discriminant,
"Found a closing endwith that doesn't match with a preceeding with.".to_string()
));
}
}
"for" => {
let (path, name) = self.parse_for(rest)?;
self.instructions
.push(Instruction::PushIterationContext(path, name));
self.block_stack
.push((discriminant, Block::For(self.instructions.len())));
self.instructions.push(Instruction::Iterate(UNKNOWN));
}
"endfor" => {
self.expect_empty(rest)?;
let num_instructions = self.instructions.len() + 1;
let goto_target = self.close_for(num_instructions, discriminant)?;
self.instructions.push(Instruction::Goto(goto_target));
self.instructions.push(Instruction::PopContext);
}
"call" => {
let (name, path) = self.parse_call(rest)?;
self.instructions.push(Instruction::Call(name, path));
}
_ => {
return Err(self.parse_error(
discriminant,
format!("Unknown block type '{}'", discriminant),
));
}
}
// Values, of the form { dotted.path.to.value.in.context }
// Note that it is not (currently) possible to escape curly braces in the templates to
// prevent them from being interpreted as values.
} else if self.remaining_text.starts_with('{') {
self.trim_next = false;
let (path, name) = self.consume_value()?;
let instruction = match name {
Some(name) => Instruction::FormattedValue(path, name),
None => Instruction::Value(path),
};
self.instructions.push(instruction);
// All other text - just consume characters until we see a {
} else {
let mut escaped = false;
loop {
let mut text = self.consume_text(escaped);
if self.trim_next {
text = text.trim_left();
self.trim_next = false;
}
escaped = text.ends_with('\\');
if escaped {
text = &text[0..(text.len() - 1)];
}
self.instructions.push(Instruction::Literal(text));
if !escaped {
break;
}
}
}
}
if let Some((text, _)) = self.block_stack.pop() {
return Err(self.parse_error(
text,
"Expected block-closing tag, but reached the end of input.".to_string(),
));
}
Ok(self.instructions)
}
/// Splits a string into a list of named segments which can later be used to look up values in the
/// context.
fn parse_path(&self, text: &'template str) -> Result<Path<'template>> {
if !text.starts_with('@') {
Ok(text
.split('.')
.map(|s| match s.parse::<usize>() {
Ok(n) => PathStep::Index(s, n),
Err(_) => PathStep::Name(s),
})
.collect::<Vec<_>>())
} else if KNOWN_KEYWORDS.iter().any(|k| *k == text) {
Ok(vec![PathStep::Name(text)])
} else {
Err(self.parse_error(text, format!("Invalid keyword name '{}'", text)))
}
}
/// Finds the line number and column where an error occurred. Location is the substring of
/// self.original_text where the error was found, and msg is the error message.
fn parse_error(&self, location: &str, msg: String) -> Error {
let (line, column) = get_offset(self.original_text, location);
ParseError { msg, line, column }
}
/// Tags which should have no text after the discriminant use this to raise an error if
/// text is found.
fn expect_empty(&self, text: &str) -> Result<()> {
if text.is_empty() {
Ok(())
} else {
Err(self.parse_error(text, format!("Unexpected text '{}'", text)))
}
}
/// Close the branch that is on top of the block stack by setting its target instruction
/// and popping it from the stack. Returns an error if the top of the block stack is not a
/// branch.
fn close_branch(&mut self, new_target: usize, discriminant: &str) -> Result<()> {
let branch_block = self.block_stack.pop();
if let Some((_, Block::Branch(index))) = branch_block {
match &mut self.instructions[index] {
Instruction::Branch(_, _, target) => {
*target = new_target;
Ok(())
}
Instruction::Goto(target) => {
*target = new_target;
Ok(())
}
_ => panic!(),
}
} else {
Err(self.parse_error(
discriminant,
"Found a closing endif or else which doesn't match with a preceding if."
.to_string(),
))
}
}
/// Close the for loop that is on top of the block stack by setting its target instruction and
/// popping it from the stack. Returns an error if the top of the stack is not a for loop.
/// Returns the index of the loop's Iterate instruction for further processing.
fn close_for(&mut self, new_target: usize, discriminant: &str) -> Result<usize> {
let branch_block = self.block_stack.pop();
if let Some((_, Block::For(index))) = branch_block {
match &mut self.instructions[index] {
Instruction::Iterate(target) => {
*target = new_target;
Ok(index)
}
_ => panic!(),
}
} else {
Err(self.parse_error(
discriminant,
"Found a closing endfor which doesn't match with a preceding for.".to_string(),
))
}
}
/// Advance the cursor to the next { and return the consumed text. If `escaped` is true, skips
/// a { at the start of the text.
fn consume_text(&mut self, escaped: bool) -> &'template str {
let search_substr = if escaped {
&self.remaining_text[1..]
} else {
self.remaining_text
};
let mut position = search_substr
.find('{')
.unwrap_or_else(|| search_substr.len());
if escaped {
position += 1;
}
let (text, remaining) = self.remaining_text.split_at(position);
self.remaining_text = remaining;
text
}
/// Advance the cursor to the end of the value tag and return the value's path and optional
/// formatter name.
fn consume_value(&mut self) -> Result<(Path<'template>, Option<&'template str>)> {
let tag = self.consume_tag("}")?;
let mut tag = tag[1..(tag.len() - 1)].trim();
if tag.starts_with('-') {
tag = tag[1..].trim();
self.trim_last_whitespace();
}
if tag.ends_with('-') {
tag = tag[0..tag.len() - 1].trim();
self.trim_next_whitespace();
}
if let Some(index) = tag.find('|') {
let (path_str, name_str) = tag.split_at(index);
let name = name_str[1..].trim();
let path = self.parse_path(path_str.trim())?;
Ok((path, Some(name)))
} else {
Ok((self.parse_path(tag)?, None))
}
}
/// Right-trim whitespace from the last text block we parsed.
fn trim_last_whitespace(&mut self) {
if let Some(Instruction::Literal(text)) = self.instructions.last_mut() {
*text = text.trim_right();
}
}
/// Make a note to left-trim whitespace from the next text block we parse.
fn trim_next_whitespace(&mut self) {
self.trim_next = true;
}
/// Advance the cursor to the end of the current block tag and return the discriminant substring
/// and the rest of the text in the tag. Also handles trimming whitespace where needed.
fn consume_block(&mut self) -> Result<(&'template str, &'template str)> {
let tag = self.consume_tag("}}")?;
let mut block = tag[2..(tag.len() - 2)].trim();
if block.starts_with('-') {
block = block[1..].trim();
self.trim_last_whitespace();
}
if block.ends_with('-') {
block = block[0..block.len() - 1].trim();
self.trim_next_whitespace();
}
let discriminant = block.split_whitespace().next().unwrap_or(block);
let rest = block[discriminant.len()..].trim();
Ok((discriminant, rest))
}
/// Advance the cursor to after the given expected_close string and return the text in between
/// (including the expected_close characters), or return an error message if we reach the end
/// of a line of text without finding it.
fn consume_tag(&mut self, expected_close: &str) -> Result<&'template str> {
if let Some(line) = self.remaining_text.lines().next() {
if let Some(pos) = line.find(expected_close) {
let (tag, remaining) = self.remaining_text.split_at(pos + expected_close.len());
self.remaining_text = remaining;
Ok(tag)
} else {
Err(self.parse_error(
line,
format!(
"Expected a closing '{}' but found end-of-line instead.",
expected_close
),
))
}
} else {
Err(self.parse_error(
self.remaining_text,
format!(
"Expected a closing '{}' but found end-of-text instead.",
expected_close
),
))
}
}
/// Parse a with tag to separate the value path from the (optional) name.
fn parse_with(&self, with_text: &'template str) -> Result<(Path<'template>, &'template str)> {
if let Some(index) = with_text.find(" as ") {
let (path_str, name_str) = with_text.split_at(index);
let path = self.parse_path(path_str.trim())?;
let name = name_str[" as ".len()..].trim();
Ok((path, name))
} else {
Err(self.parse_error(
with_text,
format!(
"Expected 'as <path>' in with block, but found \"{}\" instead",
with_text
),
))
}
}
/// Parse a for tag to separate the value path from the name.
fn parse_for(&self, for_text: &'template str) -> Result<(Path<'template>, &'template str)> {
if let Some(index) = for_text.find(" in ") {
let (name_str, path_str) = for_text.split_at(index);
let name = name_str.trim();
let path = self.parse_path(path_str[" in ".len()..].trim())?;
Ok((path, name))
} else {
Err(self.parse_error(
for_text,
format!("Unable to parse for block text '{}'", for_text),
))
}
}
/// Parse a call tag to separate the template name and context value.
fn parse_call(&self, call_text: &'template str) -> Result<(&'template str, Path<'template>)> {
if let Some(index) = call_text.find(" with ") {
let (name_str, path_str) = call_text.split_at(index);
let name = name_str.trim();
let path = self.parse_path(path_str[" with ".len()..].trim())?;
Ok((name, path))
} else {
Err(self.parse_error(
call_text,
format!("Unable to parse call block text '{}'", call_text),
))
}
}
}
#[cfg(test)]
mod test {
use super::*;
use instruction::Instruction::*;
fn compile(text: &'static str) -> Result<Vec<Instruction<'static>>> {
TemplateCompiler::new(text).compile()
}
#[test]
fn test_compile_literal() {
let text = "Test String";
let instructions = compile(text).unwrap();
assert_eq!(1, instructions.len());
assert_eq!(&Literal(text), &instructions[0]);
}
#[test]
fn test_compile_value() {
let text = "{ foobar }";
let instructions = compile(text).unwrap();
assert_eq!(1, instructions.len());
assert_eq!(&Value(vec![PathStep::Name("foobar")]), &instructions[0]);
}
#[test]
fn test_compile_value_with_formatter() {
let text = "{ foobar | my_formatter }";
let instructions = compile(text).unwrap();
assert_eq!(1, instructions.len());
assert_eq!(
&FormattedValue(vec![PathStep::Name("foobar")], "my_formatter"),
&instructions[0]
);
}
#[test]
fn test_dotted_path() {
let text = "{ foo.bar }";
let instructions = compile(text).unwrap();
assert_eq!(1, instructions.len());
assert_eq!(
&Value(vec![PathStep::Name("foo"), PathStep::Name("bar")]),
&instructions[0]
);
}
#[test]
fn test_indexed_path() {
let text = "{ foo.0.bar }";
let instructions = compile(text).unwrap();
assert_eq!(1, instructions.len());
assert_eq!(
&Value(vec![
PathStep::Name("foo"),
PathStep::Index("0", 0),
PathStep::Name("bar")
]),
&instructions[0]
);
}
#[test]
fn test_mixture() {
let text = "Hello { name }, how are you?";
let instructions = compile(text).unwrap();
assert_eq!(3, instructions.len());
assert_eq!(&Literal("Hello "), &instructions[0]);
assert_eq!(&Value(vec![PathStep::Name("name")]), &instructions[1]);
assert_eq!(&Literal(", how are you?"), &instructions[2]);
}
#[test]
fn test_if_endif() {
let text = "{{ if foo }}Hello!{{ endif }}";
let instructions = compile(text).unwrap();
assert_eq!(2, instructions.len());
assert_eq!(
&Branch(vec![PathStep::Name("foo")], true, 2),
&instructions[0]
);
assert_eq!(&Literal("Hello!"), &instructions[1]);
}
#[test]
fn test_if_not_endif() {
let text = "{{ if not foo }}Hello!{{ endif }}";
let instructions = compile(text).unwrap();
assert_eq!(2, instructions.len());
assert_eq!(
&Branch(vec![PathStep::Name("foo")], false, 2),
&instructions[0]
);
assert_eq!(&Literal("Hello!"), &instructions[1]);
}
#[test]
fn test_if_else_endif() {
let text = "{{ if foo }}Hello!{{ else }}Goodbye!{{ endif }}";
let instructions = compile(text).unwrap();
assert_eq!(4, instructions.len());
assert_eq!(
&Branch(vec![PathStep::Name("foo")], true, 3),
&instructions[0]
);
assert_eq!(&Literal("Hello!"), &instructions[1]);
assert_eq!(&Goto(4), &instructions[2]);
assert_eq!(&Literal("Goodbye!"), &instructions[3]);
}
#[test]
fn test_with() {
let text = "{{ with foo as bar }}Hello!{{ endwith }}";
let instructions = compile(text).unwrap();
assert_eq!(3, instructions.len());
assert_eq!(
&PushNamedContext(vec![PathStep::Name("foo")], "bar"),
&instructions[0]
);
assert_eq!(&Literal("Hello!"), &instructions[1]);
assert_eq!(&PopContext, &instructions[2]);
}
#[test]
fn test_foreach() {
let text = "{{ for foo in bar.baz }}{ foo }{{ endfor }}";
let instructions = compile(text).unwrap();
assert_eq!(5, instructions.len());
assert_eq!(
&PushIterationContext(vec![PathStep::Name("bar"), PathStep::Name("baz")], "foo"),
&instructions[0]
);
assert_eq!(&Iterate(4), &instructions[1]);
assert_eq!(&Value(vec![PathStep::Name("foo")]), &instructions[2]);
assert_eq!(&Goto(1), &instructions[3]);
assert_eq!(&PopContext, &instructions[4]);
}
#[test]
fn test_strip_whitespace_value() {
let text = "Hello, {- name -} , how are you?";
let instructions = compile(text).unwrap();
assert_eq!(3, instructions.len());
assert_eq!(&Literal("Hello,"), &instructions[0]);
assert_eq!(&Value(vec![PathStep::Name("name")]), &instructions[1]);
assert_eq!(&Literal(", how are you?"), &instructions[2]);
}
#[test]
fn test_strip_whitespace_block() {
let text = "Hello, {{- if name -}} {name} {{- endif -}} , how are you?";
let instructions = compile(text).unwrap();
assert_eq!(6, instructions.len());
assert_eq!(&Literal("Hello,"), &instructions[0]);
assert_eq!(
&Branch(vec![PathStep::Name("name")], true, 5),
&instructions[1]
);
assert_eq!(&Literal(""), &instructions[2]);
assert_eq!(&Value(vec![PathStep::Name("name")]), &instructions[3]);
assert_eq!(&Literal(""), &instructions[4]);
assert_eq!(&Literal(", how are you?"), &instructions[5]);
}
#[test]
fn test_comment() {
let text = "Hello, {# foo bar baz #} there!";
let instructions = compile(text).unwrap();
assert_eq!(2, instructions.len());
assert_eq!(&Literal("Hello, "), &instructions[0]);
assert_eq!(&Literal(" there!"), &instructions[1]);
}
#[test]
fn test_strip_whitespace_comment() {
let text = "Hello, \t\n {#- foo bar baz -#} \t there!";
let instructions = compile(text).unwrap();
assert_eq!(2, instructions.len());
assert_eq!(&Literal("Hello,"), &instructions[0]);
assert_eq!(&Literal("there!"), &instructions[1]);
}
#[test]
fn test_strip_whitespace_followed_by_another_tag() {
let text = "{value -}{value} Hello";
let instructions = compile(text).unwrap();
assert_eq!(3, instructions.len());
assert_eq!(&Value(vec![PathStep::Name("value")]), &instructions[0]);
assert_eq!(&Value(vec![PathStep::Name("value")]), &instructions[1]);
assert_eq!(&Literal(" Hello"), &instructions[2]);
}
#[test]
fn test_call() {
let text = "{{ call my_macro with foo.bar }}";
let instructions = compile(text).unwrap();
assert_eq!(1, instructions.len());
assert_eq!(
&Call(
"my_macro",
vec![PathStep::Name("foo"), PathStep::Name("bar")]
),
&instructions[0]
);
}
#[test]
fn test_curly_brace_escaping() {
let text = "body \\{ \nfont-size: {fontsize} \n}";
let instructions = compile(text).unwrap();
assert_eq!(4, instructions.len());
assert_eq!(&Literal("body "), &instructions[0]);
assert_eq!(&Literal("{ \nfont-size: "), &instructions[1]);
assert_eq!(&Value(vec![PathStep::Name("fontsize")]), &instructions[2]);
assert_eq!(&Literal(" \n}"), &instructions[3]);
}
#[test]
fn test_unclosed_tags() {
let tags = vec![
"{",
"{ foo.bar",
"{ foo.bar\n }",
"{{",
"{{ if foo.bar",
"{{ if foo.bar \n}}",
"{#",
"{# if foo.bar",
"{# if foo.bar \n#}",
];
for tag in tags {
compile(tag).unwrap_err();
}
}
#[test]
fn test_mismatched_blocks() {
let text = "{{ if foo }}{{ with bar }}{{ endif }} {{ endwith }}";
compile(text).unwrap_err();
}
#[test]
fn test_disallows_invalid_keywords() {
let text = "{ @foo }";
compile(text).unwrap_err();
}
#[test]
fn test_diallows_unknown_block_type() {
let text = "{{ foobar }}";
compile(text).unwrap_err();
}
#[test]
fn test_parse_error_line_column_num() {
let text = "\n\n\n{{ foobar }}";
let err = compile(text).unwrap_err();
if let ParseError { line, column, .. } = err {
assert_eq!(4, line);
assert_eq!(3, column);
} else {
panic!("Should have returned a parse error");
}
}
#[test]
fn test_parse_error_on_unclosed_if() {
let text = "{{ if foo }}";
compile(text).unwrap_err();
}
#[test]
fn test_parse_escaped_open_curly_brace() {
let text: &str = r"hello \{world}";
let instructions = compile(text).unwrap();
assert_eq!(2, instructions.len());
assert_eq!(&Literal("hello "), &instructions[0]);
assert_eq!(&Literal("{world}"), &instructions[1]);
}
}
|