Example #1
0
        public void Parse(CompileContext context, System.Xml.XmlNode node)
        {
            foreach (XmlNode child in node.ChildNodes)
            {
                if (child.NodeType != XmlNodeType.Element)
                {
                    continue;
                }

                if (inner != null)
                {
                    throw new ParseException("Cannot have multiple nodes in property.");
                }

                Type type = context.ResolveType(child.Prefix, child.LocalName);
                if (!Common.IsTypeSameOrDerived(resultType, type))
                {
                    throw new ParseException(string.Format("The types '{0}' and '{1}' are not compatible",
                                                           resultType.FullName, type != null ? type.FullName : "null"));
                }

                inner = new ASTXmlParsable(type);
                inner.Parse(context, child);
            }
        }
Example #2
0
        /// <summary>
        /// We parse the value as XmlNode.
        /// </summary>
        public static IEmittable ParseValue(CompileContext context, Type changeType, XmlNode node)
        {
            // We find the name in changeType.
            PropertyInfo info = changeType.GetProperty(node.LocalName);

            if (info == null || !info.CanWrite)
            {
                throw new ParseException(string.Format("The property '{0}' for class '{1}' either does not exist" +
                                                       " or it is not writable.", node.Name, changeType));
            }



            Type type = info.PropertyType;


            // A property defines a bucket, where internal is of different type.
            if (node.Prefix == StandardPrefixes.Property)
            {
                ASTXmlParsableProperty prop = new ASTXmlParsableProperty(type);
                prop.Parse(context, node);

                return(prop);
            }

            // A normal, direct initialization.
            if (type == typeof(GuiScalar))
            {
                ASTScalar scalar = new ASTScalar();
                scalar.Parse(context, node);
                return(scalar);
            }
            else if (type == typeof(GuiRect))
            {
                ASTRect rect = new ASTRect();
                rect.Parse(context, node);
                return(rect);
            }
            else if (type == typeof(GuiVector2))
            {
                ASTVector2 v = new ASTVector2();
                v.Parse(context, node);
                return(v);
            }
            else if (type == typeof(string))
            {
                ASTString s = new ASTString();
                s.Value = node.InnerText;
                return(s);
            }
            else
            {
                // If there is only a 'Value' tag with no children, we consider using normal parsing.
                if (node.Attributes.Count == 1 && node.Attributes["Value"] != null && node.ChildNodes.Count == 0)
                {
                    return(ParseValue(context, changeType, node.LocalName, node.Value));
                }

                // If no alternative, we use generic XMLParseable.
                ASTXmlParsable parseable = new ASTXmlParsable(type);
                parseable.Parse(context, node);
                return(parseable);
            }
        }