Improve int parsing and allow negatives

This commit is contained in:
2022-11-16 09:52:03 -05:00
parent 41bebd1472
commit 542366f74e
2 changed files with 47 additions and 1 deletions

View File

@@ -9,6 +9,7 @@
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define START_LIST_CHAR '('
#define END_LIST_CHAR ')'
@@ -71,14 +72,24 @@ struct ucl_object *ucl_token_next(const char **curr_src) {
str = strndup(start + 1, *curr_src - start - 2);
// TODO: free
return ucl_string_create(str);
case '-':
case '+':
case '0'...'9': {
// TODO: Add support for negative integers
char *end = NULL;
errno = 0;
long value = strtol(*curr_src, &end, 0);
if (errno || *curr_src == end) {
goto symbol_case;
}
*curr_src = end;
if (!ucl_is_delimiter(*end)) {
return ucl_error_create("Integer value contained non-digits");
}
return ucl_int_create(value);
}
default:
symbol_case:
while (!ucl_is_delimiter(**curr_src)) {
(*curr_src)++;
}
@@ -100,6 +111,7 @@ struct ucl_object *ucl_tokenize(const char *source) {
const char *curr_src = source;
while ((curr_token = ucl_token_next(&curr_src)) != NULL) {
UCL_RET_IF_ERROR(curr_token);
tokens_tail = ucl_list_append(tokens_tail, curr_token);
}