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



commit 7d9505973006a919eab3772bedb5e058558b7f13
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 1b7f4c6..dac11c6 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);
 
@@ -852,6 +866,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 ();
@@ -876,7 +927,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");
@@ -975,7 +1026,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;
 			}
@@ -1019,6 +1070,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");
 
@@ -1080,7 +1133,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;
 			}
@@ -1091,8 +1144,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);
@@ -1117,7 +1173,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;
 			}
@@ -1128,8 +1184,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);
@@ -1144,7 +1203,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;
 			}
@@ -1153,8 +1212,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;
@@ -1365,7 +1427,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;
 			}
@@ -1387,8 +1449,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");
 
@@ -1421,7 +1485,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;
 			}
@@ -1460,6 +1524,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);
@@ -1486,7 +1552,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;
 			}
@@ -1513,6 +1579,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)
@@ -1666,7 +1734,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");
 		}
@@ -1748,6 +1823,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;
@@ -1773,6 +1853,7 @@ public class Vala.GirParser : CodeVisitor {
 				}
 
 				parameters.add (info);
+				pop_metadata ();
 			}
 			end_element ("parameters");
 		}
@@ -1884,7 +1965,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");
 		}
@@ -1914,7 +2002,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;
 			}
@@ -1928,8 +2016,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) {
@@ -1948,7 +2038,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;
 			}
@@ -1970,8 +2060,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]