diff --git a/src/compiler/generate_code/c_code.cc b/src/compiler/generate_code/c_code.cc index 869edbd3..4d8172f9 100644 --- a/src/compiler/generate_code/c_code.cc +++ b/src/compiler/generate_code/c_code.cc @@ -22,6 +22,38 @@ using std::to_string; using std::vector; using util::escape_char; +static const map REPLACEMENTS({ + { '=', "EQ" }, + { '\'', "SQUOTE" }, + { '"', "DQUOTE" }, + { '`', "BQUOTE" }, + { '.', "DOT" }, + { ',', "COMMA" }, + { ':', "COLON" }, + { ';', "SEMI" }, + { '(', "LPAREN" }, + { ')', "RPAREN" }, + { '<', "LT" }, + { '>', "GT" }, + { '{', "LBRACE" }, + { '}', "RBRACE" }, + { '[', "LBRACK" }, + { ']', "RBRACK" }, + { '&', "AMP" }, + { '|', "PIPE" }, + { '%', "PERCENT" }, + { '-', "DASH" }, + { '+', "PLUS" }, + { '*', "STAR" }, + { '~', "TILDE" }, + { '!', "BANG" }, + { '^', "CARET" }, + { '$', "DOLLAR" }, + { '@', "AT" }, + { '/', "SLASH" }, + { '\\', "BSLASH" }, +}); + class CCodeGenerator { string buffer; size_t indent_level; @@ -279,9 +311,9 @@ class CCodeGenerator { } else { string name = sanitize_name(rule_name(symbol)); if (symbol.is_auxiliary()) - return "ts_aux_sym_" + name; + return "aux_sym_" + name; else - return "ts_sym_" + name; + return "sym_" + name; } } @@ -338,6 +370,15 @@ class CCodeGenerator { if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || (c == '_')) { stripped_name += c; + continue; + } + + auto replacement = REPLACEMENTS.find(c); + if (replacement != REPLACEMENTS.end()) { + if (stripped_name[stripped_name.size() - 1] != '_') + stripped_name += "_"; + stripped_name += replacement->second; + continue; } } diff --git a/src/compiler/prepare_grammar/token_description.cc b/src/compiler/prepare_grammar/token_description.cc index 97a0eb08..54c00562 100644 --- a/src/compiler/prepare_grammar/token_description.cc +++ b/src/compiler/prepare_grammar/token_description.cc @@ -15,11 +15,11 @@ using std::string; class TokenDescription : public rules::RuleFn { string apply_to(const rules::Pattern *rule) { - return "/" + util::escape_string(rule->value) + "/"; + return "PAT_" + util::escape_string(rule->value); } string apply_to(const rules::String *rule) { - return "'" + util::escape_string(rule->value) + "'"; + return "STR_" + util::escape_string(rule->value); } string apply_to(const rules::Metadata *rule) { return apply(rule->rule); }