98 lines
2.9 KiB
C
98 lines
2.9 KiB
C
// This file is part of noxos and licensed under the MIT open source license
|
|
|
|
#ifndef NOXOS_JSON_H
|
|
#define NOXOS_JSON_H
|
|
|
|
#include "utils/string.h"
|
|
#include "utils/stdtypes.h"
|
|
|
|
|
|
/*
|
|
* JSON node schematic
|
|
*
|
|
* +-----------+
|
|
* +----------| Root Node |------------+
|
|
* | +-----------+ |
|
|
* | ^ |
|
|
* | | |
|
|
* [childs_start] /----[parent]------\ [childs_end]
|
|
* | / | \ |
|
|
* v / | \ v
|
|
* +------+ +------+ +------+
|
|
* | Node |--[next]->| Node |--[next]->| Node |
|
|
* | |<-[prev]--| |<-[prev]--| | ---> . . .
|
|
* +------+ +------+ +------+
|
|
*
|
|
* |
|
|
* v
|
|
* . . .
|
|
*
|
|
* */
|
|
|
|
#define JSON_INCREMENT_TOKEN_ID \
|
|
*token_id += 1; \
|
|
if (*token_id >= json->num_tokens) { \
|
|
log(LOG_ERROR, "failed to parse json -> unexpected EOF"); \
|
|
json_node_dump(json->root_node, 0); \
|
|
return false; \
|
|
}
|
|
|
|
typedef enum {
|
|
JSON_TOKEN_NUMERIC,
|
|
JSON_TOKEN_TEXT,
|
|
JSON_TOKEN_STRING,
|
|
JSON_TOKEN_SPECIAL
|
|
} json_token_type_E;
|
|
|
|
typedef enum {
|
|
JSON_NODE_OBJECT,
|
|
JSON_NODE_ARRAY,
|
|
JSON_NODE_STRING,
|
|
JSON_NODE_NUMBER,
|
|
JSON_NODE_BOOL,
|
|
JSON_NODE_NULL
|
|
} json_node_type_E;
|
|
|
|
typedef struct {
|
|
json_token_type_E type;
|
|
uint64_t value;
|
|
string_t string;
|
|
|
|
uint32_t line;
|
|
uint32_t column;
|
|
} json_token_T;
|
|
|
|
typedef struct json_node_T json_node_T;
|
|
struct json_node_T {
|
|
json_node_type_E type;
|
|
string_t string;
|
|
uint64_t value;
|
|
|
|
json_node_T* parent;
|
|
json_node_T* childs_start;
|
|
json_node_T* childs_end;
|
|
json_node_T* prev;
|
|
json_node_T* next;
|
|
};
|
|
|
|
typedef struct {
|
|
json_token_T* tokens;
|
|
uint64_t num_tokens;
|
|
void* string_buffer;
|
|
json_node_T* root_node;
|
|
} json_T;
|
|
|
|
json_T* json_from_string (string_t str);
|
|
void json_destruct (json_T* json);
|
|
json_node_T* json_node_alloc (json_node_T* parent, json_node_type_E type, json_token_T* token);
|
|
void json_node_destruct (json_node_T* node);
|
|
void json_node_dump (json_node_T* node, uint32_t indent);
|
|
string_t json_node_type_to_string (json_node_type_E type);
|
|
void json_tokenize (json_T* json, string_t str);
|
|
bool json_parse (json_T* json);
|
|
bool json_parse_assignment (json_T* json, uint32_t* token_id, json_node_T* node);
|
|
bool json_parse_object (json_T* json, uint32_t* token_id, json_node_T* node);
|
|
bool json_parse_array (json_T* json, uint32_t* token_id, json_node_T* node);
|
|
|
|
#endif //NOXOS_JSON_H
|