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
use regex::{ Captures };
use std::collections::{ VecDeque };

use super::Lexer;
use error::TemplateResult;
use tokens::{ TokenRef, TokenValueRef, ConstNumberRef, ConstRef, LexerOptions };
use std::fmt;
use Expect;
use error::{ TemplateError, Received };

const PUNCTUATION: &'static str = "()[]{}?:.,|";

/// Iteration state.
#[derive(Debug, Copy, Clone)]
pub enum State {
    Data,
    Block,
    Var,
    String,
    Interpolation,
}

/// Block position.
///
/// At start, Twig runs regexp that finds all interesting block starts, like {{ or {%.
/// If nothing like that is found, no parsing occurs. Otherwise, it uses this position
/// to divide parsing in kind of "chunks".
#[derive(Debug, Copy, Clone)]
struct Position<'code> {
    loc: usize,
    len: usize,
    all_len: usize,
    value: TokenValueRef<'code>,
    ws_trim: bool,
}

impl<'code> Position<'code> {
    fn from_capture(options: &LexerOptions, c: Captures<'code>) -> Position<'code> {
        let (all_start, all_end) = c.pos(0).expect("twig bug: expected full capture when collecting positions");
        let (first_start, first_end) = c.pos(1).expect("twig bug: expected at least one subcapture (start, end) when collecting positions");
        let second = c.pos(2);

        Position {
            loc: all_start,
            len: first_end - first_start,
            all_len: all_end - all_start,
            value: match c.at(1).expect("twig bug: expected at least one subcapture (text) when collecting positions") {
                s if s == options.tag_variable.start => TokenValueRef::VarStart,
                s if s == options.tag_block.start => TokenValueRef::BlockStart,
                s if s == options.tag_comment.start => TokenValueRef::CommentStart,
                _ => unreachable!("twig bug: unexpected capture when collecting positions"),
            },
            ws_trim: match second {
                Some(_) => true,
                _ => false,
            },
        }
    }
}

/// Twig has different brackets: (, {, [, etc.
/// The "interpolation" bracket is memorized as `IntStart` and `IntEnd` and looks
/// like "#{ blah }".
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
enum BracketSymbol {
    Char(char),
    IntStart,
    IntEnd,
}

impl fmt::Display for BracketSymbol {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            BracketSymbol::Char(c) => fmt::Display::fmt(&c.to_string(), f),
            BracketSymbol::IntStart => fmt::Display::fmt(r#"#{"#, f),
            BracketSymbol::IntEnd => fmt::Display::fmt(r#"}"#, f),
        }
    }
}

/// We memorize started brackets using this struct.
#[derive(Debug, Copy, Clone)]
struct Bracket {
    open: BracketSymbol,
    close: BracketSymbol,
    line_num: usize,
}

impl Bracket {
    /// When creating bracket from the starting bracket, immediately set the
    /// kind of bracket that is oposite to starting one, so we don't have to do
    /// it in the iterator.
    fn new(open_char: BracketSymbol, line_num: usize) -> Bracket {
        Bracket {
            open: open_char,
            close: match open_char {
                BracketSymbol::Char('(') => BracketSymbol::Char(')'),
                BracketSymbol::Char('[') => BracketSymbol::Char(']'),
                BracketSymbol::Char('{') => BracketSymbol::Char('}'),
                BracketSymbol::Char('"') => BracketSymbol::Char('"'),
                BracketSymbol::IntStart => BracketSymbol::IntEnd,
                _ => unreachable!("twig bug: unknown bracket {:?}", open_char),
            },
            line_num: line_num,
        }
    }

    fn from_char(open_char: char, line_num: usize) -> Bracket {
        Bracket::new(BracketSymbol::Char(open_char), line_num)
    }
}

pub struct TokenIter<'iteration, 'code> {
    lexer: &'iteration Lexer,

    code: &'code str,
    tokens: VecDeque<TemplateResult<TokenRef<'code>>>,
    position: usize,
    positions: Vec<Position<'code>>,

    cursor: usize,
    end: usize,
    finished: bool,
    is_error: bool,

    state: State,
    states: Vec<State>,

    brackets: Vec<Bracket>,

    current_var_block_line: Option<usize>,
    line_num: usize,
}

impl<'iteration, 'code> Iterator for TokenIter<'iteration, 'code> {
    type Item = TemplateResult<TokenRef<'code>>;

    fn next(&mut self) -> Option<TemplateResult<TokenRef<'code>>> {

        if self.finished {
            return None;
        }

        if self.tokens.len() == 0 {
            self.collect_tokens();
        }

        self.tokens.pop_front()
    }
}

impl<'code, T> Expect<(usize, TokenValueRef<'code>)> for T where T: Iterator<Item=TemplateResult<TokenRef<'code>>> {
    type Output = TemplateResult<TokenRef<'code>>;

    fn expect(&mut self, (line, expected): (usize, TokenValueRef<'code>)) -> Self::Output {
        let maybe_token = self.next();
        match (maybe_token, expected) {
            (None, _) => return Err(
                TemplateError::ExpectedTokenTypeButReceived(
                    (expected.into(), Received::EndOfStream)
                ).at(line)
            ),
            (Some(Ok(token)), expected) => if token.value == expected {
                Ok(token)
            } else {
                return Err(
                    TemplateError::ExpectedTokenTypeButReceived(
                        (expected.into(), Received::Token(token.value.into()))
                    ).at(token.line)
                );
            },
            (Some(error), _) => error,
        }
    }
}

/// Iterator over tokens.
impl<'iteration, 'code> TokenIter<'iteration, 'code> {

    /// Create the iterator.
    pub fn new<'caller>(lexer: &'caller Lexer, code: &'code str) -> TokenIter<'caller, 'code> {
        // find all token starts in one go
        let positions = lexer.matchers.lex_tokens_start.captures_iter(code)
            .filter_map(|c| match c.is_empty() {
                true => None,
                false => Some(Position::from_capture(&lexer.options, c)),
            })
            .collect::<Vec<Position>>();

        let code_len = code.len();

        let iter = TokenIter {
            lexer: lexer,
            code: code,
            cursor: 0,
            current_var_block_line: None,
            line_num: 1,
            end: code_len,
            state: State::Data,
            states: Vec::new(),
            brackets: Vec::new(),
            position: 0,
            positions: positions,
            tokens: VecDeque::new(),
            is_error: false,
            finished: false,
        };

        iter
    }

    /// When we run out of tokens, we call this function to buffer more.
    fn collect_tokens(&mut self) {
        loop {
            if self.is_error {
                self.finished = true;
                break;
            }

            if self.cursor == self.end {
                match self.brackets.pop() {
                    Some(bracket) => {
                        self.push_error(
                            TemplateError::Unclosed(format!("{}", bracket.open)),
                            Some(bracket.line_num)
                        );
                        break;
                    },
                    _ => (),
                };

                self.finished = true;
                break;
            }

            if self.tokens.len() > 0 {
                break;
            }

            // dispatch to the lexing functions depending
            // on the current state
            match self.state {
                State::Data => self.lex_data(),
                State::Block => self.lex_block(),
                State::Var => self.lex_var(),
                State::String => self.lex_string(),
                State::Interpolation => self.lex_interpolation(),
            }
        }
    }

    fn lex_data(&mut self) {

        let positions_len = self.positions.len();

        // if no matches are left we return the rest of the template as simple text token
        if self.position == positions_len {
            let loc = self.cursor;

            self.push_token(TokenValueRef::Text(&self.code[loc..]));
            self.cursor = self.end;

            return;
        }

        // Find the first token after the current cursor
        let mut position = self.positions[self.position].clone(); self.position += 1;

        while position.loc < self.cursor {
            if self.position == positions_len {
                return;
            }
            position = self.positions[self.position].clone(); self.position += 1;
        }

        // push the template text first
        let loc = self.cursor;
        let text_content = &self.code[loc .. position.loc];

        self.push_token(
            if position.ws_trim {
                TokenValueRef::Text(text_content.trim_right())
            } else {
                TokenValueRef::Text(text_content)
            }
        );
        self.move_cursor(text_content.len() + position.all_len);

        match position.value {
            TokenValueRef::CommentStart => self.lex_comment(),
            TokenValueRef::BlockStart => {
                let loc = self.cursor;
                // raw data?
                if let Some(captures) = self.lexer.matchers.lex_block_raw.captures(&self.code[loc ..]) {
                    if let Some((start, end)) = captures.pos(0) {
                        if let Some(tag) = captures.at(1) {
                            self.move_cursor(end - start);
                            self.lex_raw_data(tag);
                            return;
                        }
                    } else {
                        unreachable!("twig bug: captured lex_block_raw but no capture data");
                    }
                }
                // {% line \d+ %}
                if let Some(captures) = self.lexer.matchers.lex_block_line.captures(&self.code[loc ..]) {
                    let maybe_start_and_end = captures.pos(0);
                    let maybe_line_num = captures.at(1);

                    match (maybe_start_and_end, maybe_line_num) {
                        (Some((start, end)), Some(line_num)) => {
                            self.move_cursor(end - start);
                            self.line_num = line_num.parse()
                                .ok()
                                .expect("twig bug: expected regexp matched as digit to be parseable as line number");
                            return;
                        },
                        _ => {
                            unreachable!("twig bug: captured lex_block_line but no capture data");
                        }
                    }
                }

                self.push_token(TokenValueRef::BlockStart);
                self.push_state(State::Block);
                self.current_var_block_line = Some(self.line_num);
            },
            TokenValueRef::VarStart => {
                self.push_token(TokenValueRef::VarStart);
                self.push_state(State::Var);
                self.current_var_block_line = Some(self.line_num);
            },
            _ => unreachable!("twig bug: lex_data match position.value"),
        }
    }

    fn lex_block(&mut self) {

        if 0 == self.brackets.len() {

            let loc = self.cursor;

            if let Some(captures) = self.lexer.matchers.lex_block.captures(&self.code[loc ..]) {

                if let Some((start, end)) = captures.pos(0) {
                    self.push_token(TokenValueRef::BlockEnd);
                    self.move_cursor(end - start);
                    self.pop_state();

                    return;
                } else {
                    unreachable!("twig bug: captured lex_block but no capture data");
                }
            }
        }

        self.lex_expression();
    }

    fn lex_var(&mut self) {

        if 0 == self.brackets.len() {

            let loc = self.cursor;

            if let Some(captures) = self.lexer.matchers.lex_var.captures(&self.code[loc ..]) {

                if let Some((start, end)) = captures.pos(0) {
                    self.push_token(TokenValueRef::VarEnd);
                    self.move_cursor(end - start);
                    self.pop_state();

                    return;
                } else {
                    unreachable!("twig bug: captured lex_var but no capture data");
                }
            }
        }

        self.lex_expression();
    }

    fn lex_expression(&mut self) {

        // whitespace
        let loc = self.cursor;
        if let Some(captures) = self.lexer.matchers.whitespace.captures(&self.code[loc ..]) {
            if let Some((start, end)) = captures.pos(0) {
                self.move_cursor(end - start);
                if self.cursor >= self.end {
                    let var_line = self.current_var_block_line;
                    self.push_error(
                        TemplateError::Unclosed(
                            match self.state {
                                State::Block => "block",
                                State::Var => "variable",
                                _ => unreachable!("twig bug: expected state at block or variable, but other state found"),
                            }.into()
                        ),
                        var_line
                    );
                    return;
                }
            } else {
                unreachable!("twig bug: captured whitespace but no capture data");
            }
        }

        // operators
        let loc = self.cursor;
        if let Some(captures) = self.lexer.matchers.lex_operator.captures(&self.code[loc ..]) {
            if let Some((start, end)) = captures.pos(0) {
                let op_str = self.code[loc + start .. loc + end].trim_right();

                self.push_token(TokenValueRef::Operator(op_str));
                self.move_cursor(end - start);

                return;
            } else {
                // Just skip, it is not op.
            }
        }

        // names
        let loc = self.cursor;
        if let Some(captures) = self.lexer.matchers.regex_name.captures(&self.code[loc ..]) {
            if let Some((start, end)) = captures.pos(0) {
                self.push_token(TokenValueRef::Name(&self.code[loc + start .. loc + end]));
                self.move_cursor(end - start);

                return;
            } else {
                unreachable!("twig bug: captured regex_name but no capture data");
            }
        }

        // numbers
        let loc = self.cursor;
        if let Some(captures) = self.lexer.matchers.regex_number.captures(&self.code[loc ..]) {
            if let Some((start, end)) = captures.pos(0) {
                let string = captures.at(0).unwrap(); // we checked that (0) exists above.

                let all_chars_are_digits = string.chars().all(|c| c.is_digit(10));
                let twig_number = if all_chars_are_digits {
                    let maybe_int = string.parse();
                    match maybe_int {
                        Ok(int) => ConstNumberRef::Int(int),
                        _ => ConstNumberRef::Big(string),
                    }
                } else {
                    let maybe_float = string.parse::<f64>();
                    match maybe_float {
                        Ok(float) => {
                            if float.is_finite() {
                                ConstNumberRef::Float(float)
                            } else {
                                ConstNumberRef::Big(string)
                            }
                        },
                        _ => ConstNumberRef::Big(string),
                    }
                };

                self.push_token(TokenValueRef::Value(ConstRef::Num(twig_number)));
                self.move_cursor(end - start);

                return;
            } else {
                unreachable!("twig bug: captured regex_number but no capture data");
            }
        }

        // punctuation
        let loc = self.cursor;
        if let Some(c) = self.code[loc..].chars().next() {
            if PUNCTUATION.contains(c) {

                let line_num = self.line_num;

                // opening bracket
                if "([{".contains(c) {
                    self.brackets.push(Bracket::from_char(c, line_num));
                } else if ")]}".contains(c) {
                    match self.brackets.pop() {
                        Some(expect) => {
                            if expect.close != BracketSymbol::Char(c) {
                                self.push_error(
                                    TemplateError::Unclosed(
                                        format!("{}", expect.open)
                                    ),
                                    Some(expect.line_num)
                                );
                                return;
                            }
                        },
                        None => {
                            self.push_error(
                                TemplateError::Unexpected(
                                    format!("{}", c)
                                ),
                                Some(line_num)
                            );
                            return;
                        }
                    }
                }

                self.push_token(TokenValueRef::Punctuation(c));
                self.move_cursor(1);

                return;
            }
        }

        // strings
        let loc = self.cursor;
        if let Some(captures) = self.lexer.matchers.regex_string.captures(&self.code[loc ..]) {
            if let Some((start, end)) = captures.pos(0) {
                self.push_token(TokenValueRef::Value(ConstRef::Str(
                    &self.code[loc + start + 1 .. loc + end - 1]
                )));
                self.move_cursor(end - start);

                return;
            } else {
                unreachable!("twig bug: captured regex_string but no capture data");
            }
        }

        // opening double quoted string
        let loc = self.cursor;
        if self.lexer.matchers.regex_dq_string_delim.is_match(&self.code[loc ..]) {
            self.brackets.push(Bracket::from_char('"', self.line_num));
            self.push_state(State::String);
            self.move_cursor(1);

            return;
        }

        let next_char = &self.code[loc .. loc + 1];
        let line_num = self.line_num;
        self.push_error(
            TemplateError::UnexpectedCharacter(
                format!("{}", next_char)
            ),
            Some(line_num)
        );
    }

    fn lex_string(&mut self) {

        let loc = self.cursor;

        if let Some(captures) = self.lexer.matchers.interpolation_start.captures(&self.code[loc ..]) {
            if let Some((start, end)) = captures.pos(0) {
                self.brackets.push(Bracket::new(BracketSymbol::IntStart, self.line_num));
                self.push_token(TokenValueRef::InterpolationStart);
                self.move_cursor(end - start);
                self.push_state(State::Interpolation);

                return;
            } else {
                unreachable!("twig bug: captured interpolation_start but no capture data");
            }
        }

        let (_, part_end) = self.lexer.matchers.match_regex_dq_string_part(&self.code[loc ..]);
        if part_end > 0 {
            self.push_token(TokenValueRef::Value(ConstRef::Str(
                &self.code[loc .. loc + part_end]
            )));
            self.move_cursor(part_end);

            return;
        }

        if self.lexer.matchers.regex_dq_string_delim.is_match(&self.code[loc ..]) {
            let last_bracket = self.brackets.pop();

            match last_bracket {
                Some(Bracket { close: BracketSymbol::Char('"'), .. }) => {
                    self.pop_state();
                    self.move_cursor(1);
                },
                Some(other_bracket) => {
                    self.push_error(
                        TemplateError::Unclosed(
                            format!("{}", other_bracket.open)
                        ),
                        Some(other_bracket.line_num)
                    );
                },
                None => unreachable!("twig bug: expected bracket when lexng string end"),
            }
        }
    }

    fn lex_interpolation(&mut self) {

        let in_interpolation = match self.brackets.last() {
            Some(bracket) if bracket.open == BracketSymbol::IntStart => true,
            _ => false,
        };

        if in_interpolation {
            let loc = self.cursor;
            if let Some(captures) = self.lexer.matchers.interpolation_end.captures(&self.code[loc ..]) {
                if let Some((start, end)) = captures.pos(0) {
                    self.brackets.pop();
                    self.push_token(TokenValueRef::InterpolationEnd);
                    self.move_cursor(end - start);
                    self.pop_state();

                    return;
                } else {
                    unreachable!("twig bug: captured interpolation_end but no capture data");
                }
            }
        }

        self.lex_expression();
    }

    fn lex_comment(&mut self) {

        let loc = self.cursor;
        let maybe_found = self.lexer.matchers.lex_comment.find(&self.code[loc ..]);

        match maybe_found {
            Some((_, end)) => {
                self.move_cursor(end);
            },
            None => {
                let line_num = self.line_num;
                self.push_error(TemplateError::UnclosedComment, Some(line_num));
            }
        };
    }

    fn lex_raw_data(&mut self, tag: &'code str) {
        let loc = self.cursor;
        let maybe_captures = {
            match tag {
                "raw" => self.lexer.matchers.lex_raw_data.captures(&self.code[loc ..]),
                "verbatim" => self.lexer.matchers.lex_verbatim_data.captures(&self.code[loc ..]),
                _ => unreachable!("twig bug: expected raw or verbatim tag, but got {}", tag),
            }
        };

        match maybe_captures {
            Some(captures) => {
                let maybe_full = captures.pos(0);
                let maybe_end = captures.at(1);

                match (maybe_full, maybe_end) {
                    (Some((start, end)), Some(end_text)) => {
                        let mut text = &self.code[loc..loc + start];
                        self.move_cursor(end - start);

                        if end_text.contains("-") {
                            text = text.trim_right()
                        }

                        self.push_token(TokenValueRef::Text(text));
                    },
                    _ => unreachable!("twig bug: captured lex_raw_data but no capture data"),
                }
            },
            None => {
                let line_num = self.line_num;
                self.push_error(
                    TemplateError::UnclosedBlock(
                        format!("{}", tag)
                    ),
                    Some(line_num)
                );
            }
        };
    }

    fn push_token(&mut self, token_value: TokenValueRef<'code>) {
        // do not push empty text tokens
        if let TokenValueRef::Text(ref text) = token_value {
            if text.len() == 0 {
                return;
            }
        }

        self.tokens.push_back(Ok(TokenRef { value: token_value, line: self.line_num }));
    }

    fn push_error(&mut self, message: TemplateError, line_num: Option<usize>) {
        self.tokens.push_back(Err(
            message.at(match line_num {
                Some(line) => line,
                None => unreachable!("twig bug: error should not be pushed without a line number"),
            })
        ));
        self.is_error = true;
    }

    fn push_state(&mut self, state: State) {
        self.states.push(self.state);
        self.state = state;
    }

    fn pop_state(&mut self) {
        match self.states.pop() {
            Some(state) => {
                self.state = state;
            },
            None => panic!("twig bug: cannot pop state without a previous state"),
        }
    }

    fn move_cursor(&mut self, offset: usize) {
        let prev_loc = self.cursor;

        self.cursor += offset;

        let mut lines = 0;
        for c in self.code[prev_loc .. self.cursor].chars() {
            if c == '\n' {
                lines += 1;
            }
        }

        self.line_num += lines;
    }
}