NCDValueParser_parse.y 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * @file NCDConfigParser_parse.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. %extra_argument { struct parser_state *parser_out }
  30. %token_type { struct token }
  31. %token_destructor { free_token($$); }
  32. %type list_contents { struct value }
  33. %type list { struct value }
  34. %type map_contents { struct value }
  35. %type map { struct value }
  36. %type value { struct value }
  37. %destructor list_contents { free_value($$); }
  38. %destructor list { free_value($$); }
  39. %destructor map_contents { free_value($$); }
  40. %destructor map { free_value($$); }
  41. %destructor value { free_value($$); }
  42. %stack_size 0
  43. %syntax_error {
  44. parser_out->syntax_error = 1;
  45. }
  46. // workaroud Lemon bug: if the stack overflows, the token that caused the overflow will be leaked
  47. %stack_overflow {
  48. if (yypMinor) {
  49. free_token(yypMinor->yy0);
  50. }
  51. }
  52. input ::= value(A). {
  53. if (!A.have || parser_out->have_value) {
  54. free_value(A);
  55. } else {
  56. parser_out->have_value = 1;
  57. parser_out->value = A.v;
  58. }
  59. }
  60. list_contents(R) ::= value(A). {
  61. if (!A.have) {
  62. goto failL0;
  63. }
  64. NCDValue_InitList(&R.v);
  65. if (!NCDValue_ListPrepend(&R.v, A.v)) {
  66. goto failL1;
  67. }
  68. A.have = 0;
  69. R.have = 1;
  70. goto doneL;
  71. failL1:
  72. NCDValue_Free(&R.v);
  73. failL0:
  74. R.have = 0;
  75. parser_out->out_of_memory = 1;
  76. doneL:
  77. free_value(A);
  78. }
  79. list_contents(R) ::= value(A) COMMA list_contents(N). {
  80. if (!A.have || !N.have) {
  81. goto failM0;
  82. }
  83. if (!NCDValue_ListPrepend(&N.v, A.v)) {
  84. goto failM0;
  85. }
  86. A.have = 0;
  87. R.have = 1;
  88. R.v = N.v;
  89. N.have = 0;
  90. goto doneM;
  91. failM0:
  92. R.have = 0;
  93. parser_out->out_of_memory = 1;
  94. doneM:
  95. free_value(A);
  96. free_value(N);
  97. }
  98. list(R) ::= CURLY_OPEN CURLY_CLOSE. {
  99. R.have = 1;
  100. NCDValue_InitList(&R.v);
  101. }
  102. list(R) ::= CURLY_OPEN list_contents(A) CURLY_CLOSE. {
  103. R = A;
  104. }
  105. map_contents(R) ::= value(A) COLON value(B). {
  106. if (!A.have || !B.have) {
  107. goto failS0;
  108. }
  109. NCDValue_InitMap(&R.v);
  110. if (!NCDValue_MapInsert(&R.v, A.v, B.v)) {
  111. goto failS1;
  112. }
  113. A.have = 0;
  114. B.have = 0;
  115. R.have = 1;
  116. goto doneS;
  117. failS1:
  118. NCDValue_Free(&R.v);
  119. failS0:
  120. R.have = 0;
  121. parser_out->out_of_memory = 1;
  122. doneS:
  123. free_value(A);
  124. free_value(B);
  125. }
  126. map_contents(R) ::= value(A) COLON value(B) COMMA map_contents(N). {
  127. if (!A.have || !B.have || !N.have) {
  128. goto failT0;
  129. }
  130. if (NCDValue_MapFindKey(&N.v, &A.v)) {
  131. BLog(BLOG_ERROR, "duplicate key in map");
  132. R.have = 0;
  133. parser_out->syntax_error = 1;
  134. goto doneT;
  135. }
  136. if (!NCDValue_MapInsert(&N.v, A.v, B.v)) {
  137. goto failT0;
  138. }
  139. A.have = 0;
  140. B.have = 0;
  141. R.have = 1;
  142. R.v = N.v;
  143. N.have = 0;
  144. goto doneT;
  145. failT0:
  146. R.have = 0;
  147. parser_out->out_of_memory = 1;
  148. doneT:
  149. free_value(A);
  150. free_value(B);
  151. free_value(N);
  152. }
  153. map(R) ::= BRACKET_OPEN BRACKET_CLOSE. {
  154. R.have = 1;
  155. NCDValue_InitMap(&R.v);
  156. }
  157. map(R) ::= BRACKET_OPEN map_contents(A) BRACKET_CLOSE. {
  158. R = A;
  159. }
  160. value(R) ::= STRING(A). {
  161. ASSERT(A.str)
  162. if (!NCDValue_InitStringBin(&R.v, (uint8_t *)A.str, A.len)) {
  163. goto failU0;
  164. }
  165. R.have = 1;
  166. goto doneU;
  167. failU0:
  168. R.have = 0;
  169. parser_out->out_of_memory = 1;
  170. doneU:
  171. free_token(A);
  172. }
  173. value(R) ::= list(A). {
  174. R = A;
  175. }
  176. value(R) ::= map(A). {
  177. R = A;
  178. }