8_ast_to_json_aux(
ast_node_t* root, FILE* file,
int level);
11_print_at_level(FILE* file,
int level,
char* text);
42 _ast_ensure_children_size(parent);
53 _ast_ensure_children_size(parent);
58 new_child->
parent = parent;
72 _ast_to_json_aux(root, file, 0);
76#define _JSON_PRINT_FIELD(level, name, fmt, ...) \
77 _print_at_level(file, level, "\"" name "\": "); \
78 fprintf(file, fmt, __VA_ARGS__);
81_ast_to_json_aux(
ast_node_t* root, FILE* file,
int level)
83 _print_at_level(file, level,
"{\n");
87 if (root->
token != NULL) {
94 _print_at_level(file, level + 1,
"},\n");
102 _ast_to_json_aux(&root->
children[i], file, level + 2);
104 fputs((i < root->children_count - 1 ?
",\n" :
"\n"), file);
106 _print_at_level(file, level + 1,
"]\n");
109 _print_at_level(file, level,
"}");
113_print_at_level(FILE* file,
int level,
char* text)
120 fprintf(file,
"%*c%s", level * 2,
' ', text);
void ast_node_init(ast_node_t *node, ast_node_t *parent, node_type_t type, token_t *token)
Initializes an AST node.
void ast_copy(ast_node_t *destiny, ast_node_t *source)
Copies the content of source to destinty AST nodes.
void ast_close(ast_node_t *root)
Closes the given AST root node and all children.
ast_node_t * ast_insert_children(ast_node_t *parent, ast_node_t *child)
Insert a exitent AST node as children of the given parent node.
void ast_to_json(ast_node_t *root, FILE *file)
Reads the AST and converts it to JSON, writting on the given file stream.
#define _JSON_PRINT_FIELD(level, name, fmt,...)
ast_node_t * ast_add_children(ast_node_t *parent, node_type_t type, token_t *token)
Add a new children for the given AST node.
const char * mya_node_types[]
const char * mya_token_types[]
unsigned int children_count
struct ast_node * children
unsigned int _children_length
char * data
Pointer for the raw string content (a normal C string).
long long int value
Integer value of the token.
token_type_t type
Token type.
dstring_t lexeme
Lexeme of the token.
unsigned int line
Token line inside the module.
unsigned int column
Column of the token position on the line.
enum node_type node_type_t
#define AST_CHILDREN_LENGTH_INCREMENT
struct ast_node ast_node_t
#define AST_INITIAL_CHILDREN_LENGTH
struct token token_t
Struct for a Mya token.