Compare commits
3 Commits
52870920c1
...
281a65f0d1
| Author | SHA1 | Date | |
|---|---|---|---|
| 281a65f0d1 | |||
| 2082d56c25 | |||
| 048542d079 |
1
.vscode/c_cpp_properties.json
vendored
1
.vscode/c_cpp_properties.json
vendored
@ -4,6 +4,7 @@
|
|||||||
"name": "all",
|
"name": "all",
|
||||||
"defines": [],
|
"defines": [],
|
||||||
"includePath": [
|
"includePath": [
|
||||||
|
"src",
|
||||||
"include",
|
"include",
|
||||||
"../tlibc/include",
|
"../tlibc/include",
|
||||||
"${default}"
|
"${default}"
|
||||||
|
|||||||
29
README.md
29
README.md
@ -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.git).
|
|
||||||
3. Clone [tlibc](https://timerix.ddns.net/git/Timerix/tlibtoml). 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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
149
include/tlibtoml.h
Normal file
149
include/tlibtoml.h
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
/* 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/. */
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
#include "tlibc/tlibc.h"
|
||||||
|
#include "tlibc/time.h"
|
||||||
|
#include "tlibc/errors.h"
|
||||||
|
#include "tlibc/string/str.h"
|
||||||
|
#include "tlibc/collections/HashMap.h"
|
||||||
|
#include "tlibc/collections/List.h"
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
// //
|
||||||
|
// tlibtoml //
|
||||||
|
// //
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
Result(void) tlibtoml_init();
|
||||||
|
void tlibtoml_deinit();
|
||||||
|
|
||||||
|
typedef enum TlibtomlError {
|
||||||
|
TLIBTOML_OK,
|
||||||
|
TLIBTOML_ERR,
|
||||||
|
TLIBTOML_ERR_OS,
|
||||||
|
TLIBTOML_ERR_NOMEM,
|
||||||
|
TLIBTOML_ERR_SYNTAX,
|
||||||
|
TLIBTOML_ERR_UNICODE
|
||||||
|
} TlibtomlError;
|
||||||
|
ErrorCodePage_declare(TLIBTOML);
|
||||||
|
|
||||||
|
|
||||||
|
typedef DateTime TomlDateTime;
|
||||||
|
typedef struct HashMap(TomlValue) HashMap(TomlValue);
|
||||||
|
typedef HashMap(TomlValue) TomlTable;
|
||||||
|
typedef struct List(TomlValue) List(TomlValue);
|
||||||
|
typedef List(TomlValue) TomlArray;
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
// //
|
||||||
|
// Parser //
|
||||||
|
// //
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
Result(TomlTable*) toml_load_str(str s);
|
||||||
|
Result(TomlTable*) toml_load_file(FILE* file);
|
||||||
|
Result(TomlTable*) toml_load_filename(cstr filename);
|
||||||
|
|
||||||
|
/* TODO: implement dump functions
|
||||||
|
str toml_dump_str(const TomlTable* self, TomlErr *err);
|
||||||
|
void toml_dump_file(const TomlTable* self, FILE* file, TomlErr *err);
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
// //
|
||||||
|
// TomlValue //
|
||||||
|
// //
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
typedef enum TomlType {
|
||||||
|
TLIBTOML_INVALID_TYPE,
|
||||||
|
TLIBTOML_TABLE,
|
||||||
|
TLIBTOML_ARRAY,
|
||||||
|
TLIBTOML_STRING,
|
||||||
|
TLIBTOML_INTEGER,
|
||||||
|
TLIBTOML_FLOAT,
|
||||||
|
TLIBTOML_DATETIME,
|
||||||
|
TLIBTOML_BOOLEAN,
|
||||||
|
} TomlType;
|
||||||
|
|
||||||
|
typedef struct TomlValue {
|
||||||
|
TomlType type;
|
||||||
|
union {
|
||||||
|
i64 i;
|
||||||
|
f64 f;
|
||||||
|
bool b;
|
||||||
|
str* s;
|
||||||
|
TomlArray* array;
|
||||||
|
TomlTable* table;
|
||||||
|
TomlDateTime* dt;
|
||||||
|
} value;
|
||||||
|
} TomlValue;
|
||||||
|
|
||||||
|
List_declare(TomlValue);
|
||||||
|
|
||||||
|
TomlValue TomlValue_new(TomlType type);
|
||||||
|
TomlValue TomlValue_new_table(void);
|
||||||
|
TomlValue TomlValue_new_array(void);
|
||||||
|
TomlValue TomlValue_new_integer(i64 integer);
|
||||||
|
TomlValue TomlValue_new_float(f64 flt);
|
||||||
|
TomlValue TomlValue_new_datetime(void);
|
||||||
|
TomlValue TomlValue_new_bool(bool b);
|
||||||
|
/// copies the string
|
||||||
|
TomlValue TomlValue_copy_str(str s);
|
||||||
|
/// doesnt copy the string
|
||||||
|
TomlValue TomlValue_move_str(str s);
|
||||||
|
void TomlValue_destroy(TomlValue* self);
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
// //
|
||||||
|
// TomlTable //
|
||||||
|
// //
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
TomlTable* TomlTable_new(void);
|
||||||
|
void TomlTable_free(TomlTable* self);
|
||||||
|
|
||||||
|
static inline void TomlTable_set(TomlTable* self, str key, TomlValue value){
|
||||||
|
HashMap_pushOrUpdate(self, key, &value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline TomlValue* TomlTable_get(const TomlTable* self, const str key){
|
||||||
|
return HashMap_tryGetPtr(self, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result(TomlTable*) TomlTable_get_table(const TomlTable* self, str key);
|
||||||
|
Result(TomlArray*) TomlTable_get_array(const TomlTable* self, str key);
|
||||||
|
Result(str*) TomlTable_get_str(const TomlTable* self, str key);
|
||||||
|
Result(i64) TomlTable_get_integer(const TomlTable* self, str key);
|
||||||
|
Result(f64) TomlTable_get_float(const TomlTable* self, str key);
|
||||||
|
Result(bool) TomlTable_get_bool(const TomlTable* self, str key);
|
||||||
|
Result(TomlDateTime*) TomlTable_get_datetime(const TomlTable* self, str key);
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
// //
|
||||||
|
// TomlArray //
|
||||||
|
// //
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
TomlArray* TomlArray_new(void);
|
||||||
|
void TomlArray_free(TomlArray* self);
|
||||||
|
|
||||||
|
static inline void TomlArray_append(TomlArray* self, TomlValue value){
|
||||||
|
List_TomlValue_push(self, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@ -1,109 +0,0 @@
|
|||||||
/* 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/. */
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <time.h>
|
|
||||||
#include "tlibc/std.h"
|
|
||||||
#include "tlibc/string/str.h"
|
|
||||||
#include "tlibc/collections/HashMap.h"
|
|
||||||
|
|
||||||
typedef struct tm TomlDateTime;
|
|
||||||
typedef HashMap(TomlValue) TomlTable;
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
TOML_OK,
|
|
||||||
TOML_ERR,
|
|
||||||
TOML_ERR_OS,
|
|
||||||
TOML_ERR_NOMEM,
|
|
||||||
TOML_ERR_SYNTAX,
|
|
||||||
TOML_ERR_UNICODE
|
|
||||||
} TomlErrCode;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
TomlErrCode code;
|
|
||||||
NULLABLE(char*) message;
|
|
||||||
} TomlErr;
|
|
||||||
|
|
||||||
typedef struct TomlValue TomlValue;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
TomlValue* elements;
|
|
||||||
u64 len;
|
|
||||||
u64 _capacity;
|
|
||||||
} TomlArray;
|
|
||||||
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
TOML_INVALID_TYPE,
|
|
||||||
TOML_TABLE,
|
|
||||||
TOML_ARRAY,
|
|
||||||
TOML_STRING,
|
|
||||||
TOML_INTEGER,
|
|
||||||
TOML_FLOAT,
|
|
||||||
TOML_DATETIME,
|
|
||||||
TOML_BOOLEAN,
|
|
||||||
} TomlType;
|
|
||||||
|
|
||||||
struct TomlValue {
|
|
||||||
TomlType type;
|
|
||||||
union {
|
|
||||||
TomlTable* table;
|
|
||||||
TomlArray* array;
|
|
||||||
str s;
|
|
||||||
i64 i;
|
|
||||||
f64 f;
|
|
||||||
TomlDateTime* dt;
|
|
||||||
bool b;
|
|
||||||
} value;
|
|
||||||
};
|
|
||||||
|
|
||||||
const TomlErr* toml_err(void);
|
|
||||||
void toml_err_clear(void);
|
|
||||||
|
|
||||||
|
|
||||||
TomlTable* toml_table_new(void);
|
|
||||||
void toml_table_free(TomlTable* self);
|
|
||||||
|
|
||||||
void toml_table_set(TomlTable* self, str key, TomlValue value);
|
|
||||||
TomlValue* toml_table_get(const TomlTable* self, str key);
|
|
||||||
TomlTable* toml_table_get_table(const TomlTable* self, str key);
|
|
||||||
TomlArray* toml_table_get_array(const TomlTable* self, str key);
|
|
||||||
str toml_table_get_str(const TomlTable* self, str key);
|
|
||||||
i64 toml_table_get_integer(const TomlTable* self, str key);
|
|
||||||
f64 toml_table_get_float(const TomlTable* self, str key);
|
|
||||||
TomlDateTime* toml_table_get_datetime(const TomlTable* self, str key);
|
|
||||||
bool toml_table_get_bool(const TomlTable* self, str key);
|
|
||||||
|
|
||||||
TomlArray* toml_array_new(void);
|
|
||||||
void toml_array_free(TomlArray* self);
|
|
||||||
void toml_array_append(TomlArray* self, TomlValue value);
|
|
||||||
|
|
||||||
TomlValue TomlValue_new(TomlType type);
|
|
||||||
TomlValue TomlValue_new_string(TomlType type);
|
|
||||||
TomlValue TomlValue_new_table(void);
|
|
||||||
TomlValue TomlValue_new_array(void);
|
|
||||||
TomlValue TomlValue_new_integer(i64 integer);
|
|
||||||
TomlValue TomlValue_new_float(f64 flt);
|
|
||||||
TomlValue TomlValue_new_datetime(void);
|
|
||||||
TomlValue TomlValue_new_bool(bool b);
|
|
||||||
TomlValue TomlValue_from_str(str s);
|
|
||||||
void TomlValue_destroy(TomlValue* self);
|
|
||||||
|
|
||||||
TomlTable* toml_load_str(str s);
|
|
||||||
TomlTable* toml_load_file(FILE* file);
|
|
||||||
TomlTable* toml_load_filename(cstr filename);
|
|
||||||
|
|
||||||
/* TODO: implement dump functions
|
|
||||||
str toml_dump_str(const TomlTable* self, TomlErr *err);
|
|
||||||
void toml_dump_file(const TomlTable* self, FILE* file, TomlErr *err);
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
@ -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"
|
||||||
@ -37,7 +37,7 @@ OBJDIR="obj"
|
|||||||
OUTDIR="bin"
|
OUTDIR="bin"
|
||||||
STATIC_LIB_FILE="$PROJECT.a"
|
STATIC_LIB_FILE="$PROJECT.a"
|
||||||
|
|
||||||
INCLUDE="-I./include -I../tlibc/include"
|
INCLUDE="-Isrc -Iinclude -I../tlibc/include"
|
||||||
|
|
||||||
# OS-specific options
|
# OS-specific options
|
||||||
case "$OS" in
|
case "$OS" in
|
||||||
|
|||||||
20
src/TomlArray.c
Normal file
20
src/TomlArray.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/* 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"
|
||||||
|
|
||||||
|
TomlArray* TomlArray_new(void)
|
||||||
|
{
|
||||||
|
TomlArray* array = (TomlArray*)malloc(sizeof(TomlArray));
|
||||||
|
*array = List_TomlValue_construct(NULL, 0, 0);
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TomlArray_free(TomlArray* self)
|
||||||
|
{
|
||||||
|
if(!self)
|
||||||
|
return;
|
||||||
|
List_TomlValue_destroy(self);
|
||||||
|
free(self);
|
||||||
|
}
|
||||||
83
src/TomlTable.c
Normal file
83
src/TomlTable.c
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
/* 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"
|
||||||
|
|
||||||
|
TomlTable* TomlTable_new(void)
|
||||||
|
{
|
||||||
|
TomlTable* table = (TomlTable*)malloc(sizeof(TomlTable));
|
||||||
|
HashMap_construct(table, TomlValue, (Destructor_t)TomlValue_destroy);
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TomlTable_free(TomlTable* self)
|
||||||
|
{
|
||||||
|
if(!self)
|
||||||
|
return;
|
||||||
|
HashMap_destroy(self);
|
||||||
|
free(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result(TomlTable*) TomlTable_get_table(const TomlTable* self, str key)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
TomlValue* v = TomlTable_get(self, key);
|
||||||
|
try_assert(v != NULL);
|
||||||
|
try_assert(v->type == TLIBTOML_TABLE);
|
||||||
|
Return RESULT_VALUE(p, v->value.table);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result(TomlArray*) TomlTable_get_array(const TomlTable* self, str key)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
TomlValue* v = TomlTable_get(self, key);
|
||||||
|
try_assert(v != NULL);
|
||||||
|
try_assert(v->type == TLIBTOML_ARRAY);
|
||||||
|
Return RESULT_VALUE(p, v->value.array);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result(str*) TomlTable_get_str(const TomlTable* self, str key)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
TomlValue* v = TomlTable_get(self, key);
|
||||||
|
try_assert(v != NULL);
|
||||||
|
try_assert(v->type == TLIBTOML_STRING);
|
||||||
|
Return RESULT_VALUE(p, v->value.s);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result(i64) TomlTable_get_integer(const TomlTable* self, str key)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
TomlValue* v = TomlTable_get(self, key);
|
||||||
|
try_assert(v != NULL);
|
||||||
|
try_assert(v->type == TLIBTOML_INTEGER);
|
||||||
|
Return RESULT_VALUE(i, v->value.i);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result(f64) TomlTable_get_float(const TomlTable* self, str key)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
TomlValue* v = TomlTable_get(self, key);
|
||||||
|
try_assert(v != NULL);
|
||||||
|
try_assert(v->type == TLIBTOML_FLOAT);
|
||||||
|
Return RESULT_VALUE(f, v->value.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result(TomlDateTime*) TomlTable_get_datetime(const TomlTable* self, str key)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
TomlValue* v = TomlTable_get(self, key);
|
||||||
|
try_assert(v != NULL);
|
||||||
|
try_assert(v->type == TLIBTOML_DATETIME);
|
||||||
|
Return RESULT_VALUE(p, v->value.dt);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result(bool) TomlTable_get_bool(const TomlTable* self, str key)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
TomlValue* v = TomlTable_get(self, key);
|
||||||
|
try_assert(v != NULL);
|
||||||
|
try_assert(v->type == TLIBTOML_BOOLEAN);
|
||||||
|
Return RESULT_VALUE(i, v->value.b);
|
||||||
|
}
|
||||||
120
src/TomlValue.c
Normal file
120
src/TomlValue.c
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
/* 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"
|
||||||
|
|
||||||
|
TomlValue TomlValue_new(TomlType type)
|
||||||
|
{
|
||||||
|
TomlValue value = {0};
|
||||||
|
value.type = type;
|
||||||
|
switch (type) {
|
||||||
|
default:
|
||||||
|
assert(false && "invalid type");
|
||||||
|
break;
|
||||||
|
case TLIBTOML_TABLE:
|
||||||
|
value.value.table = NULL;
|
||||||
|
break;
|
||||||
|
case TLIBTOML_ARRAY:
|
||||||
|
value.value.array = NULL;
|
||||||
|
break;
|
||||||
|
case TLIBTOML_STRING:
|
||||||
|
value.value.s = NULL;
|
||||||
|
break;
|
||||||
|
case TLIBTOML_INTEGER:
|
||||||
|
value.value.i = 0;
|
||||||
|
break;
|
||||||
|
case TLIBTOML_FLOAT:
|
||||||
|
value.value.f = 0.0;
|
||||||
|
break;
|
||||||
|
case TLIBTOML_BOOLEAN:
|
||||||
|
value.value.b = false;
|
||||||
|
break;
|
||||||
|
case TLIBTOML_DATETIME:
|
||||||
|
value.value.dt = (TomlDateTime*)malloc(sizeof(TomlDateTime));
|
||||||
|
memset(value.value.dt, 0, sizeof(TomlDateTime));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
TomlValue TomlValue_copy_str(str s)
|
||||||
|
{
|
||||||
|
return TomlValue_move_str(str_copy(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
TomlValue TomlValue_move_str(str s){
|
||||||
|
TomlValue value = {0};
|
||||||
|
value.value.s = (str*)malloc(sizeof(str));
|
||||||
|
*value.value.s = s;
|
||||||
|
value.type = TLIBTOML_STRING;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
TomlValue TomlValue_new_table(void)
|
||||||
|
{
|
||||||
|
TomlValue value = {0};
|
||||||
|
value.value.table = TomlTable_new();
|
||||||
|
value.type = TLIBTOML_TABLE;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
TomlValue TomlValue_new_array(void)
|
||||||
|
{
|
||||||
|
TomlValue value = {0};
|
||||||
|
value.value.array = TomlArray_new();
|
||||||
|
value.type = TLIBTOML_ARRAY;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
TomlValue TomlValue_new_integer(i64 integer)
|
||||||
|
{
|
||||||
|
TomlValue value = {0};
|
||||||
|
value.value.i = integer;
|
||||||
|
value.type = TLIBTOML_INTEGER;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
TomlValue TomlValue_new_float(f64 float_)
|
||||||
|
{
|
||||||
|
TomlValue value = {0};
|
||||||
|
value.value.f = float_;
|
||||||
|
value.type = TLIBTOML_FLOAT;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
TomlValue TomlValue_new_datetime(void)
|
||||||
|
{
|
||||||
|
return TomlValue_new(TLIBTOML_BOOLEAN);
|
||||||
|
}
|
||||||
|
|
||||||
|
TomlValue TomlValue_new_bool(bool b)
|
||||||
|
{
|
||||||
|
TomlValue value = {0};
|
||||||
|
value.value.b = b;
|
||||||
|
value.type = TLIBTOML_BOOLEAN;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TomlValue_destroy(TomlValue* self)
|
||||||
|
{
|
||||||
|
if (!self)
|
||||||
|
return;
|
||||||
|
|
||||||
|
switch (self->type) {
|
||||||
|
case TLIBTOML_STRING:
|
||||||
|
str_free(self->value.s);
|
||||||
|
break;
|
||||||
|
case TLIBTOML_TABLE:
|
||||||
|
TomlTable_free(self->value.table);
|
||||||
|
break;
|
||||||
|
case TLIBTOML_ARRAY:
|
||||||
|
TomlArray_free(self->value.array);
|
||||||
|
break;
|
||||||
|
case TLIBTOML_DATETIME:
|
||||||
|
free(self->value.dt);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/tlibtoml.c
Normal file
15
src/tlibtoml.c
Normal 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(){
|
||||||
|
|
||||||
|
}
|
||||||
1421
src/toml.c
1421
src/toml.c
File diff suppressed because it is too large
Load Diff
55
src/toml_internal.h
Normal file
55
src/toml_internal.h
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
/* 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/. */
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "tlibtoml.h"
|
||||||
|
#include "tlibc/string/StringBuilder.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
typedef struct _TomlParser {
|
||||||
|
cstr begin;
|
||||||
|
cstr end;
|
||||||
|
cstr ptr;
|
||||||
|
i32 lineno;
|
||||||
|
i32 colno;
|
||||||
|
cstr filename;
|
||||||
|
} TomlParser;
|
||||||
|
|
||||||
|
TomlParser* toml_parser_new(str s, cstr filename);
|
||||||
|
void toml_parser_free(TomlParser* self);
|
||||||
|
|
||||||
|
void toml_move_next(TomlParser* self);
|
||||||
|
void toml_next_n(TomlParser* self, i32 n);
|
||||||
|
u32 toml_hex_char_to_int(char ch);
|
||||||
|
Result(void) toml_encode_unicode_scalar(StringBuilder* sb_ptr, TomlParser* parser, i32 n);
|
||||||
|
|
||||||
|
Result(void) toml_parse_bare_key(TomlParser* self, str* out_str);
|
||||||
|
Result(void) toml_parse_basic_string(TomlParser* self, str* out_str);
|
||||||
|
Result(void) toml_parse_literal_string(TomlParser* self, str* out_str);
|
||||||
|
Result(void) toml_parse_basic_string_value(TomlParser* self, TomlValue* out_value);
|
||||||
|
Result(void) toml_parse_literal_string_value(TomlParser* self, TomlValue* out_value);
|
||||||
|
Result(void) toml_parse_multi_line_basic_string(TomlParser* self, TomlValue* out_value);
|
||||||
|
Result(void) toml_parse_multi_line_literal_string(TomlParser* self, TomlValue* out_value);
|
||||||
|
Result(void) toml_parse_datetime(str s, TomlValue* out_value);
|
||||||
|
Result(void) toml_parse_int_or_float_or_time(TomlParser* self, TomlValue* out_value);
|
||||||
|
Result(void) toml_parse_bool(TomlParser* self, TomlValue* out_value);
|
||||||
|
Result(void) toml_parse_value(TomlParser* self, TomlValue* out_value);
|
||||||
|
Result(void) toml_parse_array(TomlParser* self, TomlValue* out_value);
|
||||||
|
Result(void) toml_parse_inline_table(TomlParser* self, TomlValue* out_value);
|
||||||
|
Result(void) toml_parse_table(TomlParser* self, TomlTable* target_table);
|
||||||
|
Result(void) toml_parse_key_value(TomlParser* self, TomlTable* target_table);
|
||||||
|
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
|
||||||
|
}
|
||||||
|
#endif
|
||||||
68
src/toml_load.c
Normal file
68
src/toml_load.c
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/* 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"
|
||||||
|
#include "tlibc/filesystem.h"
|
||||||
|
|
||||||
|
Result(TomlTable*) toml_load_str_filename(str s, cstr filename)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
|
||||||
|
TomlParser* parser = toml_parser_new(s, filename);
|
||||||
|
Defer(toml_parser_free(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);
|
||||||
|
}
|
||||||
|
|
||||||
|
Result(TomlTable*) toml_load_str(str s)
|
||||||
|
{
|
||||||
|
return toml_load_str_filename(s, "<string>");
|
||||||
|
}
|
||||||
|
|
||||||
|
Result(TomlTable*) toml_load_file(FILE* file)
|
||||||
|
{
|
||||||
|
return toml_load_file_filename(file, "<stream>");
|
||||||
|
}
|
||||||
|
|
||||||
|
Result(TomlTable*) toml_load_filename(cstr filename)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
|
||||||
|
try(FILE* f, p, file_open(filename, FO_ReadExisting));
|
||||||
|
try(TomlTable* table, p, toml_load_file_filename(f, filename));
|
||||||
|
|
||||||
|
Return RESULT_VALUE(p, table);
|
||||||
|
}
|
||||||
45
src/toml_parse/toml_parse.c
Normal file
45
src/toml_parse/toml_parse.c
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/* 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(TomlTable*) toml_parse(TomlParser* self)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
TomlTable* table = TomlTable_new();
|
||||||
|
bool success = false;
|
||||||
|
Defer(if(!success) TomlTable_free(table));
|
||||||
|
|
||||||
|
while (self->ptr < self->end) {
|
||||||
|
char ch = *self->ptr;
|
||||||
|
|
||||||
|
while (self->ptr < self->end && isspace(ch)) {
|
||||||
|
toml_move_next(self);
|
||||||
|
ch = *self->ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ch == '#') {
|
||||||
|
do {
|
||||||
|
toml_move_next(self);
|
||||||
|
ch = *self->ptr;
|
||||||
|
} while (self->ptr < self->end && ch != '\n');
|
||||||
|
toml_move_next(self);
|
||||||
|
} else if (ch == '[') {
|
||||||
|
toml_move_next(self);
|
||||||
|
try_void(toml_parse_table(self, table));
|
||||||
|
} else if (isalnum(ch) || ch == '_' || ch == '-') {
|
||||||
|
try_void(toml_parse_key_value(self, table));
|
||||||
|
} else if (ch == ' ' || ch == '\t' || ch == '\r') {
|
||||||
|
do {
|
||||||
|
toml_move_next(self);
|
||||||
|
ch = *self->ptr;
|
||||||
|
} while (ch == ' ' || ch == '\t' || ch == '\r');
|
||||||
|
} else if (ch == '\n') {
|
||||||
|
toml_move_next(self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
success = true;
|
||||||
|
Return RESULT_VALUE(p, table);
|
||||||
|
}
|
||||||
58
src/toml_parse/toml_parse_array.c
Normal file
58
src/toml_parse/toml_parse_array.c
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
/* 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_array(TomlParser* self, TomlValue* out_value)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
TomlValue array_value = TomlValue_new_array();
|
||||||
|
bool success = false;
|
||||||
|
Defer(if(!success) TomlValue_destroy(&array_value));
|
||||||
|
|
||||||
|
while (self->ptr < self->end) {
|
||||||
|
if (isspace(*self->ptr)) {
|
||||||
|
while (self->ptr < self->end && isspace(*self->ptr)) {
|
||||||
|
toml_move_next(self);
|
||||||
|
}
|
||||||
|
} else if (*self->ptr == '#') {
|
||||||
|
do {
|
||||||
|
toml_move_next(self);
|
||||||
|
} while (self->ptr < self->end &&* self->ptr != '\n');
|
||||||
|
toml_move_next(self);
|
||||||
|
} else if (*self->ptr == '\n') {
|
||||||
|
toml_move_next(self);
|
||||||
|
} else if (*self->ptr == ']') {
|
||||||
|
toml_move_next(self);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
TomlValue value;
|
||||||
|
try_void(toml_parse_value(self, &value));
|
||||||
|
|
||||||
|
TomlArray_append(array_value.value.array, value);
|
||||||
|
|
||||||
|
while (self->ptr < self->end) {
|
||||||
|
if (isspace(*self->ptr)) {
|
||||||
|
do {
|
||||||
|
toml_move_next(self);
|
||||||
|
} while (self->ptr < self->end && isspace(*self->ptr));
|
||||||
|
} else if (*self->ptr == '#') {
|
||||||
|
do {
|
||||||
|
toml_move_next(self);
|
||||||
|
} while (self->ptr < self->end &&* self->ptr != '\n');
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self->ptr < self->end &&* self->ptr == ',') {
|
||||||
|
toml_move_next(self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
success = true;
|
||||||
|
*out_value = array_value;
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
26
src/toml_parse/toml_parse_bare_key.c
Normal file
26
src/toml_parse/toml_parse_bare_key.c
Normal 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;
|
||||||
|
}
|
||||||
71
src/toml_parse/toml_parse_basic_string.c
Normal file
71
src/toml_parse/toml_parse_basic_string.c
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
/* 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_basic_string(TomlParser* self, str* out_str)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
StringBuilder sb = StringBuilder_alloc(32);
|
||||||
|
bool success = false;
|
||||||
|
Defer(if(!success) StringBuilder_destroy(&sb));
|
||||||
|
|
||||||
|
while (self->ptr < self->end &&* self->ptr != '\"' &&* self->ptr != '\n') {
|
||||||
|
char ch1 = *self->ptr;
|
||||||
|
if (ch1 == '\\') {
|
||||||
|
if (self->ptr >= self->end) break;
|
||||||
|
|
||||||
|
toml_move_next(self);
|
||||||
|
char ch2 = *self->ptr;
|
||||||
|
|
||||||
|
if (ch2 == '\"') {
|
||||||
|
StringBuilder_append_char(&sb, '\"');
|
||||||
|
toml_move_next(self);
|
||||||
|
} else if (ch2 == 'b') {
|
||||||
|
StringBuilder_append_char(&sb, '\b');
|
||||||
|
toml_move_next(self);
|
||||||
|
} else if (ch2 == 't') {
|
||||||
|
StringBuilder_append_char(&sb, '\t');
|
||||||
|
toml_move_next(self);
|
||||||
|
} else if (ch2 == 'n') {
|
||||||
|
StringBuilder_append_char(&sb, '\n');
|
||||||
|
toml_move_next(self);
|
||||||
|
} else if (ch2 == 'f') {
|
||||||
|
StringBuilder_append_char(&sb, '\f');
|
||||||
|
toml_move_next(self);
|
||||||
|
} else if (ch2 == 'r') {
|
||||||
|
StringBuilder_append_char(&sb, '\r');
|
||||||
|
toml_move_next(self);
|
||||||
|
} else if (ch2 == '\\') {
|
||||||
|
StringBuilder_append_char(&sb, '\\');
|
||||||
|
toml_move_next(self);
|
||||||
|
} else if (ch2 == 'u') {
|
||||||
|
toml_move_next(self);
|
||||||
|
try_void(toml_encode_unicode_scalar(&sb, self, 4));
|
||||||
|
} else if (ch2 == 'U') {
|
||||||
|
toml_move_next(self);
|
||||||
|
try_void(toml_encode_unicode_scalar(&sb, self, 8));
|
||||||
|
} else {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: invalid escape character",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
StringBuilder_append_char(&sb, ch1);
|
||||||
|
toml_move_next(self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
toml_move_next(self);
|
||||||
|
|
||||||
|
*out_str = StringBuilder_getStr(&sb);
|
||||||
|
success = true;
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
16
src/toml_parse/toml_parse_basic_string_value.c
Normal file
16
src/toml_parse/toml_parse_basic_string_value.c
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
/* 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_basic_string_value(TomlParser* self, TomlValue* out_value)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
|
||||||
|
str s;
|
||||||
|
try_void(toml_parse_basic_string(self, &s));
|
||||||
|
*out_value = TomlValue_move_str(s);
|
||||||
|
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
44
src/toml_parse/toml_parse_bool.c
Normal file
44
src/toml_parse/toml_parse_bool.c
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
/* 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"
|
||||||
|
|
||||||
|
static bool is_value_end(cstr expected_value_end, cstr input_end){
|
||||||
|
if(expected_value_end == input_end)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
char next_ch = *expected_value_end;
|
||||||
|
return isspace(next_ch) || next_ch == ',' || next_ch == ']' || next_ch == '}';
|
||||||
|
}
|
||||||
|
|
||||||
|
Result(void) toml_parse_bool(TomlParser* self, TomlValue* out_value)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
|
||||||
|
cstr val_end = self->ptr + 4;
|
||||||
|
if(val_end <= self->end && strncmp(self->ptr, "true", 4) == 0)
|
||||||
|
{
|
||||||
|
if(is_value_end(val_end, self->end))
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
16
src/toml_parse/toml_parse_datetime.c
Normal file
16
src/toml_parse/toml_parse_datetime.c
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
/* 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_datetime(str s, TomlValue* out_value)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
(void)s;
|
||||||
|
//TODO: parse datetime
|
||||||
|
try_assert(false && "DateTime parsing is not implemented");
|
||||||
|
TomlValue value = TomlValue_new(TLIBTOML_DATETIME);
|
||||||
|
*out_value = value;
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
92
src/toml_parse/toml_parse_inline_table.c
Normal file
92
src/toml_parse/toml_parse_inline_table.c
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
/* 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_inline_table(TomlParser* self, TomlValue* out_value)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
|
||||||
|
TomlValue table_value = TomlValue_new_table();
|
||||||
|
bool success = false;
|
||||||
|
Defer(if(!success) TomlValue_destroy(&table_value));
|
||||||
|
|
||||||
|
while (self->ptr < self->end) {
|
||||||
|
char ch = *self->ptr;
|
||||||
|
while (self->ptr < self->end && (ch == ' ' || ch == '\t')) {
|
||||||
|
toml_move_next(self);
|
||||||
|
ch = *self->ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
str key;
|
||||||
|
if (isalnum(ch) || ch == '_') {
|
||||||
|
try_void(toml_parse_bare_key(self, &key));
|
||||||
|
} else if (ch == '\"') {
|
||||||
|
toml_move_next(self);
|
||||||
|
try_void(toml_parse_basic_string(self, &key));
|
||||||
|
} else if (ch == '\'') {
|
||||||
|
toml_move_next(self);
|
||||||
|
try_void(toml_parse_literal_string(self, &key));
|
||||||
|
} else if (ch == '}') {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: unexpected token",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
ch = *self->ptr;
|
||||||
|
while (self->ptr < self->end && (ch == ' ' || ch == '\t')) {
|
||||||
|
toml_move_next(self);
|
||||||
|
ch = *self->ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self->ptr == self->end) {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: unterminated key value pair",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ch != '=') {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: unexpected token",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
toml_move_next(self);
|
||||||
|
|
||||||
|
ch = *self->ptr;
|
||||||
|
while (self->ptr < self->end && (ch == ' ' || ch == '\t')) {
|
||||||
|
toml_move_next(self);
|
||||||
|
ch = *self->ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self->ptr == self->end) {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: unterminated key value pair",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
TomlValue value;
|
||||||
|
try_void(toml_parse_value(self, &value));
|
||||||
|
|
||||||
|
TomlTable_set(table_value.value.table, key, value);
|
||||||
|
str_destroy(key);
|
||||||
|
|
||||||
|
while (self->ptr < self->end && (*self->ptr == ' ' ||* self->ptr == '\t')) {
|
||||||
|
toml_move_next(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*self->ptr == ',') {
|
||||||
|
toml_move_next(self);
|
||||||
|
} else if (*self->ptr == '}') {
|
||||||
|
toml_move_next(self);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
success = true;
|
||||||
|
*out_value = table_value;
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
132
src/toml_parse/toml_parse_int_or_float_or_time.c
Normal file
132
src/toml_parse/toml_parse_int_or_float_or_time.c
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
/* 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_int_or_float_or_time(TomlParser* self, TomlValue* out_value)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
|
||||||
|
StringBuilder sb = StringBuilder_alloc(32);
|
||||||
|
Defer(StringBuilder_destroy(&sb));
|
||||||
|
|
||||||
|
TomlValue value = {0};
|
||||||
|
char type = 'i';
|
||||||
|
i32 base = 10;
|
||||||
|
|
||||||
|
// 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
|
||||||
|
if (self->ptr + 3 <= self->end &&
|
||||||
|
(strncmp(self->ptr, "nan", 3) == 0 || strncmp(self->ptr, "inf", 3) == 0)) {
|
||||||
|
type = 'f';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self->ptr + 4 <= self->end &&
|
||||||
|
(strncmp(self->ptr, "+nan", 4) == 0 ||
|
||||||
|
strncmp(self->ptr, "-nan", 4) == 0 ||
|
||||||
|
strncmp(self->ptr, "+inf", 4) == 0 ||
|
||||||
|
strncmp(self->ptr, "-inf", 4) == 0))
|
||||||
|
{
|
||||||
|
type = 'f';
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there is a base prefix, set the base and strip the prefix,
|
||||||
|
// because strtoll() do not recognize 0o and 0b
|
||||||
|
if (self->ptr + 2 <= self->end) {
|
||||||
|
if (strncmp(self->ptr, "0x", 2) == 0) {
|
||||||
|
base = 16;
|
||||||
|
toml_next_n(self, 2);
|
||||||
|
} else if (strncmp(self->ptr, "0o", 2) == 0) {
|
||||||
|
base = 8;
|
||||||
|
toml_next_n(self, 2);
|
||||||
|
} else if (strncmp(self->ptr, "0b", 2) == 0) {
|
||||||
|
base = 2;
|
||||||
|
toml_next_n(self, 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char last_char = 0;
|
||||||
|
i32 has_exp = false;
|
||||||
|
while (self->ptr < self->end) {
|
||||||
|
if (*self->ptr == '+' ||* self->ptr == '-') {
|
||||||
|
if (last_char == 0 || ((last_char == 'e' || last_char == 'E') && !has_exp)) {
|
||||||
|
if (last_char != 0) {
|
||||||
|
type = 'f';
|
||||||
|
has_exp = true;
|
||||||
|
}
|
||||||
|
StringBuilder_append_char(&sb,* self->ptr);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (isalnum(*self->ptr)) {
|
||||||
|
if ((*self->ptr == 'e' ||* self->ptr == 'E') && base == 10) {
|
||||||
|
type = 'f';
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder_append_char(&sb,* self->ptr);
|
||||||
|
} else if (*self->ptr == '.') {
|
||||||
|
if (type == 'i') {
|
||||||
|
type = 'f';
|
||||||
|
StringBuilder_append_char(&sb,* self->ptr);
|
||||||
|
} else {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: invalid float",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
} else if (*self->ptr == '_') {
|
||||||
|
if (type == 't') {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: invalid datetime",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isalnum(last_char)) {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: invalid integer or float",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
} else if (*self->ptr == '-') {
|
||||||
|
type = 't';
|
||||||
|
StringBuilder_append_char(&sb,* self->ptr);
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
last_char = *self->ptr;
|
||||||
|
toml_move_next(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (last_char == '_') {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: invalid integer or float or datetime",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == 'i') {
|
||||||
|
char* end = NULL;
|
||||||
|
char* start = StringBuilder_getStr(&sb).data;
|
||||||
|
i64 n = strtoll(start, &end, base);
|
||||||
|
if (end < start + sb.buffer.len) {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: invalid integer",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
value = TomlValue_new_integer(n);
|
||||||
|
} else if (type == 'f') {
|
||||||
|
char* end = NULL;
|
||||||
|
char* start = StringBuilder_getStr(&sb).data;
|
||||||
|
f64 n = strtod(start, &end);
|
||||||
|
if (end < start + sb.buffer.len) {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: invalid float",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
value = TomlValue_new_float(n);
|
||||||
|
} else if (type == 't') {
|
||||||
|
try_void(toml_parse_datetime(StringBuilder_getStr(&sb), &value));
|
||||||
|
}
|
||||||
|
|
||||||
|
*out_value = value;
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
111
src/toml_parse/toml_parse_key_value.c
Normal file
111
src/toml_parse/toml_parse_key_value.c
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
/* 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_key_value(TomlParser* self, TomlTable* target_table)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
|
||||||
|
while (self->ptr < self->end) {
|
||||||
|
char ch;
|
||||||
|
|
||||||
|
ch = *self->ptr;
|
||||||
|
while (self->ptr < self->end && isspace(ch)) {
|
||||||
|
toml_move_next(self);
|
||||||
|
ch = *self->ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self->ptr == self->end) break;
|
||||||
|
|
||||||
|
str key;
|
||||||
|
if (isalnum(ch) || ch == '_') {
|
||||||
|
try_void(toml_parse_bare_key(self, &key));
|
||||||
|
} else if (ch == '\"') {
|
||||||
|
toml_move_next(self);
|
||||||
|
try_void(toml_parse_basic_string(self, &key));
|
||||||
|
} else if (ch == '\'') {
|
||||||
|
toml_move_next(self);
|
||||||
|
try_void(toml_parse_literal_string(self, &key));
|
||||||
|
} else if (ch == '[') {
|
||||||
|
break;
|
||||||
|
} else if (ch == '#') {
|
||||||
|
do {
|
||||||
|
toml_move_next(self);
|
||||||
|
ch = *self->ptr;
|
||||||
|
} while (self->ptr < self->end && ch != '\n');
|
||||||
|
toml_move_next(self);
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: unexpected token",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
ch = *self->ptr;
|
||||||
|
while (self->ptr < self->end && (ch == ' ' || ch == '\t')) {
|
||||||
|
toml_move_next(self);
|
||||||
|
ch = *self->ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self->ptr == self->end) {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: unterminated key value pair",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ch != '=') {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: unexpected token",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
toml_move_next(self);
|
||||||
|
|
||||||
|
ch = *self->ptr;
|
||||||
|
while (self->ptr < self->end && (ch == ' ' || ch == '\t')) {
|
||||||
|
toml_move_next(self);
|
||||||
|
ch = *self->ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self->ptr == self->end) {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: unterminated key value pair",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
TomlValue value;
|
||||||
|
try_void(toml_parse_value(self, &value));
|
||||||
|
|
||||||
|
TomlTable_set(target_table, key, value);
|
||||||
|
str_destroy(key);
|
||||||
|
|
||||||
|
while (self->ptr < self->end && (*self->ptr == ' ' ||* self->ptr == '\t')) {
|
||||||
|
toml_move_next(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self->ptr == self->end)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (*self->ptr == '#') {
|
||||||
|
do {
|
||||||
|
toml_move_next(self);
|
||||||
|
} while (self->ptr < self->end &&* self->ptr != '\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*self->ptr == '\r') {
|
||||||
|
toml_move_next(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*self->ptr == '\n') {
|
||||||
|
toml_move_next(self);
|
||||||
|
} else {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: new line expected",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
30
src/toml_parse/toml_parse_literal_string.c
Normal file
30
src/toml_parse/toml_parse_literal_string.c
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/* 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_literal_string(TomlParser* self, str* out_str)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
StringBuilder sb = StringBuilder_alloc(32);
|
||||||
|
bool success = false;
|
||||||
|
Defer(if(!success) StringBuilder_destroy(&sb));
|
||||||
|
|
||||||
|
while (self->ptr < self->end &&* self->ptr != '\'' &&* self->ptr != '\n') {
|
||||||
|
StringBuilder_append_char(&sb,* self->ptr);
|
||||||
|
toml_move_next(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
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",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
toml_move_next(self);
|
||||||
|
|
||||||
|
*out_str = StringBuilder_getStr(&sb);
|
||||||
|
success = true;
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
16
src/toml_parse/toml_parse_literal_string_value.c
Normal file
16
src/toml_parse/toml_parse_literal_string_value.c
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
/* 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_literal_string_value(TomlParser* self, TomlValue* out_value)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
|
||||||
|
str s;
|
||||||
|
try_void(toml_parse_literal_string(self, &s));
|
||||||
|
*out_value = TomlValue_move_str(s);
|
||||||
|
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
81
src/toml_parse/toml_parse_multi_line_basic_string.c
Normal file
81
src/toml_parse/toml_parse_multi_line_basic_string.c
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
/* 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_multi_line_basic_string(TomlParser* self, TomlValue* out_value)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
StringBuilder sb = StringBuilder_alloc(32);
|
||||||
|
bool success = false;
|
||||||
|
Defer(if(!success) StringBuilder_destroy(&sb));
|
||||||
|
|
||||||
|
if (*self->ptr == '\n') {
|
||||||
|
toml_move_next(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (self->ptr + 3 <= self->end && strncmp(self->ptr, "\"\"\"", 3) != 0) {
|
||||||
|
char ch1 = *self->ptr;
|
||||||
|
|
||||||
|
if (ch1 == '\\') {
|
||||||
|
if (self->ptr + 3 > self->end || strncmp(self->ptr, "\"\"\"", 3) == 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
toml_move_next(self);
|
||||||
|
char ch2 = *self->ptr;
|
||||||
|
|
||||||
|
if (ch2 == '\"') {
|
||||||
|
StringBuilder_append_char(&sb, '\"');
|
||||||
|
toml_move_next(self);
|
||||||
|
} else if (ch2 == 'b') {
|
||||||
|
StringBuilder_append_char(&sb, '\b');
|
||||||
|
toml_move_next(self);
|
||||||
|
} else if (ch2 == 't') {
|
||||||
|
StringBuilder_append_char(&sb, '\t');
|
||||||
|
toml_move_next(self);
|
||||||
|
} else if (ch2 == 'n') {
|
||||||
|
StringBuilder_append_char(&sb, '\n');
|
||||||
|
toml_move_next(self);
|
||||||
|
} else if (ch2 == 'f') {
|
||||||
|
StringBuilder_append_char(&sb, '\f');
|
||||||
|
toml_move_next(self);
|
||||||
|
} else if (ch2 == 'r') {
|
||||||
|
StringBuilder_append_char(&sb, '\r');
|
||||||
|
toml_move_next(self);
|
||||||
|
} else if (ch2 == '\\') {
|
||||||
|
StringBuilder_append_char(&sb, '\\');
|
||||||
|
toml_move_next(self);
|
||||||
|
} else if (ch2 == 'u') {
|
||||||
|
toml_move_next(self);
|
||||||
|
try_void(toml_encode_unicode_scalar(&sb, self, 4));
|
||||||
|
} else if (ch2 == 'U') {
|
||||||
|
toml_move_next(self);
|
||||||
|
try_void(toml_encode_unicode_scalar(&sb, self, 8));
|
||||||
|
} else if (ch2 == '\n') {
|
||||||
|
do {
|
||||||
|
toml_move_next(self);
|
||||||
|
} while (self->ptr + 3 <= self->end && isspace(*self->ptr));
|
||||||
|
} else {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: invalid escape character",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
StringBuilder_append_char(&sb, ch1);
|
||||||
|
toml_move_next(self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 basic string",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
toml_next_n(self, 3);
|
||||||
|
|
||||||
|
*out_value = TomlValue_move_str(StringBuilder_getStr(&sb));
|
||||||
|
success = true;
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
34
src/toml_parse/toml_parse_multi_line_literal_string.c
Normal file
34
src/toml_parse/toml_parse_multi_line_literal_string.c
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/* 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_multi_line_literal_string(TomlParser* self, TomlValue* out_value)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
StringBuilder sb = StringBuilder_alloc(32);
|
||||||
|
bool success = false;
|
||||||
|
Defer(if(!success) StringBuilder_destroy(&sb));
|
||||||
|
|
||||||
|
if (*self->ptr == '\n') {
|
||||||
|
toml_move_next(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (self->ptr + 3 <= self->end && strncmp(self->ptr, "\'\'\'", 3) != 0) {
|
||||||
|
StringBuilder_append_char(&sb,* self->ptr);
|
||||||
|
toml_move_next(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
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",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
toml_next_n(self, 3);
|
||||||
|
|
||||||
|
*out_value = TomlValue_move_str(StringBuilder_getStr(&sb));
|
||||||
|
success = true;
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
87
src/toml_parse/toml_parse_table.c
Normal file
87
src/toml_parse/toml_parse_table.c
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
/* 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_table(TomlParser* self, TomlTable* target_table)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
|
||||||
|
i32 is_array = false;
|
||||||
|
TomlTable* real_table = target_table;
|
||||||
|
TomlArray* key_path = TomlArray_new();
|
||||||
|
Defer(TomlArray_free(key_path));
|
||||||
|
|
||||||
|
if (self->ptr < self->end &&* self->ptr == '[') {
|
||||||
|
is_array = true;
|
||||||
|
toml_move_next(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
if (*self->ptr == ' ' ||* self->ptr == '\t') {
|
||||||
|
do {
|
||||||
|
toml_move_next(self);
|
||||||
|
} while (*self->ptr <* self->end && (*self->ptr == ' ' ||* self->ptr == '\t'));
|
||||||
|
} else if (*self->ptr == ']') {
|
||||||
|
if (is_array) {
|
||||||
|
if (self->ptr + 2 <= self->end && strncmp(self->ptr, "]]", 2) == 0) {
|
||||||
|
toml_next_n(self, 2);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
toml_move_next(self);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
str key_part;
|
||||||
|
if (isalnum(*self->ptr) ||* self->ptr == '_') {
|
||||||
|
try_void(toml_parse_bare_key(self, &key_part));
|
||||||
|
} else if (*self->ptr == '\"') {
|
||||||
|
toml_move_next(self);
|
||||||
|
try_void(toml_parse_basic_string(self, &key_part));
|
||||||
|
} else if (*self->ptr == '\'') {
|
||||||
|
toml_move_next(self);
|
||||||
|
try_void(toml_parse_literal_string(self, &key_part));
|
||||||
|
} else {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: unexpected token",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
TomlValue key_part_value = TomlValue_move_str(key_part);
|
||||||
|
TomlArray_append(key_path, key_part_value);
|
||||||
|
|
||||||
|
while (self->ptr < self->end &&
|
||||||
|
(*self->ptr == ' ' ||* self->ptr == '\t')) {
|
||||||
|
toml_move_next(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self->ptr < self->end &&* self->ptr == '.') {
|
||||||
|
toml_move_next(self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key_path->len == 0) {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: empty table name",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (self->ptr < self->end &&
|
||||||
|
(*self->ptr == ' ' ||* self->ptr == '\t' ||* self->ptr == '\r')) {
|
||||||
|
toml_move_next(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self->ptr < self->end &&* self->ptr != '\n') {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: new line expected",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
try(real_table, p, toml_walk_table_path(self, target_table, key_path, is_array, true));
|
||||||
|
try_void(toml_parse_key_value(self, real_table));
|
||||||
|
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
44
src/toml_parse/toml_parse_value.c
Normal file
44
src/toml_parse/toml_parse_value.c
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
/* 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_value(TomlParser* self, TomlValue* out_value)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
|
||||||
|
char ch = *self->ptr;
|
||||||
|
|
||||||
|
TomlValue value;
|
||||||
|
if (strncmp(self->ptr, "\"\"\"", 3) == 0) {
|
||||||
|
toml_next_n(self, 3);
|
||||||
|
try_void(toml_parse_multi_line_basic_string(self, &value));
|
||||||
|
} else if (strncmp(self->ptr, "\'\'\'", 3) == 0) {
|
||||||
|
toml_next_n(self, 3);
|
||||||
|
try_void(toml_parse_multi_line_literal_string(self, &value));
|
||||||
|
} else if (ch == '\"') {
|
||||||
|
toml_move_next(self);
|
||||||
|
try_void(toml_parse_basic_string_value(self, &value));
|
||||||
|
} else if (ch == '\'') {
|
||||||
|
toml_move_next(self);
|
||||||
|
try_void(toml_parse_literal_string_value(self, &value));
|
||||||
|
} else if (isdigit(ch) || ch == '+' || ch == '-' || ch == '.' || ch == 'n' || ch == 'i') {
|
||||||
|
try_void(toml_parse_int_or_float_or_time(self, &value));
|
||||||
|
} else if (ch == 't' || ch == 'f') {
|
||||||
|
try_void(toml_parse_bool(self, &value));
|
||||||
|
} else if (ch == '[') {
|
||||||
|
toml_move_next(self);
|
||||||
|
try_void(toml_parse_array(self, &value));
|
||||||
|
} else if (ch == '{') {
|
||||||
|
toml_move_next(self);
|
||||||
|
try_void(toml_parse_inline_table(self, &value));
|
||||||
|
} else {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_SYNTAX,
|
||||||
|
"%s:%d:%d: unexpected token",
|
||||||
|
self->filename, self->lineno, self->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
*out_value = value;
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
84
src/toml_parse/toml_walk_table_path.c
Normal file
84
src/toml_parse/toml_walk_table_path.c
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
/* 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(Table*) toml_walk_table_path(TomlParser* parser, TomlTable* table,
|
||||||
|
TomlArray* key_path, bool is_array, bool create_if_not_exist)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
TomlTable* real_table = table;
|
||||||
|
|
||||||
|
if (is_array) {
|
||||||
|
u64 i = 0;
|
||||||
|
for (; i < key_path->len - 1; i++) {
|
||||||
|
str part = *key_path->data[i].value.s;
|
||||||
|
TomlValue* t = TomlTable_get(real_table, part);
|
||||||
|
if (t == NULL) {
|
||||||
|
if (create_if_not_exist) {
|
||||||
|
TomlValue new_table_value = TomlValue_new_table();
|
||||||
|
TomlTable_set(real_table, part, new_table_value);
|
||||||
|
real_table = new_table_value.value.table;
|
||||||
|
} else {
|
||||||
|
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 {
|
||||||
|
real_table = t->value.table;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
str part = *key_path->data[i].value.s;
|
||||||
|
TomlValue* t = TomlTable_get(real_table, part);
|
||||||
|
if (t == NULL) {
|
||||||
|
if (create_if_not_exist) {
|
||||||
|
TomlValue array_value = TomlValue_new_array();
|
||||||
|
TomlValue new_table_value = TomlValue_new_table();
|
||||||
|
TomlArray_append(array_value.value.array, new_table_value);
|
||||||
|
TomlTable_set(real_table, part, array_value);
|
||||||
|
real_table = new_table_value.value.table;
|
||||||
|
} else {
|
||||||
|
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 {
|
||||||
|
if (t->type != TLIBTOML_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);
|
||||||
|
}
|
||||||
|
|
||||||
|
TomlValue new_table_value = TomlValue_new_table();
|
||||||
|
TomlArray_append(t->value.array, new_table_value);
|
||||||
|
real_table = new_table_value.value.table;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (u64 i = 0; i < key_path->len; i++) {
|
||||||
|
str part = *key_path->data[i].value.s;
|
||||||
|
TomlValue* t = TomlTable_get(real_table, part);
|
||||||
|
if (t == NULL) {
|
||||||
|
if (create_if_not_exist) {
|
||||||
|
TomlValue new_table_value = TomlValue_new_table();
|
||||||
|
TomlTable_set(real_table, part, new_table_value);
|
||||||
|
real_table = new_table_value.value.table;
|
||||||
|
} else {
|
||||||
|
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 {
|
||||||
|
if (t->type == TLIBTOML_ARRAY) {
|
||||||
|
real_table = t->value.array->data[t->value.array->len - 1].value.table;
|
||||||
|
} else if (t->type == TLIBTOML_TABLE) {
|
||||||
|
real_table = t->value.table;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Return RESULT_VALUE(p, real_table);
|
||||||
|
}
|
||||||
140
src/toml_parser.c
Normal file
140
src/toml_parser.c
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
/* 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"
|
||||||
|
|
||||||
|
TomlParser* toml_parser_new(str s, cstr filename)
|
||||||
|
{
|
||||||
|
TomlParser* parser = (TomlParser*)malloc(sizeof(TomlParser));
|
||||||
|
parser->begin = s.data;
|
||||||
|
parser->end = s.data + s.len;
|
||||||
|
parser->ptr = s.data;
|
||||||
|
parser->lineno = 1;
|
||||||
|
parser->colno = 1;
|
||||||
|
parser->filename = filename;
|
||||||
|
return parser;
|
||||||
|
}
|
||||||
|
|
||||||
|
void toml_parser_free(TomlParser* self)
|
||||||
|
{
|
||||||
|
if (!self)
|
||||||
|
return;
|
||||||
|
|
||||||
|
free(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void toml_move_next(TomlParser* self)
|
||||||
|
{
|
||||||
|
if (self->ptr < self->end) {
|
||||||
|
if (*self->ptr == '\n') {
|
||||||
|
self->lineno++;
|
||||||
|
self->colno = 1;
|
||||||
|
} else {
|
||||||
|
self->colno++;
|
||||||
|
}
|
||||||
|
self->ptr++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void toml_next_n(TomlParser* self, i32 n)
|
||||||
|
{
|
||||||
|
for (i32 i = 0; i < n; i++) {
|
||||||
|
toml_move_next(self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 toml_hex_char_to_int(char ch)
|
||||||
|
{
|
||||||
|
assert(isxdigit(ch));
|
||||||
|
if (isdigit(ch)) {
|
||||||
|
return ch - '0';
|
||||||
|
} else if (islower(ch)) {
|
||||||
|
return ch - 'a' + 10;
|
||||||
|
} else if (isupper(ch)) {
|
||||||
|
return ch - 'A' + 10;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result(void) toml_encode_unicode_scalar(StringBuilder* sb_ptr, TomlParser* parser, i32 n)
|
||||||
|
{
|
||||||
|
Deferral(1);
|
||||||
|
u32 scalar = 0;
|
||||||
|
|
||||||
|
if (parser->ptr + n > parser->end) {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_UNICODE,
|
||||||
|
"%s:%d:%d: invalid unicode scalar",
|
||||||
|
parser->filename, parser->lineno, parser->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i32 i = 0; i < n; i++) {
|
||||||
|
char ch = *parser->ptr;
|
||||||
|
if (isxdigit(ch)) {
|
||||||
|
scalar = scalar * 16 + toml_hex_char_to_int(ch);
|
||||||
|
toml_move_next(parser);
|
||||||
|
} else {
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_UNICODE,
|
||||||
|
"%s:%d:%d: invalid unicode scalar",
|
||||||
|
parser->filename, parser->lineno, parser->colno);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((scalar >= 0xd800 && scalar <= 0xdfff) ||
|
||||||
|
(scalar >= 0xfffe && scalar <= 0xffff))
|
||||||
|
{
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_UNICODE,
|
||||||
|
"%s:%d:%d: invalid unicode scalar",
|
||||||
|
parser->filename, parser->lineno, parser->colno);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scalar <= 0x7f) {
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)scalar);
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scalar <= 0x7ff) {
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0xc0 | (scalar >> 6)));
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0x80 | (scalar & 0x3f)));
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scalar <= 0xffff) {
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0xe0 | (scalar >> 12)));
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0x80 | ((scalar >> 6) & 0x3f)));
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0x80 | (scalar & 0x3f)));
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scalar <= 0x1fffff) {
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0xf0 | (scalar >> 18)));
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0x80 | ((scalar >> 12) & 0x3f)));
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0x80 | ((scalar >> 6) & 0x3f)));
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0x80 | (scalar & 0x3f)));
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scalar <= 0x3ffffff) {
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0xf8 | (scalar >> 24)));
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0x80 | ((scalar >> 18) & 0x3f)));
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0x80 | ((scalar >> 12) & 0x3f)));
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0x80 | ((scalar >> 6) & 0x3f)));
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0x80 | (scalar & 0x3f)));
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scalar <= 0x7fffffff) {
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0xfc | (scalar >> 30)));
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0x80 | ((scalar >> 24) & 0x3f)));
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0x80 | ((scalar >> 18) & 0x3f)));
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0x80 | ((scalar >> 12) & 0x3f)));
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0x80 | ((scalar >> 6) & 0x3f)));
|
||||||
|
StringBuilder_append_char(sb_ptr, (char)(0x80 | (scalar & 0x3f)));
|
||||||
|
Return RESULT_VOID;
|
||||||
|
}
|
||||||
|
|
||||||
|
Return RESULT_ERROR_CODE_FMT(TLIBTOML, TLIBTOML_ERR_UNICODE,
|
||||||
|
"%s:%d:%d: invalid unicode scalar",
|
||||||
|
parser->filename, parser->lineno, parser->colno);
|
||||||
|
}
|
||||||
59
tests/main.c
59
tests/main.c
@ -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 ".."
|
||||||
@ -20,7 +20,7 @@ void print_array(const TomlArray *array)
|
|||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
printf(", ");
|
printf(", ");
|
||||||
}
|
}
|
||||||
print_value(&array->elements[i]);
|
print_value(&array->data[i]);
|
||||||
}
|
}
|
||||||
printf("]");
|
printf("]");
|
||||||
}
|
}
|
||||||
@ -31,25 +31,25 @@ void print_value(const TomlValue* value)
|
|||||||
default:
|
default:
|
||||||
assert(false && "invalid type");
|
assert(false && "invalid type");
|
||||||
break;
|
break;
|
||||||
case TOML_TABLE:
|
case TLIBTOML_TABLE:
|
||||||
print_table(value->value.table);
|
print_table(value->value.table);
|
||||||
break;
|
break;
|
||||||
case TOML_ARRAY:
|
case TLIBTOML_ARRAY:
|
||||||
print_array(value->value.array);
|
print_array(value->value.array);
|
||||||
break;
|
break;
|
||||||
case TOML_STRING:
|
case TLIBTOML_STRING:
|
||||||
printf("\"%s\"", value->value.s.data);
|
printf("'" FMT_str "'", value->value.s->len, value->value.s->data);
|
||||||
break;
|
break;
|
||||||
case TOML_INTEGER:
|
case TLIBTOML_INTEGER:
|
||||||
printf("%" PRId64, value->value.i);
|
printf("%" PRId64, value->value.i);
|
||||||
break;
|
break;
|
||||||
case TOML_FLOAT:
|
case TLIBTOML_FLOAT:
|
||||||
printf("%f", value->value.f);
|
printf("%f", value->value.f);
|
||||||
break;
|
break;
|
||||||
case TOML_DATETIME:
|
case TLIBTOML_DATETIME:
|
||||||
printf("(datetime)");
|
printf("(datetime)");
|
||||||
break;
|
break;
|
||||||
case TOML_BOOLEAN:
|
case TLIBTOML_BOOLEAN:
|
||||||
printf("%s", value->value.b ? "true" : "false");
|
printf("%s", value->value.b ? "true" : "false");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -78,31 +78,24 @@ void print_table(const TomlTable* table)
|
|||||||
printf("}");
|
printf("}");
|
||||||
}
|
}
|
||||||
|
|
||||||
i32 test_run(cstr filename)
|
Result(void) test_run(cstr filename)
|
||||||
{
|
{
|
||||||
TomlTable* table = NULL;
|
Deferral(1);
|
||||||
i32 rc = 0;
|
try(TomlTable* table, p, toml_load_filename(filename));
|
||||||
|
Defer(TomlTable_free(table));
|
||||||
table = toml_load_filename(filename);
|
|
||||||
if (table == NULL)
|
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
print_table(table);
|
print_table(table);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
Return RESULT_VOID;
|
||||||
cleanup:
|
|
||||||
toml_table_free(table);
|
|
||||||
|
|
||||||
if (toml_err()->code != TOML_OK) {
|
|
||||||
fprintf(stderr, "%s\n", toml_err()->message);
|
|
||||||
rc = (i32)toml_err()->code;
|
|
||||||
}
|
|
||||||
toml_err_clear();
|
|
||||||
return rc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
i32 main(void)
|
i32 main(void)
|
||||||
{
|
{
|
||||||
|
Deferral(32);
|
||||||
|
try_fatal_void(tlibc_init());
|
||||||
|
Defer(tlibc_deinit());
|
||||||
|
try_fatal_void(tlibtoml_init());
|
||||||
|
Defer(tlibtoml_deinit());
|
||||||
|
|
||||||
static cstr const filenames[] = {
|
static cstr const filenames[] = {
|
||||||
/* should parse */
|
/* should parse */
|
||||||
PROJECT_SOURCE_DIR "/tests/key-values.toml",
|
PROJECT_SOURCE_DIR "/tests/key-values.toml",
|
||||||
@ -123,12 +116,14 @@ i32 main(void)
|
|||||||
i32 num_failed = 0;
|
i32 num_failed = 0;
|
||||||
|
|
||||||
for (i32 i = 0; i < total_tests; i++) {
|
for (i32 i = 0; i < total_tests; i++) {
|
||||||
i32 rc = test_run(filenames[i]);
|
ResultVar(void) r = test_run(filenames[i]);
|
||||||
if (rc == 0) {
|
if (!r.error) {
|
||||||
printf("test %d success\n", i);
|
printf("test %d success\n", i);
|
||||||
num_passed++;
|
num_passed++;
|
||||||
} else {
|
} else {
|
||||||
printf("test %d returned %d\n", i, rc);
|
str err_str = Error_toStr(r.error);
|
||||||
|
printf("%s\ntest %d failed\n", err_str.data, i);
|
||||||
|
str_destroy(err_str);
|
||||||
num_failed++;
|
num_failed++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -136,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;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user