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_linemarks (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); }
75 "/*"[\t ]*<[\t ]*"private"[\t ]*>" */" { scanner->private = TRUE; }
76 "/*"[\t ]*<[\t ]*"public"[\t ]*>" */" { scanner->private = FALSE; }
79 "#define "[a-zA-Z_][a-zA-Z_0-9]*"(" { yyless (yyleng - 1); return FUNCTION_MACRO; }
80 "#define "[a-zA-Z_][a-zA-Z_0-9]* { return OBJECT_MACRO; }
81 "#pragma ".*"\n" { /* Ignore pragma. */ }
83 "# "[0-9]+" ".*"\n" { process_linemarks(scanner); }
97 "..." { return ELLIPSIS; }
113 "+=" { return ADDEQ; }
114 "-=" { return SUBEQ; }
115 "*=" { return MULEQ; }
116 "/=" { return DIVEQ; }
117 "%=" { return MODEQ; }
118 "^=" { return XOREQ; }
119 "&=" { return ANDEQ; }
120 "|=" { return OREQ; }
123 "<<=" { return SLEQ; }
124 ">>=" { return SREQ; }
126 "!=" { return NOTEQ; }
127 "<=" { return LTEQ; }
128 ">=" { return GTEQ; }
129 "&&" { return ANDAND; }
130 "||" { return OROR; }
131 "++" { return PLUSPLUS; }
132 "--" { return MINUSMINUS; }
134 "->" { return ARROW; }
136 "__asm" { if (!parse_ignored_macro()) REJECT; }
137 "__asm__" { if (!parse_ignored_macro()) REJECT; }
138 "__attribute__" { if (!parse_ignored_macro()) REJECT; }
139 "__attribute" { if (!parse_ignored_macro()) REJECT; }
140 "__const" { return CONST; }
141 "__extension__" { return EXTENSION; }
142 "__inline" { return INLINE; }
143 "__nonnull" { if (!parse_ignored_macro()) REJECT; }
144 "__signed__" { return SIGNED; }
145 "__restrict" { return RESTRICT; }
146 "__typeof" { if (!parse_ignored_macro()) REJECT; }
147 "_Bool" { return BOOL; }
149 [a-zA-Z_][a-zA-Z_0-9]* { if (scanner->macro_scan) return IDENTIFIER; else REJECT; }
151 "asm" { if (!parse_ignored_macro()) REJECT; }
152 "auto" { return AUTO; }
153 "break" { return BREAK; }
154 "case" { return CASE; }
155 "char" { return CHAR; }
156 "const" { return CONST; }
157 "continue" { return CONTINUE; }
158 "default" { return DEFAULT; }
160 "double" { return DOUBLE; }
161 "else" { return ELSE; }
162 "enum" { return ENUM; }
163 "extern" { return EXTERN; }
164 "float" { return FLOAT; }
165 "for" { return FOR; }
166 "goto" { return GOTO; }
168 "inline" { return INLINE; }
169 "int" { return INT; }
170 "long" { return LONG; }
171 "register" { return REGISTER; }
172 "restrict" { return RESTRICT; }
173 "return" { return RETURN; }
174 "short" { return SHORT; }
175 "signed" { return SIGNED; }
176 "sizeof" { return SIZEOF; }
177 "static" { return STATIC; }
178 "struct" { return STRUCT; }
179 "switch" { return SWITCH; }
180 "typedef" { return TYPEDEF; }
181 "union" { return UNION; }
182 "unsigned" { return UNSIGNED; }
183 "void" { return VOID; }
184 "volatile" { return VOLATILE; }
185 "while" { return WHILE; }
187 [a-zA-Z_][a-zA-Z_0-9]* { return check_identifier(scanner, yytext); }
189 "0"[xX][0-9a-fA-F]+{intsuffix}? { return INTEGER; }
190 "0"[0-7]+{intsuffix}? { return INTEGER; }
191 [0-9]+{intsuffix}? { return INTEGER; }
193 {fracconst}{exppart}?{floatsuffix}? { return FLOATING; }
194 [0-9]+{exppart}{floatsuffix}? { return FLOATING; }
196 "'"{chartext}*"'" { return CHARACTER; }
197 "L'"{chartext}*"'" { return CHARACTER; }
199 "\""{stringtext}*"\"" { return STRING; }
200 "L\""{stringtext}*"\"" { return STRING; }
202 . { if (yytext[0]) fprintf(stderr, "%s:%d: unexpected character `%c'\n", scanner->current_filename, lineno, yytext[0]); }
214 parse_comment (GISourceScanner *scanner)
218 GISourceComment *comment;
224 string = g_string_new ("");
226 comment_lineno = lineno;
228 while (c2 != EOF && !(c1 == '*' && c2 == '/'))
230 g_string_append_c (string, c1);
240 comment = g_slice_new (GISourceComment);
241 comment->comment = g_string_free (string, FALSE);
242 comment->line = comment_lineno;
243 comment->filename = g_strdup(scanner->current_filename);
245 scanner->comments = g_slist_prepend (scanner->comments,
250 check_identifier (GISourceScanner *scanner,
254 * This function checks if `s' is a type name or an
258 if (gi_source_scanner_is_typedef (scanner, s)) {
260 } else if (strcmp (s, "__builtin_va_list") == 0) {
268 * # linenum "filename" flags
269 * See http://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html
273 process_linemarks (GISourceScanner *scanner)
279 sscanf(yytext, "# %d \"%1024[^\"]\"", &lineno, filename);
281 compress = g_strcompress (filename);
282 real = g_realpath (filename);
284 g_free (scanner->current_filename);
285 scanner->current_filename = real;
293 * This parses a macro which is ignored, such as
294 * __attribute__((x)) or __asm__ (x)
297 parse_ignored_macro (void)
302 while ((c = input ()) != EOF && isspace (c))
308 while ((c = input ()) != EOF && (nest > 0 || c != ')')) {
314 while ((c = input ()) != EOF && c != '"') {
318 } else if (c == '\'') {
327 } else if (c == '\n')