NCDConfigParser_parse.y 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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 invoc { struct value }
  88. %type value { struct value }
  89. %type name_maybe { char * }
  90. %type process_or_template { int }
  91. %type name_list { struct value }
  92. %type interrupt_maybe { struct block }
  93. // mention parser_out in some destructor to avoid an unused-variable warning
  94. %destructor processes { (void)parser_out; free_program($$); }
  95. %destructor statement { free_statement($$); }
  96. %destructor elif_maybe { free_ifblock($$); }
  97. %destructor elif { free_ifblock($$); }
  98. %destructor else_maybe { free_block($$); }
  99. %destructor statements { free_block($$); }
  100. %destructor dotted_name { free($$); }
  101. %destructor statement_args_maybe { free_value($$); }
  102. %destructor list_contents { free_value($$); }
  103. %destructor list { free_value($$); }
  104. %destructor map_contents { free_value($$); }
  105. %destructor map { free_value($$); }
  106. %destructor invoc { free_value($$); }
  107. %destructor value { free_value($$); }
  108. %destructor name_maybe { free($$); }
  109. %destructor name_list { free_value($$); }
  110. %destructor interrupt_maybe { free_block($$); }
  111. %stack_size 0
  112. %syntax_error {
  113. parser_out->syntax_error = 1;
  114. }
  115. // workaroud Lemon bug: if the stack overflows, the token that caused the overflow will be leaked
  116. %stack_overflow {
  117. if (yypMinor) {
  118. free_token(yypMinor->yy0);
  119. }
  120. }
  121. input ::= processes(A). {
  122. ASSERT(!parser_out->have_ast)
  123. if (A.have) {
  124. parser_out->have_ast = 1;
  125. parser_out->ast = A.v;
  126. }
  127. }
  128. processes(R) ::= . {
  129. NCDProgram prog;
  130. NCDProgram_Init(&prog);
  131. R.have = 1;
  132. R.v = prog;
  133. }
  134. processes(R) ::= INCLUDE STRING(A) processes(N). {
  135. ASSERT(A.str)
  136. if (!N.have) {
  137. goto failA0;
  138. }
  139. NCDProgramElem elem;
  140. if (!NCDProgramElem_InitInclude(&elem, A.str, A.len)) {
  141. goto failA0;
  142. }
  143. if (!NCDProgram_PrependElem(&N.v, elem)) {
  144. goto failA1;
  145. }
  146. R.have = 1;
  147. R.v = N.v;
  148. N.have = 0;
  149. goto doneA;
  150. failA1:
  151. NCDProgramElem_Free(&elem);
  152. failA0:
  153. R.have = 0;
  154. parser_out->out_of_memory = 1;
  155. doneA:
  156. free_token(A);
  157. free_program(N);
  158. }
  159. processes(R) ::= INCLUDE_GUARD STRING(A) processes(N). {
  160. ASSERT(A.str)
  161. if (!N.have) {
  162. goto failZ0;
  163. }
  164. NCDProgramElem elem;
  165. if (!NCDProgramElem_InitIncludeGuard(&elem, A.str, A.len)) {
  166. goto failZ0;
  167. }
  168. if (!NCDProgram_PrependElem(&N.v, elem)) {
  169. goto failZ1;
  170. }
  171. R.have = 1;
  172. R.v = N.v;
  173. N.have = 0;
  174. goto doneZ;
  175. failZ1:
  176. NCDProgramElem_Free(&elem);
  177. failZ0:
  178. R.have = 0;
  179. parser_out->out_of_memory = 1;
  180. doneZ:
  181. free_token(A);
  182. free_program(N);
  183. }
  184. processes(R) ::= process_or_template(T) NAME(A) CURLY_OPEN statements(B) CURLY_CLOSE processes(N). {
  185. ASSERT(A.str)
  186. if (!B.have || !N.have) {
  187. goto failB0;
  188. }
  189. NCDProcess proc;
  190. if (!NCDProcess_Init(&proc, T, A.str, B.v)) {
  191. goto failB0;
  192. }
  193. B.have = 0;
  194. NCDProgramElem elem;
  195. NCDProgramElem_InitProcess(&elem, proc);
  196. if (!NCDProgram_PrependElem(&N.v, elem)) {
  197. goto failB1;
  198. }
  199. R.have = 1;
  200. R.v = N.v;
  201. N.have = 0;
  202. goto doneB;
  203. failB1:
  204. NCDProgramElem_Free(&elem);
  205. failB0:
  206. R.have = 0;
  207. parser_out->out_of_memory = 1;
  208. doneB:
  209. free_token(A);
  210. free_block(B);
  211. free_program(N);
  212. }
  213. statement(R) ::= dotted_name(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON. {
  214. if (!A || !B.have) {
  215. goto failC0;
  216. }
  217. if (!NCDStatement_InitReg(&R.v, C, NULL, A, B.v)) {
  218. goto failC0;
  219. }
  220. B.have = 0;
  221. R.have = 1;
  222. goto doneC;
  223. failC0:
  224. R.have = 0;
  225. parser_out->out_of_memory = 1;
  226. doneC:
  227. free(A);
  228. free_value(B);
  229. free(C);
  230. }
  231. statement(R) ::= dotted_name(M) ARROW dotted_name(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON. {
  232. if (!M || !A || !B.have) {
  233. goto failD0;
  234. }
  235. if (!NCDStatement_InitReg(&R.v, C, M, A, B.v)) {
  236. goto failD0;
  237. }
  238. B.have = 0;
  239. R.have = 1;
  240. goto doneD;
  241. failD0:
  242. R.have = 0;
  243. parser_out->out_of_memory = 1;
  244. doneD:
  245. free(M);
  246. free(A);
  247. free_value(B);
  248. free(C);
  249. }
  250. 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. {
  251. if (!A.have || !B.have || !I.have) {
  252. goto failE0;
  253. }
  254. NCDIf ifc;
  255. NCDIf_Init(&ifc, A.v, B.v);
  256. A.have = 0;
  257. B.have = 0;
  258. if (!NCDIfBlock_PrependIf(&I.v, ifc)) {
  259. NCDIf_Free(&ifc);
  260. goto failE0;
  261. }
  262. if (!NCDStatement_InitIf(&R.v, C, I.v, NCDIFTYPE_IF)) {
  263. goto failE0;
  264. }
  265. I.have = 0;
  266. if (E.have) {
  267. NCDStatement_IfAddElse(&R.v, E.v);
  268. E.have = 0;
  269. }
  270. R.have = 1;
  271. goto doneE;
  272. failE0:
  273. R.have = 0;
  274. parser_out->out_of_memory = 1;
  275. doneE:
  276. free_value(A);
  277. free_block(B);
  278. free_ifblock(I);
  279. free_block(E);
  280. free(C);
  281. }
  282. statement(R) ::= FOREACH ROUND_OPEN value(A) AS NAME(B) ROUND_CLOSE CURLY_OPEN statements(S) CURLY_CLOSE name_maybe(N) SEMICOLON. {
  283. if (!A.have || !B.str || !S.have) {
  284. goto failEA0;
  285. }
  286. if (!NCDStatement_InitForeach(&R.v, N, A.v, B.str, NULL, S.v)) {
  287. goto failEA0;
  288. }
  289. A.have = 0;
  290. S.have = 0;
  291. R.have = 1;
  292. goto doneEA0;
  293. failEA0:
  294. R.have = 0;
  295. parser_out->out_of_memory = 1;
  296. doneEA0:
  297. free_value(A);
  298. free_token(B);
  299. free_block(S);
  300. free(N);
  301. }
  302. 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. {
  303. if (!A.have || !B.str || !C.str || !S.have) {
  304. goto failEB0;
  305. }
  306. if (!NCDStatement_InitForeach(&R.v, N, A.v, B.str, C.str, S.v)) {
  307. goto failEB0;
  308. }
  309. A.have = 0;
  310. S.have = 0;
  311. R.have = 1;
  312. goto doneEB0;
  313. failEB0:
  314. R.have = 0;
  315. parser_out->out_of_memory = 1;
  316. doneEB0:
  317. free_value(A);
  318. free_token(B);
  319. free_token(C);
  320. free_block(S);
  321. free(N);
  322. }
  323. elif_maybe(R) ::= . {
  324. NCDIfBlock_Init(&R.v);
  325. R.have = 1;
  326. }
  327. elif_maybe(R) ::= elif(A). {
  328. R = A;
  329. }
  330. elif(R) ::= ELIF ROUND_OPEN value(A) ROUND_CLOSE CURLY_OPEN statements(B) CURLY_CLOSE. {
  331. if (!A.have || !B.have) {
  332. goto failF0;
  333. }
  334. NCDIfBlock_Init(&R.v);
  335. NCDIf ifc;
  336. NCDIf_Init(&ifc, A.v, B.v);
  337. A.have = 0;
  338. B.have = 0;
  339. if (!NCDIfBlock_PrependIf(&R.v, ifc)) {
  340. goto failF1;
  341. }
  342. R.have = 1;
  343. goto doneF0;
  344. failF1:
  345. NCDIf_Free(&ifc);
  346. NCDIfBlock_Free(&R.v);
  347. failF0:
  348. R.have = 0;
  349. parser_out->out_of_memory = 1;
  350. doneF0:
  351. free_value(A);
  352. free_block(B);
  353. }
  354. elif(R) ::= ELIF ROUND_OPEN value(A) ROUND_CLOSE CURLY_OPEN statements(B) CURLY_CLOSE elif(N). {
  355. if (!A.have || !B.have || !N.have) {
  356. goto failG0;
  357. }
  358. NCDIf ifc;
  359. NCDIf_Init(&ifc, A.v, B.v);
  360. A.have = 0;
  361. B.have = 0;
  362. if (!NCDIfBlock_PrependIf(&N.v, ifc)) {
  363. goto failG1;
  364. }
  365. R.have = 1;
  366. R.v = N.v;
  367. N.have = 0;
  368. goto doneG0;
  369. failG1:
  370. NCDIf_Free(&ifc);
  371. failG0:
  372. R.have = 0;
  373. parser_out->out_of_memory = 1;
  374. doneG0:
  375. free_value(A);
  376. free_block(B);
  377. free_ifblock(N);
  378. }
  379. else_maybe(R) ::= . {
  380. R.have = 0;
  381. }
  382. else_maybe(R) ::= ELSE CURLY_OPEN statements(B) CURLY_CLOSE. {
  383. R = B;
  384. }
  385. statement(R) ::= BLOCK CURLY_OPEN statements(S) CURLY_CLOSE name_maybe(N) SEMICOLON. {
  386. if (!S.have) {
  387. goto failGA0;
  388. }
  389. if (!NCDStatement_InitBlock(&R.v, N, S.v)) {
  390. goto failGA0;
  391. }
  392. S.have = 0;
  393. R.have = 1;
  394. goto doneGA0;
  395. failGA0:
  396. R.have = 0;
  397. parser_out->out_of_memory = 1;
  398. doneGA0:
  399. free_block(S);
  400. free(N);
  401. }
  402. interrupt_maybe(R) ::= . {
  403. R.have = 0;
  404. }
  405. interrupt_maybe(R) ::= TOKEN_INTERRUPT CURLY_OPEN statements(S) CURLY_CLOSE. {
  406. R = S;
  407. }
  408. statement(R) ::= TOKEN_DO CURLY_OPEN statements(S) CURLY_CLOSE interrupt_maybe(I) name_maybe(N) SEMICOLON. {
  409. if (!S.have) {
  410. goto failGB0;
  411. }
  412. NCDIfBlock if_block;
  413. NCDIfBlock_Init(&if_block);
  414. if (I.have) {
  415. NCDIf int_if;
  416. NCDIf_InitBlock(&int_if, I.v);
  417. I.have = 0;
  418. if (!NCDIfBlock_PrependIf(&if_block, int_if)) {
  419. NCDIf_Free(&int_if);
  420. goto failGB1;
  421. }
  422. }
  423. NCDIf the_if;
  424. NCDIf_InitBlock(&the_if, S.v);
  425. S.have = 0;
  426. if (!NCDIfBlock_PrependIf(&if_block, the_if)) {
  427. NCDIf_Free(&the_if);
  428. goto failGB1;
  429. }
  430. if (!NCDStatement_InitIf(&R.v, N, if_block, NCDIFTYPE_DO)) {
  431. goto failGB1;
  432. }
  433. R.have = 1;
  434. goto doneGB0;
  435. failGB1:
  436. NCDIfBlock_Free(&if_block);
  437. failGB0:
  438. R.have = 0;
  439. parser_out->out_of_memory = 1;
  440. doneGB0:
  441. free_block(S);
  442. free_block(I);
  443. free(N);
  444. }
  445. statements(R) ::= statement(A). {
  446. if (!A.have) {
  447. goto failH0;
  448. }
  449. NCDBlock_Init(&R.v);
  450. if (!NCDBlock_PrependStatement(&R.v, A.v)) {
  451. goto failH1;
  452. }
  453. A.have = 0;
  454. R.have = 1;
  455. goto doneH;
  456. failH1:
  457. NCDBlock_Free(&R.v);
  458. failH0:
  459. R.have = 0;
  460. parser_out->out_of_memory = 1;
  461. doneH:
  462. free_statement(A);
  463. }
  464. statements(R) ::= statement(A) statements(N). {
  465. if (!A.have || !N.have) {
  466. goto failI0;
  467. }
  468. if (!NCDBlock_PrependStatement(&N.v, A.v)) {
  469. goto failI1;
  470. }
  471. A.have = 0;
  472. R.have = 1;
  473. R.v = N.v;
  474. N.have = 0;
  475. goto doneI;
  476. failI1:
  477. NCDBlock_Free(&R.v);
  478. failI0:
  479. R.have = 0;
  480. parser_out->out_of_memory = 1;
  481. doneI:
  482. free_statement(A);
  483. free_block(N);
  484. }
  485. dotted_name(R) ::= NAME(A). {
  486. ASSERT(A.str)
  487. R = A.str;
  488. }
  489. dotted_name(R) ::= NAME(A) DOT dotted_name(N). {
  490. ASSERT(A.str)
  491. if (!N) {
  492. goto failJ0;
  493. }
  494. if (!(R = concat_strings(3, A.str, ".", N))) {
  495. goto failJ0;
  496. }
  497. goto doneJ;
  498. failJ0:
  499. R = NULL;
  500. parser_out->out_of_memory = 1;
  501. doneJ:
  502. free_token(A);
  503. free(N);
  504. }
  505. name_list(R) ::= NAME(A). {
  506. if (!A.str) {
  507. goto failK0;
  508. }
  509. NCDValue_InitList(&R.v);
  510. NCDValue this_string;
  511. if (!NCDValue_InitString(&this_string, A.str)) {
  512. goto failK1;
  513. }
  514. if (!NCDValue_ListPrepend(&R.v, this_string)) {
  515. goto failK2;
  516. }
  517. R.have = 1;
  518. goto doneK;
  519. failK2:
  520. NCDValue_Free(&this_string);
  521. failK1:
  522. NCDValue_Free(&R.v);
  523. failK0:
  524. R.have = 0;
  525. parser_out->out_of_memory = 1;
  526. doneK:
  527. free_token(A);
  528. }
  529. name_list(R) ::= NAME(A) DOT name_list(N). {
  530. if (!A.str || !N.have) {
  531. goto failKA0;
  532. }
  533. NCDValue this_string;
  534. if (!NCDValue_InitString(&this_string, A.str)) {
  535. goto failKA0;
  536. }
  537. if (!NCDValue_ListPrepend(&N.v, this_string)) {
  538. goto failKA1;
  539. }
  540. R.have = 1;
  541. R.v = N.v;
  542. N.have = 0;
  543. goto doneKA;
  544. failKA1:
  545. NCDValue_Free(&this_string);
  546. failKA0:
  547. R.have = 0;
  548. parser_out->out_of_memory = 1;
  549. doneKA:
  550. free_token(A);
  551. free_value(N);
  552. }
  553. statement_args_maybe(R) ::= . {
  554. R.have = 1;
  555. NCDValue_InitList(&R.v);
  556. }
  557. statement_args_maybe(R) ::= list_contents(A). {
  558. R = A;
  559. }
  560. list_contents(R) ::= value(A). {
  561. if (!A.have) {
  562. goto failL0;
  563. }
  564. NCDValue_InitList(&R.v);
  565. if (!NCDValue_ListPrepend(&R.v, A.v)) {
  566. goto failL1;
  567. }
  568. A.have = 0;
  569. R.have = 1;
  570. goto doneL;
  571. failL1:
  572. NCDValue_Free(&R.v);
  573. failL0:
  574. R.have = 0;
  575. parser_out->out_of_memory = 1;
  576. doneL:
  577. free_value(A);
  578. }
  579. list_contents(R) ::= value(A) COMMA list_contents(N). {
  580. if (!A.have || !N.have) {
  581. goto failM0;
  582. }
  583. if (!NCDValue_ListPrepend(&N.v, A.v)) {
  584. goto failM0;
  585. }
  586. A.have = 0;
  587. R.have = 1;
  588. R.v = N.v;
  589. N.have = 0;
  590. goto doneM;
  591. failM0:
  592. R.have = 0;
  593. parser_out->out_of_memory = 1;
  594. doneM:
  595. free_value(A);
  596. free_value(N);
  597. }
  598. list(R) ::= CURLY_OPEN CURLY_CLOSE. {
  599. R.have = 1;
  600. NCDValue_InitList(&R.v);
  601. }
  602. list(R) ::= CURLY_OPEN list_contents(A) CURLY_CLOSE. {
  603. R = A;
  604. }
  605. map_contents(R) ::= value(A) COLON value(B). {
  606. if (!A.have || !B.have) {
  607. goto failS0;
  608. }
  609. NCDValue_InitMap(&R.v);
  610. if (!NCDValue_MapPrepend(&R.v, A.v, B.v)) {
  611. goto failS1;
  612. }
  613. A.have = 0;
  614. B.have = 0;
  615. R.have = 1;
  616. goto doneS;
  617. failS1:
  618. NCDValue_Free(&R.v);
  619. failS0:
  620. R.have = 0;
  621. parser_out->out_of_memory = 1;
  622. doneS:
  623. free_value(A);
  624. free_value(B);
  625. }
  626. map_contents(R) ::= value(A) COLON value(B) COMMA map_contents(N). {
  627. if (!A.have || !B.have || !N.have) {
  628. goto failT0;
  629. }
  630. if (!NCDValue_MapPrepend(&N.v, A.v, B.v)) {
  631. goto failT0;
  632. }
  633. A.have = 0;
  634. B.have = 0;
  635. R.have = 1;
  636. R.v = N.v;
  637. N.have = 0;
  638. goto doneT;
  639. failT0:
  640. R.have = 0;
  641. parser_out->out_of_memory = 1;
  642. doneT:
  643. free_value(A);
  644. free_value(B);
  645. free_value(N);
  646. }
  647. map(R) ::= BRACKET_OPEN BRACKET_CLOSE. {
  648. R.have = 1;
  649. NCDValue_InitMap(&R.v);
  650. }
  651. map(R) ::= BRACKET_OPEN map_contents(A) BRACKET_CLOSE. {
  652. R = A;
  653. }
  654. invoc(R) ::= value(F) ROUND_OPEN list_contents(A) ROUND_CLOSE. {
  655. if (!F.have || !A.have) {
  656. goto failQ0;
  657. }
  658. if (!NCDValue_InitInvoc(&R.v, F.v, A.v)) {
  659. goto failQ0;
  660. }
  661. F.have = 0;
  662. A.have = 0;
  663. R.have = 1;
  664. goto doneQ;
  665. failQ0:
  666. R.have = 0;
  667. parser_out->out_of_memory = 1;
  668. doneQ:
  669. free_value(F);
  670. free_value(A);
  671. }
  672. value(R) ::= STRING(A). {
  673. ASSERT(A.str)
  674. if (!NCDValue_InitStringBin(&R.v, (uint8_t *)A.str, A.len)) {
  675. goto failU0;
  676. }
  677. R.have = 1;
  678. goto doneU;
  679. failU0:
  680. R.have = 0;
  681. parser_out->out_of_memory = 1;
  682. doneU:
  683. free_token(A);
  684. }
  685. value(R) ::= AT_SIGN dotted_name(A). {
  686. if (!A) {
  687. goto failUA0;
  688. }
  689. if (!NCDValue_InitString(&R.v, A)) {
  690. goto failUA0;
  691. }
  692. R.have = 1;
  693. goto doneUA0;
  694. failUA0:
  695. R.have = 0;
  696. parser_out->out_of_memory = 1;
  697. doneUA0:
  698. free(A);
  699. }
  700. value(R) ::= CARET name_list(A). {
  701. R = A;
  702. }
  703. value(R) ::= dotted_name(A). {
  704. if (!A) {
  705. goto failV0;
  706. }
  707. if (!NCDValue_InitVar(&R.v, A)) {
  708. goto failV0;
  709. }
  710. R.have = 1;
  711. goto doneV;
  712. failV0:
  713. R.have = 0;
  714. parser_out->out_of_memory = 1;
  715. doneV:
  716. free(A);
  717. }
  718. value(R) ::= list(A). {
  719. R = A;
  720. }
  721. value(R) ::= map(A). {
  722. R = A;
  723. }
  724. value(R) ::= ROUND_OPEN value(A) ROUND_CLOSE. {
  725. R = A;
  726. }
  727. value(R) ::= invoc(A). {
  728. R = A;
  729. }
  730. name_maybe(R) ::= . {
  731. R = NULL;
  732. }
  733. name_maybe(R) ::= NAME(A). {
  734. ASSERT(A.str)
  735. R = A.str;
  736. }
  737. process_or_template(R) ::= PROCESS. {
  738. R = 0;
  739. }
  740. process_or_template(R) ::= TEMPLATE. {
  741. R = 1;
  742. }