[gobject-introspection] [sourcescanner] Support for line numbers on Symbol



commit ef76b0e4ef0a5d4790147a746a944c5fd5f91159
Author: Alan Knowles <alan akbkhome com>
Date:   Sun Apr 4 16:52:24 2010 +0800

    [sourcescanner] Support for line numbers on Symbol
    
    Add line numbers to symbols, which can be useful
    in later stages of the scanner.

 giscanner/giscannermodule.c |    8 +++
 giscanner/scannerparser.y   |  104 +++++++++++++++++++++---------------------
 giscanner/sourcescanner.c   |    3 +-
 giscanner/sourcescanner.h   |    3 +-
 4 files changed, 64 insertions(+), 54 deletions(-)
---
diff --git a/giscanner/giscannermodule.c b/giscanner/giscannermodule.c
index 0b24714..2d2e073 100644
--- a/giscanner/giscannermodule.c
+++ b/giscanner/giscannermodule.c
@@ -112,6 +112,13 @@ symbol_get_type (PyGISourceSymbol *self,
 }
 
 static PyObject *
+symbol_get_line (PyGISourceSymbol *self,
+		 void             *context)
+{
+  return PyInt_FromLong (self->symbol->line);
+}
+
+static PyObject *
 symbol_get_ident (PyGISourceSymbol *self,
 		  void            *context)
 {
@@ -194,6 +201,7 @@ static const PyGetSetDef _PyGISourceSymbol_getsets[] = {
   { "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},
+  { "line", (getter)symbol_get_line, NULL, NULL},
   { 0 }
 };
 
diff --git a/giscanner/scannerparser.y b/giscanner/scannerparser.y
index b38fb89..600aee0 100644
--- a/giscanner/scannerparser.y
+++ b/giscanner/scannerparser.y
@@ -145,14 +145,14 @@ primary_expression
 	  {
 		$$ = g_hash_table_lookup (const_table, $1);
 		if ($$ == NULL) {
-			$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+			$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 		} else {
 			$$ = gi_source_symbol_ref ($$);
 		}
 	  }
 	| INTEGER
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		if (g_str_has_prefix (yytext, "0x") && strlen (yytext) > 2) {
 			$$->const_int = strtol (yytext + 2, NULL, 16);
@@ -164,11 +164,11 @@ primary_expression
 	  }
 	| CHARACTER
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 	  }
 	| FLOATING
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_double_set = TRUE;
 		$$->const_double = 0.0;
         sscanf (yytext, "%lf", &($$->const_double));
@@ -184,7 +184,7 @@ primary_expression
 strings
 	: STRING
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		yytext[strlen (yytext) - 1] = '\0';
 		$$->const_string = g_strcompress (yytext + 1);
                 if (!g_utf8_validate ($$->const_string, -1, NULL))
@@ -226,31 +226,31 @@ postfix_expression
 	: primary_expression
 	| postfix_expression '[' expression ']'
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 	  }
 	| postfix_expression '(' argument_expression_list ')'
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 	  }
 	| postfix_expression '(' ')'
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 	  }
 	| postfix_expression '.' identifier_or_typedef_name
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 	  }
 	| postfix_expression ARROW identifier_or_typedef_name
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 	  }
 	| postfix_expression PLUSPLUS
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 	  }
 	| postfix_expression MINUSMINUS
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 	  }
 	;
 
@@ -263,11 +263,11 @@ unary_expression
 	: postfix_expression
 	| PLUSPLUS unary_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 	  }
 	| MINUSMINUS unary_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 	  }
 	| unary_operator cast_expression
 	  {
@@ -288,18 +288,18 @@ unary_expression
 			$$->const_int = !gi_source_symbol_get_const_boolean ($2);
 			break;
 		default:
-			$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+			$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 			break;
 		}
 	  }
 	| SIZEOF unary_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 	  }
 	| SIZEOF '(' type_name ')'
 	  {
 		ctype_free ($3);
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 	  }
 	;
 
@@ -343,13 +343,13 @@ multiplicative_expression
 	: cast_expression
 	| multiplicative_expression '*' cast_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		$$->const_int = $1->const_int * $3->const_int;
 	  }
 	| multiplicative_expression '/' cast_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		if ($3->const_int != 0) {
 			$$->const_int = $1->const_int / $3->const_int;
@@ -357,7 +357,7 @@ multiplicative_expression
 	  }
 	| multiplicative_expression '%' cast_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		if ($3->const_int != 0) {
 			$$->const_int = $1->const_int % $3->const_int;
@@ -369,13 +369,13 @@ additive_expression
 	: multiplicative_expression
 	| additive_expression '+' multiplicative_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		$$->const_int = $1->const_int + $3->const_int;
 	  }
 	| additive_expression '-' multiplicative_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		$$->const_int = $1->const_int - $3->const_int;
 	  }
@@ -385,7 +385,7 @@ shift_expression
 	: additive_expression
 	| shift_expression SL additive_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		$$->const_int = $1->const_int << $3->const_int;
 
@@ -397,7 +397,7 @@ shift_expression
 	  }
 	| shift_expression SR additive_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		$$->const_int = $1->const_int >> $3->const_int;
 	  }
@@ -407,25 +407,25 @@ relational_expression
 	: shift_expression
 	| relational_expression '<' shift_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		$$->const_int = $1->const_int < $3->const_int;
 	  }
 	| relational_expression '>' shift_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		$$->const_int = $1->const_int > $3->const_int;
 	  }
 	| relational_expression LTEQ shift_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		$$->const_int = $1->const_int <= $3->const_int;
 	  }
 	| relational_expression GTEQ shift_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		$$->const_int = $1->const_int >= $3->const_int;
 	  }
@@ -435,13 +435,13 @@ equality_expression
 	: relational_expression
 	| equality_expression EQ relational_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		$$->const_int = $1->const_int == $3->const_int;
 	  }
 	| equality_expression NOTEQ relational_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		$$->const_int = $1->const_int != $3->const_int;
 	  }
@@ -451,7 +451,7 @@ and_expression
 	: equality_expression
 	| and_expression '&' equality_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		$$->const_int = $1->const_int & $3->const_int;
 	  }
@@ -461,7 +461,7 @@ exclusive_or_expression
 	: and_expression
 	| exclusive_or_expression '^' and_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		$$->const_int = $1->const_int ^ $3->const_int;
 	  }
@@ -471,7 +471,7 @@ inclusive_or_expression
 	: exclusive_or_expression
 	| inclusive_or_expression '|' exclusive_or_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		$$->const_int = $1->const_int | $3->const_int;
 	  }
@@ -481,7 +481,7 @@ logical_and_expression
 	: inclusive_or_expression
 	| logical_and_expression ANDAND inclusive_or_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		$$->const_int =
 		  gi_source_symbol_get_const_boolean ($1) &&
@@ -493,7 +493,7 @@ logical_or_expression
 	: logical_and_expression
 	| logical_or_expression OROR logical_and_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, lineno);
 		$$->const_int_set = TRUE;
 		$$->const_int =
 		  gi_source_symbol_get_const_boolean ($1) ||
@@ -513,7 +513,7 @@ assignment_expression
 	: conditional_expression
 	| unary_expression assignment_operator assignment_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 	  }
 	;
 
@@ -536,7 +536,7 @@ expression
 	| expression ',' assignment_expression
 	| EXTENSION expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 	  }
 	;
 
@@ -714,7 +714,7 @@ struct_or_union_specifier
 		$$->name = $2;
 		$$->child_list = $4;
 
-		GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 		if ($$->type == CTYPE_STRUCT) {
 			sym->type = CSYMBOL_TYPE_STRUCT;
 		} else if ($$->type == CTYPE_UNION) {
@@ -810,12 +810,12 @@ struct_declarator_list
 struct_declarator
 	: /* empty, support for anonymous structs and unions */
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 	  }
 	| declarator
 	| ':' constant_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 	  }
 	| declarator ':' constant_expression
 	  {
@@ -881,7 +881,7 @@ enumerator_list
 enumerator
 	: identifier
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_OBJECT);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_OBJECT, lineno);
 		$$->ident = $1;
 		$$->const_int_set = TRUE;
 		$$->const_int = ++last_enum_value;
@@ -889,7 +889,7 @@ enumerator
 	  }
 	| identifier '=' constant_expression
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_OBJECT);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_OBJECT, lineno);
 		$$->ident = $1;
 		$$->const_int_set = TRUE;
 		$$->const_int = $3->const_int;
@@ -936,7 +936,7 @@ declarator
 direct_declarator
 	: identifier
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 		$$->ident = $1;
 	  }
 	| '(' declarator ')'
@@ -1031,25 +1031,25 @@ parameter_declaration
 	  }
 	| declaration_specifiers
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 		$$->base_type = $1;
 	  }
 	| ELLIPSIS
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_ELLIPSIS);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_ELLIPSIS, lineno);
 	  }
 	;
 
 identifier_list
 	: identifier
 	  {
-		GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 		sym->ident = $1;
 		$$ = g_list_append (NULL, sym);
 	  }
 	| identifier_list ',' identifier
 	  {
-		GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		GISourceSymbol *sym = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 		sym->ident = $3;
 		$$ = g_list_append ($1, sym);
 	  }
@@ -1063,7 +1063,7 @@ type_name
 abstract_declarator
 	: pointer
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 		gi_source_symbol_merge_type ($$, $1);
 	  }
 	| direct_abstract_declarator
@@ -1081,12 +1081,12 @@ direct_abstract_declarator
 	  }
 	| '[' ']'
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 		gi_source_symbol_merge_type ($$, gi_source_array_new (NULL));
 	  }
 	| '[' assignment_expression ']'
 	  {
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 		gi_source_symbol_merge_type ($$, gi_source_array_new ($2));
 	  }
 	| direct_abstract_declarator '[' ']'
@@ -1102,7 +1102,7 @@ direct_abstract_declarator
 	| '(' ')'
 	  {
 		GISourceType *func = gi_source_function_new ();
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 		gi_source_symbol_merge_type ($$, func);
 	  }
 	| '(' parameter_list ')'
@@ -1112,7 +1112,7 @@ direct_abstract_declarator
 		if ($2 != NULL && ($2->next != NULL || ((GISourceSymbol *) $2->data)->base_type->type != CTYPE_VOID)) {
 			func->child_list = $2;
 		}
-		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID);
+		$$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, lineno);
 		gi_source_symbol_merge_type ($$, func);
 	  }
 	| direct_abstract_declarator '(' ')'
diff --git a/giscanner/sourcescanner.c b/giscanner/sourcescanner.c
index 79d89cd..14e3a3b 100644
--- a/giscanner/sourcescanner.c
+++ b/giscanner/sourcescanner.c
@@ -24,11 +24,12 @@
 #include <string.h>
 
 GISourceSymbol *
-gi_source_symbol_new (GISourceSymbolType type)
+gi_source_symbol_new (GISourceSymbolType type, int line)
 {
   GISourceSymbol *s = g_slice_new0 (GISourceSymbol);
   s->ref_count = 1;
   s->type = type;
+  s->line = line;
   return s;
 }
 
diff --git a/giscanner/sourcescanner.h b/giscanner/sourcescanner.h
index a49de5c..581b6b1 100644
--- a/giscanner/sourcescanner.h
+++ b/giscanner/sourcescanner.h
@@ -119,6 +119,7 @@ struct _GISourceSymbol
   gboolean const_double_set;
   double const_double;
   char *source_filename;
+  int line;
 };
 
 struct _GISourceType
@@ -146,7 +147,7 @@ GSList *            gi_source_scanner_get_symbols      (GISourceScanner  *scanne
 GSList *            gi_source_scanner_get_comments     (GISourceScanner  *scanner);
 void                gi_source_scanner_free             (GISourceScanner  *scanner);
 
-GISourceSymbol *    gi_source_symbol_new               (GISourceSymbolType  type);
+GISourceSymbol *    gi_source_symbol_new               (GISourceSymbolType  type, int line);
 gboolean            gi_source_symbol_get_const_boolean (GISourceSymbol     *symbol);
 GISourceSymbol *    gi_source_symbol_ref               (GISourceSymbol     *symbol);
 void                gi_source_symbol_unref             (GISourceSymbol     *symbol);



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