From bad1bff3bb1dc75dd172a2b204051cd94da61e78 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 26 Oct 2015 13:36:13 -0700 Subject: [PATCH] Sanitize line breaks in symbol names --- src/compiler/generate_code/c_code.cc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/compiler/generate_code/c_code.cc b/src/compiler/generate_code/c_code.cc index 2bbe1642..dc01dbb3 100644 --- a/src/compiler/generate_code/c_code.cc +++ b/src/compiler/generate_code/c_code.cc @@ -134,7 +134,7 @@ class CCodeGenerator { line("static const char *ts_symbol_names[] = {"); indent([&]() { for (const auto &symbol : parse_table.symbols) - line("[" + symbol_id(symbol) + "] = \"" + symbol_name(symbol) + "\","); + line("[" + symbol_id(symbol) + "] = \"" + sanitize_name_for_string(symbol_name(symbol)) + "\","); }); line("};"); line(); @@ -409,6 +409,12 @@ class CCodeGenerator { indent(body); } + string sanitize_name_for_string(string name) { + util::str_replace(&name, "\n", "\\n"); + util::str_replace(&name, "\r", "\\r"); + return name; + } + string sanitize_name(string name) { auto existing = sanitized_names.find(name); if (existing != sanitized_names.end())