diff options
author | Johnny Richard <johnny@johnnyrichard.com> | 2023-05-06 00:01:25 +0200 |
---|---|---|
committer | Carlos Maniero <carlos@maniero.me> | 2023-05-05 19:53:40 -0300 |
commit | 7781e41927247bff9e567a47f9fa1862ed5596e6 (patch) | |
tree | 29635bb2a7fe8a830ed165a3257acb6882dc1ca3 /src/ast_pretty_printer.h | |
parent | 17ae189d4a6aa926d8931b1e4f7db8de6caddd90 (diff) |
cli: Add AST pretty-printing option (--ast-dump)
Parsing can be a complex process, and it's not always easy to get a
clear picture of what's happening with the AST. This commit adds a new
feature to the CLI that allows us to pretty-print the AST (outputs to
stdout), making it easier to visualize the tree structure and understand
how the parser is working.
The new --ast-dump option generates a human-readable representation of
the AST, including node types, values, and child relationships. This
information can be invaluable for debugging and understanding the
parser's behavior.
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
Reviewed-by: Carlos Maniero <carlos@maniero.me>
Diffstat (limited to 'src/ast_pretty_printer.h')
-rw-r--r-- | src/ast_pretty_printer.h | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/ast_pretty_printer.h b/src/ast_pretty_printer.h new file mode 100644 index 0000000..d7e36b7 --- /dev/null +++ b/src/ast_pretty_printer.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2023 Johnny Richard + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +#ifndef AST_PRETTY_PRINTER_H +#define AST_PRETTY_PRINTER_H + +#include "ast.h" + +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> + +typedef struct ast_pretty_printer_t +{ + uint64_t indentation_fmt; + uint64_t indentation_lst_children; + uint32_t indentation_level; + bool last_children; + FILE *stream; +} ast_pretty_printer_t; + +void +ast_pretty_printer_init(ast_pretty_printer_t *printer, FILE *stream); + +void +ast_pretty_printer_print_ast(ast_pretty_printer_t *printer, ast_node_t *ast); + +#endif /* AST_PRETTY_PRINTER_H */ |