NCDConfigParser_parse.y 6.8 KB

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