]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Lexical analysis for genksyms. |
2 | Copyright 1996, 1997 Linux International. | |
3 | ||
4 | New implementation contributed by Richard Henderson <rth@tamu.edu> | |
5 | Based on original work by Bjorn Ekwall <bj0rn@blox.se> | |
6 | ||
7 | Taken from Linux modutils 2.4.22. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify it | |
10 | under the terms of the GNU General Public License as published by the | |
11 | Free Software Foundation; either version 2 of the License, or (at your | |
12 | option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, but | |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software Foundation, | |
21 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
22 | ||
23 | ||
24 | %{ | |
25 | ||
26 | #include <limits.h> | |
27 | #include <stdlib.h> | |
28 | #include <string.h> | |
29 | #include <ctype.h> | |
30 | ||
31 | #include "genksyms.h" | |
32 | #include "parse.h" | |
33 | ||
34 | /* We've got a two-level lexer here. We let flex do basic tokenization | |
35 | and then we categorize those basic tokens in the second stage. */ | |
36 | #define YY_DECL static int yylex1(void) | |
37 | ||
38 | %} | |
39 | ||
40 | IDENT [A-Za-z_\$][A-Za-z0-9_\$]* | |
41 | ||
42 | O_INT 0[0-7]* | |
43 | D_INT [1-9][0-9]* | |
44 | X_INT 0[Xx][0-9A-Fa-f]+ | |
45 | I_SUF [Uu]|[Ll]|[Uu][Ll]|[Ll][Uu] | |
46 | INT ({O_INT}|{D_INT}|{X_INT}){I_SUF}? | |
47 | ||
48 | FRAC ([0-9]*\.[0-9]+)|([0-9]+\.) | |
49 | EXP [Ee][+-]?[0-9]+ | |
50 | F_SUF [FfLl] | |
51 | REAL ({FRAC}{EXP}?{F_SUF}?)|([0-9]+{EXP}{F_SUF}?) | |
52 | ||
53 | STRING L?\"([^\\\"]*\\.)*[^\\\"]*\" | |
54 | CHAR L?\'([^\\\']*\\.)*[^\\\']*\' | |
55 | ||
56 | MC_TOKEN ([~%^&*+=|<>/-]=)|(&&)|("||")|(->)|(<<)|(>>) | |
57 | ||
1da177e4 LT |
58 | /* We don't do multiple input files. */ |
59 | %option noyywrap | |
60 | ||
11ddad39 AB |
61 | %option noinput |
62 | ||
1da177e4 LT |
63 | %% |
64 | ||
65 | ||
66 | /* Keep track of our location in the original source files. */ | |
67 | ^#[ \t]+{INT}[ \t]+\"[^\"\n]+\".*\n return FILENAME; | |
68 | ^#.*\n cur_line++; | |
69 | \n cur_line++; | |
70 | ||
71 | /* Ignore all other whitespace. */ | |
72 | [ \t\f\v\r]+ ; | |
73 | ||
74 | ||
75 | {STRING} return STRING; | |
76 | {CHAR} return CHAR; | |
77 | {IDENT} return IDENT; | |
78 | ||
79 | /* The Pedant requires that the other C multi-character tokens be | |
80 | recognized as tokens. We don't actually use them since we don't | |
81 | parse expressions, but we do want whitespace to be arranged | |
82 | around them properly. */ | |
95f1d639 MM |
83 | {MC_TOKEN} return OTHER; |
84 | {INT} return INT; | |
85 | {REAL} return REAL; | |
1da177e4 LT |
86 | |
87 | "..." return DOTS; | |
88 | ||
89 | /* All other tokens are single characters. */ | |
90 | . return yytext[0]; | |
91 | ||
92 | ||
93 | %% | |
94 | ||
95 | /* Bring in the keyword recognizer. */ | |
96 | ||
97 | #include "keywords.c" | |
98 | ||
99 | ||
100 | /* Macros to append to our phrase collection list. */ | |
101 | ||
102 | #define _APP(T,L) do { \ | |
103 | cur_node = next_node; \ | |
104 | next_node = xmalloc(sizeof(*next_node)); \ | |
105 | next_node->next = cur_node; \ | |
106 | cur_node->string = memcpy(xmalloc(L+1), T, L+1); \ | |
107 | cur_node->tag = SYM_NORMAL; \ | |
108 | } while (0) | |
109 | ||
110 | #define APP _APP(yytext, yyleng) | |
111 | ||
112 | ||
113 | /* The second stage lexer. Here we incorporate knowledge of the state | |
114 | of the parser to tailor the tokens that are returned. */ | |
115 | ||
116 | int | |
117 | yylex(void) | |
118 | { | |
119 | static enum { | |
120 | ST_NOTSTARTED, ST_NORMAL, ST_ATTRIBUTE, ST_ASM, ST_BRACKET, ST_BRACE, | |
121 | ST_EXPRESSION, ST_TABLE_1, ST_TABLE_2, ST_TABLE_3, ST_TABLE_4, | |
122 | ST_TABLE_5, ST_TABLE_6 | |
123 | } lexstate = ST_NOTSTARTED; | |
124 | ||
125 | static int suppress_type_lookup, dont_want_brace_phrase; | |
126 | static struct string_list *next_node; | |
127 | ||
128 | int token, count = 0; | |
129 | struct string_list *cur_node; | |
130 | ||
131 | if (lexstate == ST_NOTSTARTED) | |
132 | { | |
1da177e4 LT |
133 | next_node = xmalloc(sizeof(*next_node)); |
134 | next_node->next = NULL; | |
135 | lexstate = ST_NORMAL; | |
136 | } | |
137 | ||
138 | repeat: | |
139 | token = yylex1(); | |
140 | ||
141 | if (token == 0) | |
142 | return 0; | |
143 | else if (token == FILENAME) | |
144 | { | |
145 | char *file, *e; | |
146 | ||
147 | /* Save the filename and line number for later error messages. */ | |
148 | ||
149 | if (cur_filename) | |
150 | free(cur_filename); | |
151 | ||
152 | file = strchr(yytext, '\"')+1; | |
153 | e = strchr(file, '\"'); | |
154 | *e = '\0'; | |
155 | cur_filename = memcpy(xmalloc(e-file+1), file, e-file+1); | |
156 | cur_line = atoi(yytext+2); | |
157 | ||
158 | goto repeat; | |
159 | } | |
160 | ||
161 | switch (lexstate) | |
162 | { | |
163 | case ST_NORMAL: | |
164 | switch (token) | |
165 | { | |
166 | case IDENT: | |
167 | APP; | |
168 | { | |
169 | const struct resword *r = is_reserved_word(yytext, yyleng); | |
170 | if (r) | |
171 | { | |
172 | switch (token = r->token) | |
173 | { | |
174 | case ATTRIBUTE_KEYW: | |
175 | lexstate = ST_ATTRIBUTE; | |
176 | count = 0; | |
177 | goto repeat; | |
178 | case ASM_KEYW: | |
179 | lexstate = ST_ASM; | |
180 | count = 0; | |
181 | goto repeat; | |
182 | ||
183 | case STRUCT_KEYW: | |
184 | case UNION_KEYW: | |
185 | dont_want_brace_phrase = 3; | |
186 | case ENUM_KEYW: | |
187 | suppress_type_lookup = 2; | |
188 | goto fini; | |
189 | ||
190 | case EXPORT_SYMBOL_KEYW: | |
191 | goto fini; | |
192 | } | |
193 | } | |
194 | if (!suppress_type_lookup) | |
195 | { | |
196 | struct symbol *sym = find_symbol(yytext, SYM_TYPEDEF); | |
197 | if (sym && sym->type == SYM_TYPEDEF) | |
198 | token = TYPE; | |
199 | } | |
200 | } | |
201 | break; | |
202 | ||
203 | case '[': | |
204 | APP; | |
205 | lexstate = ST_BRACKET; | |
206 | count = 1; | |
207 | goto repeat; | |
208 | ||
209 | case '{': | |
210 | APP; | |
211 | if (dont_want_brace_phrase) | |
212 | break; | |
213 | lexstate = ST_BRACE; | |
214 | count = 1; | |
215 | goto repeat; | |
216 | ||
217 | case '=': case ':': | |
218 | APP; | |
219 | lexstate = ST_EXPRESSION; | |
220 | break; | |
221 | ||
222 | case DOTS: | |
223 | default: | |
224 | APP; | |
225 | break; | |
226 | } | |
227 | break; | |
228 | ||
229 | case ST_ATTRIBUTE: | |
230 | APP; | |
231 | switch (token) | |
232 | { | |
233 | case '(': | |
234 | ++count; | |
235 | goto repeat; | |
236 | case ')': | |
237 | if (--count == 0) | |
238 | { | |
239 | lexstate = ST_NORMAL; | |
240 | token = ATTRIBUTE_PHRASE; | |
241 | break; | |
242 | } | |
243 | goto repeat; | |
244 | default: | |
245 | goto repeat; | |
246 | } | |
247 | break; | |
248 | ||
249 | case ST_ASM: | |
250 | APP; | |
251 | switch (token) | |
252 | { | |
253 | case '(': | |
254 | ++count; | |
255 | goto repeat; | |
256 | case ')': | |
257 | if (--count == 0) | |
258 | { | |
259 | lexstate = ST_NORMAL; | |
260 | token = ASM_PHRASE; | |
261 | break; | |
262 | } | |
263 | goto repeat; | |
264 | default: | |
265 | goto repeat; | |
266 | } | |
267 | break; | |
268 | ||
269 | case ST_BRACKET: | |
270 | APP; | |
271 | switch (token) | |
272 | { | |
273 | case '[': | |
274 | ++count; | |
275 | goto repeat; | |
276 | case ']': | |
277 | if (--count == 0) | |
278 | { | |
279 | lexstate = ST_NORMAL; | |
280 | token = BRACKET_PHRASE; | |
281 | break; | |
282 | } | |
283 | goto repeat; | |
284 | default: | |
285 | goto repeat; | |
286 | } | |
287 | break; | |
288 | ||
289 | case ST_BRACE: | |
290 | APP; | |
291 | switch (token) | |
292 | { | |
293 | case '{': | |
294 | ++count; | |
295 | goto repeat; | |
296 | case '}': | |
297 | if (--count == 0) | |
298 | { | |
299 | lexstate = ST_NORMAL; | |
300 | token = BRACE_PHRASE; | |
301 | break; | |
302 | } | |
303 | goto repeat; | |
304 | default: | |
305 | goto repeat; | |
306 | } | |
307 | break; | |
308 | ||
309 | case ST_EXPRESSION: | |
310 | switch (token) | |
311 | { | |
312 | case '(': case '[': case '{': | |
313 | ++count; | |
314 | APP; | |
315 | goto repeat; | |
316 | case ')': case ']': case '}': | |
317 | --count; | |
318 | APP; | |
319 | goto repeat; | |
320 | case ',': case ';': | |
321 | if (count == 0) | |
322 | { | |
323 | /* Put back the token we just read so's we can find it again | |
324 | after registering the expression. */ | |
325 | unput(token); | |
326 | ||
327 | lexstate = ST_NORMAL; | |
328 | token = EXPRESSION_PHRASE; | |
329 | break; | |
330 | } | |
331 | APP; | |
332 | goto repeat; | |
333 | default: | |
334 | APP; | |
335 | goto repeat; | |
336 | } | |
337 | break; | |
338 | ||
339 | case ST_TABLE_1: | |
340 | goto repeat; | |
341 | ||
342 | case ST_TABLE_2: | |
343 | if (token == IDENT && yyleng == 1 && yytext[0] == 'X') | |
344 | { | |
345 | token = EXPORT_SYMBOL_KEYW; | |
346 | lexstate = ST_TABLE_5; | |
347 | APP; | |
348 | break; | |
349 | } | |
350 | lexstate = ST_TABLE_6; | |
351 | /* FALLTHRU */ | |
352 | ||
353 | case ST_TABLE_6: | |
354 | switch (token) | |
355 | { | |
356 | case '{': case '[': case '(': | |
357 | ++count; | |
358 | break; | |
359 | case '}': case ']': case ')': | |
360 | --count; | |
361 | break; | |
362 | case ',': | |
363 | if (count == 0) | |
364 | lexstate = ST_TABLE_2; | |
365 | break; | |
366 | }; | |
367 | goto repeat; | |
368 | ||
369 | case ST_TABLE_3: | |
370 | goto repeat; | |
371 | ||
372 | case ST_TABLE_4: | |
373 | if (token == ';') | |
374 | lexstate = ST_NORMAL; | |
375 | goto repeat; | |
376 | ||
377 | case ST_TABLE_5: | |
378 | switch (token) | |
379 | { | |
380 | case ',': | |
381 | token = ';'; | |
382 | lexstate = ST_TABLE_2; | |
383 | APP; | |
384 | break; | |
385 | default: | |
386 | APP; | |
387 | break; | |
388 | } | |
389 | break; | |
390 | ||
391 | default: | |
6803dc0e | 392 | exit(1); |
1da177e4 LT |
393 | } |
394 | fini: | |
395 | ||
396 | if (suppress_type_lookup > 0) | |
397 | --suppress_type_lookup; | |
398 | if (dont_want_brace_phrase > 0) | |
399 | --dont_want_brace_phrase; | |
400 | ||
401 | yylval = &next_node->next; | |
402 | ||
403 | return token; | |
404 | } |