lex.l 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 <[email protected]>
  7. * Based on original work by Bjorn Ekwall <[email protected]>
  8. *
  9. * Taken from Linux modutils 2.4.22.
  10. */
  11. %{
  12. #include <limits.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include "genksyms.h"
  17. #include "parse.tab.h"
  18. /* We've got a two-level lexer here. We let flex do basic tokenization
  19. and then we categorize those basic tokens in the second stage. */
  20. #define YY_DECL static int yylex1(void)
  21. %}
  22. IDENT [A-Za-z_\$][A-Za-z0-9_\$]*
  23. O_INT 0[0-7]*
  24. D_INT [1-9][0-9]*
  25. X_INT 0[Xx][0-9A-Fa-f]+
  26. I_SUF [Uu]|[Ll]|[Uu][Ll]|[Ll][Uu]
  27. INT ({O_INT}|{D_INT}|{X_INT}){I_SUF}?
  28. FRAC ([0-9]*\.[0-9]+)|([0-9]+\.)
  29. EXP [Ee][+-]?[0-9]+
  30. F_SUF [FfLl]
  31. REAL ({FRAC}{EXP}?{F_SUF}?)|([0-9]+{EXP}{F_SUF}?)
  32. STRING L?\"([^\\\"]*\\.)*[^\\\"]*\"
  33. CHAR L?\'([^\\\']*\\.)*[^\\\']*\'
  34. MC_TOKEN ([~%^&*+=|<>/-]=)|(&&)|("||")|(->)|(<<)|(>>)
  35. /* We don't do multiple input files. */
  36. %option noyywrap
  37. %option noinput
  38. %%
  39. /* Keep track of our location in the original source files. */
  40. ^#[ \t]+{INT}[ \t]+\"[^\"\n]+\".*\n return FILENAME;
  41. ^#.*\n cur_line++;
  42. \n cur_line++;
  43. /* Ignore all other whitespace. */
  44. [ \t\f\v\r]+ ;
  45. {STRING} return STRING;
  46. {CHAR} return CHAR;
  47. {IDENT} return IDENT;
  48. /* The Pedant requires that the other C multi-character tokens be
  49. recognized as tokens. We don't actually use them since we don't
  50. parse expressions, but we do want whitespace to be arranged
  51. around them properly. */
  52. {MC_TOKEN} return OTHER;
  53. {INT} return INT;
  54. {REAL} return REAL;
  55. "..." return DOTS;
  56. /* All other tokens are single characters. */
  57. . return yytext[0];
  58. %%
  59. /* Bring in the keyword recognizer. */
  60. #include "keywords.c"
  61. /* Macros to append to our phrase collection list. */
  62. /*
  63. * We mark any token, that that equals to a known enumerator, as
  64. * SYM_ENUM_CONST. The parser will change this for struct and union tags later,
  65. * the only problem is struct and union members:
  66. * enum e { a, b }; struct s { int a, b; }
  67. * but in this case, the only effect will be, that the ABI checksums become
  68. * more volatile, which is acceptable. Also, such collisions are quite rare,
  69. * so far it was only observed in include/linux/telephony.h.
  70. */
  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 = \
  77. find_symbol(cur_node->string, SYM_ENUM_CONST, 1)?\
  78. SYM_ENUM_CONST : SYM_NORMAL ; \
  79. cur_node->in_source_file = in_source_file; \
  80. } while (0)
  81. #define APP _APP(yytext, yyleng)
  82. /* The second stage lexer. Here we incorporate knowledge of the state
  83. of the parser to tailor the tokens that are returned. */
  84. int
  85. yylex(void)
  86. {
  87. static enum {
  88. ST_NOTSTARTED, ST_NORMAL, ST_ATTRIBUTE, ST_ASM, ST_TYPEOF, ST_TYPEOF_1,
  89. ST_BRACKET, ST_BRACE, ST_EXPRESSION, ST_STATIC_ASSERT,
  90. } lexstate = ST_NOTSTARTED;
  91. static int suppress_type_lookup, dont_want_brace_phrase;
  92. static struct string_list *next_node;
  93. static char *source_file;
  94. int token, count = 0;
  95. struct string_list *cur_node;
  96. if (lexstate == ST_NOTSTARTED)
  97. {
  98. next_node = xmalloc(sizeof(*next_node));
  99. next_node->next = NULL;
  100. lexstate = ST_NORMAL;
  101. }
  102. repeat:
  103. token = yylex1();
  104. if (token == 0)
  105. return 0;
  106. else if (token == FILENAME)
  107. {
  108. char *file, *e;
  109. /* Save the filename and line number for later error messages. */
  110. if (cur_filename)
  111. free(cur_filename);
  112. file = strchr(yytext, '\"')+1;
  113. e = strchr(file, '\"');
  114. *e = '\0';
  115. cur_filename = memcpy(xmalloc(e-file+1), file, e-file+1);
  116. cur_line = atoi(yytext+2);
  117. if (!source_file) {
  118. source_file = xstrdup(cur_filename);
  119. in_source_file = 1;
  120. } else {
  121. in_source_file = (strcmp(cur_filename, source_file) == 0);
  122. }
  123. goto repeat;
  124. }
  125. switch (lexstate)
  126. {
  127. case ST_NORMAL:
  128. switch (token)
  129. {
  130. case IDENT:
  131. APP;
  132. {
  133. int r = is_reserved_word(yytext, yyleng);
  134. if (r >= 0)
  135. {
  136. switch (token = r)
  137. {
  138. case ATTRIBUTE_KEYW:
  139. lexstate = ST_ATTRIBUTE;
  140. count = 0;
  141. goto repeat;
  142. case ASM_KEYW:
  143. lexstate = ST_ASM;
  144. count = 0;
  145. goto repeat;
  146. case TYPEOF_KEYW:
  147. lexstate = ST_TYPEOF;
  148. count = 0;
  149. goto repeat;
  150. case STRUCT_KEYW:
  151. case UNION_KEYW:
  152. case ENUM_KEYW:
  153. dont_want_brace_phrase = 3;
  154. suppress_type_lookup = 2;
  155. goto fini;
  156. case EXPORT_SYMBOL_KEYW:
  157. goto fini;
  158. case STATIC_ASSERT_KEYW:
  159. lexstate = ST_STATIC_ASSERT;
  160. count = 0;
  161. goto repeat;
  162. }
  163. }
  164. if (!suppress_type_lookup)
  165. {
  166. if (find_symbol(yytext, SYM_TYPEDEF, 1))
  167. token = TYPE;
  168. }
  169. }
  170. break;
  171. case '[':
  172. APP;
  173. lexstate = ST_BRACKET;
  174. count = 1;
  175. goto repeat;
  176. case '{':
  177. APP;
  178. if (dont_want_brace_phrase)
  179. break;
  180. lexstate = ST_BRACE;
  181. count = 1;
  182. goto repeat;
  183. case '=': case ':':
  184. APP;
  185. lexstate = ST_EXPRESSION;
  186. break;
  187. default:
  188. APP;
  189. break;
  190. }
  191. break;
  192. case ST_ATTRIBUTE:
  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 = ATTRIBUTE_PHRASE;
  204. break;
  205. }
  206. goto repeat;
  207. default:
  208. goto repeat;
  209. }
  210. break;
  211. case ST_ASM:
  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 = ASM_PHRASE;
  223. break;
  224. }
  225. goto repeat;
  226. default:
  227. goto repeat;
  228. }
  229. break;
  230. case ST_TYPEOF_1:
  231. if (token == IDENT)
  232. {
  233. if (is_reserved_word(yytext, yyleng) >= 0
  234. || find_symbol(yytext, SYM_TYPEDEF, 1))
  235. {
  236. yyless(0);
  237. unput('(');
  238. lexstate = ST_NORMAL;
  239. token = TYPEOF_KEYW;
  240. break;
  241. }
  242. _APP("(", 1);
  243. }
  244. lexstate = ST_TYPEOF;
  245. /* FALLTHRU */
  246. case ST_TYPEOF:
  247. switch (token)
  248. {
  249. case '(':
  250. if ( ++count == 1 )
  251. lexstate = ST_TYPEOF_1;
  252. else
  253. APP;
  254. goto repeat;
  255. case ')':
  256. APP;
  257. if (--count == 0)
  258. {
  259. lexstate = ST_NORMAL;
  260. token = TYPEOF_PHRASE;
  261. break;
  262. }
  263. goto repeat;
  264. default:
  265. APP;
  266. goto repeat;
  267. }
  268. break;
  269. case ST_BRACKET:
  270. APP;
  271. switch (token)
  272. {
  273. case '[':
  274. ++count;
  275. goto repeat;
  276. case ']':
  277. if (--count == 0)
  278. {
  279. lexstate = ST_NORMAL;
  280. token = BRACKET_PHRASE;
  281. break;
  282. }
  283. goto repeat;
  284. default:
  285. goto repeat;
  286. }
  287. break;
  288. case ST_BRACE:
  289. APP;
  290. switch (token)
  291. {
  292. case '{':
  293. ++count;
  294. goto repeat;
  295. case '}':
  296. if (--count == 0)
  297. {
  298. lexstate = ST_NORMAL;
  299. token = BRACE_PHRASE;
  300. break;
  301. }
  302. goto repeat;
  303. default:
  304. goto repeat;
  305. }
  306. break;
  307. case ST_EXPRESSION:
  308. switch (token)
  309. {
  310. case '(': case '[': case '{':
  311. ++count;
  312. APP;
  313. goto repeat;
  314. case '}':
  315. /* is this the last line of an enum declaration? */
  316. if (count == 0)
  317. {
  318. /* Put back the token we just read so's we can find it again
  319. after registering the expression. */
  320. unput(token);
  321. lexstate = ST_NORMAL;
  322. token = EXPRESSION_PHRASE;
  323. break;
  324. }
  325. /* FALLTHRU */
  326. case ')': case ']':
  327. --count;
  328. APP;
  329. goto repeat;
  330. case ',': case ';':
  331. if (count == 0)
  332. {
  333. /* Put back the token we just read so's we can find it again
  334. after registering the expression. */
  335. unput(token);
  336. lexstate = ST_NORMAL;
  337. token = EXPRESSION_PHRASE;
  338. break;
  339. }
  340. APP;
  341. goto repeat;
  342. default:
  343. APP;
  344. goto repeat;
  345. }
  346. break;
  347. case ST_STATIC_ASSERT:
  348. APP;
  349. switch (token)
  350. {
  351. case '(':
  352. ++count;
  353. goto repeat;
  354. case ')':
  355. if (--count == 0)
  356. {
  357. lexstate = ST_NORMAL;
  358. token = STATIC_ASSERT_PHRASE;
  359. break;
  360. }
  361. goto repeat;
  362. default:
  363. goto repeat;
  364. }
  365. break;
  366. default:
  367. exit(1);
  368. }
  369. fini:
  370. if (suppress_type_lookup > 0)
  371. --suppress_type_lookup;
  372. if (dont_want_brace_phrase > 0)
  373. --dont_want_brace_phrase;
  374. yylval = &next_node->next;
  375. return token;
  376. }