public void Parse(CompileContext context, Stack<Token> stream) { Token token = stream.Pop(); if (token.TokenId != TokenId.LeftBracket) { stream.Push(token); throw new ParseException("Expected '(' to begin vector."); } // We parse first scalar. ASTScalar scalar1 = new ASTScalar(), scalar2 = new ASTScalar(); scalar1.Parse(context, stream); // We expect comma. token = stream.Pop(); if (token.TokenId != TokenId.Comma) { stream.Push(token); throw new ParseException("Expected ',' seperating two scalars."); } // We parse the second. scalar2.Parse(context, stream); // We need right bracket. token = stream.Pop(); if (token.TokenId != TokenId.RightBracket) { stream.Push(token); throw new ParseException("Expecting ')' to end vector."); } // We apply them. data = new GuiVector2(scalar1.Scalar, scalar2.Scalar); }
public override string ToString() { ASTScalar s1 = new ASTScalar(), s2 = new ASTScalar(); s1.Scalar = data.X; s2.Scalar = data.Y; return string.Format("new SharpMedia.Graphics.GUI.Metrics.GuiVector2({0}, {1})", s1, s2); }
public void Test() { ASTScalar scalar = new ASTScalar(); scalar.Parse(null, Token.StackForm(Token.Tokenize("10.0c+5px+2%"))); Assert.AreEqual(5.0f, scalar.Scalar.PixelCoordinate); Assert.AreEqual(10.0f, scalar.Scalar.CanvasCoordinate); Assert.AreEqual(0.02f, scalar.Scalar.ParentCoordinate); }
public void Parse(CompileContext context, System.Xml.XmlNode node) { if (node.Attributes["Value"] != null) { ASTScalar scalar = new ASTScalar(); scalar.Parse(context, Token.StackForm(Token.Tokenize(node.Attributes["Value"].InnerText))); data = scalar.Scalar; } else { throw new ParseException("Value of scalar not present in 'Value' attribute."); } }
public void Parse(CompileContext context, System.Xml.XmlNode node) { ASTScalar width = null, height = null; if (node.Attributes["Width"] != null) { width = new ASTScalar(); width.Parse(context, Token.StackForm(Token.Tokenize(node.Attributes["Width"].InnerText))); } if (node.Attributes["X"] != null) { if (width != null) { throw new ParseException("Could not combine X and Width, same meaning in terms of vector."); } width = new ASTScalar(); width.Parse(context, Token.StackForm(Token.Tokenize(node.Attributes["X"].InnerText))); } if (node.Attributes["Height"] != null) { height = new ASTScalar(); height.Parse(context, Token.StackForm(Token.Tokenize(node.Attributes["Height"].InnerText))); } if (node.Attributes["Y"] != null) { if (height != null) { throw new ParseException("Could not combine Y and Height, same meaning in terms of vector."); } height = new ASTScalar(); height.Parse(context, Token.StackForm(Token.Tokenize(node.Attributes["Y"].InnerText))); } // Default to zero if (width == null) { width = new ASTScalar(); width.Scalar = new GuiScalar(0.0f); } if (height == null) { height = new ASTScalar(); height.Scalar = new GuiScalar(0.0f); } data = new GuiVector2(width.Scalar, height.Scalar); }
/// <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)); } }
/// <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, System.Xml.XmlNode node) { XmlAttributeCollection attr = node.Attributes; ASTScalar left = null, right = null, top = null, bottom = null, width = null, height = null; if (attr["Left"] != null) { left = new ASTScalar(); left.Parse(context, Token.StackForm(Token.Tokenize(attr["Left"].InnerText))); } if (attr["Right"] != null) { right = new ASTScalar(); right.Parse(context, Token.StackForm(Token.Tokenize(attr["Right"].InnerText))); } if (attr["Top"] != null) { top = new ASTScalar(); top.Parse(context, Token.StackForm(Token.Tokenize(attr["Top"].InnerText))); } if (attr["Bottom"] != null) { bottom = new ASTScalar(); bottom.Parse(context, Token.StackForm(Token.Tokenize(attr["Bottom"].InnerText))); } if (attr["Width"] != null) { width = new ASTScalar(); width.Parse(context, Token.StackForm(Token.Tokenize(attr["Width"].InnerText))); } if (attr["Height"] != null) { height = new ASTScalar(); height.Parse(context, Token.StackForm(Token.Tokenize(attr["Height"].InnerText))); } // We now try combinations, must be defined somehow. We check four corners. if (left != null && right != null && top != null && bottom != null && height == null && width == null) { this.rectangle = new GuiRect(RectangleMode.MinMax, new GuiVector2(left.Scalar, bottom.Scalar), new GuiVector2(right.Scalar, top.Scalar)); return; } // We default width/height. if (width == null) { width = new ASTScalar(); width.Scalar = new GuiScalar(0.0f); } if (height == null) { height = new ASTScalar(); height.Scalar = new GuiScalar(0.0f); } // Now we check combinations. if (left != null && right == null && top == null && bottom != null) { this.rectangle = new GuiRect(RectangleMode.LeftBottomAndSize, new GuiVector2(left.Scalar, bottom.Scalar), new GuiVector2(width.Scalar, height.Scalar)); return; } if (left != null && right == null && top != null && bottom == null) { this.rectangle = new GuiRect(RectangleMode.LeftTopAndSize, new GuiVector2(left.Scalar, top.Scalar), new GuiVector2(width.Scalar, height.Scalar)); return; } if (left == null && right != null && top != null && bottom == null) { this.rectangle = new GuiRect(RectangleMode.RightTopAndSize, new GuiVector2(right.Scalar, top.Scalar), new GuiVector2(width.Scalar, height.Scalar)); return; } if (left == null && right != null && top == null && bottom != null) { this.rectangle = new GuiRect(RectangleMode.RightBottomAndSize, new GuiVector2(right.Scalar, bottom.Scalar), new GuiVector2(width.Scalar, height.Scalar)); return; } throw new ParseException("The rectangle is malformed."); }