NCDConfigParser_parse.y 6.6 KB

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