--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_parse_tree_print.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/06/21 09:51:43 by ljiriste #+# #+# */
+/* Updated: 2024/06/21 10:33:09 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_parse.h"
+#include "libft.h"
+
+#define MAX_WIDTH 80
+
+static void print_last_at_depth(t_parse_tree_node *node, size_t depth, char branches[MAX_WIDTH]);
+static void print_node_at_depth(t_parse_tree_node *node, size_t depth, char branches[MAX_WIDTH]);
+
+static void print_children_at_depth(t_parse_tree_node *node, size_t depth, char branches[MAX_WIDTH])
+{
+ size_t i;
+
+ i = 0;
+ while (i + 1 < node->children.size)
+ {
+ print_node_at_depth(ft_vec_access(&node->children, i), depth + 1, branches);
+ ++i;
+ }
+ if (i < node->children.size)
+ print_last_at_depth(ft_vec_access(&node->children, i), depth + 1, branches);
+ if (depth > 0)
+ branches[ft_strlen(branches) - 1] = '\0';
+ branches[ft_strlen(branches) - 1] = '\0';
+ return ;
+}
+
+static void print_last_at_depth(t_parse_tree_node *node, size_t depth, char branches[MAX_WIDTH])
+{
+ ft_printf("%s-%s\n", branches, node->token.type);
+ branches[ft_strlen(branches) - 1] = ' ';
+ ft_strlcat(branches, " |", MAX_WIDTH);
+ print_children_at_depth(node, depth, branches);
+}
+
+static void print_node_at_depth(t_parse_tree_node *node, size_t depth, char branches[MAX_WIDTH])
+{
+ if (depth == 0)
+ {
+ ft_printf("%s\n", node->token.type);
+ ft_strlcat(branches, "|", MAX_WIDTH);
+ }
+ else
+ {
+ ft_printf("%s-%s\n", branches, node->token.type);
+ ft_strlcat(branches, " |", MAX_WIDTH);
+ }
+ print_children_at_depth(node, depth, branches);
+}
+
+void ft_parse_tree_print(t_parse_tree_node *root)
+{
+ char branches[MAX_WIDTH];
+
+ branches[0] = '\0';
+ print_node_at_depth(root, 0, branches);
+ return ;
+}
/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/27 21:21:54 by ljiriste #+# #+# */
-/* Updated: 2024/06/21 09:04:04 by ljiriste ### ########.fr */
+/* Updated: 2024/06/21 09:57:12 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
const char *filename,
const char *rules_filename);
t_parse_tree_node *ft_parse(t_vec *tokens, t_parsing_table *table);
+
void ft_parsing_table_print(t_parsing_table *table,
unsigned int column_width);
+void ft_parse_tree_print(t_parse_tree_node *root);
void free_token(void *v_token);
void ft_parse_tree_free(void *v_node);