added tlibc error handling everywhere

This commit is contained in:
Timerix 2025-11-26 16:42:28 +05:00
parent 048542d079
commit 2082d56c25
27 changed files with 374 additions and 308 deletions

View File

@ -1,21 +1,42 @@
# tlibtoml # tlibtoml
A fork of [libtoml](https://github.com/brglng/libtoml) rewritten to use [tlibc](https://timerix.ddns.net/git/Timerix/tlibtoml) instead of its own (worse) implementation of collections and strings. For example, Table from libtoml is just an array of key-value pairs, where search happens by calling `strcmp()` on each element. Such inefficient code hurts my mind, so i have no other choise than to rewrite this library. A fork of [libtoml](https://github.com/brglng/libtoml) rewritten to use [tlibc](https://timerix.ddns.net/git/Timerix/tlibtoml) instead of its own (worse) implementation of collections and strings. For example, Table from libtoml is just an array of key-value pairs, where search happens by calling `strcmp()` on each element. Such inefficient code hurts my mind, so i have no other choise than to rewrite this library.
## Build ## Build
1. Clone the repository. 1. Clone this repository.
``` ```
git clone https://timerix.ddns.net/git/Timerix/tlibtoml.git git clone https://timerix.ddns.net/git/Timerix/tlibtoml.git
``` ```
2. Install [cbuild](https://timerix.ddns.net/git/Timerix/cbuild).
3. Clone [tlibc](https://timerix.ddns.net/git/Timerix/tlibc). By default 2. Install [cbuild](https://timerix.ddns.net/git/Timerix/cbuild) version specified in `project.config`.
`dependencies/tlibc.config` expects that `tlibc/` is present in the same directory as `tlibtoml/`.
3. Clone [tlibc](https://timerix.ddns.net/git/Timerix/tlibc).
By default `dependencies/tlibc.config` expects that `tlibc/` is present in the same directory as `tlibtoml/`.
If you cloned it to another directory, change `DEPENDENCIES_DIR` in `tlibtoml/project.user.config`. If you cloned it to another directory, change `DEPENDENCIES_DIR` in `tlibtoml/project.user.config`.
``` ```
git clone https://timerix.ddns.net/git/Timerix/tlibc.git git clone https://timerix.ddns.net/git/Timerix/tlibc.git
``` ```
4. Build and run tests 4. Build and run tests
``` ```
cd tlibtoml cd tlibtoml
cbuild build_exec exec cbuild build_exec exec
``` ```
5. To build library use tasks `build_static_lib[_dbg]` or `build_shared_lib[_dbg]` 5. To build library use tasks `build_static_lib[_dbg]` or `build_shared_lib[_dbg]`
## Usage
```c
#include "tlibtoml.h"
int main(){
Deferral(32); // reserve memory for 32 defers
try_fatal_void(tlibc_init());
try_fatal_void(tlibtoml_init());
Defer(tlibc_deinit());
Defer(tlibtoml_deinit());
Return 0; // call defers
}
```

View File

@ -15,6 +15,10 @@ extern "C" {
#include "tlibc/string/str.h" #include "tlibc/string/str.h"
#include "tlibc/collections/HashMap.h" #include "tlibc/collections/HashMap.h"
Result(void) tlibtoml_init();
void tlibtoml_deinit();
typedef DateTime TomlDateTime; typedef DateTime TomlDateTime;
typedef HashMap(TomlValue) TomlTable; typedef HashMap(TomlValue) TomlTable;
@ -123,7 +127,9 @@ TomlValue TomlValue_new_float(f64 flt);
TomlValue TomlValue_new_datetime(void); TomlValue TomlValue_new_datetime(void);
TomlValue TomlValue_new_bool(bool b); TomlValue TomlValue_new_bool(bool b);
/// copies the string /// copies the string
TomlValue TomlValue_new_string(str s); TomlValue TomlValue_copy_str(str s);
/// doesnt copy the string
TomlValue TomlValue_move_str(str s);
void TomlValue_destroy(TomlValue* self); void TomlValue_destroy(TomlValue* self);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
CBUILD_VERSION=2.3.0 CBUILD_VERSION=2.3.2
PROJECT="tlibtoml" PROJECT="tlibtoml"
CMP_C="gcc" CMP_C="gcc"

View File

@ -7,7 +7,7 @@
TomlTable* TomlTable_new(void) TomlTable* TomlTable_new(void)
{ {
TomlTable* table = malloc(sizeof(TomlTable)); TomlTable* table = malloc(sizeof(TomlTable));
HashMap_construct(table, TomlValue, (FreeFunction)TomlValue_destroy); HashMap_construct(table, TomlValue, (Destructor_t)TomlValue_destroy);
return table; return table;
} }

View File

@ -38,10 +38,15 @@ TomlValue TomlValue_new(TomlType type)
return value; return value;
} }
TomlValue TomlValue_from_str(str s) TomlValue TomlValue_copy_str(str s)
{ {
return TomlValue_move_str(str_copy(s));
}
TomlValue TomlValue_move_str(str s){
TomlValue value = {0}; TomlValue value = {0};
value.value.s = str_copy(s); value.value.s = (str*)malloc(sizeof(str));
*value.value.s = s;
value.type = TLIBTOML_STRING; value.type = TLIBTOML_STRING;
return value; return value;
} }
@ -98,7 +103,7 @@ void TomlValue_destroy(TomlValue* self)
switch (self->type) { switch (self->type) {
case TLIBTOML_STRING: case TLIBTOML_STRING:
str_destroy(self->value.s); str_free(self->value.s);
break; break;
case TLIBTOML_TABLE: case TLIBTOML_TABLE:
TomlTable_free(self->value.table); TomlTable_free(self->value.table);

15
src/tlibtoml.c Normal file
View File

@ -0,0 +1,15 @@
#include "tlibtoml.h"
ErrorCodePage_define(TLIBTOML);
Result(void) tlibtoml_init(){
Deferral(8);
ErrorCodePage_register(TLIBTOML);
Return RESULT_VOID;
}
void tlibtoml_deinit(){
}

View File

@ -2,17 +2,16 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#pragma onces #pragma once
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include "tlibtoml/toml.h" #include "tlibtoml.h"
#include "tlibc/string/StringBuilder.h" #include "tlibc/string/StringBuilder.h"
#include "tlibc/collections/HashMap.h" #include "tlibc/collections/HashMap.h"
#include "tlibc/collections/List.h" #include "tlibc/collections/List.h"
// TODO: get rid of libc assert
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
@ -30,26 +29,27 @@ void toml_parser_free(TomlParser* self);
void toml_move_next(TomlParser* self); void toml_move_next(TomlParser* self);
void toml_next_n(TomlParser* self, i32 n); void toml_next_n(TomlParser* self, i32 n);
str toml_parse_bare_key(TomlParser* self); u32 toml_hex_char_to_int(char ch);
char toml_hex_char_to_int(char ch);
Result(void) toml_encode_unicode_scalar(StringBuilder* sb_ptr, TomlParser* parser, i32 n); Result(void) toml_encode_unicode_scalar(StringBuilder* sb_ptr, TomlParser* parser, i32 n);
Result(str*) toml_parse_basic_string(TomlParser* self); Result(void) toml_parse_bare_key(TomlParser* self, str* out_str);
Result(str*) toml_parse_literal_string(TomlParser* self); Result(void) toml_parse_basic_string(TomlParser* self, str* out_str);
TomlValue toml_parse_basic_string_value(TomlParser* self); Result(void) toml_parse_literal_string(TomlParser* self, str* out_str);
TomlValue toml_parse_literal_string_value(TomlParser* self); Result(void) toml_parse_basic_string_value(TomlParser* self, TomlValue* out_value);
TomlValue toml_parse_multi_line_basic_string(TomlParser* self); Result(void) toml_parse_literal_string_value(TomlParser* self, TomlValue* out_value);
TomlValue toml_parse_multi_line_literal_string(TomlParser* self); Result(void) toml_parse_multi_line_basic_string(TomlParser* self, TomlValue* out_value);
TomlValue toml_parse_datetime(str s); Result(void) toml_parse_multi_line_literal_string(TomlParser* self, TomlValue* out_value);
TomlValue toml_parse_int_or_float_or_time(TomlParser* self); Result(void) toml_parse_datetime(str s, TomlValue* out_value);
TomlValue toml_parse_bool(TomlParser* self); Result(void) toml_parse_int_or_float_or_time(TomlParser* self, TomlValue* out_value);
TomlValue toml_parse_value(TomlParser* self); Result(void) toml_parse_bool(TomlParser* self, TomlValue* out_value);
TomlValue toml_parse_array(TomlParser* self); Result(void) toml_parse_value(TomlParser* self, TomlValue* out_value);
TomlValue toml_parse_inline_table(TomlParser* self); Result(void) toml_parse_array(TomlParser* self, TomlValue* out_value);
Result(void) toml_parse_table(TomlParser* self, TomlTable* table); Result(void) toml_parse_inline_table(TomlParser* self, TomlValue* out_value);
Result(void) toml_parse_key_value(TomlParser* self, TomlTable* table); Result(void) toml_parse_table(TomlParser* self, TomlTable* target_table);
Result(TomlTable*) toml_walk_table_path(TomlParser* parser, TomlTable* table, Result(void) toml_parse_key_value(TomlParser* self, TomlTable* target_table);
TomlArray* key_path, i32 is_array, i32 create_if_not_exist); Result(TomlTable*) toml_walk_table_path(TomlParser* parser, TomlTable* target_table,
TomlArray* key_path, bool is_array, bool create_if_not_exist);
Result(TomlTable*) toml_parse(TomlParser* self);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -11,7 +11,38 @@ Result(TomlTable*) toml_load_str_filename(str s, cstr filename)
TomlParser* parser = toml_parser_new(s, filename); TomlParser* parser = toml_parser_new(s, filename);
Defer(toml_parser_free(parser)); Defer(toml_parser_free(parser));
try(TomlTable* table, p, = toml_parse(parser)); try(TomlTable* table, p, toml_parse(parser));
Return RESULT_VALUE(p, table);
}
Result(TomlTable*) toml_load_file_filename(FILE* file, cstr filename)
{
Deferral(4);
const u32 file_chunk_size = 8*1024;
StringBuilder sb = StringBuilder_alloc(file_chunk_size);
Defer(StringBuilder_destroy(&sb));
u64 count = 0;
u64 remaining_capacity = 0;
do {
remaining_capacity = sb.buffer.capacity - sb.buffer.len;
count = fread((char*)sb.buffer.data + sb.buffer.len, 1, remaining_capacity, file);
if (ferror(file)) {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_OS,
"Error when reading %s [errno %d: %s]",
filename, errno, strerror(errno));
}
sb.buffer.len += count;
if (sb.buffer.len + 1 >= sb.buffer.capacity) {
StringBuilder_increaseCapacity(&sb, file_chunk_size);
}
} while (count == remaining_capacity);
try(TomlTable* table, p, toml_load_str_filename(StringBuilder_getStr(&sb), filename));
Return RESULT_VALUE(p, table); Return RESULT_VALUE(p, table);
} }
@ -28,40 +59,10 @@ Result(TomlTable*) toml_load_file(FILE* file)
Result(TomlTable*) toml_load_filename(cstr filename) Result(TomlTable*) toml_load_filename(cstr filename)
{ {
Deferral(4); Deferral(1);
try(FILE* f, p, file_open(filename, FO_ReadExisting)); try(FILE* f, p, file_open(filename, FO_ReadExisting));
try(TomlTable* table, p, toml_load_file_filename(f, filename)); try(TomlTable* table, p, toml_load_file_filename(f, filename));
Return RESULT_VALUE(p, table); Return RESULT_VALUE(p, table);
} }
Result(TomlTable*) toml_load_file_filename(FILE* file, cstr filename)
{
Deferral(8);
const u32 file_chunk_size = 8*1024;
StringBuilder sb = StringBuilder_alloc(file_chunk_size);
Defer(StringBuilder_destroy(&sb));
u64 count = 0;
u64 remaining_capacity = 0;
do {
remaining_capacity = sb.buffer.allocated_size - sb.buffer.size;
count = fread((char*)sb.buffer.data + sb.buffer.size, 1, remaining_capacity, file);
if (ferror(file)) {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_OS,
"Error when reading %s [errno %d: %s]",
filename, errno, strerror(errno));
}
sb.buffer.size += count;
if (sb.buffer.size + 1 >= sb.buffer.allocated_size) {
StringBuilder_increaseCapacity(&sb, file_chunk_size);
}
} while (count == remaining_capacity);
try(TomlTable* table, p, toml_load_str_filename(StringBuilder_getStr(&sb), filename));
Return RESULT_VALUE(p, table);
}

View File

@ -6,9 +6,10 @@
Result(TomlTable*) toml_parse(TomlParser* self) Result(TomlTable*) toml_parse(TomlParser* self)
{ {
Deferral(8); Deferral(1);
TomlTable* table = TomlTable_new(); TomlTable* table = TomlTable_new();
Defer(TomlTable_free(table)); bool success = false;
Defer(if(!success) TomlTable_free(table));
while (self->ptr < self->end) { while (self->ptr < self->end) {
char ch = *self->ptr; char ch = *self->ptr;
@ -26,11 +27,9 @@ Result(TomlTable*) toml_parse(TomlParser* self)
toml_move_next(self); toml_move_next(self);
} else if (ch == '[') { } else if (ch == '[') {
toml_move_next(self); toml_move_next(self);
if (toml_parse_table(self, table) != 0) try_void(toml_parse_table(self, table));
return NULL;
} else if (isalnum(ch) || ch == '_' || ch == '-') { } else if (isalnum(ch) || ch == '_' || ch == '-') {
if (toml_parse_key_value(self, table) != 0) try_void(toml_parse_key_value(self, table));
return NULL;
} else if (ch == ' ' || ch == '\t' || ch == '\r') { } else if (ch == ' ' || ch == '\t' || ch == '\r') {
do { do {
toml_move_next(self); toml_move_next(self);
@ -41,5 +40,6 @@ Result(TomlTable*) toml_parse(TomlParser* self)
} }
} }
return table; success = true;
Return RESULT_VALUE(p, table);
} }

View File

@ -4,9 +4,12 @@
#include "toml_internal.h" #include "toml_internal.h"
TomlValue toml_parse_array(TomlParser* self) Result(void) toml_parse_array(TomlParser* self, TomlValue* out_value)
{ {
Deferral(1);
TomlValue array_value = TomlValue_new_array(); TomlValue array_value = TomlValue_new_array();
bool success = false;
Defer(if(!success) TomlValue_destroy(&array_value));
while (self->ptr < self->end) { while (self->ptr < self->end) {
if (isspace(*self->ptr)) { if (isspace(*self->ptr)) {
@ -24,12 +27,8 @@ TomlValue toml_parse_array(TomlParser* self)
toml_move_next(self); toml_move_next(self);
break; break;
} else { } else {
TomlValue value = toml_parse_value(self); TomlValue value;
if (value.type == TLIBTOML_INVALID_TYPE) { try_void(toml_parse_value(self, &value));
TomlValue_destroy(&array_value);
//TODO: error handling
assert(false && "TODO error handling");
}
TomlArray_append(array_value.value.array, value); TomlArray_append(array_value.value.array, value);
@ -53,5 +52,7 @@ TomlValue toml_parse_array(TomlParser* self)
} }
} }
return array_value; success = true;
*out_value = array_value;
Return RESULT_VOID;
} }

View File

@ -0,0 +1,26 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "toml_internal.h"
Result(void) toml_parse_bare_key(TomlParser* self, str* s_out)
{
Deferral(1);
cstr s = self->ptr;
u64 len = 0;
while (self->ptr < self->end) {
char ch = *self->ptr;
if (!(isalnum(ch) || ch == '_' || ch == '-'))
break;
len++;
toml_move_next(self);
}
*s_out = str_copy(str_construct((void*)s, len, false));
Return RESULT_VOID;
}

View File

@ -4,10 +4,12 @@
#include "toml_internal.h" #include "toml_internal.h"
Result(str*) toml_parse_basic_string(TomlParser* self) Result(void) toml_parse_basic_string(TomlParser* self, str* out_str)
{ {
Deferral(16); Deferral(1);
StringBuilder sb = StringBuilder_alloc(32); StringBuilder sb = StringBuilder_alloc(32);
bool success = false;
Defer(if(!success) StringBuilder_destroy(&sb));
while (self->ptr < self->end &&* self->ptr != '\"' &&* self->ptr != '\n') { while (self->ptr < self->end &&* self->ptr != '\"' &&* self->ptr != '\n') {
char ch1 = *self->ptr; char ch1 = *self->ptr;
@ -45,7 +47,8 @@ Result(str*) toml_parse_basic_string(TomlParser* self)
toml_move_next(self); toml_move_next(self);
try_void(toml_encode_unicode_scalar(&sb, self, 8)); try_void(toml_encode_unicode_scalar(&sb, self, 8));
} else { } else {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: invalid escape character", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: invalid escape character",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
} }
} else { } else {
@ -55,9 +58,14 @@ Result(str*) toml_parse_basic_string(TomlParser* self)
} }
if (self->ptr >= self->end ||* self->ptr != '\"' ||* self->ptr == '\n') { if (self->ptr >= self->end ||* self->ptr != '\"' ||* self->ptr == '\n') {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: unterminated basic string", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: unterminated basic string",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
} }
toml_move_next(self); toml_move_next(self);
*out_str = StringBuilder_getStr(&sb);
success = true;
Return RESULT_VOID;
} }

View File

@ -4,13 +4,13 @@
#include "toml_internal.h" #include "toml_internal.h"
TomlValue toml_parse_basic_string_value(TomlParser* self) Result(void) toml_parse_basic_string_value(TomlParser* self, TomlValue* out_value)
{ {
str s = toml_parse_basic_string(self); Deferral(1);
//TODO: error handling
assert(s.data != NULL); str s;
try_void(toml_parse_basic_string(self, &s));
TomlValue value = TomlValue_new(TLIBTOML_STRING); *out_value = TomlValue_move_str(s);
value.value.s = s;
return value; Return RESULT_VOID;
} }

View File

@ -4,21 +4,41 @@
#include "toml_internal.h" #include "toml_internal.h"
TomlValue toml_parse_bool(TomlParser* self) static bool is_value_end(cstr expected_value_end, cstr input_end){
{ if(expected_value_end == input_end)
if (self->ptr + 4 <= self->end && strncmp(self->ptr, "true", 4) == 0 && return true;
(self->ptr + 4 == self->end || isspace(*(self->ptr + 4)) || *(self->ptr + 4) == ',' || *(self->ptr + 4) == ']' || *(self->ptr + 4) == '}')) {
toml_next_n(self, 4); char next_ch = *expected_value_end;
return TomlValue_new_bool(true); return isspace(next_ch) || next_ch == ',' || next_ch == ']' || next_ch == '}';
} }
if (self->ptr + 5 <= self->end && strncmp(self->ptr, "false", 5) == 0 && Result(void) toml_parse_bool(TomlParser* self, TomlValue* out_value)
(self->ptr + 5 == self->end || isspace(*(self->ptr + 5)) || *(self->ptr + 5) == ',' || *(self->ptr + 5) == ']' || *(self->ptr + 5) == '}')) { {
toml_next_n(self, 5); Deferral(1);
return TomlValue_new_bool(false);
} cstr val_end = self->ptr + 4;
if(val_end <= self->end && strncmp(self->ptr, "true", 4) == 0)
//TODO: error handling {
assert(false && "TODO error handling"); if(is_value_end(val_end, self->end))
return TomlValue_new(TLIBTOML_INVALID_TYPE); {
toml_next_n(self, 4);
*out_value = TomlValue_new_bool(true);
Return RESULT_VOID;
}
}
val_end = self->ptr + 5;
if(val_end <= self->end && strncmp(self->ptr, "false", 5) == 0)
{
if(is_value_end(val_end, self->end))
{
toml_next_n(self, 5);
*out_value = TomlValue_new_bool(false);
Return RESULT_VOID;
}
}
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: value of unknown type",
self->filename, self->lineno, self->colno);
} }

View File

@ -4,9 +4,13 @@
#include "toml_internal.h" #include "toml_internal.h"
TomlValue toml_parse_datetime(str s) Result(void) toml_parse_datetime(str s, TomlValue* out_value)
{ {
Deferral(1);
(void)s; (void)s;
//TODO: parse datetime //TODO: parse datetime
return TomlValue_new(TLIBTOML_DATETIME); try_assert(false && "DateTime parsing is not implemented");
TomlValue value = TomlValue_new(TLIBTOML_DATETIME);
*out_value = value;
Return RESULT_VOID;
} }

View File

@ -4,9 +4,13 @@
#include "toml_internal.h" #include "toml_internal.h"
TomlValue toml_parse_inline_table(TomlParser* self) Result(void) toml_parse_inline_table(TomlParser* self, TomlValue* out_value)
{ {
Deferral(1);
TomlValue table_value = TomlValue_new_table(); TomlValue table_value = TomlValue_new_table();
bool success = false;
Defer(if(!success) TomlValue_destroy(&table_value));
while (self->ptr < self->end) { while (self->ptr < self->end) {
char ch = *self->ptr; char ch = *self->ptr;
@ -17,25 +21,19 @@ TomlValue toml_parse_inline_table(TomlParser* self)
str key; str key;
if (isalnum(ch) || ch == '_') { if (isalnum(ch) || ch == '_') {
key = toml_parse_bare_key(self); try_void(toml_parse_bare_key(self, &key));
if (key.data == NULL)
goto error;
} else if (ch == '\"') { } else if (ch == '\"') {
toml_move_next(self); toml_move_next(self);
key = toml_parse_basic_string(self); try_void(toml_parse_basic_string(self, &key));
if (key.data == NULL)
goto error;
} else if (ch == '\'') { } else if (ch == '\'') {
toml_move_next(self); toml_move_next(self);
key = toml_parse_literal_string(self); try_void(toml_parse_literal_string(self, &key));
if (key.data == NULL)
goto error;
} else if (ch == '}') { } else if (ch == '}') {
break; break;
} else { } else {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: unexpected token", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: unexpected token",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
goto error;
} }
ch = *self->ptr; ch = *self->ptr;
@ -45,15 +43,15 @@ TomlValue toml_parse_inline_table(TomlParser* self)
} }
if (self->ptr == self->end) { if (self->ptr == self->end) {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: unterminated key value pair", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: unterminated key value pair",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
goto error;
} }
if (ch != '=') { if (ch != '=') {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: unexpected token", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: unexpected token",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
goto error;
} }
toml_move_next(self); toml_move_next(self);
@ -65,14 +63,13 @@ TomlValue toml_parse_inline_table(TomlParser* self)
} }
if (self->ptr == self->end) { if (self->ptr == self->end) {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: unterminated key value pair", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: unterminated key value pair",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
goto error;
} }
TomlValue value = toml_parse_value(self); TomlValue value;
if (value.type == TLIBTOML_INVALID_TYPE) try_void(toml_parse_value(self, &value));
goto error;
TomlTable_set(table_value.value.table, key, value); TomlTable_set(table_value.value.table, key, value);
str_destroy(key); str_destroy(key);
@ -89,12 +86,7 @@ TomlValue toml_parse_inline_table(TomlParser* self)
} }
} }
goto end; success = true;
*out_value = table_value;
error: Return RESULT_VOID;
TomlValue_destroy(&table_value);
//TODO: error handling
assert(false && "TODO error handling");
end:
return table_value;
} }

View File

@ -4,15 +4,17 @@
#include "toml_internal.h" #include "toml_internal.h"
TomlValue toml_parse_int_or_float_or_time(TomlParser* self) Result(void) toml_parse_int_or_float_or_time(TomlParser* self, TomlValue* out_value)
{ {
TomlValue value = {0}; Deferral(1);
StringBuilder sb = StringBuilder_alloc(32);
Defer(StringBuilder_destroy(&sb));
TomlValue value = {0};
char type = 'i'; char type = 'i';
i32 base = 10; i32 base = 10;
StringBuilder sb = StringBuilder_alloc(32);
// Determine nan and inf type as float, as we cannot determine by dot. // Determine nan and inf type as float, as we cannot determine by dot.
// But do not strip it because we will append it to the string later // But do not strip it because we will append it to the string later
if (self->ptr + 3 <= self->end && if (self->ptr + 3 <= self->end &&
@ -24,7 +26,8 @@ TomlValue toml_parse_int_or_float_or_time(TomlParser* self)
(strncmp(self->ptr, "+nan", 4) == 0 || (strncmp(self->ptr, "+nan", 4) == 0 ||
strncmp(self->ptr, "-nan", 4) == 0 || strncmp(self->ptr, "-nan", 4) == 0 ||
strncmp(self->ptr, "+inf", 4) == 0 || strncmp(self->ptr, "+inf", 4) == 0 ||
strncmp(self->ptr, "-inf", 4) == 0)) { strncmp(self->ptr, "-inf", 4) == 0))
{
type = 'f'; type = 'f';
} }
@ -67,27 +70,21 @@ TomlValue toml_parse_int_or_float_or_time(TomlParser* self)
type = 'f'; type = 'f';
StringBuilder_append_char(&sb,* self->ptr); StringBuilder_append_char(&sb,* self->ptr);
} else { } else {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: invalid float", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: invalid float",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
StringBuilder_destroy(&sb);
//TODO: error handling
assert(false && "TODO error handling");
} }
} else if (*self->ptr == '_') { } else if (*self->ptr == '_') {
if (type == 't') { if (type == 't') {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: invalid datetime", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: invalid datetime",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
StringBuilder_destroy(&sb);
//TODO: error handling
assert(false && "TODO error handling");
} }
if (!isalnum(last_char)) { if (!isalnum(last_char)) {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: invalid integer or float", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: invalid integer or float",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
StringBuilder_destroy(&sb);
//TODO: error handling
assert(false && "TODO error handling");
} }
} else if (*self->ptr == '-') { } else if (*self->ptr == '-') {
type = 't'; type = 't';
@ -101,40 +98,35 @@ TomlValue toml_parse_int_or_float_or_time(TomlParser* self)
} }
if (last_char == '_') { if (last_char == '_') {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: invalid integer or float or datetime", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: invalid integer or float or datetime",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
StringBuilder_destroy(&sb);
//TODO: error handling
assert(false && "TODO error handling");
} }
if (type == 'i') { if (type == 'i') {
char* end = NULL; char* end = NULL;
char* start = StringBuilder_getStr(&sb).data; char* start = StringBuilder_getStr(&sb).data;
i64 n = strtoll(start, &end, base); i64 n = strtoll(start, &end, base);
if (end < start + sb.buffer.size) { if (end < start + sb.buffer.len) {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: invalid integer", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: invalid integer",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
StringBuilder_destroy(&sb);
//TODO: error handling
assert(false && "TODO error handling");
} }
value = TomlValue_new_integer(n); value = TomlValue_new_integer(n);
} else if (type == 'f') { } else if (type == 'f') {
char* end = NULL; char* end = NULL;
char* start = StringBuilder_getStr(&sb).data; char* start = StringBuilder_getStr(&sb).data;
f64 n = strtod(start, &end); f64 n = strtod(start, &end);
if (end < start + sb.buffer.size) { if (end < start + sb.buffer.len) {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: invalid float", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: invalid float",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
goto cleanup;
} }
value = TomlValue_new_float(n); value = TomlValue_new_float(n);
} else if (type == 't') { } else if (type == 't') {
value = toml_parse_datetime(StringBuilder_getStr(&sb)); try_void(toml_parse_datetime(StringBuilder_getStr(&sb), &value));
} }
cleanup: *out_value = value;
StringBuilder_destroy(&sb); Return RESULT_VOID;
return value;
} }

View File

@ -4,8 +4,10 @@
#include "toml_internal.h" #include "toml_internal.h"
Result(void) toml_parse_key_value(TomlParser* self, TomlTable* table) Result(void) toml_parse_key_value(TomlParser* self, TomlTable* target_table)
{ {
Deferral(1);
while (self->ptr < self->end) { while (self->ptr < self->end) {
char ch; char ch;
@ -19,19 +21,13 @@ Result(void) toml_parse_key_value(TomlParser* self, TomlTable* table)
str key; str key;
if (isalnum(ch) || ch == '_') { if (isalnum(ch) || ch == '_') {
key = toml_parse_bare_key(self); try_void(toml_parse_bare_key(self, &key));
if (key.data == NULL)
return toml_err()->code;
} else if (ch == '\"') { } else if (ch == '\"') {
toml_move_next(self); toml_move_next(self);
key = toml_parse_basic_string(self); try_void(toml_parse_basic_string(self, &key));
if (key.data == NULL)
return toml_err()->code;
} else if (ch == '\'') { } else if (ch == '\'') {
toml_move_next(self); toml_move_next(self);
key = toml_parse_literal_string(self); try_void(toml_parse_literal_string(self, &key));
if (key.data == NULL)
return toml_err()->code;
} else if (ch == '[') { } else if (ch == '[') {
break; break;
} else if (ch == '#') { } else if (ch == '#') {
@ -42,7 +38,8 @@ Result(void) toml_parse_key_value(TomlParser* self, TomlTable* table)
toml_move_next(self); toml_move_next(self);
continue; continue;
} else { } else {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: unexpected token", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: unexpected token",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
} }
@ -53,12 +50,14 @@ Result(void) toml_parse_key_value(TomlParser* self, TomlTable* table)
} }
if (self->ptr == self->end) { if (self->ptr == self->end) {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: unterminated key value pair", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: unterminated key value pair",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
} }
if (ch != '=') { if (ch != '=') {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: unexpected token", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: unexpected token",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
} }
@ -71,15 +70,15 @@ Result(void) toml_parse_key_value(TomlParser* self, TomlTable* table)
} }
if (self->ptr == self->end) { if (self->ptr == self->end) {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: unterminated key value pair", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: unterminated key value pair",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
} }
TomlValue value = toml_parse_value(self); TomlValue value;
if (value.type == TLIBTOML_INVALID_TYPE) try_void(toml_parse_value(self, &value));
return toml_err()->code;
TomlTable_set(table, key, value); TomlTable_set(target_table, key, value);
str_destroy(key); str_destroy(key);
while (self->ptr < self->end && (*self->ptr == ' ' ||* self->ptr == '\t')) { while (self->ptr < self->end && (*self->ptr == ' ' ||* self->ptr == '\t')) {
@ -102,10 +101,11 @@ Result(void) toml_parse_key_value(TomlParser* self, TomlTable* table)
if (*self->ptr == '\n') { if (*self->ptr == '\n') {
toml_move_next(self); toml_move_next(self);
} else { } else {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: new line expected", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: new line expected",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
} }
} }
return TLIBTOML_OK; Return RESULT_VOID;
} }

View File

@ -4,9 +4,9 @@
#include "toml_internal.h" #include "toml_internal.h"
Result(str*) toml_parse_literal_string(TomlParser* self) Result(void) toml_parse_literal_string(TomlParser* self, str* out_str)
{ {
Deferral(4); Deferral(1);
StringBuilder sb = StringBuilder_alloc(32); StringBuilder sb = StringBuilder_alloc(32);
bool success = false; bool success = false;
Defer(if(!success) StringBuilder_destroy(&sb)); Defer(if(!success) StringBuilder_destroy(&sb));
@ -17,12 +17,14 @@ Result(str*) toml_parse_literal_string(TomlParser* self)
} }
if (self->ptr >= self->end ||* self->ptr != '\'' ||* self->ptr == '\n') { if (self->ptr >= self->end ||* self->ptr != '\'' ||* self->ptr == '\n') {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: unterminated literal string", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: unterminated literal string",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
} }
toml_move_next(self); toml_move_next(self);
*out_str = StringBuilder_getStr(&sb);
success = true; success = true;
return StringBuilder_getStr(&sb); Return RESULT_VOID;
} }

View File

@ -4,13 +4,13 @@
#include "toml_internal.h" #include "toml_internal.h"
TomlValue toml_parse_literal_string_value(TomlParser* self) Result(void) toml_parse_literal_string_value(TomlParser* self, TomlValue* out_value)
{ {
str s = toml_parse_literal_string(self); Deferral(1);
//TODO: error handling
assert(s.data != NULL); str s;
try_void(toml_parse_literal_string(self, &s));
*out_value = TomlValue_move_str(s);
TomlValue value = TomlValue_new(TLIBTOML_STRING); Return RESULT_VOID;
value.value.s = s;
return value;
} }

View File

@ -4,9 +4,9 @@
#include "toml_internal.h" #include "toml_internal.h"
TomlValue toml_parse_multi_line_basic_string(TomlParser* self) Result(void) toml_parse_multi_line_basic_string(TomlParser* self, TomlValue* out_value)
{ {
Deferral(4); Deferral(1);
StringBuilder sb = StringBuilder_alloc(32); StringBuilder sb = StringBuilder_alloc(32);
bool success = false; bool success = false;
Defer(if(!success) StringBuilder_destroy(&sb)); Defer(if(!success) StringBuilder_destroy(&sb));
@ -75,7 +75,7 @@ TomlValue toml_parse_multi_line_basic_string(TomlParser* self)
toml_next_n(self, 3); toml_next_n(self, 3);
TomlValue value = TomlValue_new(TLIBTOML_STRING); *out_value = TomlValue_move_str(StringBuilder_getStr(&sb));
value.value.s = StringBuilder_getStr(&sb); success = true;
return value; Return RESULT_VOID;
} }

View File

@ -4,9 +4,12 @@
#include "toml_internal.h" #include "toml_internal.h"
TomlValue toml_parse_multi_line_literal_string(TomlParser* self) Result(void) toml_parse_multi_line_literal_string(TomlParser* self, TomlValue* out_value)
{ {
Deferral(1);
StringBuilder sb = StringBuilder_alloc(32); StringBuilder sb = StringBuilder_alloc(32);
bool success = false;
Defer(if(!success) StringBuilder_destroy(&sb));
if (*self->ptr == '\n') { if (*self->ptr == '\n') {
toml_move_next(self); toml_move_next(self);
@ -18,16 +21,14 @@ TomlValue toml_parse_multi_line_literal_string(TomlParser* self)
} }
if (self->ptr + 3 > self->end || strncmp(self->ptr, "\'\'\'", 3) != 0) { if (self->ptr + 3 > self->end || strncmp(self->ptr, "\'\'\'", 3) != 0) {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: unterminated multi-line literal string", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: unterminated multi-line literal string",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
StringBuilder_destroy(&sb);
//TODO: error handling
assert(false && "TODO error handling");
} }
toml_next_n(self, 3); toml_next_n(self, 3);
TomlValue value = TomlValue_new(TLIBTOML_STRING); *out_value = TomlValue_move_str(StringBuilder_getStr(&sb));
value.value.s = StringBuilder_getStr(&sb); success = true;
return value; Return RESULT_VOID;
} }

View File

@ -4,12 +4,12 @@
#include "toml_internal.h" #include "toml_internal.h"
Result(void) toml_parse_table(TomlParser* self, TomlTable* table) Result(void) toml_parse_table(TomlParser* self, TomlTable* target_table)
{ {
Deferral(16); Deferral(1);
i32 is_array = false; i32 is_array = false;
TomlTable* real_table = table; TomlTable* real_table = target_table;
TomlArray* key_path = TomlArray_new(); TomlArray* key_path = TomlArray_new();
Defer(TomlArray_free(key_path)); Defer(TomlArray_free(key_path));
@ -36,28 +36,20 @@ Result(void) toml_parse_table(TomlParser* self, TomlTable* table)
} else { } else {
str key_part; str key_part;
if (isalnum(*self->ptr) ||* self->ptr == '_') { if (isalnum(*self->ptr) ||* self->ptr == '_') {
key_part = toml_parse_bare_key(self); try_void(toml_parse_bare_key(self, &key_part));
if (key_part.data == NULL)
goto cleanup;
} else if (*self->ptr == '\"') { } else if (*self->ptr == '\"') {
toml_move_next(self); toml_move_next(self);
key_part = toml_parse_basic_string(self); try_void(toml_parse_basic_string(self, &key_part));
if (key_part.data == NULL)
goto cleanup;
} else if (*self->ptr == '\'') { } else if (*self->ptr == '\'') {
toml_move_next(self); toml_move_next(self);
key_part = toml_parse_literal_string(self); try_void(toml_parse_literal_string(self, &key_part));
if (key_part.data == NULL)
goto cleanup;
} else { } else {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: unexpected token", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: unexpected token",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
goto cleanup;
} }
TomlValue key_part_value = TomlValue_new(TLIBTOML_STRING); TomlValue key_part_value = TomlValue_move_str(key_part);
key_part_value.value.s = key_part;
TomlArray_append(key_path, key_part_value); TomlArray_append(key_path, key_part_value);
while (self->ptr < self->end && while (self->ptr < self->end &&
@ -72,9 +64,9 @@ Result(void) toml_parse_table(TomlParser* self, TomlTable* table)
} }
if (key_path->len == 0) { if (key_path->len == 0) {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: empty table name", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: empty table name",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
goto cleanup;
} }
while (self->ptr < self->end && while (self->ptr < self->end &&
@ -83,17 +75,13 @@ Result(void) toml_parse_table(TomlParser* self, TomlTable* table)
} }
if (self->ptr < self->end &&* self->ptr != '\n') { if (self->ptr < self->end &&* self->ptr != '\n') {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: new line expected", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: new line expected",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
goto cleanup;
} }
real_table = toml_walk_table_path(self, table, key_path, is_array, true); try(real_table, p, toml_walk_table_path(self, target_table, key_path, is_array, true));
if (real_table == NULL) try_void(toml_parse_key_value(self, real_table));
goto error;
if(toml_parse_key_value(self, real_table) != TLIBTOML_OK)
goto error;
Return RESULT_VOID; Return RESULT_VOID;
} }

View File

@ -4,39 +4,41 @@
#include "toml_internal.h" #include "toml_internal.h"
TomlValue toml_parse_value(TomlParser* self) Result(void) toml_parse_value(TomlParser* self, TomlValue* out_value)
{ {
Deferral(1);
char ch = *self->ptr; char ch = *self->ptr;
TomlValue value; TomlValue value;
if (strncmp(self->ptr, "\"\"\"", 3) == 0) { if (strncmp(self->ptr, "\"\"\"", 3) == 0) {
toml_next_n(self, 3); toml_next_n(self, 3);
value = toml_parse_multi_line_basic_string(self); try_void(toml_parse_multi_line_basic_string(self, &value));
} else if (strncmp(self->ptr, "\'\'\'", 3) == 0) { } else if (strncmp(self->ptr, "\'\'\'", 3) == 0) {
toml_next_n(self, 3); toml_next_n(self, 3);
value = toml_parse_multi_line_literal_string(self); try_void(toml_parse_multi_line_literal_string(self, &value));
} else if (ch == '\"') { } else if (ch == '\"') {
toml_move_next(self); toml_move_next(self);
value = toml_parse_basic_string_value(self); try_void(toml_parse_basic_string_value(self, &value));
} else if (ch == '\'') { } else if (ch == '\'') {
toml_move_next(self); toml_move_next(self);
value = toml_parse_literal_string_value(self); try_void(toml_parse_literal_string_value(self, &value));
} else if (isdigit(ch) || ch == '+' || ch == '-' || ch == '.' || ch == 'n' || ch == 'i') { } else if (isdigit(ch) || ch == '+' || ch == '-' || ch == '.' || ch == 'n' || ch == 'i') {
value = toml_parse_int_or_float_or_time(self); try_void(toml_parse_int_or_float_or_time(self, &value));
} else if (ch == 't' || ch == 'f') { } else if (ch == 't' || ch == 'f') {
value = toml_parse_bool(self); try_void(toml_parse_bool(self, &value));
} else if (ch == '[') { } else if (ch == '[') {
toml_move_next(self); toml_move_next(self);
value = toml_parse_array(self); try_void(toml_parse_array(self, &value));
} else if (ch == '{') { } else if (ch == '{') {
toml_move_next(self); toml_move_next(self);
value = toml_parse_inline_table(self); try_void(toml_parse_inline_table(self, &value));
} else { } else {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: unexpected token", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: unexpected token",
self->filename, self->lineno, self->colno); self->filename, self->lineno, self->colno);
//TODO: error handling
assert(false && "TODO error handling");
} }
return value; *out_value = value;
Return RESULT_VOID;
} }

View File

@ -5,15 +5,16 @@
#include "toml_internal.h" #include "toml_internal.h"
TomlTable* toml_walk_table_path(TomlParser* parser, TomlTable* table, Result(Table*) toml_walk_table_path(TomlParser* parser, TomlTable* table,
TomlArray* key_path, i32 is_array, i32 create_if_not_exist) TomlArray* key_path, bool is_array, bool create_if_not_exist)
{ {
Deferral(1);
TomlTable* real_table = table; TomlTable* real_table = table;
if (is_array) { if (is_array) {
u64 i = 0; u64 i = 0;
for (; i < key_path->len - 1; i++) { for (; i < key_path->len - 1; i++) {
str part = key_path->elements[i].value.s; str part = *key_path->elements[i].value.s;
TomlValue* t = TomlTable_get(real_table, part); TomlValue* t = TomlTable_get(real_table, part);
if (t == NULL) { if (t == NULL) {
if (create_if_not_exist) { if (create_if_not_exist) {
@ -21,15 +22,16 @@ TomlTable* toml_walk_table_path(TomlParser* parser, TomlTable* table,
TomlTable_set(real_table, part, new_table_value); TomlTable_set(real_table, part, new_table_value);
real_table = new_table_value.value.table; real_table = new_table_value.value.table;
} else { } else {
real_table = NULL; Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
break; "%s:%d:%d: not found key '" FMT_str "'",
parser->filename, parser->lineno, parser->colno, part.len, part.data);
} }
} else { } else {
real_table = t->value.table; real_table = t->value.table;
} }
} }
str part = key_path->elements[i].value.s; str part = *key_path->elements[i].value.s;
TomlValue* t = TomlTable_get(real_table, part); TomlValue* t = TomlTable_get(real_table, part);
if (t == NULL) { if (t == NULL) {
if (create_if_not_exist) { if (create_if_not_exist) {
@ -39,13 +41,15 @@ TomlTable* toml_walk_table_path(TomlParser* parser, TomlTable* table,
TomlTable_set(real_table, part, array_value); TomlTable_set(real_table, part, array_value);
real_table = new_table_value.value.table; real_table = new_table_value.value.table;
} else { } else {
real_table = NULL; Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: not found key '" FMT_str "'",
parser->filename, parser->lineno, parser->colno, part.len, part.data);
} }
} else { } else {
if (t->type != TLIBTOML_ARRAY) { if (t->type != TLIBTOML_ARRAY) {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX, "%s:%d:%d: this key was not an array", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
"%s:%d:%d: this key was not an array",
parser->filename, parser->lineno, parser->colno); parser->filename, parser->lineno, parser->colno);
goto error;
} }
TomlValue new_table_value = TomlValue_new_table(); TomlValue new_table_value = TomlValue_new_table();
@ -54,7 +58,7 @@ TomlTable* toml_walk_table_path(TomlParser* parser, TomlTable* table,
} }
} else { } else {
for (u64 i = 0; i < key_path->len; i++) { for (u64 i = 0; i < key_path->len; i++) {
str part = key_path->elements[i].value.s; str part = *key_path->elements[i].value.s;
TomlValue* t = TomlTable_get(real_table, part); TomlValue* t = TomlTable_get(real_table, part);
if (t == NULL) { if (t == NULL) {
if (create_if_not_exist) { if (create_if_not_exist) {
@ -62,8 +66,9 @@ TomlTable* toml_walk_table_path(TomlParser* parser, TomlTable* table,
TomlTable_set(real_table, part, new_table_value); TomlTable_set(real_table, part, new_table_value);
real_table = new_table_value.value.table; real_table = new_table_value.value.table;
} else { } else {
real_table = NULL; Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
break; "%s:%d:%d: not found key '" FMT_str "'",
parser->filename, parser->lineno, parser->colno, part.len, part.data);
} }
} else { } else {
if (t->type == TLIBTOML_ARRAY) { if (t->type == TLIBTOML_ARRAY) {
@ -75,13 +80,5 @@ TomlTable* toml_walk_table_path(TomlParser* parser, TomlTable* table,
} }
} }
goto end; Return RESULT_VALUE(p, real_table);
error:
real_table = NULL;
//TODO: error handling
assert(false && "TODO error handling");
end:
return real_table;
} }

View File

@ -8,7 +8,7 @@ TomlParser* toml_parser_new(str s, cstr filename)
{ {
TomlParser* parser = malloc(sizeof(TomlParser)); TomlParser* parser = malloc(sizeof(TomlParser));
parser->begin = s.data; parser->begin = s.data;
parser->end = s.data + s.size; parser->end = s.data + s.len;
parser->ptr = s.data; parser->ptr = s.data;
parser->lineno = 1; parser->lineno = 1;
parser->colno = 1; parser->colno = 1;
@ -45,25 +45,7 @@ void toml_next_n(TomlParser* self, i32 n)
} }
} }
str toml_parse_bare_key(TomlParser* self) u32 toml_hex_char_to_int(char ch)
{
cstr s = self->ptr;
u64 len = 0;
while (self->ptr < self->end) {
char ch = *self->ptr;
if (!(isalnum(ch) || ch == '_' || ch == '-'))
break;
len++;
toml_move_next(self);
}
return str_copy(str_construct((void*)s, len, false));
}
char toml_hex_char_to_int(char ch)
{ {
assert(isxdigit(ch)); assert(isxdigit(ch));
if (isdigit(ch)) { if (isdigit(ch)) {
@ -78,7 +60,7 @@ char toml_hex_char_to_int(char ch)
Result(void) toml_encode_unicode_scalar(StringBuilder* sb_ptr, TomlParser* parser, i32 n) Result(void) toml_encode_unicode_scalar(StringBuilder* sb_ptr, TomlParser* parser, i32 n)
{ {
Deferral(16); Deferral(1);
u32 scalar = 0; u32 scalar = 0;
if (parser->ptr + n > parser->end) { if (parser->ptr + n > parser->end) {
@ -90,10 +72,11 @@ Result(void) toml_encode_unicode_scalar(StringBuilder* sb_ptr, TomlParser* parse
for (i32 i = 0; i < n; i++) { for (i32 i = 0; i < n; i++) {
char ch = *parser->ptr; char ch = *parser->ptr;
if (isxdigit(ch)) { if (isxdigit(ch)) {
scalar = scalar * 16 + (u32)toml_hex_char_to_int(ch); scalar = scalar * 16 + toml_hex_char_to_int(ch);
toml_move_next(parser); toml_move_next(parser);
} else { } else {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_UNICODE, "%s:%d:%d: invalid unicode scalar", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_UNICODE,
"%s:%d:%d: invalid unicode scalar",
parser->filename, parser->lineno, parser->colno); parser->filename, parser->lineno, parser->colno);
} }
} }
@ -101,7 +84,8 @@ Result(void) toml_encode_unicode_scalar(StringBuilder* sb_ptr, TomlParser* parse
if ((scalar >= 0xd800 && scalar <= 0xdfff) || if ((scalar >= 0xd800 && scalar <= 0xdfff) ||
(scalar >= 0xfffe && scalar <= 0xffff)) (scalar >= 0xfffe && scalar <= 0xffff))
{ {
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_UNICODE, "%s:%d:%d: invalid unicode scalar", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_UNICODE,
"%s:%d:%d: invalid unicode scalar",
parser->filename, parser->lineno, parser->colno); parser->filename, parser->lineno, parser->colno);
} }
@ -150,6 +134,7 @@ Result(void) toml_encode_unicode_scalar(StringBuilder* sb_ptr, TomlParser* parse
Return RESULT_VOID; Return RESULT_VOID;
} }
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_UNICODE, "%s:%d:%d: invalid unicode scalar", Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_UNICODE,
"%s:%d:%d: invalid unicode scalar",
parser->filename, parser->lineno, parser->colno); parser->filename, parser->lineno, parser->colno);
} }

View File

@ -4,7 +4,7 @@
#include <inttypes.h> #include <inttypes.h>
#include <assert.h> #include <assert.h>
#include "tlibtoml/toml.h" #include "tlibtoml.h"
#ifndef PROJECT_SOURCE_DIR #ifndef PROJECT_SOURCE_DIR
#define PROJECT_SOURCE_DIR ".." #define PROJECT_SOURCE_DIR ".."
@ -38,7 +38,7 @@ void print_value(const TomlValue* value)
print_array(value->value.array); print_array(value->value.array);
break; break;
case TLIBTOML_STRING: case TLIBTOML_STRING:
printf("\"%s\"", value->value.s.data); printf("'" FMT_str "'", value->value.s->len, value->value.s->data);
break; break;
case TLIBTOML_INTEGER: case TLIBTOML_INTEGER:
printf("%" PRId64, value->value.i); printf("%" PRId64, value->value.i);
@ -80,14 +80,12 @@ void print_table(const TomlTable* table)
Result(void) test_run(cstr filename) Result(void) test_run(cstr filename)
{ {
Deferral(4); Deferral(1);
try(TomlTable* table, p, toml_load_filename(filename)); try(TomlTable* table, p, toml_load_filename(filename));
Defer(TomlTable_free(table)); Defer(TomlTable_free(table));
print_table(table); print_table(table);
printf("\n"); printf("\n");
Return RESULT_VOID;
cleanup:
;
} }
i32 main(void) i32 main(void)
@ -95,6 +93,8 @@ i32 main(void)
Deferral(32); Deferral(32);
try_fatal_void(tlibc_init()); try_fatal_void(tlibc_init());
Defer(tlibc_deinit()); Defer(tlibc_deinit());
try_fatal_void(tlibtoml_init());
Defer(tlibtoml_deinit());
static cstr const filenames[] = { static cstr const filenames[] = {
/* should parse */ /* should parse */
@ -131,5 +131,5 @@ i32 main(void)
printf("total %d tests, %d passed, %d failed\n", printf("total %d tests, %d passed, %d failed\n",
total_tests, num_passed, num_failed); total_tests, num_passed, num_failed);
return num_failed; Return num_failed;
} }