Add the json parser
This commit is contained in:
30
haversine_02/include/json/dstring.h
Normal file
30
haversine_02/include/json/dstring.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef DSTRING_H
|
||||
#define DSTRING_H
|
||||
|
||||
#include "aliases.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct dstring dstr_t;
|
||||
|
||||
dstr_t *dstr_with_capacity(u64 capacity);
|
||||
dstr_t *dstr_from_string(const char *str);
|
||||
void dstr_update(dstr_t **dst, const char *src);
|
||||
void dstr_free(dstr_t **str);
|
||||
void dstr_concat(dstr_t **dst, const char *src);
|
||||
void dstr_append(dstr_t **dst, char c);
|
||||
void dstr_resize(dstr_t **str);
|
||||
void dstr_clear(dstr_t *str);
|
||||
void dstr_print(const dstr_t *str);
|
||||
i64 dstr_find(const dstr_t *str, const char *substr);
|
||||
u64 dstr_length(const dstr_t *str);
|
||||
u64 dstr_capacity(const dstr_t *str);
|
||||
const char *dstr_to_cstr(const dstr_t *str);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // !DSTRING_H
|
||||
82
haversine_02/include/json/json_entities.h
Normal file
82
haversine_02/include/json/json_entities.h
Normal file
@@ -0,0 +1,82 @@
|
||||
#ifndef JSON_ENTITIES_H
|
||||
#define JSON_ENTITIES_H
|
||||
|
||||
#include "aliases.h"
|
||||
#include "dstring.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct json_entity jentity_t;
|
||||
typedef struct json_collection jcoll_t;
|
||||
typedef struct json_value jval_t;
|
||||
typedef struct json_pair jpair_t;
|
||||
|
||||
typedef enum {
|
||||
JVAL_EMPTY,
|
||||
JVAL_COLLECTION,
|
||||
JVAL_STRING,
|
||||
JVAL_INTEGER,
|
||||
JVAL_DOUBLE,
|
||||
JVAL_BOOLEAN,
|
||||
JVAL_NULL,
|
||||
} jval_type;
|
||||
|
||||
struct json_value {
|
||||
jval_type type;
|
||||
union {
|
||||
void *null_val;
|
||||
jcoll_t *collection;
|
||||
dstr_t *string;
|
||||
i64 num_int;
|
||||
f64 num_dbl;
|
||||
bool boolean;
|
||||
};
|
||||
};
|
||||
|
||||
struct json_pair {
|
||||
dstr_t *key;
|
||||
jval_t value;
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
JENTITY_SINGLE,
|
||||
JENTITY_PAIR,
|
||||
} jentity_type;
|
||||
|
||||
struct json_entity {
|
||||
jentity_type type;
|
||||
union {
|
||||
jval_t value;
|
||||
jpair_t pair;
|
||||
};
|
||||
jentity_t *parent;
|
||||
jentity_t *next;
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
JCOLL_OBJECT,
|
||||
JCOLL_ARRAY,
|
||||
} jcoll_type;
|
||||
|
||||
struct json_collection {
|
||||
u64 size;
|
||||
jcoll_type type;
|
||||
jentity_t *begin;
|
||||
jentity_t *end;
|
||||
};
|
||||
|
||||
void print_json(const jentity_t *entity, u32 indent);
|
||||
void free_json(jentity_t **entity);
|
||||
jcoll_t *get_collection_from_entity(const jentity_t *entity);
|
||||
jentity_t *create_new_single_entity(const jval_t value, jentity_t *parent);
|
||||
jentity_t *create_new_pair_entity(dstr_t *key, const jval_t value,
|
||||
jentity_t *parent);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // !JSON_ENTITIES_H
|
||||
72
haversine_02/include/json/lexer.h
Normal file
72
haversine_02/include/json/lexer.h
Normal file
@@ -0,0 +1,72 @@
|
||||
#ifndef LEXER_STATES_H
|
||||
#define LEXER_STATES_H
|
||||
|
||||
#include "aliases.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#define VALID_JSON true
|
||||
#define INVALID_JSON false
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef const char *str_view_t;
|
||||
|
||||
typedef enum {
|
||||
TK_NO_TOKEN,
|
||||
TK_L_BRACE,
|
||||
TK_R_BRACE,
|
||||
TK_L_BRACKET,
|
||||
TK_R_BRACKET,
|
||||
TK_NULL,
|
||||
TK_BOOL,
|
||||
TK_STR_KEY,
|
||||
TK_STR_VAL,
|
||||
TK_INTEGER,
|
||||
TK_DOUBLE,
|
||||
} token_type;
|
||||
|
||||
typedef union {
|
||||
void *no_val;
|
||||
i64 num_int;
|
||||
f64 num_frac;
|
||||
str_view_t string;
|
||||
bool boolean;
|
||||
} token_value_t;
|
||||
|
||||
typedef struct {
|
||||
u64 line;
|
||||
u64 column;
|
||||
token_type type;
|
||||
token_value_t value;
|
||||
} token_t;
|
||||
|
||||
typedef enum {
|
||||
LEX_ERR_NONE,
|
||||
LEX_ERR_INVALID,
|
||||
} lex_err_type;
|
||||
|
||||
typedef struct {
|
||||
lex_err_type errno;
|
||||
str_view_t msg;
|
||||
} lex_err_t;
|
||||
|
||||
typedef struct {
|
||||
lex_err_t error;
|
||||
token_t token;
|
||||
} lex_result_t;
|
||||
|
||||
typedef struct lexer_s lexer_t;
|
||||
|
||||
void lexer_init(lexer_t **lexer);
|
||||
void lexer_free(lexer_t **lexer);
|
||||
lex_result_t get_next_token(lexer_t *lexer, const char *text);
|
||||
|
||||
void print_token(token_t token);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // !LEXER_STATES_H
|
||||
19
haversine_02/include/json/parser.h
Normal file
19
haversine_02/include/json/parser.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef PARSER_H
|
||||
#define PARSER_H
|
||||
|
||||
#include "json_entities.h"
|
||||
#include "lexer.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct parser_s parser_t;
|
||||
|
||||
jentity_t *load_json(const char *filepath);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // !PARSER_H
|
||||
Reference in New Issue
Block a user