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);
53 intsuffix ([uU][lL]?[lL]?)|([lL][lL]?[uU]?)
54 fracconst ([0-9]*\.[0-9]+)|([0-9]+\.)
55 exppart [eE][-+]?[0-9]+
57 chartext ([^\\\'])|(\\.)
58 stringtext ([^\\\"])|(\\.)
62 "\n" { ++lineno; } /* " */
64 [\t\f\v\r ]+ { /* Ignore whitespace. */ }
66 "/*" { parse_comment(scanner); }
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; }
72 "#" { process_directive(scanner); }
86 "..." { return ELLIPSIS; }
102 "+=" { return ADDEQ; }
103 "-=" { return SUBEQ; }
104 "*=" { return MULEQ; }
105 "/=" { return DIVEQ; }
106 "%=" { return MODEQ; }
107 "^=" { return XOREQ; }
108 "&=" { return ANDEQ; }
109 "|=" { return OREQ; }
112 "<<=" { return SLEQ; }
113 ">>=" { return SREQ; }
115 "!=" { return NOTEQ; }
116 "<=" { return LTEQ; }
117 ">=" { return GTEQ; }
118 "&&" { return ANDAND; }
119 "||" { return OROR; }
120 "++" { return PLUSPLUS; }
121 "--" { return MINUSMINUS; }
123 "->" { return ARROW; }
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; }
138 [a-zA-Z_][a-zA-Z_0-9]* { if (scanner->macro_scan) return IDENTIFIER; else REJECT; }
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; }
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; }
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; }
176 [a-zA-Z_][a-zA-Z_0-9]* { return check_identifier(scanner, yytext); }
178 "0"[xX][0-9a-fA-F]+{intsuffix}? { return INTEGER; }
179 "0"[0-7]+{intsuffix}? { return INTEGER; }
180 [0-9]+{intsuffix}? { return INTEGER; }
182 {fracconst}{exppart}?{floatsuffix}? { return FLOATING; }
183 [0-9]+{exppart}{floatsuffix}? { return FLOATING; }
185 "'"{chartext}*"'" { return CHARACTER; }
186 "L'"{chartext}*"'" { return CHARACTER; }
188 "\""{stringtext}*"\"" { return STRING; }
189 "L\""{stringtext}*"\"" { return STRING; }
191 . { fprintf(stderr, "%s:%d: unexpected character `%c'\n", scanner->current_filename, lineno, yytext[0]); }
203 parse_comment (GISourceScanner *scanner)
211 comment = g_string_new ("");
213 while (c2 != EOF && !(c1 == '*' && c2 == '/'))
215 g_string_append_c (comment, c1);
222 scanner->comments = g_slist_prepend (scanner->comments,
223 g_string_free (comment, FALSE));
227 check_identifier (GISourceScanner *scanner,
231 * This function checks if `s' is a type name or an
235 if (gi_source_scanner_is_typedef (scanner, s)) {
237 } else if (strcmp (s, "__builtin_va_list") == 0) {
245 process_directive (GISourceScanner *scanner)
247 /* extract current filename from #line directives */
248 GString *filename_builder;
249 gboolean in_string, found_filename;
252 found_filename = FALSE;
254 filename_builder = g_string_new ("");
257 while (c != EOF && c != '\n') {
261 found_filename = TRUE;
262 } else if (c >= '0' && c <= '9') {
263 if (!found_filename) {
264 lineno = lineno * 10 + (c - '0');
270 } else if (c == '\\') {
271 g_string_append_c (filename_builder, c);
273 g_string_append_c (filename_builder, c);
275 g_string_append_c (filename_builder, c);
281 if (filename_builder->len > 0) {
282 char *filename = g_strcompress (filename_builder->str);
283 if (g_realpath (filename))
285 g_free (scanner->current_filename);
286 scanner->current_filename = g_realpath (filename);
287 g_assert (scanner->current_filename);
292 g_string_free (filename_builder, TRUE);
296 * This parses a macro which is ignored, such as
297 * __attribute__((x)) or __asm__ (x)
300 parse_ignored_macro (void)
305 while ((c = input ()) != EOF && isspace (c))
311 while ((c = input ()) != EOF && (nest > 0 || c != ')')) {
317 while ((c = input ()) != EOF && c != '"') {
321 } else if (c == '\'') {
330 } else if (c == '\n')