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
use self::Token::*;
use self::Element::*;
use self::ComparisonOperator::*;
use regex::Regex;
use error::{Error, Result};

#[derive(Clone, Debug, PartialEq)]
pub enum ComparisonOperator {
    Equals,
    NotEquals,
    LessThan,
    GreaterThan,
    LessThanEquals,
    GreaterThanEquals,
    Contains,
}

#[derive(Clone, Debug, PartialEq)]
pub enum Token {
    Pipe,
    Dot,
    Colon,
    Comma,
    OpenSquare,
    CloseSquare,
    OpenRound,
    CloseRound,
    Question,
    Dash,

    Identifier(String),
    StringLiteral(String),
    NumberLiteral(f32),
    DotDot,
    Comparison(ComparisonOperator),
}

#[derive(Clone, Debug, PartialEq)]
pub enum Element {
    Expression(Vec<Token>, String),
    Tag(Vec<Token>, String),
    Raw(String),
}

fn split_blocks(text: &str) -> Vec<&str> {
    let markup = Regex::new("\\{%.*?%\\}|\\{\\{.*?\\}\\}").unwrap();
    let mut tokens = vec![];
    let mut current = 0;
    for (begin, end) in markup.find_iter(text) {
        match &text[current..begin] {
            "" => {}
            t => tokens.push(t),
        }
        tokens.push(&text[begin..end]);
        current = end;
    }
    match &text[current..text.len()] {
        "" => {}
        t => tokens.push(t),
    }
    tokens
}

pub fn tokenize(text: &str) -> Result<Vec<Element>> {
    let expression = Regex::new("\\{\\{(.*?)\\}\\}").unwrap();
    let tag = Regex::new("\\{%(.*?)%\\}").unwrap();

    let mut blocks = vec![];

    for block in split_blocks(text) {
        if let Some(caps) = tag.captures(block) {
            blocks.push(Tag(try!(granularize(caps.at(1).unwrap_or(""))),
                            block.to_string()));
        } else if let Some(caps) = expression.captures(block) {
            blocks.push(Expression(try!(granularize(caps.at(1).unwrap_or(""))),
                                   block.to_string()));
        } else {
            blocks.push(Raw(block.to_string()));
        }
    }

    Ok(blocks)
}

fn split_atom(block: &str) -> Vec<String> {

    let mut vec = vec![];
    let mut buff = String::new();
    for c in block.chars() {
        if c == ' ' {
            if !buff.is_empty() {
                vec.push(buff.clone());
                buff.clear();
            }
        } else if c == ',' || c == ':' {
            if !buff.is_empty() { vec.push(buff.clone()) };
            vec.push(c.to_string());
            buff.clear();
        } else {
            buff.push(c);
        }
    }
    vec.push(buff.clone());
    vec
}

fn granularize(block: &str) -> Result<Vec<Token>> {
    let identifier = Regex::new(r"[a-zA-Z_][\w-]*\??").unwrap();
    let single_string_literal = Regex::new(r"'[^']*'").unwrap();
    let double_string_literal = Regex::new("\"[^\"]*\"").unwrap();
    let number_literal = Regex::new(r"^-?\d+(\.\d+)?$").unwrap();
    let dotdot = Regex::new(r"\.\.").unwrap();

    let mut result = vec![];

    for el in split_atom(block) {
        if el == "" {
            continue;
        }
        result.push(match &*el {
            "|" => Pipe,
            "." => Dot,
            ":" => Colon,
            "," => Comma,
            "[" => OpenSquare,
            "]" => CloseSquare,
            "(" => OpenRound,
            ")" => CloseRound,
            "?" => Question,
            "-" => Dash,

            "==" => Comparison(Equals),
            "!=" => Comparison(NotEquals),
            "<=" => Comparison(LessThanEquals),
            ">=" => Comparison(GreaterThanEquals),
            "<" => Comparison(LessThan),
            ">" => Comparison(GreaterThan),
            "contains" => Comparison(Contains),

            x if dotdot.is_match(x) => DotDot,
            x if single_string_literal.is_match(x) => StringLiteral(x[1..x.len() - 1].to_string()),
            x if double_string_literal.is_match(x) => StringLiteral(x[1..x.len() - 1].to_string()),
            x if number_literal.is_match(x) => NumberLiteral(x.parse::<f32>().expect(&format!("Could not parse {:?} as float", x))),
            x if identifier.is_match(x) => Identifier(x.to_string()),
            x => return Err(Error::Lexer(format!("{} is not a valid identifier", x))),
        });
    }

    Ok(result)
}

#[test]
fn test_split_blocks() {
    assert_eq!(split_blocks("asdlkjfn\n{{askdljfbalkjsdbf}} asdjlfb"),
               vec!["asdlkjfn\n", "{{askdljfbalkjsdbf}}", " asdjlfb"]);
    assert_eq!(split_blocks("asdlkjfn\n{%askdljfbalkjsdbf%} asdjlfb"),
               vec!["asdlkjfn\n", "{%askdljfbalkjsdbf%}", " asdjlfb"]);
}

#[test]
fn test_split_atom() {
    assert_eq!(split_atom("truc | arg:val"), vec!["truc", "|", "arg", ":", "val"]);
    assert_eq!(split_atom("truc | filter:arg1,arg2"), vec!["truc", "|", "filter", ":", "arg1", ",", "arg2"]);
}

#[test]
fn test_tokenize() {
    assert_eq!(tokenize("{{hello 'world'}}").unwrap(),
               vec![Expression(vec![Identifier("hello".to_string()),
                                    StringLiteral("world".to_string())],
                               "{{hello 'world'}}".to_string())]);
    assert_eq!(tokenize("{{hello.world}}").unwrap(),
               vec![Expression(vec![Identifier("hello.world".to_string())],
                               "{{hello.world}}".to_string())]);
    assert_eq!(tokenize("{{ hello 'world' }}").unwrap(),
               vec![Expression(vec![Identifier("hello".to_string()),
                                    StringLiteral("world".to_string())],
                               "{{ hello 'world' }}".to_string())]);
    assert_eq!(tokenize("{{   hello   'world'    }}").unwrap(),
               vec![Expression(vec![Identifier("hello".to_string()),
                                    StringLiteral("world".to_string())],
                               "{{   hello   'world'    }}".to_string())]);
    assert_eq!(tokenize("wat\n{{hello 'world'}} test").unwrap(),
               vec![Raw("wat\n".to_string()),
                    Expression(vec![Identifier("hello".to_string()),
                                    StringLiteral("world".to_string())],
                               "{{hello 'world'}}".to_string()),
                    Raw(" test".to_string())]);
}

#[test]
fn test_granularize() {
    assert_eq!(granularize("test me").unwrap(),
               vec![Identifier("test".to_string()), Identifier("me".to_string())]);
    assert_eq!(granularize("test == me").unwrap(),
               vec![Identifier("test".to_string()),
                    Comparison(Equals),
                    Identifier("me".to_string())]);
    assert_eq!(granularize("'test' == \"me\"").unwrap(),
               vec![StringLiteral("test".to_string()),
                    Comparison(Equals),
                    StringLiteral("me".to_string())]);
    assert_eq!(granularize("test | me:arg").unwrap(),
               vec![Identifier("test".to_string()), Pipe,
               Identifier("me".to_string()), Colon, Identifier("arg".to_string())]);
    assert_eq!(granularize("test | me:arg1,arg2").unwrap(),
               vec![Identifier("test".to_string()), Pipe,
               Identifier("me".to_string()), Colon, Identifier("arg1".to_string()),
               Comma, Identifier("arg2".to_string())]);
    assert_eq!(granularize("test | me : arg1, arg2").unwrap(),
               vec![Identifier("test".to_string()), Pipe,
               Identifier("me".to_string()), Colon, Identifier("arg1".to_string()),
               Comma, Identifier("arg2".to_string())]);
}