lex.l 7.7 KB

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