NCDConfigParser_parse.y 5.7 KB

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