/* * TinyBlocks - tinyblocks-toml * ast.h: arvore sintatica e leitura de documentos TOML. */ #ifndef TB_TOML_AST_H #define TB_TOML_AST_H #include #include #include "lexeme.h" #ifdef __cplusplus extern "C" { #endif typedef enum { TOML_STRING, TOML_INTEGER, TOML_FLOAT, TOML_BOOL, TOML_DATETIME, TOML_ARRAY, TOML_TABLE } TomlValueKind; typedef struct TomlValue TomlValue; typedef struct TomlKeyVal TomlKeyVal; struct TomlKeyVal { char* key; TomlValue* value; TomlKeyVal* next; }; struct TomlValue { TomlValueKind kind; int line, col; union { char* string; long long integer; double real; bool boolean; struct { TomlValue** items; size_t count, cap; } array; struct { TomlKeyVal* head, *tail; size_t count; } table; } as; }; typedef struct TomlDoc { TomlValue* root; /* tabela raiz */ } TomlDoc; /* Parser (texto em memoria / arquivo). Em erro preenche err. */ TomlDoc* toml_parse(const char* src, TomlLexError* err); TomlDoc* toml_parse_file(const char* path, TomlLexError* err); void toml_free(TomlDoc* doc); /* Navegacao */ const TomlValue* toml_table_get(const TomlValue* table, const char* key); const TomlValue* toml_path_get(const TomlValue* root, const char* dotted); const TomlValue* toml_array_get(const TomlValue* arr, size_t i); size_t toml_array_count(const TomlValue* arr); size_t toml_table_count(const TomlValue* table); const TomlKeyVal* toml_table_head(const TomlValue* table); /* Conversoes com valor padrao */ const char* toml_as_string(const TomlValue* v, const char* def); long long toml_as_int(const TomlValue* v, long long def); double toml_as_float(const TomlValue* v, double def); bool toml_as_bool(const TomlValue* v, bool def); /* Escreve a arvore em stdout (debug) */ void toml_dump(const TomlValue* v, int indent); #ifdef __cplusplus } #endif #endif /* TB_TOML_AST_H */