tree-sitter/src/compiler/lex_table.cc

75 lines
2.1 KiB
C++
Raw Normal View History

2014-03-09 21:37:21 -07:00
#include "compiler/lex_table.h"
#include "compiler/rules/symbol.h"
#include "compiler/rules/built_in_symbols.h"
2014-01-11 15:14:17 -08:00
namespace tree_sitter {
2014-03-09 19:49:35 -07:00
using std::function;
using std::string;
using std::to_string;
using std::map;
using std::set;
using rules::Symbol;
using rules::CharacterSet;
AdvanceAction::AdvanceAction() : state_index(-1) {}
AdvanceAction::AdvanceAction(size_t state_index,
PrecedenceRange precedence_range)
: state_index(state_index), precedence_range(precedence_range) {}
2014-03-09 19:49:35 -07:00
bool AdvanceAction::operator==(const AdvanceAction &other) const {
return (state_index == other.state_index) &&
(precedence_range == other.precedence_range);
}
2014-03-09 19:49:35 -07:00
AcceptTokenAction::AcceptTokenAction()
: symbol(rules::NONE()), precedence(0), is_string(false), is_fragile(false) {}
AcceptTokenAction::AcceptTokenAction(Symbol symbol, int precedence,
bool is_string)
: symbol(symbol),
precedence(precedence),
is_string(is_string),
is_fragile(false) {}
bool AcceptTokenAction::is_present() const {
return symbol != rules::NONE();
}
2014-03-09 19:49:35 -07:00
bool AcceptTokenAction::operator==(const AcceptTokenAction &other) const {
return (symbol == other.symbol) && (precedence == other.precedence) &&
(is_string == other.is_string) && (is_fragile == other.is_fragile);
}
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 : advance_actions)
result.insert(pair.first);
return result;
}
2014-03-09 19:49:35 -07:00
bool LexState::operator==(const LexState &other) const {
return advance_actions == other.advance_actions &&
accept_action == other.accept_action &&
is_token_start == other.is_token_start;
}
void LexState::each_advance_action(function<void(AdvanceAction *)> fn) {
for (auto &entry : advance_actions)
fn(&entry.second);
}
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) {
return states[id];
2014-04-28 21:46:43 -07:00
}
} // namespace tree_sitter