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

use std::collections::HashMap;

use data;
use parser::{ParseError, Parser};
use parser;

use self::ResolveError::*;
use self::ResolveTarget::*;


pub fn compile(source: &str) -> Result<HashMap<String, parser::Entry>, ParseError> {
    let p = Parser::new(source.chars());
    let entries = try!(p.parse());

    let mut map = HashMap::new();

    for mut entry in entries {
        let id = match entry {
            parser::Comment(..) => continue,
            parser::Macro(ref id, _, _) => id.clone(),
            parser::Entity(ref id, ref mut value, ref indices, ref mut attrs)    => {
                // while we're here, fix up and Hash values with default indices
                match *value {
                    parser::Hash(..) => {
                        if indices.len() > 0 {
                            add_default_indices(value, indices.iter());
                        }
                    },
                    _ => {}
                };
                for &mut parser::Attr(_, ref mut value, ref indices) in attrs.iter_mut() {
                    match *value {
                        parser::Hash(..) => {
                            if indices.len() > 0 {
                                add_default_indices(value, indices.iter());
                            }
                        },
                        _ => {}
                    };
                }

                id.clone()
            }
        };
        map.insert(id, entry);
    }

    Ok(map)
}


fn add_default_indices<'r, I: Iterator<Item=&'r parser::Expr> + Clone>(value: &mut parser::Value, mut indices: I) {
    match *value {
        parser::Hash(ref mut map, _, ref mut def_index) => {
            match indices.next() {
                Some(idx) => {
                    for (_k, v) in map.iter_mut() {
                        add_default_indices(v, indices.clone());
                    }
                    *def_index = Some(Box::new(idx.clone()));
                },
                None => {}
            }
        },
        _ => {}
    }
}

pub type Env = HashMap<String, parser::Entry>;

pub struct ResolveContext<'a> {
    data: &'a data::Data,
    env: &'a Env,
    locals: Option<&'a data::Data>,
    index: Option<String>,
}

impl<'a> ResolveContext<'a> {
    pub fn new(env: &'a Env, data: &'a data::Data) -> ResolveContext<'a> {
        ResolveContext {
            env: env,
            data: data,
            locals: None,
            index: None,
        }
    }

    fn with_locals(&'a self, locals: &'a data::Data) -> ResolveContext<'a> {
        ResolveContext {
            env: self.env,
            data: self.data,
            locals: Some(locals),
            index: None,
        }
    }

    fn with_index(&'a self, index: Option<String>) -> ResolveContext<'a> {
        ResolveContext {
            env: self.env,
            data: self.data,
            locals: self.locals,
            index: index,
        }
    }
}

pub type ResolveResult = Result<ResolveTarget, ResolveError>;

pub enum ResolveTarget {
    Entry(parser::Entry),
    Value(parser::Value),
    Data(data::Data)
}

/// Errors that can occur when resolving a set of l20n resources into strings.
/// These errors are cause by problems in the l20n file, or incorrect Data
/// provided when localizing.
#[derive(Debug)]
pub enum ResolveError {
    /// A resource received a value of the wrong type.
    WrongType,
    /// A macro was called with the wrong number of arguments.
    WrongNumberOfArgs,
    /// Accessed an index of a Hash that does not exist.
    MissingIndex,
    /// Accessed an attribute of an entity that does not exist.
    MissingAttr,
    /// Tried to use a $var that did not exist in the provided Data.
    MissingVar(String),
    /// A string tried to use another string in the l20n resource that did not
    /// exist.
    MissingIdent(String),
}

/// Resolve an L20n resource into Data.
pub trait Resolve {

    /// Resolves this value a step. It could resolve to another Value, or
    /// resolve completely to a Data.
    fn resolve(&self, ctx: &ResolveContext) -> ResolveResult;

    /// Keeps resolving until a Data value is returned.
    fn resolve_data(&self, ctx: &ResolveContext) -> Result<data::Data, ResolveError> {
        match self.resolve(ctx) {
            Ok(Data(d)) => Ok(d),
            Ok(other) => other.resolve_data(ctx),
            Err(e) => Err(e)
        }
    }
}

impl Resolve for ResolveTarget {
    fn resolve(&self, ctx: &ResolveContext) -> ResolveResult {
        match *self {
            Data(ref d) => Ok(Data(d.clone())),
            Entry(ref e) => e.resolve(ctx),
            Value(ref v) => v.resolve(ctx)
        }
    }
}

impl Resolve for parser::Entry {
    fn resolve(&self, _ctx: &ResolveContext) -> ResolveResult {
        match *self {
            parser::Entity(_, ref value, _, _) => {
                Ok(Value(value.clone()))
            }
            _ => Ok(Data(data::Null))
        }
    }
}

impl Resolve for parser::Value {
    fn resolve(&self, ctx: &ResolveContext) -> ResolveResult {
        match *self {
            parser::Str(ref s) => Ok(Data(data::Str(s.clone()))),
            parser::ComplexStr(ref exprs) => {
                let mut vec = Vec::with_capacity(exprs.len());
                for expr in exprs.iter() {
                    vec.push(match expr.resolve_data(ctx) {
                        Ok(data::Str(s)) => s,
                        Ok(data::Num(n)) => format!("{}", n),
                        Ok(_) => return Err(WrongType),
                        Err(e) => return Err(e)
                    });
                }
                Ok(Data(data::Str(vec.concat())))
            }
            parser::Hash(ref map, ref def_key, ref def_index) => {
                match ctx.index {
                    Some(ref s) => match map.get(s) {
                            Some(v) => return Ok(Value(v.clone())),
                            None => {}
                    },
                    None => {}
                };
                match *def_key {
                    Some(ref s) => match map.get(s) {
                            Some(v) => return Ok(Value(v.clone())),
                            None => {}
                    },
                    None => {}
                };
                match *def_index {
                    Some(ref e) => match e.resolve_data(ctx) {
                            Ok(data::Str(ref s)) => match map.get(s) {
                                Some(v) => return Ok(Value(v.clone())),
                                None => {}
                            },
                            Ok(_) => return Err(WrongType),
                            Err(e) => return Err(e)
                    },
                    None => {}
                };
                Err(MissingIndex)
            }
        }
    }
}

impl Resolve for parser::Expr {
    fn resolve(&self, ctx: &ResolveContext) -> ResolveResult {
        match *self {
            parser::ValExpr(ref val) => Ok(Value(val.clone())),
            parser::NumExpr(ref n) => Ok(Data(data::Num(*n))),
            parser::BinExpr(ref left, ref op, ref right) => {
                let left = try!(left.resolve_data(ctx));
                let right = try!(right.resolve_data(ctx));
                match (*op, left, right) {
                    // math ops
                    (parser::BiAdd, data::Num(l), data::Num(r)) => Ok(Data(data::Num(l + r))),
                    (parser::BiSub, data::Num(l), data::Num(r)) => Ok(Data(data::Num(l - r))),
                    (parser::BiMul, data::Num(l), data::Num(r)) => Ok(Data(data::Num(l * r))),
                    (parser::BiDiv, data::Num(l), data::Num(r)) => Ok(Data(data::Num(l / r))),
                    (parser::BiRem, data::Num(l), data::Num(r)) => Ok(Data(data::Num(l % r))),

                    (parser::BiLt, data::Num(l), data::Num(r)) => Ok(Data(data::Bool(l < r))),
                    (parser::BiLe, data::Num(l), data::Num(r)) => Ok(Data(data::Bool(l <= r))),
                    (parser::BiGt, data::Num(l), data::Num(r)) => Ok(Data(data::Bool(l > r))),
                    (parser::BiGe, data::Num(l), data::Num(r)) => Ok(Data(data::Bool(l >= r))),

                    // logical ops
                    (parser::BiAnd, data::Bool(l), data::Bool(r)) => Ok(Data(data::Bool(l && r))),
                    (parser::BiOr, data::Bool(l), data::Bool(r)) => Ok(Data(data::Bool(l || r))),

                    // equality ops. can be Num, Bool, or Str
                    (parser::BiEq, data::Bool(l), data::Bool(r)) => Ok(Data(data::Bool(l == r))),
                    (parser::BiEq, data::Str(l), data::Str(r)) => Ok(Data(data::Bool(l == r))),
                    (parser::BiEq, data::Num(l), data::Num(r)) => Ok(Data(data::Bool(l == r))),
                    (parser::BiNe, data::Bool(l), data::Bool(r)) => Ok(Data(data::Bool(l == r))),
                    (parser::BiNe, data::Str(l), data::Str(r)) => Ok(Data(data::Bool(l == r))),
                    (parser::BiNe, data::Num(l), data::Num(r)) => Ok(Data(data::Bool(l != r))),

                    (_, _, _) => Err(WrongType)
                }
            }
            parser::UnExpr(ref op, ref expr) => {
                let expr = try!(expr.resolve_data(ctx));
                match (*op, expr) {
                    (parser::UnAdd, data::Num(n)) => Ok(Data(data::Num(n))),
                    (parser::UnSub, data::Num(n)) => Ok(Data(data::Num(-n))),
                    (parser::UnNot, data::Bool(b)) => Ok(Data(data::Bool(!b))),
                    _ => Err(WrongType)
                }
            }
            parser::VarExpr(ref name) => {
                match ctx.locals.and_then(|locals| locals.get(name)) {
                    Some(val) => return Ok(Data(val.clone())),
                    _ => {}
                };
                match ctx.data.get(name) {
                    Some(d) => Ok(Data(d.clone())),
                    None => Err(MissingVar(name.clone()))
                }
            }
            parser::IdentExpr(ref ident) => {
                match ctx.env.get(ident) {
                    Some(e) => Ok(Entry(e.clone())),
                    None => Err(MissingIdent(ident.clone()))
                }
            }
            parser::CondExpr(ref cond, ref consequent, ref alt) => {
                match try!(cond.resolve_data(ctx)) {
                    data::Bool(b) => {
                        if b {
                            consequent.resolve(ctx)
                        } else {
                            alt.resolve(ctx)
                        }
                    },
                    _ => Err(WrongType)
                }
            }
            parser::CallExpr(ref ident, ref args) => {
                match **ident {
                    parser::IdentExpr(ref ident) => {
                        match ctx.env.get(ident) {
                            Some(&parser::Macro(_, ref arg_names, ref body)) => {
                                if args.len() == arg_names.len() {
                                    let mut map = HashMap::new();
                                    for (k, v) in arg_names.iter().zip(args.iter()) {
                                        let name = match k {
                                            &parser::VarExpr(ref name) => name.clone(),
                                            // not a VarExpr would be the parser going nuts
                                            _ => unreachable!()
                                        };
                                        let arg = match v.resolve_data(ctx) {
                                            Ok(val) => val,
                                            Err(e) => return Err(e)
                                        };
                                        map.insert(name, arg);
                                    }
                                    let locals = data::Map(map);
                                    body.resolve(&ctx.with_locals(&locals))
                                } else {
                                    Err(WrongNumberOfArgs)
                                }
                            }
                            Some(_) => Err(WrongType),
                            None => Err(MissingIdent(ident.clone()))
                        }
                    },
                    _ => panic!("CallExpr may only have IdentExpr in first spot")
                }
            }
            parser::PropExpr(ref parent, ref prop, ref access) => {
                let prop = match *access {
                    parser::Computed => match prop.resolve_data(ctx) {
                        Ok(data::Str(s)) => s,
                        Ok(_) => return Err(WrongType),
                        Err(e) => return Err(e)
                    },
                    parser::Static => match **prop {
                        parser::IdentExpr(ref s) => s.clone(),
                        _ => return Err(WrongType)
                    }
                };

                match parent.resolve(ctx) {
                    Ok(Data(data::Map(ref m))) => {
                        match m.get(&prop) {
                            Some(d) => Ok(Data(d.clone())),
                            None => Err(MissingIndex)
                        }
                    },
                    Ok(Entry(ref e)) => {
                        match e.resolve(ctx) {
                            Ok(Value(ref v)) => v.resolve(&ctx.with_index(Some(prop))),
                            Ok(_) => Err(WrongType),
                            Err(e) => Err(e)
                        }
                    },
                    Ok(Value(ref v)) => {
                        v.resolve(&ctx.with_index(Some(prop)))
                    },
                    Ok(_) => Err(WrongType),
                    Err(e) => Err(e)
                }
            }
            parser::Expr::AttrExpr(ref parent, ref prop, ref access) => {
                let prop = match *access {
                    parser::AccessType::Computed => match prop.resolve_data(ctx) {
                        Ok(data::Str(s)) => s,
                        Ok(_) => return Err(WrongType),
                        Err(e) => return Err(e)
                    },
                    parser::AccessType::Static => match **prop {
                        parser::Expr::IdentExpr(ref s) => s.clone(),
                        _ => return Err(WrongType)
                    }
                };

                match parent.resolve(ctx) {
                    Ok(Entry(parser::Entry::Entity(_, _, _, ref attrs))) => {
                        for &parser::Attr(ref id, ref value, _) in attrs.iter() {
                            if *id == prop {
                                return value.resolve(ctx)
                            }
                        }
                        Err(MissingAttr)
                    },
                    Ok(_) => Err(WrongType),
                    Err(e) => Err(e)
                }
            }
            ref e => panic!("{:?} not yet implemented", e)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{compile, Resolve, ResolveContext};
    use data::{Str, Null};

    #[test]
    fn test_compile() {
        let map = compile("<hi 'hello world'>").unwrap();
        let entity = &map["hi"];
        let data = Null;
        let ctx = ResolveContext::new(&map, &data);

        assert_eq!(entity.resolve_data(&ctx).unwrap(), Str(String::from("hello world")));

    }
}