[vala/switch-to-gir] girparser: Decouple from Attribute and use Argument enums



commit f0c087cb1152ff7ead8eca3be583dff4df0c1be8
Author: Luca Bruno <lethalman88 gmail com>
Date:   Wed Aug 25 01:14:45 2010 +0200

    girparser: Decouple from Attribute and use Argument enums

 vala/valagirparser.vala |  107 ++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 88 insertions(+), 19 deletions(-)
---
diff --git a/vala/valagirparser.vala b/vala/valagirparser.vala
index 4c2b561..565d4b6 100644
--- a/vala/valagirparser.vala
+++ b/vala/valagirparser.vala
@@ -32,7 +32,21 @@ public class Vala.GirParser : CodeVisitor {
 		SIGNAL
 	}
 
-	class Metadata : Attribute {
+	enum Argument {
+		HIDDEN;
+
+		public static Argument? from_string (string name) {
+			var enum_class = (EnumClass) typeof(Argument).class_ref ();
+			unowned GLib.EnumValue? enum_value = enum_class.get_value_by_nick (name);
+			if (enum_value != null) {
+				Argument value = (Argument) enum_value.value;
+				return value;
+			}
+			return null;
+		}
+	}
+
+	class Metadata {
 		private static Metadata _empty = new Metadata ("");
 		public static Metadata empty {
 			get {
@@ -40,23 +54,22 @@ public class Vala.GirParser : CodeVisitor {
 			}
 		}
 
+		public SourceReference source_reference;
 		public string pattern;
+		public PatternSpec pattern_spec;
 		public MetadataType type;
 		public bool used = false;
-
-		private ArrayList<Metadata> children = new ArrayList<Metadata> ();
-		private PatternSpec pattern_spec;
+		public Vala.Map<Argument,CodeNode> args = new HashMap<Argument,CodeNode> ();
+		public ArrayList<Metadata> children = new ArrayList<Metadata> ();
 
 		public Metadata (string pattern, MetadataType type = MetadataType.GENERIC, SourceReference? source_reference = null) {
-			base ("", source_reference);
-			pattern_spec = new PatternSpec (pattern);
+			this.pattern = pattern;
+			this.pattern_spec = new PatternSpec (pattern);
 			this.type = type;
+			this.source_reference = source_reference;
 		}
 
 		public void add_child (Metadata metadata) {
-			if (get_child (metadata.name, metadata.type) != Metadata.empty) {
-				Report.error (metadata.source_reference, "duplicate metadata pattern");
-			}
 			children.add (metadata);
 		}
 
@@ -68,6 +81,57 @@ public class Vala.GirParser : CodeVisitor {
 			}
 			return Metadata.empty;
 		}
+
+		public void add_argument (Argument key, CodeNode value) {
+			args.set (key, value);
+		}
+
+		public bool has_argument (Argument key) {
+			return args.contains (key);
+		}
+
+		public string? get_string (Argument arg) {
+			var lit = args.get (arg) as StringLiteral;
+			if (lit != null) {
+				return lit.eval ();
+			}
+
+			return null;
+		}
+
+		public int get_integer (Argument arg) {
+			var lit = args.get (arg) as IntegerLiteral;
+			if (lit != null) {
+				return lit.value.to_int ();
+			}
+
+			return 0;
+		}
+
+		public double get_double (Argument arg) {
+			var name = args.get (arg);
+			if (name is RealLiteral) {
+				var lit = (RealLiteral) name;
+				return lit.value.to_double ();
+			} else if (name is IntegerLiteral) {
+				var lit = (IntegerLiteral) name;
+				return lit.value.to_int ();
+			}
+			return 0;
+		}
+
+		public bool get_bool (Argument arg) {
+			var lit = args.get (arg) as BooleanLiteral;
+			if (lit != null) {
+				return lit.value;
+			}
+
+			return false;
+		}
+
+		public DataType get_data_type (Argument arg) {
+			return args.get (arg) as DataType;
+		}
 	}
 
 	class MetadataParser {
@@ -94,12 +158,12 @@ public class Vala.GirParser : CodeVisitor {
 			return new SourceReference (scanner.source_file, begin.line, begin.column, end.line, end.column);
 		}
 
-		public Metadata? parse_metadata (SourceFile metadata_file) {
+		public Metadata parse_metadata (SourceFile metadata_file) {
 			scanner = new Scanner (metadata_file);
 			next ();
 			while (current != TokenType.EOF) {
 				if (!parse_rule ()) {
-					return null;
+					return Metadata.empty;
 				}
 			}
 			return tree;
@@ -217,7 +281,11 @@ public class Vala.GirParser : CodeVisitor {
 					break;
 				}
 
-				var id = get_string ();
+				var id = Argument.from_string (get_string ());
+				if (id == null) {
+					Report.error (get_current_src (), "unknown argument");
+					return false;
+				}
 
 				if (next () != TokenType.ASSIGN) {
 					Report.error (get_current_src (), "expected `='");
@@ -229,6 +297,7 @@ public class Vala.GirParser : CodeVisitor {
 				if (expr == null) {
 					return false;
 				}
+
 				metadata.add_argument (id, expr);
 			} while (current == TokenType.IDENTIFIER);
 
@@ -392,7 +461,7 @@ public class Vala.GirParser : CodeVisitor {
 			return false;
 		}
 		var new_metadata = get_metadata_from_element ();
-		if (new_metadata.has_argument ("hidden") && new_metadata.get_bool ("hidden")) {
+		if (new_metadata.has_argument (Argument.HIDDEN) && new_metadata.get_bool (Argument.HIDDEN)) {
 			return false;
 		}
 
@@ -602,7 +671,7 @@ public class Vala.GirParser : CodeVisitor {
 		var alias = new Alias ();
 		alias.source_reference = get_current_src ();
 		alias.name = reader.get_attribute ("name");
-		alias.base_type = parse_type_from_name (reader.get_attribute ("target"));
+		alias.base_type = parse_type_from_gir_name (reader.get_attribute ("target"));
 		alias.parent_namespace = current_namespace;
 		next ();
 		end_element ("alias");
@@ -871,7 +940,7 @@ public class Vala.GirParser : CodeVisitor {
 			type_name = "GLib.GenericArray";
 		}
 
-		DataType type = parse_type_from_name (type_name);
+		DataType type = parse_type_from_gir_name (type_name);
 
 		// type arguments / element types
 		while (current_token == MarkupTokenType.START_ELEMENT) {
@@ -884,7 +953,7 @@ public class Vala.GirParser : CodeVisitor {
 		return type;
 	}
 
-	DataType parse_type_from_name (string type_name) {
+	DataType parse_type_from_gir_name (string type_name) {
 		DataType type;
 		if (type_name == "none") {
 			type = new VoidType (get_current_src ());
@@ -1067,7 +1136,7 @@ public class Vala.GirParser : CodeVisitor {
 
 		string parent = reader.get_attribute ("parent");
 		if (parent != null) {
-			cl.add_base_type (parse_type_from_name (parent));
+			cl.add_base_type (parse_type_from_gir_name (parent));
 		}
 
 		next ();
@@ -1083,7 +1152,7 @@ public class Vala.GirParser : CodeVisitor {
 
 			if (reader.name == "implements") {
 				start_element ("implements");
-				cl.add_base_type (parse_type_from_name (reader.get_attribute ("name")));
+				cl.add_base_type (parse_type_from_gir_name (reader.get_attribute ("name")));
 				next ();
 				end_element ("implements");
 			} else if (reader.name == "constant") {
@@ -1203,7 +1272,7 @@ public class Vala.GirParser : CodeVisitor {
 
 			if (reader.name == "prerequisite") {
 				start_element ("prerequisite");
-				iface.add_prerequisite (parse_type_from_name (reader.get_attribute ("name")));
+				iface.add_prerequisite (parse_type_from_gir_name (reader.get_attribute ("name")));
 				next ();
 				end_element ("prerequisite");
 			} else if (reader.name == "field") {



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