/// <summary> /// We parse value as text. /// </summary> public static IEmittable ParseValue(CompileContext context, Type changeType, string property, string value) { // We find the name in changeType. PropertyInfo info = changeType.GetProperty(property); 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.", property, changeType.Name)); } Type type = info.PropertyType; if (type == typeof(GuiScalar)) { ASTScalar scalar = new ASTScalar(); scalar.Parse(context, Token.StackForm(Token.Tokenize(value))); return(scalar); } else if (type == typeof(GuiRect)) { ASTRect rect = new ASTRect(); rect.Parse(context, Token.StackForm(Token.Tokenize(value))); return(rect); } else if (type == typeof(GuiVector2)) { ASTVector2 v = new ASTVector2(); v.Parse(context, Token.StackForm(Token.Tokenize(value))); return(v); } else if (type == typeof(string)) { ASTString s = new ASTString(); s.Value = value; return(s); } else if (type == typeof(int)) { ASTInt s = new ASTInt(); s.Value = int.Parse(value, CultureInfo.InvariantCulture.NumberFormat); return(s); } else if (type == typeof(uint)) { ASTUInt s = new ASTUInt(); s.Value = uint.Parse(value, CultureInfo.InvariantCulture.NumberFormat); return(s); } else if (type == typeof(bool)) { ASTBool s = new ASTBool(); s.Value = bool.Parse(value); return(s); } else if (type.BaseType == typeof(Enum)) { ASTEnum e = new ASTEnum(type, value); return(e); } else if (type == typeof(Colour)) { ASTColour c = new ASTColour(); c.Parse(context, Token.StackForm(Token.Tokenize(value))); return(c); } else if (type.GetMethod("Parse", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typeof(string) }, null) != null) { // We try with "Parse" method. ASTStringParsable p = new ASTStringParsable(value, type); return(p); } else { throw new ParseException(string.Format("The property {0} is not supported because it is not built-in or does not" + " implement a static Parse(string) method.", property)); } }
public void Test() { ASTVector2 vector = new ASTVector2(); vector.Parse(null, Token.StackForm(Token.Tokenize("(10px+10%, -10%-5px)"))); }
/// <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); } }
public void Parse(CompileContext context, Stack <Token> stream) { // We have following parsing: // [Type:]GuiVector2, GuiVector2 // where Type = MinMax | LeftBottomAndSize | RightBottomAndSize | LeftTopAndSize | RightTopAndSize // We first check for type. Token token = stream.Pop(); RectangleMode mode = RectangleMode.MinMax; if (token.TokenId == TokenId.Identifier) { string id = token.Identifier; switch (id.ToLower()) { case "minmax": mode = RectangleMode.MinMax; break; case "leftbottomandsize": mode = RectangleMode.LeftBottomAndSize; break; case "rightbottomandsize": mode = RectangleMode.RightBottomAndSize; break; case "lefttopandsize": mode = RectangleMode.LeftTopAndSize; break; case "righttopandsize": mode = RectangleMode.RightTopAndSize; break; default: stream.Push(token); throw new ParseException("Expected a valid type identifier for rectangle."); } token = stream.Pop(); if (token.TokenId != TokenId.Colon) { stream.Push(token); throw new ParseException("If specifier for rectangle is used, it must be followed by ':'."); } token = stream.Pop(); } stream.Push(token); ASTVector2 v1 = new ASTVector2(), v2 = new ASTVector2(); v1.Parse(context, stream); token = stream.Pop(); if (token.TokenId != TokenId.Comma) { stream.Push(token); throw new ParseException("Comma expected to seperate vectors in rectangle."); } v2.Parse(context, stream); // We now apply data. rectangle = new GuiRect(mode, v1.Vector, v2.Vector); }