2014-03-09 21:37:21 -07:00
|
|
|
#include "compiler/parse_table.h"
|
2014-03-09 22:45:33 -07:00
|
|
|
#include <string>
|
2014-01-11 15:14:17 -08:00
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
using std::ostream;
|
|
|
|
|
using std::to_string;
|
|
|
|
|
using std::set;
|
|
|
|
|
using std::vector;
|
|
|
|
|
using rules::Symbol;
|
|
|
|
|
|
|
|
|
|
ParseAction::ParseAction(ParseActionType type, ParseStateId state_index,
|
|
|
|
|
Symbol symbol, size_t consumed_symbol_count,
|
2015-02-21 10:41:23 -08:00
|
|
|
set<int> precedence_values,
|
2015-09-03 17:49:20 -07:00
|
|
|
Associativity associativity, int production_id)
|
2014-07-20 21:43:27 -07:00
|
|
|
: type(type),
|
|
|
|
|
symbol(symbol),
|
|
|
|
|
state_index(state_index),
|
|
|
|
|
consumed_symbol_count(consumed_symbol_count),
|
2015-02-21 10:41:23 -08:00
|
|
|
precedence_values(precedence_values),
|
2015-03-16 23:12:08 -07:00
|
|
|
associativity(associativity),
|
2015-02-21 10:41:23 -08:00
|
|
|
production_id(production_id) {}
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
ParseAction::ParseAction()
|
|
|
|
|
: type(ParseActionTypeError),
|
|
|
|
|
symbol(Symbol(-1)),
|
|
|
|
|
state_index(-1),
|
2015-03-16 23:12:08 -07:00
|
|
|
consumed_symbol_count(0),
|
2015-09-03 17:49:20 -07:00
|
|
|
associativity(AssociativityNone) {}
|
2014-07-20 21:43:27 -07:00
|
|
|
|
2015-07-31 16:32:24 -07:00
|
|
|
ParseAction ParseAction::Error() {
|
|
|
|
|
return ParseAction();
|
|
|
|
|
}
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
ParseAction ParseAction::Accept() {
|
2015-03-16 23:12:08 -07:00
|
|
|
ParseAction action;
|
|
|
|
|
action.type = ParseActionTypeAccept;
|
|
|
|
|
return action;
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ParseAction ParseAction::Shift(ParseStateId state_index,
|
|
|
|
|
set<int> precedence_values) {
|
|
|
|
|
return ParseAction(ParseActionTypeShift, state_index, Symbol(-1), 0,
|
2015-09-03 17:49:20 -07:00
|
|
|
precedence_values, AssociativityNone, -1);
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ParseAction ParseAction::ShiftExtra() {
|
2015-03-16 23:12:08 -07:00
|
|
|
ParseAction action;
|
|
|
|
|
action.type = ParseActionTypeShiftExtra;
|
|
|
|
|
return action;
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ParseAction ParseAction::ReduceExtra(Symbol symbol) {
|
2015-03-16 23:12:08 -07:00
|
|
|
ParseAction action;
|
|
|
|
|
action.type = ParseActionTypeReduceExtra;
|
|
|
|
|
action.symbol = symbol;
|
|
|
|
|
return action;
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ParseAction ParseAction::Reduce(Symbol symbol, size_t consumed_symbol_count,
|
2015-09-03 17:49:20 -07:00
|
|
|
int precedence, Associativity associativity,
|
2015-10-01 17:10:39 -07:00
|
|
|
unsigned int production_id) {
|
2014-07-20 21:43:27 -07:00
|
|
|
return ParseAction(ParseActionTypeReduce, 0, symbol, consumed_symbol_count,
|
2015-03-16 23:12:08 -07:00
|
|
|
{ precedence }, associativity, production_id);
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ParseAction::operator==(const ParseAction &other) const {
|
|
|
|
|
bool types_eq = type == other.type;
|
|
|
|
|
bool symbols_eq = symbol == other.symbol;
|
|
|
|
|
bool state_indices_eq = state_index == other.state_index;
|
|
|
|
|
bool consumed_symbol_counts_eq =
|
2015-07-27 18:29:48 -07:00
|
|
|
consumed_symbol_count == other.consumed_symbol_count;
|
2014-09-09 13:15:40 -07:00
|
|
|
return types_eq && symbols_eq && state_indices_eq && consumed_symbol_counts_eq;
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
2014-07-20 21:43:27 -07:00
|
|
|
|
2015-03-07 10:47:37 -08:00
|
|
|
bool ParseAction::operator<(const ParseAction &other) const {
|
2015-07-27 18:29:48 -07:00
|
|
|
if (type < other.type)
|
|
|
|
|
return true;
|
|
|
|
|
if (other.type < type)
|
|
|
|
|
return false;
|
|
|
|
|
if (symbol < other.symbol)
|
|
|
|
|
return true;
|
|
|
|
|
if (other.symbol < symbol)
|
|
|
|
|
return false;
|
|
|
|
|
if (state_index < other.state_index)
|
|
|
|
|
return true;
|
|
|
|
|
if (other.state_index < state_index)
|
|
|
|
|
return false;
|
2015-03-07 10:47:37 -08:00
|
|
|
return consumed_symbol_count < other.consumed_symbol_count;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
ParseState::ParseState() : lex_state_id(-1) {}
|
|
|
|
|
|
|
|
|
|
set<Symbol> ParseState::expected_inputs() const {
|
|
|
|
|
set<Symbol> result;
|
|
|
|
|
for (auto &pair : actions)
|
|
|
|
|
result.insert(pair.first);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ParseStateId ParseTable::add_state() {
|
|
|
|
|
states.push_back(ParseState());
|
|
|
|
|
return states.size() - 1;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-27 18:29:48 -07:00
|
|
|
ParseAction &ParseTable::set_action(ParseStateId id, Symbol symbol,
|
|
|
|
|
ParseAction action) {
|
2014-07-20 21:43:27 -07:00
|
|
|
symbols.insert(symbol);
|
2015-06-28 16:22:31 -05:00
|
|
|
states[id].actions[symbol] = vector<ParseAction>({ action });
|
|
|
|
|
return *states[id].actions[symbol].begin();
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-27 18:29:48 -07:00
|
|
|
ParseAction &ParseTable::add_action(ParseStateId id, Symbol symbol,
|
|
|
|
|
ParseAction action) {
|
2015-06-28 16:22:31 -05:00
|
|
|
symbols.insert(symbol);
|
|
|
|
|
states[id].actions[symbol].push_back(action);
|
|
|
|
|
return *states[id].actions[symbol].rbegin();
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace tree_sitter
|