diff --git a/src/compiler/code_gen/c_code.cpp b/src/compiler/code_gen/c_code.cpp index a0e9ca26..50aec939 100644 --- a/src/compiler/code_gen/c_code.cpp +++ b/src/compiler/code_gen/c_code.cpp @@ -30,7 +30,7 @@ namespace tree_sitter { string _switch(string condition, string body) { return "switch (" + condition + ") {\n" + - indent(body) + + indent(body) + "\n" "}"; } @@ -41,29 +41,16 @@ namespace tree_sitter { string _default(string body) { return "default:\n" + - indent(body) + "\n"; + indent(body); } class CCodeGenerator { const Grammar grammar; const ParseTable parse_table; - const unordered_map symbol_ids; public: - static unordered_map get_symbol_ids(vector rule_names) { - size_t i = 0; - unordered_map result; - for (string name : rule_names) { - result[name] = i; - i++; - } - result[ParseTable::END_OF_INPUT] = i; - return result; - } - CCodeGenerator(const Grammar &grammar, const ParseTable &parse_table) : grammar(grammar), - parse_table(parse_table), - symbol_ids(get_symbol_ids(grammar.rule_names())) + parse_table(parse_table) {} string symbol_id(string symbol_name) { diff --git a/src/compiler/lr/parse_table.cpp b/src/compiler/lr/parse_table.cpp index 9f1c2685..e830e037 100644 --- a/src/compiler/lr/parse_table.cpp +++ b/src/compiler/lr/parse_table.cpp @@ -5,12 +5,6 @@ using namespace std; namespace tree_sitter { namespace lr { // Action - ParseAction::ParseAction() : - type(ParseActionTypeError), - state_index(-1), - symbol_name(""), - child_symbol_count(-1) {}; - ParseAction::ParseAction(ParseActionType type, size_t state_index, string symbol_name, size_t child_symbol_count) : type(type), state_index(state_index), @@ -36,23 +30,20 @@ namespace tree_sitter { bool ParseAction::operator==(const ParseAction &other) const { bool types_eq = type == other.type; bool state_indices_eq = state_index == other.state_index; - bool symbol_ids_eq = symbol_name == other.symbol_name; bool child_symbol_counts_eq = child_symbol_count == other.child_symbol_count; - return types_eq && state_indices_eq && symbol_ids_eq && child_symbol_counts_eq; + return types_eq && state_indices_eq && child_symbol_counts_eq; } ostream& operator<<(ostream &stream, const ParseAction &action) { switch (action.type) { + case ParseActionTypeError: + return stream << string("error"); case ParseActionTypeAccept: return stream << string("accept"); - case ParseActionTypeAdvance: - return stream << (string("(advance ") + to_string(action.state_index) + ")"); case ParseActionTypeShift: return stream << (string("(shift ") + to_string(action.state_index) + ")"); case ParseActionTypeReduce: return stream << (string("(reduce ") + action.symbol_name + ")"); - case ParseActionTypeError: - return stream << string("error"); } } @@ -60,18 +51,7 @@ namespace tree_sitter { ParseState::ParseState() : actions(unordered_map>()) {} // Table - unordered_map get_symbol_id_map(const vector &names) { - unordered_map result; - size_t i = 0; - for (string name : names) { - result[name] = i; - i++; - } - return result; - } - ParseTable::ParseTable(vector symbol_names) : - symbol_ids(get_symbol_id_map(symbol_names)), symbol_names(symbol_names), states(vector()) {}; @@ -84,10 +64,6 @@ namespace tree_sitter { states[state_index].actions[sym_name].insert(action); } - ParseState ParseTable::starting_state() const { - return states[0]; - } - unordered_map> ParseTable::actions_for(size_t state_index) const { return states[state_index].actions; } diff --git a/src/compiler/lr/parse_table.h b/src/compiler/lr/parse_table.h index 0f9aab29..bcb87c8d 100644 --- a/src/compiler/lr/parse_table.h +++ b/src/compiler/lr/parse_table.h @@ -10,22 +10,20 @@ namespace tree_sitter { namespace lr { typedef enum { ParseActionTypeAccept, - ParseActionTypeAdvance, ParseActionTypeError, ParseActionTypeShift, ParseActionTypeReduce, } ParseActionType; class ParseAction { - public: - ParseAction(); ParseAction(ParseActionType type, size_t state_index, std::string symbol_name, size_t child_symbol_count); - bool operator==(const ParseAction &action) const; + public: static ParseAction Accept(); static ParseAction Advance(size_t state_index); static ParseAction Error(); static ParseAction Shift(size_t state_index); static ParseAction Reduce(std::string symbol_name, size_t child_symbol_count); + bool operator==(const ParseAction &action) const; ParseActionType type; size_t child_symbol_count; @@ -43,21 +41,16 @@ namespace tree_sitter { class ParseTable { public: - std::vector states; - const std::unordered_map symbol_ids; - const std::vector symbol_names; - ParseTable(std::vector rule_names); - ParseState starting_state() const; - ParseState get_state(size_t index) const; - ParseAction action_for(size_t state_index, std::string symbol_name) const; std::unordered_map> actions_for(size_t state_index) const; size_t add_state(); void add_action(size_t state_index, std::string symbol_name, ParseAction action); static const std::string START; static const std::string END_OF_INPUT; + std::vector states; + const std::vector symbol_names; }; } }