[gobject-introspection] scanner: Support boolean constants



commit e4efb97c8a20f5dcbf6cd2736bd5bcd92e1d814d
Author: Florian Müllner <fmuellner gnome org>
Date:   Fri Nov 29 01:12:40 2013 +0100

    scanner: Support boolean constants
    
    Aliasing TRUE or FALSE is not very common, but done occasionally
    for extra clarity. Namely G_SOURCE_REMOVE / G_SOURCE_CONTINUE are
    self-explanatory, unlike the "raw" booleans.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=719566

 girepository/girnode.c                 |    4 ++--
 giscanner/giscannermodule.c            |   15 +++++++++++++++
 giscanner/scannerlexer.l               |    6 ++++++
 giscanner/scannerparser.y              |   10 ++++++++--
 giscanner/sourcescanner.c              |    3 +++
 giscanner/sourcescanner.h              |    2 ++
 giscanner/sourcescanner.py             |    8 ++++++--
 giscanner/transformer.py               |    3 +++
 tests/scanner/Regress-1.0-expected.gir |    3 +++
 tests/scanner/regress.h                |    1 +
 10 files changed, 49 insertions(+), 6 deletions(-)
---
diff --git a/girepository/girnode.c b/girepository/girnode.c
index 4361474..093a378 100644
--- a/girepository/girnode.c
+++ b/girepository/girnode.c
@@ -1010,10 +1010,10 @@ parse_float_value (const gchar *str)
 static gboolean
 parse_boolean_value (const gchar *str)
 {
-  if (strcmp (str, "TRUE") == 0)
+  if (g_ascii_strcasecmp (str, "TRUE") == 0)
     return TRUE;
 
-  if (strcmp (str, "FALSE") == 0)
+  if (g_ascii_strcasecmp (str, "FALSE") == 0)
     return FALSE;
 
   return parse_int_value (str) ? TRUE : FALSE;
diff --git a/giscanner/giscannermodule.c b/giscanner/giscannermodule.c
index 2f413a0..925b3e0 100644
--- a/giscanner/giscannermodule.c
+++ b/giscanner/giscannermodule.c
@@ -193,6 +193,19 @@ symbol_get_const_string (PyGISourceSymbol *self,
 }
 
 static PyObject *
+symbol_get_const_boolean (PyGISourceSymbol *self,
+                         void             *context)
+{
+  if (!self->symbol->const_boolean_set)
+    {
+      Py_INCREF(Py_None);
+      return Py_None;
+    }
+
+  return PyBool_FromLong (self->symbol->const_boolean);
+}
+
+static PyObject *
 symbol_get_source_filename (PyGISourceSymbol *self,
                             void             *context)
 {
@@ -216,6 +229,8 @@ static const PyGetSetDef _PyGISourceSymbol_getsets[] = {
   /* gboolean const_double_set; */
   { "const_double", (getter)symbol_get_const_double, NULL, NULL},
   { "const_string", (getter)symbol_get_const_string, NULL, NULL},
+  /* gboolean const_boolean_set; */
+  { "const_boolean", (getter)symbol_get_const_boolean, NULL, NULL},
   { "source_filename", (getter)symbol_get_source_filename, NULL, NULL},
   { "line", (getter)symbol_get_line, NULL, NULL},
   { "private", (getter)symbol_get_private, NULL, NULL},
diff --git a/giscanner/scannerlexer.l b/giscanner/scannerlexer.l
index 89a34b8..413bd98 100644
--- a/giscanner/scannerlexer.l
+++ b/giscanner/scannerlexer.l
@@ -169,6 +169,12 @@ stringtext                         ([^\\\"])|(\\.)
 
 "G_GINT64_CONSTANT"                    { return INTL_CONST; }
 "G_GUINT64_CONSTANT"                   { return INTUL_CONST; }
+
+"TRUE"                                 { return BOOLEAN; }
+"FALSE"                                        { return BOOLEAN; }
+"true"                                 { return BOOLEAN; }
+"false"                                        { return BOOLEAN; }
+
 [a-zA-Z_][a-zA-Z_0-9]*                 { if (scanner->macro_scan) return check_identifier(scanner, yytext); 
else REJECT; }
 
 "asm"                                  { if (!parse_ignored_macro()) REJECT; }
diff --git a/giscanner/scannerparser.y b/giscanner/scannerparser.y
index faf7341..6cbf36a 100644
--- a/giscanner/scannerparser.y
+++ b/giscanner/scannerparser.y
@@ -226,7 +226,7 @@ toggle_conditional (GISourceScanner *scanner)
 %token <str> IDENTIFIER "identifier"
 %token <str> TYPEDEF_NAME "typedef-name"
 
-%token INTEGER FLOATING CHARACTER STRING
+%token INTEGER FLOATING BOOLEAN CHARACTER STRING
 
 %token INTL_CONST INTUL_CONST
 %token ELLIPSIS ADDEQ SUBEQ MULEQ DIVEQ MODEQ XOREQ ANDEQ OREQ SL SR
@@ -326,6 +326,12 @@ primary_expression
                $$->const_int = value;
                $$->const_int_is_unsigned = (rest && (rest[0] == 'U'));
          }
+       | BOOLEAN
+         {
+               $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
+               $$->const_boolean_set = TRUE;
+               $$->const_boolean = g_ascii_strcasecmp (yytext, "true") == 0 ? TRUE : FALSE;
+         }
        | CHARACTER
          {
                $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
@@ -1475,7 +1481,7 @@ function_macro_define
 object_macro_define
        : object_macro constant_expression
          {
-               if ($2->const_int_set || $2->const_double_set || $2->const_string != NULL) {
+               if ($2->const_int_set || $2->const_boolean_set || $2->const_double_set || $2->const_string != 
NULL) {
                        $2->ident = $1;
                        gi_source_scanner_add_symbol (scanner, $2);
                        gi_source_symbol_unref ($2);
diff --git a/giscanner/sourcescanner.c b/giscanner/sourcescanner.c
index 98dbebd..070397b 100644
--- a/giscanner/sourcescanner.c
+++ b/giscanner/sourcescanner.c
@@ -60,6 +60,9 @@ gi_source_symbol_copy (GISourceSymbol * symbol)
     new_symbol->const_int = symbol->const_int;
     new_symbol->const_int_is_unsigned = symbol->const_int_is_unsigned;
     new_symbol->const_int_set = TRUE;
+  } else if (symbol->const_boolean_set) {
+    new_symbol->const_boolean = symbol->const_boolean;
+    new_symbol->const_boolean_set = TRUE;
   } else if (symbol->const_double_set) {
     new_symbol->const_double = symbol->const_double;
     new_symbol->const_double_set = TRUE;
diff --git a/giscanner/sourcescanner.h b/giscanner/sourcescanner.h
index 39ae84d..9e37131 100644
--- a/giscanner/sourcescanner.h
+++ b/giscanner/sourcescanner.h
@@ -131,6 +131,8 @@ struct _GISourceSymbol
   char *const_string;
   gboolean const_double_set;
   double const_double;
+  gboolean const_boolean_set;
+  int const_boolean;
   char *source_filename;
   int line;
 };
diff --git a/giscanner/sourcescanner.py b/giscanner/sourcescanner.py
index 42af96f..a115158 100644
--- a/giscanner/sourcescanner.py
+++ b/giscanner/sourcescanner.py
@@ -154,8 +154,8 @@ class SourceType(object):
 
 
 class SourceSymbol(object):
-    __members__ = ['const_int', 'const_double', 'const_string', 'ident',
-                   'type', 'base_type']
+    __members__ = ['const_int', 'const_double', 'const_string', 'const_boolean',
+                   'ident', 'type', 'base_type']
 
     def __init__(self, scanner, symbol):
         self._scanner = scanner
@@ -186,6 +186,10 @@ class SourceSymbol(object):
         return self._symbol.const_string
 
     @property
+    def const_boolean(self):
+        return self._symbol.const_boolean
+
+    @property
     def ident(self):
         return self._symbol.ident
 
diff --git a/giscanner/transformer.py b/giscanner/transformer.py
index bd476cc..156148f 100644
--- a/giscanner/transformer.py
+++ b/giscanner/transformer.py
@@ -728,6 +728,9 @@ raise ValueError."""
                 value = str(symbol.const_int % 2 ** 16)
             else:
                 value = str(symbol.const_int)
+        elif symbol.const_boolean is not None:
+            typeval = ast.TYPE_BOOLEAN
+            value = "true" if symbol.const_boolean else "false"
         elif symbol.const_double is not None:
             typeval = ast.TYPE_DOUBLE
             value = '%f' % (symbol.const_double, )
diff --git a/tests/scanner/Regress-1.0-expected.gir b/tests/scanner/Regress-1.0-expected.gir
index c6ae224..a565113 100644
--- a/tests/scanner/Regress-1.0-expected.gir
+++ b/tests/scanner/Regress-1.0-expected.gir
@@ -883,6 +883,9 @@ it says it's pointer but it's actually a string.</doc>
         </array>
       </field>
     </record>
+    <constant name="BOOL_CONSTANT" value="true" c:type="REGRESS_BOOL_CONSTANT">
+      <type name="gboolean" c:type="gboolean"/>
+    </constant>
     <constant name="DOUBLE_CONSTANT"
               value="44.220000"
               c:type="REGRESS_DOUBLE_CONSTANT">
diff --git a/tests/scanner/regress.h b/tests/scanner/regress.h
index 079c6fe..3f917e6 100644
--- a/tests/scanner/regress.h
+++ b/tests/scanner/regress.h
@@ -283,6 +283,7 @@ GQuark regress_atest_error_quark (void);
 #define REGRESS_DOUBLE_CONSTANT 44.22
 #define REGRESS_STRING_CONSTANT "Some String"
 #define REGRESS_Mixed_Case_Constant 4423
+#define REGRESS_BOOL_CONSTANT TRUE
 #define REGRESS_G_GINT64_CONSTANT (G_GINT64_CONSTANT (1000))
 #define REGRESS_GUINT64_CONSTANT ((guint64) -1)
 


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