diff options
Diffstat (limited to 'src/sp_var_parser.c')
| -rw-r--r-- | src/sp_var_parser.c | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/src/sp_var_parser.c b/src/sp_var_parser.c index 2b4c544..3f3dcdc 100644 --- a/src/sp_var_parser.c +++ b/src/sp_var_parser.c | |||
| @@ -72,7 +72,7 @@ static int create_var(sp_tree *tree, const char *restrict value, | |||
| 72 | sp_log_err("config", "Can't allocate a strndup"); | 72 | sp_log_err("config", "Can't allocate a strndup"); |
| 73 | return -1; | 73 | return -1; |
| 74 | } | 74 | } |
| 75 | if (var_node->type != STRING_DELIMITER && !is_var_name_valid(var_node->value)) { | 75 | if (var_node->type != INTERPRETED_STRING && !is_var_name_valid(var_node->value)) { |
| 76 | sp_log_err("config", "Invalid var name: %s.", var_node->value); | 76 | sp_log_err("config", "Invalid var name: %s.", var_node->value); |
| 77 | return -1; | 77 | return -1; |
| 78 | } | 78 | } |
| @@ -101,7 +101,7 @@ static int is_next_token_empty(sp_conf_token *token, sp_conf_token *token_next, | |||
| 101 | return 0; | 101 | return 0; |
| 102 | } | 102 | } |
| 103 | 103 | ||
| 104 | static int is_token_valid(sp_list_node *tokens_list, elem_type ignore, | 104 | static int is_token_valid(sp_list_node *tokens_list, elem_type quote, |
| 105 | int array_count, const char * restrict str, | 105 | int array_count, const char * restrict str, |
| 106 | size_t pos) { | 106 | size_t pos) { |
| 107 | sp_conf_token *token = (sp_conf_token *)tokens_list->data; | 107 | sp_conf_token *token = (sp_conf_token *)tokens_list->data; |
| @@ -111,9 +111,9 @@ static int is_token_valid(sp_list_node *tokens_list, elem_type ignore, | |||
| 111 | token_next = (sp_conf_token *)tokens_list->next->data; | 111 | token_next = (sp_conf_token *)tokens_list->next->data; |
| 112 | } | 112 | } |
| 113 | switch (token->type) { | 113 | switch (token->type) { |
| 114 | case ESC_STRING_DELIMITER: | 114 | case LITERAL_STRING: |
| 115 | case STRING_DELIMITER: | 115 | case INTERPRETED_STRING: |
| 116 | if (ignore == token->type) { | 116 | if (quote == token->type) { |
| 117 | if (token_next) { | 117 | if (token_next) { |
| 118 | if (token_next->pos != token->pos + 1) { | 118 | if (token_next->pos != token->pos + 1) { |
| 119 | return -1; | 119 | return -1; |
| @@ -124,12 +124,12 @@ static int is_token_valid(sp_list_node *tokens_list, elem_type ignore, | |||
| 124 | } | 124 | } |
| 125 | break; | 125 | break; |
| 126 | case ARRAY_END: | 126 | case ARRAY_END: |
| 127 | if (!ignore) { | 127 | if (!quote) { |
| 128 | if (array_count < 1) { | 128 | if (array_count < 1) { |
| 129 | return -1; | 129 | return -1; |
| 130 | } else if (token_next) { | 130 | } else if (token_next) { |
| 131 | if (token_next->type == STRING_DELIMITER | 131 | if (token_next->type == INTERPRETED_STRING |
| 132 | || token_next->type == ESC_STRING_DELIMITER) { | 132 | || token_next->type == LITERAL_STRING) { |
| 133 | return -1; | 133 | return -1; |
| 134 | } | 134 | } |
| 135 | } else if (token->pos != strlen(str) - strlen(token->text_repr)) { | 135 | } else if (token->pos != strlen(str) - strlen(token->text_repr)) { |
| @@ -138,7 +138,7 @@ static int is_token_valid(sp_list_node *tokens_list, elem_type ignore, | |||
| 138 | } | 138 | } |
| 139 | break; | 139 | break; |
| 140 | case OBJECT: | 140 | case OBJECT: |
| 141 | if (!ignore && -1 == is_next_token_empty(token, token_next, str)) { | 141 | if (!quote && -1 == is_next_token_empty(token, token_next, str)) { |
| 142 | return -1; | 142 | return -1; |
| 143 | } | 143 | } |
| 144 | if (pos == 0 && *str != VARIABLE_TOKEN) { | 144 | if (pos == 0 && *str != VARIABLE_TOKEN) { |
| @@ -146,7 +146,7 @@ static int is_token_valid(sp_list_node *tokens_list, elem_type ignore, | |||
| 146 | } | 146 | } |
| 147 | break; | 147 | break; |
| 148 | case CLASS: | 148 | case CLASS: |
| 149 | if (!ignore && -1 == is_next_token_empty(token, token_next, str)) { | 149 | if (!quote && -1 == is_next_token_empty(token, token_next, str)) { |
| 150 | return -1; | 150 | return -1; |
| 151 | } | 151 | } |
| 152 | break; | 152 | break; |
| @@ -160,7 +160,7 @@ static sp_tree *parse_tokens(const char * restrict str, | |||
| 160 | sp_list_node *tokens_list) { | 160 | sp_list_node *tokens_list) { |
| 161 | size_t pos = 0; | 161 | size_t pos = 0; |
| 162 | int array_count = 0, pos_idx_start = -1; | 162 | int array_count = 0, pos_idx_start = -1; |
| 163 | elem_type ignore = 0; | 163 | elem_type quote = 0; |
| 164 | sp_tree *tree = sp_tree_new(); | 164 | sp_tree *tree = sp_tree_new(); |
| 165 | 165 | ||
| 166 | for (; tokens_list && tokens_list->data; tokens_list = tokens_list->next) { | 166 | for (; tokens_list && tokens_list->data; tokens_list = tokens_list->next) { |
| @@ -168,16 +168,16 @@ static sp_tree *parse_tokens(const char * restrict str, | |||
| 168 | size_t value_len; | 168 | size_t value_len; |
| 169 | char *idx = NULL; | 169 | char *idx = NULL; |
| 170 | 170 | ||
| 171 | if (-1 == is_token_valid(tokens_list, ignore, array_count, str, pos)) { | 171 | if (-1 == is_token_valid(tokens_list, quote, array_count, str, pos)) { |
| 172 | sp_log_err("config", "Invalid `%s` position.", token->text_repr); | 172 | sp_log_err("config", "Invalid `%s` position.", token->text_repr); |
| 173 | goto error; | 173 | goto error; |
| 174 | } | 174 | } |
| 175 | if (token->type == STRING_DELIMITER || token->type == ESC_STRING_DELIMITER) { | 175 | if (token->type == INTERPRETED_STRING || token->type == LITERAL_STRING) { |
| 176 | pos = (!ignore && !array_count) ? pos + strlen(token->text_repr) : pos; | 176 | pos = (!quote && !array_count) ? pos + strlen(token->text_repr) : pos; |
| 177 | ignore = (!ignore) ? token->type : (ignore == token->type) ? 0 : ignore; | 177 | quote = (!quote) ? token->type : (quote == token->type) ? 0 : quote; |
| 178 | token->type = STRING_DELIMITER; | 178 | token->type = INTERPRETED_STRING; |
| 179 | } | 179 | } |
| 180 | if (ignore == 0) { | 180 | if (quote == 0) { |
| 181 | if (token->type == ARRAY) { | 181 | if (token->type == ARRAY) { |
| 182 | pos_idx_start = (array_count) ? pos_idx_start : (int)(token->pos + strlen(token->text_repr)); | 182 | pos_idx_start = (array_count) ? pos_idx_start : (int)(token->pos + strlen(token->text_repr)); |
| 183 | array_count++; | 183 | array_count++; |
| @@ -204,7 +204,12 @@ static sp_tree *parse_tokens(const char * restrict str, | |||
| 204 | } | 204 | } |
| 205 | } | 205 | } |
| 206 | 206 | ||
| 207 | if (ignore != 0 || array_count != 0) { | 207 | if (array_count != 0) { |
| 208 | sp_log_err("config", "You forgot to close a bracket."); | ||
| 209 | goto error; | ||
| 210 | } | ||
| 211 | if (quote != 0) { | ||
| 212 | sp_log_err("config", "Missing a closing quote."); | ||
| 208 | error: | 213 | error: |
| 209 | sp_tree_free(tree); | 214 | sp_tree_free(tree); |
| 210 | return NULL; | 215 | return NULL; |
| @@ -223,8 +228,8 @@ sp_tree *parse_var(const char *line) { | |||
| 223 | {.type=OBJECT, .text_repr=OBJECT_TOKEN}, | 228 | {.type=OBJECT, .text_repr=OBJECT_TOKEN}, |
| 224 | {.type=ARRAY, .text_repr=ARRAY_TOKEN}, | 229 | {.type=ARRAY, .text_repr=ARRAY_TOKEN}, |
| 225 | {.type=ARRAY_END, .text_repr=ARRAY_END_TOKEN}, | 230 | {.type=ARRAY_END, .text_repr=ARRAY_END_TOKEN}, |
| 226 | {.type=STRING_DELIMITER, .text_repr=STRING_TOKEN}, | 231 | {.type=INTERPRETED_STRING, .text_repr=STRING_TOKEN}, |
| 227 | {.type=ESC_STRING_DELIMITER, .text_repr=ESC_STRING_TOKEN}, | 232 | {.type=LITERAL_STRING, .text_repr=ESC_STRING_TOKEN}, |
| 228 | {.type=CLASS, .text_repr=CLASS_TOKEN} | 233 | {.type=CLASS, .text_repr=CLASS_TOKEN} |
| 229 | }; | 234 | }; |
| 230 | 235 | ||
