Include parse tree rendering at end of debug output
This commit is contained in:
parent
57f669dfda
commit
b40c0326dc
3 changed files with 37 additions and 0 deletions
|
|
@ -35,6 +35,11 @@
|
|||
fputs("\n\n", stderr); \
|
||||
}
|
||||
|
||||
#define LOG_TREE() \
|
||||
if (self->print_debugging_graphs) { \
|
||||
ts_tree_print_dot_graph(self->finished_tree, self->language, stderr); \
|
||||
}
|
||||
|
||||
#define SYM_NAME(symbol) ts_language_symbol_name(self->language, symbol)
|
||||
|
||||
#define BOOL_STRING(value) (value ? "true" : "false")
|
||||
|
|
@ -1135,6 +1140,8 @@ TSTree *ts_parser_parse(TSParser *self, TSInput input, TSTree *old_tree) {
|
|||
break;
|
||||
}
|
||||
|
||||
LOG_ACTION("done");
|
||||
LOG_TREE();
|
||||
ts_stack_clear(self->stack);
|
||||
ts_tree_assign_parents(self->finished_tree);
|
||||
return self->finished_tree;
|
||||
|
|
|
|||
|
|
@ -439,3 +439,31 @@ char *ts_tree_string(const TSTree *self, const TSLanguage *language,
|
|||
ts_tree__write_to_string(self, language, result, size, true, include_all);
|
||||
return result;
|
||||
}
|
||||
|
||||
void ts_tree__print_dot_graph(const TSTree *self, size_t offset,
|
||||
const TSLanguage *language, FILE *f) {
|
||||
fprintf(f, "tree_%p [label=\"%s\"", self,
|
||||
ts_language_symbol_name(language, self->symbol));
|
||||
|
||||
if (self->child_count == 0)
|
||||
fprintf(f, ", shape=plaintext");
|
||||
if (self->extra)
|
||||
fprintf(f, ", fontcolor=gray");
|
||||
|
||||
fprintf(f, ", tooltip=\"%lu - %lu\"]\n", offset,
|
||||
offset + ts_tree_total_chars(self));
|
||||
for (size_t i = 0; i < self->child_count; i++) {
|
||||
const TSTree *child = self->children[i];
|
||||
ts_tree__print_dot_graph(child, offset, language, f);
|
||||
fprintf(f, "tree_%p -> tree_%p [tooltip=%lu]\n", self, child, i);
|
||||
offset += ts_tree_total_chars(child);
|
||||
}
|
||||
}
|
||||
|
||||
void ts_tree_print_dot_graph(const TSTree *self, const TSLanguage *language,
|
||||
FILE *f) {
|
||||
fprintf(f, "digraph tree {\n");
|
||||
fprintf(f, "edge [arrowhead=none]\n");
|
||||
ts_tree__print_dot_graph(self, 0, language, f);
|
||||
fprintf(f, "}\n");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ extern "C" {
|
|||
#include "tree_sitter/parser.h"
|
||||
#include "runtime/length.h"
|
||||
#include "runtime/array.h"
|
||||
#include <stdio.h>
|
||||
|
||||
extern TSStateId TS_TREE_STATE_INDEPENDENT;
|
||||
extern TSStateId TS_TREE_STATE_ERROR;
|
||||
|
|
@ -70,6 +71,7 @@ void ts_tree_set_children(TSTree *, size_t, TSTree **);
|
|||
void ts_tree_assign_parents(TSTree *);
|
||||
void ts_tree_edit(TSTree *, TSInputEdit);
|
||||
char *ts_tree_string(const TSTree *, const TSLanguage *, bool include_all);
|
||||
void ts_tree_print_dot_graph(const TSTree *, const TSLanguage *, FILE *);
|
||||
|
||||
static inline size_t ts_tree_total_chars(const TSTree *self) {
|
||||
return self->padding.chars + self->size.chars;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue