NCDValueParser_parse.y 4.9 KB

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