NCDConfigParser_parse.y 14 KB

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