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"
41 extern int yylex (GISourceScanner *scanner);
42 #define YY_DECL int yylex (GISourceScanner *scanner)
43 static int yywrap (void);
44 static void parse_comment (GISourceScanner *scanner);
45 static void process_directive (GISourceScanner *scanner);
46 static int check_identifier (GISourceScanner *scanner, const char *);
47 static int parse_attribute (void);
50 intsuffix ([uU][lL]?[lL]?)|([lL][lL]?[uU]?)
51 fracconst ([0-9]*\.[0-9]+)|([0-9]+\.)
52 exppart [eE][-+]?[0-9]+
54 chartext ([^\'])|(\\.)
55 stringtext ([^\"])|(\\.)
59 "\n" { ++lineno; } /* " */
60 [\t\f\v\r ]+ { /* Ignore whitespace. */ }
62 "/*" { parse_comment(scanner); }
65 "#define "[a-zA-Z_][a-zA-Z_0-9]*"(" { yyless (yyleng - 1); return FUNCTION_MACRO; }
66 "#define "[a-zA-Z_][a-zA-Z_0-9]* { return OBJECT_MACRO; }
68 "#" { process_directive(scanner); }
82 "..." { return ELLIPSIS; }
98 "+=" { return ADDEQ; }
99 "-=" { return SUBEQ; }
100 "*=" { return MULEQ; }
101 "/=" { return DIVEQ; }
102 "%=" { return MODEQ; }
103 "^=" { return XOREQ; }
104 "&=" { return ANDEQ; }
105 "|=" { return OREQ; }
108 "<<=" { return SLEQ; }
109 ">>=" { return SREQ; }
111 "!=" { return NOTEQ; }
112 "<=" { return LTEQ; }
113 ">=" { return GTEQ; }
114 "&&" { return ANDAND; }
115 "||" { return OROR; }
116 "++" { return PLUSPLUS; }
117 "--" { return MINUSMINUS; }
119 "->" { return ARROW; }
121 "__attribute__" { if (!parse_attribute()) REJECT; }
123 [a-zA-Z_][a-zA-Z_0-9]* { if (scanner->macro_scan) return IDENTIFIER; else REJECT; }
125 "auto" { return AUTO; }
126 "_Bool" { return BOOL; }
127 "break" { return BREAK; }
128 "case" { return CASE; }
129 "char" { return CHAR; }
130 "const" { return CONST; }
131 "continue" { return CONTINUE; }
132 "default" { return DEFAULT; }
134 "double" { return DOUBLE; }
135 "else" { return ELSE; }
136 "enum" { return ENUM; }
137 "extern" { return EXTERN; }
138 "float" { return FLOAT; }
139 "for" { return FOR; }
140 "goto" { return GOTO; }
142 "inline" { return INLINE; }
143 "__inline__" { return INLINE; }
144 "int" { return INT; }
145 "long" { return LONG; }
146 "register" { return REGISTER; }
147 "restrict" { return RESTRICT; }
148 "return" { return RETURN; }
149 "short" { return SHORT; }
150 "signed" { return SIGNED; }
151 "sizeof" { return SIZEOF; }
152 "static" { return STATIC; }
153 "struct" { return STRUCT; }
154 "switch" { return SWITCH; }
155 "typedef" { return TYPEDEF; }
156 "union" { return UNION; }
157 "unsigned" { return UNSIGNED; }
158 "void" { return VOID; }
159 "volatile" { return VOLATILE; }
160 "while" { return WHILE; }
162 [a-zA-Z_][a-zA-Z_0-9]* { return check_identifier(scanner, yytext); }
164 "0"[xX][0-9a-fA-F]+{intsuffix}? { return INTEGER; }
165 "0"[0-7]+{intsuffix}? { return INTEGER; }
166 [0-9]+{intsuffix}? { return INTEGER; }
168 {fracconst}{exppart}?{floatsuffix}? { return FLOATING; }
169 [0-9]+{exppart}{floatsuffix}? { return FLOATING; }
171 "'"{chartext}*"'" { return CHARACTER; }
172 "L'"{chartext}*"'" { return CHARACTER; }
174 "\""{stringtext}*"\"" { return STRING; }
175 "L\""{stringtext}*"\"" { return STRING; }
177 . { fprintf(stderr, "%s:%d: unexpected character `%c'\n", scanner->current_filename, lineno, yytext[0]); }
189 parse_gtkdoc (GISourceScanner *scanner,
194 gboolean isline = FALSE;
198 GISourceDirective *directive;
201 GSList *options = NULL;
217 } while (*c2 != EOF && !(*c1 == '*' && *c2 == '/'));
224 parts = g_strsplit (line, ": ", 3);
226 if (g_strv_length (parts) >= 2)
230 if (g_strv_length (parts) == 3)
232 char *ptr = parts[1];
233 GString *current = NULL;
236 current = g_string_new ("");
247 else if (*ptr == ')')
252 options = g_slist_prepend (options, current->str);
255 g_string_append_c (current, *ptr);
259 g_string_free (current, FALSE);
264 else /* parts == 1 */
271 * This is a special case for return values, name will only be
272 * 'eturn' or a valid name, check the call site.
273 * Context-sensitive parsing would probably be the right way to go
275 if (g_ascii_strncasecmp ("eturn", name, 5) == 0)
280 directive = gi_source_directive_new (rname, value, options);
281 directives = g_hash_table_lookup (scanner->directives_map, symbol);
282 directives = g_slist_prepend (directives, directive);
283 g_hash_table_replace (scanner->directives_map,
284 g_strdup (symbol), directives);
292 parse_comment (GISourceScanner *scanner)
294 GString *symbol = NULL;
295 gboolean startofline = FALSE, have_symbol = FALSE, start1 = FALSE, start_symbol = FALSE;
301 while (c2 != EOF && !(c1 == '*' && c2 == '/'))
307 else if (c1 == '*' && start1)
309 else if (!have_symbol && start_symbol)
312 symbol = g_string_new ("");
314 g_string_append_c (symbol, c1);
326 if ((c1 != '*' && c1 != ' '))
329 if (startofline && (c1 == ' ') && (c2 == '@' || (c2 == 'r') || (c2 == 'R')))
334 parse_gtkdoc (scanner, symbol->str, &c1, &c2);
339 g_string_free (symbol, TRUE);
344 check_identifier (GISourceScanner *scanner,
348 * This function checks if `s' is a type name or an
352 if (gi_source_scanner_is_typedef (scanner, s)) {
354 } else if (strcmp (s, "__builtin_va_list") == 0) {
362 process_directive (GISourceScanner *scanner)
364 /* extract current filename from #line directives */
365 GString *filename_builder;
366 gboolean in_string, found_filename;
369 found_filename = FALSE;
371 filename_builder = g_string_new ("");
374 while (c != EOF && c != '\n') {
378 found_filename = TRUE;
379 } else if (c >= '0' && c <= '9') {
380 if (!found_filename) {
381 lineno = lineno * 10 + (c - '0');
387 } else if (c == '\\') {
388 g_string_append_c (filename_builder, c);
390 g_string_append_c (filename_builder, c);
392 g_string_append_c (filename_builder, c);
398 if (filename_builder->len > 0) {
399 char *filename = g_strcompress (filename_builder->str);
400 if (g_realpath (filename))
402 g_free (scanner->current_filename);
403 scanner->current_filename = g_realpath (filename);
404 g_assert (scanner->current_filename);
409 g_string_free (filename_builder, TRUE);
413 parse_attribute (void)
418 while ((c = input ()) != EOF && isspace (c))
422 while ((c = input ()) != EOF && isspace (c))
428 while ((c = input ()) != EOF && (nest > 0 || c != ')')) {
434 while ((c = input ()) != EOF && c != '"') {
438 } else if (c == '\'') {
447 } else if (c == '\n')
451 while ((c = input ()) != EOF && isspace (c))