Don’t pass rule names to code generator separately from parse tables
This commit is contained in:
parent
b85b15db42
commit
5eb5b61c14
7 changed files with 41 additions and 44 deletions
20
spec/fixtures/parsers/arithmetic.c
vendored
20
spec/fixtures/parsers/arithmetic.c
vendored
|
|
@ -2,29 +2,29 @@
|
|||
#include <ctype.h>
|
||||
|
||||
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) {
|
||||
|
|
|
|||
44
spec/fixtures/parsers/json.c
vendored
44
spec/fixtures/parsers/json.c
vendored
|
|
@ -2,39 +2,39 @@
|
|||
#include <ctype.h>
|
||||
|
||||
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) {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -73,14 +73,12 @@ namespace tree_sitter {
|
|||
}
|
||||
|
||||
class CCodeGenerator {
|
||||
const vector<string> rule_names;
|
||||
const ParseTable parse_table;
|
||||
const LexTable lex_table;
|
||||
const string name;
|
||||
public:
|
||||
CCodeGenerator(string name, vector<string> 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<string> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
namespace tree_sitter {
|
||||
namespace generate_code {
|
||||
std::string c_code(std::string name, std::vector<std::string> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -64,6 +64,7 @@ namespace tree_sitter {
|
|||
static const std::string START;
|
||||
static const std::string END_OF_INPUT;
|
||||
std::vector<ParseState> states;
|
||||
std::unordered_set<std::string> symbol_names;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue