Start work on lexing
This commit is contained in:
parent
323184f981
commit
a5e39d2512
20 changed files with 719 additions and 334 deletions
67
src/compiler/lr/lex_table.cpp
Normal file
67
src/compiler/lr/lex_table.cpp
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#include "lex_table.h"
|
||||
|
||||
using std::string;
|
||||
using std::to_string;
|
||||
using std::unordered_map;
|
||||
using std::unordered_set;
|
||||
using std::vector;
|
||||
|
||||
namespace tree_sitter {
|
||||
namespace lr {
|
||||
|
||||
// Action
|
||||
LexAction::LexAction(LexActionType type, size_t state_index, std::string symbol_name) :
|
||||
type(type),
|
||||
state_index(state_index),
|
||||
symbol_name(symbol_name) {}
|
||||
|
||||
LexAction LexAction::Error() {
|
||||
return LexAction(LexActionTypeError, -1, "");
|
||||
}
|
||||
|
||||
LexAction LexAction::Advance(size_t state_index) {
|
||||
return LexAction(LexActionTypeAdvance, state_index, "");
|
||||
}
|
||||
|
||||
LexAction LexAction::Accept(std::string symbol_name) {
|
||||
return LexAction(LexActionTypeAccept, -1, symbol_name);
|
||||
}
|
||||
|
||||
bool LexAction::operator==(const LexAction &other) const {
|
||||
return
|
||||
(type == other.type) &&
|
||||
(state_index == other.state_index) &&
|
||||
(symbol_name == other.symbol_name);
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream &stream, const LexAction &action) {
|
||||
switch (action.type) {
|
||||
case LexActionTypeError:
|
||||
return stream << string("(error)");
|
||||
case LexActionTypeAccept:
|
||||
return stream << string("(accept ") + action.symbol_name + ")";
|
||||
case LexActionTypeAdvance:
|
||||
return stream << string("(advance ") + to_string(action.state_index) + ")";
|
||||
}
|
||||
}
|
||||
|
||||
// State
|
||||
LexState::LexState() : actions(unordered_map<CharMatch, unordered_set<LexAction>>()) {}
|
||||
|
||||
// Table
|
||||
LexTable::LexTable(vector<string> rule_names) : symbol_names(rule_names) {}
|
||||
|
||||
size_t LexTable::add_state() {
|
||||
states.push_back(LexState());
|
||||
return states.size() - 1;
|
||||
}
|
||||
|
||||
void LexTable::add_action(size_t state_index, CharMatch match, LexAction action) {
|
||||
states[state_index].actions[match].insert(action);
|
||||
}
|
||||
|
||||
void LexTable::add_default_action(size_t state_index, LexAction action) {
|
||||
states[state_index].default_actions.insert(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue