NCDConfigParser_parse.y 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <ncdconfig/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 {struct NCDConfig_arguments *}
  40. %destructor interfaces { NCDConfig_free_interfaces($$); }
  41. %destructor statements { NCDConfig_free_statements($$); }
  42. %destructor statement_names { NCDConfig_free_strings($$); }
  43. %destructor statement_args { NCDConfig_free_arguments($$); }
  44. %stack_size 1000
  45. %syntax_error {
  46. parser_out->syntax_error = 1;
  47. }
  48. // workaroud Lemon bug: if the stack overflows, the token that caused the overflow will be leaked
  49. %stack_overflow {
  50. if (yypMinor) {
  51. free(yypMinor->yy0);
  52. }
  53. }
  54. input ::= interfaces(A). {
  55. parser_out->ast = A;
  56. if (!A) {
  57. parser_out->out_of_memory = 1;
  58. }
  59. }
  60. interfaces(R) ::= PROCESS NAME(A) CURLY_OPEN statements(B) CURLY_CLOSE. {
  61. R = NCDConfig_make_interfaces(A, B, 0, NULL);
  62. if (!R) {
  63. parser_out->out_of_memory = 1;
  64. }
  65. }
  66. interfaces(R) ::= PROCESS NAME(A) CURLY_OPEN statements(B) CURLY_CLOSE interfaces(N). {
  67. R = NCDConfig_make_interfaces(A, B, 1, N);
  68. if (!R) {
  69. parser_out->out_of_memory = 1;
  70. }
  71. }
  72. statements(R) ::= statement_names(A) ROUND_OPEN ROUND_CLOSE SEMICOLON. {
  73. R = NCDConfig_make_statements(A, NULL, NULL, NULL);
  74. if (!R) {
  75. parser_out->out_of_memory = 1;
  76. }
  77. }
  78. statements(R) ::= statement_names(A) ROUND_OPEN statement_args(B) ROUND_CLOSE SEMICOLON. {
  79. R = NCDConfig_make_statements(A, B, NULL, NULL);
  80. if (!R) {
  81. parser_out->out_of_memory = 1;
  82. }
  83. }
  84. statements(R) ::= statement_names(A) ROUND_OPEN ROUND_CLOSE SEMICOLON statements(N). {
  85. R = NCDConfig_make_statements(A, NULL, NULL, N);
  86. if (!R) {
  87. parser_out->out_of_memory = 1;
  88. }
  89. }
  90. statements(R) ::= statement_names(A) ROUND_OPEN statement_args(B) ROUND_CLOSE SEMICOLON statements(N). {
  91. R = NCDConfig_make_statements(A, B, NULL, N);
  92. if (!R) {
  93. parser_out->out_of_memory = 1;
  94. }
  95. }
  96. statements(R) ::= statement_names(A) ROUND_OPEN ROUND_CLOSE NAME(C) SEMICOLON. {
  97. R = NCDConfig_make_statements(A, NULL, C, NULL);
  98. if (!R) {
  99. parser_out->out_of_memory = 1;
  100. }
  101. }
  102. statements(R) ::= statement_names(A) ROUND_OPEN statement_args(B) ROUND_CLOSE NAME(C) SEMICOLON. {
  103. R = NCDConfig_make_statements(A, B, C, NULL);
  104. if (!R) {
  105. parser_out->out_of_memory = 1;
  106. }
  107. }
  108. statements(R) ::= statement_names(A) ROUND_OPEN ROUND_CLOSE NAME(C) SEMICOLON statements(N). {
  109. R = NCDConfig_make_statements(A, NULL, C, N);
  110. if (!R) {
  111. parser_out->out_of_memory = 1;
  112. }
  113. }
  114. statements(R) ::= statement_names(A) ROUND_OPEN statement_args(B) ROUND_CLOSE NAME(C) SEMICOLON statements(N). {
  115. R = NCDConfig_make_statements(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, 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, 1, N);
  128. if (!R) {
  129. parser_out->out_of_memory = 1;
  130. }
  131. }
  132. statement_args(R) ::= STRING(A). {
  133. R = NCDConfig_make_arguments_string(A, 0, NULL);
  134. if (!R) {
  135. parser_out->out_of_memory = 1;
  136. }
  137. }
  138. statement_args(R) ::= statement_names(A). {
  139. R = NCDConfig_make_arguments_var(A, 0, NULL);
  140. if (!R) {
  141. parser_out->out_of_memory = 1;
  142. }
  143. }
  144. statement_args(R) ::= STRING(A) COMMA statement_args(N). {
  145. R = NCDConfig_make_arguments_string(A, 1, N);
  146. if (!R) {
  147. parser_out->out_of_memory = 1;
  148. }
  149. }
  150. statement_args(R) ::= statement_names(A) COMMA statement_args(N). {
  151. R = NCDConfig_make_arguments_var(A, 1, N);
  152. if (!R) {
  153. parser_out->out_of_memory = 1;
  154. }
  155. }