[vala/0.10-parallel: 21/46] Simplify attribute objects



commit cd9505a32fb4079d0acb45dc87ef684c41c0fa56
Author: Jürg Billeter <j bitron ch>
Date:   Sat Aug 21 13:01:26 2010 +0200

    Simplify attribute objects

 vala/valaattribute.vala   |   62 +++++++++++++++++---------------------------
 vala/valacodewriter.vala  |    4 +--
 vala/valafield.vala       |    2 +-
 vala/valagenieparser.vala |   28 +++++++++++++++++++-
 vala/valaparser.vala      |   28 +++++++++++++++++++-
 5 files changed, 78 insertions(+), 46 deletions(-)
---
diff --git a/vala/valaattribute.vala b/vala/valaattribute.vala
index 32f475f..9fdc28a 100644
--- a/vala/valaattribute.vala
+++ b/vala/valaattribute.vala
@@ -34,7 +34,7 @@ public class Vala.Attribute : CodeNode {
 	/**
 	 * Contains all specified attribute arguments.
 	 */
-	public Vala.Map<string,Expression> args = new HashMap<string,Expression> (str_hash, str_equal);
+	public Vala.Map<string,string> args = new HashMap<string,string> (str_hash, str_equal);
 
 	/**
 	 * Creates a new attribute.
@@ -53,7 +53,7 @@ public class Vala.Attribute : CodeNode {
 	 *
 	 * @param arg named argument
 	 */
-	public void add_argument (string key, Expression value) {
+	public void add_argument (string key, string value) {
 		args.set (key, value);
 	}
 	
@@ -74,12 +74,16 @@ public class Vala.Attribute : CodeNode {
 	 * @return     string value
 	 */
 	public string? get_string (string name) {
-		var lit = args.get (name) as StringLiteral;
-		if (lit != null) {
-			return lit.eval ();
+		string value = args.get (name);
+
+		if (value == null) {
+			return null;
 		}
-		
-		return null;
+
+		/* remove quotes */
+		var noquotes = value.substring (1, (uint) (value.length - 2));
+		/* unescape string */
+		return noquotes.compress ();
 	}
 	
 	/**
@@ -89,12 +93,13 @@ public class Vala.Attribute : CodeNode {
 	 * @return     integer value
 	 */
 	public int get_integer (string name) {
-		var lit = args.get (name) as IntegerLiteral;
-		if (lit != null) {
-			return lit.value.to_int ();
+		string value = args.get (name);
+
+		if (value == null) {
+			return 0;
 		}
-		
-		return 0;
+
+		return value.to_int ();
 	}
 
 	/**
@@ -104,27 +109,13 @@ public class Vala.Attribute : CodeNode {
 	 * @return     double value
 	 */
 	public double get_double (string name) {
-		var arg = args.get (name);
-		if (arg is RealLiteral) {
-			var lit = (RealLiteral) arg;
-			return lit.value.to_double ();
-		} else if (arg is IntegerLiteral) {
-			var lit = (IntegerLiteral) arg;
-			return lit.value.to_int ();
-		} else if (arg is UnaryExpression) {
-			var unary = (UnaryExpression) arg;
-			if (unary.operator == UnaryOperator.MINUS) {
-				if (unary.inner is RealLiteral) {
-					var lit = (RealLiteral) unary.inner;
-					return -lit.value.to_double ();
-				} else if (unary.inner is IntegerLiteral) {
-					var lit = (IntegerLiteral) unary.inner;
-					return -lit.value.to_int ();
-				}
-			}
+		string value = args.get (name);
+
+		if (value == null) {
+			return 0;
 		}
-		
-		return 0;
+
+		return value.to_double ();
 	}
 
 	/**
@@ -134,11 +125,6 @@ public class Vala.Attribute : CodeNode {
 	 * @return     boolean value
 	 */
 	public bool get_bool (string name) {
-		var lit = args.get (name) as BooleanLiteral;
-		if (lit != null) {
-			return lit.value;
-		}
-		
-		return false;
+		return (args.get (name) == "true");
 	}
 }
diff --git a/vala/valacodewriter.vala b/vala/valacodewriter.vala
index a1cda97..09a0808 100644
--- a/vala/valacodewriter.vala
+++ b/vala/valacodewriter.vala
@@ -1915,9 +1915,7 @@ public class Vala.CodeWriter : CodeVisitor {
 
 					string separator = "";
 					foreach (string arg_name in keys) {
-						stream.printf ("%s%s = ", separator, arg_name);
-						var expr = attr.args.get (arg_name);
-						expr.accept (this);
+						stream.printf ("%s%s = %s", separator, arg_name, attr.args.get (arg_name));
 						separator = ", ";
 					}
 
diff --git a/vala/valafield.vala b/vala/valafield.vala
index 07281f2..fc218e3 100644
--- a/vala/valafield.vala
+++ b/vala/valafield.vala
@@ -257,7 +257,7 @@ public class Vala.Field : Variable, Lockable {
 			attr = new Attribute ("CCode");
 			attributes.append (attr);
 		}
-		attr.add_argument ("type", new StringLiteral ("\"%s\"".printf (ctype)));
+		attr.add_argument ("type", "\"%s\"".printf (ctype));
 	}
 
 	public override bool check (SemanticAnalyzer analyzer) {
diff --git a/vala/valagenieparser.vala b/vala/valagenieparser.vala
index 415a9fe..28ffbac 100644
--- a/vala/valagenieparser.vala
+++ b/vala/valagenieparser.vala
@@ -2284,6 +2284,31 @@ public class Vala.Genie.Parser : CodeVisitor {
 		return new DeleteStatement (expr, get_src (begin));
 	}
 
+	string parse_attribute_value () throws ParseError {
+		switch (current ()) {
+		case TokenType.NULL:
+		case TokenType.TRUE:
+		case TokenType.FALSE:
+		case TokenType.INTEGER_LITERAL:
+		case TokenType.REAL_LITERAL:
+		case TokenType.STRING_LITERAL:
+			next ();
+			return get_last_string ();
+		case TokenType.MINUS:
+			next ();
+			switch (current ()) {
+			case TokenType.INTEGER_LITERAL:
+			case TokenType.REAL_LITERAL:
+				next ();
+				return "-" + get_last_string ();
+			default:
+				throw new ParseError.SYNTAX (get_error ("expected number"));
+			}
+		default:
+			throw new ParseError.SYNTAX (get_error ("expected literal"));
+		}
+	}
+
 	List<Attribute>? parse_attributes () throws ParseError {
 		if (current () != TokenType.OPEN_BRACKET) {
 			return null;
@@ -2299,8 +2324,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 						do {
 							id = parse_identifier ();
 							expect (TokenType.ASSIGN);
-							var expr = parse_expression ();
-							attr.add_argument (id, expr);
+							attr.add_argument (id, parse_attribute_value ());
 						} while (accept (TokenType.COMMA));
 					}
 					expect (TokenType.CLOSE_PARENS);
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index 093cd4e..9f4f34d 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -1973,6 +1973,31 @@ public class Vala.Parser : CodeVisitor {
 		return new DeleteStatement (expr, get_src (begin));
 	}
 
+	string parse_attribute_value () throws ParseError {
+		switch (current ()) {
+		case TokenType.NULL:
+		case TokenType.TRUE:
+		case TokenType.FALSE:
+		case TokenType.INTEGER_LITERAL:
+		case TokenType.REAL_LITERAL:
+		case TokenType.STRING_LITERAL:
+			next ();
+			return get_last_string ();
+		case TokenType.MINUS:
+			next ();
+			switch (current ()) {
+			case TokenType.INTEGER_LITERAL:
+			case TokenType.REAL_LITERAL:
+				next ();
+				return "-" + get_last_string ();
+			default:
+				throw new ParseError.SYNTAX (get_error ("expected number"));
+			}
+		default:
+			throw new ParseError.SYNTAX (get_error ("expected literal"));
+		}
+	}
+
 	List<Attribute>? parse_attributes () throws ParseError {
 		if (current () != TokenType.OPEN_BRACKET) {
 			return null;
@@ -1988,8 +2013,7 @@ public class Vala.Parser : CodeVisitor {
 						do {
 							id = parse_identifier ();
 							expect (TokenType.ASSIGN);
-							var expr = parse_expression ();
-							attr.add_argument (id, expr);
+							attr.add_argument (id, parse_attribute_value ());
 						} while (accept (TokenType.COMMA));
 					}
 					expect (TokenType.CLOSE_PARENS);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]