NCDValueParser.c 8.8 KB

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