Sanitize line breaks in symbol names

This commit is contained in:
Max Brunsfeld 2015-10-26 13:36:13 -07:00
parent c885eea706
commit bad1bff3bb

View file

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