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