Remove unused methods and members
This commit is contained in:
parent
3417ad5adb
commit
2f74c1de10
3 changed files with 10 additions and 54 deletions
|
|
@ -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<string, size_t> symbol_ids;
|
||||
public:
|
||||
static unordered_map<string, size_t> get_symbol_ids(vector<string> rule_names) {
|
||||
size_t i = 0;
|
||||
unordered_map<string, size_t> 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) {
|
||||
|
|
|
|||
|
|
@ -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<string, unordered_set<ParseAction>>()) {}
|
||||
|
||||
// Table
|
||||
unordered_map<string, size_t> get_symbol_id_map(const vector<string> &names) {
|
||||
unordered_map<string, size_t> result;
|
||||
size_t i = 0;
|
||||
for (string name : names) {
|
||||
result[name] = i;
|
||||
i++;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
ParseTable::ParseTable(vector<string> symbol_names) :
|
||||
symbol_ids(get_symbol_id_map(symbol_names)),
|
||||
symbol_names(symbol_names),
|
||||
states(vector<ParseState>()) {};
|
||||
|
||||
|
|
@ -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<string, unordered_set<ParseAction>> ParseTable::actions_for(size_t state_index) const {
|
||||
return states[state_index].actions;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<ParseState> states;
|
||||
const std::unordered_map<std::string, size_t> symbol_ids;
|
||||
const std::vector<std::string> symbol_names;
|
||||
|
||||
ParseTable(std::vector<std::string> 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<std::string, std::unordered_set<ParseAction>> 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<ParseState> states;
|
||||
const std::vector<std::string> symbol_names;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue