From 7781e41927247bff9e567a47f9fa1862ed5596e6 Mon Sep 17 00:00:00 2001 From: Johnny Richard Date: Sat, 6 May 2023 00:01:25 +0200 Subject: 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 Reviewed-by: Carlos Maniero --- src/ast_pretty_printer.h | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/ast_pretty_printer.h (limited to 'src/ast_pretty_printer.h') 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 . + */ +#ifndef AST_PRETTY_PRINTER_H +#define AST_PRETTY_PRINTER_H + +#include "ast.h" + +#include +#include +#include + +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 */ -- cgit v1.2.3