using System; using System.Collections.Generic; using mustache.Properties; namespace mustache { internal sealed class Scope { private readonly object topLevel; private Scope parent; public Scope(object topLevel) { parent = null; this.topLevel = topLevel; } public Scope CreateChildScope(object topLevel) { Scope scope = new Scope(topLevel); scope.parent = this; return scope; } public object Find(string name) { string[] names = name.Split('.'); string member = names[0]; object nextLevel = topLevel; if (member != "this") { nextLevel = find(member); } for (int index = 1; index < names.Length; ++index) { IDictionary context = toLookup(nextLevel); member = names[index]; nextLevel = context[member]; } return nextLevel; } private object find(string name) { IDictionary lookup = toLookup(topLevel); if (lookup.ContainsKey(name)) { return lookup[name]; } if (parent == null) { string message = String.Format(Resources.KeyNotFound, name); throw new KeyNotFoundException(message); } return parent.find(name); } private static IDictionary toLookup(object value) { IDictionary lookup = value as IDictionary; if (lookup == null) { lookup = new PropertyDictionary(value); } return lookup; } } }