2014-03-09 21:37:21 -07:00
|
|
|
#include "compiler/lex_table.h"
|
2014-04-28 20:43:27 -07:00
|
|
|
#include "compiler/rules/symbol.h"
|
2014-01-11 15:14:17 -08:00
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
2014-02-12 23:06:26 -08:00
|
|
|
using std::string;
|
|
|
|
|
using std::to_string;
|
|
|
|
|
using std::map;
|
|
|
|
|
using std::set;
|
2014-04-28 20:43:27 -07:00
|
|
|
using rules::Symbol;
|
2014-02-12 23:06:26 -08:00
|
|
|
using rules::CharacterSet;
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-04-08 20:34:29 -07:00
|
|
|
LexAction::LexAction() :
|
|
|
|
|
type(LexActionTypeError),
|
2014-04-28 20:43:27 -07:00
|
|
|
symbol(Symbol(-1)),
|
2014-05-30 13:29:54 -07:00
|
|
|
state_index(-1),
|
|
|
|
|
precedence_values({0}) {}
|
2014-03-24 19:18:06 -07:00
|
|
|
|
2014-05-30 13:29:54 -07:00
|
|
|
LexAction::LexAction(LexActionType type, size_t state_index, Symbol symbol, set<int> precedence_values) :
|
2014-01-27 12:40:06 -08:00
|
|
|
type(type),
|
2014-02-18 09:07:00 -08:00
|
|
|
symbol(symbol),
|
2014-05-01 13:25:20 -07:00
|
|
|
state_index(state_index),
|
2014-05-30 13:29:54 -07:00
|
|
|
precedence_values(precedence_values) {}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-11 15:14:17 -08:00
|
|
|
LexAction LexAction::Error() {
|
2014-05-30 13:29:54 -07:00
|
|
|
return LexAction(LexActionTypeError, -1, Symbol(-1), {0});
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-05-30 13:29:54 -07:00
|
|
|
LexAction LexAction::Advance(size_t state_index, set<int> precedence_values) {
|
|
|
|
|
return LexAction(LexActionTypeAdvance, state_index, Symbol(-1), precedence_values);
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-05-01 13:25:20 -07:00
|
|
|
LexAction LexAction::Accept(Symbol symbol, int precedence) {
|
2014-05-30 13:29:54 -07:00
|
|
|
return LexAction(LexActionTypeAccept, -1, symbol, { 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 LexAction::operator==(const LexAction &other) const {
|
|
|
|
|
return
|
|
|
|
|
(type == other.type) &&
|
|
|
|
|
(state_index == other.state_index) &&
|
2014-01-27 12:40:06 -08:00
|
|
|
(symbol == other.symbol);
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-11 15:14:17 -08:00
|
|
|
std::ostream& operator<<(std::ostream &stream, const LexAction &action) {
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
case LexActionTypeError:
|
|
|
|
|
return stream << string("#<error>");
|
|
|
|
|
case LexActionTypeAccept:
|
2014-04-22 23:38:26 -07:00
|
|
|
return stream << string("#<accept ") + to_string(action.symbol.index) + ">";
|
2014-01-11 15:14:17 -08:00
|
|
|
case LexActionTypeAdvance:
|
|
|
|
|
return stream << string("#<advance ") + to_string(action.state_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-10 18:38:01 -08:00
|
|
|
set<CharacterSet> LexState::expected_inputs() const {
|
|
|
|
|
set<CharacterSet> result;
|
2014-02-12 23:06:26 -08: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-02-24 18:42:54 -08:00
|
|
|
LexStateId LexTable::add_state() {
|
2014-01-11 15:14:17 -08:00
|
|
|
states.push_back(LexState());
|
|
|
|
|
return states.size() - 1;
|
|
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-04-03 19:10:09 -07:00
|
|
|
LexState & LexTable::state(LexStateId id) {
|
2014-02-24 18:42:54 -08:00
|
|
|
if (id < 0)
|
2014-04-03 19:10:09 -07:00
|
|
|
return error_state;
|
2014-02-24 18:42:54 -08:00
|
|
|
else
|
2014-04-03 19:10:09 -07:00
|
|
|
return states[id];
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-24 18:42:54 -08:00
|
|
|
const LexStateId LexTable::ERROR_STATE_ID = -1;
|
2014-04-28 21:46:43 -07:00
|
|
|
}
|