In parse table, store symbols as Symbol objects, not strings

This commit is contained in:
Max Brunsfeld 2014-01-27 12:40:06 -08:00
parent 0877d01194
commit ca33c3942a
20 changed files with 262 additions and 262 deletions

View file

@ -4,29 +4,30 @@ using std::string;
using std::ostream;
using std::to_string;
using std::unordered_set;
using tree_sitter::rules::Symbol;
namespace tree_sitter {
// Action
ParseAction::ParseAction(ParseActionType type, size_t state_index, string symbol_name, size_t child_symbol_count) :
type(type),
state_index(state_index),
symbol_name(symbol_name),
child_symbol_count(child_symbol_count) {};
ParseAction::ParseAction(ParseActionType type, size_t state_index, rules::Symbol symbol, size_t child_symbol_count) :
type(type),
state_index(state_index),
symbol(symbol),
child_symbol_count(child_symbol_count) {};
ParseAction ParseAction::Error() {
return ParseAction(ParseActionTypeError, -1, "", -1);
return ParseAction(ParseActionTypeError, -1, Symbol(""), -1);
}
ParseAction ParseAction::Accept() {
return ParseAction(ParseActionTypeAccept, -1, "", -1);
return ParseAction(ParseActionTypeAccept, -1, Symbol(""), -1);
}
ParseAction ParseAction::Shift(size_t state_index) {
return ParseAction(ParseActionTypeShift, state_index, "", -1);
return ParseAction(ParseActionTypeShift, state_index, Symbol(""), -1);
}
ParseAction ParseAction::Reduce(std::string symbol_name, size_t child_symbol_count) {
return ParseAction(ParseActionTypeReduce, -1, symbol_name, child_symbol_count);
ParseAction ParseAction::Reduce(Symbol symbol, size_t child_symbol_count) {
return ParseAction(ParseActionTypeReduce, -1, symbol, child_symbol_count);
}
bool ParseAction::operator==(const ParseAction &other) const {
@ -45,15 +46,15 @@ namespace tree_sitter {
case ParseActionTypeShift:
return stream << (string("#<shift ") + to_string(action.state_index) + ">");
case ParseActionTypeReduce:
return stream << (string("#<reduce ") + action.symbol_name + ">");
return stream << (string("#<reduce ") + action.symbol.name + ">");
}
}
// State
ParseState::ParseState() : lex_state_index(-1) {}
unordered_set<string> ParseState::expected_inputs() const {
unordered_set<string> result;
unordered_set<rules::Symbol> ParseState::expected_inputs() const {
unordered_set<rules::Symbol> result;
for (auto pair : actions)
result.insert(pair.first);
return result;
@ -84,8 +85,8 @@ namespace tree_sitter {
return states.size() - 1;
}
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);
void ParseTable::add_action(size_t state_index, rules::Symbol symbol, ParseAction action) {
symbols.insert(symbol);
states[state_index].actions[symbol].insert(action);
}
}