lexer.l 9.0 KB

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