コード例 #1
0
 //--- Class Methods ---
 private static void FillDictionaryRecursively(DekiScriptMap map, Dictionary <string, DekiScriptLiteral> dictionary)
 {
     if (map.Outer != null)
     {
         FillDictionaryRecursively(map.Outer, dictionary);
     }
     foreach (var entry in map._value)
     {
         dictionary[entry.Key] = entry.Value;
     }
 }
コード例 #2
0
 public DekiScriptMap(Hashtable value, DekiScriptMap outer)
 {
     this.Outer = outer;
     if (value != null)
     {
         foreach (DictionaryEntry entry in value)
         {
             Add(entry.Key.ToString(), FromNativeValue(entry.Value));
         }
     }
 }
コード例 #3
0
        public DekiScriptMap AddRange(DekiScriptMap map)
        {
            // check if colleciton is readonly
            if (_readonly)
            {
                throw new ReadOnlyException("map is read-only");
            }

            // add values
            foreach (KeyValuePair <string, DekiScriptLiteral> entry in map.Value)
            {
                _value[entry.Key] = entry.Value;
            }
            return(this);
        }
コード例 #4
0
        public DekiScriptMap AddAt(string[] keys, DekiScriptLiteral value, bool ignoreOnError)
        {
            if (ArrayUtil.IsNullOrEmpty(keys))
            {
                throw new ArgumentNullException("keys");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            // loop over all keys, except last, to get to the last map
            string        key;
            DekiScriptMap current = this;

            for (int i = 0; i < keys.Length - 1; ++i)
            {
                if (keys[i] == null)
                {
                    if (ignoreOnError)
                    {
                        return(this);
                    }
                    throw new ArgumentException(string.Format("keys[{0}] is null", i));
                }
                key = keys[i];
                DekiScriptLiteral next = current[key];
                if (next.ScriptType == DekiScriptType.NIL)
                {
                    next = new DekiScriptMap();
                    current.Add(key, next);
                }
                else if (next.ScriptType != DekiScriptType.MAP)
                {
                    if (ignoreOnError)
                    {
                        return(this);
                    }
                    throw new Exception(string.Format("entry at '{0}' is not a map", string.Join(".", keys, 0, i + 1)));
                }
                current = (DekiScriptMap)next;
            }

            // add new item using the final key
            current.Add(keys[keys.Length - 1], value);
            return(this);
        }
コード例 #5
0
        public DekiScriptLiteral GetAt(string[] keys)
        {
            if (ArrayUtil.IsNullOrEmpty(keys))
            {
                return(DekiScriptNil.Value);
            }
            DekiScriptMap container = this;

            for (int i = 0; i < keys.Length - 1; ++i)
            {
                container = container[keys[i]] as DekiScriptMap;
                if (container == null)
                {
                    return(DekiScriptNil.Value);
                }
            }
            return(container[keys[keys.Length - 1]]);
        }
コード例 #6
0
        public Empty Visit(DekiScriptMap expr, StringBuilder state)
        {
            state.Append("{");

            // convert values to an array so they can be sorted
            var values = new List <KeyValuePair <string, DekiScriptLiteral> >();

            foreach (KeyValuePair <string, DekiScriptLiteral> entry in expr.Value)
            {
                values.Add(entry);
            }
            values.Sort((left, right) => left.Key.CompareInvariantIgnoreCase(right.Key));

            // emit values
            bool first = true;

            foreach (KeyValuePair <string, DekiScriptLiteral> entry in values)
            {
                if (!first)
                {
                    state.Append(", ");
                }
                else
                {
                    state.Append(" ");
                }
                first = false;
                if (DekiScriptParser.IsIdentifier(entry.Key) || DekiScriptParser.IsNumber(entry.Key))
                {
                    state.Append(entry.Key);
                }
                else
                {
                    state.Append(entry.Key.QuoteString());
                }
                state.Append(" : ");
                entry.Value.VisitWith(this, state);
            }
            state.Append(" }");
            return(Empty.Value);
        }
コード例 #7
0
        public DekiScriptLiteral this[string name] {
            get {
                DekiScriptLiteral result;
                if (!_value.TryGetValue(name, out result))
                {
                    if (Outer != null)
                    {
                        return(Outer[name]);
                    }
                    return(DekiScriptNil.Value);
                }
                return(result);
            }
            set {
                // check if collection is readonly
                if (_readonly)
                {
                    throw new ReadOnlyException(string.Format("map is read-only (attempted to set key '{0}')", name));
                }

                // find defining scope (if any)
                DekiScriptMap scope = this;
                while ((scope != null) && !scope._value.ContainsKey(name) && (scope.Outer == null || !scope.Outer.IsReadOnly))
                {
                    scope = scope.Outer;
                }

                // check if we found a scope
                if (scope != null)
                {
                    scope._value[name] = value;
                }
                else
                {
                    Add(name, value);
                }
            }
        }
コード例 #8
0
 public static DekiScriptLiteral Constant(XUri value, DekiScriptMap args)
 {
     return(new DekiScriptUri(value.WithoutFragment(), args));
 }
コード例 #9
0
 public DekiScriptMap(DekiScriptMap outer) : this(null, outer)
 {
 }
コード例 #10
0
        public static DekiScriptLiteral FromXml(XDoc doc)
        {
            // check if response is an HTML document
            if (doc.HasName("html"))
            {
                // TODO (steveb): this handling seems to be to specific to belong here.

                return(new DekiScriptList().Add(new DekiScriptXml(doc)));
            }

            // check if response is a DekiScript XML document
            if (!doc.HasName("value") || (doc["@type"].AsText == null))
            {
                throw new ArgumentException("doc");
            }
            switch (doc["@type"].AsText)
            {
            case "nil":
                return(DekiScriptNil.Value);

            case "bool":
                return(Constant(doc.AsBool ?? false));

            case "num":
                return(Constant(doc.AsDouble ?? 0.0));

            case "str":
                return(Constant(doc.AsText ?? string.Empty));

            case "uri": {
                return(Constant(doc.AsUri));
            }

            case "map": {
                DekiScriptMap result = new DekiScriptMap();
                foreach (XDoc value in doc["value"])
                {
                    result.Add(value["@key"].AsText, FromXml(value));
                }
                return(result);
            }

            case "list": {
                DekiScriptList result = new DekiScriptList();
                foreach (XDoc value in doc["value"])
                {
                    result.Add(FromXml(value));
                }
                return(result);
            }

            case "xml":
                if ((doc.AsXmlNode.ChildNodes.Count == 1) && (doc.AsXmlNode.ChildNodes[0].NodeType == XmlNodeType.Element))
                {
                    return(new DekiScriptXml(doc[doc.AsXmlNode.ChildNodes[0]]));
                }
                return(DekiScriptNil.Value);

            default:
                throw new ArgumentException("doc");
            }
        }