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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
use std::error::Error as StdError;
use std::mem;
use std::fmt;

/// `Token` is a section of a compiled mustache string.
#[derive(Clone, Debug, PartialEq)]
pub enum Token {
    Text(String),
    EscapedTag(Vec<String>, String),
    UnescapedTag(Vec<String>, String),
    Section(Vec<String>, bool, Vec<Token>, String, String, String, String, String),
    IncompleteSection(Vec<String>, bool, String, bool),
    Partial(String, String, String),
}

/// Error type to represent parsing failure.
///
/// This type is not intended to be matched exhaustively as new variants
/// may be added in future without a version bump.
#[derive(Debug, PartialEq)]
pub enum Error {
    BadClosingTag(char, char),
    UnclosedTag,
    UnclosedSection(String),
    UnbalancedUnescapeTag,
    EmptyTag,
    EarlySectionClose(String),
    MissingSetDelimeterClosingTag,
    InvalidSetDelimeterSyntax,

    #[doc(hidden)]
    __Nonexhaustive,
}

impl StdError for Error {
    fn description(&self) -> &'static str {
        match *self {
            Error::BadClosingTag(..) => "found a malformed closing tag",
            Error::UnclosedTag => "found an unclosed tag",
            Error::UnclosedSection(..) => "found an unclosed section",
            Error::UnbalancedUnescapeTag => "found an unbalanced unescape tag",
            Error::EmptyTag => "found an empty tag",
            Error::EarlySectionClose(..) => "found a closing tag for an unopened section",
            Error::MissingSetDelimeterClosingTag => "missing the new closing tag in set delimeter tag",
            Error::InvalidSetDelimeterSyntax => "invalid set delimeter tag syntax",
            Error::__Nonexhaustive => unreachable!(),
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // Provide more information where possible
        match *self {
            Error::BadClosingTag(actual, expected) => {
                write!(f,
                       "character {:?} was unexpected in the closing tag, expected {:?}",
                       actual,
                       expected)
            }
            Error::UnclosedSection(ref name) => {
                write!(f, "found an unclosed section: {:?}", name)
            },
            Error::EarlySectionClose(ref name) => {
                write!(f, "found a closing tag for an unopened section {:?}", name)
            },
            _ => write!(f, "{}", self.description()),
        }
    }
}

enum TokenClass {
    Normal,
    StandAlone,
    WhiteSpace(String, usize),
}

/// `Parser` parses a string into a series of `Token`s.
pub struct Parser<'a, T: 'a> {
    reader: &'a mut T,
    ch: Option<char>,
    lookahead: Option<char>,
    line: usize,
    col: usize,
    content: String,
    state: ParserState,
    opening_tag: String,
    closing_tag: String,
    opening_tag_chars: Vec<char>,
    closing_tag_chars: Vec<char>,
    tag_position: usize,
    tokens: Vec<Token>,
    partials: Vec<String>,
}

enum ParserState {
    Text,
    OpeningTag,
    Tag,
    ClosingTag,
}

impl<'a, T: Iterator<Item = char>> Parser<'a, T> {
    pub fn new(reader: &'a mut T, opening_tag: &str, closing_tag: &str) -> Parser<'a, T> {
        let mut parser = Parser {
            reader: reader,
            ch: None,
            lookahead: None,
            line: 1,
            col: 1,
            content: String::new(),
            state: ParserState::Text,
            opening_tag: opening_tag.to_string(),
            closing_tag: closing_tag.to_string(),
            opening_tag_chars: opening_tag.chars().collect(),
            closing_tag_chars: closing_tag.chars().collect(),
            tag_position: 0,
            tokens: Vec::new(),
            partials: Vec::new(),
        };

        parser.bump();
        parser
    }

    fn bump(&mut self) {
        match self.lookahead.take() {
            None => {
                self.ch = self.reader.next();
            }
            Some(ch) => {
                self.ch = Some(ch);
            }
        }

        if let Some(ch) = self.ch {
            if ch == '\n' {
                self.line += 1;
                self.col = 1;
            } else {
                self.col += 1;
            }
        }
    }

    fn peek(&mut self) -> Option<char> {
        match self.lookahead {
            None => {
                self.lookahead = self.reader.next();
                self.lookahead
            }
            Some(ch) => Some(ch),
        }
    }

    fn ch_is(&self, ch: char) -> bool {
        match self.ch {
            Some(c) => c == ch,
            None => false,
        }
    }

    /// Parse the template into tokens and a list of partial files.
    pub fn parse(mut self) -> Result<(Vec<Token>, Vec<String>), Error> {
        let mut curly_brace_tag = false;

        while let Some(ch) = self.ch {
            match self.state {
                ParserState::Text => {
                    if ch == self.opening_tag_chars[0] {
                        if self.opening_tag_chars.len() > 1 {
                            self.tag_position = 1;
                            self.state = ParserState::OpeningTag;
                        } else {
                            self.add_text();
                            self.state = ParserState::Tag;
                        }
                    } else {
                        self.content.push(ch);
                    }
                    self.bump();
                }
                ParserState::OpeningTag => {
                    if ch == self.opening_tag_chars[self.tag_position] {
                        if self.tag_position == self.opening_tag_chars.len() - 1 {
                            self.add_text();
                            curly_brace_tag = false;
                            self.state = ParserState::Tag;
                        } else {
                            self.tag_position = self.tag_position + 1;
                        }
                    } else {
                        // We don't have a tag, so add all the tag parts we've seen
                        // so far to the string.
                        self.state = ParserState::Text;
                        self.not_otag();
                        self.content.push(ch);
                    }
                    self.bump();
                }
                ParserState::Tag => {
                    if self.content.is_empty() && ch == '{' {
                        curly_brace_tag = true;
                        self.content.push(ch);
                        self.bump();
                    } else if curly_brace_tag && ch == '}' {
                        curly_brace_tag = false;
                        self.content.push(ch);
                        self.bump();
                    } else if ch == self.closing_tag_chars[0] {
                        if self.closing_tag_chars.len() > 1 {
                            self.tag_position = 1;
                            self.state = ParserState::ClosingTag;
                            self.bump();
                        } else {
                            try!(self.add_tag());
                            self.state = ParserState::Text;
                        }
                    } else {
                        self.content.push(ch);
                        self.bump();
                    }
                }
                ParserState::ClosingTag => {
                    if ch == self.closing_tag_chars[self.tag_position] {
                        if self.tag_position == self.closing_tag_chars.len() - 1 {
                            try!(self.add_tag());
                            self.state = ParserState::Text;
                        } else {
                            self.state = ParserState::Tag;
                            self.not_ctag();
                            self.content.push(ch);
                            self.bump();
                        }
                    } else {
                        let expected = self.closing_tag_chars[self.tag_position];
                        return Err(Error::BadClosingTag(ch, expected));
                    }
                }
            }
        }

        match self.state {
            ParserState::Text => {
                self.add_text();
            }
            ParserState::OpeningTag => {
                self.not_otag();
                self.add_text();
            }
            ParserState::ClosingTag => {
                self.not_ctag();
                self.add_text();
            }
            ParserState::Tag => return Err(Error::UnclosedTag),
        }

        // Check that we don't have any incomplete sections.
        for token in self.tokens.iter().rev() {
            if let Token::IncompleteSection(ref path, _, _, _) = *token {
                return Err(Error::UnclosedSection(path.join(".")))
            }
        }

        let Parser { tokens, partials, .. } = self;

        Ok((tokens, partials))
    }

    fn add_text(&mut self) {
        if !self.content.is_empty() {
            let mut content = String::new();
            mem::swap(&mut content, &mut self.content);

            self.tokens.push(Token::Text(content));
        }
    }

    // This function classifies whether or not a token is standalone, or if it
    // has trailing whitespace. It's looking for this pattern:
    //
    //   ("\n" | "\r\n") whitespace* token ("\n" | "\r\n")
    //
    fn classify_token(&mut self) -> TokenClass {
        // Exit early if the next character is not '\n' or '\r\n'.
        if let Some(ch) = self.ch {
            if !(ch == '\n' || (ch == '\r' && self.peek() == Some('\n'))) {
                return TokenClass::Normal;
            }
        }

        match self.tokens.last() {
            // If the last token ends with a newline (or there is no previous
            // token), then this token is standalone.
            None => TokenClass::StandAlone,

            Some(&Token::IncompleteSection(_, _, _, true)) => TokenClass::StandAlone,

            Some(&Token::Text(ref s)) if !s.is_empty() => {
                // Look for the last newline character that may have whitespace
                // following it.
                match s.rfind(|c: char| c == '\n' || !c.is_whitespace()) {
                    // It's all whitespace.
                    None => {
                        if self.tokens.len() == 1 {
                            TokenClass::WhiteSpace(s.clone(), 0)
                        } else {
                            TokenClass::Normal
                        }
                    }
                    Some(pos) => {
                        if s.as_bytes()[pos] == b'\n' {
                            if pos == s.len() - 1 {
                                TokenClass::StandAlone
                            } else {
                                TokenClass::WhiteSpace(s.clone(), pos + 1)
                            }
                        } else {
                            TokenClass::Normal
                        }
                    }
                }
            }
            Some(_) => TokenClass::Normal,
        }
    }

    fn eat_whitespace(&mut self) -> bool {
        // If the next character is a newline, and the last token ends with a
        // newline and whitespace, clear out the whitespace.

        match self.classify_token() {
            TokenClass::Normal => false,
            TokenClass::StandAlone => {
                if self.ch_is('\r') {
                    self.bump();
                }
                self.bump();
                true
            }
            TokenClass::WhiteSpace(s, pos) => {
                if self.ch_is('\r') {
                    self.bump();
                }
                self.bump();

                // Trim the whitespace from the last token.
                self.tokens.pop();
                self.tokens.push(Token::Text(s[0..pos].to_string()));

                true
            }
        }
    }

    fn add_tag(&mut self) -> Result<(), Error> {
        self.bump();

        let tag = self.opening_tag.clone() + &self.content + &self.closing_tag;

        // Move the content to avoid a copy.
        let mut content = String::new();
        mem::swap(&mut content, &mut self.content);
        let len = content.len();
        try!(deny_blank(&content));
        let content = content;

        match content.as_bytes()[0] as char {
            '!' => {
                // ignore comments
                self.eat_whitespace();
            }
            '&' => {
                let name = &content[1..len];
                let name = try!(get_name_or_implicit(name));
                self.tokens.push(Token::UnescapedTag(name, tag));
            }
            '{' => {
                if content.ends_with('}') {
                    let name = &content[1..len - 1];
                    let name = try!(get_name_or_implicit(name));
                    self.tokens.push(Token::UnescapedTag(name, tag));
                } else {
                    return Err(Error::UnbalancedUnescapeTag)
                }
            }
            '#' => {
                let newlined = self.eat_whitespace();

                let name = try!(get_name_or_implicit(&content[1..len]));
                self.tokens.push(Token::IncompleteSection(name, false, tag, newlined));
            }
            '^' => {
                let newlined = self.eat_whitespace();

                let name = try!(get_name_or_implicit(&content[1..len]));
                self.tokens.push(Token::IncompleteSection(name, true, tag, newlined));
            }
            '/' => {
                self.eat_whitespace();

                let name = try!(get_name_or_implicit(&content[1..len]));
                let mut children: Vec<Token> = Vec::new();

                loop {
                    if self.tokens.is_empty() {
                        return Err(Error::EarlySectionClose(name.join(".")))
                    }

                    let last = self.tokens.pop();

                    match last {
                        Some(Token::IncompleteSection(section_name, inverted, osection, _)) => {
                            children.reverse();

                            // Collect all the children's sources.
                            let mut srcs = Vec::new();
                            for child in children.iter() {
                                match *child {
                                    Token::Text(ref s) |
                                    Token::EscapedTag(_, ref s) |
                                    Token::UnescapedTag(_, ref s) |
                                    Token::Partial(_, _, ref s) => srcs.push(s.clone()),
                                    Token::Section(_, _, _, _, ref osection, ref src, ref csection, _) => {
                                        srcs.push(osection.clone());
                                        srcs.push(src.clone());
                                        srcs.push(csection.clone());
                                    }
                                    _ => bug!("Incomplete sections should not be nested"),
                                }
                            }

                            if section_name == name {
                                // Cache the combination of all the sources in the
                                // section. It's unfortunate, but we need to do this in
                                // case the user uses a function to instantiate the
                                // tag.
                                let mut src = String::new();
                                for s in srcs.iter() {
                                    src.push_str(s);
                                }

                                self.tokens.push(Token::Section(name,
                                                         inverted,
                                                         children,
                                                         self.opening_tag.clone(),
                                                         osection,
                                                         src,
                                                         tag,
                                                         self.closing_tag.clone()));
                                break;
                            } else {
                                return Err(Error::UnclosedSection(section_name.join(".")))
                            }
                        }
                        Some(last_token) => children.push(last_token),
                        None => (),
                    }
                }
            }
            '>' => {
                try!(self.add_partial(&content, tag));
            }
            '=' => {
                self.eat_whitespace();

                if len > 2usize && content.ends_with('=') {
                    let s = try!(deny_blank(&content[1..len - 1]));

                    let pos = s.find(char::is_whitespace);
                    let pos = match pos {
                        None => return Err(Error::MissingSetDelimeterClosingTag),
                        Some(pos) => pos,
                    };

                    self.opening_tag = s[0..pos].to_string();
                    self.opening_tag_chars = self.opening_tag.chars().collect();

                    let s2 = &s[pos..];
                    let pos = s2.find(|c: char| !c.is_whitespace());
                    let pos = match pos {
                        None => return Err(Error::MissingSetDelimeterClosingTag),
                        Some(pos) => pos,
                    };

                    self.closing_tag = s2[pos..].to_string();
                    self.closing_tag_chars = self.closing_tag.chars().collect();
                } else {
                    return Err(Error::InvalidSetDelimeterSyntax)
                }
            }
            _ => {
                // If the name is "." then we want the top element, which we represent with
                // an empty name.
                let name = try!(get_name_or_implicit(&content));
                self.tokens.push(Token::EscapedTag(name, tag));
            }
        };

        Ok(())
    }

    fn add_partial(&mut self, content: &str, tag: String) -> Result<(), Error> {
        let indent = match self.classify_token() {
            TokenClass::Normal => "".to_string(),
            TokenClass::StandAlone => {
                if self.ch_is('\r') {
                    self.bump();
                }
                self.bump();
                "".to_string()
            }
            TokenClass::WhiteSpace(s, pos) => {
                if self.ch_is('\r') {
                    self.bump();
                }
                self.bump();

                let ws = &s[pos..];

                // Trim the whitespace from the last token.
                self.tokens.pop();
                self.tokens.push(Token::Text(s[0..pos].to_string()));

                ws.to_string()
            }
        };

        // We can't inline the tokens directly as we may have a recursive
        // partial. So instead, we'll cache the partials we used and look them
        // up later.
        let name = &content[1..content.len()];
        let name = try!(deny_blank(name));

        self.tokens.push(Token::Partial(name.into(), indent, tag));
        self.partials.push(name.into());

        Ok(())
    }

    fn not_otag(&mut self) {
        for (i, ch) in self.opening_tag_chars.iter().enumerate() {
            if !(i < self.tag_position) {
                break;
            }
            self.content.push(*ch);
        }
    }

    fn not_ctag(&mut self) {
        for (i, ch) in self.closing_tag_chars.iter().enumerate() {
            if !(i < self.tag_position) {
                break;
            }
            self.content.push(*ch);
        }
    }
}

fn get_name_or_implicit(name: &str) -> Result<Vec<String>, Error> {
    // If the name is "." then we want the top element, which we represent with
    // an empty name.
    let name = try!(deny_blank(&name));
    Ok(if name == "." {
        Vec::new()
    } else {
        name.split_terminator('.')
            .map(|x| x.to_string())
            .collect()
    })
}

fn deny_blank(content: &str) -> Result<&str, Error> {
    let trimmed = content.trim();
    if trimmed.is_empty() {
        Err(Error::EmptyTag)
    } else {
        Ok(trimmed)
    }
}

//FIXME: These tests are mainly to guide the removal of panics from the parser (turning them
// into `Error`s instead). It would be good to make them more robust and make assertions on
// the tokens & partials returned.
#[cfg(test)]
mod tests {
    use super::*;

    pub fn parse(input: &str) -> Result<(Vec<Token>, Vec<String>), Error> {
        let input = &mut input.chars();
        let parser = Parser::new(input, "{{", "}}");
        parser.parse()
    }

    pub fn assert_parse(input: &str) -> (Vec<Token>, Vec<String>) {
        parse(input).expect(&format!("Failed to parse: {}", input))
    }

    #[test]
    fn empty_input() {
        assert_parse("");
    }

    #[test]
    fn empty_tag() {
        assert_eq!(parse("{{}}"), Err(Error::EmptyTag));
    }

    #[test]
    fn whitespace_only_tag() {
        assert_eq!(parse("{{ }}"), Err(Error::EmptyTag));
    }

    #[test]
    fn bad_closing_tag() {
        assert_eq!(parse("{{hello}?"), Err(Error::BadClosingTag('?', '}')))
    }

    #[test]
    fn unclosed_tag() {
        assert_eq!(parse("{{hi"), Err(Error::UnclosedTag))
    }

    mod sections {
        use super::*;

        #[test]
        fn sanity() {
            assert_parse("{{#people}} Hi {{name}}! {{/people}}");
        }

        #[test]
        fn unclosed() {
            assert_eq!(parse("{{#world}}hi"), Err(Error::UnclosedSection("world".into())))
        }

        #[test]
        fn unclosed_nested_with_wrong_closing_tag() {
            assert_eq!(
                parse("{{#universe}} {{#world}} {{/universe}}"),
                Err(Error::UnclosedSection("world".into()))
            )
        }

        #[test]
        fn unclosed_nested() {
            assert_eq!(
                parse("{{#universe}} {{#world}}"),
                Err(Error::UnclosedSection("world".into()))
            )
        }

        #[test]
        fn unclosed_with_path() {
            assert_eq!(
                parse("{{#universe}} {{#world.and.stuff}} {{/universe}}"),
                Err(Error::UnclosedSection("world.and.stuff".into()))
            )
        }

        #[test]
        fn early_close() {
            assert_eq!(parse("{{/world}}"), Err(Error::EarlySectionClose("world".into())))
        }
    }

    mod inverted {
        use super::*;

        #[test]
        fn sanity() {
            assert_parse("{{^people}} No people! {{/people}}");
        }

        #[test]
        fn unclosed() {
            assert_eq!(parse("{{^world}}hi"), Err(Error::UnclosedSection("world".into())))
        }

        #[test]
        fn unclosed_nested_with_wrong_closing_tag() {
            assert_eq!(
                parse("{{#universe}} {{^world}} {{/universe}}"),
                Err(Error::UnclosedSection("world".into()))
            );

            assert_eq!(
                parse("{{^universe}} {{^world}} {{/universe}}"),
                Err(Error::UnclosedSection("world".into()))
            )
        }

        #[test]
        fn unclosed_nested() {
            assert_eq!(
                parse("{{#universe}} {{^world}}"),
                Err(Error::UnclosedSection("world".into()))
            )
        }

        #[test]
        fn unclosed_with_path() {
            assert_eq!(
                parse("{{#universe}} {{^world.and.stuff}} {{/universe}}"),
                Err(Error::UnclosedSection("world.and.stuff".into()))
            )
        }
    }

    mod set_delimeter {
        use super::*;

        #[test]
        fn sanity() {
            assert_parse("{{=<% %>=}}");
        }

        #[test]
        fn closing_tag_is_whitespace() {
            assert_eq!(parse("{{=<% =}}"), Err(Error::MissingSetDelimeterClosingTag))
        }

        #[test]
        fn missing_closing_tag() {
            assert_eq!(parse("{{=<%=}}"), Err(Error::MissingSetDelimeterClosingTag))
        }

        #[test]
        fn missing_closing_equals() {
            assert_eq!(parse("{{=<% %>}}"), Err(Error::InvalidSetDelimeterSyntax))
        }
    }

    #[test]
    fn unbalanced_unescape() {
        // use the set delimiter tag to change the brace type. Currently this error will
        // not trigger with "{{{ }}"
        let input = "{{=<% %>=}} <%{ %>";
        assert_eq!(parse(input), Err(Error::UnbalancedUnescapeTag))
    }
}