Parse GCC extensions in the parser instead of just undeffing them in the
[platform/upstream/gobject-introspection.git] / giscanner / scannerlexer.l
1 /* -*- Mode: C -*-
2 /* GObject introspection: C lexer
3  *
4  * Copyright (c) 1997 Sandro Sigala  <ssigala@globalnet.it>
5  * Copyright (c) 2007-2008 Jürg Billeter  <j@bitron.ch>
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 %{
31 #include <ctype.h>
32 #include <stdio.h>
33
34 #include <glib.h>
35 #include "sourcescanner.h"
36 #include "scannerparser.h"
37 #include "grealpath.h"
38
39 int lineno;
40
41 extern int yylex (GISourceScanner *scanner);
42 #define YY_DECL int yylex (GISourceScanner *scanner)
43 static int yywrap (void);
44 static void parse_comment (GISourceScanner *scanner);
45 static void process_directive (GISourceScanner *scanner);
46 static int check_identifier (GISourceScanner *scanner, const char *);
47 static int parse_ignored_macro (void);
48 %}
49
50 intsuffix                               ([uU][lL]?[lL]?)|([lL][lL]?[uU]?)
51 fracconst                               ([0-9]*\.[0-9]+)|([0-9]+\.)
52 exppart                                 [eE][-+]?[0-9]+
53 floatsuffix                             [fFlL]
54 chartext                                ([^\'])|(\\.) 
55 stringtext                              ([^\"])|(\\.)
56
57 %%
58
59 "\n"                                    { ++lineno; } /* " */
60 [\t\f\v\r ]+                            { /* Ignore whitespace. */ }
61
62 "/*"                                    { parse_comment(scanner); }
63 "//".*                                  { }
64
65 "#define "[a-zA-Z_][a-zA-Z_0-9]*"("     { yyless (yyleng - 1); return FUNCTION_MACRO; }
66 "#define "[a-zA-Z_][a-zA-Z_0-9]*        { return OBJECT_MACRO; }
67
68 "#"                                     { process_directive(scanner); }
69
70 "{"                                     { return '{'; }
71 "<%"                                    { return '{'; }
72 "}"                                     { return '}'; }
73 "%>"                                    { return '}'; }
74 "["                                     { return '['; }
75 "<:"                                    { return '['; }
76 "]"                                     { return ']'; }
77 ":>"                                    { return ']'; }
78 "("                                     { return '('; }
79 ")"                                     { return ')'; }
80 ";"                                     { return ';'; }
81 ":"                                     { return ':'; }
82 "..."                                   { return ELLIPSIS; }
83 "?"                                     { return '?'; }
84 "."                                     { return '.'; }
85 "+"                                     { return '+'; }
86 "-"                                     { return '-'; }
87 "*"                                     { return '*'; }
88 "/"                                     { return '/'; }
89 "%"                                     { return '%'; }
90 "^"                                     { return '^'; }
91 "&"                                     { return '&'; }
92 "|"                                     { return '|'; }
93 "~"                                     { return '~'; }
94 "!"                                     { return '!'; }
95 "="                                     { return '='; }
96 "<"                                     { return '<'; }
97 ">"                                     { return '>'; }
98 "+="                                    { return ADDEQ; }
99 "-="                                    { return SUBEQ; }
100 "*="                                    { return MULEQ; }
101 "/="                                    { return DIVEQ; }
102 "%="                                    { return MODEQ; }
103 "^="                                    { return XOREQ; }
104 "&="                                    { return ANDEQ; }
105 "|="                                    { return OREQ; }
106 "<<"                                    { return SL; }
107 ">>"                                    { return SR; }
108 "<<="                                   { return SLEQ; }
109 ">>="                                   { return SREQ; }
110 "=="                                    { return EQ; }
111 "!="                                    { return NOTEQ; }
112 "<="                                    { return LTEQ; }
113 ">="                                    { return GTEQ; }
114 "&&"                                    { return ANDAND; }
115 "||"                                    { return OROR; }
116 "++"                                    { return PLUSPLUS; }
117 "--"                                    { return MINUSMINUS; }
118 ","                                     { return ','; }
119 "->"                                    { return ARROW; }
120
121 "__attribute__"                         { if (!parse_ignored_macro()) REJECT; }
122 "__const"                               { return CONST; }
123 "__extension__"                         { return EXTENSION; }
124 "__inline"                              { return INLINE; }
125 "__nonnull"                             { if (!parse_ignored_macro()) REJECT; }
126 "__restrict"                            { return RESTRICT; }
127
128 [a-zA-Z_][a-zA-Z_0-9]*                  { if (scanner->macro_scan) return IDENTIFIER; else REJECT; }
129
130 "auto"                                  { return AUTO; }
131 "_Bool"                                 { return BOOL; }
132 "break"                                 { return BREAK; }
133 "case"                                  { return CASE; }
134 "char"                                  { return CHAR; }
135 "const"                                 { return CONST; }
136 "continue"                              { return CONTINUE; }
137 "default"                               { return DEFAULT; }
138 "do"                                    { return DO; }
139 "double"                                { return DOUBLE; }
140 "else"                                  { return ELSE; }
141 "enum"                                  { return ENUM; }
142 "extern"                                { return EXTERN; }
143 "float"                                 { return FLOAT; }
144 "for"                                   { return FOR; }
145 "goto"                                  { return GOTO; }
146 "if"                                    { return IF; }
147 "inline"                                { return INLINE; }
148 "__inline__"                            { return INLINE; }
149 "int"                                   { return INT; }
150 "long"                                  { return LONG; }
151 "register"                              { return REGISTER; }
152 "restrict"                              { return RESTRICT; }
153 "return"                                { return RETURN; }
154 "short"                                 { return SHORT; }
155 "signed"                                { return SIGNED; }
156 "sizeof"                                { return SIZEOF; }
157 "static"                                { return STATIC; }
158 "struct"                                { return STRUCT; }
159 "switch"                                { return SWITCH; }
160 "typedef"                               { return TYPEDEF; }
161 "union"                                 { return UNION; }
162 "unsigned"                              { return UNSIGNED; }
163 "void"                                  { return VOID; }
164 "volatile"                              { return VOLATILE; }
165 "while"                                 { return WHILE; }
166
167 [a-zA-Z_][a-zA-Z_0-9]*                  { return check_identifier(scanner, yytext); }
168
169 "0"[xX][0-9a-fA-F]+{intsuffix}?         { return INTEGER; }
170 "0"[0-7]+{intsuffix}?                   { return INTEGER; }
171 [0-9]+{intsuffix}?                      { return INTEGER; }
172
173 {fracconst}{exppart}?{floatsuffix}?     { return FLOATING; }
174 [0-9]+{exppart}{floatsuffix}?           { return FLOATING; }
175
176 "'"{chartext}*"'"                       { return CHARACTER; }
177 "L'"{chartext}*"'"                      { return CHARACTER; }
178
179 "\""{stringtext}*"\""                   { return STRING; }
180 "L\""{stringtext}*"\""                  { return STRING; }
181
182 .                                       { fprintf(stderr, "%s:%d: unexpected character `%c'\n", scanner->current_filename, lineno, yytext[0]); }
183
184 %%
185
186 static int
187 yywrap (void)
188 {
189   return 1;
190 }
191
192
193 static void
194 parse_gtkdoc (GISourceScanner *scanner,
195               gchar           *symbol,
196               int             *c1,
197               int             *c2)
198 {
199   gboolean isline = FALSE;
200   gchar line[256];
201   int i;
202   gchar **parts;
203   GISourceDirective *directive;
204   char *name,*value;
205   GSList *directives;
206   GSList *options = NULL;
207   char *rname;
208
209   i = 0;
210   do 
211     {
212       *c1 = *c2;
213       if (*c1 == '\n')
214         {
215           isline = TRUE;
216           break;
217         }
218       if (i >= 256)
219         break;
220       line[i++] = *c1;
221       *c2 = input();
222     } while (*c2 != EOF && !(*c1 == '*' && *c2 == '/'));
223   
224   if (!isline)
225     return;
226
227   line[i] = '\0';
228
229   parts = g_strsplit (line, ": ", 3);
230
231   if (g_strv_length (parts) >= 2)
232     {
233       name = parts[0];
234
235       if (g_strv_length (parts) == 3) 
236         {
237           char *ptr = parts[1];
238           GString *current = NULL;
239           gint8 pstack = 0;
240
241           current = g_string_new ("");
242           value = parts[2];
243
244           do
245             {
246               if (*ptr == '(')
247                 {
248                   pstack++;
249                   if (pstack == 1)
250                     continue;
251                 }
252               else if (*ptr == ')')
253                 pstack--;
254                        
255               if (pstack == 0)
256                 {
257                   options = g_slist_prepend (options, current->str);
258                   break;
259                 }
260               g_string_append_c (current, *ptr);
261             }
262           while (*ptr++);
263
264           g_string_free (current, FALSE);
265         } 
266       else
267         value = parts[1];
268     }
269   else /* parts == 1 */
270     {
271       name = parts[0];
272       value = NULL;
273     }
274
275   /*
276    * This is a special case for return values, name will only be
277    * 'eturn' or a valid name, check the call site.
278    * Context-sensitive parsing would probably be the right way to go
279    */
280   if (g_ascii_strncasecmp ("eturn", name, 5) == 0)
281     rname = "return";
282   else
283     rname = name;
284
285   directive = gi_source_directive_new (rname, value, options);
286   directives = g_hash_table_lookup (scanner->directives_map, symbol);
287   directives = g_slist_prepend (directives, directive);
288   g_hash_table_replace (scanner->directives_map, 
289                         g_strdup (symbol), directives);
290
291   g_strfreev (parts);
292   
293 }
294
295
296 static void
297 parse_comment (GISourceScanner *scanner)
298 {
299   GString *symbol = NULL;
300   gboolean startofline = FALSE, have_symbol = FALSE, start1 = FALSE, start_symbol = FALSE;
301   int c1, c2;
302
303   c1 = input();
304   c2 = input();
305
306   while (c2 != EOF && !(c1 == '*' && c2 == '/'))
307     {
308       if (c1 == ':')
309         have_symbol = TRUE;
310       else if (c1 == '\n')
311          start1 = TRUE;
312       else if (c1 == '*' && start1)
313          start_symbol = TRUE;
314       else if (!have_symbol && start_symbol) 
315         {
316           if (!symbol)
317             symbol = g_string_new ("");
318           if (c1 != ' ')
319             g_string_append_c (symbol, c1);
320         }
321
322       if (c1 == '\n') 
323         {
324           ++lineno;
325           startofline = TRUE;
326         }
327
328       c1 = c2;
329       c2 = input();
330
331       if ((c1 != '*' && c1 != ' '))
332           startofline = FALSE;
333
334       if (startofline && (c1 == ' ') && (c2 == '@' || (c2 == 'r') || (c2 == 'R')))
335         {
336            c1 = c2;
337            c2 = input();
338            if (symbol)
339              parse_gtkdoc (scanner, symbol->str, &c1, &c2);
340         }
341     }
342
343   if (symbol)
344     g_string_free (symbol, TRUE);
345   
346 }
347
348 static int
349 check_identifier (GISourceScanner *scanner,
350                   const char  *s)
351 {
352         /*
353          * This function checks if `s' is a type name or an
354          * identifier.
355          */
356
357         if (gi_source_scanner_is_typedef (scanner, s)) {
358                 return TYPEDEF_NAME;
359         } else if (strcmp (s, "__builtin_va_list") == 0) {
360                 return TYPEDEF_NAME;
361         }
362
363         return IDENTIFIER;
364 }
365
366 static void
367 process_directive (GISourceScanner *scanner)
368 {
369         /* extract current filename from #line directives */
370         GString *filename_builder;
371         gboolean in_string, found_filename;
372
373         lineno = 0;
374         found_filename = FALSE;
375         in_string = FALSE;
376         filename_builder = g_string_new ("");
377
378         int c = input ();
379         while (c != EOF && c != '\n') {
380                 if (!in_string) {
381                         if (c == '\"') {
382                                 in_string = TRUE;
383                                 found_filename = TRUE;
384                         } else if (c >= '0' && c <= '9') {
385                                 if (!found_filename) {
386                                         lineno = lineno * 10 + (c - '0');
387                                 }
388                         }
389                 } else {
390                         if (c == '\"') {
391                                 in_string = FALSE;
392                         } else if (c == '\\') {
393                                 g_string_append_c (filename_builder, c);
394                                 c = input ();
395                                 g_string_append_c (filename_builder, c);
396                         } else {
397                                 g_string_append_c (filename_builder, c);
398                         }
399                 }
400                 c = input ();
401         }
402
403         if (filename_builder->len > 0) {
404                 char *filename = g_strcompress (filename_builder->str);
405                 if (g_realpath (filename))
406                   {
407                     g_free (scanner->current_filename);
408                     scanner->current_filename = g_realpath (filename);
409                     g_assert (scanner->current_filename);
410                     g_free(filename);
411                   }
412         }
413
414         g_string_free (filename_builder, TRUE);
415 }
416
417 /*
418  * This parses a macro which is ignored, such as
419  * __attribute__(x)
420  */
421 static int
422 parse_ignored_macro (void)
423 {
424         int c;
425         int nest;
426
427         while ((c = input ()) != EOF && isspace (c))
428                 ;
429         if (c != '(')
430                 return FALSE;
431         while ((c = input ()) != EOF && isspace (c))
432                 ;
433         if (c != '(')
434                 return FALSE;
435
436         nest = 0;
437         while ((c = input ()) != EOF && (nest > 0 || c != ')')) {
438                 if (c == '(')
439                         nest++;
440                 else if (c == ')')
441                         nest--;
442                 else if (c == '"') {
443                         while ((c = input ()) != EOF && c != '"') {
444                                 if (c == '\\')
445                                         c = input ();
446                         }
447                 } else if (c == '\'') {
448                         c = input ();
449                         if (c == '\\')
450                                 c = input ();
451                         else if (c == '\'')
452                                 return FALSE;
453                         c = input ();
454                         if (c != '\'')
455                                 return FALSE;
456                 } else if (c == '\n')
457                         lineno++;
458         }
459
460         while ((c = input ()) != EOF && isspace (c))
461                 ;
462         if (c != ')')
463                 return FALSE;
464
465         return TRUE;
466 }