tree-sitter/src/compiler/lex_table.h

65 lines
1.7 KiB
C
Raw Normal View History

2014-01-11 15:14:17 -08:00
#ifndef __TreeSitter__lex_table__
#define __TreeSitter__lex_table__
#include <vector>
#include <string>
#include <set>
#include <map>
#include "symbol.h"
#include "character_set.h"
2014-01-11 15:14:17 -08:00
namespace tree_sitter {
typedef enum {
LexActionTypeAccept,
LexActionTypeError,
LexActionTypeAdvance
} LexActionType;
class LexAction {
LexAction(LexActionType type, size_t state_index, rules::Symbol symbol);
2014-01-11 15:14:17 -08:00
public:
static LexAction Accept(rules::Symbol symbol);
2014-01-11 15:14:17 -08:00
static LexAction Error();
static LexAction Advance(size_t state_index);
bool operator==(const LexAction &action) const;
bool operator<(const LexAction &action) const;
2014-01-11 15:14:17 -08:00
LexActionType type;
rules::Symbol symbol;
2014-01-11 15:14:17 -08:00
size_t state_index;
};
std::ostream& operator<<(std::ostream &stream, const LexAction &item);
}
namespace std {
template<>
struct hash<tree_sitter::LexAction> {
size_t operator()(const tree_sitter::LexAction &action) const {
return (hash<int>()(action.type) ^
hash<tree_sitter::rules::Symbol>()(action.symbol) ^
2014-01-11 15:14:17 -08:00
hash<size_t>()(action.state_index));
}
};
}
namespace tree_sitter {
class LexState {
public:
std::map<rules::CharacterSet, std::set<LexAction>> actions;
std::set<LexAction> default_actions;
std::set<rules::CharacterSet> expected_inputs() const;
2014-01-11 15:14:17 -08:00
};
class LexTable {
public:
size_t add_state();
void add_action(size_t state_index, rules::CharacterSet rule, LexAction action);
2014-01-11 15:14:17 -08:00
void add_default_action(size_t state_index, LexAction action);
std::vector<LexState> states;
};
}
#endif