Example #1
0
        public void Visit(Ast.DictNode dict)
        {
            IDictionary obj = new Dictionary <object, object>(dict.Elements.Count);

            foreach (var node in dict.Elements)
            {
                var kv = (Ast.KeyValueNode)node;
                kv.Key.Accept(this);
                object key = generated.Pop();
                kv.Value.Accept(this);
                object value = generated.Pop();
                obj[key] = value;
            }

            if (dictToInstance == null || !obj.Contains("__class__"))
            {
                generated.Push(obj);
            }
            else
            {
                object result = dictToInstance(obj);
                if (result == null)
                {
                    generated.Push(obj);
                }
                else
                {
                    generated.Push(result);
                }
            }
        }
Example #2
0
 public void Visit(Ast.DictNode dict)
 {
     result.AppendLine("(dict");
     indent++;
     foreach (Ast.KeyValueNode kv in dict.Elements)
     {
         Indent();
         kv.Key.Accept(this);
         result.Append(" = ");
         kv.Value.Accept(this);
         result.AppendLine(",");
     }
     indent--;
     Indent();
     result.Append(")");
 }
Example #3
0
 public void Visit(Ast.DictNode dict)
 {
     _result.AppendLine("(dict");
     _indent++;
     foreach (var node in dict.Elements)
     {
         var kv = (Ast.KeyValueNode)node;
         Indent();
         kv.Key.Accept(this);
         _result.Append(" = ");
         kv.Value.Accept(this);
         _result.AppendLine(",");
     }
     _indent--;
     Indent();
     _result.Append(")");
 }
Example #4
0
        Ast.DictNode ParseDict(SeekableStringReader sr)
        {
            //dict            = '{' keyvalue_list '}' .
            //keyvalue_list   = keyvalue { ',' keyvalue } .
            //keyvalue        = expr ':' expr .

            sr.Read();                  // {
            sr.SkipWhitespace();
            Ast.DictNode dict = new Ast.DictNode();
            if (sr.Peek() == '}')
            {
                sr.Read();
                return(dict);                           // empty dict
            }

            List <Ast.INode> elts = ParseKeyValueList(sr);

            if (!sr.HasMore())
            {
                throw new ParseException("missing '}'");
            }
            char closechar = sr.Read();

            if (closechar != '}')
            {
                throw new ParseException("expected '}'");
            }

            // make sure it has dict semantics (remove duplicate keys)
            Dictionary <Ast.INode, Ast.INode> fixedDict = new Dictionary <Ast.INode, Ast.INode>(elts.Count);

            foreach (Ast.KeyValueNode kv in elts)
            {
                fixedDict[kv.Key] = kv.Value;
            }
            foreach (var kv in fixedDict)
            {
                dict.Elements.Add(new Ast.KeyValueNode()
                {
                    Key   = kv.Key,
                    Value = kv.Value
                });
            }
            return(dict);
        }
Example #5
0
        private Ast.INode ParseDict(SeekableStringReader sr)
        {
            //dict            = '{' keyvalue_list trailing_comma '}' .
            //keyvalue_list   = keyvalue { ',' keyvalue } .
            //keyvalue        = expr ':' expr .
            // trailing_comma  = '' | ',' .

            sr.Read();                  // {
            sr.SkipWhitespace();
            Ast.DictNode dict = new Ast.DictNode();
            if (sr.Peek() == '}')
            {
                sr.Read();
                return(dict);                           // empty dict
            }

            var elts = ParseKeyValueList(sr);

            // handle trailing comma if present
            sr.SkipWhitespace();
            if (!sr.HasMore())
            {
                throw new ParseException("missing '}'");
            }
            if (sr.Peek() == ',')
            {
                sr.Read();
            }

            if (!sr.HasMore())
            {
                throw new ParseException("missing '}'");
            }
            char closechar = sr.Read();

            if (closechar != '}')
            {
                throw new ParseException("expected '}'");
            }

            // make sure it has dict semantics (remove duplicate keys)
            var fixedDict = new Dictionary <Ast.INode, Ast.INode>(elts.Count);

            foreach (var node in elts)
            {
                var kv = (Ast.KeyValueNode)node;
                fixedDict[kv.Key] = kv.Value;
            }

            foreach (var kv in fixedDict)
            {
                dict.Elements.Add(new Ast.KeyValueNode
                {
                    Key   = kv.Key,
                    Value = kv.Value
                });
            }

            // SPECIAL CASE: {'__class__':'float','value':'nan'}  ---> Double.NaN
            if (dict.Elements.Count != 2)
            {
                return(dict);
            }
            if (!dict.Elements.Contains(new Ast.KeyValueNode(new Ast.StringNode("__class__"),
                                                             new Ast.StringNode("float"))))
            {
                return(dict);
            }
            if (dict.Elements.Contains(new Ast.KeyValueNode(new Ast.StringNode("value"), new Ast.StringNode("nan"))))
            {
                return(new Ast.DoubleNode(double.NaN));
            }

            return(dict);
        }