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"
|
2017-03-17 12:52:01 -07:00
|
|
|
#include "compiler/rule.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::to_string;
|
|
|
|
|
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;
|
|
|
|
|
|
2017-07-21 09:54:22 -07:00
|
|
|
ParseAction::ParseAction() :
|
|
|
|
|
type(ParseActionTypeError),
|
|
|
|
|
state_index(-1),
|
|
|
|
|
symbol(rules::NONE()),
|
|
|
|
|
consumed_symbol_count(0),
|
|
|
|
|
precedence(0),
|
|
|
|
|
dynamic_precedence(0),
|
|
|
|
|
associativity(rules::AssociativityNone),
|
2017-07-31 11:45:24 -07:00
|
|
|
alias_sequence_id(0),
|
2017-07-21 09:54:22 -07:00
|
|
|
fragile(false),
|
|
|
|
|
extra(false) {}
|
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
|
|
|
}
|
|
|
|
|
|
2016-11-17 17:29:10 -08:00
|
|
|
ParseAction ParseAction::Shift(ParseStateId state_index) {
|
2017-07-06 15:20:11 -07:00
|
|
|
ParseAction result;
|
|
|
|
|
result.type = ParseActionTypeShift;
|
|
|
|
|
result.state_index = state_index;
|
|
|
|
|
return result;
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
|
|
|
|
|
2016-06-27 14:07:47 -07:00
|
|
|
ParseAction ParseAction::Recover(ParseStateId state_index) {
|
2017-07-06 15:20:11 -07:00
|
|
|
ParseAction result;
|
|
|
|
|
result.type = ParseActionTypeRecover;
|
|
|
|
|
result.state_index = state_index;
|
|
|
|
|
return result;
|
2016-06-27 14:07:47 -07:00
|
|
|
}
|
|
|
|
|
|
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::Reduce(Symbol symbol, size_t consumed_symbol_count,
|
2017-07-21 09:54:22 -07:00
|
|
|
int precedence, int dynamic_precedence,
|
2017-07-31 11:45:24 -07:00
|
|
|
rules::Associativity associativity, unsigned alias_sequence_id) {
|
2017-07-06 15:20:11 -07:00
|
|
|
ParseAction result;
|
|
|
|
|
result.type = ParseActionTypeReduce;
|
|
|
|
|
result.symbol = symbol;
|
|
|
|
|
result.consumed_symbol_count = consumed_symbol_count;
|
2017-07-21 09:54:22 -07:00
|
|
|
result.precedence = precedence;
|
|
|
|
|
result.dynamic_precedence = dynamic_precedence;
|
|
|
|
|
result.associativity = associativity;
|
2017-07-31 11:45:24 -07:00
|
|
|
result.alias_sequence_id = alias_sequence_id;
|
2017-07-06 15:20:11 -07:00
|
|
|
return result;
|
2016-11-17 17:29:10 -08:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
bool ParseAction::operator==(const ParseAction &other) const {
|
2017-07-13 17:17:22 -07:00
|
|
|
return
|
|
|
|
|
type == other.type &&
|
|
|
|
|
state_index == other.state_index &&
|
2017-07-21 09:54:22 -07:00
|
|
|
symbol == other.symbol &&
|
2017-07-13 17:17:22 -07:00
|
|
|
consumed_symbol_count == other.consumed_symbol_count &&
|
2017-07-21 09:54:22 -07:00
|
|
|
precedence == other.precedence &&
|
2017-07-20 15:28:55 -07:00
|
|
|
dynamic_precedence == other.dynamic_precedence &&
|
2017-07-21 09:54:22 -07:00
|
|
|
associativity == other.associativity &&
|
2017-07-31 11:45:24 -07:00
|
|
|
alias_sequence_id == other.alias_sequence_id &&
|
2017-07-21 09:54:22 -07:00
|
|
|
extra == other.extra &&
|
|
|
|
|
fragile == other.fragile;
|
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 {
|
2017-07-13 17:17:22 -07:00
|
|
|
if (type < other.type) return true;
|
|
|
|
|
if (other.type < type) return false;
|
|
|
|
|
if (state_index < other.state_index) return true;
|
|
|
|
|
if (other.state_index < state_index) return false;
|
2017-07-21 09:54:22 -07:00
|
|
|
if (symbol < other.symbol) return true;
|
|
|
|
|
if (other.symbol < symbol) return false;
|
2017-07-13 17:17:22 -07:00
|
|
|
if (consumed_symbol_count < other.consumed_symbol_count) return true;
|
|
|
|
|
if (other.consumed_symbol_count < consumed_symbol_count) return false;
|
2017-07-21 09:54:22 -07:00
|
|
|
if (precedence < other.precedence) return true;
|
|
|
|
|
if (other.precedence < precedence) return false;
|
2017-07-20 15:28:55 -07:00
|
|
|
if (dynamic_precedence < other.dynamic_precedence) return true;
|
|
|
|
|
if (other.dynamic_precedence < dynamic_precedence) return false;
|
2017-07-21 09:54:22 -07:00
|
|
|
if (associativity < other.associativity) return true;
|
|
|
|
|
if (other.associativity < associativity) return false;
|
|
|
|
|
if (extra && !other.extra) return true;
|
|
|
|
|
if (other.extra && !extra) return false;
|
|
|
|
|
if (fragile && !other.fragile) return true;
|
|
|
|
|
if (other.fragile && !fragile) return false;
|
2017-07-31 11:45:24 -07:00
|
|
|
return alias_sequence_id < other.alias_sequence_id;
|
2015-03-07 10:47:37 -08:00
|
|
|
}
|
|
|
|
|
|
2016-06-21 07:28:04 -07:00
|
|
|
ParseTableEntry::ParseTableEntry()
|
2017-07-13 17:17:22 -07:00
|
|
|
: reusable(true), depends_on_lookahead(false) {}
|
2016-06-21 07:28:04 -07:00
|
|
|
|
|
|
|
|
ParseTableEntry::ParseTableEntry(const vector<ParseAction> &actions,
|
|
|
|
|
bool reusable, bool depends_on_lookahead)
|
2017-07-13 17:17:22 -07:00
|
|
|
: actions(actions),
|
|
|
|
|
reusable(reusable),
|
|
|
|
|
depends_on_lookahead(depends_on_lookahead) {}
|
2016-06-21 07:28:04 -07:00
|
|
|
|
|
|
|
|
bool ParseTableEntry::operator==(const ParseTableEntry &other) const {
|
2017-07-13 17:17:22 -07:00
|
|
|
return
|
|
|
|
|
actions == other.actions &&
|
|
|
|
|
reusable == other.reusable &&
|
|
|
|
|
depends_on_lookahead == other.depends_on_lookahead;
|
2016-06-21 07:28:04 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
ParseState::ParseState() : lex_state_id(-1) {}
|
|
|
|
|
|
2016-09-06 10:22:16 -07:00
|
|
|
bool ParseState::has_shift_action() const {
|
2016-11-14 08:36:06 -08:00
|
|
|
for (const auto &pair : terminal_entries)
|
2016-10-05 14:02:49 -07:00
|
|
|
if (pair.second.actions.size() > 0 &&
|
|
|
|
|
pair.second.actions.back().type == ParseActionTypeShift)
|
2016-09-06 10:22:16 -07:00
|
|
|
return true;
|
2016-11-14 08:36:06 -08:00
|
|
|
return (!nonterminal_entries.empty());
|
2016-09-06 10:22:16 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-14 08:36:06 -08:00
|
|
|
void ParseState::each_referenced_state(function<void(ParseStateId *)> fn) {
|
|
|
|
|
for (auto &entry : terminal_entries)
|
2016-06-21 07:28:04 -07:00
|
|
|
for (ParseAction &action : entry.second.actions)
|
2017-06-09 16:25:40 -04:00
|
|
|
if ((action.type == ParseActionTypeShift && !action.extra) || action.type == ParseActionTypeRecover)
|
2016-11-14 08:36:06 -08:00
|
|
|
fn(&action.state_index);
|
|
|
|
|
for (auto &entry : nonterminal_entries)
|
2017-06-14 08:49:38 -04:00
|
|
|
fn(&entry.second);
|
2015-12-20 15:26:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ParseState::operator==(const ParseState &other) const {
|
2016-11-14 08:36:06 -08:00
|
|
|
return terminal_entries == other.terminal_entries &&
|
|
|
|
|
nonterminal_entries == other.nonterminal_entries;
|
2015-12-20 15:26:35 -08:00
|
|
|
}
|
|
|
|
|
|
2016-11-14 10:25:26 -08:00
|
|
|
ParseAction &ParseTable::add_terminal_action(ParseStateId state_id,
|
2016-11-30 09:34:47 -08:00
|
|
|
Symbol lookahead,
|
2016-11-14 08:36:06 -08:00
|
|
|
ParseAction action) {
|
2016-06-26 22:14:31 -07:00
|
|
|
if (action.type == ParseActionTypeShift && action.extra)
|
2016-11-30 09:34:47 -08:00
|
|
|
symbols[lookahead].extra = true;
|
2015-12-17 12:48:55 -08:00
|
|
|
else
|
2016-11-30 09:34:47 -08:00
|
|
|
symbols[lookahead].structural = true;
|
2016-02-12 23:44:05 -08:00
|
|
|
|
2016-11-30 09:34:47 -08:00
|
|
|
ParseTableEntry &entry = states[state_id].terminal_entries[lookahead];
|
2016-11-14 08:36:06 -08:00
|
|
|
entry.actions.push_back(action);
|
|
|
|
|
return *entry.actions.rbegin();
|
|
|
|
|
}
|
2016-02-12 23:44:05 -08:00
|
|
|
|
2016-11-14 10:25:26 -08:00
|
|
|
void ParseTable::set_nonterminal_action(ParseStateId state_id,
|
2016-11-30 09:34:47 -08:00
|
|
|
Symbol::Index lookahead,
|
2016-11-14 08:36:06 -08:00
|
|
|
ParseStateId next_state_id) {
|
2017-03-17 12:52:01 -07:00
|
|
|
symbols[Symbol::non_terminal(lookahead)].structural = true;
|
2016-11-30 09:34:47 -08:00
|
|
|
states[state_id].nonterminal_entries[lookahead] = next_state_id;
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace tree_sitter
|