[vala] girparser: Add support for parsing error domains
- From: Jürg Billeter <juergbi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala] girparser: Add support for parsing error domains
- Date: Thu, 12 Aug 2010 19:26:10 +0000 (UTC)
commit 648cf84c988cb456d98637601dcac096ba125ac3
Author: Philip Withnall <philip withnall collabora co uk>
Date: Mon Aug 2 15:51:46 2010 +0100
girparser: Add support for parsing error domains
Fixes bug 625837.
vala/valaerrordomain.vala | 10 ++++-
vala/valagirparser.vala | 101 ++++++++++++++++++++++++++++++++++++---------
2 files changed, 89 insertions(+), 22 deletions(-)
---
diff --git a/vala/valaerrordomain.vala b/vala/valaerrordomain.vala
index 24b8072..f80750f 100644
--- a/vala/valaerrordomain.vala
+++ b/vala/valaerrordomain.vala
@@ -132,7 +132,13 @@ public class Vala.ErrorDomain : TypeSymbol {
if (infix == null) {
infix = "";
}
- return "%s%s%s".printf (parent_symbol.get_lower_case_cprefix (), infix, get_lower_case_csuffix ());
+
+ string cprefix = "";
+ if (parent_symbol != null) {
+ cprefix = parent_symbol.get_lower_case_cprefix ();
+ }
+
+ return "%s%s%s".printf (cprefix, infix, get_lower_case_csuffix ());
}
public override string? get_upper_case_cname (string? infix) {
@@ -143,7 +149,7 @@ public class Vala.ErrorDomain : TypeSymbol {
return false;
}
- private void set_cname (string cname) {
+ public void set_cname (string cname) {
this.cname = cname;
}
diff --git a/vala/valagirparser.vala b/vala/valagirparser.vala
index 4726858..f235b2a 100644
--- a/vala/valagirparser.vala
+++ b/vala/valagirparser.vala
@@ -228,7 +228,11 @@ public class Vala.GirParser : CodeVisitor {
if (reader.name == "alias") {
sym = parse_alias ();
} else if (reader.name == "enumeration") {
- sym = parse_enumeration ();
+ if (reader.get_attribute ("glib:error-quark") != null) {
+ sym = parse_error_domain ();
+ } else {
+ sym = parse_enumeration ();
+ }
} else if (reader.name == "bitfield") {
sym = parse_bitfield ();
} else if (reader.name == "function") {
@@ -265,6 +269,8 @@ public class Vala.GirParser : CodeVisitor {
ns.add_struct ((Struct) sym);
} else if (sym is Enum) {
ns.add_enum ((Enum) sym);
+ } else if (sym is ErrorDomain) {
+ ns.add_error_domain ((ErrorDomain) sym);
} else if (sym is Delegate) {
ns.add_delegate ((Delegate) sym);
} else if (sym is Method) {
@@ -298,6 +304,25 @@ public class Vala.GirParser : CodeVisitor {
return st;
}
+ private void calculate_common_prefix (ref string common_prefix, string cname) {
+ if (common_prefix == null) {
+ common_prefix = cname;
+ while (common_prefix.len () > 0 && !common_prefix.has_suffix ("_")) {
+ // FIXME: could easily be made faster
+ common_prefix = common_prefix.ndup (common_prefix.size () - 1);
+ }
+ } else {
+ while (!cname.has_prefix (common_prefix)) {
+ common_prefix = common_prefix.ndup (common_prefix.size () - 1);
+ }
+ }
+ while (common_prefix.len () > 0 && (!common_prefix.has_suffix ("_") ||
+ (cname.offset (common_prefix.length).get_char ().isdigit ()) && (cname.len () - common_prefix.len ()) <= 1)) {
+ // enum values may not consist solely of digits
+ common_prefix = common_prefix.ndup (common_prefix.size () - 1);
+ }
+ }
+
Enum parse_enumeration () {
start_element ("enumeration");
var en = new Enum (reader.get_attribute ("name"), get_current_src ());
@@ -321,25 +346,7 @@ public class Vala.GirParser : CodeVisitor {
if (reader.name == "member") {
var ev = parse_enumeration_member ();
en.add_value (ev);
-
- string cname = ev.get_cname ();
-
- if (common_prefix == null) {
- common_prefix = cname;
- while (common_prefix.len () > 0 && !common_prefix.has_suffix ("_")) {
- // FIXME: could easily be made faster
- common_prefix = common_prefix.ndup (common_prefix.size () - 1);
- }
- } else {
- while (!cname.has_prefix (common_prefix)) {
- common_prefix = common_prefix.ndup (common_prefix.size () - 1);
- }
- }
- while (common_prefix.len () > 0 && (!common_prefix.has_suffix ("_") ||
- (cname.offset (common_prefix.length).get_char ().isdigit ()) && (cname.len () - common_prefix.len ()) <= 1)) {
- // enum values may not consist solely of digits
- common_prefix = common_prefix.ndup (common_prefix.size () - 1);
- }
+ calculate_common_prefix (ref common_prefix, ev.get_cname ());
} else {
// error
break;
@@ -352,6 +359,43 @@ public class Vala.GirParser : CodeVisitor {
return en;
}
+ ErrorDomain parse_error_domain () {
+ start_element ("enumeration");
+
+ var ed = new ErrorDomain (reader.get_attribute ("name"), get_current_src ());
+ ed.access = SymbolAccessibility.PUBLIC;
+
+ string enum_cname = reader.get_attribute ("c:type");
+ if (enum_cname != null) {
+ ed.set_cname (enum_cname);
+ }
+
+ next ();
+
+ string common_prefix = null;
+
+ while (current_token == MarkupTokenType.START_ELEMENT) {
+ if (reader.get_attribute ("introspectable") == "0") {
+ skip_element ();
+ continue;
+ }
+
+ if (reader.name == "member") {
+ ErrorCode ec = parse_error_member ();
+ ed.add_code (ec);
+ calculate_common_prefix (ref common_prefix, ec.get_cname ());
+ } else {
+ // error
+ break;
+ }
+ }
+
+ ed.set_cprefix (common_prefix);
+
+ end_element ("enumeration");
+ return ed;
+ }
+
Enum parse_bitfield () {
start_element ("bitfield");
var en = new Enum (reader.get_attribute ("name"), get_current_src ());
@@ -383,6 +427,23 @@ public class Vala.GirParser : CodeVisitor {
return ev;
}
+ ErrorCode parse_error_member () {
+ start_element ("member");
+
+ ErrorCode ec;
+ string name = string.joinv ("_", reader.get_attribute ("name").up ().split ("-"));
+ string value = reader.get_attribute ("value");
+ if (value != null) {
+ ec = new ErrorCode.with_value (name, new IntegerLiteral (value));
+ } else {
+ ec = new ErrorCode (name);
+ }
+
+ next ();
+ end_element ("member");
+ return ec;
+ }
+
DataType parse_return_value (out string? ctype = null) {
start_element ("return-value");
string transfer = reader.get_attribute ("transfer-ownership");
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]