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_ignored_macro (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 "__asm" { if (!parse_ignored_macro()) REJECT; }
122 "__asm__" { if (!parse_ignored_macro()) REJECT; }
123 "__attribute__" { if (!parse_ignored_macro()) REJECT; }
124 "__attribute" { if (!parse_ignored_macro()) REJECT; }
125 "__const" { return CONST; }
126 "__extension__" { return EXTENSION; }
127 "__inline" { return INLINE; }
128 "__nonnull" { if (!parse_ignored_macro()) REJECT; }
129 "__signed__" { return SIGNED; }
130 "__restrict" { return RESTRICT; }
131 "__typeof" { if (!parse_ignored_macro()) REJECT; }
132 "_Bool" { return BOOL; }
134 [a-zA-Z_][a-zA-Z_0-9]* { if (scanner->macro_scan) return IDENTIFIER; else REJECT; }
136 "asm" { if (!parse_ignored_macro()) REJECT; }
137 "auto" { return AUTO; }
138 "break" { return BREAK; }
139 "case" { return CASE; }
140 "char" { return CHAR; }
141 "const" { return CONST; }
142 "continue" { return CONTINUE; }
143 "default" { return DEFAULT; }
145 "double" { return DOUBLE; }
146 "else" { return ELSE; }
147 "enum" { return ENUM; }
148 "extern" { return EXTERN; }
149 "float" { return FLOAT; }
150 "for" { return FOR; }
151 "goto" { return GOTO; }
153 "inline" { return INLINE; }
154 "int" { return INT; }
155 "long" { return LONG; }
156 "register" { return REGISTER; }
157 "restrict" { return RESTRICT; }
158 "return" { return RETURN; }
159 "short" { return SHORT; }
160 "signed" { return SIGNED; }
161 "sizeof" { return SIZEOF; }
162 "static" { return STATIC; }
163 "struct" { return STRUCT; }
164 "switch" { return SWITCH; }
165 "typedef" { return TYPEDEF; }
166 "union" { return UNION; }
167 "unsigned" { return UNSIGNED; }
168 "void" { return VOID; }
169 "volatile" { return VOLATILE; }
170 "while" { return WHILE; }
172 [a-zA-Z_][a-zA-Z_0-9]* { return check_identifier(scanner, yytext); }
174 "0"[xX][0-9a-fA-F]+{intsuffix}? { return INTEGER; }
175 "0"[0-7]+{intsuffix}? { return INTEGER; }
176 [0-9]+{intsuffix}? { return INTEGER; }
178 {fracconst}{exppart}?{floatsuffix}? { return FLOATING; }
179 [0-9]+{exppart}{floatsuffix}? { return FLOATING; }
181 "'"{chartext}*"'" { return CHARACTER; }
182 "L'"{chartext}*"'" { return CHARACTER; }
184 "\""{stringtext}*"\"" { return STRING; }
185 "L\""{stringtext}*"\"" { return STRING; }
187 . { fprintf(stderr, "%s:%d: unexpected character `%c'\n", scanner->current_filename, lineno, yytext[0]); }
199 parse_gtkdoc (GISourceScanner *scanner,
204 gboolean isline = FALSE;
208 GISourceDirective *directive;
211 GSList *options = NULL;
228 } while (*c2 != EOF && !(*c1 == '*' && *c2 == '/'));
233 /* Ignore lines that don't have a : - this is a hack but avoids
234 * trying to parse too many things as annotations
236 if (!strchr (line, ':'))
241 parts = g_strsplit (line, ":", 3);
242 n_parts = g_strv_length (parts);
244 if (g_ascii_strcasecmp (parts[0], "eprecated") == 0)
247 options = g_slist_prepend (options, g_strdup_printf ("%s: %s", parts[1], parts[2]));
248 else if (n_parts == 2)
249 options = g_slist_prepend (options, g_strdup (parts[1]));
251 options = g_slist_prepend (options, g_strdup (""));
255 else if (n_parts >= 2)
261 char *ptr = g_strdup (parts[1]);
266 start = strchr (ptr, '(');
267 while (start != NULL)
269 end = strchr (start, ')');
272 options = g_slist_prepend (options, g_strndup (start+1, end-(start+1)));
273 start = strchr (end+1, '(');
286 else /* parts == 1 */
293 * Special cases for global annotations.
294 * Context-sensitive parsing would probably be the right way to go.
296 if (g_ascii_strncasecmp ("eturn", name, 5) == 0)
298 else if (g_ascii_strncasecmp ("eprecated", name, 9) == 0)
299 rname = "deprecated";
303 directive = gi_source_directive_new (rname, value, options);
304 directives = g_hash_table_lookup (scanner->directives_map, symbol);
305 directives = g_slist_prepend (directives, directive);
306 g_hash_table_replace (scanner->directives_map,
307 g_strdup (symbol), directives);
314 parse_comment (GISourceScanner *scanner)
316 GString *symbol = NULL;
317 gboolean startofline = FALSE, have_symbol = FALSE, start1 = FALSE, start_symbol = FALSE;
323 while (c2 != EOF && !(c1 == '*' && c2 == '/'))
329 else if (c1 == '*' && start1)
331 else if (!have_symbol && start_symbol)
334 symbol = g_string_new ("");
336 g_string_append_c (symbol, c1);
348 if ((c1 != '*' && c1 != ' '))
351 if (startofline && (c1 == ' ') && (c2 == '@' || (c2 == 'r') || (c2 == 'R') || (c2 == 'D')))
356 parse_gtkdoc (scanner, symbol->str, &c1, &c2);
361 g_string_free (symbol, TRUE);
366 check_identifier (GISourceScanner *scanner,
370 * This function checks if `s' is a type name or an
374 if (gi_source_scanner_is_typedef (scanner, s)) {
376 } else if (strcmp (s, "__builtin_va_list") == 0) {
384 process_directive (GISourceScanner *scanner)
386 /* extract current filename from #line directives */
387 GString *filename_builder;
388 gboolean in_string, found_filename;
391 found_filename = FALSE;
393 filename_builder = g_string_new ("");
396 while (c != EOF && c != '\n') {
400 found_filename = TRUE;
401 } else if (c >= '0' && c <= '9') {
402 if (!found_filename) {
403 lineno = lineno * 10 + (c - '0');
409 } else if (c == '\\') {
410 g_string_append_c (filename_builder, c);
412 g_string_append_c (filename_builder, c);
414 g_string_append_c (filename_builder, c);
420 if (filename_builder->len > 0) {
421 char *filename = g_strcompress (filename_builder->str);
422 if (g_realpath (filename))
424 g_free (scanner->current_filename);
425 scanner->current_filename = g_realpath (filename);
426 g_assert (scanner->current_filename);
431 g_string_free (filename_builder, TRUE);
435 * This parses a macro which is ignored, such as
436 * __attribute__((x)) or __asm__ (x)
439 parse_ignored_macro (void)
444 while ((c = input ()) != EOF && isspace (c))
450 while ((c = input ()) != EOF && (nest > 0 || c != ')')) {
456 while ((c = input ()) != EOF && c != '"') {
460 } else if (c == '\'') {
469 } else if (c == '\n')