2014-01-11 15:14:17 -08:00
|
|
|
#include "parse_table.h"
|
|
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
using std::ostream;
|
|
|
|
|
using std::to_string;
|
|
|
|
|
using std::unordered_set;
|
2014-01-27 12:40:06 -08:00
|
|
|
using tree_sitter::rules::Symbol;
|
2014-01-11 15:14:17 -08:00
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
|
|
|
|
// Action
|
2014-01-27 12:40:06 -08:00
|
|
|
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) {};
|
2014-01-11 15:14:17 -08:00
|
|
|
|
|
|
|
|
ParseAction ParseAction::Error() {
|
2014-01-27 12:40:06 -08:00
|
|
|
return ParseAction(ParseActionTypeError, -1, Symbol(""), -1);
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ParseAction ParseAction::Accept() {
|
2014-01-27 12:40:06 -08:00
|
|
|
return ParseAction(ParseActionTypeAccept, -1, Symbol(""), -1);
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ParseAction ParseAction::Shift(size_t state_index) {
|
2014-01-27 12:40:06 -08:00
|
|
|
return ParseAction(ParseActionTypeShift, state_index, Symbol(""), -1);
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
|
|
|
|
|
2014-01-27 12:40:06 -08:00
|
|
|
ParseAction ParseAction::Reduce(Symbol symbol, size_t child_symbol_count) {
|
|
|
|
|
return ParseAction(ParseActionTypeReduce, -1, symbol, child_symbol_count);
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ParseAction::operator==(const ParseAction &other) const {
|
|
|
|
|
bool types_eq = type == other.type;
|
|
|
|
|
bool state_indices_eq = state_index == other.state_index;
|
|
|
|
|
bool child_symbol_counts_eq = child_symbol_count == other.child_symbol_count;
|
|
|
|
|
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 ParseActionTypeShift:
|
|
|
|
|
return stream << (string("#<shift ") + to_string(action.state_index) + ">");
|
|
|
|
|
case ParseActionTypeReduce:
|
2014-01-27 12:40:06 -08:00
|
|
|
return stream << (string("#<reduce ") + action.symbol.name + ">");
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// State
|
|
|
|
|
ParseState::ParseState() : lex_state_index(-1) {}
|
|
|
|
|
|
2014-01-27 12:40:06 -08:00
|
|
|
unordered_set<rules::Symbol> ParseState::expected_inputs() const {
|
|
|
|
|
unordered_set<rules::Symbol> result;
|
2014-01-11 15:14:17 -08:00
|
|
|
for (auto pair : actions)
|
|
|
|
|
result.insert(pair.first);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-26 16:38:41 -08:00
|
|
|
ostream& operator<<(ostream &stream, const ParseState &state) {
|
|
|
|
|
stream << string("#<parse_state ");
|
|
|
|
|
bool started1 = false;
|
|
|
|
|
for (auto pair : state.actions) {
|
|
|
|
|
if (started1) stream << string(", ");
|
|
|
|
|
stream << pair.first << string(" => #<set: ");
|
|
|
|
|
bool started2 = false;
|
|
|
|
|
for (auto action : pair.second) {
|
|
|
|
|
if (started2) stream << string(", ");
|
|
|
|
|
stream << action;
|
|
|
|
|
started2 = true;
|
|
|
|
|
}
|
|
|
|
|
stream << string(">");
|
|
|
|
|
started1 = true;
|
|
|
|
|
}
|
|
|
|
|
stream << string(">");
|
|
|
|
|
return stream;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-11 15:14:17 -08:00
|
|
|
// Table
|
|
|
|
|
size_t ParseTable::add_state() {
|
|
|
|
|
states.push_back(ParseState());
|
|
|
|
|
return states.size() - 1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-27 12:40:06 -08:00
|
|
|
void ParseTable::add_action(size_t state_index, rules::Symbol symbol, ParseAction action) {
|
|
|
|
|
symbols.insert(symbol);
|
|
|
|
|
states[state_index].actions[symbol].insert(action);
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
|
|
|
|
}
|