diff --git a/spec/fixtures/parsers/arithmetic.c b/spec/fixtures/parsers/arithmetic.c index 3a0a0cdd..2a253996 100644 --- a/spec/fixtures/parsers/arithmetic.c +++ b/spec/fixtures/parsers/arithmetic.c @@ -2,29 +2,29 @@ #include enum ts_symbol { - ts_symbol_expression, ts_symbol_factor, - ts_symbol_term, - ts_symbol_1, - ts_symbol_2, - ts_symbol_times, ts_symbol_plus, ts_symbol_number, ts_symbol___END__, ts_symbol_variable, + ts_symbol_1, + ts_symbol_2, + ts_symbol_term, + ts_symbol_times, + ts_symbol_expression, }; static const char *ts_symbol_names[] = { - "expression", "factor", - "term", - "1", - "2", - "times", "plus", "number", "__END__", "variable", + "1", + "2", + "term", + "times", + "expression", }; static void ts_lex(TSParser *parser) { diff --git a/spec/fixtures/parsers/json.c b/spec/fixtures/parsers/json.c index a4e7f189..f4806708 100644 --- a/spec/fixtures/parsers/json.c +++ b/spec/fixtures/parsers/json.c @@ -2,39 +2,39 @@ #include enum ts_symbol { - ts_symbol_repeat_helper1, - ts_symbol_value, + ts_symbol_number, + ts_symbol_string, + ts_symbol_array, + ts_symbol_7, ts_symbol_object, ts_symbol_repeat_helper2, - ts_symbol_array, - ts_symbol___END__, - ts_symbol_1, - ts_symbol_2, - ts_symbol_4, - ts_symbol_7, - ts_symbol_5, ts_symbol_6, - ts_symbol_number, + ts_symbol_4, + ts_symbol_repeat_helper1, ts_symbol_3, - ts_symbol_string, + ts_symbol_2, + ts_symbol_5, + ts_symbol_1, + ts_symbol_value, + ts_symbol___END__, }; static const char *ts_symbol_names[] = { - "repeat_helper1", - "value", + "number", + "string", + "array", + "7", "object", "repeat_helper2", - "array", - "__END__", - "1", - "2", - "4", - "7", - "5", "6", - "number", + "4", + "repeat_helper1", "3", - "string", + "2", + "5", + "1", + "value", + "__END__", }; static void ts_lex(TSParser *parser) { diff --git a/src/compiler/compile.cpp b/src/compiler/compile.cpp index 4d980311..a344368a 100644 --- a/src/compiler/compile.cpp +++ b/src/compiler/compile.cpp @@ -9,9 +9,6 @@ namespace tree_sitter { std::string compile(const Grammar &grammar, std::string name) { auto grammars = prepare_grammar::perform(grammar); auto tables = build_tables::perform(grammars.first, grammars.second); - auto rule_names = grammars.first.rule_names(); - auto token_names = grammars.second.rule_names(); - rule_names.insert(rule_names.end(), token_names.begin(), token_names.end()); - return generate_code::c_code(name, rule_names, tables.first, tables.second); + return generate_code::c_code(name, tables.first, tables.second); } } diff --git a/src/compiler/generate_code/c_code.cpp b/src/compiler/generate_code/c_code.cpp index daf87ea2..c90d9172 100644 --- a/src/compiler/generate_code/c_code.cpp +++ b/src/compiler/generate_code/c_code.cpp @@ -73,14 +73,12 @@ namespace tree_sitter { } class CCodeGenerator { - const vector rule_names; const ParseTable parse_table; const LexTable lex_table; const string name; public: - CCodeGenerator(string name, vector rule_names, const ParseTable &parse_table, const LexTable &lex_table) : + CCodeGenerator(string name, const ParseTable &parse_table, const LexTable &lex_table) : name(name), - rule_names(rule_names), parse_table(parse_table), lex_table(lex_table) {} @@ -217,14 +215,14 @@ namespace tree_sitter { string symbol_enum() { string result = "enum ts_symbol {\n"; - for (string rule_name : rule_names) + for (string rule_name : parse_table.symbol_names) result += indent(symbol_id(rule_name)) + ",\n"; return result + "};"; } string rule_names_list() { string result = "static const char *ts_symbol_names[] = {\n"; - for (string rule_name : rule_names) + for (string rule_name : parse_table.symbol_names) result += indent(string("\"") + rule_name) + "\",\n"; return result + "};"; } @@ -277,8 +275,8 @@ namespace tree_sitter { } }; - string c_code(string name, const vector rule_names, const ParseTable &parse_table, const LexTable &lex_table) { - return CCodeGenerator(name, rule_names, parse_table, lex_table).code(); + string c_code(string name, const ParseTable &parse_table, const LexTable &lex_table) { + return CCodeGenerator(name, parse_table, lex_table).code(); } } } \ No newline at end of file diff --git a/src/compiler/generate_code/c_code.h b/src/compiler/generate_code/c_code.h index 9b13d62c..8d093d65 100644 --- a/src/compiler/generate_code/c_code.h +++ b/src/compiler/generate_code/c_code.h @@ -7,7 +7,7 @@ namespace tree_sitter { namespace generate_code { - std::string c_code(std::string name, std::vector rule_names, const ParseTable &parse_table, const LexTable &lex_table); + std::string c_code(std::string name, const ParseTable &parse_table, const LexTable &lex_table); } } diff --git a/src/compiler/parse_table.cpp b/src/compiler/parse_table.cpp index a36b830f..3df9675e 100644 --- a/src/compiler/parse_table.cpp +++ b/src/compiler/parse_table.cpp @@ -66,6 +66,7 @@ namespace tree_sitter { } void ParseTable::add_action(size_t state_index, string sym_name, ParseAction action) { + symbol_names.insert(sym_name); states[state_index].actions[sym_name].insert(action); } diff --git a/src/compiler/parse_table.h b/src/compiler/parse_table.h index eebc83c4..1b597c5f 100644 --- a/src/compiler/parse_table.h +++ b/src/compiler/parse_table.h @@ -64,6 +64,7 @@ namespace tree_sitter { static const std::string START; static const std::string END_OF_INPUT; std::vector states; + std::unordered_set symbol_names; }; }