lex.l 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Lexical analysis for genksyms.
  4. * Copyright 1996, 1997 Linux International.
  5. *
  6. * New implementation contributed by Richard Henderson <rth@tamu.edu>
  7. * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
  8. *
  9. * Taken from Linux modutils 2.4.22.
  10. */
  11. %{
  12. #include <limits.h>
  13. #include <stdbool.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include "genksyms.h"
  18. #include "parse.tab.h"
  19. /* We've got a two-level lexer here. We let flex do basic tokenization
  20. and then we categorize those basic tokens in the second stage. */
  21. #define YY_DECL static int yylex1(void)
  22. %}
  23. IDENT [A-Za-z_\$][A-Za-z0-9_\$]*
  24. O_INT 0[0-7]*
  25. D_INT [1-9][0-9]*
  26. X_INT 0[Xx][0-9A-Fa-f]+
  27. I_SUF [Uu]|[Ll]|[Uu][Ll]|[Ll][Uu]
  28. INT ({O_INT}|{D_INT}|{X_INT}){I_SUF}?
  29. FRAC ([0-9]*\.[0-9]+)|([0-9]+\.)
  30. EXP [Ee][+-]?[0-9]+
  31. F_SUF [FfLl]
  32. REAL ({FRAC}{EXP}?{F_SUF}?)|([0-9]+{EXP}{F_SUF}?)
  33. STRING L?\"([^\\\"]*\\.)*[^\\\"]*\"
  34. CHAR L?\'([^\\\']*\\.)*[^\\\']*\'
  35. MC_TOKEN ([~%^&*+=|<>/-]=)|(&&)|("||")|(->)|(<<)|(>>)
  36. /* We don't do multiple input files. */
  37. %option noyywrap
  38. %option noinput
  39. %%
  40. u?int(8|16|32|64)x(1|2|4|8|16)_t return BUILTIN_INT_KEYW;
  41. /* Keep track of our location in the original source files. */
  42. ^#[ \t]+{INT}[ \t]+\"[^\"\n]+\".*\n return FILENAME;
  43. ^#.*\n cur_line++;
  44. \n cur_line++;
  45. /* Ignore all other whitespace. */
  46. [ \t\f\v\r]+ ;
  47. {STRING} return STRING;
  48. {CHAR} return CHAR;
  49. {IDENT} return IDENT;
  50. /* The Pedant requires that the other C multi-character tokens be
  51. recognized as tokens. We don't actually use them since we don't
  52. parse expressions, but we do want whitespace to be arranged
  53. around them properly. */
  54. {MC_TOKEN} return OTHER;
  55. {INT} return INT;
  56. {REAL} return REAL;
  57. "..." return DOTS;
  58. /* All other tokens are single characters. */
  59. . return yytext[0];
  60. %%
  61. /* Bring in the keyword recognizer. */
  62. #include "keywords.c"
  63. /* Macros to append to our phrase collection list. */
  64. /*
  65. * We mark any token, that that equals to a known enumerator, as
  66. * SYM_ENUM_CONST. The parser will change this for struct and union tags later,
  67. * the only problem is struct and union members:
  68. * enum e { a, b }; struct s { int a, b; }
  69. * but in this case, the only effect will be, that the ABI checksums become
  70. * more volatile, which is acceptable. Also, such collisions are quite rare,
  71. * so far it was only observed in include/linux/telephony.h.
  72. */
  73. #define _APP(T,L) do { \
  74. cur_node = next_node; \
  75. next_node = xmalloc(sizeof(*next_node)); \
  76. next_node->next = cur_node; \
  77. cur_node->string = memcpy(xmalloc(L+1), T, L+1); \
  78. cur_node->tag = \
  79. find_symbol(cur_node->string, SYM_ENUM_CONST, 1)?\
  80. SYM_ENUM_CONST : SYM_NORMAL ; \
  81. cur_node->in_source_file = in_source_file; \
  82. } while (0)
  83. #define APP _APP(yytext, yyleng)
  84. /* The second stage lexer. Here we incorporate knowledge of the state
  85. of the parser to tailor the tokens that are returned. */
  86. /*
  87. * The lexer cannot distinguish whether a typedef'ed string is a TYPE or an
  88. * IDENT. We need a hint from the parser to handle this accurately.
  89. */
  90. bool dont_want_type_specifier;
  91. int
  92. yylex(void)
  93. {
  94. static enum {
  95. ST_NOTSTARTED, ST_NORMAL, ST_ATTRIBUTE, ST_ASM, ST_TYPEOF, ST_TYPEOF_1,
  96. ST_BRACKET, ST_BRACE, ST_EXPRESSION, ST_STATIC_ASSERT,
  97. } lexstate = ST_NOTSTARTED;
  98. static int suppress_type_lookup, dont_want_brace_phrase;
  99. static struct string_list *next_node;
  100. static char *source_file;
  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. APP;
  136. switch (token)
  137. {
  138. case IDENT:
  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. case STATIC_ASSERT_KEYW:
  166. lexstate = ST_STATIC_ASSERT;
  167. count = 0;
  168. goto repeat;
  169. }
  170. }
  171. if (!suppress_type_lookup && !dont_want_type_specifier)
  172. {
  173. if (find_symbol(yytext, SYM_TYPEDEF, 1))
  174. token = TYPE;
  175. }
  176. }
  177. break;
  178. case '[':
  179. lexstate = ST_BRACKET;
  180. count = 1;
  181. goto repeat;
  182. case '{':
  183. if (dont_want_brace_phrase)
  184. break;
  185. lexstate = ST_BRACE;
  186. count = 1;
  187. goto repeat;
  188. case '=': case ':':
  189. lexstate = ST_EXPRESSION;
  190. break;
  191. default:
  192. break;
  193. }
  194. break;
  195. case ST_ATTRIBUTE:
  196. APP;
  197. switch (token)
  198. {
  199. case '(':
  200. ++count;
  201. goto repeat;
  202. case ')':
  203. if (--count == 0)
  204. {
  205. lexstate = ST_NORMAL;
  206. token = ATTRIBUTE_PHRASE;
  207. break;
  208. }
  209. goto repeat;
  210. default:
  211. goto repeat;
  212. }
  213. break;
  214. case ST_ASM:
  215. APP;
  216. switch (token)
  217. {
  218. case '(':
  219. ++count;
  220. goto repeat;
  221. case ')':
  222. if (--count == 0)
  223. {
  224. lexstate = ST_NORMAL;
  225. token = ASM_PHRASE;
  226. break;
  227. }
  228. goto repeat;
  229. default:
  230. goto repeat;
  231. }
  232. break;
  233. case ST_TYPEOF_1:
  234. if (token == IDENT)
  235. {
  236. if (is_reserved_word(yytext, yyleng) >= 0
  237. || find_symbol(yytext, SYM_TYPEDEF, 1))
  238. {
  239. yyless(0);
  240. unput('(');
  241. lexstate = ST_NORMAL;
  242. token = TYPEOF_KEYW;
  243. break;
  244. }
  245. _APP("(", 1);
  246. }
  247. lexstate = ST_TYPEOF;
  248. /* FALLTHRU */
  249. case ST_TYPEOF:
  250. switch (token)
  251. {
  252. case '(':
  253. if ( ++count == 1 )
  254. lexstate = ST_TYPEOF_1;
  255. else
  256. APP;
  257. goto repeat;
  258. case ')':
  259. APP;
  260. if (--count == 0)
  261. {
  262. lexstate = ST_NORMAL;
  263. token = TYPEOF_PHRASE;
  264. break;
  265. }
  266. goto repeat;
  267. default:
  268. APP;
  269. goto repeat;
  270. }
  271. break;
  272. case ST_BRACKET:
  273. APP;
  274. switch (token)
  275. {
  276. case '[':
  277. ++count;
  278. goto repeat;
  279. case ']':
  280. if (--count == 0)
  281. {
  282. lexstate = ST_NORMAL;
  283. token = BRACKET_PHRASE;
  284. break;
  285. }
  286. goto repeat;
  287. default:
  288. goto repeat;
  289. }
  290. break;
  291. case ST_BRACE:
  292. APP;
  293. switch (token)
  294. {
  295. case '{':
  296. ++count;
  297. goto repeat;
  298. case '}':
  299. if (--count == 0)
  300. {
  301. lexstate = ST_NORMAL;
  302. token = BRACE_PHRASE;
  303. break;
  304. }
  305. goto repeat;
  306. default:
  307. goto repeat;
  308. }
  309. break;
  310. case ST_EXPRESSION:
  311. switch (token)
  312. {
  313. case '(': case '[': case '{':
  314. ++count;
  315. APP;
  316. goto repeat;
  317. case '}':
  318. /* is this the last line of an enum declaration? */
  319. if (count == 0)
  320. {
  321. /* Put back the token we just read so's we can find it again
  322. after registering the expression. */
  323. unput(token);
  324. lexstate = ST_NORMAL;
  325. token = EXPRESSION_PHRASE;
  326. break;
  327. }
  328. /* FALLTHRU */
  329. case ')': case ']':
  330. --count;
  331. APP;
  332. goto repeat;
  333. case ',': case ';':
  334. if (count == 0)
  335. {
  336. /* Put back the token we just read so's we can find it again
  337. after registering the expression. */
  338. unput(token);
  339. lexstate = ST_NORMAL;
  340. token = EXPRESSION_PHRASE;
  341. break;
  342. }
  343. APP;
  344. goto repeat;
  345. default:
  346. APP;
  347. goto repeat;
  348. }
  349. break;
  350. case ST_STATIC_ASSERT:
  351. APP;
  352. switch (token)
  353. {
  354. case '(':
  355. ++count;
  356. goto repeat;
  357. case ')':
  358. if (--count == 0)
  359. {
  360. lexstate = ST_NORMAL;
  361. token = STATIC_ASSERT_PHRASE;
  362. break;
  363. }
  364. goto repeat;
  365. default:
  366. goto repeat;
  367. }
  368. break;
  369. default:
  370. exit(1);
  371. }
  372. fini:
  373. if (suppress_type_lookup > 0)
  374. --suppress_type_lookup;
  375. /*
  376. * __attribute__() can be placed immediately after the 'struct' keyword.
  377. * e.g.) struct __attribute__((__packed__)) foo { ... };
  378. */
  379. if (token != ATTRIBUTE_PHRASE && dont_want_brace_phrase > 0)
  380. --dont_want_brace_phrase;
  381. yylval = &next_node->next;
  382. return token;
  383. }