Handle backslashes in token names when printing DOT debug graphs

This commit is contained in:
Max Brunsfeld 2022-06-25 17:13:11 -07:00
parent 04381dcea3
commit 3ac36b0cbe
4 changed files with 28 additions and 19 deletions

View file

@ -283,6 +283,31 @@ static inline void ts_language_aliases_for_symbol(
}
}
static inline void ts_language_write_symbol_as_dot_string(
const TSLanguage *self,
FILE *f,
TSSymbol symbol
) {
const char *name = ts_language_symbol_name(self, symbol);
for (const char *c = name; *c; c++) {
switch (*c) {
case '"':
case '\\':
fputc('\\', f);
fputc(*c, f);
break;
case '\n':
fputs("\\n", f);
break;
case '\t':
fputs("\\n", f);
break;
default:
fputc(*c, f);
break;
}
}
}
#ifdef __cplusplus
}

View file

@ -160,7 +160,7 @@ static void ts_parser__log(TSParser *self) {
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('\\', self->dot_graph_file);
if (*c == '"' || *c == '\\') fputc('\\', self->dot_graph_file);
fputc(*c, self->dot_graph_file);
}
fprintf(self->dot_graph_file, "\"\n}\n\n");

View file

@ -846,11 +846,7 @@ bool ts_stack_print_dot_graph(Stack *self, const TSLanguage *language, FILE *f)
fprintf(f, "label=\"");
bool quoted = ts_subtree_visible(link.subtree) && !ts_subtree_named(link.subtree);
if (quoted) fprintf(f, "'");
const char *name = ts_language_symbol_name(language, ts_subtree_symbol(link.subtree));
for (const char *c = name; *c; c++) {
if (*c == '\"' || *c == '\\') fprintf(f, "\\");
fprintf(f, "%c", *c);
}
ts_language_write_symbol_as_dot_string(language, f, ts_subtree_symbol(link.subtree));
if (quoted) fprintf(f, "'");
fprintf(f, "\"");
fprintf(

View file

@ -834,18 +834,6 @@ static size_t ts_subtree__write_char_to_string(char *s, size_t n, int32_t c) {
return snprintf(s, n, "%d", c);
}
static void ts_subtree__write_dot_string(FILE *f, const char *string) {
for (const char *c = string; *c; c++) {
if (*c == '"') {
fputs("\\\"", f);
} else if (*c == '\n') {
fputs("\\n", f);
} else {
fputc(*c, f);
}
}
}
static const char *ROOT_FIELD = "__ROOT__";
static size_t ts_subtree__write_to_string(
@ -975,7 +963,7 @@ void ts_subtree__print_dot_graph(const Subtree *self, uint32_t start_offset,
TSSymbol symbol = alias_symbol ? alias_symbol : subtree_symbol;
uint32_t end_offset = start_offset + ts_subtree_total_bytes(*self);
fprintf(f, "tree_%p [label=\"", (void *)self);
ts_subtree__write_dot_string(f, ts_language_symbol_name(language, symbol));
ts_language_write_symbol_as_dot_string(language, f, symbol);
fprintf(f, "\"");
if (ts_subtree_child_count(*self) == 0) fprintf(f, ", shape=plaintext");