NCDConfigParser_parse.y 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. // mention parser_out in some destructor to a void unused variable warning
  91. %destructor processes { (void)parser_out; free_program($$); }
  92. %destructor statement { free_statement($$); }
  93. %destructor elif_maybe { free_ifblock($$); }
  94. %destructor elif { free_ifblock($$); }
  95. %destructor else_maybe { free_block($$); }
  96. %destructor statements { free_block($$); }
  97. %destructor dotted_name { free($$); }
  98. %destructor statement_args_maybe { free_value($$); }
  99. %destructor list_contents { free_value($$); }
  100. %destructor list { free_value($$); }
  101. %destructor map_contents { free_value($$); }
  102. %destructor map { free_value($$); }
  103. %destructor value { free_value($$); }
  104. %destructor name_maybe { free($$); }
  105. %stack_size 0
  106. %syntax_error {
  107. parser_out->syntax_error = 1;
  108. }
  109. // workaroud Lemon bug: if the stack overflows, the token that caused the overflow will be leaked
  110. %stack_overflow {
  111. if (yypMinor) {
  112. free_token(yypMinor->yy0);
  113. }
  114. }
  115. input ::= processes(A). {
  116. ASSERT(!parser_out->have_ast)
  117. if (A.have) {
  118. parser_out->have_ast = 1;
  119. parser_out->ast = A.v;
  120. }
  121. }
  122. processes(R) ::= . {
  123. NCDProgram prog;
  124. NCDProgram_Init(&prog);
  125. R.have = 1;
  126. R.v = prog;
  127. }
  128. processes(R) ::= INCLUDE STRING(A) processes(N). {
  129. ASSERT(A.str)
  130. if (!N.have) {
  131. goto failA0;
  132. }
  133. NCDProgramElem elem;
  134. if (!NCDProgramElem_InitInclude(&elem, A.str, A.len)) {
  135. goto failA0;
  136. }
  137. if (!NCDProgram_PrependElem(&N.v, elem)) {
  138. goto failA1;
  139. }
  140. R.have = 1;
  141. R.v = N.v;
  142. N.have = 0;
  143. goto doneA;
  144. failA1:
  145. NCDProgramElem_Free(&elem);
  146. failA0:
  147. R.have = 0;
  148. parser_out->out_of_memory = 1;
  149. doneA:
  150. free_token(A);
  151. free_program(N);
  152. }
  153. processes(R) ::= process_or_template(T) NAME(A) CURLY_OPEN statements(B) CURLY_CLOSE processes(N). {
  154. ASSERT(A.str)
  155. if (!B.have || !N.have) {
  156. goto failB0;
  157. }
  158. NCDProcess proc;
  159. if (!NCDProcess_Init(&proc, T, A.str, B.v)) {
  160. goto failB0;
  161. }
  162. B.have = 0;
  163. NCDProgramElem elem;
  164. NCDProgramElem_InitProcess(&elem, proc);
  165. if (!NCDProgram_PrependElem(&N.v, elem)) {
  166. goto failB1;
  167. }
  168. R.have = 1;
  169. R.v = N.v;
  170. N.have = 0;
  171. goto doneB;
  172. failB1:
  173. NCDProgramElem_Free(&elem);
  174. failB0:
  175. R.have = 0;
  176. parser_out->out_of_memory = 1;
  177. doneB:
  178. free_token(A);
  179. free_block(B);
  180. free_program(N);
  181. }
  182. statement(R) ::= dotted_name(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON. {
  183. if (!A || !B.have) {
  184. goto failC0;
  185. }
  186. if (!NCDStatement_InitReg(&R.v, C, NULL, A, B.v)) {
  187. goto failC0;
  188. }
  189. B.have = 0;
  190. R.have = 1;
  191. goto doneC;
  192. failC0:
  193. R.have = 0;
  194. parser_out->out_of_memory = 1;
  195. doneC:
  196. free(A);
  197. free_value(B);
  198. free(C);
  199. }
  200. statement(R) ::= dotted_name(M) ARROW dotted_name(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON. {
  201. if (!M || !A || !B.have) {
  202. goto failD0;
  203. }
  204. if (!NCDStatement_InitReg(&R.v, C, M, A, B.v)) {
  205. goto failD0;
  206. }
  207. B.have = 0;
  208. R.have = 1;
  209. goto doneD;
  210. failD0:
  211. R.have = 0;
  212. parser_out->out_of_memory = 1;
  213. doneD:
  214. free(M);
  215. free(A);
  216. free_value(B);
  217. free(C);
  218. }
  219. 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. {
  220. if (!A.have || !B.have || !I.have) {
  221. goto failE0;
  222. }
  223. NCDIf ifc;
  224. NCDIf_Init(&ifc, A.v, B.v);
  225. A.have = 0;
  226. B.have = 0;
  227. if (!NCDIfBlock_PrependIf(&I.v, ifc)) {
  228. NCDIf_Free(&ifc);
  229. goto failE0;
  230. }
  231. if (!NCDStatement_InitIf(&R.v, C, I.v)) {
  232. goto failE0;
  233. }
  234. I.have = 0;
  235. if (E.have) {
  236. NCDStatement_IfAddElse(&R.v, E.v);
  237. E.have = 0;
  238. }
  239. R.have = 1;
  240. goto doneE;
  241. failE0:
  242. R.have = 0;
  243. parser_out->out_of_memory = 1;
  244. doneE:
  245. free_value(A);
  246. free_block(B);
  247. free_ifblock(I);
  248. free_block(E);
  249. free(C);
  250. }
  251. statement(R) ::= FOREACH ROUND_OPEN value(A) AS NAME(B) ROUND_CLOSE CURLY_OPEN statements(S) CURLY_CLOSE name_maybe(N) SEMICOLON. {
  252. if (!A.have || !B.str || !S.have) {
  253. goto failEA0;
  254. }
  255. if (!NCDStatement_InitForeach(&R.v, N, A.v, B.str, NULL, S.v)) {
  256. goto failEA0;
  257. }
  258. A.have = 0;
  259. S.have = 0;
  260. R.have = 1;
  261. goto doneEA0;
  262. failEA0:
  263. R.have = 0;
  264. parser_out->out_of_memory = 1;
  265. doneEA0:
  266. free_value(A);
  267. free_token(B);
  268. free_block(S);
  269. free(N);
  270. }
  271. 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. {
  272. if (!A.have || !B.str || !C.str || !S.have) {
  273. goto failEB0;
  274. }
  275. if (!NCDStatement_InitForeach(&R.v, N, A.v, B.str, C.str, S.v)) {
  276. goto failEB0;
  277. }
  278. A.have = 0;
  279. S.have = 0;
  280. R.have = 1;
  281. goto doneEB0;
  282. failEB0:
  283. R.have = 0;
  284. parser_out->out_of_memory = 1;
  285. doneEB0:
  286. free_value(A);
  287. free_token(B);
  288. free_token(C);
  289. free_block(S);
  290. free(N);
  291. }
  292. elif_maybe(R) ::= . {
  293. NCDIfBlock_Init(&R.v);
  294. R.have = 1;
  295. }
  296. elif_maybe(R) ::= elif(A). {
  297. R = A;
  298. }
  299. elif(R) ::= ELIF ROUND_OPEN value(A) ROUND_CLOSE CURLY_OPEN statements(B) CURLY_CLOSE. {
  300. if (!A.have || !B.have) {
  301. goto failF0;
  302. }
  303. NCDIfBlock_Init(&R.v);
  304. NCDIf ifc;
  305. NCDIf_Init(&ifc, A.v, B.v);
  306. A.have = 0;
  307. B.have = 0;
  308. if (!NCDIfBlock_PrependIf(&R.v, ifc)) {
  309. goto failF1;
  310. }
  311. R.have = 1;
  312. goto doneF0;
  313. failF1:
  314. NCDIf_Free(&ifc);
  315. NCDIfBlock_Free(&R.v);
  316. failF0:
  317. R.have = 0;
  318. parser_out->out_of_memory = 1;
  319. doneF0:
  320. free_value(A);
  321. free_block(B);
  322. }
  323. elif(R) ::= ELIF ROUND_OPEN value(A) ROUND_CLOSE CURLY_OPEN statements(B) CURLY_CLOSE elif(N). {
  324. if (!A.have || !B.have || !N.have) {
  325. goto failG0;
  326. }
  327. NCDIf ifc;
  328. NCDIf_Init(&ifc, A.v, B.v);
  329. A.have = 0;
  330. B.have = 0;
  331. if (!NCDIfBlock_PrependIf(&N.v, ifc)) {
  332. goto failG1;
  333. }
  334. R.have = 1;
  335. R.v = N.v;
  336. N.have = 0;
  337. goto doneG0;
  338. failG1:
  339. NCDIf_Free(&ifc);
  340. failG0:
  341. R.have = 0;
  342. parser_out->out_of_memory = 1;
  343. doneG0:
  344. free_value(A);
  345. free_block(B);
  346. free_ifblock(N);
  347. }
  348. else_maybe(R) ::= . {
  349. R.have = 0;
  350. }
  351. else_maybe(R) ::= ELSE CURLY_OPEN statements(B) CURLY_CLOSE. {
  352. R = B;
  353. }
  354. statements(R) ::= statement(A). {
  355. if (!A.have) {
  356. goto failH0;
  357. }
  358. NCDBlock_Init(&R.v);
  359. if (!NCDBlock_PrependStatement(&R.v, A.v)) {
  360. goto failH1;
  361. }
  362. A.have = 0;
  363. R.have = 1;
  364. goto doneH;
  365. failH1:
  366. NCDBlock_Free(&R.v);
  367. failH0:
  368. R.have = 0;
  369. parser_out->out_of_memory = 1;
  370. doneH:
  371. free_statement(A);
  372. }
  373. statements(R) ::= statement(A) statements(N). {
  374. if (!A.have || !N.have) {
  375. goto failI0;
  376. }
  377. if (!NCDBlock_PrependStatement(&N.v, A.v)) {
  378. goto failI1;
  379. }
  380. A.have = 0;
  381. R.have = 1;
  382. R.v = N.v;
  383. N.have = 0;
  384. goto doneI;
  385. failI1:
  386. NCDBlock_Free(&R.v);
  387. failI0:
  388. R.have = 0;
  389. parser_out->out_of_memory = 1;
  390. doneI:
  391. free_statement(A);
  392. free_block(N);
  393. }
  394. dotted_name(R) ::= NAME(A). {
  395. ASSERT(A.str)
  396. R = A.str;
  397. }
  398. dotted_name(R) ::= NAME(A) DOT dotted_name(N). {
  399. ASSERT(A.str)
  400. if (!N) {
  401. goto failJ0;
  402. }
  403. if (!(R = concat_strings(3, A.str, ".", N))) {
  404. goto failJ0;
  405. }
  406. goto doneJ;
  407. failJ0:
  408. R = NULL;
  409. parser_out->out_of_memory = 1;
  410. doneJ:
  411. free_token(A);
  412. free(N);
  413. }
  414. statement_args_maybe(R) ::= . {
  415. R.have = 1;
  416. NCDValue_InitList(&R.v);
  417. }
  418. statement_args_maybe(R) ::= list_contents(A). {
  419. R = A;
  420. }
  421. list_contents(R) ::= value(A). {
  422. if (!A.have) {
  423. goto failL0;
  424. }
  425. NCDValue_InitList(&R.v);
  426. if (!NCDValue_ListPrepend(&R.v, A.v)) {
  427. goto failL1;
  428. }
  429. A.have = 0;
  430. R.have = 1;
  431. goto doneL;
  432. failL1:
  433. NCDValue_Free(&R.v);
  434. failL0:
  435. R.have = 0;
  436. parser_out->out_of_memory = 1;
  437. doneL:
  438. free_value(A);
  439. }
  440. list_contents(R) ::= value(A) COMMA list_contents(N). {
  441. if (!A.have || !N.have) {
  442. goto failM0;
  443. }
  444. if (!NCDValue_ListPrepend(&N.v, A.v)) {
  445. goto failM0;
  446. }
  447. A.have = 0;
  448. R.have = 1;
  449. R.v = N.v;
  450. N.have = 0;
  451. goto doneM;
  452. failM0:
  453. R.have = 0;
  454. parser_out->out_of_memory = 1;
  455. doneM:
  456. free_value(A);
  457. free_value(N);
  458. }
  459. list(R) ::= CURLY_OPEN CURLY_CLOSE. {
  460. R.have = 1;
  461. NCDValue_InitList(&R.v);
  462. }
  463. list(R) ::= CURLY_OPEN list_contents(A) CURLY_CLOSE. {
  464. R = A;
  465. }
  466. map_contents(R) ::= value(A) COLON value(B). {
  467. if (!A.have || !B.have) {
  468. goto failS0;
  469. }
  470. NCDValue_InitMap(&R.v);
  471. if (!NCDValue_MapPrepend(&R.v, A.v, B.v)) {
  472. goto failS1;
  473. }
  474. A.have = 0;
  475. B.have = 0;
  476. R.have = 1;
  477. goto doneS;
  478. failS1:
  479. NCDValue_Free(&R.v);
  480. failS0:
  481. R.have = 0;
  482. parser_out->out_of_memory = 1;
  483. doneS:
  484. free_value(A);
  485. free_value(B);
  486. }
  487. map_contents(R) ::= value(A) COLON value(B) COMMA map_contents(N). {
  488. if (!A.have || !B.have || !N.have) {
  489. goto failT0;
  490. }
  491. if (!NCDValue_MapPrepend(&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. }