lexer.l 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
  4. */
  5. %option nostdinit noyywrap never-interactive full ecs
  6. %option 8bit nodefault yylineno
  7. %x ASSIGN_VAL HELP STRING
  8. %{
  9. #include <assert.h>
  10. #include <limits.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <xalloc.h>
  15. #include "lkc.h"
  16. #include "preprocess.h"
  17. #include "parser.tab.h"
  18. #define YY_DECL static int yylex1(void)
  19. #define START_STRSIZE 16
  20. /* The Kconfig file currently being parsed. */
  21. const char *cur_filename;
  22. /*
  23. * The line number of the current statement. This does not match yylineno.
  24. * yylineno is used by the lexer, while cur_lineno is used by the parser.
  25. */
  26. int cur_lineno;
  27. static int prev_prev_token = T_EOL;
  28. static int prev_token = T_EOL;
  29. static char *text;
  30. static int text_size, text_asize;
  31. struct buffer {
  32. struct buffer *parent;
  33. YY_BUFFER_STATE state;
  34. int yylineno;
  35. const char *filename;
  36. int source_lineno;
  37. };
  38. static struct buffer *current_buf;
  39. static int last_ts, first_ts;
  40. static char *expand_token(const char *in, size_t n);
  41. static void append_expanded_string(const char *in);
  42. static void zconf_endhelp(void);
  43. static void zconf_endfile(void);
  44. static void new_string(void)
  45. {
  46. text = xmalloc(START_STRSIZE);
  47. text_asize = START_STRSIZE;
  48. text_size = 0;
  49. *text = 0;
  50. }
  51. static void append_string(const char *str, int size)
  52. {
  53. int new_size = text_size + size + 1;
  54. if (new_size > text_asize) {
  55. new_size += START_STRSIZE - 1;
  56. new_size &= -START_STRSIZE;
  57. text = xrealloc(text, new_size);
  58. text_asize = new_size;
  59. }
  60. memcpy(text + text_size, str, size);
  61. text_size += size;
  62. text[text_size] = 0;
  63. }
  64. static void alloc_string(const char *str, int size)
  65. {
  66. text = xmalloc(size + 1);
  67. memcpy(text, str, size);
  68. text[size] = 0;
  69. }
  70. static void warn_ignored_character(char chr)
  71. {
  72. fprintf(stderr,
  73. "%s:%d:warning: ignoring unsupported character '%c'\n",
  74. cur_filename, yylineno, chr);
  75. }
  76. %}
  77. n [A-Za-z0-9_-]
  78. %%
  79. char open_quote = 0;
  80. #.* /* ignore comment */
  81. [ \t]* /* whitespaces */
  82. \\\n /* escaped new line */
  83. \n return T_EOL;
  84. "bool" return T_BOOL;
  85. "choice" return T_CHOICE;
  86. "comment" return T_COMMENT;
  87. "config" return T_CONFIG;
  88. "def_bool" return T_DEF_BOOL;
  89. "def_tristate" return T_DEF_TRISTATE;
  90. "default" return T_DEFAULT;
  91. "depends" return T_DEPENDS;
  92. "endchoice" return T_ENDCHOICE;
  93. "endif" return T_ENDIF;
  94. "endmenu" return T_ENDMENU;
  95. "help" return T_HELP;
  96. "hex" return T_HEX;
  97. "if" return T_IF;
  98. "imply" return T_IMPLY;
  99. "int" return T_INT;
  100. "mainmenu" return T_MAINMENU;
  101. "menu" return T_MENU;
  102. "menuconfig" return T_MENUCONFIG;
  103. "modules" return T_MODULES;
  104. "on" return T_ON;
  105. "prompt" return T_PROMPT;
  106. "range" return T_RANGE;
  107. "select" return T_SELECT;
  108. "source" return T_SOURCE;
  109. "string" return T_STRING;
  110. "transitional" return T_TRANSITIONAL;
  111. "tristate" return T_TRISTATE;
  112. "visible" return T_VISIBLE;
  113. "||" return T_OR;
  114. "&&" return T_AND;
  115. "=" return T_EQUAL;
  116. "!=" return T_UNEQUAL;
  117. "<" return T_LESS;
  118. "<=" return T_LESS_EQUAL;
  119. ">" return T_GREATER;
  120. ">=" return T_GREATER_EQUAL;
  121. "!" return T_NOT;
  122. "(" return T_OPEN_PAREN;
  123. ")" return T_CLOSE_PAREN;
  124. ":=" return T_COLON_EQUAL;
  125. "+=" return T_PLUS_EQUAL;
  126. \"|\' {
  127. open_quote = yytext[0];
  128. new_string();
  129. BEGIN(STRING);
  130. }
  131. {n}+ {
  132. alloc_string(yytext, yyleng);
  133. yylval.string = text;
  134. return T_WORD;
  135. }
  136. ({n}|$)+ {
  137. /* this token includes at least one '$' */
  138. yylval.string = expand_token(yytext, yyleng);
  139. if (strlen(yylval.string))
  140. return T_WORD;
  141. free(yylval.string);
  142. }
  143. . warn_ignored_character(*yytext);
  144. <ASSIGN_VAL>{
  145. [^[:blank:]\n]+.* {
  146. alloc_string(yytext, yyleng);
  147. yylval.string = text;
  148. return T_ASSIGN_VAL;
  149. }
  150. \n { BEGIN(INITIAL); return T_EOL; }
  151. .
  152. }
  153. <STRING>{
  154. "$".* append_expanded_string(yytext);
  155. [^$'"\\\n]+ {
  156. append_string(yytext, yyleng);
  157. }
  158. \\.? {
  159. append_string(yytext + 1, yyleng - 1);
  160. }
  161. \'|\" {
  162. if (open_quote == yytext[0]) {
  163. BEGIN(INITIAL);
  164. yylval.string = text;
  165. return T_WORD_QUOTE;
  166. } else
  167. append_string(yytext, 1);
  168. }
  169. \n {
  170. fprintf(stderr,
  171. "%s:%d:warning: multi-line strings not supported\n",
  172. cur_filename, cur_lineno);
  173. unput('\n');
  174. BEGIN(INITIAL);
  175. yylval.string = text;
  176. return T_WORD_QUOTE;
  177. }
  178. <<EOF>> {
  179. BEGIN(INITIAL);
  180. yylval.string = text;
  181. return T_WORD_QUOTE;
  182. }
  183. }
  184. <HELP>{
  185. [ \t]+ {
  186. int ts, i;
  187. ts = 0;
  188. for (i = 0; i < yyleng; i++) {
  189. if (yytext[i] == '\t')
  190. ts = (ts & ~7) + 8;
  191. else
  192. ts++;
  193. }
  194. last_ts = ts;
  195. if (first_ts) {
  196. if (ts < first_ts) {
  197. zconf_endhelp();
  198. return T_HELPTEXT;
  199. }
  200. ts -= first_ts;
  201. while (ts > 8) {
  202. append_string(" ", 8);
  203. ts -= 8;
  204. }
  205. append_string(" ", ts);
  206. }
  207. }
  208. [ \t]*\n/[^ \t\n] {
  209. zconf_endhelp();
  210. return T_HELPTEXT;
  211. }
  212. [ \t]*\n {
  213. append_string("\n", 1);
  214. }
  215. [^ \t\n].* {
  216. while (yyleng) {
  217. if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
  218. break;
  219. yyleng--;
  220. }
  221. append_string(yytext, yyleng);
  222. if (!first_ts)
  223. first_ts = last_ts;
  224. }
  225. <<EOF>> {
  226. zconf_endhelp();
  227. return T_HELPTEXT;
  228. }
  229. }
  230. <<EOF>> {
  231. BEGIN(INITIAL);
  232. if (prev_token != T_EOL && prev_token != T_HELPTEXT)
  233. fprintf(stderr, "%s:%d:warning: no new line at end of file\n",
  234. cur_filename, yylineno);
  235. if (current_buf) {
  236. zconf_endfile();
  237. return T_EOL;
  238. }
  239. fclose(yyin);
  240. yyterminate();
  241. }
  242. %%
  243. /* second stage lexer */
  244. int yylex(void)
  245. {
  246. int token;
  247. repeat:
  248. token = yylex1();
  249. if (prev_token == T_EOL || prev_token == T_HELPTEXT) {
  250. if (token == T_EOL)
  251. /* Do not pass unneeded T_EOL to the parser. */
  252. goto repeat;
  253. else
  254. /*
  255. * For the parser, update lineno at the first token
  256. * of each statement. Generally, \n is a statement
  257. * terminator in Kconfig, but it is not always true
  258. * because \n could be escaped by a backslash.
  259. */
  260. cur_lineno = yylineno;
  261. }
  262. if (prev_prev_token == T_EOL && prev_token == T_WORD &&
  263. (token == T_EQUAL || token == T_COLON_EQUAL || token == T_PLUS_EQUAL))
  264. BEGIN(ASSIGN_VAL);
  265. prev_prev_token = prev_token;
  266. prev_token = token;
  267. return token;
  268. }
  269. static char *expand_token(const char *in, size_t n)
  270. {
  271. char *out;
  272. int c;
  273. char c2;
  274. const char *rest, *end;
  275. new_string();
  276. append_string(in, n);
  277. /*
  278. * get the whole line because we do not know the end of token.
  279. * input() returns 0 (not EOF!) when it reachs the end of file.
  280. */
  281. while ((c = input()) != 0) {
  282. if (c == '\n') {
  283. unput(c);
  284. break;
  285. }
  286. c2 = c;
  287. append_string(&c2, 1);
  288. }
  289. rest = text;
  290. out = expand_one_token(&rest);
  291. /* push back unused characters to the input stream */
  292. end = rest + strlen(rest);
  293. while (end > rest)
  294. unput(*--end);
  295. free(text);
  296. return out;
  297. }
  298. static void append_expanded_string(const char *str)
  299. {
  300. const char *end;
  301. char *res;
  302. str++;
  303. res = expand_dollar(&str);
  304. /* push back unused characters to the input stream */
  305. end = str + strlen(str);
  306. while (end > str)
  307. unput(*--end);
  308. append_string(res, strlen(res));
  309. free(res);
  310. }
  311. void zconf_starthelp(void)
  312. {
  313. new_string();
  314. last_ts = first_ts = 0;
  315. BEGIN(HELP);
  316. }
  317. static void zconf_endhelp(void)
  318. {
  319. yylval.string = text;
  320. BEGIN(INITIAL);
  321. }
  322. /*
  323. * Try to open specified file with following names:
  324. * ./name
  325. * $(srctree)/name
  326. * The latter is used when srctree is separate from objtree
  327. * when compiling the kernel.
  328. * Return NULL if file is not found.
  329. */
  330. FILE *zconf_fopen(const char *name)
  331. {
  332. char *env, fullname[PATH_MAX+1];
  333. FILE *f;
  334. f = fopen(name, "r");
  335. if (!f && name != NULL && name[0] != '/') {
  336. env = getenv(SRCTREE);
  337. if (env) {
  338. snprintf(fullname, sizeof(fullname),
  339. "%s/%s", env, name);
  340. f = fopen(fullname, "r");
  341. }
  342. }
  343. return f;
  344. }
  345. void zconf_initscan(const char *name)
  346. {
  347. yyin = zconf_fopen(name);
  348. if (!yyin) {
  349. fprintf(stderr, "can't find file %s\n", name);
  350. exit(1);
  351. }
  352. cur_filename = file_lookup(name);
  353. yylineno = 1;
  354. }
  355. void zconf_nextfile(const char *name)
  356. {
  357. struct buffer *buf = xmalloc(sizeof(*buf));
  358. bool recur_include = false;
  359. buf->state = YY_CURRENT_BUFFER;
  360. buf->yylineno = yylineno;
  361. buf->filename = cur_filename;
  362. buf->source_lineno = cur_lineno;
  363. buf->parent = current_buf;
  364. current_buf = buf;
  365. yyin = zconf_fopen(name);
  366. if (!yyin) {
  367. fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
  368. cur_filename, cur_lineno, name);
  369. exit(1);
  370. }
  371. yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
  372. for (buf = current_buf; buf; buf = buf->parent) {
  373. if (!strcmp(buf->filename, name))
  374. recur_include = true;
  375. }
  376. if (recur_include) {
  377. fprintf(stderr,
  378. "Recursive inclusion detected.\n"
  379. "Inclusion path:\n"
  380. " current file : %s\n", name);
  381. for (buf = current_buf; buf; buf = buf->parent)
  382. fprintf(stderr, " included from: %s:%d\n",
  383. buf->filename, buf->source_lineno);
  384. exit(1);
  385. }
  386. yylineno = 1;
  387. cur_filename = file_lookup(name);
  388. }
  389. static void zconf_endfile(void)
  390. {
  391. struct buffer *tmp;
  392. fclose(yyin);
  393. yy_delete_buffer(YY_CURRENT_BUFFER);
  394. yy_switch_to_buffer(current_buf->state);
  395. yylineno = current_buf->yylineno;
  396. cur_filename = current_buf->filename;
  397. tmp = current_buf;
  398. current_buf = current_buf->parent;
  399. free(tmp);
  400. }