NCDValueParser.c 9.0 KB

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