NCDValParser_parse.y 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. // argument for passing state to parser hooks
  30. %extra_argument { struct parser_state *parser_out }
  31. // type of structure representing tokens
  32. %token_type { struct token }
  33. // token destructor frees extra memory allocated for tokens
  34. %token_destructor { free_token($$); }
  35. // types of nonterminals
  36. %type list_contents { struct value }
  37. %type list { struct value }
  38. %type map_contents { struct value }
  39. %type map { struct value }
  40. %type value { struct value }
  41. // mention parser_out in some destructor to and avoid unused variable warning
  42. %destructor list_contents { (void)parser_out; }
  43. // try to dynamically grow the parse stack
  44. %stack_size 0
  45. // on syntax error, set the corresponding error flag
  46. %syntax_error {
  47. parser_out->error_flags |= ERROR_FLAG_SYNTAX;
  48. }
  49. // workaroud Lemon bug: if the stack overflows, the token that caused the overflow will be leaked
  50. %stack_overflow {
  51. if (yypMinor) {
  52. free_token(yypMinor->yy0);
  53. }
  54. }
  55. input ::= value(A). {
  56. if (!A.have) {
  57. goto failZ0;
  58. }
  59. if (!NCDVal_IsInvalid(parser_out->value)) {
  60. // should never happen
  61. parser_out->error_flags |= ERROR_FLAG_SYNTAX;
  62. goto failZ0;
  63. }
  64. if (!NCDValCons_Complete(&parser_out->cons, A.v, &parser_out->value, &parser_out->cons_error)) {
  65. handle_cons_error(parser_out);
  66. goto failZ0;
  67. }
  68. failZ0:;
  69. }
  70. list_contents(R) ::= value(A). {
  71. if (!A.have) {
  72. goto failL0;
  73. }
  74. NCDValCons_NewList(&parser_out->cons, &R.v);
  75. if (!NCDValCons_ListPrepend(&parser_out->cons, &R.v, A.v, &parser_out->cons_error)) {
  76. handle_cons_error(parser_out);
  77. goto failL0;
  78. }
  79. R.have = 1;
  80. goto doneL;
  81. failL0:
  82. R.have = 0;
  83. doneL:;
  84. }
  85. list_contents(R) ::= value(A) COMMA list_contents(N). {
  86. if (!A.have || !N.have) {
  87. goto failM0;
  88. }
  89. if (!NCDValCons_ListPrepend(&parser_out->cons, &N.v, A.v, &parser_out->cons_error)) {
  90. handle_cons_error(parser_out);
  91. goto failM0;
  92. }
  93. R.have = 1;
  94. R.v = N.v;
  95. goto doneM;
  96. failM0:
  97. R.have = 0;
  98. doneM:;
  99. }
  100. list(R) ::= CURLY_OPEN CURLY_CLOSE. {
  101. NCDValCons_NewList(&parser_out->cons, &R.v);
  102. R.have = 1;
  103. }
  104. list(R) ::= CURLY_OPEN list_contents(A) CURLY_CLOSE. {
  105. R = A;
  106. }
  107. map_contents(R) ::= value(A) COLON value(B). {
  108. if (!A.have || !B.have) {
  109. goto failS0;
  110. }
  111. NCDValCons_NewMap(&parser_out->cons, &R.v);
  112. if (!NCDValCons_MapInsert(&parser_out->cons, &R.v, A.v, B.v, &parser_out->cons_error)) {
  113. handle_cons_error(parser_out);
  114. goto failS0;
  115. }
  116. R.have = 1;
  117. goto doneS;
  118. failS0:
  119. R.have = 0;
  120. doneS:;
  121. }
  122. map_contents(R) ::= value(A) COLON value(B) COMMA map_contents(N). {
  123. if (!A.have || !B.have || !N.have) {
  124. goto failT0;
  125. }
  126. if (!NCDValCons_MapInsert(&parser_out->cons, &N.v, A.v, B.v, &parser_out->cons_error)) {
  127. handle_cons_error(parser_out);
  128. goto failT0;
  129. }
  130. R.have = 1;
  131. R.v = N.v;
  132. goto doneT;
  133. failT0:
  134. R.have = 0;
  135. doneT:;
  136. }
  137. map(R) ::= BRACKET_OPEN BRACKET_CLOSE. {
  138. NCDValCons_NewMap(&parser_out->cons, &R.v);
  139. R.have = 1;
  140. }
  141. map(R) ::= BRACKET_OPEN map_contents(A) BRACKET_CLOSE. {
  142. R = A;
  143. }
  144. value(R) ::= STRING(A). {
  145. ASSERT(A.str)
  146. if (!NCDValCons_NewString(&parser_out->cons, (const uint8_t *)A.str, A.len, &R.v, &parser_out->cons_error)) {
  147. handle_cons_error(parser_out);
  148. goto failU0;
  149. }
  150. R.have = 1;
  151. goto doneU;
  152. failU0:
  153. R.have = 0;
  154. doneU:;
  155. free_token(A);
  156. }
  157. value(R) ::= list(A). {
  158. R = A;
  159. }
  160. value(R) ::= map(A). {
  161. R = A;
  162. }