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