2014-03-09 21:37:21 -07:00
|
|
|
#include "compiler/parse_table.h"
|
2014-03-09 22:45:33 -07:00
|
|
|
#include <string>
|
2015-10-12 17:29:02 -07:00
|
|
|
#include "compiler/precedence_range.h"
|
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;
|
2015-12-20 15:26:35 -08:00
|
|
|
using std::function;
|
2014-07-20 21:43:27 -07:00
|
|
|
using rules::Symbol;
|
|
|
|
|
|
|
|
|
|
ParseAction::ParseAction(ParseActionType type, ParseStateId state_index,
|
|
|
|
|
Symbol symbol, size_t consumed_symbol_count,
|
2015-10-05 16:05:19 -07:00
|
|
|
PrecedenceRange precedence_range,
|
2015-10-17 22:54:56 -07:00
|
|
|
rules::Associativity associativity,
|
|
|
|
|
const Production *production)
|
2014-07-20 21:43:27 -07:00
|
|
|
: type(type),
|
2015-12-17 12:48:55 -08:00
|
|
|
extra(false),
|
|
|
|
|
fragile(false),
|
2014-07-20 21:43:27 -07:00
|
|
|
symbol(symbol),
|
|
|
|
|
state_index(state_index),
|
|
|
|
|
consumed_symbol_count(consumed_symbol_count),
|
2015-10-05 16:05:19 -07:00
|
|
|
precedence_range(precedence_range),
|
2015-03-16 23:12:08 -07:00
|
|
|
associativity(associativity),
|
2015-10-17 22:54:56 -07:00
|
|
|
production(production) {}
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
ParseAction::ParseAction()
|
|
|
|
|
: type(ParseActionTypeError),
|
2015-12-17 12:48:55 -08:00
|
|
|
extra(false),
|
|
|
|
|
fragile(false),
|
2014-07-20 21:43:27 -07:00
|
|
|
symbol(Symbol(-1)),
|
|
|
|
|
state_index(-1),
|
2015-03-16 23:12:08 -07:00
|
|
|
consumed_symbol_count(0),
|
2016-01-21 23:52:05 -07:00
|
|
|
associativity(rules::AssociativityNone),
|
|
|
|
|
production(nullptr) {}
|
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,
|
2015-10-05 16:05:19 -07:00
|
|
|
PrecedenceRange precedence_range) {
|
2014-07-20 21:43:27 -07:00
|
|
|
return ParseAction(ParseActionTypeShift, state_index, Symbol(-1), 0,
|
2015-10-17 22:54:56 -07:00
|
|
|
precedence_range, rules::AssociativityNone, nullptr);
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
|
|
|
|
|
2016-06-27 14:07:47 -07:00
|
|
|
ParseAction ParseAction::Recover(ParseStateId state_index) {
|
|
|
|
|
return ParseAction(ParseActionTypeRecover, state_index, Symbol(-1), 0,
|
|
|
|
|
PrecedenceRange(), rules::AssociativityNone, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
ParseAction ParseAction::ShiftExtra() {
|
2015-03-16 23:12:08 -07:00
|
|
|
ParseAction action;
|
2015-12-17 12:48:55 -08:00
|
|
|
action.type = ParseActionTypeShift;
|
|
|
|
|
action.extra = true;
|
2015-03-16 23:12:08 -07:00
|
|
|
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;
|
2015-12-17 12:48:55 -08:00
|
|
|
action.type = ParseActionTypeReduce;
|
|
|
|
|
action.extra = true;
|
2015-03-16 23:12:08 -07:00
|
|
|
action.symbol = symbol;
|
2015-12-17 12:48:55 -08:00
|
|
|
action.consumed_symbol_count = 1;
|
2015-03-16 23:12:08 -07:00
|
|
|
return action;
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ParseAction ParseAction::Reduce(Symbol symbol, size_t consumed_symbol_count,
|
2015-10-13 11:23:02 -07:00
|
|
|
int precedence,
|
|
|
|
|
rules::Associativity associativity,
|
2015-10-17 22:54:56 -07:00
|
|
|
const Production &production) {
|
2014-07-20 21:43:27 -07:00
|
|
|
return ParseAction(ParseActionTypeReduce, 0, symbol, consumed_symbol_count,
|
2015-10-17 22:54:56 -07:00
|
|
|
{ precedence, precedence }, associativity, &production);
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ParseAction::operator==(const ParseAction &other) const {
|
2015-12-17 12:48:55 -08:00
|
|
|
return (type == other.type && extra == other.extra &&
|
2016-06-21 07:28:04 -07:00
|
|
|
fragile == other.fragile && symbol == other.symbol &&
|
|
|
|
|
state_index == other.state_index && production == other.production &&
|
2015-12-17 12:48:55 -08:00
|
|
|
consumed_symbol_count == other.consumed_symbol_count);
|
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;
|
2015-12-17 12:48:55 -08:00
|
|
|
if (extra && !other.extra)
|
|
|
|
|
return true;
|
|
|
|
|
if (other.extra && !extra)
|
|
|
|
|
return false;
|
|
|
|
|
if (fragile && !other.fragile)
|
|
|
|
|
return true;
|
|
|
|
|
if (other.fragile && !fragile)
|
|
|
|
|
return false;
|
2015-07-27 18:29:48 -07:00
|
|
|
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-12-09 14:23:19 -08:00
|
|
|
if (production < other.production)
|
|
|
|
|
return true;
|
|
|
|
|
if (other.production < production)
|
|
|
|
|
return false;
|
2015-03-07 10:47:37 -08:00
|
|
|
return consumed_symbol_count < other.consumed_symbol_count;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-21 07:28:04 -07:00
|
|
|
ParseTableEntry::ParseTableEntry()
|
|
|
|
|
: reusable(true), depends_on_lookahead(false) {}
|
|
|
|
|
|
|
|
|
|
ParseTableEntry::ParseTableEntry(const vector<ParseAction> &actions,
|
|
|
|
|
bool reusable, bool depends_on_lookahead)
|
|
|
|
|
: actions(actions),
|
|
|
|
|
reusable(reusable),
|
|
|
|
|
depends_on_lookahead(depends_on_lookahead) {}
|
|
|
|
|
|
|
|
|
|
bool ParseTableEntry::operator==(const ParseTableEntry &other) const {
|
|
|
|
|
return actions == other.actions && reusable == other.reusable &&
|
|
|
|
|
depends_on_lookahead == other.depends_on_lookahead;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
ParseState::ParseState() : lex_state_id(-1) {}
|
|
|
|
|
|
|
|
|
|
set<Symbol> ParseState::expected_inputs() const {
|
|
|
|
|
set<Symbol> result;
|
2016-06-21 07:28:04 -07:00
|
|
|
for (auto &entry : entries)
|
|
|
|
|
result.insert(entry.first);
|
2014-07-20 21:43:27 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-22 22:16:47 -07:00
|
|
|
void ParseState::each_advance_action(function<void(ParseAction *)> fn) {
|
2016-06-21 07:28:04 -07:00
|
|
|
for (auto &entry : entries)
|
|
|
|
|
for (ParseAction &action : entry.second.actions)
|
2016-06-27 14:07:47 -07:00
|
|
|
if (action.type == ParseActionTypeShift || ParseActionTypeRecover)
|
2016-01-22 22:16:47 -07:00
|
|
|
fn(&action);
|
2015-12-20 15:26:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ParseState::operator==(const ParseState &other) const {
|
2016-06-21 07:28:04 -07:00
|
|
|
return entries == other.entries;
|
2015-12-20 15:26:35 -08:00
|
|
|
}
|
|
|
|
|
|
2015-12-02 07:36:31 -08:00
|
|
|
set<Symbol> ParseTable::all_symbols() const {
|
|
|
|
|
set<Symbol> result;
|
|
|
|
|
for (auto &pair : symbols)
|
|
|
|
|
result.insert(pair.first);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
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) {
|
2016-06-26 22:14:31 -07:00
|
|
|
if (action.type == ParseActionTypeShift && action.extra)
|
2016-06-21 07:28:04 -07:00
|
|
|
symbols[symbol].extra = true;
|
2015-12-17 12:48:55 -08:00
|
|
|
else
|
|
|
|
|
symbols[symbol].structural = true;
|
2016-06-21 07:28:04 -07:00
|
|
|
|
|
|
|
|
states[id].entries[symbol].actions = { action };
|
|
|
|
|
return *states[id].entries[symbol].actions.begin();
|
2015-06-28 16:22:31 -05:00
|
|
|
}
|
|
|
|
|
|
2015-07-27 18:29:48 -07:00
|
|
|
ParseAction &ParseTable::add_action(ParseStateId id, Symbol symbol,
|
|
|
|
|
ParseAction action) {
|
2016-06-26 22:14:31 -07:00
|
|
|
if (action.type == ParseActionTypeShift && action.extra)
|
2016-06-21 07:28:04 -07:00
|
|
|
symbols[symbol].extra = true;
|
2015-12-17 12:48:55 -08:00
|
|
|
else
|
|
|
|
|
symbols[symbol].structural = true;
|
2016-02-12 23:44:05 -08:00
|
|
|
|
2016-06-21 07:28:04 -07:00
|
|
|
ParseState &state = states[id];
|
|
|
|
|
for (ParseAction &existing_action : state.entries[symbol].actions)
|
2016-02-12 23:44:05 -08:00
|
|
|
if (existing_action == action)
|
|
|
|
|
return existing_action;
|
|
|
|
|
|
2016-06-21 07:28:04 -07:00
|
|
|
state.entries[symbol].actions.push_back(action);
|
|
|
|
|
return *state.entries[symbol].actions.rbegin();
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
|
|
|
|
|
2016-06-29 09:54:08 -07:00
|
|
|
static bool has_entry(const ParseState &state, const ParseTableEntry &entry) {
|
|
|
|
|
for (const auto &pair : state.entries)
|
|
|
|
|
if (pair.second == entry)
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ParseTable::merge_state(size_t i, size_t j) {
|
|
|
|
|
ParseState &state = states[i];
|
|
|
|
|
ParseState &other = states[j];
|
|
|
|
|
|
|
|
|
|
for (auto &entry : state.entries) {
|
|
|
|
|
const Symbol &symbol = entry.first;
|
|
|
|
|
const vector<ParseAction> &actions = entry.second.actions;
|
|
|
|
|
|
|
|
|
|
const auto &other_entry = other.entries.find(symbol);
|
|
|
|
|
if (other_entry == other.entries.end()) {
|
|
|
|
|
if (actions.back().type != ParseActionTypeReduce)
|
|
|
|
|
return false;
|
|
|
|
|
if (!has_entry(other, entry.second))
|
|
|
|
|
return false;
|
|
|
|
|
} else if (entry.second != other_entry->second) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set<Symbol> symbols_to_merge;
|
|
|
|
|
|
|
|
|
|
for (auto &entry : other.entries) {
|
|
|
|
|
const Symbol &symbol = entry.first;
|
|
|
|
|
const vector<ParseAction> &actions = entry.second.actions;
|
|
|
|
|
|
|
|
|
|
if (!state.entries.count(symbol)) {
|
|
|
|
|
if (actions.back().type != ParseActionTypeReduce)
|
|
|
|
|
return false;
|
|
|
|
|
if (!has_entry(state, entry.second))
|
|
|
|
|
return false;
|
|
|
|
|
symbols_to_merge.insert(symbol);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const Symbol &symbol : symbols_to_merge)
|
|
|
|
|
state.entries[symbol] = other.entries.find(symbol)->second;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
} // namespace tree_sitter
|