NCDValueParser_parse.y 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. struct parser_out {
  38. int out_of_memory;
  39. int syntax_error;
  40. int ast_type;
  41. char *ast_string;
  42. struct NCDConfig_list *ast_list;
  43. };
  44. }
  45. %extra_argument {struct parser_out *parser_out}
  46. %token_type {void *}
  47. %token_destructor { free($$); }
  48. %type list_contents {struct NCDConfig_list *}
  49. %type list {struct NCDConfig_list *}
  50. %type value {struct NCDConfig_list *}
  51. %destructor list_contents { NCDConfig_free_list($$); }
  52. %destructor list { NCDConfig_free_list($$); }
  53. %destructor value { NCDConfig_free_list($$); }
  54. %stack_size 0
  55. %syntax_error {
  56. parser_out->syntax_error = 1;
  57. }
  58. // workaroud Lemon bug: if the stack overflows, the token that caused the overflow will be leaked
  59. %stack_overflow {
  60. if (yypMinor) {
  61. free(yypMinor->yy0);
  62. }
  63. }
  64. input ::= STRING(A). {
  65. ASSERT(parser_out->ast_type == AST_TYPE_NONE)
  66. parser_out->ast_string = A;
  67. parser_out->ast_type = AST_TYPE_STRING;
  68. }
  69. input ::= list(A). {
  70. ASSERT(parser_out->ast_type == AST_TYPE_NONE)
  71. parser_out->ast_list = A;
  72. parser_out->ast_type = AST_TYPE_LIST;
  73. }
  74. list_contents(R) ::= value(A). {
  75. R = A;
  76. }
  77. list_contents(R) ::= value(A) COMMA list_contents(N). {
  78. if (!A) {
  79. NCDConfig_free_list(N);
  80. } else {
  81. ASSERT(!A->next)
  82. A->next = N;
  83. }
  84. R = A;
  85. }
  86. list(R) ::= CURLY_OPEN CURLY_CLOSE. {
  87. R = NULL;
  88. }
  89. list(R) ::= CURLY_OPEN list_contents(A) CURLY_CLOSE. {
  90. R = A;
  91. }
  92. value(R) ::= STRING(A). {
  93. R = NCDConfig_make_list_string(A, NULL);
  94. if (!R) {
  95. parser_out->out_of_memory = 1;
  96. }
  97. }
  98. value(R) ::= list(A). {
  99. R = NCDConfig_make_list_list(A, NULL);
  100. if (!R) {
  101. parser_out->out_of_memory = 1;
  102. }
  103. }