Parse and discard __typeof and __attribute.
[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 "__asm"                                 { if (!parse_ignored_macro()) REJECT; }
122 "__asm__"                               { if (!parse_ignored_macro()) REJECT; }
123 "__attribute__"                         { if (!parse_ignored_macro()) REJECT; }
124 "__attribute"                           { if (!parse_ignored_macro()) REJECT; }
125 "__const"                               { return CONST; }
126 "__extension__"                         { return EXTENSION; }
127 "__inline"                              { return INLINE; }
128 "__nonnull"                             { if (!parse_ignored_macro()) REJECT; }
129 "__restrict"                            { return RESTRICT; }
130 "__typeof"                              { if (!parse_ignored_macro()) REJECT; }
131 "_Bool"                                 { return BOOL; }
132
133 [a-zA-Z_][a-zA-Z_0-9]*                  { if (scanner->macro_scan) return IDENTIFIER; else REJECT; }
134
135 "asm"                                   { if (!parse_ignored_macro()) REJECT; }
136 "auto"                                  { return AUTO; }
137 "break"                                 { return BREAK; }
138 "case"                                  { return CASE; }
139 "char"                                  { return CHAR; }
140 "const"                                 { return CONST; }
141 "continue"                              { return CONTINUE; }
142 "default"                               { return DEFAULT; }
143 "do"                                    { return DO; }
144 "double"                                { return DOUBLE; }
145 "else"                                  { return ELSE; }
146 "enum"                                  { return ENUM; }
147 "extern"                                { return EXTERN; }
148 "float"                                 { return FLOAT; }
149 "for"                                   { return FOR; }
150 "goto"                                  { return GOTO; }
151 "if"                                    { return IF; }
152 "inline"                                { return INLINE; }
153 "int"                                   { return INT; }
154 "long"                                  { return LONG; }
155 "register"                              { return REGISTER; }
156 "restrict"                              { return RESTRICT; }
157 "return"                                { return RETURN; }
158 "short"                                 { return SHORT; }
159 "signed"                                { return SIGNED; }
160 "sizeof"                                { return SIZEOF; }
161 "static"                                { return STATIC; }
162 "struct"                                { return STRUCT; }
163 "switch"                                { return SWITCH; }
164 "typedef"                               { return TYPEDEF; }
165 "union"                                 { return UNION; }
166 "unsigned"                              { return UNSIGNED; }
167 "void"                                  { return VOID; }
168 "volatile"                              { return VOLATILE; }
169 "while"                                 { return WHILE; }
170
171 [a-zA-Z_][a-zA-Z_0-9]*                  { return check_identifier(scanner, yytext); }
172
173 "0"[xX][0-9a-fA-F]+{intsuffix}?         { return INTEGER; }
174 "0"[0-7]+{intsuffix}?                   { return INTEGER; }
175 [0-9]+{intsuffix}?                      { return INTEGER; }
176
177 {fracconst}{exppart}?{floatsuffix}?     { return FLOATING; }
178 [0-9]+{exppart}{floatsuffix}?           { return FLOATING; }
179
180 "'"{chartext}*"'"                       { return CHARACTER; }
181 "L'"{chartext}*"'"                      { return CHARACTER; }
182
183 "\""{stringtext}*"\""                   { return STRING; }
184 "L\""{stringtext}*"\""                  { return STRING; }
185
186 .                                       { fprintf(stderr, "%s:%d: unexpected character `%c'\n", scanner->current_filename, lineno, yytext[0]); }
187
188 %%
189
190 static int
191 yywrap (void)
192 {
193   return 1;
194 }
195
196
197 static void
198 parse_gtkdoc (GISourceScanner *scanner,
199               gchar           *symbol,
200               int             *c1,
201               int             *c2)
202 {
203   gboolean isline = FALSE;
204   gchar line[256];
205   int i;
206   gchar **parts;
207   GISourceDirective *directive;
208   char *name,*value;
209   GSList *directives;
210   GSList *options = NULL;
211   char *rname;
212   int n_parts;
213
214   i = 0;
215   do 
216     {
217       *c1 = *c2;
218       if (*c1 == '\n')
219         {
220           isline = TRUE;
221           break;
222         }
223       if (i >= 256)
224         break;
225       line[i++] = *c1;
226       *c2 = input();
227     } while (*c2 != EOF && !(*c1 == '*' && *c2 == '/'));
228   
229   if (!isline)
230     return;
231
232   /* Ignore lines that don't have a : - this is a hack but avoids
233    * trying to parse too many things as annotations
234    */
235   if (!strchr (line, ':'))
236     return;
237
238   line[i] = '\0';
239
240   parts = g_strsplit (line, ":", 3);
241   n_parts = g_strv_length (parts);
242
243   if (g_ascii_strcasecmp (parts[0], "eprecated") == 0)
244     {
245       if (n_parts == 3)
246         options = g_slist_prepend (options, g_strdup_printf ("%s: %s", parts[1], parts[2]));
247       else if (n_parts == 2)
248         options = g_slist_prepend (options, g_strdup (parts[1]));
249       else
250         options = g_slist_prepend (options, g_strdup (""));
251       name = parts[0];
252       value = NULL;
253     }
254   else if (n_parts >= 2)
255     {
256       name = parts[0];
257
258       if (n_parts == 3) 
259         {
260           char *ptr = g_strdup (parts[1]);
261           char *start;
262           char *end;
263
264           options = NULL;
265           start = strchr (ptr, '(');
266           while (start != NULL) 
267             {
268               end = strchr (start, ')');
269               if (end)
270                 {
271                   options = g_slist_prepend (options, g_strndup (start+1, end-(start+1)));
272                   start = strchr (end+1, '(');
273                 }
274               else
275                 {
276                   break;
277                 }
278             }
279           g_free (ptr);
280           value = parts[2];
281         } 
282       else
283         value = parts[1];
284     }
285   else /* parts == 1 */
286     {
287       name = parts[0];
288       value = NULL;
289     }
290
291   /*
292    * Special cases for global annotations.
293    * Context-sensitive parsing would probably be the right way to go.
294    */
295   if (g_ascii_strncasecmp ("eturn", name, 5) == 0)
296     rname = "return";
297   else if (g_ascii_strncasecmp ("eprecated", name, 9) == 0)
298     rname = "deprecated";
299   else
300     rname = name;
301
302   directive = gi_source_directive_new (rname, value, options);
303   directives = g_hash_table_lookup (scanner->directives_map, symbol);
304   directives = g_slist_prepend (directives, directive);
305   g_hash_table_replace (scanner->directives_map,
306                         g_strdup (symbol), directives);
307
308   g_strfreev (parts);
309 }
310
311
312 static void
313 parse_comment (GISourceScanner *scanner)
314 {
315   GString *symbol = NULL;
316   gboolean startofline = FALSE, have_symbol = FALSE, start1 = FALSE, start_symbol = FALSE;
317   int c1, c2;
318
319   c1 = input();
320   c2 = input();
321
322   while (c2 != EOF && !(c1 == '*' && c2 == '/'))
323     {
324       if (c1 == ':')
325         have_symbol = TRUE;
326       else if (c1 == '\n')
327          start1 = TRUE;
328       else if (c1 == '*' && start1)
329          start_symbol = TRUE;
330       else if (!have_symbol && start_symbol) 
331         {
332           if (!symbol)
333             symbol = g_string_new ("");
334           if (c1 != ' ')
335             g_string_append_c (symbol, c1);
336         }
337
338       if (c1 == '\n') 
339         {
340           ++lineno;
341           startofline = TRUE;
342         }
343
344       c1 = c2;
345       c2 = input();
346
347       if ((c1 != '*' && c1 != ' '))
348           startofline = FALSE;
349
350       if (startofline && (c1 == ' ') && (c2 == '@' || (c2 == 'r') || (c2 == 'R') || (c2 == 'D')))
351         {
352            c1 = c2;
353            c2 = input();
354            if (symbol)
355              parse_gtkdoc (scanner, symbol->str, &c1, &c2);
356         }
357     }
358
359   if (symbol)
360     g_string_free (symbol, TRUE);
361   
362 }
363
364 static int
365 check_identifier (GISourceScanner *scanner,
366                   const char  *s)
367 {
368         /*
369          * This function checks if `s' is a type name or an
370          * identifier.
371          */
372
373         if (gi_source_scanner_is_typedef (scanner, s)) {
374                 return TYPEDEF_NAME;
375         } else if (strcmp (s, "__builtin_va_list") == 0) {
376                 return TYPEDEF_NAME;
377         }
378
379         return IDENTIFIER;
380 }
381
382 static void
383 process_directive (GISourceScanner *scanner)
384 {
385         /* extract current filename from #line directives */
386         GString *filename_builder;
387         gboolean in_string, found_filename;
388
389         lineno = 0;
390         found_filename = FALSE;
391         in_string = FALSE;
392         filename_builder = g_string_new ("");
393
394         int c = input ();
395         while (c != EOF && c != '\n') {
396                 if (!in_string) {
397                         if (c == '\"') {
398                                 in_string = TRUE;
399                                 found_filename = TRUE;
400                         } else if (c >= '0' && c <= '9') {
401                                 if (!found_filename) {
402                                         lineno = lineno * 10 + (c - '0');
403                                 }
404                         }
405                 } else {
406                         if (c == '\"') {
407                                 in_string = FALSE;
408                         } else if (c == '\\') {
409                                 g_string_append_c (filename_builder, c);
410                                 c = input ();
411                                 g_string_append_c (filename_builder, c);
412                         } else {
413                                 g_string_append_c (filename_builder, c);
414                         }
415                 }
416                 c = input ();
417         }
418
419         if (filename_builder->len > 0) {
420                 char *filename = g_strcompress (filename_builder->str);
421                 if (g_realpath (filename))
422                   {
423                     g_free (scanner->current_filename);
424                     scanner->current_filename = g_realpath (filename);
425                     g_assert (scanner->current_filename);
426                     g_free(filename);
427                   }
428         }
429
430         g_string_free (filename_builder, TRUE);
431 }
432
433 /*
434  * This parses a macro which is ignored, such as
435  * __attribute__((x)) or __asm__ (x)
436  */
437 static int
438 parse_ignored_macro (void)
439 {
440         int c;
441         int nest;
442
443         while ((c = input ()) != EOF && isspace (c))
444                 ;
445         if (c != '(')
446                 return FALSE;
447
448         nest = 0;
449         while ((c = input ()) != EOF && (nest > 0 || c != ')')) {
450                 if (c == '(')
451                         nest++;
452                 else if (c == ')')
453                         nest--;
454                 else if (c == '"') {
455                         while ((c = input ()) != EOF && c != '"') {
456                                 if (c == '\\')
457                                         c = input ();
458                         }
459                 } else if (c == '\'') {
460                         c = input ();
461                         if (c == '\\')
462                                 c = input ();
463                         else if (c == '\'')
464                                 return FALSE;
465                         c = input ();
466                         if (c != '\'')
467                                 return FALSE;
468                 } else if (c == '\n')
469                         lineno++;
470         }
471
472         return TRUE;
473 }