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; } /* " */
61 [\t\f\v\r ]+ { /* Ignore whitespace. */ }
63 "/*" { parse_comment(scanner); }
66 "#define "[a-zA-Z_][a-zA-Z_0-9]*"(" { yyless (yyleng - 1); return FUNCTION_MACRO; }
67 "#define "[a-zA-Z_][a-zA-Z_0-9]* { return OBJECT_MACRO; }
69 "#" { process_directive(scanner); }
83 "..." { return ELLIPSIS; }
99 "+=" { return ADDEQ; }
100 "-=" { return SUBEQ; }
101 "*=" { return MULEQ; }
102 "/=" { return DIVEQ; }
103 "%=" { return MODEQ; }
104 "^=" { return XOREQ; }
105 "&=" { return ANDEQ; }
106 "|=" { return OREQ; }
109 "<<=" { return SLEQ; }
110 ">>=" { return SREQ; }
112 "!=" { return NOTEQ; }
113 "<=" { return LTEQ; }
114 ">=" { return GTEQ; }
115 "&&" { return ANDAND; }
116 "||" { return OROR; }
117 "++" { return PLUSPLUS; }
118 "--" { return MINUSMINUS; }
120 "->" { return ARROW; }
122 "__asm" { if (!parse_ignored_macro()) REJECT; }
123 "__asm__" { if (!parse_ignored_macro()) REJECT; }
124 "__attribute__" { if (!parse_ignored_macro()) REJECT; }
125 "__attribute" { if (!parse_ignored_macro()) REJECT; }
126 "__const" { return CONST; }
127 "__extension__" { return EXTENSION; }
128 "__inline" { return INLINE; }
129 "__nonnull" { if (!parse_ignored_macro()) REJECT; }
130 "__signed__" { return SIGNED; }
131 "__restrict" { return RESTRICT; }
132 "__typeof" { if (!parse_ignored_macro()) REJECT; }
133 "_Bool" { return BOOL; }
135 [a-zA-Z_][a-zA-Z_0-9]* { if (scanner->macro_scan) return IDENTIFIER; else REJECT; }
137 "asm" { if (!parse_ignored_macro()) REJECT; }
138 "auto" { return AUTO; }
139 "break" { return BREAK; }
140 "case" { return CASE; }
141 "char" { return CHAR; }
142 "const" { return CONST; }
143 "continue" { return CONTINUE; }
144 "default" { return DEFAULT; }
146 "double" { return DOUBLE; }
147 "else" { return ELSE; }
148 "enum" { return ENUM; }
149 "extern" { return EXTERN; }
150 "float" { return FLOAT; }
151 "for" { return FOR; }
152 "goto" { return GOTO; }
154 "inline" { return INLINE; }
155 "int" { return INT; }
156 "long" { return LONG; }
157 "register" { return REGISTER; }
158 "restrict" { return RESTRICT; }
159 "return" { return RETURN; }
160 "short" { return SHORT; }
161 "signed" { return SIGNED; }
162 "sizeof" { return SIZEOF; }
163 "static" { return STATIC; }
164 "struct" { return STRUCT; }
165 "switch" { return SWITCH; }
166 "typedef" { return TYPEDEF; }
167 "union" { return UNION; }
168 "unsigned" { return UNSIGNED; }
169 "void" { return VOID; }
170 "volatile" { return VOLATILE; }
171 "while" { return WHILE; }
173 [a-zA-Z_][a-zA-Z_0-9]* { return check_identifier(scanner, yytext); }
175 "0"[xX][0-9a-fA-F]+{intsuffix}? { return INTEGER; }
176 "0"[0-7]+{intsuffix}? { return INTEGER; }
177 [0-9]+{intsuffix}? { return INTEGER; }
179 {fracconst}{exppart}?{floatsuffix}? { return FLOATING; }
180 [0-9]+{exppart}{floatsuffix}? { return FLOATING; }
182 "'"{chartext}*"'" { return CHARACTER; }
183 "L'"{chartext}*"'" { return CHARACTER; }
185 "\""{stringtext}*"\"" { return STRING; }
186 "L\""{stringtext}*"\"" { return STRING; }
188 . { fprintf(stderr, "%s:%d: unexpected character `%c'\n", scanner->current_filename, lineno, yytext[0]); }
200 parse_gtkdoc (GISourceScanner *scanner,
205 gboolean isline = FALSE;
209 GISourceDirective *directive;
212 GSList *options = NULL;
216 line_buf = g_string_new ("");
226 g_string_append_c (line_buf, *c1);
228 } while (*c2 != EOF && !(*c1 == '*' && *c2 == '/'));
232 g_string_free (line_buf, TRUE);
236 line = g_string_free (line_buf, FALSE);
238 /* Ignore lines that don't have a : - this is a hack but avoids
239 * trying to parse too many things as annotations
241 if (!strchr (line, ':'))
247 parts = g_strsplit (line, ":", 3);
248 n_parts = g_strv_length (parts);
250 if (g_ascii_strcasecmp (parts[0], "eprecated") == 0)
253 options = g_slist_prepend (options, g_strdup_printf ("%s: %s", parts[1], parts[2]));
254 else if (n_parts == 2)
255 options = g_slist_prepend (options, g_strdup (parts[1]));
257 options = g_slist_prepend (options, g_strdup (""));
261 else if (n_parts >= 2)
267 char *ptr = g_strdup (parts[1]);
272 start = strchr (ptr, '(');
273 while (start != NULL)
275 end = strchr (start, ')');
278 options = g_slist_prepend (options, g_strndup (start+1, end-(start+1)));
279 start = strchr (end+1, '(');
292 else /* parts == 1 */
299 * Special cases for global annotations.
300 * Context-sensitive parsing would probably be the right way to go.
302 if (g_ascii_strncasecmp ("eturn", name, 5) == 0)
304 else if (g_ascii_strncasecmp ("eprecated", name, 9) == 0)
305 rname = "deprecated";
309 directive = gi_source_directive_new (rname, value, options);
310 directives = g_hash_table_lookup (scanner->directives_map, symbol);
311 directives = g_slist_prepend (directives, directive);
312 g_hash_table_replace (scanner->directives_map,
313 g_strdup (symbol), directives);
321 parse_comment (GISourceScanner *scanner)
323 GString *symbol = NULL;
324 gboolean startofline = FALSE, have_symbol = FALSE, start1 = FALSE, start_symbol = FALSE;
330 while (c2 != EOF && !(c1 == '*' && c2 == '/'))
336 else if (c1 == '*' && start1)
338 else if (!have_symbol && start_symbol)
341 symbol = g_string_new ("");
343 g_string_append_c (symbol, c1);
355 if ((c1 != '*' && c1 != ' '))
358 if (startofline && (c1 == ' ') && (c2 == '@' || (c2 == 'r') || (c2 == 'R') || (c2 == 'D')))
363 parse_gtkdoc (scanner, symbol->str, &c1, &c2);
368 g_string_free (symbol, TRUE);
373 check_identifier (GISourceScanner *scanner,
377 * This function checks if `s' is a type name or an
381 if (gi_source_scanner_is_typedef (scanner, s)) {
383 } else if (strcmp (s, "__builtin_va_list") == 0) {
391 process_directive (GISourceScanner *scanner)
393 /* extract current filename from #line directives */
394 GString *filename_builder;
395 gboolean in_string, found_filename;
398 found_filename = FALSE;
400 filename_builder = g_string_new ("");
403 while (c != EOF && c != '\n') {
407 found_filename = TRUE;
408 } else if (c >= '0' && c <= '9') {
409 if (!found_filename) {
410 lineno = lineno * 10 + (c - '0');
416 } else if (c == '\\') {
417 g_string_append_c (filename_builder, c);
419 g_string_append_c (filename_builder, c);
421 g_string_append_c (filename_builder, c);
427 if (filename_builder->len > 0) {
428 char *filename = g_strcompress (filename_builder->str);
429 if (g_realpath (filename))
431 g_free (scanner->current_filename);
432 scanner->current_filename = g_realpath (filename);
433 g_assert (scanner->current_filename);
438 g_string_free (filename_builder, TRUE);
442 * This parses a macro which is ignored, such as
443 * __attribute__((x)) or __asm__ (x)
446 parse_ignored_macro (void)
451 while ((c = input ()) != EOF && isspace (c))
457 while ((c = input ()) != EOF && (nest > 0 || c != ')')) {
463 while ((c = input ()) != EOF && c != '"') {
467 } else if (c == '\'') {
476 } else if (c == '\n')