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 "__attribute__" { if (!parse_ignored_macro()) REJECT; }
122 "__const" { return CONST; }
123 "__extension__" { return EXTENSION; }
124 "__inline" { return INLINE; }
125 "__nonnull" { if (!parse_ignored_macro()) REJECT; }
126 "__restrict" { return RESTRICT; }
128 [a-zA-Z_][a-zA-Z_0-9]* { if (scanner->macro_scan) return IDENTIFIER; else REJECT; }
130 "asm" { if (!parse_ignored_macro()) REJECT; }
131 "__asm__" { if (!parse_ignored_macro()) REJECT; }
132 "auto" { return AUTO; }
133 "_Bool" { return BOOL; }
134 "break" { return BREAK; }
135 "case" { return CASE; }
136 "char" { return CHAR; }
137 "const" { return CONST; }
138 "continue" { return CONTINUE; }
139 "default" { return DEFAULT; }
141 "double" { return DOUBLE; }
142 "else" { return ELSE; }
143 "enum" { return ENUM; }
144 "extern" { return EXTERN; }
145 "float" { return FLOAT; }
146 "for" { return FOR; }
147 "goto" { return GOTO; }
149 "inline" { return INLINE; }
150 "__inline__" { return INLINE; }
151 "int" { return INT; }
152 "long" { return LONG; }
153 "register" { return REGISTER; }
154 "restrict" { return RESTRICT; }
155 "return" { return RETURN; }
156 "short" { return SHORT; }
157 "signed" { return SIGNED; }
158 "sizeof" { return SIZEOF; }
159 "static" { return STATIC; }
160 "struct" { return STRUCT; }
161 "switch" { return SWITCH; }
162 "typedef" { return TYPEDEF; }
163 "union" { return UNION; }
164 "unsigned" { return UNSIGNED; }
165 "void" { return VOID; }
166 "volatile" { return VOLATILE; }
167 "while" { return WHILE; }
169 [a-zA-Z_][a-zA-Z_0-9]* { return check_identifier(scanner, yytext); }
171 "0"[xX][0-9a-fA-F]+{intsuffix}? { return INTEGER; }
172 "0"[0-7]+{intsuffix}? { return INTEGER; }
173 [0-9]+{intsuffix}? { return INTEGER; }
175 {fracconst}{exppart}?{floatsuffix}? { return FLOATING; }
176 [0-9]+{exppart}{floatsuffix}? { return FLOATING; }
178 "'"{chartext}*"'" { return CHARACTER; }
179 "L'"{chartext}*"'" { return CHARACTER; }
181 "\""{stringtext}*"\"" { return STRING; }
182 "L\""{stringtext}*"\"" { return STRING; }
184 . { fprintf(stderr, "%s:%d: unexpected character `%c'\n", scanner->current_filename, lineno, yytext[0]); }
196 parse_gtkdoc (GISourceScanner *scanner,
201 gboolean isline = FALSE;
205 GISourceDirective *directive;
208 GSList *options = NULL;
225 } while (*c2 != EOF && !(*c1 == '*' && *c2 == '/'));
232 parts = g_strsplit (line, ": ", 3);
233 n_parts = g_strv_length (parts);
235 if (g_ascii_strcasecmp (parts[0], "eprecated") == 0)
238 options = g_slist_prepend (options, g_strdup_printf ("%s: %s", parts[1], parts[2]));
239 else if (n_parts == 2)
240 options = g_slist_prepend (options, g_strdup (parts[1]));
242 options = g_slist_prepend (options, g_strdup (""));
246 else if (n_parts >= 2)
252 char *ptr = parts[1];
253 GString *current = NULL;
256 current = g_string_new ("");
267 else if (*ptr == '>')
272 options = g_slist_prepend (options, current->str);
275 g_string_append_c (current, *ptr);
279 g_string_free (current, FALSE);
284 else /* parts == 1 */
291 * Special cases for global annotations.
292 * Context-sensitive parsing would probably be the right way to go.
294 if (g_ascii_strncasecmp ("eturn", name, 5) == 0)
296 else if (g_ascii_strncasecmp ("eprecated", name, 9) == 0)
297 rname = "deprecated";
301 directive = gi_source_directive_new (rname, value, options);
302 directives = g_hash_table_lookup (scanner->directives_map, symbol);
303 directives = g_slist_prepend (directives, directive);
304 g_hash_table_replace (scanner->directives_map,
305 g_strdup (symbol), directives);
313 parse_comment (GISourceScanner *scanner)
315 GString *symbol = NULL;
316 gboolean startofline = FALSE, have_symbol = FALSE, start1 = FALSE, start_symbol = FALSE;
322 while (c2 != EOF && !(c1 == '*' && c2 == '/'))
328 else if (c1 == '*' && start1)
330 else if (!have_symbol && start_symbol)
333 symbol = g_string_new ("");
335 g_string_append_c (symbol, c1);
347 if ((c1 != '*' && c1 != ' '))
350 if (startofline && (c1 == ' ') && (c2 == '@' || (c2 == 'r') || (c2 == 'R') || (c2 == 'D')))
355 parse_gtkdoc (scanner, symbol->str, &c1, &c2);
360 g_string_free (symbol, TRUE);
365 check_identifier (GISourceScanner *scanner,
369 * This function checks if `s' is a type name or an
373 if (gi_source_scanner_is_typedef (scanner, s)) {
375 } else if (strcmp (s, "__builtin_va_list") == 0) {
383 process_directive (GISourceScanner *scanner)
385 /* extract current filename from #line directives */
386 GString *filename_builder;
387 gboolean in_string, found_filename;
390 found_filename = FALSE;
392 filename_builder = g_string_new ("");
395 while (c != EOF && c != '\n') {
399 found_filename = TRUE;
400 } else if (c >= '0' && c <= '9') {
401 if (!found_filename) {
402 lineno = lineno * 10 + (c - '0');
408 } else if (c == '\\') {
409 g_string_append_c (filename_builder, c);
411 g_string_append_c (filename_builder, c);
413 g_string_append_c (filename_builder, c);
419 if (filename_builder->len > 0) {
420 char *filename = g_strcompress (filename_builder->str);
421 if (g_realpath (filename))
423 g_free (scanner->current_filename);
424 scanner->current_filename = g_realpath (filename);
425 g_assert (scanner->current_filename);
430 g_string_free (filename_builder, TRUE);
434 * This parses a macro which is ignored, such as
435 * __attribute__((x)) or __asm__ (x)
438 parse_ignored_macro (void)
443 while ((c = input ()) != EOF && isspace (c))
449 while ((c = input ()) != EOF && (nest > 0 || c != ')')) {
455 while ((c = input ()) != EOF && c != '"') {
459 } else if (c == '\'') {
468 } else if (c == '\n')