diff options
| author | jvoisin | 2021-01-30 21:24:39 +0100 |
|---|---|---|
| committer | jvoisin | 2021-01-30 21:24:39 +0100 |
| commit | e7de504856c4b6140c40b5e766459c7ed1f9e992 (patch) | |
| tree | 6913d518c648be3a5bf0d4375b1c8d82b5ed9971 | |
| parent | 467f965bf178b1c4d60ddac87af14718f6013bab (diff) | |
Improve a bit the typing of the parser
Use enum members instead of their numbers directly.
| -rw-r--r-- | src/sp_var_parser.c | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/src/sp_var_parser.c b/src/sp_var_parser.c index 1544030..b066b84 100644 --- a/src/sp_var_parser.c +++ b/src/sp_var_parser.c | |||
| @@ -124,7 +124,7 @@ static int is_token_valid(sp_list_node *tokens_list, elem_type quote, | |||
| 124 | } | 124 | } |
| 125 | break; | 125 | break; |
| 126 | case ARRAY_END: | 126 | case ARRAY_END: |
| 127 | if (!quote) { | 127 | if (UNDEFINED == 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) { |
| @@ -138,7 +138,8 @@ static int is_token_valid(sp_list_node *tokens_list, elem_type quote, | |||
| 138 | } | 138 | } |
| 139 | break; | 139 | break; |
| 140 | case OBJECT: | 140 | case OBJECT: |
| 141 | if (!quote && -1 == is_next_token_empty(token, token_next, str)) { | 141 | if (UNDEFINED == quote && |
| 142 | -1 == is_next_token_empty(token, token_next, str)) { | ||
| 142 | return -1; | 143 | return -1; |
| 143 | } | 144 | } |
| 144 | if (pos == 0 && *str != VARIABLE_TOKEN) { | 145 | if (pos == 0 && *str != VARIABLE_TOKEN) { |
| @@ -146,7 +147,8 @@ static int is_token_valid(sp_list_node *tokens_list, elem_type quote, | |||
| 146 | } | 147 | } |
| 147 | break; | 148 | break; |
| 148 | case CLASS: | 149 | case CLASS: |
| 149 | if (!quote && -1 == is_next_token_empty(token, token_next, str)) { | 150 | if (UNDEFINED == quote && |
| 151 | -1 == is_next_token_empty(token, token_next, str)) { | ||
| 150 | return -1; | 152 | return -1; |
| 151 | } | 153 | } |
| 152 | break; | 154 | break; |
| @@ -160,7 +162,7 @@ static sp_tree *parse_tokens(const char *restrict str, | |||
| 160 | sp_list_node *tokens_list) { | 162 | sp_list_node *tokens_list) { |
| 161 | size_t pos = 0; | 163 | size_t pos = 0; |
| 162 | int array_count = 0, pos_idx_start = -1; | 164 | int array_count = 0, pos_idx_start = -1; |
| 163 | elem_type quote = 0; | 165 | elem_type quote = UNDEFINED; |
| 164 | sp_tree *tree = sp_tree_new(); | 166 | sp_tree *tree = sp_tree_new(); |
| 165 | 167 | ||
| 166 | for (; tokens_list && tokens_list->data; tokens_list = tokens_list->next) { | 168 | for (; tokens_list && tokens_list->data; tokens_list = tokens_list->next) { |
| @@ -173,11 +175,15 @@ static sp_tree *parse_tokens(const char *restrict str, | |||
| 173 | goto error; | 175 | goto error; |
| 174 | } | 176 | } |
| 175 | if (token->type == INTERPRETED_STRING || token->type == LITERAL_STRING) { | 177 | if (token->type == INTERPRETED_STRING || token->type == LITERAL_STRING) { |
| 176 | pos = (!quote && !array_count) ? pos + strlen(token->text_repr) : pos; | 178 | pos = (UNDEFINED == quote && !array_count) |
| 177 | quote = (!quote) ? token->type : (quote == token->type) ? 0 : quote; | 179 | ? pos + strlen(token->text_repr) |
| 180 | : pos; | ||
| 181 | quote = (UNDEFINED == quote) ? token->type | ||
| 182 | : (quote == token->type) ? 0 | ||
| 183 | : quote; | ||
| 178 | token->type = INTERPRETED_STRING; | 184 | token->type = INTERPRETED_STRING; |
| 179 | } | 185 | } |
| 180 | if (quote == 0) { | 186 | if (UNDEFINED == quote) { |
| 181 | if (token->type == ARRAY) { | 187 | if (token->type == ARRAY) { |
| 182 | pos_idx_start = (array_count) | 188 | pos_idx_start = (array_count) |
| 183 | ? pos_idx_start | 189 | ? pos_idx_start |
| @@ -210,7 +216,7 @@ static sp_tree *parse_tokens(const char *restrict str, | |||
| 210 | sp_log_err("config", "You forgot to close a bracket."); | 216 | sp_log_err("config", "You forgot to close a bracket."); |
| 211 | goto error; | 217 | goto error; |
| 212 | } | 218 | } |
| 213 | if (quote != 0) { | 219 | if (quote != UNDEFINED) { |
| 214 | sp_log_err("config", "Missing a closing quote."); | 220 | sp_log_err("config", "Missing a closing quote."); |
| 215 | error: | 221 | error: |
| 216 | sp_tree_free(tree); | 222 | sp_tree_free(tree); |
