• Home
  • History
  • Annotate
  • Raw
  • Download
  • only in /freebsd-13-stable/contrib/libucl/src/

Lines Matching refs:parser

31  * The implementation of ucl parser
43 * @param parser
61 ucl_set_err (struct ucl_parser *parser, int code, const char *str, UT_string **err)
64 struct ucl_chunk *chunk = parser->chunks;
66 if (parser->cur_file) {
67 filename = parser->cur_file;
91 parser->err_code = code;
92 parser->state = UCL_STATE_ERROR;
96 ucl_save_comment (struct ucl_parser *parser, const char *begin, size_t len)
103 if (parser->last_comment) {
105 DL_APPEND (parser->last_comment, nobj);
108 parser->last_comment = nobj;
114 ucl_attach_comment (struct ucl_parser *parser, ucl_object_t *obj, bool before)
116 if (parser->last_comment) {
117 ucl_object_insert_key (parser->comments, parser->last_comment,
121 parser->last_comment->flags |= UCL_OBJECT_INHERITED;
124 parser->last_comment = NULL;
130 * @param parser
134 ucl_skip_comments (struct ucl_parser *parser)
136 struct ucl_chunk *chunk = parser->chunks;
145 if (parser->state != UCL_STATE_SCOMMENT &&
146 parser->state != UCL_STATE_MCOMMENT) {
151 if (parser->flags & UCL_PARSER_SAVE_COMMENTS) {
152 ucl_save_comment (parser, beg, p - beg);
182 if (parser->flags & UCL_PARSER_SAVE_COMMENTS) {
183 ucl_save_comment (parser, beg, p - beg + 1);
204 ucl_set_err (parser, UCL_ENESTED,
205 "unfinished multiline comment", &parser->err);
211 if (beg && p > beg && (parser->flags & UCL_PARSER_SAVE_COMMENTS)) {
212 ucl_save_comment (parser, beg, p - beg);
305 * @param parser
314 ucl_check_variable_safe (struct ucl_parser *parser, const char *ptr, size_t remain,
322 LL_FOREACH (parser->variables, var) {
344 if (!(*found) && parser->var_handler != NULL && strict) {
346 if (parser->var_handler (ptr, remain, &dst, &dstlen, &need_free,
347 parser->var_data)) {
362 * @param parser
370 ucl_check_variable (struct ucl_parser *parser, const char *ptr,
382 ret = ucl_check_variable_safe (parser, ptr + 1, p - ptr - 1,
401 ret = ucl_check_variable_safe (parser, ptr, remain, out_len, false, &found);
419 * @param parser
426 ucl_expand_single_variable (struct ucl_parser *parser, const char *ptr,
452 LL_FOREACH (parser->variables, var) {
464 if (strict && parser->var_handler != NULL) {
465 if (parser->var_handler (p, remain, &dst, &dstlen, &need_free,
466 parser->var_data)) {
498 * @param parser
505 ucl_expand_variable (struct ucl_parser *parser, unsigned char **dst,
513 if (parser->flags & UCL_PARSER_DISABLE_MACRO) {
521 p = ucl_check_variable (parser, p + 1, end - p - 1, &out_len, &vars_found);
544 p = ucl_expand_single_variable (parser, p, end - p, &d);
558 * @param parser parser object
570 ucl_copy_or_store_ptr (struct ucl_parser *parser,
580 (need_expand && parser->variables != NULL) ||
581 !(parser->flags & UCL_PARSER_ZEROCOPY)) {
585 ucl_set_err (parser, UCL_EINTERNAL, "cannot allocate memory for a string",
586 &parser->err);
608 ret = ucl_expand_variable (parser, dst, tmp, ret);
631 * @param parser
637 ucl_parser_add_container (ucl_object_t *obj, struct ucl_parser *parser,
644 nobj = ucl_object_new_full (is_array ? UCL_ARRAY : UCL_OBJECT, parser->chunks->priority);
651 ucl_set_err (parser, UCL_EMERGE,
653 &parser->err);
663 nobj->value.ov = ucl_hash_create (parser->flags & UCL_PARSER_KEY_LOWERCASE);
668 parser->state = UCL_STATE_KEY;
670 parser->state = UCL_STATE_VALUE;
682 ucl_set_err (parser, UCL_ENESTED,
684 &parser->err);
694 st->e.params.line = parser->chunks->line;
695 st->chunk = parser->chunks;
704 LL_PREPEND (parser->stack, st);
705 parser->cur_obj = nobj;
712 ucl_set_err (parser, UCL_EINTERNAL, "cannot allocate memory for an object",
713 &parser->err);
975 * @param parser
981 ucl_lex_number (struct ucl_parser *parser,
988 true, false, ((parser->flags & UCL_PARSER_NO_TIME) == 0));
997 ucl_set_err (parser, UCL_ESYNTAX, "numeric value out of range",
998 &parser->err);
1006 * @param parser
1014 ucl_lex_json_string (struct ucl_parser *parser,
1029 ucl_set_err (parser, UCL_ESYNTAX, "unexpected newline",
1030 &parser->err);
1033 ucl_set_err (parser, UCL_ESYNTAX, "unexpected control character",
1034 &parser->err);
1042 ucl_set_err (parser, UCL_ESYNTAX, "unfinished escape character",
1043 &parser->err);
1051 ucl_set_err (parser, UCL_ESYNTAX, "invalid utf escape",
1052 &parser->err);
1058 ucl_set_err (parser, UCL_ESYNTAX,
1060 &parser->err);
1085 ucl_set_err (parser, UCL_ESYNTAX,
1087 &parser->err);
1093 * @param parser
1099 ucl_lex_squoted_string (struct ucl_parser *parser,
1111 ucl_set_err (parser, UCL_ESYNTAX,
1113 &parser->err);
1131 ucl_set_err (parser, UCL_ESYNTAX,
1133 &parser->err);
1138 ucl_parser_append_elt (struct ucl_parser *parser, ucl_hash_t *cont,
1144 if ((parser->flags & UCL_PARSER_NO_IMPLICIT_ARRAYS) == 0) {
1148 parser->stack->obj->len ++;
1169 ucl_parser_process_object_element (struct ucl_parser *parser, ucl_object_t *nobj)
1175 container = parser->stack->obj->value.ov;
1177 DL_FOREACH (parser->stack->obj, cur) {
1188 parser->flags & UCL_PARSER_KEY_LOWERCASE);
1194 parser->stack->obj->len ++;
1199 switch (parser->chunks->strategy) {
1216 ucl_parser_append_elt (parser, container, tobj, nobj);
1224 DL_APPEND (parser->trash_objs, nobj);
1244 ucl_set_err (parser, UCL_EMERGE, errmsg, &parser->err);
1257 ucl_parser_append_elt (parser, container, tobj, nobj);
1265 DL_APPEND (parser->trash_objs, nobj);
1275 parser->stack->obj->value.ov = container;
1276 parser->cur_obj = nobj;
1277 ucl_attach_comment (parser, nobj, false);
1284 * @param parser
1291 ucl_parse_key (struct ucl_parser *parser, struct ucl_chunk *chunk,
1306 if (!(parser->flags & UCL_PARSER_DISABLE_MACRO)) {
1310 parser->prev_state = parser->state;
1311 parser->state = UCL_STATE_MACRO_NAME;
1321 if (!ucl_skip_comments (parser)) {
1349 parser->prev_state = parser->state;
1350 parser->state = UCL_STATE_MACRO_NAME;
1355 ucl_set_err (parser, UCL_ESYNTAX, "key must begin with a letter",
1356 &parser->err);
1372 ucl_set_err (parser, UCL_ESYNTAX, "invalid character in a key",
1373 &parser->err);
1379 if (!ucl_lex_json_string (parser, chunk, &need_unescape, &ucl_escape, &var_expand)) {
1391 ucl_set_err (parser, UCL_ESYNTAX, "unfinished key", &parser->err);
1409 ucl_set_err (parser, UCL_ESYNTAX, "unexpected '=' character",
1410 &parser->err);
1420 ucl_set_err (parser, UCL_ESYNTAX, "unexpected ':' character",
1421 &parser->err);
1427 if (!ucl_skip_comments (parser)) {
1439 ucl_set_err (parser, UCL_ESYNTAX, "unfinished key", &parser->err);
1474 nobj = ucl_object_new_full (UCL_NULL, parser->chunks->priority);
1478 keylen = ucl_copy_or_store_ptr (parser, c, &nobj->trash_stack[UCL_TRASH_KEY],
1479 &key, end - c, need_unescape, parser->flags & UCL_PARSER_KEY_LOWERCASE,
1486 ucl_set_err (parser, UCL_ESYNTAX, "empty keys are not allowed", &parser->err);
1494 if (!ucl_parser_process_object_element (parser, nobj)) {
1508 * @param parser
1515 ucl_parse_string_value (struct ucl_parser *parser,
1577 * @param parser
1586 ucl_parse_multiline_string (struct ucl_parser *parser,
1635 ucl_parser_get_container (struct ucl_parser *parser)
1639 if (parser == NULL || parser->stack == NULL || parser->stack->obj == NULL) {
1643 if (parser->stack->obj->type == UCL_ARRAY) {
1645 obj = ucl_object_new_full (UCL_NULL, parser->chunks->priority);
1646 t = parser->stack->obj;
1653 parser->cur_obj = obj;
1654 ucl_attach_comment (parser, obj, false);
1658 obj = parser->cur_obj;
1666 * @param parser
1671 ucl_parse_value (struct ucl_parser *parser, struct ucl_chunk *chunk)
1687 if (!ucl_skip_comments (parser)) {
1699 if (!ucl_lex_json_string (parser, chunk, &need_unescape, &ucl_escape,
1704 obj = ucl_parser_get_container (parser);
1711 if ((str_len = ucl_copy_or_store_ptr (parser, c + 1,
1719 parser->state = UCL_STATE_AFTER_VALUE;
1726 if (!ucl_lex_squoted_string (parser, chunk, &need_unescape)) {
1730 obj = ucl_parser_get_container (parser);
1739 if ((str_len = ucl_copy_or_store_ptr (parser, c + 1,
1748 parser->state = UCL_STATE_AFTER_VALUE;
1753 obj = ucl_parser_get_container (parser);
1758 if (parser->stack) {
1759 obj = ucl_parser_add_container (obj, parser, false,
1760 parser->stack->e.params.level, true);
1774 obj = ucl_parser_get_container (parser);
1779 if (parser->stack) {
1780 obj = ucl_parser_add_container (obj, parser, true,
1781 parser->stack->e.params.level, true);
1797 if (parser->stack && parser->stack->obj->type == UCL_ARRAY) {
1798 parser->state = UCL_STATE_AFTER_VALUE;
1806 obj = ucl_parser_get_container (parser);
1822 if ((str_len = ucl_parse_multiline_string (parser, chunk, c,
1824 ucl_set_err (parser, UCL_ESYNTAX,
1825 "unterminated multiline value", &parser->err);
1831 if ((str_len = ucl_copy_or_store_ptr (parser, c,
1839 parser->state = UCL_STATE_AFTER_VALUE;
1850 obj = ucl_parser_get_container (parser);
1855 if (!ucl_lex_number (parser, chunk, obj)) {
1856 if (parser->state == UCL_STATE_ERROR) {
1861 parser->state = UCL_STATE_AFTER_VALUE;
1867 if (!ucl_parse_string_value (parser, chunk, &var_expand,
1879 ucl_set_err (parser, UCL_ESYNTAX, "string value must not be empty",
1880 &parser->err);
1899 if ((str_len = ucl_copy_or_store_ptr (parser, c,
1908 parser->state = UCL_STATE_AFTER_VALUE;
1920 * @param parser
1925 ucl_parse_after_value (struct ucl_parser *parser, struct ucl_chunk *chunk)
1940 if (!ucl_skip_comments (parser)) {
1949 if (parser->stack == NULL) {
1950 ucl_set_err (parser, UCL_ESYNTAX,
1952 &parser->err);
1955 if ((*p == '}' && parser->stack->obj->type == UCL_OBJECT) ||
1956 (*p == ']' && parser->stack->obj->type == UCL_ARRAY)) {
1959 st = parser->stack;
1962 parser->err_code = UCL_EUNPAIRED;
1963 ucl_create_err (&parser->err,
1966 parser->chunks->line, st->e.params.line);
1971 parser->stack = st->next;
1974 if (parser->cur_obj) {
1975 ucl_attach_comment (parser, parser->cur_obj, true);
1978 while (parser->stack != NULL) {
1979 st = parser->stack;
1989 parser->stack = st->next;
1990 parser->cur_obj = st->obj;
1995 ucl_set_err (parser, UCL_ESYNTAX,
1997 &parser->err);
2001 if (parser->stack == NULL) {
2019 ucl_set_err (parser, UCL_ESYNTAX, "delimiter is missing",
2020 &parser->err);
2031 ucl_skip_macro_as_comment (struct ucl_parser *parser,
2122 if (parser->flags & UCL_PARSER_SAVE_COMMENTS) {
2123 ucl_save_comment (parser, c, p - c);
2135 * @param parser
2143 ucl_parse_macro_value (struct ucl_parser *parser,
2157 if (!ucl_lex_json_string (parser, chunk, &need_unescape, &ucl_escape, &var_expand)) {
2215 * @param parser parser structure
2220 ucl_parse_macro_arguments (struct ucl_parser *parser,
2286 params_parser = ucl_parser_new (parser->flags);
2288 ucl_set_err (parser, UCL_ESYNTAX, "macro arguments parsing error",
2289 &parser->err);
2313 #define SKIP_SPACES_COMMENTS(parser, chunk, p) do { \
2317 if (!ucl_skip_comments (parser)) { \
2329 * Handle the main states of rcl parser
2330 * @param parser parser structure
2334 ucl_state_machine (struct ucl_parser *parser)
2337 struct ucl_chunk *chunk = parser->chunks;
2344 if (parser->top_obj == NULL) {
2345 parser->state = UCL_STATE_INIT;
2350 switch (parser->state) {
2357 if (!ucl_skip_comments (parser)) {
2358 parser->prev_state = parser->state;
2359 parser->state = UCL_STATE_ERROR;
2375 parser->state = UCL_STATE_VALUE;
2383 parser->state = UCL_STATE_KEY_OBRACE;
2387 parser->state = UCL_STATE_KEY;
2392 if (parser->top_obj == NULL) {
2393 if (parser->state == UCL_STATE_VALUE) {
2394 obj = ucl_parser_add_container (NULL, parser, true, 0,
2398 obj = ucl_parser_add_container (NULL, parser, false, 0,
2406 parser->top_obj = obj;
2407 parser->cur_obj = obj;
2420 parser->state = UCL_STATE_AFTER_VALUE;
2423 if (parser->stack == NULL) {
2425 ucl_set_err (parser, UCL_ESYNTAX, "top object is finished but the parser "
2426 "expects a key", &parser->err);
2427 parser->prev_state = parser->state;
2428 parser->state = UCL_STATE_ERROR;
2431 if (!ucl_parse_key (parser, chunk, &next_key, &end_of_object)) {
2432 parser->prev_state = parser->state;
2433 parser->state = UCL_STATE_ERROR;
2439 parser->state = UCL_STATE_AFTER_VALUE;
2442 else if (parser->state != UCL_STATE_MACRO_NAME) {
2443 if (next_key && parser->stack->obj->type == UCL_OBJECT) {
2445 obj = ucl_parser_add_container (parser->cur_obj,
2446 parser,
2448 parser->stack->e.params.level + 1,
2449 parser->state == UCL_STATE_KEY_OBRACE);
2455 parser->state = UCL_STATE_VALUE;
2465 if (!parser->cur_obj || !ucl_parse_value (parser, chunk)) {
2466 parser->prev_state = parser->state;
2467 parser->state = UCL_STATE_ERROR;
2474 if (!ucl_parse_after_value (parser, chunk)) {
2475 parser->prev_state = parser->state;
2476 parser->state = UCL_STATE_ERROR;
2480 if (parser->stack != NULL) {
2481 if (parser->stack->obj->type == UCL_OBJECT) {
2482 parser->state = UCL_STATE_KEY;
2486 parser->state = UCL_STATE_VALUE;
2497 if (parser->flags & UCL_PARSER_DISABLE_MACRO) {
2498 if (!ucl_skip_macro_as_comment (parser, chunk)) {
2500 ucl_create_err (&parser->err,
2505 parser->state = UCL_STATE_ERROR;
2510 parser->state = parser->prev_state;
2522 HASH_FIND (hh, parser->macroes, c, macro_len, macro);
2524 ucl_create_err (&parser->err,
2533 parser->state = UCL_STATE_ERROR;
2537 SKIP_SPACES_COMMENTS(parser, chunk, p);
2538 parser->state = UCL_STATE_MACRO;
2542 ucl_create_err (&parser->err,
2547 parser->state = UCL_STATE_ERROR;
2555 macro_args = ucl_parse_macro_arguments (parser, chunk);
2558 SKIP_SPACES_COMMENTS(parser, chunk, p);
2564 if (!ucl_parse_macro_value (parser, chunk, macro,
2566 parser->prev_state = parser->state;
2567 parser->state = UCL_STATE_ERROR;
2570 macro_len = ucl_expand_variable (parser, &macro_escaped,
2572 parser->state = parser->prev_state;
2578 parser->top_obj,
2590 parser->top_obj,
2602 ucl_set_err (parser, UCL_EINTERNAL,
2603 "internal error: parser has macro undefined", &parser->err);
2609 chunk = parser->chunks;
2621 ucl_set_err (parser, UCL_EINTERNAL,
2622 "internal error: parser is in an unknown state", &parser->err);
2623 parser->state = UCL_STATE_ERROR;
2628 if (parser->last_comment) {
2629 if (parser->cur_obj) {
2630 ucl_attach_comment (parser, parser->cur_obj, true);
2632 else if (parser->stack && parser->stack->obj) {
2633 ucl_attach_comment (parser, parser->stack->obj, true);
2635 else if (parser->top_obj) {
2636 ucl_attach_comment (parser, parser->top_obj, true);
2639 ucl_object_unref (parser->last_comment);
2643 if (parser->stack != NULL && parser->state != UCL_STATE_ERROR) {
2647 LL_FOREACH (parser->stack, st) {
2648 if (st->chunk != parser->chunks) {
2652 if (parser->err == NULL) {
2653 utstring_new (parser->err);
2656 utstring_printf (parser->err, "%s:%d unmatched open brace at %d; ",
2658 parser->chunks->line,
2666 parser->err_code = UCL_EUNPAIRED;
2683 struct ucl_parser *parser;
2685 parser = UCL_ALLOC (sizeof (struct ucl_parser));
2686 if (parser == NULL) {
2690 memset (parser, 0, sizeof (struct ucl_parser));
2692 UPRM_SAFE(ucl_parser_register_macro, parser, "include", ucl_include_handler, e0);
2693 UPRM_SAFE(ucl_parser_register_macro, parser, "try_include", ucl_try_include_handler, e0);
2694 UPRM_SAFE(ucl_parser_register_macro, parser, "includes", ucl_includes_handler, e0);
2695 UPRM_SAFE(ucl_parser_register_macro, parser, "priority", ucl_priority_handler, e0);
2696 UPRM_SAFE(ucl_parser_register_macro, parser, "load", ucl_load_handler, e0);
2697 UPRM_SAFE(ucl_parser_register_context_macro, parser, "inherit", ucl_inherit_handler, e0);
2699 parser->flags = flags;
2700 parser->includepaths = NULL;
2703 parser->comments = ucl_object_typed_new (UCL_OBJECT);
2708 ucl_parser_set_filevars (parser, NULL, false);
2711 return parser;
2713 ucl_parser_free(parser);
2718 ucl_parser_set_default_priority (struct ucl_parser *parser, unsigned prio)
2720 if (parser == NULL) {
2724 parser->default_priority = prio;
2730 ucl_parser_get_default_priority (struct ucl_parser *parser)
2732 if (parser == NULL) {
2736 return parser->default_priority;
2740 ucl_parser_register_macro (struct ucl_parser *parser, const char *macro,
2762 HASH_ADD_KEYPTR (hh, parser->macroes, new->name, strlen (new->name), new);
2767 ucl_parser_register_context_macro (struct ucl_parser *parser, const char *macro,
2790 HASH_ADD_KEYPTR (hh, parser->macroes, new->name, strlen (new->name), new);
2795 ucl_parser_register_variable (struct ucl_parser *parser, const char *var,
2805 LL_FOREACH (parser->variables, cur) {
2816 DL_DELETE (parser->variables, new);
2838 DL_APPEND (parser->variables, new);
2849 ucl_parser_set_variables_handler (struct ucl_parser *parser,
2852 parser->var_handler = handler;
2853 parser->var_data = ud;
2857 ucl_parser_add_chunk_full (struct ucl_parser *parser, const unsigned char *data,
2864 if (parser == NULL) {
2869 ucl_create_err (&parser->err, "invalid chunk added");
2873 if (parser->state != UCL_STATE_ERROR) {
2876 ucl_create_err (&parser->err, "cannot allocate chunk structure");
2883 LL_FOREACH (parser->special_handlers, special_handler) {
2890 if (!special_handler->handler (parser, data, len, &ndata, &nlen,
2892 ucl_create_err (&parser->err, "call for external handler failed");
2933 if (parser->cur_file) {
2934 chunk->fname = strdup (parser->cur_file);
2937 LL_PREPEND (parser->chunks, chunk);
2938 parser->recursion ++;
2940 if (parser->recursion > UCL_MAX_RECURSION) {
2941 ucl_create_err (&parser->err, "maximum include nesting limit is reached: %d",
2942 parser->recursion);
2951 return ucl_state_machine (parser);
2953 return ucl_parse_msgpack (parser);
2955 return ucl_parse_csexp (parser);
2960 if (parser->top_obj == NULL) {
2965 parser->top_obj = ucl_object_new_full (UCL_OBJECT, priority);
2972 ucl_create_err (&parser->err, "a parser is in an invalid state");
2978 ucl_parser_add_chunk_priority (struct ucl_parser *parser,
2981 /* We dereference parser, so this check is essential */
2982 if (parser == NULL) {
2986 return ucl_parser_add_chunk_full (parser, data, len,
2991 ucl_parser_add_chunk (struct ucl_parser *parser, const unsigned char *data,
2994 if (parser == NULL) {
2998 return ucl_parser_add_chunk_full (parser, data, len,
2999 parser->default_priority, UCL_DUPLICATE_APPEND, UCL_PARSE_UCL);
3003 ucl_parser_insert_chunk (struct ucl_parser *parser, const unsigned char *data,
3006 if (parser == NULL || parser->top_obj == NULL) {
3013 int state = parser->state;
3014 parser->state = UCL_STATE_INIT;
3017 if (parser->stack != NULL && parser->stack->next != NULL) {
3018 parser->stack->e.params.level = parser->stack->next->e.params.level;
3021 res = ucl_parser_add_chunk_full (parser, data, len, parser->chunks->priority,
3022 parser->chunks->strategy, parser->chunks->parse_type);
3025 chunk = parser->chunks;
3027 parser->chunks = chunk->next;
3029 parser->recursion --;
3032 parser->state = state;
3038 ucl_parser_add_string_priority (struct ucl_parser *parser, const char *data,
3042 ucl_create_err (&parser->err, "invalid string added");
3049 return ucl_parser_add_chunk_priority (parser,
3054 ucl_parser_add_string (struct ucl_parser *parser, const char *data,
3057 if (parser == NULL) {
3061 return ucl_parser_add_string_priority (parser,
3062 (const unsigned char *)data, len, parser->default_priority);
3066 ucl_set_include_path (struct ucl_parser *parser, ucl_object_t *paths)
3068 if (parser == NULL || paths == NULL) {
3072 if (parser->includepaths == NULL) {
3073 parser->includepaths = ucl_object_copy (paths);
3076 ucl_object_unref (parser->includepaths);
3077 parser->includepaths = ucl_object_copy (paths);
3080 if (parser->includepaths == NULL) {
3087 unsigned char ucl_parser_chunk_peek (struct ucl_parser *parser)
3089 if (parser == NULL || parser->chunks == NULL || parser->chunks->pos == NULL || parser->chunks->end == NULL ||
3090 parser->chunks->pos == parser->chunks->end) {
3094 return( *parser->chunks->pos );
3097 bool ucl_parser_chunk_skip (struct ucl_parser *parser)
3099 if (parser == NULL || parser->chunks == NULL || parser->chunks->pos == NULL || parser->chunks->end == NULL ||
3100 parser->chunks->pos == parser->chunks->end) {
3104 const unsigned char *p = parser->chunks->pos;
3105 ucl_chunk_skipc( parser->chunks, p );
3106 if( parser->chunks->pos != NULL ) return true;
3111 ucl_parser_get_current_stack_object (struct ucl_parser *parser, unsigned int depth)
3115 if (parser == NULL || parser->stack == NULL) {
3119 struct ucl_stack *stack = parser->stack;