NCDValueParser_parse.y 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * @file NCDConfigParser_parse.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. #define AST_TYPE_NONE 0
  35. #define AST_TYPE_STRING 1
  36. #define AST_TYPE_LIST 2
  37. #define AST_TYPE_MAP 3
  38. struct parser_minor {
  39. char *str;
  40. size_t len;
  41. };
  42. struct parser_out {
  43. int out_of_memory;
  44. int syntax_error;
  45. int ast_type;
  46. struct parser_minor ast_string;
  47. struct NCDConfig_list *ast_list;
  48. };
  49. }
  50. %extra_argument {struct parser_out *parser_out}
  51. %token_type {struct parser_minor}
  52. %token_destructor { free($$.str); }
  53. %type list_contents {struct NCDConfig_list *}
  54. %type list {struct NCDConfig_list *}
  55. %type map_contents {struct NCDConfig_list *}
  56. %type map {struct NCDConfig_list *}
  57. %type value {struct NCDConfig_list *}
  58. %destructor list_contents { NCDConfig_free_list($$); }
  59. %destructor list { NCDConfig_free_list($$); }
  60. %destructor map_contents { NCDConfig_free_list($$); }
  61. %destructor map { NCDConfig_free_list($$); }
  62. %destructor value { NCDConfig_free_list($$); }
  63. %stack_size 0
  64. %syntax_error {
  65. parser_out->syntax_error = 1;
  66. }
  67. // workaroud Lemon bug: if the stack overflows, the token that caused the overflow will be leaked
  68. %stack_overflow {
  69. if (yypMinor) {
  70. free(yypMinor->yy0.str);
  71. }
  72. }
  73. input ::= STRING(A). {
  74. ASSERT(parser_out->ast_type == AST_TYPE_NONE)
  75. parser_out->ast_string = A;
  76. parser_out->ast_type = AST_TYPE_STRING;
  77. }
  78. input ::= list(A). {
  79. ASSERT(parser_out->ast_type == AST_TYPE_NONE)
  80. parser_out->ast_list = A;
  81. parser_out->ast_type = AST_TYPE_LIST;
  82. }
  83. input ::= map(A). {
  84. ASSERT(parser_out->ast_type == AST_TYPE_NONE)
  85. parser_out->ast_list = A;
  86. parser_out->ast_type = AST_TYPE_MAP;
  87. }
  88. list_contents(R) ::= value(A). {
  89. R = A;
  90. }
  91. list_contents(R) ::= value(A) COMMA list_contents(N). {
  92. if (!A) {
  93. NCDConfig_free_list(N);
  94. } else {
  95. ASSERT(!A->next)
  96. A->next = N;
  97. }
  98. R = A;
  99. }
  100. map_contents(R) ::= value(A) COLON value(B). {
  101. if (!A || !B) {
  102. NCDConfig_free_list(A);
  103. NCDConfig_free_list(B);
  104. R = NULL;
  105. } else {
  106. ASSERT(!A->next)
  107. ASSERT(!B->next)
  108. A->next = B;
  109. R = A;
  110. }
  111. }
  112. map_contents(R) ::= value(A) COLON value(B) COMMA map_contents(N). {
  113. if (!A || !B) {
  114. NCDConfig_free_list(A);
  115. NCDConfig_free_list(B);
  116. NCDConfig_free_list(N);
  117. R = NULL;
  118. } else {
  119. ASSERT(!A->next)
  120. ASSERT(!B->next)
  121. A->next = B;
  122. B->next = N;
  123. R = A;
  124. }
  125. }
  126. list(R) ::= CURLY_OPEN CURLY_CLOSE. {
  127. R = NULL;
  128. }
  129. list(R) ::= CURLY_OPEN list_contents(A) CURLY_CLOSE. {
  130. R = A;
  131. }
  132. map(R) ::= BRACKET_OPEN BRACKET_CLOSE. {
  133. R = NULL;
  134. }
  135. map(R) ::= BRACKET_OPEN map_contents(A) BRACKET_CLOSE. {
  136. R = A;
  137. }
  138. value(R) ::= STRING(A). {
  139. R = NCDConfig_make_list_string(A.str, A.len, NULL);
  140. if (!R) {
  141. parser_out->out_of_memory = 1;
  142. }
  143. }
  144. value(R) ::= list(A). {
  145. R = NCDConfig_make_list_list(A, NULL);
  146. if (!R) {
  147. parser_out->out_of_memory = 1;
  148. }
  149. }
  150. value(R) ::= map(A). {
  151. R = NCDConfig_make_list_maplist(A, NULL);
  152. if (!R) {
  153. parser_out->out_of_memory = 1;
  154. }
  155. }