2 * GObject introspection: C lexer
4 * Copyright (c) 1997 Sandro Sigala <ssigala@globalnet.it>
5 * Copyright (c) 2007-2008 Jürg Billeter <j@bitron.ch>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
35 #include "sourcescanner.h"
36 #include "scannerparser.h"
37 #include "grealpath.h"
43 #define YY_BUF_SIZE 1048576
45 extern int yylex (GISourceScanner *scanner);
46 #define YY_DECL int yylex (GISourceScanner *scanner)
47 static int yywrap (void);
48 static void parse_comment (GISourceScanner *scanner);
49 static void process_directive (GISourceScanner *scanner);
50 static int check_identifier (GISourceScanner *scanner, const char *);
51 static int parse_ignored_macro (void);
56 intsuffix ([uU][lL]?[lL]?)|([lL][lL]?[uU]?)
57 fracconst ([0-9]*\.[0-9]+)|([0-9]+\.)
58 exppart [eE][-+]?[0-9]+
60 chartext ([^\\\'])|(\\.)
61 stringtext ([^\\\"])|(\\.)
65 \n.* { strncpy(linebuf, yytext+1, sizeof(linebuf)); /* save the next line */
66 linebuf[sizeof(linebuf)-1]='\0';
67 /* printf("%4d:%s\n",lineno,linebuf); */
68 yyless(1); /* give back all but the \n to rescan */
72 [\t\f\v\r ]+ { /* Ignore whitespace. */ }
74 "/*" { parse_comment(scanner); }
77 "#define "[a-zA-Z_][a-zA-Z_0-9]*"(" { yyless (yyleng - 1); return FUNCTION_MACRO; }
78 "#define "[a-zA-Z_][a-zA-Z_0-9]* { return OBJECT_MACRO; }
80 "#" { process_directive(scanner); }
94 "..." { return ELLIPSIS; }
110 "+=" { return ADDEQ; }
111 "-=" { return SUBEQ; }
112 "*=" { return MULEQ; }
113 "/=" { return DIVEQ; }
114 "%=" { return MODEQ; }
115 "^=" { return XOREQ; }
116 "&=" { return ANDEQ; }
117 "|=" { return OREQ; }
120 "<<=" { return SLEQ; }
121 ">>=" { return SREQ; }
123 "!=" { return NOTEQ; }
124 "<=" { return LTEQ; }
125 ">=" { return GTEQ; }
126 "&&" { return ANDAND; }
127 "||" { return OROR; }
128 "++" { return PLUSPLUS; }
129 "--" { return MINUSMINUS; }
131 "->" { return ARROW; }
133 "__asm" { if (!parse_ignored_macro()) REJECT; }
134 "__asm__" { if (!parse_ignored_macro()) REJECT; }
135 "__attribute__" { if (!parse_ignored_macro()) REJECT; }
136 "__attribute" { if (!parse_ignored_macro()) REJECT; }
137 "__const" { return CONST; }
138 "__extension__" { return EXTENSION; }
139 "__inline" { return INLINE; }
140 "__nonnull" { if (!parse_ignored_macro()) REJECT; }
141 "__signed__" { return SIGNED; }
142 "__restrict" { return RESTRICT; }
143 "__typeof" { if (!parse_ignored_macro()) REJECT; }
144 "_Bool" { return BOOL; }
146 [a-zA-Z_][a-zA-Z_0-9]* { if (scanner->macro_scan) return IDENTIFIER; else REJECT; }
148 "asm" { if (!parse_ignored_macro()) REJECT; }
149 "auto" { return AUTO; }
150 "break" { return BREAK; }
151 "case" { return CASE; }
152 "char" { return CHAR; }
153 "const" { return CONST; }
154 "continue" { return CONTINUE; }
155 "default" { return DEFAULT; }
157 "double" { return DOUBLE; }
158 "else" { return ELSE; }
159 "enum" { return ENUM; }
160 "extern" { return EXTERN; }
161 "float" { return FLOAT; }
162 "for" { return FOR; }
163 "goto" { return GOTO; }
165 "inline" { return INLINE; }
166 "int" { return INT; }
167 "long" { return LONG; }
168 "register" { return REGISTER; }
169 "restrict" { return RESTRICT; }
170 "return" { return RETURN; }
171 "short" { return SHORT; }
172 "signed" { return SIGNED; }
173 "sizeof" { return SIZEOF; }
174 "static" { return STATIC; }
175 "struct" { return STRUCT; }
176 "switch" { return SWITCH; }
177 "typedef" { return TYPEDEF; }
178 "union" { return UNION; }
179 "unsigned" { return UNSIGNED; }
180 "void" { return VOID; }
181 "volatile" { return VOLATILE; }
182 "while" { return WHILE; }
184 [a-zA-Z_][a-zA-Z_0-9]* { return check_identifier(scanner, yytext); }
186 "0"[xX][0-9a-fA-F]+{intsuffix}? { return INTEGER; }
187 "0"[0-7]+{intsuffix}? { return INTEGER; }
188 [0-9]+{intsuffix}? { return INTEGER; }
190 {fracconst}{exppart}?{floatsuffix}? { return FLOATING; }
191 [0-9]+{exppart}{floatsuffix}? { return FLOATING; }
193 "'"{chartext}*"'" { return CHARACTER; }
194 "L'"{chartext}*"'" { return CHARACTER; }
196 "\""{stringtext}*"\"" { return STRING; }
197 "L\""{stringtext}*"\"" { return STRING; }
199 . { if (yytext[0]) fprintf(stderr, "%s:%d: unexpected character `%c'\n", scanner->current_filename, lineno, yytext[0]); }
211 parse_comment (GISourceScanner *scanner)
219 comment = g_string_new ("");
221 while (c2 != EOF && !(c1 == '*' && c2 == '/'))
223 g_string_append_c (comment, c1);
233 scanner->comments = g_slist_prepend (scanner->comments,
234 g_string_free (comment, FALSE));
238 check_identifier (GISourceScanner *scanner,
242 * This function checks if `s' is a type name or an
246 if (gi_source_scanner_is_typedef (scanner, s)) {
248 } else if (strcmp (s, "__builtin_va_list") == 0) {
256 process_directive (GISourceScanner *scanner)
258 /* extract current filename from #line directives */
259 GString *filename_builder;
260 gboolean in_string, found_filename;
263 found_filename = FALSE;
265 filename_builder = g_string_new ("");
268 while (c != EOF && c != '\n') {
272 found_filename = TRUE;
273 } else if (c >= '0' && c <= '9') {
274 if (!found_filename) {
275 lineno = lineno * 10 + (c - '0');
281 } else if (c == '\\') {
282 g_string_append_c (filename_builder, c);
284 g_string_append_c (filename_builder, c);
286 g_string_append_c (filename_builder, c);
292 if (filename_builder->len > 0) {
293 char *filename = g_strcompress (filename_builder->str);
294 if (g_realpath (filename))
296 g_free (scanner->current_filename);
297 scanner->current_filename = g_realpath (filename);
298 g_assert (scanner->current_filename);
303 g_string_free (filename_builder, TRUE);
307 * This parses a macro which is ignored, such as
308 * __attribute__((x)) or __asm__ (x)
311 parse_ignored_macro (void)
316 while ((c = input ()) != EOF && isspace (c))
322 while ((c = input ()) != EOF && (nest > 0 || c != ')')) {
328 while ((c = input ()) != EOF && c != '"') {
332 } else if (c == '\'') {
341 } else if (c == '\n')