NCDConfigParser_parse.y 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_interfaces *ast;
  31. };
  32. }
  33. %extra_argument {struct parser_out *parser_out}
  34. %token_type {void *}
  35. %token_destructor { free($$); }
  36. %type interfaces {struct NCDConfig_interfaces *}
  37. %type statements {struct NCDConfig_statements *}
  38. %type statement_names {struct NCDConfig_strings *}
  39. %type statement_args_maybe {struct NCDConfig_arguments *}
  40. %type statement_args {struct NCDConfig_arguments *}
  41. %type name_maybe {char *}
  42. %type process_or_template {int}
  43. %destructor interfaces { NCDConfig_free_interfaces($$); }
  44. %destructor statements { NCDConfig_free_statements($$); }
  45. %destructor statement_names { NCDConfig_free_strings($$); }
  46. %destructor statement_args_maybe { NCDConfig_free_arguments($$); }
  47. %destructor statement_args { NCDConfig_free_arguments($$); }
  48. %destructor name_maybe { free($$); }
  49. %stack_size 0
  50. %syntax_error {
  51. parser_out->syntax_error = 1;
  52. }
  53. // workaroud Lemon bug: if the stack overflows, the token that caused the overflow will be leaked
  54. %stack_overflow {
  55. if (yypMinor) {
  56. free(yypMinor->yy0);
  57. }
  58. }
  59. input ::= interfaces(A). {
  60. parser_out->ast = A;
  61. if (!A) {
  62. parser_out->out_of_memory = 1;
  63. }
  64. }
  65. interfaces(R) ::= process_or_template(T) NAME(A) CURLY_OPEN statements(B) CURLY_CLOSE. {
  66. R = NCDConfig_make_interfaces(T, A, B, 0, NULL);
  67. if (!R) {
  68. parser_out->out_of_memory = 1;
  69. }
  70. }
  71. interfaces(R) ::= process_or_template(T) NAME(A) CURLY_OPEN statements(B) CURLY_CLOSE interfaces(N). {
  72. R = NCDConfig_make_interfaces(T, A, B, 1, N);
  73. if (!R) {
  74. parser_out->out_of_memory = 1;
  75. }
  76. }
  77. statements(R) ::= statement_names(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON. {
  78. R = NCDConfig_make_statements(NULL, A, B, C, NULL);
  79. if (!R) {
  80. parser_out->out_of_memory = 1;
  81. }
  82. }
  83. statements(R) ::= statement_names(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON statements(N). {
  84. R = NCDConfig_make_statements(NULL, A, B, C, N);
  85. if (!R) {
  86. parser_out->out_of_memory = 1;
  87. }
  88. }
  89. statements(R) ::= statement_names(M) ARROW statement_names(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON. {
  90. R = NCDConfig_make_statements(M, A, B, C, NULL);
  91. if (!R) {
  92. parser_out->out_of_memory = 1;
  93. }
  94. }
  95. statements(R) ::= statement_names(M) ARROW statement_names(A) ROUND_OPEN statement_args_maybe(B) ROUND_CLOSE name_maybe(C) SEMICOLON statements(N). {
  96. R = NCDConfig_make_statements(M, A, B, C, N);
  97. if (!R) {
  98. parser_out->out_of_memory = 1;
  99. }
  100. }
  101. statement_names(R) ::= NAME(A). {
  102. R = NCDConfig_make_strings(A, 0, NULL);
  103. if (!R) {
  104. parser_out->out_of_memory = 1;
  105. }
  106. }
  107. statement_names(R) ::= NAME(A) DOT statement_names(N). {
  108. R = NCDConfig_make_strings(A, 1, N);
  109. if (!R) {
  110. parser_out->out_of_memory = 1;
  111. }
  112. }
  113. statement_args_maybe(R) ::= . {
  114. R = NULL;
  115. }
  116. statement_args_maybe(R) ::= statement_args(A). {
  117. R = A;
  118. }
  119. statement_args(R) ::= STRING(A). {
  120. R = NCDConfig_make_arguments_string(A, NULL);
  121. if (!R) {
  122. parser_out->out_of_memory = 1;
  123. }
  124. }
  125. statement_args(R) ::= statement_names(A). {
  126. R = NCDConfig_make_arguments_var(A, NULL);
  127. if (!R) {
  128. parser_out->out_of_memory = 1;
  129. }
  130. }
  131. statement_args(R) ::= STRING(A) COMMA statement_args(N). {
  132. R = NCDConfig_make_arguments_string(A, N);
  133. if (!R) {
  134. parser_out->out_of_memory = 1;
  135. }
  136. }
  137. statement_args(R) ::= statement_names(A) COMMA statement_args(N). {
  138. R = NCDConfig_make_arguments_var(A, N);
  139. if (!R) {
  140. parser_out->out_of_memory = 1;
  141. }
  142. }
  143. name_maybe(R) ::= . {
  144. R = NULL;
  145. }
  146. name_maybe(R) ::= NAME(A). {
  147. R = A;
  148. }
  149. process_or_template(R) ::= PROCESS. {
  150. R = 0;
  151. }
  152. process_or_template(R) ::= TEMPLATE. {
  153. R = 1;
  154. }