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. // 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) ::= process_or_template(T) NAME(A) CURLY_OPEN statements(B) CURLY_CLOSE. {
  123. ASSERT(A.str)
  124. if (!B.have) {
  125. goto failA0;
  126. }
  127. NCDProcess proc;
  128. if (!NCDProcess_Init(&proc, T, A.str, B.v)) {
  129. goto failA0;
  130. }
  131. B.have = 0;
  132. NCDProgramElem elem;
  133. NCDProgramElem_InitProcess(&elem, proc);
  134. NCDProgram prog;
  135. NCDProgram_Init(&prog);
  136. if (!NCDProgram_PrependElem(&prog, elem)) {
  137. goto failA1;
  138. }
  139. R.have = 1;
  140. R.v = prog;
  141. goto doneA;
  142. failA1:
  143. NCDProgram_Free(&prog);
  144. NCDProgramElem_Free(&elem);
  145. failA0:
  146. R.have = 0;
  147. parser_out->out_of_memory = 1;
  148. doneA:
  149. free_token(A);
  150. free_block(B);
  151. }
  152. processes(R) ::= process_or_template(T) NAME(A) CURLY_OPEN statements(B) CURLY_CLOSE processes(N). {
  153. ASSERT(A.str)
  154. if (!B.have || !N.have) {
  155. goto failB0;
  156. }
  157. NCDProcess proc;
  158. if (!NCDProcess_Init(&proc, T, A.str, B.v)) {
  159. goto failB0;
  160. }
  161. B.have = 0;
  162. NCDProgramElem elem;
  163. NCDProgramElem_InitProcess(&elem, proc);
  164. if (!NCDProgram_PrependElem(&N.v, elem)) {
  165. goto failB1;
  166. }
  167. R.have = 1;
  168. R.v = N.v;
  169. N.have = 0;
  170. goto doneB;
  171. failB1:
  172. NCDProgramElem_Free(&elem);
  173. failB0:
  174. R.have = 0;
  175. parser_out->out_of_memory = 1;
  176. doneB:
  177. free_token(A);
  178. free_block(B);
  179. free_program(N);
  180. }
  181. statement(R) ::= dotted_name(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON. {
  182. if (!A || !B.have) {
  183. goto failC0;
  184. }
  185. if (!NCDStatement_InitReg(&R.v, C, NULL, A, B.v)) {
  186. goto failC0;
  187. }
  188. B.have = 0;
  189. R.have = 1;
  190. goto doneC;
  191. failC0:
  192. R.have = 0;
  193. parser_out->out_of_memory = 1;
  194. doneC:
  195. free(A);
  196. free_value(B);
  197. free(C);
  198. }
  199. statement(R) ::= dotted_name(M) ARROW dotted_name(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON. {
  200. if (!M || !A || !B.have) {
  201. goto failD0;
  202. }
  203. if (!NCDStatement_InitReg(&R.v, C, M, A, B.v)) {
  204. goto failD0;
  205. }
  206. B.have = 0;
  207. R.have = 1;
  208. goto doneD;
  209. failD0:
  210. R.have = 0;
  211. parser_out->out_of_memory = 1;
  212. doneD:
  213. free(M);
  214. free(A);
  215. free_value(B);
  216. free(C);
  217. }
  218. 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. {
  219. if (!A.have || !B.have || !I.have) {
  220. goto failE0;
  221. }
  222. NCDIf ifc;
  223. NCDIf_Init(&ifc, A.v, B.v);
  224. A.have = 0;
  225. B.have = 0;
  226. if (!NCDIfBlock_PrependIf(&I.v, ifc)) {
  227. NCDIf_Free(&ifc);
  228. goto failE0;
  229. }
  230. if (!NCDStatement_InitIf(&R.v, C, I.v)) {
  231. goto failE0;
  232. }
  233. I.have = 0;
  234. if (E.have) {
  235. NCDStatement_IfAddElse(&R.v, E.v);
  236. E.have = 0;
  237. }
  238. R.have = 1;
  239. goto doneE;
  240. failE0:
  241. R.have = 0;
  242. parser_out->out_of_memory = 1;
  243. doneE:
  244. free_value(A);
  245. free_block(B);
  246. free_ifblock(I);
  247. free_block(E);
  248. free(C);
  249. }
  250. statement(R) ::= FOREACH ROUND_OPEN value(A) AS NAME(B) ROUND_CLOSE CURLY_OPEN statements(S) CURLY_CLOSE name_maybe(N) SEMICOLON. {
  251. if (!A.have || !B.str || !S.have) {
  252. goto failEA0;
  253. }
  254. if (!NCDStatement_InitForeach(&R.v, N, A.v, B.str, NULL, S.v)) {
  255. goto failEA0;
  256. }
  257. A.have = 0;
  258. S.have = 0;
  259. R.have = 1;
  260. goto doneEA0;
  261. failEA0:
  262. R.have = 0;
  263. parser_out->out_of_memory = 1;
  264. doneEA0:
  265. free_value(A);
  266. free_token(B);
  267. free_block(S);
  268. free(N);
  269. }
  270. 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. {
  271. if (!A.have || !B.str || !C.str || !S.have) {
  272. goto failEB0;
  273. }
  274. if (!NCDStatement_InitForeach(&R.v, N, A.v, B.str, C.str, S.v)) {
  275. goto failEB0;
  276. }
  277. A.have = 0;
  278. S.have = 0;
  279. R.have = 1;
  280. goto doneEB0;
  281. failEB0:
  282. R.have = 0;
  283. parser_out->out_of_memory = 1;
  284. doneEB0:
  285. free_value(A);
  286. free_token(B);
  287. free_token(C);
  288. free_block(S);
  289. free(N);
  290. }
  291. elif_maybe(R) ::= . {
  292. NCDIfBlock_Init(&R.v);
  293. R.have = 1;
  294. }
  295. elif_maybe(R) ::= elif(A). {
  296. R = A;
  297. }
  298. elif(R) ::= ELIF ROUND_OPEN value(A) ROUND_CLOSE CURLY_OPEN statements(B) CURLY_CLOSE. {
  299. if (!A.have || !B.have) {
  300. goto failF0;
  301. }
  302. NCDIfBlock_Init(&R.v);
  303. NCDIf ifc;
  304. NCDIf_Init(&ifc, A.v, B.v);
  305. A.have = 0;
  306. B.have = 0;
  307. if (!NCDIfBlock_PrependIf(&R.v, ifc)) {
  308. goto failF1;
  309. }
  310. R.have = 1;
  311. goto doneF0;
  312. failF1:
  313. NCDIf_Free(&ifc);
  314. NCDIfBlock_Free(&R.v);
  315. failF0:
  316. R.have = 0;
  317. parser_out->out_of_memory = 1;
  318. doneF0:
  319. free_value(A);
  320. free_block(B);
  321. }
  322. elif(R) ::= ELIF ROUND_OPEN value(A) ROUND_CLOSE CURLY_OPEN statements(B) CURLY_CLOSE elif(N). {
  323. if (!A.have || !B.have || !N.have) {
  324. goto failG0;
  325. }
  326. NCDIf ifc;
  327. NCDIf_Init(&ifc, A.v, B.v);
  328. A.have = 0;
  329. B.have = 0;
  330. if (!NCDIfBlock_PrependIf(&N.v, ifc)) {
  331. goto failG1;
  332. }
  333. R.have = 1;
  334. R.v = N.v;
  335. N.have = 0;
  336. goto doneG0;
  337. failG1:
  338. NCDIf_Free(&ifc);
  339. failG0:
  340. R.have = 0;
  341. parser_out->out_of_memory = 1;
  342. doneG0:
  343. free_value(A);
  344. free_block(B);
  345. free_ifblock(N);
  346. }
  347. else_maybe(R) ::= . {
  348. R.have = 0;
  349. }
  350. else_maybe(R) ::= ELSE CURLY_OPEN statements(B) CURLY_CLOSE. {
  351. R = B;
  352. }
  353. statements(R) ::= statement(A). {
  354. if (!A.have) {
  355. goto failH0;
  356. }
  357. NCDBlock_Init(&R.v);
  358. if (!NCDBlock_PrependStatement(&R.v, A.v)) {
  359. goto failH1;
  360. }
  361. A.have = 0;
  362. R.have = 1;
  363. goto doneH;
  364. failH1:
  365. NCDBlock_Free(&R.v);
  366. failH0:
  367. R.have = 0;
  368. parser_out->out_of_memory = 1;
  369. doneH:
  370. free_statement(A);
  371. }
  372. statements(R) ::= statement(A) statements(N). {
  373. if (!A.have || !N.have) {
  374. goto failI0;
  375. }
  376. if (!NCDBlock_PrependStatement(&N.v, A.v)) {
  377. goto failI1;
  378. }
  379. A.have = 0;
  380. R.have = 1;
  381. R.v = N.v;
  382. N.have = 0;
  383. goto doneI;
  384. failI1:
  385. NCDBlock_Free(&R.v);
  386. failI0:
  387. R.have = 0;
  388. parser_out->out_of_memory = 1;
  389. doneI:
  390. free_statement(A);
  391. free_block(N);
  392. }
  393. dotted_name(R) ::= NAME(A). {
  394. ASSERT(A.str)
  395. R = A.str;
  396. }
  397. dotted_name(R) ::= NAME(A) DOT dotted_name(N). {
  398. ASSERT(A.str)
  399. if (!N) {
  400. goto failJ0;
  401. }
  402. if (!(R = concat_strings(3, A.str, ".", N))) {
  403. goto failJ0;
  404. }
  405. goto doneJ;
  406. failJ0:
  407. R = NULL;
  408. parser_out->out_of_memory = 1;
  409. doneJ:
  410. free_token(A);
  411. free(N);
  412. }
  413. statement_args_maybe(R) ::= . {
  414. R.have = 1;
  415. NCDValue_InitList(&R.v);
  416. }
  417. statement_args_maybe(R) ::= list_contents(A). {
  418. R = A;
  419. }
  420. list_contents(R) ::= value(A). {
  421. if (!A.have) {
  422. goto failL0;
  423. }
  424. NCDValue_InitList(&R.v);
  425. if (!NCDValue_ListPrepend(&R.v, A.v)) {
  426. goto failL1;
  427. }
  428. A.have = 0;
  429. R.have = 1;
  430. goto doneL;
  431. failL1:
  432. NCDValue_Free(&R.v);
  433. failL0:
  434. R.have = 0;
  435. parser_out->out_of_memory = 1;
  436. doneL:
  437. free_value(A);
  438. }
  439. list_contents(R) ::= value(A) COMMA list_contents(N). {
  440. if (!A.have || !N.have) {
  441. goto failM0;
  442. }
  443. if (!NCDValue_ListPrepend(&N.v, A.v)) {
  444. goto failM0;
  445. }
  446. A.have = 0;
  447. R.have = 1;
  448. R.v = N.v;
  449. N.have = 0;
  450. goto doneM;
  451. failM0:
  452. R.have = 0;
  453. parser_out->out_of_memory = 1;
  454. doneM:
  455. free_value(A);
  456. free_value(N);
  457. }
  458. list(R) ::= CURLY_OPEN CURLY_CLOSE. {
  459. R.have = 1;
  460. NCDValue_InitList(&R.v);
  461. }
  462. list(R) ::= CURLY_OPEN list_contents(A) CURLY_CLOSE. {
  463. R = A;
  464. }
  465. map_contents(R) ::= value(A) COLON value(B). {
  466. if (!A.have || !B.have) {
  467. goto failS0;
  468. }
  469. NCDValue_InitMap(&R.v);
  470. if (!NCDValue_MapPrepend(&R.v, A.v, B.v)) {
  471. goto failS1;
  472. }
  473. A.have = 0;
  474. B.have = 0;
  475. R.have = 1;
  476. goto doneS;
  477. failS1:
  478. NCDValue_Free(&R.v);
  479. failS0:
  480. R.have = 0;
  481. parser_out->out_of_memory = 1;
  482. doneS:
  483. free_value(A);
  484. free_value(B);
  485. }
  486. map_contents(R) ::= value(A) COLON value(B) COMMA map_contents(N). {
  487. if (!A.have || !B.have || !N.have) {
  488. goto failT0;
  489. }
  490. if (!NCDValue_MapPrepend(&N.v, A.v, B.v)) {
  491. goto failT0;
  492. }
  493. A.have = 0;
  494. B.have = 0;
  495. R.have = 1;
  496. R.v = N.v;
  497. N.have = 0;
  498. goto doneT;
  499. failT0:
  500. R.have = 0;
  501. parser_out->out_of_memory = 1;
  502. doneT:
  503. free_value(A);
  504. free_value(B);
  505. free_value(N);
  506. }
  507. map(R) ::= BRACKET_OPEN BRACKET_CLOSE. {
  508. R.have = 1;
  509. NCDValue_InitMap(&R.v);
  510. }
  511. map(R) ::= BRACKET_OPEN map_contents(A) BRACKET_CLOSE. {
  512. R = A;
  513. }
  514. value(R) ::= STRING(A). {
  515. ASSERT(A.str)
  516. if (!NCDValue_InitStringBin(&R.v, (uint8_t *)A.str, A.len)) {
  517. goto failU0;
  518. }
  519. R.have = 1;
  520. goto doneU;
  521. failU0:
  522. R.have = 0;
  523. parser_out->out_of_memory = 1;
  524. doneU:
  525. free_token(A);
  526. }
  527. value(R) ::= dotted_name(A). {
  528. if (!A) {
  529. goto failV0;
  530. }
  531. if (!NCDValue_InitVar(&R.v, A)) {
  532. goto failV0;
  533. }
  534. R.have = 1;
  535. goto doneV;
  536. failV0:
  537. R.have = 0;
  538. parser_out->out_of_memory = 1;
  539. doneV:
  540. free(A);
  541. }
  542. value(R) ::= list(A). {
  543. R = A;
  544. }
  545. value(R) ::= map(A). {
  546. R = A;
  547. }
  548. name_maybe(R) ::= . {
  549. R = NULL;
  550. }
  551. name_maybe(R) ::= NAME(A). {
  552. ASSERT(A.str)
  553. R = A.str;
  554. }
  555. process_or_template(R) ::= PROCESS. {
  556. R = 0;
  557. }
  558. process_or_template(R) ::= TEMPLATE. {
  559. R = 1;
  560. }