lex.l 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. %option noinput
  47. %%
  48. /* Keep track of our location in the original source files. */
  49. ^#[ \t]+{INT}[ \t]+\"[^\"\n]+\".*\n return FILENAME;
  50. ^#.*\n cur_line++;
  51. \n cur_line++;
  52. /* Ignore all other whitespace. */
  53. [ \t\f\v\r]+ ;
  54. {STRING} return STRING;
  55. {CHAR} return CHAR;
  56. {IDENT} return IDENT;
  57. /* The Pedant requires that the other C multi-character tokens be
  58. recognized as tokens. We don't actually use them since we don't
  59. parse expressions, but we do want whitespace to be arranged
  60. around them properly. */
  61. <V2_TOKENS>{MC_TOKEN} return OTHER;
  62. <V2_TOKENS>{INT} return INT;
  63. <V2_TOKENS>{REAL} return REAL;
  64. "..." return DOTS;
  65. /* All other tokens are single characters. */
  66. . return yytext[0];
  67. %%
  68. /* Bring in the keyword recognizer. */
  69. #include "keywords.c"
  70. /* Macros to append to our phrase collection list. */
  71. #define _APP(T,L) do { \
  72. cur_node = next_node; \
  73. next_node = xmalloc(sizeof(*next_node)); \
  74. next_node->next = cur_node; \
  75. cur_node->string = memcpy(xmalloc(L+1), T, L+1); \
  76. cur_node->tag = SYM_NORMAL; \
  77. } while (0)
  78. #define APP _APP(yytext, yyleng)
  79. /* The second stage lexer. Here we incorporate knowledge of the state
  80. of the parser to tailor the tokens that are returned. */
  81. int
  82. yylex(void)
  83. {
  84. static enum {
  85. ST_NOTSTARTED, ST_NORMAL, ST_ATTRIBUTE, ST_ASM, ST_BRACKET, ST_BRACE,
  86. ST_EXPRESSION, ST_TABLE_1, ST_TABLE_2, ST_TABLE_3, ST_TABLE_4,
  87. ST_TABLE_5, ST_TABLE_6
  88. } lexstate = ST_NOTSTARTED;
  89. static int suppress_type_lookup, dont_want_brace_phrase;
  90. static struct string_list *next_node;
  91. int token, count = 0;
  92. struct string_list *cur_node;
  93. if (lexstate == ST_NOTSTARTED)
  94. {
  95. BEGIN(V2_TOKENS);
  96. next_node = xmalloc(sizeof(*next_node));
  97. next_node->next = NULL;
  98. lexstate = ST_NORMAL;
  99. }
  100. repeat:
  101. token = yylex1();
  102. if (token == 0)
  103. return 0;
  104. else if (token == FILENAME)
  105. {
  106. char *file, *e;
  107. /* Save the filename and line number for later error messages. */
  108. if (cur_filename)
  109. free(cur_filename);
  110. file = strchr(yytext, '\"')+1;
  111. e = strchr(file, '\"');
  112. *e = '\0';
  113. cur_filename = memcpy(xmalloc(e-file+1), file, e-file+1);
  114. cur_line = atoi(yytext+2);
  115. goto repeat;
  116. }
  117. switch (lexstate)
  118. {
  119. case ST_NORMAL:
  120. switch (token)
  121. {
  122. case IDENT:
  123. APP;
  124. {
  125. const struct resword *r = is_reserved_word(yytext, yyleng);
  126. if (r)
  127. {
  128. switch (token = r->token)
  129. {
  130. case ATTRIBUTE_KEYW:
  131. lexstate = ST_ATTRIBUTE;
  132. count = 0;
  133. goto repeat;
  134. case ASM_KEYW:
  135. lexstate = ST_ASM;
  136. count = 0;
  137. goto repeat;
  138. case STRUCT_KEYW:
  139. case UNION_KEYW:
  140. dont_want_brace_phrase = 3;
  141. case ENUM_KEYW:
  142. suppress_type_lookup = 2;
  143. goto fini;
  144. case EXPORT_SYMBOL_KEYW:
  145. goto fini;
  146. }
  147. }
  148. if (!suppress_type_lookup)
  149. {
  150. struct symbol *sym = find_symbol(yytext, SYM_TYPEDEF);
  151. if (sym && sym->type == SYM_TYPEDEF)
  152. token = TYPE;
  153. }
  154. }
  155. break;
  156. case '[':
  157. APP;
  158. lexstate = ST_BRACKET;
  159. count = 1;
  160. goto repeat;
  161. case '{':
  162. APP;
  163. if (dont_want_brace_phrase)
  164. break;
  165. lexstate = ST_BRACE;
  166. count = 1;
  167. goto repeat;
  168. case '=': case ':':
  169. APP;
  170. lexstate = ST_EXPRESSION;
  171. break;
  172. case DOTS:
  173. default:
  174. APP;
  175. break;
  176. }
  177. break;
  178. case ST_ATTRIBUTE:
  179. APP;
  180. switch (token)
  181. {
  182. case '(':
  183. ++count;
  184. goto repeat;
  185. case ')':
  186. if (--count == 0)
  187. {
  188. lexstate = ST_NORMAL;
  189. token = ATTRIBUTE_PHRASE;
  190. break;
  191. }
  192. goto repeat;
  193. default:
  194. goto repeat;
  195. }
  196. break;
  197. case ST_ASM:
  198. APP;
  199. switch (token)
  200. {
  201. case '(':
  202. ++count;
  203. goto repeat;
  204. case ')':
  205. if (--count == 0)
  206. {
  207. lexstate = ST_NORMAL;
  208. token = ASM_PHRASE;
  209. break;
  210. }
  211. goto repeat;
  212. default:
  213. goto repeat;
  214. }
  215. break;
  216. case ST_BRACKET:
  217. APP;
  218. switch (token)
  219. {
  220. case '[':
  221. ++count;
  222. goto repeat;
  223. case ']':
  224. if (--count == 0)
  225. {
  226. lexstate = ST_NORMAL;
  227. token = BRACKET_PHRASE;
  228. break;
  229. }
  230. goto repeat;
  231. default:
  232. goto repeat;
  233. }
  234. break;
  235. case ST_BRACE:
  236. APP;
  237. switch (token)
  238. {
  239. case '{':
  240. ++count;
  241. goto repeat;
  242. case '}':
  243. if (--count == 0)
  244. {
  245. lexstate = ST_NORMAL;
  246. token = BRACE_PHRASE;
  247. break;
  248. }
  249. goto repeat;
  250. default:
  251. goto repeat;
  252. }
  253. break;
  254. case ST_EXPRESSION:
  255. switch (token)
  256. {
  257. case '(': case '[': case '{':
  258. ++count;
  259. APP;
  260. goto repeat;
  261. case ')': case ']': case '}':
  262. --count;
  263. APP;
  264. goto repeat;
  265. case ',': case ';':
  266. if (count == 0)
  267. {
  268. /* Put back the token we just read so's we can find it again
  269. after registering the expression. */
  270. unput(token);
  271. lexstate = ST_NORMAL;
  272. token = EXPRESSION_PHRASE;
  273. break;
  274. }
  275. APP;
  276. goto repeat;
  277. default:
  278. APP;
  279. goto repeat;
  280. }
  281. break;
  282. case ST_TABLE_1:
  283. goto repeat;
  284. case ST_TABLE_2:
  285. if (token == IDENT && yyleng == 1 && yytext[0] == 'X')
  286. {
  287. token = EXPORT_SYMBOL_KEYW;
  288. lexstate = ST_TABLE_5;
  289. APP;
  290. break;
  291. }
  292. lexstate = ST_TABLE_6;
  293. /* FALLTHRU */
  294. case ST_TABLE_6:
  295. switch (token)
  296. {
  297. case '{': case '[': case '(':
  298. ++count;
  299. break;
  300. case '}': case ']': case ')':
  301. --count;
  302. break;
  303. case ',':
  304. if (count == 0)
  305. lexstate = ST_TABLE_2;
  306. break;
  307. };
  308. goto repeat;
  309. case ST_TABLE_3:
  310. goto repeat;
  311. case ST_TABLE_4:
  312. if (token == ';')
  313. lexstate = ST_NORMAL;
  314. goto repeat;
  315. case ST_TABLE_5:
  316. switch (token)
  317. {
  318. case ',':
  319. token = ';';
  320. lexstate = ST_TABLE_2;
  321. APP;
  322. break;
  323. default:
  324. APP;
  325. break;
  326. }
  327. break;
  328. default:
  329. exit(1);
  330. }
  331. fini:
  332. if (suppress_type_lookup > 0)
  333. --suppress_type_lookup;
  334. if (dont_want_brace_phrase > 0)
  335. --dont_want_brace_phrase;
  336. yylval = &next_node->next;
  337. return token;
  338. }