lex.l 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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.tab.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.hash.c"
  67. /* Macros to append to our phrase collection list. */
  68. /*
  69. * We mark any token, that that equals to a known enumerator, as
  70. * SYM_ENUM_CONST. The parser will change this for struct and union tags later,
  71. * the only problem is struct and union members:
  72. * enum e { a, b }; struct s { int a, b; }
  73. * but in this case, the only effect will be, that the ABI checksums become
  74. * more volatile, which is acceptable. Also, such collisions are quite rare,
  75. * so far it was only observed in include/linux/telephony.h.
  76. */
  77. #define _APP(T,L) do { \
  78. cur_node = next_node; \
  79. next_node = xmalloc(sizeof(*next_node)); \
  80. next_node->next = cur_node; \
  81. cur_node->string = memcpy(xmalloc(L+1), T, L+1); \
  82. cur_node->tag = \
  83. find_symbol(cur_node->string, SYM_ENUM_CONST, 1)?\
  84. SYM_ENUM_CONST : SYM_NORMAL ; \
  85. cur_node->in_source_file = in_source_file; \
  86. } while (0)
  87. #define APP _APP(yytext, yyleng)
  88. /* The second stage lexer. Here we incorporate knowledge of the state
  89. of the parser to tailor the tokens that are returned. */
  90. int
  91. yylex(void)
  92. {
  93. static enum {
  94. ST_NOTSTARTED, ST_NORMAL, ST_ATTRIBUTE, ST_ASM, ST_TYPEOF, ST_TYPEOF_1,
  95. ST_BRACKET, ST_BRACE, ST_EXPRESSION,
  96. ST_TABLE_1, ST_TABLE_2, ST_TABLE_3, ST_TABLE_4,
  97. ST_TABLE_5, ST_TABLE_6
  98. } lexstate = ST_NOTSTARTED;
  99. static int suppress_type_lookup, dont_want_brace_phrase;
  100. static struct string_list *next_node;
  101. int token, count = 0;
  102. struct string_list *cur_node;
  103. if (lexstate == ST_NOTSTARTED)
  104. {
  105. next_node = xmalloc(sizeof(*next_node));
  106. next_node->next = NULL;
  107. lexstate = ST_NORMAL;
  108. }
  109. repeat:
  110. token = yylex1();
  111. if (token == 0)
  112. return 0;
  113. else if (token == FILENAME)
  114. {
  115. char *file, *e;
  116. /* Save the filename and line number for later error messages. */
  117. if (cur_filename)
  118. free(cur_filename);
  119. file = strchr(yytext, '\"')+1;
  120. e = strchr(file, '\"');
  121. *e = '\0';
  122. cur_filename = memcpy(xmalloc(e-file+1), file, e-file+1);
  123. cur_line = atoi(yytext+2);
  124. if (!source_file) {
  125. source_file = xstrdup(cur_filename);
  126. in_source_file = 1;
  127. } else {
  128. in_source_file = (strcmp(cur_filename, source_file) == 0);
  129. }
  130. goto repeat;
  131. }
  132. switch (lexstate)
  133. {
  134. case ST_NORMAL:
  135. switch (token)
  136. {
  137. case IDENT:
  138. APP;
  139. {
  140. const struct resword *r = is_reserved_word(yytext, yyleng);
  141. if (r)
  142. {
  143. switch (token = r->token)
  144. {
  145. case ATTRIBUTE_KEYW:
  146. lexstate = ST_ATTRIBUTE;
  147. count = 0;
  148. goto repeat;
  149. case ASM_KEYW:
  150. lexstate = ST_ASM;
  151. count = 0;
  152. goto repeat;
  153. case TYPEOF_KEYW:
  154. lexstate = ST_TYPEOF;
  155. count = 0;
  156. goto repeat;
  157. case STRUCT_KEYW:
  158. case UNION_KEYW:
  159. case ENUM_KEYW:
  160. dont_want_brace_phrase = 3;
  161. suppress_type_lookup = 2;
  162. goto fini;
  163. case EXPORT_SYMBOL_KEYW:
  164. goto fini;
  165. }
  166. }
  167. if (!suppress_type_lookup)
  168. {
  169. if (find_symbol(yytext, SYM_TYPEDEF, 1))
  170. token = TYPE;
  171. }
  172. }
  173. break;
  174. case '[':
  175. APP;
  176. lexstate = ST_BRACKET;
  177. count = 1;
  178. goto repeat;
  179. case '{':
  180. APP;
  181. if (dont_want_brace_phrase)
  182. break;
  183. lexstate = ST_BRACE;
  184. count = 1;
  185. goto repeat;
  186. case '=': case ':':
  187. APP;
  188. lexstate = ST_EXPRESSION;
  189. break;
  190. case DOTS:
  191. default:
  192. APP;
  193. break;
  194. }
  195. break;
  196. case ST_ATTRIBUTE:
  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 = ATTRIBUTE_PHRASE;
  208. break;
  209. }
  210. goto repeat;
  211. default:
  212. goto repeat;
  213. }
  214. break;
  215. case ST_ASM:
  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 = ASM_PHRASE;
  227. break;
  228. }
  229. goto repeat;
  230. default:
  231. goto repeat;
  232. }
  233. break;
  234. case ST_TYPEOF:
  235. switch (token)
  236. {
  237. case '(':
  238. if ( ++count == 1 )
  239. lexstate = ST_TYPEOF_1;
  240. else
  241. APP;
  242. goto repeat;
  243. case ')':
  244. APP;
  245. if (--count == 0)
  246. {
  247. lexstate = ST_NORMAL;
  248. token = TYPEOF_PHRASE;
  249. break;
  250. }
  251. goto repeat;
  252. default:
  253. APP;
  254. goto repeat;
  255. }
  256. break;
  257. case ST_TYPEOF_1:
  258. if (token == IDENT)
  259. {
  260. if (is_reserved_word(yytext, yyleng)
  261. || find_symbol(yytext, SYM_TYPEDEF, 1))
  262. {
  263. yyless(0);
  264. unput('(');
  265. lexstate = ST_NORMAL;
  266. token = TYPEOF_KEYW;
  267. break;
  268. }
  269. _APP("(", 1);
  270. }
  271. APP;
  272. lexstate = ST_TYPEOF;
  273. goto repeat;
  274. case ST_BRACKET:
  275. APP;
  276. switch (token)
  277. {
  278. case '[':
  279. ++count;
  280. goto repeat;
  281. case ']':
  282. if (--count == 0)
  283. {
  284. lexstate = ST_NORMAL;
  285. token = BRACKET_PHRASE;
  286. break;
  287. }
  288. goto repeat;
  289. default:
  290. goto repeat;
  291. }
  292. break;
  293. case ST_BRACE:
  294. APP;
  295. switch (token)
  296. {
  297. case '{':
  298. ++count;
  299. goto repeat;
  300. case '}':
  301. if (--count == 0)
  302. {
  303. lexstate = ST_NORMAL;
  304. token = BRACE_PHRASE;
  305. break;
  306. }
  307. goto repeat;
  308. default:
  309. goto repeat;
  310. }
  311. break;
  312. case ST_EXPRESSION:
  313. switch (token)
  314. {
  315. case '(': case '[': case '{':
  316. ++count;
  317. APP;
  318. goto repeat;
  319. case '}':
  320. /* is this the last line of an enum declaration? */
  321. if (count == 0)
  322. {
  323. /* Put back the token we just read so's we can find it again
  324. after registering the expression. */
  325. unput(token);
  326. lexstate = ST_NORMAL;
  327. token = EXPRESSION_PHRASE;
  328. break;
  329. }
  330. /* FALLTHRU */
  331. case ')': case ']':
  332. --count;
  333. APP;
  334. goto repeat;
  335. case ',': case ';':
  336. if (count == 0)
  337. {
  338. /* Put back the token we just read so's we can find it again
  339. after registering the expression. */
  340. unput(token);
  341. lexstate = ST_NORMAL;
  342. token = EXPRESSION_PHRASE;
  343. break;
  344. }
  345. APP;
  346. goto repeat;
  347. default:
  348. APP;
  349. goto repeat;
  350. }
  351. break;
  352. case ST_TABLE_1:
  353. goto repeat;
  354. case ST_TABLE_2:
  355. if (token == IDENT && yyleng == 1 && yytext[0] == 'X')
  356. {
  357. token = EXPORT_SYMBOL_KEYW;
  358. lexstate = ST_TABLE_5;
  359. APP;
  360. break;
  361. }
  362. lexstate = ST_TABLE_6;
  363. /* FALLTHRU */
  364. case ST_TABLE_6:
  365. switch (token)
  366. {
  367. case '{': case '[': case '(':
  368. ++count;
  369. break;
  370. case '}': case ']': case ')':
  371. --count;
  372. break;
  373. case ',':
  374. if (count == 0)
  375. lexstate = ST_TABLE_2;
  376. break;
  377. };
  378. goto repeat;
  379. case ST_TABLE_3:
  380. goto repeat;
  381. case ST_TABLE_4:
  382. if (token == ';')
  383. lexstate = ST_NORMAL;
  384. goto repeat;
  385. case ST_TABLE_5:
  386. switch (token)
  387. {
  388. case ',':
  389. token = ';';
  390. lexstate = ST_TABLE_2;
  391. APP;
  392. break;
  393. default:
  394. APP;
  395. break;
  396. }
  397. break;
  398. default:
  399. exit(1);
  400. }
  401. fini:
  402. if (suppress_type_lookup > 0)
  403. --suppress_type_lookup;
  404. if (dont_want_brace_phrase > 0)
  405. --dont_want_brace_phrase;
  406. yylval = &next_node->next;
  407. return token;
  408. }