NCDValueParser.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * @file NCDValueParser.c
  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 <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <misc/debug.h>
  33. #include <base/BLog.h>
  34. #include <ncd/NCDConfigTokenizer.h>
  35. #include "NCDValueParser.h"
  36. #include <generated/blog_channel_NCDValueParser.h>
  37. // rename non-static functions defined by our Lemon parser
  38. // to avoid clashes with other Lemon parsers
  39. #define ParseTrace ParseTrace_NCDValueParser
  40. #define ParseAlloc ParseAlloc_NCDValueParser
  41. #define ParseFree ParseFree_NCDValueParser
  42. #define Parse Parse_NCDValueParser
  43. #include "../generated/NCDValueParser_parse.c"
  44. #include "../generated/NCDValueParser_parse.h"
  45. #include <sys/stat.h>
  46. struct parser_state {
  47. struct parser_out out;
  48. int error;
  49. void *parser;
  50. };
  51. static int tokenizer_output (void *user, int token, char *value, size_t line, size_t line_char)
  52. {
  53. struct parser_state *state = (struct parser_state *)user;
  54. ASSERT(!state->out.out_of_memory)
  55. ASSERT(!state->out.syntax_error)
  56. ASSERT(!state->error)
  57. // some tokens are only valid in NCD scripts and not value strings
  58. switch (token) {
  59. case NCD_TOKEN_NAME:
  60. case NCD_TOKEN_ROUND_OPEN:
  61. case NCD_TOKEN_ROUND_CLOSE:
  62. case NCD_TOKEN_SEMICOLON:
  63. case NCD_TOKEN_DOT:
  64. case NCD_TOKEN_ARROW:
  65. case NCD_TOKEN_PROCESS:
  66. case NCD_TOKEN_TEMPLATE:
  67. token = NCD_ERROR;
  68. free(value);
  69. }
  70. if (token == NCD_ERROR) {
  71. BLog(BLOG_ERROR, "line %zu, character %zu: tokenizer error", line, line_char);
  72. goto fail;
  73. }
  74. switch (token) {
  75. case NCD_EOF: {
  76. Parse(state->parser, 0, NULL, &state->out);
  77. } break;
  78. case NCD_TOKEN_CURLY_OPEN: {
  79. Parse(state->parser, CURLY_OPEN, NULL, &state->out);
  80. } break;
  81. case NCD_TOKEN_CURLY_CLOSE: {
  82. Parse(state->parser, CURLY_CLOSE, NULL, &state->out);
  83. } break;
  84. case NCD_TOKEN_COMMA: {
  85. Parse(state->parser, COMMA, NULL, &state->out);
  86. } break;
  87. case NCD_TOKEN_STRING: {
  88. Parse(state->parser, STRING, value, &state->out);
  89. } break;
  90. default:
  91. ASSERT(0);
  92. }
  93. if (state->out.syntax_error) {
  94. BLog(BLOG_ERROR, "line %zu, character %zu: syntax error", line, line_char);
  95. goto fail;
  96. }
  97. if (state->out.out_of_memory) {
  98. BLog(BLOG_ERROR, "line %zu, character %zu: out of memory", line, line_char);
  99. goto fail;
  100. }
  101. return 1;
  102. fail:
  103. state->error = 1;
  104. return 0;
  105. }
  106. static int build_list_value (struct NCDConfig_list *list, NCDValue *out)
  107. {
  108. NCDValue list_val;
  109. NCDValue_InitList(&list_val);
  110. for (struct NCDConfig_list *elem = list; elem; elem = elem->next) {
  111. NCDValue v;
  112. if (elem->type == NCDCONFIG_ARG_STRING) {
  113. if (!NCDValue_InitString(&v, elem->string)) {
  114. BLog(BLOG_ERROR, "NCDValue_InitString failed");
  115. goto fail;
  116. }
  117. }
  118. else if (elem->type == NCDCONFIG_ARG_LIST) {
  119. if (!build_list_value(elem->list, &v)) {
  120. goto fail;
  121. }
  122. }
  123. else {
  124. ASSERT(0)
  125. }
  126. if (!NCDValue_ListAppend(&list_val, v)) {
  127. BLog(BLOG_ERROR, "NCDValue_ListAppend failed");
  128. NCDValue_Free(&v);
  129. goto fail;
  130. }
  131. }
  132. *out = list_val;
  133. return 1;
  134. fail:
  135. NCDValue_Free(&list_val);
  136. return 0;
  137. }
  138. int NCDValueParser_Parse (const char *str, size_t str_len, NCDValue *out_value)
  139. {
  140. int res = 0;
  141. // init parse state
  142. struct parser_state state;
  143. state.out.out_of_memory = 0;
  144. state.out.syntax_error = 0;
  145. state.out.ast_type = AST_TYPE_NONE;
  146. state.out.ast_string = NULL;
  147. state.out.ast_list = NULL;
  148. state.error = 0;
  149. // allocate parser
  150. if (!(state.parser = ParseAlloc(malloc))) {
  151. BLog(BLOG_ERROR, "ParseAlloc failed");
  152. goto out;
  153. }
  154. // tokenize and parse
  155. NCDConfigTokenizer_Tokenize((char *)str, str_len, tokenizer_output, &state);
  156. // check for errors
  157. if (state.error) {
  158. goto out;
  159. }
  160. ASSERT(state.out.ast_type == AST_TYPE_STRING || state.out.ast_type == AST_TYPE_LIST)
  161. // convert AST to value
  162. NCDValue val;
  163. if (state.out.ast_type == AST_TYPE_STRING) {
  164. if (!NCDValue_InitString(&val, state.out.ast_string)) {
  165. BLog(BLOG_ERROR, "NCDValue_InitString failed");
  166. goto out;
  167. }
  168. } else {
  169. if (!build_list_value(state.out.ast_list, &val)) {
  170. goto out;
  171. }
  172. }
  173. *out_value = val;
  174. res = 1;
  175. out:
  176. if (state.parser) {
  177. ParseFree(state.parser, free);
  178. }
  179. free(state.out.ast_string);
  180. NCDConfig_free_list(state.out.ast_list);
  181. return res;
  182. }