NCDValueParser.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. struct parser_state {
  46. struct parser_out out;
  47. int error;
  48. void *parser;
  49. };
  50. static int tokenizer_output (void *user, int token, char *value, size_t value_len, size_t line, size_t line_char);
  51. static int build_value (struct NCDConfig_list *ast, NCDValue *out);
  52. static int build_list_value (struct NCDConfig_list *list, NCDValue *out);
  53. static int build_map_value (struct NCDConfig_list *list, NCDValue *out);
  54. static int tokenizer_output (void *user, int token, char *value, size_t value_len, size_t line, size_t line_char)
  55. {
  56. struct parser_state *state = user;
  57. ASSERT(!state->out.out_of_memory)
  58. ASSERT(!state->out.syntax_error)
  59. ASSERT(!state->error)
  60. if (token == NCD_ERROR) {
  61. BLog(BLOG_ERROR, "line %zu, character %zu: tokenizer error", line, line_char);
  62. goto fail;
  63. }
  64. struct parser_minor minor;
  65. minor.str = value;
  66. minor.len = value_len;
  67. switch (token) {
  68. case NCD_EOF: {
  69. Parse(state->parser, 0, minor, &state->out);
  70. } break;
  71. case NCD_TOKEN_CURLY_OPEN: {
  72. Parse(state->parser, CURLY_OPEN, minor, &state->out);
  73. } break;
  74. case NCD_TOKEN_CURLY_CLOSE: {
  75. Parse(state->parser, CURLY_CLOSE, minor, &state->out);
  76. } break;
  77. case NCD_TOKEN_COMMA: {
  78. Parse(state->parser, COMMA, minor, &state->out);
  79. } break;
  80. case NCD_TOKEN_STRING: {
  81. Parse(state->parser, STRING, minor, &state->out);
  82. } break;
  83. case NCD_TOKEN_COLON: {
  84. Parse(state->parser, COLON, minor, &state->out);
  85. } break;
  86. case NCD_TOKEN_BRACKET_OPEN: {
  87. Parse(state->parser, BRACKET_OPEN, minor, &state->out);
  88. } break;
  89. case NCD_TOKEN_BRACKET_CLOSE: {
  90. Parse(state->parser, BRACKET_CLOSE, minor, &state->out);
  91. } break;
  92. default:
  93. BLog(BLOG_ERROR, "line %zu, character %zu: invalid token", line, line_char);
  94. free(minor.str);
  95. goto fail;
  96. }
  97. if (state->out.syntax_error) {
  98. BLog(BLOG_ERROR, "line %zu, character %zu: syntax error", line, line_char);
  99. goto fail;
  100. }
  101. if (state->out.out_of_memory) {
  102. BLog(BLOG_ERROR, "line %zu, character %zu: out of memory", line, line_char);
  103. goto fail;
  104. }
  105. return 1;
  106. fail:
  107. state->error = 1;
  108. return 0;
  109. }
  110. static int build_value (struct NCDConfig_list *ast, NCDValue *out)
  111. {
  112. switch (ast->type) {
  113. case NCDCONFIG_ARG_STRING: {
  114. if (!NCDValue_InitStringBin(out, (uint8_t *)ast->string, ast->string_len)) {
  115. BLog(BLOG_ERROR, "NCDValue_InitStringBin failed");
  116. return 0;
  117. }
  118. } break;
  119. case NCDCONFIG_ARG_LIST: {
  120. if (!build_list_value(ast->list, out)) {
  121. return 0;
  122. }
  123. } break;
  124. case NCDCONFIG_ARG_MAPLIST: {
  125. if (!build_map_value(ast->list, out)) {
  126. return 0;
  127. }
  128. } break;
  129. default: ASSERT(0);
  130. }
  131. return 1;
  132. }
  133. static int build_list_value (struct NCDConfig_list *list, NCDValue *out)
  134. {
  135. NCDValue list_val;
  136. NCDValue_InitList(&list_val);
  137. for (struct NCDConfig_list *elem = list; elem; elem = elem->next) {
  138. NCDValue v;
  139. if (!build_value(elem, &v)) {
  140. goto fail;
  141. }
  142. if (!NCDValue_ListAppend(&list_val, v)) {
  143. BLog(BLOG_ERROR, "NCDValue_ListAppend failed");
  144. NCDValue_Free(&v);
  145. goto fail;
  146. }
  147. }
  148. *out = list_val;
  149. return 1;
  150. fail:
  151. NCDValue_Free(&list_val);
  152. return 0;
  153. }
  154. static int build_map_value (struct NCDConfig_list *list, NCDValue *out)
  155. {
  156. NCDValue map_val;
  157. NCDValue_InitMap(&map_val);
  158. for (struct NCDConfig_list *elem = list; elem; elem = elem->next->next) {
  159. ASSERT(elem->next)
  160. NCDValue key;
  161. NCDValue val;
  162. if (!build_value(elem, &key)) {
  163. goto fail;
  164. }
  165. if (!build_value(elem->next, &val)) {
  166. NCDValue_Free(&key);
  167. goto fail;
  168. }
  169. if (NCDValue_MapFindKey(&map_val, &key)) {
  170. BLog(BLOG_ERROR, "duplicate map keys");
  171. NCDValue_Free(&key);
  172. NCDValue_Free(&val);
  173. goto fail;
  174. }
  175. if (!NCDValue_MapInsert(&map_val, key, val)) {
  176. BLog(BLOG_ERROR, "NCDValue_MapInsert failed");
  177. NCDValue_Free(&key);
  178. NCDValue_Free(&val);
  179. goto fail;
  180. }
  181. }
  182. *out = map_val;
  183. return 1;
  184. fail:
  185. NCDValue_Free(&map_val);
  186. return 0;
  187. }
  188. int NCDValueParser_Parse (const char *str, size_t str_len, NCDValue *out_value)
  189. {
  190. int res = 0;
  191. // init parse state
  192. struct parser_state state;
  193. state.out.out_of_memory = 0;
  194. state.out.syntax_error = 0;
  195. state.out.ast_type = AST_TYPE_NONE;
  196. state.out.ast_string.str = NULL;
  197. state.out.ast_list = NULL;
  198. state.error = 0;
  199. // allocate parser
  200. if (!(state.parser = ParseAlloc(malloc))) {
  201. BLog(BLOG_ERROR, "ParseAlloc failed");
  202. goto out;
  203. }
  204. // tokenize and parse
  205. NCDConfigTokenizer_Tokenize((char *)str, str_len, tokenizer_output, &state);
  206. // check for errors
  207. if (state.error) {
  208. goto out;
  209. }
  210. ASSERT(state.out.ast_type == AST_TYPE_STRING || state.out.ast_type == AST_TYPE_LIST ||
  211. state.out.ast_type == AST_TYPE_MAP)
  212. // convert AST to value
  213. NCDValue val;
  214. switch (state.out.ast_type) {
  215. case AST_TYPE_STRING: {
  216. if (!NCDValue_InitStringBin(&val, (uint8_t *)state.out.ast_string.str, state.out.ast_string.len)) {
  217. BLog(BLOG_ERROR, "NCDValue_InitStringBin failed");
  218. goto out;
  219. }
  220. } break;
  221. case AST_TYPE_LIST: {
  222. if (!build_list_value(state.out.ast_list, &val)) {
  223. goto out;
  224. }
  225. } break;
  226. case AST_TYPE_MAP: {
  227. if (!build_map_value(state.out.ast_list, &val)) {
  228. goto out;
  229. }
  230. } break;
  231. }
  232. *out_value = val;
  233. res = 1;
  234. out:
  235. if (state.parser) {
  236. ParseFree(state.parser, free);
  237. }
  238. free(state.out.ast_string.str);
  239. NCDConfig_free_list(state.out.ast_list);
  240. return res;
  241. }