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())