53603e25b4680e457bec45262ee3ae852663e1b2
[gnome.gobject-introspection] / 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_comment (GISourceScanner *scanner)
204 {
205   GString *comment;
206   int c1, c2;
207
208   c1 = input();
209   c2 = input();
210
211   comment = g_string_new ("");
212
213   while (c2 != EOF && !(c1 == '*' && c2 == '/'))
214     {
215       g_string_append_c (comment, c1);
216
217       c1 = c2;
218       c2 = input();
219
220     }
221
222   scanner->comments = g_slist_prepend (scanner->comments,
223                                        g_string_free (comment, FALSE));
224 }
225
226 static int
227 check_identifier (GISourceScanner *scanner,
228                   const char  *s)
229 {
230         /*
231          * This function checks if `s' is a type name or an
232          * identifier.
233          */
234
235         if (gi_source_scanner_is_typedef (scanner, s)) {
236                 return TYPEDEF_NAME;
237         } else if (strcmp (s, "__builtin_va_list") == 0) {
238                 return TYPEDEF_NAME;
239         }
240
241         return IDENTIFIER;
242 }
243
244 static void
245 process_directive (GISourceScanner *scanner)
246 {
247         /* extract current filename from #line directives */
248         GString *filename_builder;
249         gboolean in_string, found_filename;
250
251         lineno = 0;
252         found_filename = FALSE;
253         in_string = FALSE;
254         filename_builder = g_string_new ("");
255
256         int c = input ();
257         while (c != EOF && c != '\n') {
258                 if (!in_string) {
259                         if (c == '\"') {
260                                 in_string = TRUE;
261                                 found_filename = TRUE;
262                         } else if (c >= '0' && c <= '9') {
263                                 if (!found_filename) {
264                                         lineno = lineno * 10 + (c - '0');
265                                 }
266                         }
267                 } else {
268                         if (c == '\"') {
269                                 in_string = FALSE;
270                         } else if (c == '\\') {
271                                 g_string_append_c (filename_builder, c);
272                                 c = input ();
273                                 g_string_append_c (filename_builder, c);
274                         } else {
275                                 g_string_append_c (filename_builder, c);
276                         }
277                 }
278                 c = input ();
279         }
280
281         if (filename_builder->len > 0) {
282                 char *filename = g_strcompress (filename_builder->str);
283                 if (g_realpath (filename))
284                   {
285                     g_free (scanner->current_filename);
286                     scanner->current_filename = g_realpath (filename);
287                     g_assert (scanner->current_filename);
288                     g_free(filename);
289                   }
290         }
291
292         g_string_free (filename_builder, TRUE);
293 }
294
295 /*
296  * This parses a macro which is ignored, such as
297  * __attribute__((x)) or __asm__ (x)
298  */
299 static int
300 parse_ignored_macro (void)
301 {
302         int c;
303         int nest;
304
305         while ((c = input ()) != EOF && isspace (c))
306                 ;
307         if (c != '(')
308                 return FALSE;
309
310         nest = 0;
311         while ((c = input ()) != EOF && (nest > 0 || c != ')')) {
312                 if (c == '(')
313                         nest++;
314                 else if (c == ')')
315                         nest--;
316                 else if (c == '"') {
317                         while ((c = input ()) != EOF && c != '"') {
318                                 if (c == '\\')
319                                         c = input ();
320                         }
321                 } else if (c == '\'') {
322                         c = input ();
323                         if (c == '\\')
324                                 c = input ();
325                         else if (c == '\'')
326                                 return FALSE;
327                         c = input ();
328                         if (c != '\'')
329                                 return FALSE;
330                 } else if (c == '\n')
331                         lineno++;
332         }
333
334         return TRUE;
335 }