lex.l 9.3 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.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. int r = is_reserved_word(yytext, yyleng);
  141. if (r >= 0)
  142. {
  143. switch (token = r)
  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_1:
  235. if (token == IDENT)
  236. {
  237. if (is_reserved_word(yytext, yyleng) >= 0
  238. || find_symbol(yytext, SYM_TYPEDEF, 1))
  239. {
  240. yyless(0);
  241. unput('(');
  242. lexstate = ST_NORMAL;
  243. token = TYPEOF_KEYW;
  244. break;
  245. }
  246. _APP("(", 1);
  247. }
  248. lexstate = ST_TYPEOF;
  249. /* FALLTHRU */
  250. case ST_TYPEOF:
  251. switch (token)
  252. {
  253. case '(':
  254. if ( ++count == 1 )
  255. lexstate = ST_TYPEOF_1;
  256. else
  257. APP;
  258. goto repeat;
  259. case ')':
  260. APP;
  261. if (--count == 0)
  262. {
  263. lexstate = ST_NORMAL;
  264. token = TYPEOF_PHRASE;
  265. break;
  266. }
  267. goto repeat;
  268. default:
  269. APP;
  270. goto repeat;
  271. }
  272. break;
  273. case ST_BRACKET:
  274. APP;
  275. switch (token)
  276. {
  277. case '[':
  278. ++count;
  279. goto repeat;
  280. case ']':
  281. if (--count == 0)
  282. {
  283. lexstate = ST_NORMAL;
  284. token = BRACKET_PHRASE;
  285. break;
  286. }
  287. goto repeat;
  288. default:
  289. goto repeat;
  290. }
  291. break;
  292. case ST_BRACE:
  293. APP;
  294. switch (token)
  295. {
  296. case '{':
  297. ++count;
  298. goto repeat;
  299. case '}':
  300. if (--count == 0)
  301. {
  302. lexstate = ST_NORMAL;
  303. token = BRACE_PHRASE;
  304. break;
  305. }
  306. goto repeat;
  307. default:
  308. goto repeat;
  309. }
  310. break;
  311. case ST_EXPRESSION:
  312. switch (token)
  313. {
  314. case '(': case '[': case '{':
  315. ++count;
  316. APP;
  317. goto repeat;
  318. case '}':
  319. /* is this the last line of an enum declaration? */
  320. if (count == 0)
  321. {
  322. /* Put back the token we just read so's we can find it again
  323. after registering the expression. */
  324. unput(token);
  325. lexstate = ST_NORMAL;
  326. token = EXPRESSION_PHRASE;
  327. break;
  328. }
  329. /* FALLTHRU */
  330. case ')': case ']':
  331. --count;
  332. APP;
  333. goto repeat;
  334. case ',': case ';':
  335. if (count == 0)
  336. {
  337. /* Put back the token we just read so's we can find it again
  338. after registering the expression. */
  339. unput(token);
  340. lexstate = ST_NORMAL;
  341. token = EXPRESSION_PHRASE;
  342. break;
  343. }
  344. APP;
  345. goto repeat;
  346. default:
  347. APP;
  348. goto repeat;
  349. }
  350. break;
  351. case ST_TABLE_1:
  352. goto repeat;
  353. case ST_TABLE_2:
  354. if (token == IDENT && yyleng == 1 && yytext[0] == 'X')
  355. {
  356. token = EXPORT_SYMBOL_KEYW;
  357. lexstate = ST_TABLE_5;
  358. APP;
  359. break;
  360. }
  361. lexstate = ST_TABLE_6;
  362. /* FALLTHRU */
  363. case ST_TABLE_6:
  364. switch (token)
  365. {
  366. case '{': case '[': case '(':
  367. ++count;
  368. break;
  369. case '}': case ']': case ')':
  370. --count;
  371. break;
  372. case ',':
  373. if (count == 0)
  374. lexstate = ST_TABLE_2;
  375. break;
  376. };
  377. goto repeat;
  378. case ST_TABLE_3:
  379. goto repeat;
  380. case ST_TABLE_4:
  381. if (token == ';')
  382. lexstate = ST_NORMAL;
  383. goto repeat;
  384. case ST_TABLE_5:
  385. switch (token)
  386. {
  387. case ',':
  388. token = ';';
  389. lexstate = ST_TABLE_2;
  390. APP;
  391. break;
  392. default:
  393. APP;
  394. break;
  395. }
  396. break;
  397. default:
  398. exit(1);
  399. }
  400. fini:
  401. if (suppress_type_lookup > 0)
  402. --suppress_type_lookup;
  403. if (dont_want_brace_phrase > 0)
  404. --dont_want_brace_phrase;
  405. yylval = &next_node->next;
  406. return token;
  407. }