[gobject-introspection/wip/structure-parsing: 1/3] scanner: Unify parsing of compounds



commit 51fe1536f23fb3b8ee2d8083036278c808128966
Author: Colin Walters <walters verbum org>
Date:   Wed Sep 8 15:08:19 2010 -0400

    scanner: Unify parsing of compounds
    
    Avoids duplicate code.

 giscanner/girparser.py |   61 ++++++++++++++++++-----------------------------
 1 files changed, 23 insertions(+), 38 deletions(-)
---
diff --git a/giscanner/girparser.py b/giscanner/girparser.py
index 5c54dbe..ab2b29d 100644
--- a/giscanner/girparser.py
+++ b/giscanner/girparser.py
@@ -330,56 +330,41 @@ class GIRParser(object):
                 res.append(fieldobj)
         return res
 
-    def _parse_record(self, node, anonymous=False):
-        struct = ast.Record(node.attrib.get('name'),
-                            node.attrib.get(_cns('type')),
-                            disguised=node.attrib.get('disguised') == '1',
-                            gtype_name=node.attrib.get(_glibns('type-name')),
-                            get_type=node.attrib.get(_glibns('get-type')),
-                            c_symbol_prefix=node.attrib.get(_cns('symbol-prefix')))
-        is_gtype_struct_for = node.attrib.get(_glibns('is-gtype-struct-for'))
-        if is_gtype_struct_for is not None:
-            struct.is_gtype_struct_for = self._namespace.type_from_name(is_gtype_struct_for)
+    def _parse_compound(self, cls, node):
+        compound = cls(node.attrib.get('name'),
+                       ctype=node.attrib.get(_cns('type')),
+                       disguised=node.attrib.get('disguised') == '1',
+                       gtype_name=node.attrib.get(_glibns('type-name')),
+                       get_type=node.attrib.get(_glibns('get-type')),
+                       c_symbol_prefix=node.attrib.get(_cns('symbol-prefix')))
         if node.attrib.get('foreign') == '1':
-            struct.foreign = True
-        self._parse_generic_attribs(node, struct)
-        if not anonymous:
-            self._namespace.append(struct)
-
-        struct.fields.extend(self._parse_fields(node))
+            compound.foreign = True
+        self._parse_generic_attribs(node, compound)
+        compound.fields.extend(self._parse_fields(node))
         for method in self._find_children(node, _corens('method')):
-            struct.methods.append(
+            compound.methods.append(
                 self._parse_function_common(method, ast.Function))
         for func in self._find_children(node, _corens('function')):
-            struct.static_methods.append(
+            compound.static_methods.append(
                 self._parse_function_common(func, ast.Function))
         for ctor in self._find_children(node, _corens('constructor')):
-            struct.constructors.append(
+            compound.constructors.append(
                 self._parse_function_common(ctor, ast.Function))
+        return compound
+
+    def _parse_record(self, node, anonymous=False):
+        struct = self._parse_compound(ast.Record, node)
+        is_gtype_struct_for = node.attrib.get(_glibns('is-gtype-struct-for'))
+        if is_gtype_struct_for is not None:
+            struct.is_gtype_struct_for = self._namespace.type_from_name(is_gtype_struct_for)
+        if not anonymous:
+            self._namespace.append(struct)
         return struct
 
     def _parse_union(self, node, anonymous=False):
-        union = ast.Union(node.attrib.get('name'),
-                          node.attrib.get(_cns('type')),
-                          gtype_name=node.attrib.get(_glibns('type-name')),
-                          get_type=node.attrib.get(_glibns('get-type')),
-                          c_symbol_prefix=node.attrib.get(_cns('symbol-prefix')))
+        union = self._parse_compound(ast.Union, node)
         if not anonymous:
             self._namespace.append(union)
-
-        for callback in self._find_children(node, _corens('callback')):
-            union.fields.append(
-                self._parse_function_common(callback, ast.Callback))
-        union.fields.extend(self._parse_fields(node))
-        for method in self._find_children(node, _corens('method')):
-            union.methods.append(
-                self._parse_function_common(method, ast.Function))
-        for func in self._find_children(node, _corens('function')):
-            union.static_methods.append(
-                self._parse_function_common(func, ast.Function))
-        for ctor in self._find_children(node, _corens('constructor')):
-            union.constructors.append(
-                self._parse_function_common(ctor, ast.Function))
         return union
 
     def _parse_type_simple(self, typenode):



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