BPredicate.y 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /**
  2. * @file BPredicate.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. * @section DESCRIPTION
  23. *
  24. * {@link BPredicate} grammar file.
  25. */
  26. %{
  27. #include <stdlib.h>
  28. #include <predicate/BPredicate_internal.h>
  29. #include <predicate/BPredicate_parser.h>
  30. #define YYLEX_PARAM scanner
  31. static struct predicate_node * make_constant (int val)
  32. {
  33. struct predicate_node *n = malloc(sizeof(*n));
  34. if (!n) {
  35. return NULL;
  36. }
  37. n->type = NODE_CONSTANT;
  38. n->constant.val = val;
  39. return n;
  40. }
  41. static struct predicate_node * make_negation (struct predicate_node *op)
  42. {
  43. if (!op) {
  44. goto fail;
  45. }
  46. struct predicate_node *n = malloc(sizeof(*n));
  47. if (!n) {
  48. goto fail;
  49. }
  50. n->type = NODE_NEG;
  51. n->neg.op = op;
  52. return n;
  53. fail:
  54. if (op) {
  55. free_predicate_node(op);
  56. }
  57. return NULL;
  58. }
  59. static struct predicate_node * make_conjunction (struct predicate_node *op1, struct predicate_node *op2)
  60. {
  61. if (!op1 || !op2) {
  62. goto fail;
  63. }
  64. struct predicate_node *n = malloc(sizeof(*n));
  65. if (!n) {
  66. goto fail;
  67. }
  68. n->type = NODE_CONJUNCT;
  69. n->conjunct.op1 = op1;
  70. n->conjunct.op2 = op2;
  71. return n;
  72. fail:
  73. if (op1) {
  74. free_predicate_node(op1);
  75. }
  76. if (op2) {
  77. free_predicate_node(op2);
  78. }
  79. return NULL;
  80. }
  81. static struct predicate_node * make_disjunction (struct predicate_node *op1, struct predicate_node *op2)
  82. {
  83. if (!op1 || !op2) {
  84. goto fail;
  85. }
  86. struct predicate_node *n = malloc(sizeof(*n));
  87. if (!n) {
  88. goto fail;
  89. }
  90. n->type = NODE_DISJUNCT;
  91. n->disjunct.op1 = op1;
  92. n->disjunct.op2 = op2;
  93. return n;
  94. fail:
  95. if (op1) {
  96. free_predicate_node(op1);
  97. }
  98. if (op2) {
  99. free_predicate_node(op2);
  100. }
  101. return NULL;
  102. }
  103. static struct predicate_node * make_function (char *name, struct arguments_node *args, int need_args)
  104. {
  105. if (!name || (!args && need_args)) {
  106. goto fail;
  107. }
  108. struct predicate_node *n = malloc(sizeof(*n));
  109. if (!n) {
  110. goto fail;
  111. }
  112. n->type = NODE_FUNCTION;
  113. n->function.name = name;
  114. n->function.args = args;
  115. return n;
  116. fail:
  117. if (name) {
  118. free(name);
  119. }
  120. if (args) {
  121. free_arguments_node(args);
  122. }
  123. return NULL;
  124. }
  125. static struct arguments_node * make_arguments (struct arguments_arg arg, struct arguments_node *next, int need_next)
  126. {
  127. if (arg.type == ARGUMENT_INVALID || (!next && need_next)) {
  128. goto fail;
  129. }
  130. struct arguments_node *n = malloc(sizeof(*n));
  131. if (!n) {
  132. goto fail;
  133. }
  134. n->arg = arg;
  135. n->next = next;
  136. return n;
  137. fail:
  138. free_argument(arg);
  139. if (next) {
  140. free_arguments_node(next);
  141. }
  142. return NULL;
  143. }
  144. static struct arguments_arg make_argument_predicate (struct predicate_node *pr)
  145. {
  146. struct arguments_arg ret;
  147. if (!pr) {
  148. goto fail;
  149. }
  150. ret.type = ARGUMENT_PREDICATE;
  151. ret.predicate = pr;
  152. return ret;
  153. fail:
  154. ret.type = ARGUMENT_INVALID;
  155. return ret;
  156. }
  157. static struct arguments_arg make_argument_string (char *string)
  158. {
  159. struct arguments_arg ret;
  160. if (!string) {
  161. goto fail;
  162. }
  163. ret.type = ARGUMENT_STRING;
  164. ret.string = string;
  165. return ret;
  166. fail:
  167. ret.type = ARGUMENT_INVALID;
  168. return ret;
  169. }
  170. %}
  171. %pure-parser
  172. %locations
  173. %parse-param {void *scanner}
  174. %parse-param {struct predicate_node **result}
  175. %union {
  176. char *text;
  177. struct predicate_node *node;
  178. struct arguments_node *arg_node;
  179. struct predicate_node nfaw;
  180. struct arguments_arg arg_arg;
  181. };
  182. // token types
  183. %token <text> STRING NAME
  184. %token PEER1_NAME PEER2_NAME AND OR NOT SPAR EPAR CONSTANT_TRUE CONSTANT_FALSE COMMA
  185. // string token destructor
  186. %destructor {
  187. free($$);
  188. } STRING NAME
  189. // return values
  190. %type <node> predicate constant parentheses neg conjunct disjunct function
  191. %type <arg_node> arguments
  192. %type <arg_arg> argument
  193. // predicate node destructor
  194. %destructor {
  195. if ($$) {
  196. free_predicate_node($$);
  197. }
  198. } predicate constant parentheses neg conjunct disjunct function
  199. // argument node destructor
  200. %destructor {
  201. if ($$) {
  202. free_arguments_node($$);
  203. }
  204. } arguments
  205. // argument argument destructor
  206. %destructor {
  207. free_argument($$);
  208. } argument
  209. %left OR
  210. %left AND
  211. %nonassoc NOT
  212. %right COMMA
  213. %%
  214. input:
  215. predicate {
  216. *result = $1;
  217. }
  218. ;
  219. predicate: constant | parentheses | neg | conjunct | disjunct | function;
  220. constant:
  221. CONSTANT_TRUE {
  222. $$ = make_constant(1);
  223. }
  224. |
  225. CONSTANT_FALSE {
  226. $$ = make_constant(0);
  227. }
  228. ;
  229. parentheses:
  230. SPAR predicate EPAR {
  231. $$ = $2;
  232. }
  233. ;
  234. neg:
  235. NOT predicate {
  236. $$ = make_negation($2);
  237. }
  238. ;
  239. conjunct:
  240. predicate AND predicate {
  241. $$ = make_conjunction($1, $3);
  242. }
  243. ;
  244. disjunct:
  245. predicate OR predicate {
  246. $$ = make_disjunction($1, $3);
  247. }
  248. ;
  249. function:
  250. NAME SPAR EPAR {
  251. $$ = make_function($1, NULL, 0);
  252. }
  253. |
  254. NAME SPAR arguments EPAR {
  255. $$ = make_function($1, $3, 1);
  256. }
  257. ;
  258. arguments:
  259. argument {
  260. $$ = make_arguments($1, NULL, 0);
  261. }
  262. |
  263. argument COMMA arguments {
  264. $$ = make_arguments($1, $3, 1);
  265. }
  266. ;
  267. argument:
  268. predicate {
  269. $$ = make_argument_predicate($1);
  270. }
  271. |
  272. STRING {
  273. $$ = make_argument_string($1);
  274. }
  275. ;