From 1850951b4f940312eed67ef750fc3e35e44ff1ef Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 24 Feb 2016 17:23:58 -0800 Subject: [PATCH] Escape special chars in production names for debug graphs --- src/runtime/stack.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/runtime/stack.c b/src/runtime/stack.c index d93d3303..21b8e39a 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -519,8 +519,19 @@ size_t ts_stack__write_dot_graph(Stack *self, char *string, size_t n, for (int j = 0; j < node->successor_count; j++) { StackLink successor = node->successors[j]; - cursor += snprintf(*s, n, "node_%p -> node_%p [label=\"%s\"];\n", node, - successor.node, symbol_names[successor.tree->symbol]); + cursor += snprintf(*s, n, "node_%p -> node_%p [label=\"", node, successor.node); + + const char *name = symbol_names[successor.tree->symbol]; + for (const char *c = name; *c; c++) { + if (*c == '\"' || *c == '\\') { + **s = '\\'; + cursor++; + } + **s = *c; + cursor++; + } + + cursor += snprintf(*s, n, "\"];\n"); if (j == 0) { path->node = successor.node;