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
use value::{ TwigValueRef, TwigValue };

/// Lexer output token, lexer's output and parser's input.
#[derive(Debug, Clone)]
pub struct Token<'a> {
    pub value: Value<'a>,
    pub line: usize,
}

/// Token value.
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum Value<'a> {
    Text(&'a str),
    BlockStart,
    VarStart,
    BlockEnd,
    VarEnd,
    Name(&'a str),
    Value(TwigValueRef<'a>),
    Operator(&'a str),
    Punctuation(char),
    InterpolationStart,
    InterpolationEnd,
    CommentStart, // Not in vanilla Twig.
}

impl<'a> Into<DebugValue> for Value<'a> {
    fn into(self) -> DebugValue {
        match self {
            Value::Text(t) => DebugValue::Text(t.into()),
            Value::BlockStart => DebugValue::BlockStart,
            Value::VarStart => DebugValue::VarStart,
            Value::BlockEnd => DebugValue::BlockEnd,
            Value::VarEnd => DebugValue::VarEnd,
            Value::Name(n) => DebugValue::Name(n.into()),
            Value::Value(v) => DebugValue::Value(v.into()),
            Value::Operator(s) => DebugValue::Operator(s.into()),
            Value::Punctuation(s) => DebugValue::Punctuation(s.into()),
            Value::InterpolationStart => DebugValue::InterpolationStart,
            Value::InterpolationEnd => DebugValue::InterpolationEnd,
            Value::CommentStart => DebugValue::CommentStart,
        }
    }
}

/// Token value.
#[derive(PartialEq, Debug, Clone)]
pub enum DebugValue {
    Text(String),
    BlockStart,
    VarStart,
    BlockEnd,
    VarEnd,
    Name(String),
    Value(TwigValue),
    Operator(String),
    Punctuation(char),
    InterpolationStart,
    InterpolationEnd,
    CommentStart, // Not in vanilla Twig.
}

impl DebugValue {
    /// Return english name and value for token.
    pub fn get_english(&self) -> (&'static str, Option<String>) {
        match *self {
            DebugValue::Text(ref v) => ("text", Some(v.to_string())),
            DebugValue::BlockStart => ("begin of statement block", None),
            DebugValue::VarStart => ("begin of print statement", None),
            DebugValue::BlockEnd => ("end of statement block", None),
            DebugValue::VarEnd => ("end of print statement", None),
            DebugValue::Name(ref n) => ("name", Some(n.to_string())),
            DebugValue::Value(TwigValue::Num(ref n)) => ("number", Some(n.to_string())),
            DebugValue::Value(TwigValue::Str(ref s)) => ("string", Some(s.to_string())),
            DebugValue::Operator(ref s) => ("operator", Some(s.to_string())),
            DebugValue::Punctuation(s) => ("punctuation", Some(s.to_string())),
            DebugValue::InterpolationStart => ("begin of string interpolation", None),
            DebugValue::InterpolationEnd => ("end of string interpolation", None),
            DebugValue::CommentStart => ("comment start", None),
        }
    }
}