Improve int parsing and allow negatives
This commit is contained in:
14
src/parse.c
14
src/parse.c
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user