NCDConfigParser_parse.y 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 name_maybe {char *}
  50. %type process_or_template {int}
  51. %destructor processes { NCDConfig_free_processes($$); }
  52. %destructor statements { NCDConfig_free_statements($$); }
  53. %destructor statement_names { NCDConfig_free_strings($$); }
  54. %destructor statement_args_maybe { NCDConfig_free_list($$); }
  55. %destructor list_contents { NCDConfig_free_list($$); }
  56. %destructor list { NCDConfig_free_list($$); }
  57. %destructor name_maybe { free($$); }
  58. %stack_size 0
  59. %syntax_error {
  60. parser_out->syntax_error = 1;
  61. }
  62. // workaroud Lemon bug: if the stack overflows, the token that caused the overflow will be leaked
  63. %stack_overflow {
  64. if (yypMinor) {
  65. free(yypMinor->yy0);
  66. }
  67. }
  68. input ::= processes(A). {
  69. parser_out->ast = A;
  70. if (!A) {
  71. parser_out->out_of_memory = 1;
  72. }
  73. }
  74. processes(R) ::= process_or_template(T) NAME(A) CURLY_OPEN statements(B) CURLY_CLOSE. {
  75. R = NCDConfig_make_processes(T, A, B, 0, NULL);
  76. if (!R) {
  77. parser_out->out_of_memory = 1;
  78. }
  79. }
  80. processes(R) ::= process_or_template(T) NAME(A) CURLY_OPEN statements(B) CURLY_CLOSE processes(N). {
  81. R = NCDConfig_make_processes(T, A, B, 1, N);
  82. if (!R) {
  83. parser_out->out_of_memory = 1;
  84. }
  85. }
  86. statements(R) ::= statement_names(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON. {
  87. R = NCDConfig_make_statements(NULL, A, B, C, NULL);
  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 statements(N). {
  93. R = NCDConfig_make_statements(NULL, A, B, C, N);
  94. if (!R) {
  95. parser_out->out_of_memory = 1;
  96. }
  97. }
  98. statements(R) ::= statement_names(M) ARROW statement_names(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON. {
  99. R = NCDConfig_make_statements(M, A, B, C, NULL);
  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 statements(N). {
  105. R = NCDConfig_make_statements(M, A, B, C, N);
  106. if (!R) {
  107. parser_out->out_of_memory = 1;
  108. }
  109. }
  110. statement_names(R) ::= NAME(A). {
  111. R = NCDConfig_make_strings(A, 0, NULL);
  112. if (!R) {
  113. parser_out->out_of_memory = 1;
  114. }
  115. }
  116. statement_names(R) ::= NAME(A) DOT statement_names(N). {
  117. R = NCDConfig_make_strings(A, 1, N);
  118. if (!R) {
  119. parser_out->out_of_memory = 1;
  120. }
  121. }
  122. statement_args_maybe(R) ::= . {
  123. R = NULL;
  124. }
  125. statement_args_maybe(R) ::= list_contents(A). {
  126. R = A;
  127. }
  128. list_contents(R) ::= STRING(A). {
  129. R = NCDConfig_make_list_string(A, NULL);
  130. if (!R) {
  131. parser_out->out_of_memory = 1;
  132. }
  133. }
  134. list_contents(R) ::= statement_names(A). {
  135. R = NCDConfig_make_list_var(A, NULL);
  136. if (!R) {
  137. parser_out->out_of_memory = 1;
  138. }
  139. }
  140. list_contents(R) ::= list(A). {
  141. R = NCDConfig_make_list_list(A, NULL);
  142. if (!R) {
  143. parser_out->out_of_memory = 1;
  144. }
  145. }
  146. list_contents(R) ::= STRING(A) COMMA list_contents(N). {
  147. R = NCDConfig_make_list_string(A, N);
  148. if (!R) {
  149. parser_out->out_of_memory = 1;
  150. }
  151. }
  152. list_contents(R) ::= statement_names(A) COMMA list_contents(N). {
  153. R = NCDConfig_make_list_var(A, N);
  154. if (!R) {
  155. parser_out->out_of_memory = 1;
  156. }
  157. }
  158. list_contents(R) ::= list(A) COMMA list_contents(N). {
  159. R = NCDConfig_make_list_list(A, N);
  160. if (!R) {
  161. parser_out->out_of_memory = 1;
  162. }
  163. }
  164. list(R) ::= CURLY_OPEN CURLY_CLOSE. {
  165. R = NULL;
  166. }
  167. list(R) ::= CURLY_OPEN list_contents(A) CURLY_CLOSE. {
  168. R = A;
  169. }
  170. name_maybe(R) ::= . {
  171. R = NULL;
  172. }
  173. name_maybe(R) ::= NAME(A). {
  174. R = A;
  175. }
  176. process_or_template(R) ::= PROCESS. {
  177. R = 0;
  178. }
  179. process_or_template(R) ::= TEMPLATE. {
  180. R = 1;
  181. }