NCDValueParser_parse.y 4.5 KB

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