From 199a94cc26640c3add7b23757a1de510d7acf058 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 11 May 2018 12:43:04 -0700 Subject: [PATCH] Allow the parser to print dot graphs to any file --- include/tree_sitter/runtime.h | 12 +++++----- src/runtime/parser.c | 36 ++++++++++++++-------------- test/benchmarks.cc | 2 +- test/integration/fuzzing-examples.cc | 2 +- test/integration/real_grammars.cc | 2 +- test/integration/test_grammars.cc | 2 +- test/runtime/parser_test.cc | 2 +- 7 files changed, 29 insertions(+), 29 deletions(-) diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index fa6fd919..acd2c3b3 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -34,6 +34,11 @@ typedef struct { uint32_t column; } TSPoint; +typedef struct { + TSPoint start; + TSPoint end; +} TSRange; + typedef struct { void *payload; const char *(*read)(void *payload, uint32_t *bytes_read); @@ -60,11 +65,6 @@ typedef struct { TSPoint extent_added; } TSInputEdit; -typedef struct { - TSPoint start; - TSPoint end; -} TSRange; - typedef struct { const void *subtree; const TSTree *tree; @@ -79,7 +79,7 @@ const TSLanguage *ts_parser_language(const TSParser *); bool ts_parser_set_language(TSParser *, const TSLanguage *); TSLogger ts_parser_logger(const TSParser *); void ts_parser_set_logger(TSParser *, TSLogger); -void ts_parser_print_debugging_graphs(TSParser *, bool); +void ts_parser_print_dot_graphs(TSParser *, FILE *); void ts_parser_halt_on_error(TSParser *, bool); TSTree *ts_parser_parse(TSParser *, const TSTree *, TSInput); TSTree *ts_parser_parse_string(TSParser *, const TSTree *, const char *, uint32_t); diff --git a/src/runtime/parser.c b/src/runtime/parser.c index 1c7da6b1..02e96857 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -17,21 +17,21 @@ #include "runtime/tree.h" #define LOG(...) \ - if (self->lexer.logger.log || self->print_debugging_graphs) { \ + if (self->lexer.logger.log || self->dot_graph_file) { \ snprintf(self->lexer.debug_buffer, TREE_SITTER_SERIALIZATION_BUFFER_SIZE, __VA_ARGS__); \ ts_parser__log(self); \ } -#define LOG_STACK() \ - if (self->print_debugging_graphs) { \ - ts_stack_print_dot_graph(self->stack, self->language, stderr); \ - fputs("\n\n", stderr); \ +#define LOG_STACK() \ + if (self->dot_graph_file) { \ + ts_stack_print_dot_graph(self->stack, self->language, self->dot_graph_file); \ + fputs("\n\n", self->dot_graph_file); \ } -#define LOG_TREE() \ - if (self->print_debugging_graphs) { \ - ts_subtree_print_dot_graph(self->finished_tree, self->language, stderr); \ - fputs("\n", stderr); \ +#define LOG_TREE() \ + if (self->dot_graph_file) { \ + ts_subtree_print_dot_graph(self->finished_tree, self->language, self->dot_graph_file); \ + fputs("\n", self->dot_graph_file); \ } #define SYM_NAME(symbol) ts_language_symbol_name(self->language, symbol) @@ -58,7 +58,7 @@ struct TSParser { ReusableNode reusable_node; void *external_scanner_payload; bool in_ambiguity; - bool print_debugging_graphs; + FILE *dot_graph_file; bool halt_on_error; unsigned accept_count; }; @@ -89,13 +89,13 @@ static void ts_parser__log(TSParser *self) { ); } - if (self->print_debugging_graphs) { - fprintf(stderr, "graph {\nlabel=\""); + if (self->dot_graph_file) { + fprintf(self->dot_graph_file, "graph {\nlabel=\""); for (char *c = &self->lexer.debug_buffer[0]; *c != 0; c++) { - if (*c == '"') fputc('\\', stderr); - fputc(*c, stderr); + if (*c == '"') fputc('\\', self->dot_graph_file); + fputc(*c, self->dot_graph_file); } - fprintf(stderr, "\"\n}\n\n"); + fprintf(self->dot_graph_file, "\"\n}\n\n"); } } @@ -1297,7 +1297,7 @@ TSParser *ts_parser_new() { self->stack = ts_stack_new(&self->tree_pool); self->finished_tree = NULL; self->reusable_node = reusable_node_new(); - self->print_debugging_graphs = false; + self->dot_graph_file = NULL; self->halt_on_error = false; ts_parser__set_cached_token(self, 0, NULL, NULL); return self; @@ -1345,8 +1345,8 @@ void ts_parser_set_logger(TSParser *self, TSLogger logger) { self->lexer.logger = logger; } -void ts_parser_print_debugging_graphs(TSParser *self, bool should_print) { - self->print_debugging_graphs = should_print; +void ts_parser_print_dot_graphs(TSParser *self, FILE *file) { + self->dot_graph_file = file; } void ts_parser_halt_on_error(TSParser *self, bool should_halt_on_error) { diff --git a/test/benchmarks.cc b/test/benchmarks.cc index d4f475b5..6612444e 100644 --- a/test/benchmarks.cc +++ b/test/benchmarks.cc @@ -46,7 +46,7 @@ int main(int argc, char *arg[]) { TSParser *parser = ts_parser_new(); if (getenv("TREE_SITTER_BENCHMARK_SVG")) { - ts_parser_print_debugging_graphs(parser, true); + ts_parser_print_dot_graphs(parser, stderr); } else if (getenv("TREE_SITTER_BENCHMARK_LOG")) { ts_parser_set_logger(parser, stderr_logger_new(false)); } diff --git a/test/integration/fuzzing-examples.cc b/test/integration/fuzzing-examples.cc index 92682d4c..aca95840 100644 --- a/test/integration/fuzzing-examples.cc +++ b/test/integration/fuzzing-examples.cc @@ -32,7 +32,7 @@ describe("examples found via fuzzing", [&]() { TSParser *parser = ts_parser_new(); if (getenv("TREE_SITTER_ENABLE_DEBUG_GRAPHS")) { - ts_parser_print_debugging_graphs(parser, true); + ts_parser_print_dot_graphs(parser, stderr); } const string &language_name = examples[i].first; diff --git a/test/integration/real_grammars.cc b/test/integration/real_grammars.cc index 0d594ce5..a628177f 100644 --- a/test/integration/real_grammars.cc +++ b/test/integration/real_grammars.cc @@ -43,7 +43,7 @@ for (auto &language_name : test_languages) { // ts_parser_set_logger(parser, stderr_logger_new(true)); if (debug_graphs_enabled) { - ts_parser_print_debugging_graphs(parser, true); + ts_parser_print_dot_graphs(parser, stderr); } }); diff --git a/test/integration/test_grammars.cc b/test/integration/test_grammars.cc index bba40ace..62174855 100644 --- a/test/integration/test_grammars.cc +++ b/test/integration/test_grammars.cc @@ -56,7 +56,7 @@ for (auto &language_name : test_languages) { ts_parser_set_language(parser, language); if (getenv("TREE_SITTER_ENABLE_DEBUG_GRAPHS")) { - ts_parser_print_debugging_graphs(parser, true); + ts_parser_print_dot_graphs(parser, stderr); } TSTree *tree = ts_parser_parse_string(parser, nullptr, entry.input.c_str(), entry.input.size()); diff --git a/test/runtime/parser_test.cc b/test/runtime/parser_test.cc index e9cfbe72..7d0b2d1d 100644 --- a/test/runtime/parser_test.cc +++ b/test/runtime/parser_test.cc @@ -27,7 +27,7 @@ describe("Parser", [&]() { tree = nullptr; parser = ts_parser_new(); if (getenv("TREE_SITTER_ENABLE_DEBUG_GRAPHS")) { - ts_parser_print_debugging_graphs(parser, true); + ts_parser_print_dot_graphs(parser, stderr); } });