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