1 /* Copyright (c) 2013, Vsevolod Stakhov
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
12 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
13 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
16 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include "ucl_internal.h"
26 #include "ucl_chartable.h"
30 * The implementation of rcl parser
33 struct ucl_parser_saved_state {
37 const unsigned char *pos;
41 * Move up to len characters
45 * @return new position in chunk
47 #define ucl_chunk_skipc(chunk, p) do{ \
50 (chunk)->column = 0; \
52 else (chunk)->column ++; \
59 ucl_set_err (struct ucl_chunk *chunk, int code, const char *str, UT_string **err)
61 if (chunk->pos < chunk->end) {
62 if (isgraph (*chunk->pos)) {
63 ucl_create_err (err, "error on line %d at column %d: '%s', character: '%c'",
64 chunk->line, chunk->column, str, *chunk->pos);
67 ucl_create_err (err, "error on line %d at column %d: '%s', character: '0x%02x'",
68 chunk->line, chunk->column, str, (int)*chunk->pos);
72 ucl_create_err (err, "error at the end of chunk: %s", str);
77 * Skip all comments from the current pos resolving nested and multiline comments
82 ucl_skip_comments (struct ucl_parser *parser)
84 struct ucl_chunk *chunk = parser->chunks;
85 const unsigned char *p;
86 int comments_nested = 0;
92 if (parser->state != UCL_STATE_SCOMMENT &&
93 parser->state != UCL_STATE_MCOMMENT) {
94 while (p < chunk->end) {
96 ucl_chunk_skipc (chunk, p);
99 ucl_chunk_skipc (chunk, p);
103 else if (*p == '/' && chunk->remain >= 2) {
105 ucl_chunk_skipc (chunk, p);
107 ucl_chunk_skipc (chunk, p);
109 while (p < chunk->end) {
111 ucl_chunk_skipc (chunk, p);
114 if (comments_nested == 0) {
115 ucl_chunk_skipc (chunk, p);
119 ucl_chunk_skipc (chunk, p);
121 else if (p[0] == '/' && chunk->remain >= 2 && p[1] == '*') {
123 ucl_chunk_skipc (chunk, p);
124 ucl_chunk_skipc (chunk, p);
127 ucl_chunk_skipc (chunk, p);
129 if (comments_nested != 0) {
130 ucl_set_err (chunk, UCL_ENESTED, "unfinished multiline comment", &parser->err);
140 * Return multiplier for a character
141 * @param c multiplier character
142 * @param is_bytes if true use 1024 multiplier
145 static inline unsigned long
146 ucl_lex_num_multiplier (const unsigned char c, bool is_bytes) {
152 {'m', 1000 * 1000, 1024 * 1024},
154 {'g', 1000 * 1000 * 1000, 1024 * 1024 * 1024}
158 for (i = 0; i < 3; i ++) {
159 if (tolower (c) == multipliers[i].c) {
161 return multipliers[i].mult_bytes;
163 return multipliers[i].mult_normal;
172 * Return multiplier for time scaling
177 ucl_lex_time_multiplier (const unsigned char c) {
185 {'w', 60 * 60 * 24 * 7},
186 {'y', 60 * 60 * 24 * 7 * 365}
190 for (i = 0; i < 5; i ++) {
191 if (tolower (c) == multipliers[i].c) {
192 return multipliers[i].mult;
200 * Return true if a character is a end of an atom
205 ucl_lex_is_atom_end (const unsigned char c)
207 return ucl_test_character (c, UCL_CHARACTER_VALUE_END);
211 ucl_lex_is_comment (const unsigned char c1, const unsigned char c2)
218 else if (c1 == '#') {
225 * Check variable found
234 static inline const char *
235 ucl_check_variable_safe (struct ucl_parser *parser, const char *ptr, size_t remain,
236 size_t *out_len, bool strict, bool *found)
238 struct ucl_variable *var;
240 LL_FOREACH (parser->variables, var) {
242 if (remain == var->var_len) {
243 if (memcmp (ptr, var->var, var->var_len) == 0) {
244 *out_len += var->value_len;
246 return (ptr + var->var_len);
251 if (remain >= var->var_len) {
252 if (memcmp (ptr, var->var, var->var_len) == 0) {
253 *out_len += var->value_len;
255 return (ptr + var->var_len);
265 * Check for a variable in a given string
274 ucl_check_variable (struct ucl_parser *parser, const char *ptr, size_t remain, size_t *out_len, bool *vars_found)
276 const char *p, *end, *ret = ptr;
280 /* We need to match the variable enclosed in braces */
285 ret = ucl_check_variable_safe (parser, ptr + 1, p - ptr - 1, out_len, true, &found);
287 /* {} must be excluded actually */
301 else if (*ptr != '$') {
302 /* Not count escaped dollar sign */
303 ret = ucl_check_variable_safe (parser, ptr, remain, out_len, false, &found);
304 if (found && !*vars_found) {
320 * Expand a single variable
328 ucl_expand_single_variable (struct ucl_parser *parser, const char *ptr,
329 size_t remain, unsigned char **dest)
331 unsigned char *d = *dest;
332 const char *p = ptr + 1, *ret;
333 struct ucl_variable *var;
344 else if (*p == '{') {
350 LL_FOREACH (parser->variables, var) {
351 if (remain >= var->var_len) {
352 if (memcmp (p, var->var, var->var_len) == 0) {
353 memcpy (d, var->value, var->value_len);
372 * Expand variables in string
380 ucl_expand_variable (struct ucl_parser *parser, unsigned char **dst,
381 const char *src, size_t in_len)
383 const char *p, *end = src + in_len;
386 bool vars_found = false;
391 p = ucl_check_variable (parser, p + 1, end - p - 1, &out_len, &vars_found);
405 *dst = UCL_ALLOC (out_len + 1);
414 p = ucl_expand_single_variable (parser, p, end - p, &d);
427 * Store or copy pointer to the trash stack
428 * @param parser parser object
429 * @param src src string
430 * @param dst destination buffer (trash stack pointer)
431 * @param dst_const const destination pointer (e.g. value of object)
432 * @param in_len input length
433 * @param need_unescape need to unescape source (and copy it)
434 * @param need_lowercase need to lowercase value (and copy)
435 * @param need_expand need to expand variables (and copy as well)
436 * @return output length (excluding \0 symbol)
438 static inline ssize_t
439 ucl_copy_or_store_ptr (struct ucl_parser *parser,
440 const unsigned char *src, unsigned char **dst,
441 const char **dst_const, size_t in_len,
442 bool need_unescape, bool need_lowercase, bool need_expand)
444 ssize_t ret = -1, tret;
447 if (need_unescape || need_lowercase ||
448 (need_expand && parser->variables != NULL) ||
449 !(parser->flags & UCL_PARSER_ZEROCOPY)) {
451 *dst = UCL_ALLOC (in_len + 1);
453 ucl_set_err (parser->chunks, 0, "cannot allocate memory for a string", &parser->err);
456 if (need_lowercase) {
457 ret = ucl_strlcpy_tolower (*dst, src, in_len + 1);
460 ret = ucl_strlcpy_unsafe (*dst, src, in_len + 1);
464 ret = ucl_unescape_json_string (*dst, ret);
469 ret = ucl_expand_variable (parser, dst, tmp, ret);
471 /* Nothing to expand */
487 * Create and append an object at the specified level
493 static inline ucl_object_t *
494 ucl_add_parser_stack (ucl_object_t *obj, struct ucl_parser *parser, bool is_array, int level)
496 struct ucl_stack *st;
500 obj = ucl_object_typed_new (UCL_OBJECT);
503 obj->type = UCL_OBJECT;
505 obj->value.ov = ucl_hash_create ();
506 parser->state = UCL_STATE_KEY;
510 obj = ucl_object_typed_new (UCL_ARRAY);
513 obj->type = UCL_ARRAY;
515 parser->state = UCL_STATE_VALUE;
518 st = UCL_ALLOC (sizeof (struct ucl_stack));
520 ucl_set_err (parser->chunks, 0, "cannot allocate memory for an object", &parser->err);
525 LL_PREPEND (parser->stack, st);
526 parser->cur_obj = obj;
532 ucl_maybe_parse_number (ucl_object_t *obj,
533 const char *start, const char *end, const char **pos,
534 bool allow_double, bool number_bytes, bool allow_time)
536 const char *p = start, *c = start;
538 bool got_dot = false, got_exp = false, need_double = false,
539 is_time = false, valid_start = false, is_hex = false,
550 if (is_hex && isxdigit (*p)) {
553 else if (isdigit (*p)) {
557 else if (!is_hex && (*p == 'x' || *p == 'X')) {
559 allow_double = false;
562 else if (allow_double) {
564 /* Empty digits sequence, not a number */
568 else if (*p == '.') {
570 /* Double dots, not a number */
580 else if (*p == 'e' || *p == 'E') {
582 /* Double exp, not a number */
594 if (!isdigit (*p) && *p != '+' && *p != '-') {
595 /* Wrong exponent sign */
605 /* Got the end of the number, need to check */
621 dv = strtod (c, &endptr);
625 lv = strtoimax (c, &endptr, 16);
628 lv = strtoimax (c, &endptr, 10);
631 if (errno == ERANGE) {
636 /* Now check endptr */
637 if (endptr == NULL || ucl_lex_is_atom_end (*endptr) || *endptr == '\0' ||
638 ucl_test_character (*endptr, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
643 if (endptr < end && endptr != start) {
653 if (p[1] == 's' || p[1] == 'S') {
660 if (p[0] == 'm' || p[0] == 'M') {
664 dv *= ucl_lex_num_multiplier (*p, false);
669 else if (number_bytes || (p[1] == 'b' || p[1] == 'B')) {
675 lv *= ucl_lex_num_multiplier (*p, true);
679 else if (ucl_lex_is_atom_end (p[1])) {
681 dv *= ucl_lex_num_multiplier (*p, false);
684 lv *= ucl_lex_num_multiplier (*p, number_bytes);
689 else if (allow_time && end - p >= 3) {
690 if (tolower (p[0]) == 'm' &&
691 tolower (p[1]) == 'i' &&
692 tolower (p[2]) == 'n') {
707 dv *= ucl_lex_num_multiplier (*p, false);
710 lv *= ucl_lex_num_multiplier (*p, number_bytes);
719 (p == end - 1 || ucl_lex_is_atom_end (p[1]))) {
738 (p == end - 1 || ucl_lex_is_atom_end (p[1]))) {
744 dv *= ucl_lex_time_multiplier (*p);
756 if (allow_double && (need_double || is_time)) {
758 obj->type = UCL_FLOAT;
761 obj->type = UCL_TIME;
763 obj->value.dv = is_neg ? (-dv) : dv;
767 obj->value.iv = is_neg ? (-lv) : lv;
774 * Parse possible number
777 * @return true if a number has been parsed
780 ucl_lex_number (struct ucl_parser *parser,
781 struct ucl_chunk *chunk, ucl_object_t *obj)
783 const unsigned char *pos;
786 ret = ucl_maybe_parse_number (obj, chunk->pos, chunk->end, (const char **)&pos,
787 true, false, ((parser->flags & UCL_PARSER_NO_TIME) == 0));
790 chunk->remain -= pos - chunk->pos;
791 chunk->column += pos - chunk->pos;
795 else if (ret == ERANGE) {
796 ucl_set_err (chunk, ERANGE, "numeric value out of range", &parser->err);
803 * Parse quoted string with possible escapes
806 * @return true if a string has been parsed
809 ucl_lex_json_string (struct ucl_parser *parser,
810 struct ucl_chunk *chunk, bool *need_unescape, bool *ucl_escape, bool *var_expand)
812 const unsigned char *p = chunk->pos;
816 while (p < chunk->end) {
819 /* Unmasked control character */
821 ucl_set_err (chunk, UCL_ESYNTAX, "unexpected newline", &parser->err);
824 ucl_set_err (chunk, UCL_ESYNTAX, "unexpected control character", &parser->err);
828 else if (c == '\\') {
829 ucl_chunk_skipc (chunk, p);
831 if (p >= chunk->end) {
832 ucl_set_err (chunk, UCL_ESYNTAX, "unfinished escape character", &parser->err);
835 else if (ucl_test_character (c, UCL_CHARACTER_ESCAPE)) {
837 ucl_chunk_skipc (chunk, p);
838 for (i = 0; i < 4 && p < chunk->end; i ++) {
839 if (!isxdigit (*p)) {
840 ucl_set_err (chunk, UCL_ESYNTAX, "invalid utf escape", &parser->err);
843 ucl_chunk_skipc (chunk, p);
845 if (p >= chunk->end) {
846 ucl_set_err (chunk, UCL_ESYNTAX, "unfinished escape character", &parser->err);
851 ucl_chunk_skipc (chunk, p);
854 *need_unescape = true;
859 ucl_chunk_skipc (chunk, p);
862 else if (ucl_test_character (c, UCL_CHARACTER_UCL_UNSAFE)) {
868 ucl_chunk_skipc (chunk, p);
871 ucl_set_err (chunk, UCL_ESYNTAX, "no quote at the end of json string", &parser->err);
876 * Parse a key in an object
879 * @return true if a key has been parsed
882 ucl_parse_key (struct ucl_parser *parser, struct ucl_chunk *chunk, bool *next_key, bool *end_of_object)
884 const unsigned char *p, *c = NULL, *end, *t;
885 const char *key = NULL;
886 bool got_quote = false, got_eq = false, got_semicolon = false,
887 need_unescape = false, ucl_escape = false, var_expand = false,
888 got_content = false, got_sep = false;
889 ucl_object_t *nobj, *tobj;
890 ucl_hash_t *container;
896 /* It is macro actually */
897 ucl_chunk_skipc (chunk, p);
898 parser->prev_state = parser->state;
899 parser->state = UCL_STATE_MACRO_NAME;
902 while (p < chunk->end) {
904 * A key must start with alpha, number, '/' or '_' and end with space character
907 if (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1])) {
908 if (!ucl_skip_comments (parser)) {
913 else if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
914 ucl_chunk_skipc (chunk, p);
916 else if (ucl_test_character (*p, UCL_CHARACTER_KEY_START)) {
917 /* The first symbol */
919 ucl_chunk_skipc (chunk, p);
922 else if (*p == '"') {
927 ucl_chunk_skipc (chunk, p);
929 else if (*p == '}') {
930 /* We have actually end of an object */
931 *end_of_object = true;
934 else if (*p == '.') {
935 ucl_chunk_skipc (chunk, p);
936 parser->prev_state = parser->state;
937 parser->state = UCL_STATE_MACRO_NAME;
941 /* Invalid identifier */
942 ucl_set_err (chunk, UCL_ESYNTAX, "key must begin with a letter", &parser->err);
947 /* Parse the body of a key */
949 if (ucl_test_character (*p, UCL_CHARACTER_KEY)) {
951 ucl_chunk_skipc (chunk, p);
953 else if (ucl_test_character (*p, UCL_CHARACTER_KEY_SEP)) {
958 ucl_set_err (chunk, UCL_ESYNTAX, "invalid character in a key", &parser->err);
963 /* We need to parse json like quoted string */
964 if (!ucl_lex_json_string (parser, chunk, &need_unescape, &ucl_escape, &var_expand)) {
967 /* Always escape keys obtained via json */
968 end = chunk->pos - 1;
975 if (p >= chunk->end && got_content) {
976 ucl_set_err (chunk, UCL_ESYNTAX, "unfinished key", &parser->err);
979 else if (!got_content) {
982 *end_of_object = false;
983 /* We are now at the end of the key, need to parse the rest */
984 while (p < chunk->end) {
985 if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE)) {
986 ucl_chunk_skipc (chunk, p);
988 else if (*p == '=') {
989 if (!got_eq && !got_semicolon) {
990 ucl_chunk_skipc (chunk, p);
994 ucl_set_err (chunk, UCL_ESYNTAX, "unexpected '=' character", &parser->err);
998 else if (*p == ':') {
999 if (!got_eq && !got_semicolon) {
1000 ucl_chunk_skipc (chunk, p);
1001 got_semicolon = true;
1004 ucl_set_err (chunk, UCL_ESYNTAX, "unexpected ':' character", &parser->err);
1008 else if (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1])) {
1009 /* Check for comment */
1010 if (!ucl_skip_comments (parser)) {
1021 if (p >= chunk->end && got_content) {
1022 ucl_set_err (chunk, UCL_ESYNTAX, "unfinished key", &parser->err);
1026 got_sep = got_semicolon || got_eq;
1030 * Maybe we have more keys nested, so search for termination character.
1032 * 1) key1 key2 ... keyN [:=] value <- we treat that as error
1033 * 2) key1 ... keyN {} or [] <- we treat that as nested objects
1034 * 3) key1 value[;,\n] <- we treat that as linear object
1038 while (ucl_test_character (*t, UCL_CHARACTER_WHITESPACE)) {
1041 /* Check first non-space character after a key */
1042 if (*t != '{' && *t != '[') {
1043 while (t < chunk->end) {
1044 if (*t == ',' || *t == ';' || *t == '\n' || *t == '\r') {
1047 else if (*t == '{' || *t == '[') {
1056 /* Create a new object */
1057 nobj = ucl_object_new ();
1058 keylen = ucl_copy_or_store_ptr (parser, c, &nobj->trash_stack[UCL_TRASH_KEY],
1059 &key, end - c, need_unescape, parser->flags & UCL_PARSER_KEY_LOWERCASE, false);
1061 ucl_object_unref (nobj);
1064 else if (keylen == 0) {
1065 ucl_set_err (chunk, UCL_ESYNTAX, "empty keys are not allowed", &parser->err);
1066 ucl_object_unref (nobj);
1070 container = parser->stack->obj->value.ov;
1072 nobj->keylen = keylen;
1073 tobj = __DECONST (ucl_object_t *, ucl_hash_search_obj (container, nobj));
1075 container = ucl_hash_insert_object (container, nobj);
1078 parser->stack->obj->len ++;
1081 DL_APPEND (tobj, nobj);
1085 nobj->flags |= UCL_OBJECT_NEED_KEY_ESCAPE;
1087 parser->stack->obj->value.ov = container;
1089 parser->cur_obj = nobj;
1098 * @return true if a key has been parsed
1101 ucl_parse_string_value (struct ucl_parser *parser,
1102 struct ucl_chunk *chunk, bool *var_expand, bool *need_unescape)
1104 const unsigned char *p;
1106 UCL_BRACE_ROUND = 0,
1110 int braces[3][2] = {{0, 0}, {0, 0}, {0, 0}};
1114 while (p < chunk->end) {
1116 /* Skip pairs of figure braces */
1118 braces[UCL_BRACE_FIGURE][0] ++;
1120 else if (*p == '}') {
1121 braces[UCL_BRACE_FIGURE][1] ++;
1122 if (braces[UCL_BRACE_FIGURE][1] <= braces[UCL_BRACE_FIGURE][0]) {
1123 /* This is not a termination symbol, continue */
1124 ucl_chunk_skipc (chunk, p);
1128 /* Skip pairs of square braces */
1129 else if (*p == '[') {
1130 braces[UCL_BRACE_SQUARE][0] ++;
1132 else if (*p == ']') {
1133 braces[UCL_BRACE_SQUARE][1] ++;
1134 if (braces[UCL_BRACE_SQUARE][1] <= braces[UCL_BRACE_SQUARE][0]) {
1135 /* This is not a termination symbol, continue */
1136 ucl_chunk_skipc (chunk, p);
1140 else if (*p == '$') {
1143 else if (*p == '\\') {
1144 *need_unescape = true;
1145 ucl_chunk_skipc (chunk, p);
1146 if (p < chunk->end) {
1147 ucl_chunk_skipc (chunk, p);
1152 if (ucl_lex_is_atom_end (*p) || (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1]))) {
1155 ucl_chunk_skipc (chunk, p);
1158 if (p >= chunk->end) {
1159 ucl_set_err (chunk, UCL_ESYNTAX, "unfinished value", &parser->err);
1167 * Parse multiline string ending with \n{term}\n
1172 * @return size of multiline string or 0 in case of error
1175 ucl_parse_multiline_string (struct ucl_parser *parser,
1176 struct ucl_chunk *chunk, const unsigned char *term,
1177 int term_len, unsigned char const **beg,
1180 const unsigned char *p, *c;
1181 bool newline = false;
1188 while (p < chunk->end) {
1190 if (chunk->end - p < term_len) {
1193 else if (memcmp (p, term, term_len) == 0 && (p[term_len] == '\n' || p[term_len] == '\r')) {
1195 chunk->remain -= term_len;
1196 chunk->pos = p + term_len;
1197 chunk->column = term_len;
1211 ucl_chunk_skipc (chunk, p);
1217 static ucl_object_t*
1218 ucl_get_value_object (struct ucl_parser *parser)
1220 ucl_object_t *t, *obj = NULL;
1222 if (parser->stack->obj->type == UCL_ARRAY) {
1223 /* Object must be allocated */
1224 obj = ucl_object_new ();
1225 t = parser->stack->obj->value.av;
1227 parser->cur_obj = obj;
1228 parser->stack->obj->value.av = t;
1229 parser->stack->obj->len ++;
1232 /* Object has been already allocated */
1233 obj = parser->cur_obj;
1246 ucl_parse_value (struct ucl_parser *parser, struct ucl_chunk *chunk)
1248 const unsigned char *p, *c;
1249 ucl_object_t *obj = NULL;
1250 unsigned int stripped_spaces;
1252 bool need_unescape = false, ucl_escape = false, var_expand = false;
1256 /* Skip any spaces and comments */
1257 if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE) ||
1258 (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1]))) {
1259 while (p < chunk->end && ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
1260 ucl_chunk_skipc (chunk, p);
1262 if (!ucl_skip_comments (parser)) {
1268 while (p < chunk->end) {
1272 obj = ucl_get_value_object (parser);
1273 ucl_chunk_skipc (chunk, p);
1274 if (!ucl_lex_json_string (parser, chunk, &need_unescape, &ucl_escape, &var_expand)) {
1277 str_len = chunk->pos - c - 2;
1278 obj->type = UCL_STRING;
1279 if ((str_len = ucl_copy_or_store_ptr (parser, c + 1, &obj->trash_stack[UCL_TRASH_VALUE],
1280 &obj->value.sv, str_len, need_unescape, false, var_expand)) == -1) {
1284 parser->state = UCL_STATE_AFTER_VALUE;
1289 obj = ucl_get_value_object (parser);
1290 /* We have a new object */
1291 obj = ucl_add_parser_stack (obj, parser, false, parser->stack->level);
1296 ucl_chunk_skipc (chunk, p);
1300 obj = ucl_get_value_object (parser);
1301 /* We have a new array */
1302 obj = ucl_add_parser_stack (obj, parser, true, parser->stack->level);
1307 ucl_chunk_skipc (chunk, p);
1311 /* We have the array ending */
1312 if (parser->stack && parser->stack->obj->type == UCL_ARRAY) {
1313 parser->state = UCL_STATE_AFTER_VALUE;
1321 obj = ucl_get_value_object (parser);
1322 /* We have something like multiline value, which must be <<[A-Z]+\n */
1323 if (chunk->end - p > 3) {
1324 if (memcmp (p, "<<", 2) == 0) {
1326 /* We allow only uppercase characters in multiline definitions */
1327 while (p < chunk->end && *p >= 'A' && *p <= 'Z') {
1331 /* Set chunk positions and start multiline parsing */
1333 chunk->remain -= p - c;
1337 if ((str_len = ucl_parse_multiline_string (parser, chunk, c,
1338 p - c, &c, &var_expand)) == 0) {
1339 ucl_set_err (chunk, UCL_ESYNTAX, "unterminated multiline value", &parser->err);
1342 obj->type = UCL_STRING;
1343 if ((str_len = ucl_copy_or_store_ptr (parser, c, &obj->trash_stack[UCL_TRASH_VALUE],
1344 &obj->value.sv, str_len - 1, false, false, var_expand)) == -1) {
1348 parser->state = UCL_STATE_AFTER_VALUE;
1353 /* Fallback to ordinary strings */
1357 obj = ucl_get_value_object (parser);
1360 if (ucl_test_character (*p, UCL_CHARACTER_VALUE_DIGIT_START)) {
1361 if (!ucl_lex_number (parser, chunk, obj)) {
1362 if (parser->state == UCL_STATE_ERROR) {
1367 parser->state = UCL_STATE_AFTER_VALUE;
1370 /* Fallback to normal string */
1373 if (!ucl_parse_string_value (parser, chunk, &var_expand, &need_unescape)) {
1376 /* Cut trailing spaces */
1377 stripped_spaces = 0;
1378 while (ucl_test_character (*(chunk->pos - 1 - stripped_spaces),
1379 UCL_CHARACTER_WHITESPACE)) {
1382 str_len = chunk->pos - c - stripped_spaces;
1384 ucl_set_err (chunk, 0, "string value must not be empty", &parser->err);
1387 else if (str_len == 4 && memcmp (c, "null", 4) == 0) {
1389 obj->type = UCL_NULL;
1391 else if (!ucl_maybe_parse_boolean (obj, c, str_len)) {
1392 obj->type = UCL_STRING;
1393 if ((str_len = ucl_copy_or_store_ptr (parser, c, &obj->trash_stack[UCL_TRASH_VALUE],
1394 &obj->value.sv, str_len, need_unescape,
1395 false, var_expand)) == -1) {
1400 parser->state = UCL_STATE_AFTER_VALUE;
1412 * Handle after value data
1418 ucl_parse_after_value (struct ucl_parser *parser, struct ucl_chunk *chunk)
1420 const unsigned char *p;
1421 bool got_sep = false;
1422 struct ucl_stack *st;
1426 while (p < chunk->end) {
1427 if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE)) {
1428 /* Skip whitespaces */
1429 ucl_chunk_skipc (chunk, p);
1431 else if (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1])) {
1433 if (!ucl_skip_comments (parser)) {
1436 /* Treat comment as a separator */
1440 else if (ucl_test_character (*p, UCL_CHARACTER_VALUE_END)) {
1441 if (*p == '}' || *p == ']') {
1442 if (parser->stack == NULL) {
1443 ucl_set_err (chunk, UCL_ESYNTAX, "end of array or object detected without corresponding start", &parser->err);
1446 if ((*p == '}' && parser->stack->obj->type == UCL_OBJECT) ||
1447 (*p == ']' && parser->stack->obj->type == UCL_ARRAY)) {
1449 /* Pop all nested objects from a stack */
1451 parser->stack = st->next;
1452 UCL_FREE (sizeof (struct ucl_stack), st);
1454 while (parser->stack != NULL) {
1456 if (st->next == NULL || st->next->level == st->level) {
1459 parser->stack = st->next;
1460 UCL_FREE (sizeof (struct ucl_stack), st);
1464 ucl_set_err (chunk, UCL_ESYNTAX, "unexpected terminating symbol detected", &parser->err);
1468 if (parser->stack == NULL) {
1469 /* Ignore everything after a top object */
1473 ucl_chunk_skipc (chunk, p);
1478 /* Got a separator */
1480 ucl_chunk_skipc (chunk, p);
1486 ucl_set_err (chunk, UCL_ESYNTAX, "delimiter is missing", &parser->err);
1503 ucl_parse_macro_value (struct ucl_parser *parser,
1504 struct ucl_chunk *chunk, struct ucl_macro *macro,
1505 unsigned char const **macro_start, size_t *macro_len)
1507 const unsigned char *p, *c;
1508 bool need_unescape = false, ucl_escape = false, var_expand = false;
1514 /* We have macro value encoded in quotes */
1516 ucl_chunk_skipc (chunk, p);
1517 if (!ucl_lex_json_string (parser, chunk, &need_unescape, &ucl_escape, &var_expand)) {
1521 *macro_start = c + 1;
1522 *macro_len = chunk->pos - c - 2;
1526 /* We got a multiline macro body */
1527 ucl_chunk_skipc (chunk, p);
1528 /* Skip spaces at the beginning */
1529 while (p < chunk->end) {
1530 if (ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
1531 ucl_chunk_skipc (chunk, p);
1538 while (p < chunk->end) {
1542 ucl_chunk_skipc (chunk, p);
1546 ucl_chunk_skipc (chunk, p);
1549 /* Macro is not enclosed in quotes or braces */
1551 while (p < chunk->end) {
1552 if (ucl_lex_is_atom_end (*p)) {
1555 ucl_chunk_skipc (chunk, p);
1562 /* We are at the end of a macro */
1563 /* Skip ';' and space characters and return to previous state */
1564 while (p < chunk->end) {
1565 if (!ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE) && *p != ';') {
1568 ucl_chunk_skipc (chunk, p);
1574 * Handle the main states of rcl parser
1575 * @param parser parser structure
1576 * @param data the pointer to the beginning of a chunk
1577 * @param len the length of a chunk
1578 * @return true if chunk has been parsed and false in case of error
1581 ucl_state_machine (struct ucl_parser *parser)
1584 struct ucl_chunk *chunk = parser->chunks;
1585 const unsigned char *p, *c = NULL, *macro_start = NULL;
1586 unsigned char *macro_escaped;
1587 size_t macro_len = 0;
1588 struct ucl_macro *macro = NULL;
1589 bool next_key = false, end_of_object = false;
1591 if (parser->top_obj == NULL) {
1592 if (*chunk->pos == '[') {
1593 obj = ucl_add_parser_stack (NULL, parser, true, 0);
1596 obj = ucl_add_parser_stack (NULL, parser, false, 0);
1601 parser->top_obj = obj;
1602 parser->cur_obj = obj;
1603 parser->state = UCL_STATE_INIT;
1607 while (chunk->pos < chunk->end) {
1608 switch (parser->state) {
1609 case UCL_STATE_INIT:
1611 * At the init state we can either go to the parse array or object
1612 * if we got [ or { correspondingly or can just treat new data as
1613 * a key of newly created object
1615 obj = parser->cur_obj;
1616 if (!ucl_skip_comments (parser)) {
1617 parser->prev_state = parser->state;
1618 parser->state = UCL_STATE_ERROR;
1624 parser->state = UCL_STATE_VALUE;
1625 ucl_chunk_skipc (chunk, p);
1628 parser->state = UCL_STATE_KEY;
1630 ucl_chunk_skipc (chunk, p);
1636 /* Skip any spaces */
1637 while (p < chunk->end && ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
1638 ucl_chunk_skipc (chunk, p);
1641 /* We have the end of an object */
1642 parser->state = UCL_STATE_AFTER_VALUE;
1645 if (parser->stack == NULL) {
1646 /* No objects are on stack, but we want to parse a key */
1647 ucl_set_err (chunk, UCL_ESYNTAX, "top object is finished but the parser "
1648 "expects a key", &parser->err);
1649 parser->prev_state = parser->state;
1650 parser->state = UCL_STATE_ERROR;
1653 if (!ucl_parse_key (parser, chunk, &next_key, &end_of_object)) {
1654 parser->prev_state = parser->state;
1655 parser->state = UCL_STATE_ERROR;
1658 if (end_of_object) {
1660 parser->state = UCL_STATE_AFTER_VALUE;
1663 else if (parser->state != UCL_STATE_MACRO_NAME) {
1664 if (next_key && parser->stack->obj->type == UCL_OBJECT) {
1665 /* Parse more keys and nest objects accordingly */
1666 obj = ucl_add_parser_stack (parser->cur_obj, parser, false,
1667 parser->stack->level + 1);
1673 parser->state = UCL_STATE_VALUE;
1681 case UCL_STATE_VALUE:
1682 /* We need to check what we do have */
1683 if (!ucl_parse_value (parser, chunk)) {
1684 parser->prev_state = parser->state;
1685 parser->state = UCL_STATE_ERROR;
1688 /* State is set in ucl_parse_value call */
1691 case UCL_STATE_AFTER_VALUE:
1692 if (!ucl_parse_after_value (parser, chunk)) {
1693 parser->prev_state = parser->state;
1694 parser->state = UCL_STATE_ERROR;
1697 if (parser->stack != NULL) {
1698 if (parser->stack->obj->type == UCL_OBJECT) {
1699 parser->state = UCL_STATE_KEY;
1703 parser->state = UCL_STATE_VALUE;
1707 /* Skip everything at the end */
1712 case UCL_STATE_MACRO_NAME:
1713 if (!ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
1714 ucl_chunk_skipc (chunk, p);
1716 else if (p - c > 0) {
1717 /* We got macro name */
1718 macro_len = (size_t)(p - c);
1719 HASH_FIND (hh, parser->macroes, c, macro_len, macro);
1720 if (macro == NULL) {
1721 ucl_create_err (&parser->err, "error on line %d at column %d: "
1722 "unknown macro: '%.*s', character: '%c'",
1723 chunk->line, chunk->column, (int)(p - c), c, *chunk->pos);
1724 parser->state = UCL_STATE_ERROR;
1727 /* Now we need to skip all spaces */
1728 while (p < chunk->end) {
1729 if (!ucl_test_character (*p, UCL_CHARACTER_WHITESPACE_UNSAFE)) {
1730 if (chunk->remain >= 2 && ucl_lex_is_comment (p[0], p[1])) {
1732 if (!ucl_skip_comments (parser)) {
1739 ucl_chunk_skipc (chunk, p);
1741 parser->state = UCL_STATE_MACRO;
1744 case UCL_STATE_MACRO:
1745 if (!ucl_parse_macro_value (parser, chunk, macro,
1746 ¯o_start, ¯o_len)) {
1747 parser->prev_state = parser->state;
1748 parser->state = UCL_STATE_ERROR;
1751 macro_len = ucl_expand_variable (parser, ¯o_escaped, macro_start, macro_len);
1752 parser->state = parser->prev_state;
1753 if (macro_escaped == NULL) {
1754 if (!macro->handler (macro_start, macro_len, macro->ud)) {
1759 if (!macro->handler (macro_escaped, macro_len, macro->ud)) {
1760 UCL_FREE (macro_len + 1, macro_escaped);
1763 UCL_FREE (macro_len + 1, macro_escaped);
1768 /* TODO: add all states */
1769 ucl_set_err (chunk, UCL_EINTERNAL, "internal error: parser is in an unknown state", &parser->err);
1770 parser->state = UCL_STATE_ERROR;
1779 ucl_parser_new (int flags)
1781 struct ucl_parser *new;
1783 new = UCL_ALLOC (sizeof (struct ucl_parser));
1787 memset (new, 0, sizeof (struct ucl_parser));
1789 ucl_parser_register_macro (new, "include", ucl_include_handler, new);
1790 ucl_parser_register_macro (new, "try_include", ucl_try_include_handler, new);
1791 ucl_parser_register_macro (new, "includes", ucl_includes_handler, new);
1795 /* Initial assumption about filevars */
1796 ucl_parser_set_filevars (new, NULL, false);
1803 ucl_parser_register_macro (struct ucl_parser *parser, const char *macro,
1804 ucl_macro_handler handler, void* ud)
1806 struct ucl_macro *new;
1808 if (macro == NULL || handler == NULL) {
1811 new = UCL_ALLOC (sizeof (struct ucl_macro));
1815 memset (new, 0, sizeof (struct ucl_macro));
1816 new->handler = handler;
1817 new->name = strdup (macro);
1819 HASH_ADD_KEYPTR (hh, parser->macroes, new->name, strlen (new->name), new);
1823 ucl_parser_register_variable (struct ucl_parser *parser, const char *var,
1826 struct ucl_variable *new = NULL, *cur;
1832 /* Find whether a variable already exists */
1833 LL_FOREACH (parser->variables, cur) {
1834 if (strcmp (cur->var, var) == 0) {
1840 if (value == NULL) {
1843 /* Remove variable */
1844 LL_DELETE (parser->variables, new);
1847 UCL_FREE (sizeof (struct ucl_variable), new);
1856 new = UCL_ALLOC (sizeof (struct ucl_variable));
1860 memset (new, 0, sizeof (struct ucl_variable));
1861 new->var = strdup (var);
1862 new->var_len = strlen (var);
1863 new->value = strdup (value);
1864 new->value_len = strlen (value);
1866 LL_PREPEND (parser->variables, new);
1870 new->value = strdup (value);
1871 new->value_len = strlen (value);
1877 ucl_parser_add_chunk (struct ucl_parser *parser, const unsigned char *data,
1880 struct ucl_chunk *chunk;
1882 if (data == NULL || len == 0) {
1883 ucl_create_err (&parser->err, "invalid chunk added");
1886 if (parser->state != UCL_STATE_ERROR) {
1887 chunk = UCL_ALLOC (sizeof (struct ucl_chunk));
1888 if (chunk == NULL) {
1889 ucl_create_err (&parser->err, "cannot allocate chunk structure");
1892 chunk->begin = data;
1893 chunk->remain = len;
1894 chunk->pos = chunk->begin;
1895 chunk->end = chunk->begin + len;
1898 LL_PREPEND (parser->chunks, chunk);
1899 parser->recursion ++;
1900 if (parser->recursion > UCL_MAX_RECURSION) {
1901 ucl_create_err (&parser->err, "maximum include nesting limit is reached: %d",
1905 return ucl_state_machine (parser);
1908 ucl_create_err (&parser->err, "a parser is in an invalid state");
1914 ucl_parser_add_string (struct ucl_parser *parser, const char *data,
1918 ucl_create_err (&parser->err, "invalid string added");
1922 len = strlen (data);
1925 return ucl_parser_add_chunk (parser, (const unsigned char *)data, len);