NCDValueParser_parse.y 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. %destructor list_contents { NCDConfig_free_list($$); }
  51. %destructor list { NCDConfig_free_list($$); }
  52. %stack_size 0
  53. %syntax_error {
  54. parser_out->syntax_error = 1;
  55. }
  56. // workaroud Lemon bug: if the stack overflows, the token that caused the overflow will be leaked
  57. %stack_overflow {
  58. if (yypMinor) {
  59. free(yypMinor->yy0);
  60. }
  61. }
  62. input ::= STRING(A). {
  63. ASSERT(parser_out->ast_type == AST_TYPE_NONE)
  64. parser_out->ast_string = A;
  65. parser_out->ast_type = AST_TYPE_STRING;
  66. }
  67. input ::= list(A). {
  68. ASSERT(parser_out->ast_type == AST_TYPE_NONE)
  69. parser_out->ast_list = A;
  70. parser_out->ast_type = AST_TYPE_LIST;
  71. }
  72. list_contents(R) ::= STRING(A). {
  73. R = NCDConfig_make_list_string(A, NULL);
  74. if (!R) {
  75. parser_out->out_of_memory = 1;
  76. }
  77. }
  78. list_contents(R) ::= list(A). {
  79. R = NCDConfig_make_list_list(A, NULL);
  80. if (!R) {
  81. parser_out->out_of_memory = 1;
  82. }
  83. }
  84. list_contents(R) ::= STRING(A) COMMA list_contents(N). {
  85. R = NCDConfig_make_list_string(A, N);
  86. if (!R) {
  87. parser_out->out_of_memory = 1;
  88. }
  89. }
  90. list_contents(R) ::= list(A) COMMA list_contents(N). {
  91. R = NCDConfig_make_list_list(A, N);
  92. if (!R) {
  93. parser_out->out_of_memory = 1;
  94. }
  95. }
  96. list(R) ::= CURLY_OPEN CURLY_CLOSE. {
  97. R = NULL;
  98. }
  99. list(R) ::= CURLY_OPEN list_contents(A) CURLY_CLOSE. {
  100. R = A;
  101. }