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-02-12 23:06:26 -08:00
|
|
|
using std::string;
|
|
|
|
|
using std::ostream;
|
|
|
|
|
using std::to_string;
|
|
|
|
|
using std::set;
|
|
|
|
|
using std::vector;
|
2014-04-22 23:38:26 -07:00
|
|
|
using rules::ISymbol;
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-03-09 23:51:33 -07:00
|
|
|
ParseAction::ParseAction(ParseActionType type,
|
|
|
|
|
size_t state_index,
|
2014-04-22 23:38:26 -07:00
|
|
|
ISymbol symbol,
|
2014-04-14 23:11:10 -07:00
|
|
|
size_t consumed_symbol_count,
|
|
|
|
|
set<int> precedence_values) :
|
2014-01-27 12:40:06 -08:00
|
|
|
type(type),
|
|
|
|
|
symbol(symbol),
|
2014-02-18 09:07:00 -08:00
|
|
|
state_index(state_index),
|
2014-04-14 23:11:10 -07:00
|
|
|
consumed_symbol_count(consumed_symbol_count),
|
|
|
|
|
precedence_values(precedence_values) {}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-04-08 20:34:29 -07:00
|
|
|
ParseAction::ParseAction() :
|
|
|
|
|
type(ParseActionTypeError),
|
2014-04-22 23:38:26 -07:00
|
|
|
symbol(ISymbol(-1)),
|
2014-04-08 20:34:29 -07:00
|
|
|
state_index(-1),
|
|
|
|
|
consumed_symbol_count(0) {}
|
2014-04-08 08:19:55 -07:00
|
|
|
|
2014-01-11 15:14:17 -08:00
|
|
|
ParseAction ParseAction::Error() {
|
2014-04-22 23:38:26 -07:00
|
|
|
return ParseAction(ParseActionTypeError, -1, ISymbol(-1), 0, { 0 });
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-11 15:14:17 -08:00
|
|
|
ParseAction ParseAction::Accept() {
|
2014-04-22 23:38:26 -07:00
|
|
|
return ParseAction(ParseActionTypeAccept, -1, ISymbol(-1), 0, { 0 });
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-04-14 23:11:10 -07:00
|
|
|
ParseAction ParseAction::Shift(size_t state_index, set<int> precedence_values) {
|
2014-04-22 23:38:26 -07:00
|
|
|
return ParseAction(ParseActionTypeShift, state_index, ISymbol(-1), 0, precedence_values);
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-04-22 23:38:26 -07:00
|
|
|
ParseAction ParseAction::Reduce(ISymbol symbol, size_t consumed_symbol_count, int precedence) {
|
2014-04-14 23:11:10 -07:00
|
|
|
return ParseAction(ParseActionTypeReduce, -1, symbol, consumed_symbol_count, { precedence });
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
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;
|
2014-03-26 21:02:53 -07:00
|
|
|
bool consumed_symbol_counts_eq = consumed_symbol_count == other.consumed_symbol_count;
|
|
|
|
|
return types_eq && state_indices_eq && consumed_symbol_counts_eq;
|
2014-02-10 18:38:01 -08:00
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-11 15:14:17 -08:00
|
|
|
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-04-22 23:38:26 -07:00
|
|
|
return stream << (string("#<reduce ") + to_string(action.symbol.index) + ">");
|
2014-04-08 20:34:29 -07:00
|
|
|
default:
|
|
|
|
|
return stream;
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
|
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-24 18:42:54 -08:00
|
|
|
ParseState::ParseState() : lex_state_id(-1) {}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-04-22 23:38:26 -07:00
|
|
|
set<ISymbol> ParseState::expected_inputs() const {
|
|
|
|
|
set<ISymbol> result;
|
2014-04-04 08:44:35 -07:00
|
|
|
for (auto &pair : actions)
|
2014-01-11 15:14:17 -08:00
|
|
|
result.insert(pair.first);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-26 16:38:41 -08:00
|
|
|
ostream& operator<<(ostream &stream, const ParseState &state) {
|
|
|
|
|
stream << string("#<parse_state ");
|
2014-03-24 19:18:06 -07:00
|
|
|
bool started = false;
|
2014-01-26 16:38:41 -08:00
|
|
|
for (auto pair : state.actions) {
|
2014-03-24 19:18:06 -07:00
|
|
|
if (started) stream << string(", ");
|
|
|
|
|
stream << pair.first << string(" => ") << pair.second;
|
|
|
|
|
started = true;
|
2014-01-26 16:38:41 -08:00
|
|
|
}
|
|
|
|
|
stream << string(">");
|
|
|
|
|
return stream;
|
|
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-24 18:42:54 -08:00
|
|
|
ParseStateId ParseTable::add_state() {
|
2014-01-11 15:14:17 -08:00
|
|
|
states.push_back(ParseState());
|
|
|
|
|
return states.size() - 1;
|
|
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-04-22 23:38:26 -07:00
|
|
|
void ParseTable::add_action(ParseStateId id, ISymbol symbol, ParseAction action) {
|
2014-01-27 12:40:06 -08:00
|
|
|
symbols.insert(symbol);
|
2014-04-14 23:11:10 -07:00
|
|
|
states[id].actions[symbol] = action;
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
|
|
|
|
}
|