1 /* Lexical analysis for genksyms.
2 Copyright 1996, 1997 Linux International.
4 New implementation contributed by Richard Henderson <rth@tamu.edu>
5 Based on original work by Bjorn Ekwall <bj0rn@blox.se>
7 Taken from Linux modutils 2.4.22.
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.
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.
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. */
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)
40 IDENT [A-Za-z_\$][A-Za-z0-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}?
48 FRAC ([0-9]*\.[0-9]+)|([0-9]+\.)
51 REAL ({FRAC}{EXP}?{F_SUF}?)|([0-9]+{EXP}{F_SUF}?)
53 STRING L?\"([^\\\"]*\\.)*[^\\\"]*\"
54 CHAR L?\'([^\\\']*\\.)*[^\\\']*\'
56 MC_TOKEN ([~%^&*+=|<>/-]=)|(&&)|("||")|(->)|(<<)|(>>)
58 /* Version 2 checksumming does proper tokenization; version 1 wasn't
62 /* We don't do multiple input files. */
70 /* Keep track of our location in the original source files. */
71 ^#[ \t]+{INT}[ \t]+\"[^\"\n]+\".*\n return FILENAME;
75 /* Ignore all other whitespace. */
79 {STRING} return STRING;
83 /* The Pedant requires that the other C multi-character tokens be
84 recognized as tokens. We don't actually use them since we don't
85 parse expressions, but we do want whitespace to be arranged
86 around them properly. */
87 <V2_TOKENS>{MC_TOKEN} return OTHER;
88 <V2_TOKENS>{INT} return INT;
89 <V2_TOKENS>{REAL} return REAL;
93 /* All other tokens are single characters. */
99 /* Bring in the keyword recognizer. */
101 #include "keywords.c"
104 /* Macros to append to our phrase collection list. */
106 #define _APP(T,L) do { \
107 cur_node = next_node; \
108 next_node = xmalloc(sizeof(*next_node)); \
109 next_node->next = cur_node; \
110 cur_node->string = memcpy(xmalloc(L+1), T, L+1); \
111 cur_node->tag = SYM_NORMAL; \
114 #define APP _APP(yytext, yyleng)
117 /* The second stage lexer. Here we incorporate knowledge of the state
118 of the parser to tailor the tokens that are returned. */
124 ST_NOTSTARTED, ST_NORMAL, ST_ATTRIBUTE, ST_ASM, ST_BRACKET, ST_BRACE,
125 ST_EXPRESSION, ST_TABLE_1, ST_TABLE_2, ST_TABLE_3, ST_TABLE_4,
126 ST_TABLE_5, ST_TABLE_6
127 } lexstate = ST_NOTSTARTED;
129 static int suppress_type_lookup, dont_want_brace_phrase;
130 static struct string_list *next_node;
132 int token, count = 0;
133 struct string_list *cur_node;
135 if (lexstate == ST_NOTSTARTED)
138 next_node = xmalloc(sizeof(*next_node));
139 next_node->next = NULL;
140 lexstate = ST_NORMAL;
148 else if (token == FILENAME)
152 /* Save the filename and line number for later error messages. */
157 file = strchr(yytext, '\"')+1;
158 e = strchr(file, '\"');
160 cur_filename = memcpy(xmalloc(e-file+1), file, e-file+1);
161 cur_line = atoi(yytext+2);
174 const struct resword *r = is_reserved_word(yytext, yyleng);
177 switch (token = r->token)
180 lexstate = ST_ATTRIBUTE;
190 dont_want_brace_phrase = 3;
192 suppress_type_lookup = 2;
195 case EXPORT_SYMBOL_KEYW:
199 if (!suppress_type_lookup)
201 struct symbol *sym = find_symbol(yytext, SYM_TYPEDEF);
202 if (sym && sym->type == SYM_TYPEDEF)
210 lexstate = ST_BRACKET;
216 if (dont_want_brace_phrase)
224 lexstate = ST_EXPRESSION;
244 lexstate = ST_NORMAL;
245 token = ATTRIBUTE_PHRASE;
264 lexstate = ST_NORMAL;
284 lexstate = ST_NORMAL;
285 token = BRACKET_PHRASE;
304 lexstate = ST_NORMAL;
305 token = BRACE_PHRASE;
317 case '(': case '[': case '{':
321 case ')': case ']': case '}':
328 /* Put back the token we just read so's we can find it again
329 after registering the expression. */
332 lexstate = ST_NORMAL;
333 token = EXPRESSION_PHRASE;
348 if (token == IDENT && yyleng == 1 && yytext[0] == 'X')
350 token = EXPORT_SYMBOL_KEYW;
351 lexstate = ST_TABLE_5;
355 lexstate = ST_TABLE_6;
361 case '{': case '[': case '(':
364 case '}': case ']': case ')':
369 lexstate = ST_TABLE_2;
379 lexstate = ST_NORMAL;
387 lexstate = ST_TABLE_2;
401 if (suppress_type_lookup > 0)
402 --suppress_type_lookup;
403 if (dont_want_brace_phrase > 0)
404 --dont_want_brace_phrase;
406 yylval = &next_node->next;