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"
42 #define YY_BUF_SIZE 1048576
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);
55 intsuffix ([uU][lL]?[lL]?)|([lL][lL]?[uU]?)
56 fracconst ([0-9]*\.[0-9]+)|([0-9]+\.)
57 exppart [eE][-+]?[0-9]+
59 chartext ([^\\\'])|(\\.)
60 stringtext ([^\\\"])|(\\.)
64 "\n" { ++lineno; } /* " */
66 [\t\f\v\r ]+ { /* Ignore whitespace. */ }
68 "/*" { parse_comment(scanner); }
71 "#define "[a-zA-Z_][a-zA-Z_0-9]*"(" { yyless (yyleng - 1); return FUNCTION_MACRO; }
72 "#define "[a-zA-Z_][a-zA-Z_0-9]* { return OBJECT_MACRO; }
74 "#" { process_directive(scanner); }
88 "..." { return ELLIPSIS; }
104 "+=" { return ADDEQ; }
105 "-=" { return SUBEQ; }
106 "*=" { return MULEQ; }
107 "/=" { return DIVEQ; }
108 "%=" { return MODEQ; }
109 "^=" { return XOREQ; }
110 "&=" { return ANDEQ; }
111 "|=" { return OREQ; }
114 "<<=" { return SLEQ; }
115 ">>=" { return SREQ; }
117 "!=" { return NOTEQ; }
118 "<=" { return LTEQ; }
119 ">=" { return GTEQ; }
120 "&&" { return ANDAND; }
121 "||" { return OROR; }
122 "++" { return PLUSPLUS; }
123 "--" { return MINUSMINUS; }
125 "->" { return ARROW; }
127 "__asm" { if (!parse_ignored_macro()) REJECT; }
128 "__asm__" { if (!parse_ignored_macro()) REJECT; }
129 "__attribute__" { if (!parse_ignored_macro()) REJECT; }
130 "__attribute" { if (!parse_ignored_macro()) REJECT; }
131 "__const" { return CONST; }
132 "__extension__" { return EXTENSION; }
133 "__inline" { return INLINE; }
134 "__nonnull" { if (!parse_ignored_macro()) REJECT; }
135 "__signed__" { return SIGNED; }
136 "__restrict" { return RESTRICT; }
137 "__typeof" { if (!parse_ignored_macro()) REJECT; }
138 "_Bool" { return BOOL; }
140 [a-zA-Z_][a-zA-Z_0-9]* { if (scanner->macro_scan) return IDENTIFIER; else REJECT; }
142 "asm" { if (!parse_ignored_macro()) REJECT; }
143 "auto" { return AUTO; }
144 "break" { return BREAK; }
145 "case" { return CASE; }
146 "char" { return CHAR; }
147 "const" { return CONST; }
148 "continue" { return CONTINUE; }
149 "default" { return DEFAULT; }
151 "double" { return DOUBLE; }
152 "else" { return ELSE; }
153 "enum" { return ENUM; }
154 "extern" { return EXTERN; }
155 "float" { return FLOAT; }
156 "for" { return FOR; }
157 "goto" { return GOTO; }
159 "inline" { return INLINE; }
160 "int" { return INT; }
161 "long" { return LONG; }
162 "register" { return REGISTER; }
163 "restrict" { return RESTRICT; }
164 "return" { return RETURN; }
165 "short" { return SHORT; }
166 "signed" { return SIGNED; }
167 "sizeof" { return SIZEOF; }
168 "static" { return STATIC; }
169 "struct" { return STRUCT; }
170 "switch" { return SWITCH; }
171 "typedef" { return TYPEDEF; }
172 "union" { return UNION; }
173 "unsigned" { return UNSIGNED; }
174 "void" { return VOID; }
175 "volatile" { return VOLATILE; }
176 "while" { return WHILE; }
178 [a-zA-Z_][a-zA-Z_0-9]* { return check_identifier(scanner, yytext); }
180 "0"[xX][0-9a-fA-F]+{intsuffix}? { return INTEGER; }
181 "0"[0-7]+{intsuffix}? { return INTEGER; }
182 [0-9]+{intsuffix}? { return INTEGER; }
184 {fracconst}{exppart}?{floatsuffix}? { return FLOATING; }
185 [0-9]+{exppart}{floatsuffix}? { return FLOATING; }
187 "'"{chartext}*"'" { return CHARACTER; }
188 "L'"{chartext}*"'" { return CHARACTER; }
190 "\""{stringtext}*"\"" { return STRING; }
191 "L\""{stringtext}*"\"" { return STRING; }
193 . { if (yytext[0]) fprintf(stderr, "%s:%d: unexpected character `%c'\n", scanner->current_filename, lineno, yytext[0]); }
205 parse_comment (GISourceScanner *scanner)
213 comment = g_string_new ("");
215 while (c2 != EOF && !(c1 == '*' && c2 == '/'))
217 g_string_append_c (comment, c1);
224 scanner->comments = g_slist_prepend (scanner->comments,
225 g_string_free (comment, FALSE));
229 check_identifier (GISourceScanner *scanner,
233 * This function checks if `s' is a type name or an
237 if (gi_source_scanner_is_typedef (scanner, s)) {
239 } else if (strcmp (s, "__builtin_va_list") == 0) {
247 process_directive (GISourceScanner *scanner)
249 /* extract current filename from #line directives */
250 GString *filename_builder;
251 gboolean in_string, found_filename;
254 found_filename = FALSE;
256 filename_builder = g_string_new ("");
259 while (c != EOF && c != '\n') {
263 found_filename = TRUE;
264 } else if (c >= '0' && c <= '9') {
265 if (!found_filename) {
266 lineno = lineno * 10 + (c - '0');
272 } else if (c == '\\') {
273 g_string_append_c (filename_builder, c);
275 g_string_append_c (filename_builder, c);
277 g_string_append_c (filename_builder, c);
283 if (filename_builder->len > 0) {
284 char *filename = g_strcompress (filename_builder->str);
285 if (g_realpath (filename))
287 g_free (scanner->current_filename);
288 scanner->current_filename = g_realpath (filename);
289 g_assert (scanner->current_filename);
294 g_string_free (filename_builder, TRUE);
298 * This parses a macro which is ignored, such as
299 * __attribute__((x)) or __asm__ (x)
302 parse_ignored_macro (void)
307 while ((c = input ()) != EOF && isspace (c))
313 while ((c = input ()) != EOF && (nest > 0 || c != ')')) {
319 while ((c = input ()) != EOF && c != '"') {
323 } else if (c == '\'') {
332 } else if (c == '\n')