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 *);
49 intsuffix ([uU][lL]?)|([lL][uU]?)
50 fracconst ([0-9]*\.[0-9]+)|([0-9]+\.)
51 exppart [eE][-+]?[0-9]+
53 chartext ([^\'])|(\\.)
54 stringtext ([^\"])|(\\.)
58 "\n" { ++lineno; } /* " */
59 [\t\f\v\r ]+ { /* Ignore whitespace. */ }
61 "/*" { parse_comment(scanner); }
64 "#define "[a-zA-Z_][a-zA-Z_0-9]*"(" { yyless (yyleng - 1); return FUNCTION_MACRO; }
65 "#define "[a-zA-Z_][a-zA-Z_0-9]* { return OBJECT_MACRO; }
67 "#" { process_directive(scanner); }
81 "..." { return ELLIPSIS; }
97 "+=" { return ADDEQ; }
98 "-=" { return SUBEQ; }
99 "*=" { return MULEQ; }
100 "/=" { return DIVEQ; }
101 "%=" { return MODEQ; }
102 "^=" { return XOREQ; }
103 "&=" { return ANDEQ; }
104 "|=" { return OREQ; }
107 "<<=" { return SLEQ; }
108 ">>=" { return SREQ; }
110 "!=" { return NOTEQ; }
111 "<=" { return LTEQ; }
112 ">=" { return GTEQ; }
113 "&&" { return ANDAND; }
114 "||" { return OROR; }
115 "++" { return PLUSPLUS; }
116 "--" { return MINUSMINUS; }
118 "->" { return ARROW; }
120 [a-zA-Z_][a-zA-Z_0-9]* { if (scanner->macro_scan) return IDENTIFIER; else REJECT; }
122 "auto" { return AUTO; }
123 "_Bool" { return BOOL; }
124 "break" { return BREAK; }
125 "case" { return CASE; }
126 "char" { return CHAR; }
127 "const" { return CONST; }
128 "continue" { return CONTINUE; }
129 "default" { return DEFAULT; }
131 "double" { return DOUBLE; }
132 "else" { return ELSE; }
133 "enum" { return ENUM; }
134 "extern" { return EXTERN; }
135 "float" { return FLOAT; }
136 "for" { return FOR; }
137 "goto" { return GOTO; }
139 "inline" { return INLINE; }
140 "int" { return INT; }
141 "long" { return LONG; }
142 "register" { return REGISTER; }
143 "restrict" { return RESTRICT; }
144 "return" { return RETURN; }
145 "short" { return SHORT; }
146 "signed" { return SIGNED; }
147 "sizeof" { return SIZEOF; }
148 "static" { return STATIC; }
149 "struct" { return STRUCT; }
150 "switch" { return SWITCH; }
151 "typedef" { return TYPEDEF; }
152 "union" { return UNION; }
153 "unsigned" { return UNSIGNED; }
154 "void" { return VOID; }
155 "volatile" { return VOLATILE; }
156 "while" { return WHILE; }
158 [a-zA-Z_][a-zA-Z_0-9]* { return check_identifier(scanner, yytext); }
160 "0"[xX][0-9a-fA-F]+{intsuffix}? { return INTEGER; }
161 "0"[0-7]+{intsuffix}? { return INTEGER; }
162 [0-9]+{intsuffix}? { return INTEGER; }
164 {fracconst}{exppart}?{floatsuffix}? { return FLOATING; }
165 [0-9]+{exppart}{floatsuffix}? { return FLOATING; }
167 "'"{chartext}*"'" { return CHARACTER; }
168 "L'"{chartext}*"'" { return CHARACTER; }
170 "\""{stringtext}*"\"" { return STRING; }
171 "L\""{stringtext}*"\"" { return STRING; }
173 . { fprintf(stderr, "%s:%d: unexpected character `%c'\n", scanner->current_filename, lineno, yytext[0]); }
185 parse_gtkdoc (GISourceScanner *scanner,
190 gboolean isline = FALSE;
194 GISourceDirective *directive;
197 GSList *options = NULL;
213 } while (*c2 != EOF && !(*c1 == '*' && *c2 == '/'));
220 parts = g_strsplit (line, ": ", 3);
222 if (g_strv_length (parts) >= 2)
226 if (g_strv_length (parts) == 3)
228 char *ptr = parts[1];
229 GString *current = NULL;
232 current = g_string_new ("");
243 else if (*ptr == ')')
248 options = g_slist_prepend (options, current->str);
251 g_string_append_c (current, *ptr);
255 g_string_free (current, FALSE);
260 else /* parts == 1 */
267 * This is a special case for return values, name will only be
268 * 'eturn' or a valid name, check the call site.
269 * Context-sensitive parsing would probably be the right way to go
271 if (g_ascii_strncasecmp ("eturn", name, 5) == 0)
276 directive = gi_source_directive_new (rname, value, options);
277 directives = g_hash_table_lookup (scanner->directives_map, symbol);
278 directives = g_slist_prepend (directives, directive);
279 g_hash_table_replace (scanner->directives_map,
280 g_strdup (symbol), directives);
288 parse_comment (GISourceScanner *scanner)
290 GString *symbol = NULL;
291 gboolean startofline = FALSE, have_symbol = FALSE, start1 = FALSE, start_symbol = FALSE;
297 while (c2 != EOF && !(c1 == '*' && c2 == '/'))
303 else if (c1 == '*' && start1)
305 else if (!have_symbol && start_symbol)
308 symbol = g_string_new ("");
310 g_string_append_c (symbol, c1);
322 if ((c1 != '*' && c1 != ' '))
325 if (startofline && (c1 == ' ') && (c2 == '@' || (c2 == 'r') || (c2 == 'R')))
330 parse_gtkdoc (scanner, symbol->str, &c1, &c2);
335 g_string_free (symbol, TRUE);
340 check_identifier (GISourceScanner *scanner,
344 * This function checks if `s' is a type name or an
348 if (gi_source_scanner_is_typedef (scanner, s)) {
350 } else if (strcmp (s, "__builtin_va_list") == 0) {
358 process_directive (GISourceScanner *scanner)
360 /* extract current filename from #line directives */
361 GString *filename_builder;
362 gboolean in_string, found_filename;
365 found_filename = FALSE;
367 filename_builder = g_string_new ("");
370 while (c != EOF && c != '\n') {
374 found_filename = TRUE;
375 } else if (c >= '0' && c <= '9') {
376 if (!found_filename) {
377 lineno = lineno * 10 + (c - '0');
383 } else if (c == '\\') {
384 g_string_append_c (filename_builder, c);
386 g_string_append_c (filename_builder, c);
388 g_string_append_c (filename_builder, c);
394 if (filename_builder->len > 0) {
395 char *filename = g_strcompress (filename_builder->str);
396 if (g_realpath (filename))
398 g_free (scanner->current_filename);
399 scanner->current_filename = g_realpath (filename);
400 g_assert (scanner->current_filename);
405 g_string_free (filename_builder, TRUE);