NCDConfigParser_parse.y 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 <ncd/NCDConfig.h>
  34. struct parser_out {
  35. int out_of_memory;
  36. int syntax_error;
  37. struct NCDConfig_processes *ast;
  38. };
  39. }
  40. %extra_argument {struct parser_out *parser_out}
  41. %token_type {void *}
  42. %token_destructor { free($$); }
  43. %type processes {struct NCDConfig_processes *}
  44. %type statements {struct NCDConfig_statements *}
  45. %type statement_names {struct NCDConfig_strings *}
  46. %type statement_args_maybe {struct NCDConfig_list *}
  47. %type list_contents {struct NCDConfig_list *}
  48. %type list {struct NCDConfig_list *}
  49. %type map_contents {struct NCDConfig_list *}
  50. %type map {struct NCDConfig_list *}
  51. %type value {struct NCDConfig_list *}
  52. %type name_maybe {char *}
  53. %type process_or_template {int}
  54. %destructor processes { NCDConfig_free_processes($$); }
  55. %destructor statements { NCDConfig_free_statements($$); }
  56. %destructor statement_names { NCDConfig_free_strings($$); }
  57. %destructor statement_args_maybe { NCDConfig_free_list($$); }
  58. %destructor list_contents { NCDConfig_free_list($$); }
  59. %destructor list { NCDConfig_free_list($$); }
  60. %destructor map_contents { NCDConfig_free_list($$); }
  61. %destructor map { NCDConfig_free_list($$); }
  62. %destructor value { NCDConfig_free_list($$); }
  63. %destructor name_maybe { free($$); }
  64. %stack_size 0
  65. %syntax_error {
  66. parser_out->syntax_error = 1;
  67. }
  68. // workaroud Lemon bug: if the stack overflows, the token that caused the overflow will be leaked
  69. %stack_overflow {
  70. if (yypMinor) {
  71. free(yypMinor->yy0);
  72. }
  73. }
  74. input ::= processes(A). {
  75. parser_out->ast = A;
  76. if (!A) {
  77. parser_out->out_of_memory = 1;
  78. }
  79. }
  80. processes(R) ::= process_or_template(T) NAME(A) CURLY_OPEN statements(B) CURLY_CLOSE. {
  81. R = NCDConfig_make_processes(T, A, B, 0, NULL);
  82. if (!R) {
  83. parser_out->out_of_memory = 1;
  84. }
  85. }
  86. processes(R) ::= process_or_template(T) NAME(A) CURLY_OPEN statements(B) CURLY_CLOSE processes(N). {
  87. R = NCDConfig_make_processes(T, A, B, 1, N);
  88. if (!R) {
  89. parser_out->out_of_memory = 1;
  90. }
  91. }
  92. statements(R) ::= statement_names(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON. {
  93. R = NCDConfig_make_statements(NULL, A, B, C, NULL);
  94. if (!R) {
  95. parser_out->out_of_memory = 1;
  96. }
  97. }
  98. statements(R) ::= statement_names(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON statements(N). {
  99. R = NCDConfig_make_statements(NULL, A, B, C, N);
  100. if (!R) {
  101. parser_out->out_of_memory = 1;
  102. }
  103. }
  104. statements(R) ::= statement_names(M) ARROW statement_names(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON. {
  105. R = NCDConfig_make_statements(M, A, B, C, NULL);
  106. if (!R) {
  107. parser_out->out_of_memory = 1;
  108. }
  109. }
  110. statements(R) ::= statement_names(M) ARROW statement_names(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON statements(N). {
  111. R = NCDConfig_make_statements(M, A, B, C, N);
  112. if (!R) {
  113. parser_out->out_of_memory = 1;
  114. }
  115. }
  116. statement_names(R) ::= NAME(A). {
  117. R = NCDConfig_make_strings(A, 0, NULL);
  118. if (!R) {
  119. parser_out->out_of_memory = 1;
  120. }
  121. }
  122. statement_names(R) ::= NAME(A) DOT statement_names(N). {
  123. R = NCDConfig_make_strings(A, 1, N);
  124. if (!R) {
  125. parser_out->out_of_memory = 1;
  126. }
  127. }
  128. statement_args_maybe(R) ::= . {
  129. R = NULL;
  130. }
  131. statement_args_maybe(R) ::= list_contents(A). {
  132. R = A;
  133. }
  134. list_contents(R) ::= value(A). {
  135. R = A;
  136. }
  137. list_contents(R) ::= value(A) COMMA list_contents(N). {
  138. if (!A) {
  139. NCDConfig_free_list(N);
  140. } else {
  141. ASSERT(!A->next)
  142. A->next = N;
  143. }
  144. R = A;
  145. }
  146. list(R) ::= CURLY_OPEN CURLY_CLOSE. {
  147. R = NULL;
  148. }
  149. list(R) ::= CURLY_OPEN list_contents(A) CURLY_CLOSE. {
  150. R = A;
  151. }
  152. map_contents(R) ::= value(A) COLON value(B). {
  153. if (!A || !B) {
  154. NCDConfig_free_list(A);
  155. NCDConfig_free_list(B);
  156. R = NULL;
  157. } else {
  158. ASSERT(!A->next)
  159. ASSERT(!B->next)
  160. A->next = B;
  161. R = A;
  162. }
  163. }
  164. map_contents(R) ::= value(A) COLON value(B) COMMA map_contents(N). {
  165. if (!A || !B) {
  166. NCDConfig_free_list(A);
  167. NCDConfig_free_list(B);
  168. NCDConfig_free_list(N);
  169. R = NULL;
  170. } else {
  171. ASSERT(!A->next)
  172. ASSERT(!B->next)
  173. A->next = B;
  174. B->next = N;
  175. R = A;
  176. }
  177. }
  178. map(R) ::= BRACKET_OPEN BRACKET_CLOSE. {
  179. R = NULL;
  180. }
  181. map(R) ::= BRACKET_OPEN map_contents(A) BRACKET_CLOSE. {
  182. R = A;
  183. }
  184. value(R) ::= STRING(A). {
  185. R = NCDConfig_make_list_string(A, NULL);
  186. if (!R) {
  187. parser_out->out_of_memory = 1;
  188. }
  189. }
  190. value(R) ::= statement_names(A). {
  191. R = NCDConfig_make_list_var(A, NULL);
  192. if (!R) {
  193. parser_out->out_of_memory = 1;
  194. }
  195. }
  196. value(R) ::= list(A). {
  197. R = NCDConfig_make_list_list(A, NULL);
  198. if (!R) {
  199. parser_out->out_of_memory = 1;
  200. }
  201. }
  202. value(R) ::= map(A). {
  203. R = NCDConfig_make_list_maplist(A, NULL);
  204. if (!R) {
  205. parser_out->out_of_memory = 1;
  206. }
  207. }
  208. name_maybe(R) ::= . {
  209. R = NULL;
  210. }
  211. name_maybe(R) ::= NAME(A). {
  212. R = A;
  213. }
  214. process_or_template(R) ::= PROCESS. {
  215. R = 0;
  216. }
  217. process_or_template(R) ::= TEMPLATE. {
  218. R = 1;
  219. }