lex.l 7.5 KB

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