NCDValueParser.c 8.8 KB

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