Bug 555964 - Parse floating-point #defines
authorColin Walters <walters@verbum.org>
Wed, 25 Feb 2009 20:25:36 +0000 (15:25 -0500)
committerColin Walters <walters@verbum.org>
Wed, 25 Feb 2009 20:34:21 +0000 (15:34 -0500)
Previously we just supported int and string, add double to this.
Technically we should probably differentiate between float and
double, but it's not likely to be very useful in practice to do so.

giscanner/giscannermodule.c
giscanner/scannerparser.y
giscanner/sourcescanner.h
giscanner/sourcescanner.py
giscanner/transformer.py
tests/scanner/foo-1.0-expected.gir
tests/scanner/foo-1.0-expected.tgir
tests/scanner/foo.h

index 7d63784..80d7f6b 100644 (file)
@@ -135,9 +135,26 @@ static PyObject *
 symbol_get_const_int (PyGISourceSymbol *self,
                      void             *context)
 {
+  if (!self->symbol->const_int_set)
+    {
+      Py_INCREF(Py_None);
+      return Py_None;
+    }
   return PyInt_FromLong (self->symbol->const_int);
 }
 
+static PyObject *
+symbol_get_const_double (PyGISourceSymbol *self,
+                         void             *context)
+{
+  if (!self->symbol->const_double_set)
+    {
+      Py_INCREF(Py_None);
+      return Py_None;
+    }
+  return PyFloat_FromDouble (self->symbol->const_double);
+}
+
 static PyObject *
 symbol_get_const_string (PyGISourceSymbol *self,
                         void             *context)
@@ -171,7 +188,9 @@ static const PyGetSetDef _PyGISourceSymbol_getsets[] = {
   { "ident", (getter)symbol_get_ident, NULL, NULL},
   { "base_type", (getter)symbol_get_base_type, NULL, NULL},
   /* gboolean const_int_set; */
-  { "const_int", (getter)symbol_get_const_int, NULL, NULL},  
+  { "const_int", (getter)symbol_get_const_int, NULL, NULL},
+  /* gboolean const_double_set; */
+  { "const_double", (getter)symbol_get_const_double, NULL, NULL},
   { "const_string", (getter)symbol_get_const_string, NULL, NULL},
   { "source_filename", (getter)symbol_get_source_filename, NULL, NULL},
   { 0 }
index 85640fe..c92a538 100644 (file)
@@ -167,7 +167,10 @@ primary_expression
          }
        | FLOATING
          {
-               $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+               $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+               $$->const_double_set = TRUE;
+               $$->const_double = 0.0;
+        sscanf (yytext, "%lf", &($$->const_double));
          }
        | strings
        | '(' expression ')'
@@ -1256,7 +1259,7 @@ function_macro_define
 object_macro_define
        : object_macro constant_expression
          {
-               if ($2->const_int_set || $2->const_string != NULL) {
+               if ($2->const_int_set || $2->const_double_set || $2->const_string != NULL) {
                        $2->ident = $1;
                        gi_source_scanner_add_symbol (scanner, $2);
                        gi_source_symbol_unref ($2);
index bbc4977..a49de5c 100644 (file)
@@ -116,6 +116,8 @@ struct _GISourceSymbol
   gboolean const_int_set;
   int const_int;
   char *const_string;
+  gboolean const_double_set;
+  double const_double;
   char *source_filename;
 };
 
index 30e624b..1a8194d 100644 (file)
@@ -146,7 +146,8 @@ class SourceType(object):
 
 
 class SourceSymbol(object):
-    __members__ = ['const_int', 'const_string', 'ident', 'type', 'base_type']
+    __members__ = ['const_int', 'const_double', 'const_string', 'ident',
+                   'type', 'base_type']
 
     def __init__(self, scanner, symbol):
         self._scanner = scanner
@@ -162,6 +163,10 @@ class SourceSymbol(object):
     def const_int(self):
         return self._symbol.const_int
 
+    @property
+    def const_double(self):
+        return self._symbol.const_double
+
     @property
     def const_string(self):
         return self._symbol.const_string
index 58c2fbc..eb76f83 100644 (file)
@@ -465,12 +465,18 @@ class Transformer(object):
         if not symbol.source_filename.endswith('.h'):
             return None
         name = self.remove_prefix(symbol.ident)
-        if symbol.const_string is None:
+        if symbol.const_string is not None:
+            type_name = 'utf8'
+            value = symbol.const_string
+        elif symbol.const_int is not None:
             type_name = 'int'
             value = symbol.const_int
+        elif symbol.const_double is not None:
+            type_name = 'double'
+            value = symbol.const_double
         else:
-            type_name = 'utf8'
-            value = symbol.const_string
+            raise AssertionError()
+
         const = Constant(name, type_name, value)
         return const
 
index 2f82175..4bd7745 100644 (file)
@@ -399,6 +399,9 @@ and/or use gtk-doc annotations.  -->
         </parameters>
       </callback>
     </record>
+    <constant name="PIE_IS_TASTY" value="3.14159">
+      <type name="double"/>
+    </constant>
     <record name="Rectangle" c:type="FooRectangle">
       <field name="x" writable="1">
         <type name="int" c:type="gint"/>
index edf58ef..dcd9789 100644 (file)
         <type name="GObject.ObjectClass"/>
       </field>
     </record>
+    <constant name="PIE_IS_TASTY" value="3.141590">
+      <type name="double"/>
+    </constant>
     <record name="Rectangle">
       <field name="x" writable="1">
         <type name="int"/>
index d3dd29f..da30df3 100644 (file)
@@ -8,6 +8,8 @@
 
 #define FOO_DEFINE_SHOULD_BE_EXPOSED "should be exposed"
 
+#define FOO_PIE_IS_TASTY 3.14159
+
 #define FOO_TYPE_INTERFACE           (foo_interface_get_type ())
 #define FOO_INTERFACE(object)        (G_TYPE_CHECK_INSTANCE_CAST ((object), FOO_TYPE_INTERFACE, FooInterface))
 #define FOO_IS_INTERFACE(object)     (G_TYPE_CHECK_INSTANCE_TYPE ((object), FOO_TYPE_INTERFACE))