[vala/switch-to-gir: 20/34] girparser: Add metadata stack while parsing the gir



commit 369dd4e2e200868b2e6e7ef79aaa00d45f03d1a1
Author: Luca Bruno <lucabru src gnome org>
Date:   Sun Aug 29 14:35:09 2010 +0200

    girparser: Add metadata stack while parsing the gir

 vala/valagirparser.vala  |  124 ++++++++++++++++++++++++++++++++++++++++------
 vapigen/valavapigen.vala |    5 ++
 2 files changed, 113 insertions(+), 16 deletions(-)
---
diff --git a/vala/valagirparser.vala b/vala/valagirparser.vala
index dfd1421..f1723e3 100644
--- a/vala/valagirparser.vala
+++ b/vala/valagirparser.vala
@@ -494,6 +494,9 @@ public class Vala.GirParser : CodeVisitor {
 
 	string[] cheader_filenames;
 
+	ArrayList<Metadata> metadata_stack;
+	Metadata metadata;
+
 	HashMap<string,ArrayList<SymbolInfo>> current_symbols_info;
 
 	HashMap<UnresolvedSymbol,Symbol> unresolved_symbols_map = new HashMap<UnresolvedSymbol,Symbol> (unresolved_symbol_hash, unresolved_symbol_equal);
@@ -548,6 +551,17 @@ public class Vala.GirParser : CodeVisitor {
 	}
 
 	public void parse_file (SourceFile source_file) {
+		// load metadata
+		metadata_stack = new ArrayList<Metadata> ();
+		metadata = Metadata.empty;
+		string metadata_filename = "%s.metadata".printf (source_file.filename.ndup (source_file.filename.length - ".gir".length));
+		if (FileUtils.test (metadata_filename, FileTest.EXISTS)) {
+			var metadata_parser = new MetadataParser ();
+			var metadata_file = new SourceFile (context, metadata_filename, source_file.external_package);
+			context.add_source_file (metadata_file);
+			metadata = metadata_parser.parse_metadata (metadata_file);
+		}
+
 		this.current_source_file = source_file;
 		reader = new MarkupReader (source_file.filename);
 
@@ -839,6 +853,43 @@ public class Vala.GirParser : CodeVisitor {
 		}
 	}
 
+	Metadata get_current_metadata () {
+		var name = reader.name;
+		var child_name = reader.get_attribute ("name");
+		if (child_name == null) {
+			return Metadata.empty;
+		}
+
+		var type = MetadataType.GENERIC;
+		if (name == "glib:signal") {
+			type = MetadataType.SIGNAL;
+		} else if (name == "property") {
+			type = MetadataType.PROPERTY;
+		}
+
+		return metadata.match_child (child_name, type);
+	}
+
+	bool push_metadata () {
+		// skip?
+		if (reader.get_attribute ("introspectable") == "0") {
+			return false;
+		}
+		var new_metadata = get_current_metadata ();
+		if (new_metadata.get_bool (ArgumentType.SKIP)) {
+			return false;
+		}
+
+		metadata_stack.add (metadata);
+		metadata = new_metadata;
+		return true;
+	}
+
+	void pop_metadata () {
+		metadata = metadata_stack[metadata_stack.size - 1];
+		metadata_stack.remove_at (metadata_stack.size - 1);
+	}
+
 	void parse_repository () {
 		start_element ("repository");
 		next ();
@@ -863,7 +914,7 @@ public class Vala.GirParser : CodeVisitor {
 			} else {
 				// error
 				Report.error (get_current_src (), "unknown child element `%s' in `repository'".printf (reader.name));
-				break;
+				skip_element ();
 			}
 		}
 		end_element ("repository");
@@ -962,7 +1013,7 @@ public class Vala.GirParser : CodeVisitor {
 		var old_symbols_info = current_symbols_info;
 		current_symbols_info = new HashMap<string,ArrayList<SymbolInfo>> (str_hash, str_equal);
 		while (current_token == MarkupTokenType.START_ELEMENT) {
-			if (reader.get_attribute ("introspectable") == "0") {
+			if (!push_metadata ()) {
 				skip_element ();
 				continue;
 			}
@@ -1006,6 +1057,8 @@ public class Vala.GirParser : CodeVisitor {
 				Report.error (get_current_src (), "unknown child element `%s' in `namespace'".printf (reader.name));
 				skip_element ();
 			}
+
+			pop_metadata ();
 		}
 		end_element ("namespace");
 
@@ -1067,7 +1120,7 @@ public class Vala.GirParser : CodeVisitor {
 		string common_prefix = null;
 		
 		while (current_token == MarkupTokenType.START_ELEMENT) {
-			if (reader.get_attribute ("introspectable") == "0") {
+			if (!push_metadata ()) {
 				skip_element ();
 				continue;
 			}
@@ -1078,8 +1131,11 @@ public class Vala.GirParser : CodeVisitor {
 				calculate_common_prefix (ref common_prefix, ev.get_cname ());
 			} else {
 				// error
-				break;
+				Report.error (get_current_src (), "unknown child element `%s' in `enumaration'".printf (reader.name));
+				skip_element ();
 			}
+
+			pop_metadata ();
 		}
 
 		en.set_cprefix (common_prefix);
@@ -1104,7 +1160,7 @@ public class Vala.GirParser : CodeVisitor {
 		string common_prefix = null;
 
 		while (current_token == MarkupTokenType.START_ELEMENT) {
-			if (reader.get_attribute ("introspectable") == "0") {
+			if (!push_metadata ()) {
 				skip_element ();
 				continue;
 			}
@@ -1115,8 +1171,11 @@ public class Vala.GirParser : CodeVisitor {
 				calculate_common_prefix (ref common_prefix, ec.get_cname ());
 			} else {
 				// error
-				break;
+				Report.error (get_current_src (), "unknown child element `%s' in `enumeration'".printf (reader.name));
+				skip_element ();
 			}
+
+			pop_metadata ();
 		}
 
 		ed.set_cprefix (common_prefix);
@@ -1131,7 +1190,7 @@ public class Vala.GirParser : CodeVisitor {
 		en.access = SymbolAccessibility.PUBLIC;
 		next ();
 		while (current_token == MarkupTokenType.START_ELEMENT) {
-			if (reader.get_attribute ("introspectable") == "0") {
+			if (!push_metadata ()) {
 				skip_element ();
 				continue;
 			}
@@ -1140,8 +1199,11 @@ public class Vala.GirParser : CodeVisitor {
 				en.add_value (parse_enumeration_member ());
 			} else {
 				// error
-				break;
+				Report.error (get_current_src (), "unknown child element `%s' in `bitfield'".printf (reader.name));
+				skip_element ();
 			}
+
+			pop_metadata ();
 		}
 		end_element ("bitfield");
 		return en;
@@ -1352,7 +1414,7 @@ public class Vala.GirParser : CodeVisitor {
 		st.access = SymbolAccessibility.PUBLIC;
 		next ();
 		while (current_token == MarkupTokenType.START_ELEMENT) {
-			if (reader.get_attribute ("introspectable") == "0") {
+			if (!push_metadata ()) {
 				skip_element ();
 				continue;
 			}
@@ -1374,8 +1436,10 @@ public class Vala.GirParser : CodeVisitor {
 			} else {
 				// error
 				Report.error (get_current_src (), "unknown child element `%s' in `record'".printf (reader.name));
-				break;
+				skip_element ();
 			}
+
+			pop_metadata ();
 		}
 		end_element ("record");
 
@@ -1408,7 +1472,7 @@ public class Vala.GirParser : CodeVisitor {
 		var old_symbols_info = current_symbols_info;
 		current_symbols_info = new HashMap<string,ArrayList<SymbolInfo>> (str_hash, str_equal);
 		while (current_token == MarkupTokenType.START_ELEMENT) {
-			if (reader.get_attribute ("introspectable") == "0") {
+			if (!push_metadata ()) {
 				skip_element ();
 				continue;
 			}
@@ -1447,6 +1511,8 @@ public class Vala.GirParser : CodeVisitor {
 				Report.error (get_current_src (), "unknown child element `%s' in `class'".printf (reader.name));
 				skip_element ();
 			}
+
+			pop_metadata ();
 		}
 
 		merge_add_process (cl);
@@ -1473,7 +1539,7 @@ public class Vala.GirParser : CodeVisitor {
 		var old_symbols_info = current_symbols_info;
 		current_symbols_info = new HashMap<string,ArrayList<SymbolInfo>> (str_hash, str_equal);
 		while (current_token == MarkupTokenType.START_ELEMENT) {
-			if (reader.get_attribute ("introspectable") == "0") {
+			if (!push_metadata ()) {
 				skip_element ();
 				continue;
 			}
@@ -1500,6 +1566,8 @@ public class Vala.GirParser : CodeVisitor {
 				Report.error (get_current_src (), "unknown child element `%s' in `interface'".printf (reader.name));
 				skip_element ();
 			}
+
+			pop_metadata ();
 		}
 
 		// ensure we have at least one instantiable prerequisite (GLib.Object)
@@ -1653,7 +1721,14 @@ public class Vala.GirParser : CodeVisitor {
 			start_element ("parameters");
 			next ();
 			while (current_token == MarkupTokenType.START_ELEMENT) {
+				if (!push_metadata ()) {
+					skip_element ();
+					continue;
+				}
+
 				m.add_parameter (parse_parameter ());
+
+				pop_metadata ();
 			}
 			end_element ("parameters");
 		}
@@ -1735,6 +1810,11 @@ public class Vala.GirParser : CodeVisitor {
 			next ();
 
 			while (current_token == MarkupTokenType.START_ELEMENT) {
+				if (!push_metadata ()) {
+					skip_element ();
+					continue;
+				}
+
 				int array_length_idx, closure_idx, destroy_idx;
 				string scope;
 				string default_param_name = null;
@@ -1760,6 +1840,7 @@ public class Vala.GirParser : CodeVisitor {
 				}
 
 				parameters.add (info);
+				pop_metadata ();
 			}
 			end_element ("parameters");
 		}
@@ -1871,7 +1952,14 @@ public class Vala.GirParser : CodeVisitor {
 			start_element ("parameters");
 			next ();
 			while (current_token == MarkupTokenType.START_ELEMENT) {
+				if (!push_metadata ()) {
+					skip_element ();
+					continue;
+				}
+
 				sig.add_parameter (parse_parameter ());
+
+				pop_metadata ();
 			}
 			end_element ("parameters");
 		}
@@ -1901,7 +1989,7 @@ public class Vala.GirParser : CodeVisitor {
 		next ();
 
 		while (current_token == MarkupTokenType.START_ELEMENT) {
-			if (reader.get_attribute ("introspectable") == "0") {
+			if (!push_metadata ()) {
 				skip_element ();
 				continue;
 			}
@@ -1915,8 +2003,10 @@ public class Vala.GirParser : CodeVisitor {
 			} else {
 				// error
 				Report.error (get_current_src (), "unknown child element `%s' in `class'".printf (reader.name));
-				break;
+				skip_element ();
 			}
+
+			pop_metadata ();
 		}
 
 		if (current_token != MarkupTokenType.END_ELEMENT) {
@@ -1935,7 +2025,7 @@ public class Vala.GirParser : CodeVisitor {
 		next ();
 
 		while (current_token == MarkupTokenType.START_ELEMENT) {
-			if (reader.get_attribute ("introspectable") == "0") {
+			if (!push_metadata ()) {
 				skip_element ();
 				continue;
 			}
@@ -1957,8 +2047,10 @@ public class Vala.GirParser : CodeVisitor {
 			} else {
 				// error
 				Report.error (get_current_src (), "unknown child element `%s' in `union'".printf (reader.name));
-				break;
+				skip_element ();
 			}
+
+			pop_metadata ();
 		}
 
 		end_element ("union");
diff --git a/vapigen/valavapigen.vala b/vapigen/valavapigen.vala
index d104ca0..3dd19fe 100644
--- a/vapigen/valavapigen.vala
+++ b/vapigen/valavapigen.vala
@@ -173,6 +173,11 @@ class Vala.VAPIGen : Object {
 			}
 			if (file.filename in sources) {
 				file.external_package = false;
+			} else if (file.filename.has_suffix (".metadata")) {
+				string gir_filename = "%s.gir".printf (file.filename.ndup (file.filename.length - ".metadata".length));
+				if (gir_filename in sources) {
+					file.external_package = false;
+				}
 			}
 		}
 



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