lex.l 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. /*
  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. } while (0)
  86. #define APP _APP(yytext, yyleng)
  87. /* The second stage lexer. Here we incorporate knowledge of the state
  88. of the parser to tailor the tokens that are returned. */
  89. int
  90. yylex(void)
  91. {
  92. static enum {
  93. ST_NOTSTARTED, ST_NORMAL, ST_ATTRIBUTE, ST_ASM, ST_BRACKET, ST_BRACE,
  94. ST_EXPRESSION, ST_TABLE_1, ST_TABLE_2, ST_TABLE_3, ST_TABLE_4,
  95. ST_TABLE_5, ST_TABLE_6
  96. } lexstate = ST_NOTSTARTED;
  97. static int suppress_type_lookup, dont_want_brace_phrase;
  98. static struct string_list *next_node;
  99. int token, count = 0;
  100. struct string_list *cur_node;
  101. if (lexstate == ST_NOTSTARTED)
  102. {
  103. next_node = xmalloc(sizeof(*next_node));
  104. next_node->next = NULL;
  105. lexstate = ST_NORMAL;
  106. }
  107. repeat:
  108. token = yylex1();
  109. if (token == 0)
  110. return 0;
  111. else if (token == FILENAME)
  112. {
  113. char *file, *e;
  114. /* Save the filename and line number for later error messages. */
  115. if (cur_filename)
  116. free(cur_filename);
  117. file = strchr(yytext, '\"')+1;
  118. e = strchr(file, '\"');
  119. *e = '\0';
  120. cur_filename = memcpy(xmalloc(e-file+1), file, e-file+1);
  121. cur_line = atoi(yytext+2);
  122. goto repeat;
  123. }
  124. switch (lexstate)
  125. {
  126. case ST_NORMAL:
  127. switch (token)
  128. {
  129. case IDENT:
  130. APP;
  131. {
  132. const struct resword *r = is_reserved_word(yytext, yyleng);
  133. if (r)
  134. {
  135. switch (token = r->token)
  136. {
  137. case ATTRIBUTE_KEYW:
  138. lexstate = ST_ATTRIBUTE;
  139. count = 0;
  140. goto repeat;
  141. case ASM_KEYW:
  142. lexstate = ST_ASM;
  143. count = 0;
  144. goto repeat;
  145. case STRUCT_KEYW:
  146. case UNION_KEYW:
  147. case ENUM_KEYW:
  148. dont_want_brace_phrase = 3;
  149. suppress_type_lookup = 2;
  150. goto fini;
  151. case EXPORT_SYMBOL_KEYW:
  152. goto fini;
  153. }
  154. }
  155. if (!suppress_type_lookup)
  156. {
  157. if (find_symbol(yytext, SYM_TYPEDEF, 1))
  158. token = TYPE;
  159. }
  160. }
  161. break;
  162. case '[':
  163. APP;
  164. lexstate = ST_BRACKET;
  165. count = 1;
  166. goto repeat;
  167. case '{':
  168. APP;
  169. if (dont_want_brace_phrase)
  170. break;
  171. lexstate = ST_BRACE;
  172. count = 1;
  173. goto repeat;
  174. case '=': case ':':
  175. APP;
  176. lexstate = ST_EXPRESSION;
  177. break;
  178. case DOTS:
  179. default:
  180. APP;
  181. break;
  182. }
  183. break;
  184. case ST_ATTRIBUTE:
  185. APP;
  186. switch (token)
  187. {
  188. case '(':
  189. ++count;
  190. goto repeat;
  191. case ')':
  192. if (--count == 0)
  193. {
  194. lexstate = ST_NORMAL;
  195. token = ATTRIBUTE_PHRASE;
  196. break;
  197. }
  198. goto repeat;
  199. default:
  200. goto repeat;
  201. }
  202. break;
  203. case ST_ASM:
  204. APP;
  205. switch (token)
  206. {
  207. case '(':
  208. ++count;
  209. goto repeat;
  210. case ')':
  211. if (--count == 0)
  212. {
  213. lexstate = ST_NORMAL;
  214. token = ASM_PHRASE;
  215. break;
  216. }
  217. goto repeat;
  218. default:
  219. goto repeat;
  220. }
  221. break;
  222. case ST_BRACKET:
  223. APP;
  224. switch (token)
  225. {
  226. case '[':
  227. ++count;
  228. goto repeat;
  229. case ']':
  230. if (--count == 0)
  231. {
  232. lexstate = ST_NORMAL;
  233. token = BRACKET_PHRASE;
  234. break;
  235. }
  236. goto repeat;
  237. default:
  238. goto repeat;
  239. }
  240. break;
  241. case ST_BRACE:
  242. APP;
  243. switch (token)
  244. {
  245. case '{':
  246. ++count;
  247. goto repeat;
  248. case '}':
  249. if (--count == 0)
  250. {
  251. lexstate = ST_NORMAL;
  252. token = BRACE_PHRASE;
  253. break;
  254. }
  255. goto repeat;
  256. default:
  257. goto repeat;
  258. }
  259. break;
  260. case ST_EXPRESSION:
  261. switch (token)
  262. {
  263. case '(': case '[': case '{':
  264. ++count;
  265. APP;
  266. goto repeat;
  267. case '}':
  268. /* is this the last line of an enum declaration? */
  269. if (count == 0)
  270. {
  271. /* Put back the token we just read so's we can find it again
  272. after registering the expression. */
  273. unput(token);
  274. lexstate = ST_NORMAL;
  275. token = EXPRESSION_PHRASE;
  276. break;
  277. }
  278. /* FALLTHRU */
  279. case ')': case ']':
  280. --count;
  281. APP;
  282. goto repeat;
  283. case ',': case ';':
  284. if (count == 0)
  285. {
  286. /* Put back the token we just read so's we can find it again
  287. after registering the expression. */
  288. unput(token);
  289. lexstate = ST_NORMAL;
  290. token = EXPRESSION_PHRASE;
  291. break;
  292. }
  293. APP;
  294. goto repeat;
  295. default:
  296. APP;
  297. goto repeat;
  298. }
  299. break;
  300. case ST_TABLE_1:
  301. goto repeat;
  302. case ST_TABLE_2:
  303. if (token == IDENT && yyleng == 1 && yytext[0] == 'X')
  304. {
  305. token = EXPORT_SYMBOL_KEYW;
  306. lexstate = ST_TABLE_5;
  307. APP;
  308. break;
  309. }
  310. lexstate = ST_TABLE_6;
  311. /* FALLTHRU */
  312. case ST_TABLE_6:
  313. switch (token)
  314. {
  315. case '{': case '[': case '(':
  316. ++count;
  317. break;
  318. case '}': case ']': case ')':
  319. --count;
  320. break;
  321. case ',':
  322. if (count == 0)
  323. lexstate = ST_TABLE_2;
  324. break;
  325. };
  326. goto repeat;
  327. case ST_TABLE_3:
  328. goto repeat;
  329. case ST_TABLE_4:
  330. if (token == ';')
  331. lexstate = ST_NORMAL;
  332. goto repeat;
  333. case ST_TABLE_5:
  334. switch (token)
  335. {
  336. case ',':
  337. token = ';';
  338. lexstate = ST_TABLE_2;
  339. APP;
  340. break;
  341. default:
  342. APP;
  343. break;
  344. }
  345. break;
  346. default:
  347. exit(1);
  348. }
  349. fini:
  350. if (suppress_type_lookup > 0)
  351. --suppress_type_lookup;
  352. if (dont_want_brace_phrase > 0)
  353. --dont_want_brace_phrase;
  354. yylval = &next_node->next;
  355. return token;
  356. }