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. */
32 #include "parse.tab.h"
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 /* We don't do multiple input files. */
66 /* Keep track of our location in the original source files. */
67 ^#[ \t]+{INT}[ \t]+\"[^\"\n]+\".*\n return FILENAME;
71 /* Ignore all other whitespace. */
75 {STRING} return STRING;
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. */
83 {MC_TOKEN} return OTHER;
89 /* All other tokens are single characters. */
95 /* Bring in the keyword recognizer. */
97 #include "keywords.hash.c"
100 /* Macros to append to our phrase collection list. */
103 * We mark any token, that that equals to a known enumerator, as
104 * SYM_ENUM_CONST. The parser will change this for struct and union tags later,
105 * the only problem is struct and union members:
106 * enum e { a, b }; struct s { int a, b; }
107 * but in this case, the only effect will be, that the ABI checksums become
108 * more volatile, which is acceptable. Also, such collisions are quite rare,
109 * so far it was only observed in include/linux/telephony.h.
111 #define _APP(T,L) do { \
112 cur_node = next_node; \
113 next_node = xmalloc(sizeof(*next_node)); \
114 next_node->next = cur_node; \
115 cur_node->string = memcpy(xmalloc(L+1), T, L+1); \
117 find_symbol(cur_node->string, SYM_ENUM_CONST, 1)?\
118 SYM_ENUM_CONST : SYM_NORMAL ; \
119 cur_node->in_source_file = in_source_file; \
122 #define APP _APP(yytext, yyleng)
125 /* The second stage lexer. Here we incorporate knowledge of the state
126 of the parser to tailor the tokens that are returned. */
132 ST_NOTSTARTED, ST_NORMAL, ST_ATTRIBUTE, ST_ASM, ST_TYPEOF, ST_TYPEOF_1,
133 ST_BRACKET, ST_BRACE, ST_EXPRESSION,
134 ST_TABLE_1, ST_TABLE_2, ST_TABLE_3, ST_TABLE_4,
135 ST_TABLE_5, ST_TABLE_6
136 } lexstate = ST_NOTSTARTED;
138 static int suppress_type_lookup, dont_want_brace_phrase;
139 static struct string_list *next_node;
141 int token, count = 0;
142 struct string_list *cur_node;
144 if (lexstate == ST_NOTSTARTED)
146 next_node = xmalloc(sizeof(*next_node));
147 next_node->next = NULL;
148 lexstate = ST_NORMAL;
156 else if (token == FILENAME)
160 /* Save the filename and line number for later error messages. */
165 file = strchr(yytext, '\"')+1;
166 e = strchr(file, '\"');
168 cur_filename = memcpy(xmalloc(e-file+1), file, e-file+1);
169 cur_line = atoi(yytext+2);
172 source_file = xstrdup(cur_filename);
175 in_source_file = (strcmp(cur_filename, source_file) == 0);
189 const struct resword *r = is_reserved_word(yytext, yyleng);
192 switch (token = r->token)
195 lexstate = ST_ATTRIBUTE;
203 lexstate = ST_TYPEOF;
210 dont_want_brace_phrase = 3;
211 suppress_type_lookup = 2;
214 case EXPORT_SYMBOL_KEYW:
218 if (!suppress_type_lookup)
220 if (find_symbol(yytext, SYM_TYPEDEF, 1))
228 lexstate = ST_BRACKET;
234 if (dont_want_brace_phrase)
242 lexstate = ST_EXPRESSION;
262 lexstate = ST_NORMAL;
263 token = ATTRIBUTE_PHRASE;
282 lexstate = ST_NORMAL;
295 if (is_reserved_word(yytext, yyleng)
296 || find_symbol(yytext, SYM_TYPEDEF, 1))
300 lexstate = ST_NORMAL;
306 lexstate = ST_TYPEOF;
314 lexstate = ST_TYPEOF_1;
322 lexstate = ST_NORMAL;
323 token = TYPEOF_PHRASE;
343 lexstate = ST_NORMAL;
344 token = BRACKET_PHRASE;
363 lexstate = ST_NORMAL;
364 token = BRACE_PHRASE;
376 case '(': case '[': case '{':
381 /* is this the last line of an enum declaration? */
384 /* Put back the token we just read so's we can find it again
385 after registering the expression. */
388 lexstate = ST_NORMAL;
389 token = EXPRESSION_PHRASE;
400 /* Put back the token we just read so's we can find it again
401 after registering the expression. */
404 lexstate = ST_NORMAL;
405 token = EXPRESSION_PHRASE;
420 if (token == IDENT && yyleng == 1 && yytext[0] == 'X')
422 token = EXPORT_SYMBOL_KEYW;
423 lexstate = ST_TABLE_5;
427 lexstate = ST_TABLE_6;
433 case '{': case '[': case '(':
436 case '}': case ']': case ')':
441 lexstate = ST_TABLE_2;
451 lexstate = ST_NORMAL;
459 lexstate = ST_TABLE_2;
473 if (suppress_type_lookup > 0)
474 --suppress_type_lookup;
475 if (dont_want_brace_phrase > 0)
476 --dont_want_brace_phrase;
478 yylval = &next_node->next;