NCDConfigParser_parse.y 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @file NCDConfigParser.y
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. %include {
  23. #include <string.h>
  24. #include <stddef.h>
  25. #include <misc/debug.h>
  26. #include <ncd/NCDConfig.h>
  27. struct parser_out {
  28. int out_of_memory;
  29. int syntax_error;
  30. struct NCDConfig_processes *ast;
  31. };
  32. }
  33. %extra_argument {struct parser_out *parser_out}
  34. %token_type {void *}
  35. %token_destructor { free($$); }
  36. %type processes {struct NCDConfig_processes *}
  37. %type statements {struct NCDConfig_statements *}
  38. %type statement_names {struct NCDConfig_strings *}
  39. %type statement_args_maybe {struct NCDConfig_list *}
  40. %type list_contents {struct NCDConfig_list *}
  41. %type list {struct NCDConfig_list *}
  42. %type name_maybe {char *}
  43. %type process_or_template {int}
  44. %destructor processes { NCDConfig_free_processes($$); }
  45. %destructor statements { NCDConfig_free_statements($$); }
  46. %destructor statement_names { NCDConfig_free_strings($$); }
  47. %destructor statement_args_maybe { NCDConfig_free_list($$); }
  48. %destructor list_contents { NCDConfig_free_list($$); }
  49. %destructor list { NCDConfig_free_list($$); }
  50. %destructor name_maybe { free($$); }
  51. %stack_size 0
  52. %syntax_error {
  53. parser_out->syntax_error = 1;
  54. }
  55. // workaroud Lemon bug: if the stack overflows, the token that caused the overflow will be leaked
  56. %stack_overflow {
  57. if (yypMinor) {
  58. free(yypMinor->yy0);
  59. }
  60. }
  61. input ::= processes(A). {
  62. parser_out->ast = A;
  63. if (!A) {
  64. parser_out->out_of_memory = 1;
  65. }
  66. }
  67. processes(R) ::= process_or_template(T) NAME(A) CURLY_OPEN statements(B) CURLY_CLOSE. {
  68. R = NCDConfig_make_processes(T, A, B, 0, NULL);
  69. if (!R) {
  70. parser_out->out_of_memory = 1;
  71. }
  72. }
  73. processes(R) ::= process_or_template(T) NAME(A) CURLY_OPEN statements(B) CURLY_CLOSE processes(N). {
  74. R = NCDConfig_make_processes(T, A, B, 1, N);
  75. if (!R) {
  76. parser_out->out_of_memory = 1;
  77. }
  78. }
  79. statements(R) ::= statement_names(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON. {
  80. R = NCDConfig_make_statements(NULL, A, B, C, NULL);
  81. if (!R) {
  82. parser_out->out_of_memory = 1;
  83. }
  84. }
  85. statements(R) ::= statement_names(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON statements(N). {
  86. R = NCDConfig_make_statements(NULL, A, B, C, N);
  87. if (!R) {
  88. parser_out->out_of_memory = 1;
  89. }
  90. }
  91. statements(R) ::= statement_names(M) ARROW statement_names(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON. {
  92. R = NCDConfig_make_statements(M, A, B, C, NULL);
  93. if (!R) {
  94. parser_out->out_of_memory = 1;
  95. }
  96. }
  97. statements(R) ::= statement_names(M) ARROW statement_names(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON statements(N). {
  98. R = NCDConfig_make_statements(M, A, B, C, N);
  99. if (!R) {
  100. parser_out->out_of_memory = 1;
  101. }
  102. }
  103. statement_names(R) ::= NAME(A). {
  104. R = NCDConfig_make_strings(A, 0, NULL);
  105. if (!R) {
  106. parser_out->out_of_memory = 1;
  107. }
  108. }
  109. statement_names(R) ::= NAME(A) DOT statement_names(N). {
  110. R = NCDConfig_make_strings(A, 1, N);
  111. if (!R) {
  112. parser_out->out_of_memory = 1;
  113. }
  114. }
  115. statement_args_maybe(R) ::= . {
  116. R = NULL;
  117. }
  118. statement_args_maybe(R) ::= list_contents(A). {
  119. R = A;
  120. }
  121. list_contents(R) ::= STRING(A). {
  122. R = NCDConfig_make_list_string(A, NULL);
  123. if (!R) {
  124. parser_out->out_of_memory = 1;
  125. }
  126. }
  127. list_contents(R) ::= statement_names(A). {
  128. R = NCDConfig_make_list_var(A, NULL);
  129. if (!R) {
  130. parser_out->out_of_memory = 1;
  131. }
  132. }
  133. list_contents(R) ::= list(A). {
  134. R = NCDConfig_make_list_list(A, NULL);
  135. if (!R) {
  136. parser_out->out_of_memory = 1;
  137. }
  138. }
  139. list_contents(R) ::= STRING(A) COMMA list_contents(N). {
  140. R = NCDConfig_make_list_string(A, N);
  141. if (!R) {
  142. parser_out->out_of_memory = 1;
  143. }
  144. }
  145. list_contents(R) ::= statement_names(A) COMMA list_contents(N). {
  146. R = NCDConfig_make_list_var(A, N);
  147. if (!R) {
  148. parser_out->out_of_memory = 1;
  149. }
  150. }
  151. list_contents(R) ::= list(A) COMMA list_contents(N). {
  152. R = NCDConfig_make_list_list(A, N);
  153. if (!R) {
  154. parser_out->out_of_memory = 1;
  155. }
  156. }
  157. list(R) ::= CURLY_OPEN CURLY_CLOSE. {
  158. R = NULL;
  159. }
  160. list(R) ::= CURLY_OPEN list_contents(A) CURLY_CLOSE. {
  161. R = A;
  162. }
  163. name_maybe(R) ::= . {
  164. R = NULL;
  165. }
  166. name_maybe(R) ::= NAME(A). {
  167. R = A;
  168. }
  169. process_or_template(R) ::= PROCESS. {
  170. R = 0;
  171. }
  172. process_or_template(R) ::= TEMPLATE. {
  173. R = 1;
  174. }