NCDConfigParser_parse.y 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /**
  2. * @file NCDConfigParser.y
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. %include {
  30. #include <string.h>
  31. #include <stddef.h>
  32. #include <misc/debug.h>
  33. #include <misc/concat_strings.h>
  34. #include <ncd/NCDAst.h>
  35. struct parser_out {
  36. int out_of_memory;
  37. int syntax_error;
  38. int have_ast;
  39. NCDProgram ast;
  40. };
  41. struct token {
  42. char *str;
  43. size_t len;
  44. };
  45. struct program {
  46. int have;
  47. NCDProgram v;
  48. };
  49. struct block {
  50. int have;
  51. NCDBlock v;
  52. };
  53. struct statement {
  54. int have;
  55. NCDStatement v;
  56. };
  57. struct ifblock {
  58. int have;
  59. NCDIfBlock v;
  60. };
  61. struct value {
  62. int have;
  63. NCDValue v;
  64. };
  65. static void free_token (struct token o) { free(o.str); }
  66. static void free_program (struct program o) { if (o.have) NCDProgram_Free(&o.v); }
  67. static void free_block (struct block o) { if (o.have) NCDBlock_Free(&o.v); }
  68. static void free_statement (struct statement o) { if (o.have) NCDStatement_Free(&o.v); }
  69. static void free_ifblock (struct ifblock o) { if (o.have) NCDIfBlock_Free(&o.v); }
  70. static void free_value (struct value o) { if (o.have) NCDValue_Free(&o.v); }
  71. }
  72. %extra_argument { struct parser_out *parser_out }
  73. %token_type { struct token }
  74. %token_destructor { free_token($$); }
  75. %type processes { struct program }
  76. %type statement { struct statement }
  77. %type elif_maybe { struct ifblock }
  78. %type elif { struct ifblock }
  79. %type else_maybe { struct block }
  80. %type statements { struct block }
  81. %type dotted_name { char * }
  82. %type statement_args_maybe { struct value }
  83. %type list_contents { struct value }
  84. %type list { struct value }
  85. %type map_contents { struct value }
  86. %type map { struct value }
  87. %type value { struct value }
  88. %type name_maybe { char * }
  89. %type process_or_template { int }
  90. %destructor processes { free_program($$); }
  91. %destructor statement { free_statement($$); }
  92. %destructor elif_maybe { free_ifblock($$); }
  93. %destructor elif { free_ifblock($$); }
  94. %destructor else_maybe { free_block($$); }
  95. %destructor statements { free_block($$); }
  96. %destructor dotted_name { free($$); }
  97. %destructor statement_args_maybe { free_value($$); }
  98. %destructor list_contents { free_value($$); }
  99. %destructor list { free_value($$); }
  100. %destructor map_contents { free_value($$); }
  101. %destructor map { free_value($$); }
  102. %destructor value { free_value($$); }
  103. %destructor name_maybe { free($$); }
  104. %stack_size 0
  105. %syntax_error {
  106. parser_out->syntax_error = 1;
  107. }
  108. // workaroud Lemon bug: if the stack overflows, the token that caused the overflow will be leaked
  109. %stack_overflow {
  110. if (yypMinor) {
  111. free_token(yypMinor->yy0);
  112. }
  113. }
  114. input ::= processes(A). {
  115. ASSERT(!parser_out->have_ast)
  116. if (A.have) {
  117. parser_out->have_ast = 1;
  118. parser_out->ast = A.v;
  119. }
  120. }
  121. processes(R) ::= process_or_template(T) NAME(A) CURLY_OPEN statements(B) CURLY_CLOSE. {
  122. ASSERT(A.str)
  123. if (!B.have) {
  124. goto failA0;
  125. }
  126. NCDProcess proc;
  127. if (!NCDProcess_Init(&proc, T, A.str, B.v)) {
  128. goto failA0;
  129. }
  130. B.have = 0;
  131. NCDProgram prog;
  132. NCDProgram_Init(&prog);
  133. if (!NCDProgram_PrependProcess(&prog, proc)) {
  134. goto failA1;
  135. }
  136. R.have = 1;
  137. R.v = prog;
  138. goto doneA;
  139. failA1:
  140. NCDProgram_Free(&prog);
  141. NCDProcess_Free(&proc);
  142. failA0:
  143. R.have = 0;
  144. parser_out->out_of_memory = 1;
  145. doneA:
  146. free_token(A);
  147. free_block(B);
  148. }
  149. processes(R) ::= process_or_template(T) NAME(A) CURLY_OPEN statements(B) CURLY_CLOSE processes(N). {
  150. ASSERT(A.str)
  151. if (!B.have || !N.have) {
  152. goto failB0;
  153. }
  154. NCDProcess proc;
  155. if (!NCDProcess_Init(&proc, T, A.str, B.v)) {
  156. goto failB0;
  157. }
  158. B.have = 0;
  159. if (!NCDProgram_PrependProcess(&N.v, proc)) {
  160. goto failB1;
  161. }
  162. R.have = 1;
  163. R.v = N.v;
  164. N.have = 0;
  165. goto doneB;
  166. failB1:
  167. NCDProcess_Free(&proc);
  168. failB0:
  169. R.have = 0;
  170. parser_out->out_of_memory = 1;
  171. doneB:
  172. free_token(A);
  173. free_block(B);
  174. free_program(N);
  175. }
  176. statement(R) ::= dotted_name(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON. {
  177. if (!A || !B.have) {
  178. goto failC0;
  179. }
  180. if (!NCDStatement_InitReg(&R.v, C, NULL, A, B.v)) {
  181. goto failC0;
  182. }
  183. B.have = 0;
  184. R.have = 1;
  185. goto doneC;
  186. failC0:
  187. R.have = 0;
  188. parser_out->out_of_memory = 1;
  189. doneC:
  190. free(A);
  191. free_value(B);
  192. free(C);
  193. }
  194. statement(R) ::= dotted_name(M) ARROW dotted_name(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON. {
  195. if (!M || !A || !B.have) {
  196. goto failD0;
  197. }
  198. if (!NCDStatement_InitReg(&R.v, C, M, A, B.v)) {
  199. goto failD0;
  200. }
  201. B.have = 0;
  202. R.have = 1;
  203. goto doneD;
  204. failD0:
  205. R.have = 0;
  206. parser_out->out_of_memory = 1;
  207. doneD:
  208. free(M);
  209. free(A);
  210. free_value(B);
  211. free(C);
  212. }
  213. statement(R) ::= IF ROUND_OPEN value(A) ROUND_CLOSE CURLY_OPEN statements(B) CURLY_CLOSE elif_maybe(I) else_maybe(E) name_maybe(C) SEMICOLON. {
  214. if (!A.have || !B.have || !I.have) {
  215. goto failE0;
  216. }
  217. NCDIf ifc;
  218. NCDIf_Init(&ifc, A.v, B.v);
  219. A.have = 0;
  220. B.have = 0;
  221. if (!NCDIfBlock_PrependIf(&I.v, ifc)) {
  222. NCDIf_Free(&ifc);
  223. goto failE0;
  224. }
  225. if (!NCDStatement_InitIf(&R.v, C, I.v)) {
  226. goto failE0;
  227. }
  228. I.have = 0;
  229. if (E.have) {
  230. NCDStatement_IfAddElse(&R.v, E.v);
  231. E.have = 0;
  232. }
  233. R.have = 1;
  234. goto doneE;
  235. failE0:
  236. R.have = 0;
  237. parser_out->out_of_memory = 1;
  238. doneE:
  239. free_value(A);
  240. free_block(B);
  241. free_ifblock(I);
  242. free_block(E);
  243. free(C);
  244. }
  245. statement(R) ::= FOREACH ROUND_OPEN value(A) AS NAME(B) ROUND_CLOSE CURLY_OPEN statements(S) CURLY_CLOSE name_maybe(N) SEMICOLON. {
  246. if (!A.have || !B.str || !S.have) {
  247. goto failEA0;
  248. }
  249. if (!NCDStatement_InitForeach(&R.v, N, A.v, B.str, NULL, S.v)) {
  250. goto failEA0;
  251. }
  252. A.have = 0;
  253. S.have = 0;
  254. R.have = 1;
  255. goto doneEA0;
  256. failEA0:
  257. R.have = 0;
  258. parser_out->out_of_memory = 1;
  259. doneEA0:
  260. free_value(A);
  261. free_token(B);
  262. free_block(S);
  263. free(N);
  264. }
  265. statement(R) ::= FOREACH ROUND_OPEN value(A) AS NAME(B) COLON NAME(C) ROUND_CLOSE CURLY_OPEN statements(S) CURLY_CLOSE name_maybe(N) SEMICOLON. {
  266. if (!A.have || !B.str || !C.str || !S.have) {
  267. goto failEB0;
  268. }
  269. if (!NCDStatement_InitForeach(&R.v, N, A.v, B.str, C.str, S.v)) {
  270. goto failEB0;
  271. }
  272. A.have = 0;
  273. S.have = 0;
  274. R.have = 1;
  275. goto doneEB0;
  276. failEB0:
  277. R.have = 0;
  278. parser_out->out_of_memory = 1;
  279. doneEB0:
  280. free_value(A);
  281. free_token(B);
  282. free_token(C);
  283. free_block(S);
  284. free(N);
  285. }
  286. elif_maybe(R) ::= . {
  287. NCDIfBlock_Init(&R.v);
  288. R.have = 1;
  289. }
  290. elif_maybe(R) ::= elif(A). {
  291. R = A;
  292. }
  293. elif(R) ::= ELIF ROUND_OPEN value(A) ROUND_CLOSE CURLY_OPEN statements(B) CURLY_CLOSE. {
  294. if (!A.have || !B.have) {
  295. goto failF0;
  296. }
  297. NCDIfBlock_Init(&R.v);
  298. NCDIf ifc;
  299. NCDIf_Init(&ifc, A.v, B.v);
  300. A.have = 0;
  301. B.have = 0;
  302. if (!NCDIfBlock_PrependIf(&R.v, ifc)) {
  303. goto failF1;
  304. }
  305. R.have = 1;
  306. goto doneF0;
  307. failF1:
  308. NCDIf_Free(&ifc);
  309. NCDIfBlock_Free(&R.v);
  310. failF0:
  311. R.have = 0;
  312. parser_out->out_of_memory = 1;
  313. doneF0:
  314. free_value(A);
  315. free_block(B);
  316. }
  317. elif(R) ::= ELIF ROUND_OPEN value(A) ROUND_CLOSE CURLY_OPEN statements(B) CURLY_CLOSE elif(N). {
  318. if (!A.have || !B.have || !N.have) {
  319. goto failG0;
  320. }
  321. NCDIf ifc;
  322. NCDIf_Init(&ifc, A.v, B.v);
  323. A.have = 0;
  324. B.have = 0;
  325. if (!NCDIfBlock_PrependIf(&N.v, ifc)) {
  326. goto failG1;
  327. }
  328. R.have = 1;
  329. R.v = N.v;
  330. N.have = 0;
  331. goto doneG0;
  332. failG1:
  333. NCDIf_Free(&ifc);
  334. failG0:
  335. R.have = 0;
  336. parser_out->out_of_memory = 1;
  337. doneG0:
  338. free_value(A);
  339. free_block(B);
  340. free_ifblock(N);
  341. }
  342. else_maybe(R) ::= . {
  343. R.have = 0;
  344. }
  345. else_maybe(R) ::= ELSE CURLY_OPEN statements(B) CURLY_CLOSE. {
  346. R = B;
  347. }
  348. statements(R) ::= statement(A). {
  349. if (!A.have) {
  350. goto failH0;
  351. }
  352. NCDBlock_Init(&R.v);
  353. if (!NCDBlock_PrependStatement(&R.v, A.v)) {
  354. goto failH1;
  355. }
  356. A.have = 0;
  357. R.have = 1;
  358. goto doneH;
  359. failH1:
  360. NCDBlock_Free(&R.v);
  361. failH0:
  362. R.have = 0;
  363. parser_out->out_of_memory = 1;
  364. doneH:
  365. free_statement(A);
  366. }
  367. statements(R) ::= statement(A) statements(N). {
  368. if (!A.have || !N.have) {
  369. goto failI0;
  370. }
  371. if (!NCDBlock_PrependStatement(&N.v, A.v)) {
  372. goto failI1;
  373. }
  374. A.have = 0;
  375. R.have = 1;
  376. R.v = N.v;
  377. N.have = 0;
  378. goto doneI;
  379. failI1:
  380. NCDBlock_Free(&R.v);
  381. failI0:
  382. R.have = 0;
  383. parser_out->out_of_memory = 1;
  384. doneI:
  385. free_statement(A);
  386. free_block(N);
  387. }
  388. dotted_name(R) ::= NAME(A). {
  389. ASSERT(A.str)
  390. R = A.str;
  391. }
  392. dotted_name(R) ::= NAME(A) DOT dotted_name(N). {
  393. ASSERT(A.str)
  394. if (!N) {
  395. goto failJ0;
  396. }
  397. if (!(R = concat_strings(3, A.str, ".", N))) {
  398. goto failJ0;
  399. }
  400. goto doneJ;
  401. failJ0:
  402. R = NULL;
  403. parser_out->out_of_memory = 1;
  404. doneJ:
  405. free_token(A);
  406. free(N);
  407. }
  408. statement_args_maybe(R) ::= . {
  409. R.have = 1;
  410. NCDValue_InitList(&R.v);
  411. }
  412. statement_args_maybe(R) ::= list_contents(A). {
  413. R = A;
  414. }
  415. list_contents(R) ::= value(A). {
  416. if (!A.have) {
  417. goto failL0;
  418. }
  419. NCDValue_InitList(&R.v);
  420. if (!NCDValue_ListPrepend(&R.v, A.v)) {
  421. goto failL1;
  422. }
  423. A.have = 0;
  424. R.have = 1;
  425. goto doneL;
  426. failL1:
  427. NCDValue_Free(&R.v);
  428. failL0:
  429. R.have = 0;
  430. parser_out->out_of_memory = 1;
  431. doneL:
  432. free_value(A);
  433. }
  434. list_contents(R) ::= value(A) COMMA list_contents(N). {
  435. if (!A.have || !N.have) {
  436. goto failM0;
  437. }
  438. if (!NCDValue_ListPrepend(&N.v, A.v)) {
  439. goto failM0;
  440. }
  441. A.have = 0;
  442. R.have = 1;
  443. R.v = N.v;
  444. N.have = 0;
  445. goto doneM;
  446. failM0:
  447. R.have = 0;
  448. parser_out->out_of_memory = 1;
  449. doneM:
  450. free_value(A);
  451. free_value(N);
  452. }
  453. list(R) ::= CURLY_OPEN CURLY_CLOSE. {
  454. R.have = 1;
  455. NCDValue_InitList(&R.v);
  456. }
  457. list(R) ::= CURLY_OPEN list_contents(A) CURLY_CLOSE. {
  458. R = A;
  459. }
  460. map_contents(R) ::= value(A) COLON value(B). {
  461. if (!A.have || !B.have) {
  462. goto failS0;
  463. }
  464. NCDValue_InitMap(&R.v);
  465. if (!NCDValue_MapInsert(&R.v, A.v, B.v)) {
  466. goto failS1;
  467. }
  468. A.have = 0;
  469. B.have = 0;
  470. R.have = 1;
  471. goto doneS;
  472. failS1:
  473. NCDValue_Free(&R.v);
  474. failS0:
  475. R.have = 0;
  476. parser_out->out_of_memory = 1;
  477. doneS:
  478. free_value(A);
  479. free_value(B);
  480. }
  481. map_contents(R) ::= value(A) COLON value(B) COMMA map_contents(N). {
  482. if (!A.have || !B.have || !N.have) {
  483. goto failT0;
  484. }
  485. if (NCDValue_MapFindKey(&N.v, &A.v)) {
  486. BLog(BLOG_ERROR, "duplicate key in map");
  487. R.have = 0;
  488. parser_out->syntax_error = 1;
  489. goto doneT;
  490. }
  491. if (!NCDValue_MapInsert(&N.v, A.v, B.v)) {
  492. goto failT0;
  493. }
  494. A.have = 0;
  495. B.have = 0;
  496. R.have = 1;
  497. R.v = N.v;
  498. N.have = 0;
  499. goto doneT;
  500. failT0:
  501. R.have = 0;
  502. parser_out->out_of_memory = 1;
  503. doneT:
  504. free_value(A);
  505. free_value(B);
  506. free_value(N);
  507. }
  508. map(R) ::= BRACKET_OPEN BRACKET_CLOSE. {
  509. R.have = 1;
  510. NCDValue_InitMap(&R.v);
  511. }
  512. map(R) ::= BRACKET_OPEN map_contents(A) BRACKET_CLOSE. {
  513. R = A;
  514. }
  515. value(R) ::= STRING(A). {
  516. ASSERT(A.str)
  517. if (!NCDValue_InitStringBin(&R.v, (uint8_t *)A.str, A.len)) {
  518. goto failU0;
  519. }
  520. R.have = 1;
  521. goto doneU;
  522. failU0:
  523. R.have = 0;
  524. parser_out->out_of_memory = 1;
  525. doneU:
  526. free_token(A);
  527. }
  528. value(R) ::= dotted_name(A). {
  529. if (!A) {
  530. goto failV0;
  531. }
  532. if (!NCDValue_InitVar(&R.v, A)) {
  533. goto failV0;
  534. }
  535. R.have = 1;
  536. goto doneV;
  537. failV0:
  538. R.have = 0;
  539. parser_out->out_of_memory = 1;
  540. doneV:
  541. free(A);
  542. }
  543. value(R) ::= list(A). {
  544. R = A;
  545. }
  546. value(R) ::= map(A). {
  547. R = A;
  548. }
  549. name_maybe(R) ::= . {
  550. R = NULL;
  551. }
  552. name_maybe(R) ::= NAME(A). {
  553. ASSERT(A.str)
  554. R = A.str;
  555. }
  556. process_or_template(R) ::= PROCESS. {
  557. R = 0;
  558. }
  559. process_or_template(R) ::= TEMPLATE. {
  560. R = 1;
  561. }