NCDValueParser.c 8.6 KB

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