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"
43 #define YY_BUF_SIZE 1048576
45 extern int yylex (GISourceScanner *scanner);
46 #define YY_DECL int yylex (GISourceScanner *scanner)
47 static int yywrap (void);
48 static void parse_comment (GISourceScanner *scanner);
49 static void process_linemarks (GISourceScanner *scanner);
50 static int check_identifier (GISourceScanner *scanner, const char *);
51 static int parse_ignored_macro (void);
56 intsuffix ([uU][lL]?[lL]?)|([lL][lL]?[uU]?)
57 fracconst ([0-9]*\.[0-9]+)|([0-9]+\.)
58 exppart [eE][-+]?[0-9]+
60 chartext ([^\\\'])|(\\.)
61 stringtext ([^\\\"])|(\\.)
65 \n.* { strncpy(linebuf, yytext+1, sizeof(linebuf)); /* save the next line */
66 linebuf[sizeof(linebuf)-1]='\0';
67 /* printf("%4d:%s\n",lineno,linebuf); */
68 yyless(1); /* give back all but the \n to rescan */
72 [\t\f\v\r ]+ { /* Ignore whitespace. */ }
74 "/*" { parse_comment(scanner); }
75 "/*"[\t ]*<[\t ]*"private"[\t ]*>" */" { scanner->private = TRUE; }
76 "/*"[\t ]*<[\t ]*"public"[\t ]*>" */" { scanner->private = FALSE; }
79 "#define "[a-zA-Z_][a-zA-Z_0-9]*"(" { yyless (yyleng - 1); return FUNCTION_MACRO; }
80 "#define "[a-zA-Z_][a-zA-Z_0-9]* { return OBJECT_MACRO; }
82 "# "[0-9]+" ".*"\n" { process_linemarks(scanner); }
96 "..." { return ELLIPSIS; }
112 "+=" { return ADDEQ; }
113 "-=" { return SUBEQ; }
114 "*=" { return MULEQ; }
115 "/=" { return DIVEQ; }
116 "%=" { return MODEQ; }
117 "^=" { return XOREQ; }
118 "&=" { return ANDEQ; }
119 "|=" { return OREQ; }
122 "<<=" { return SLEQ; }
123 ">>=" { return SREQ; }
125 "!=" { return NOTEQ; }
126 "<=" { return LTEQ; }
127 ">=" { return GTEQ; }
128 "&&" { return ANDAND; }
129 "||" { return OROR; }
130 "++" { return PLUSPLUS; }
131 "--" { return MINUSMINUS; }
133 "->" { return ARROW; }
135 "__asm" { if (!parse_ignored_macro()) REJECT; }
136 "__asm__" { if (!parse_ignored_macro()) REJECT; }
137 "__attribute__" { if (!parse_ignored_macro()) REJECT; }
138 "__attribute" { if (!parse_ignored_macro()) REJECT; }
139 "__const" { return CONST; }
140 "__extension__" { return EXTENSION; }
141 "__inline" { return INLINE; }
142 "__nonnull" { if (!parse_ignored_macro()) REJECT; }
143 "__signed__" { return SIGNED; }
144 "__restrict" { return RESTRICT; }
145 "__typeof" { if (!parse_ignored_macro()) REJECT; }
146 "_Bool" { return BOOL; }
148 [a-zA-Z_][a-zA-Z_0-9]* { if (scanner->macro_scan) return IDENTIFIER; else REJECT; }
150 "asm" { if (!parse_ignored_macro()) REJECT; }
151 "auto" { return AUTO; }
152 "break" { return BREAK; }
153 "case" { return CASE; }
154 "char" { return CHAR; }
155 "const" { return CONST; }
156 "continue" { return CONTINUE; }
157 "default" { return DEFAULT; }
159 "double" { return DOUBLE; }
160 "else" { return ELSE; }
161 "enum" { return ENUM; }
162 "extern" { return EXTERN; }
163 "float" { return FLOAT; }
164 "for" { return FOR; }
165 "goto" { return GOTO; }
167 "inline" { return INLINE; }
168 "int" { return INT; }
169 "long" { return LONG; }
170 "register" { return REGISTER; }
171 "restrict" { return RESTRICT; }
172 "return" { return RETURN; }
173 "short" { return SHORT; }
174 "signed" { return SIGNED; }
175 "sizeof" { return SIZEOF; }
176 "static" { return STATIC; }
177 "struct" { return STRUCT; }
178 "switch" { return SWITCH; }
179 "typedef" { return TYPEDEF; }
180 "union" { return UNION; }
181 "unsigned" { return UNSIGNED; }
182 "void" { return VOID; }
183 "volatile" { return VOLATILE; }
184 "while" { return WHILE; }
186 [a-zA-Z_][a-zA-Z_0-9]* { return check_identifier(scanner, yytext); }
188 "0"[xX][0-9a-fA-F]+{intsuffix}? { return INTEGER; }
189 "0"[0-7]+{intsuffix}? { return INTEGER; }
190 [0-9]+{intsuffix}? { return INTEGER; }
192 {fracconst}{exppart}?{floatsuffix}? { return FLOATING; }
193 [0-9]+{exppart}{floatsuffix}? { return FLOATING; }
195 "'"{chartext}*"'" { return CHARACTER; }
196 "L'"{chartext}*"'" { return CHARACTER; }
198 "\""{stringtext}*"\"" { return STRING; }
199 "L\""{stringtext}*"\"" { return STRING; }
201 . { if (yytext[0]) fprintf(stderr, "%s:%d: unexpected character `%c'\n", scanner->current_filename, lineno, yytext[0]); }
213 parse_comment (GISourceScanner *scanner)
217 GISourceComment *comment;
223 string = g_string_new ("");
225 comment_lineno = lineno;
227 while (c2 != EOF && !(c1 == '*' && c2 == '/'))
229 g_string_append_c (string, c1);
239 comment = g_slice_new (GISourceComment);
240 comment->comment = g_string_free (string, FALSE);
241 comment->line = comment_lineno;
242 comment->filename = g_strdup(scanner->current_filename);
244 scanner->comments = g_slist_prepend (scanner->comments,
249 check_identifier (GISourceScanner *scanner,
253 * This function checks if `s' is a type name or an
257 if (gi_source_scanner_is_typedef (scanner, s)) {
259 } else if (strcmp (s, "__builtin_va_list") == 0) {
267 * # linenum "filename" flags
268 * See http://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html
272 process_linemarks (GISourceScanner *scanner)
278 sscanf(yytext, "# %d \"%1024[^\"]\"", &lineno, filename);
280 compress = g_strcompress (filename);
281 real = g_realpath (filename);
283 g_free (scanner->current_filename);
284 scanner->current_filename = real;
292 * This parses a macro which is ignored, such as
293 * __attribute__((x)) or __asm__ (x)
296 parse_ignored_macro (void)
301 while ((c = input ()) != EOF && isspace (c))
307 while ((c = input ()) != EOF && (nest > 0 || c != ')')) {
313 while ((c = input ()) != EOF && c != '"') {
317 } else if (c == '\'') {
326 } else if (c == '\n')