tree-sitter/src/compiler/lex_table.cc

68 lines
1.6 KiB
C++
Raw Normal View History

2014-03-09 21:37:21 -07:00
#include "compiler/lex_table.h"
#include "compiler/rules/symbol.h"
2014-01-11 15:14:17 -08:00
namespace tree_sitter {
2014-03-09 19:49:35 -07:00
using std::string;
using std::to_string;
using std::map;
using std::set;
using rules::Symbol;
using rules::CharacterSet;
LexAction::LexAction()
: type(LexActionTypeError),
symbol(Symbol(-1)),
state_index(-1),
precedence_values({ 0 }) {}
2014-03-09 19:49:35 -07:00
LexAction::LexAction(LexActionType type, size_t state_index, Symbol symbol,
set<int> precedence_values)
: type(type),
symbol(symbol),
state_index(state_index),
precedence_values(precedence_values) {}
2014-03-09 19:49:35 -07:00
LexAction LexAction::Error() {
return LexAction(LexActionTypeError, -1, Symbol(-1), { 0 });
}
2014-03-09 19:49:35 -07:00
LexAction LexAction::Advance(size_t state_index, set<int> precedence_values) {
return LexAction(LexActionTypeAdvance, state_index, Symbol(-1),
precedence_values);
}
2014-03-09 19:49:35 -07:00
LexAction LexAction::Accept(Symbol symbol, int precedence) {
return LexAction(LexActionTypeAccept, -1, symbol, { precedence });
}
2014-03-09 19:49:35 -07:00
bool LexAction::operator==(const LexAction &other) const {
return (type == other.type) && (state_index == other.state_index) &&
(symbol == other.symbol);
}
2014-03-09 19:49:35 -07:00
2015-02-13 22:16:27 -08:00
LexState::LexState() : is_token_start(false) {}
set<CharacterSet> LexState::expected_inputs() const {
set<CharacterSet> result;
for (auto &pair : actions)
result.insert(pair.first);
return result;
}
2014-03-09 19:49:35 -07:00
LexStateId LexTable::add_state() {
states.push_back(LexState());
return states.size() - 1;
}
2014-03-09 19:49:35 -07:00
LexState &LexTable::state(LexStateId id) {
if (id < 0)
return error_state;
else
return states[id];
2014-04-28 21:46:43 -07:00
}
const LexStateId LexTable::ERROR_STATE_ID = -1;
} // namespace tree_sitter