tree-sitter/src/compiler/lex_table.h

69 lines
1.9 KiB
C
Raw Normal View History

2014-03-09 22:05:17 -07:00
#ifndef COMPILER_LEX_TABLE_H_
#define COMPILER_LEX_TABLE_H_
2014-01-11 15:14:17 -08:00
2014-03-09 21:37:21 -07:00
#include <map>
2014-01-11 15:14:17 -08:00
#include <vector>
#include <set>
2014-03-09 21:37:21 -07:00
#include <string>
#include "compiler/rules/symbol.h"
#include "compiler/rules/character_set.h"
2014-01-11 15:14:17 -08:00
namespace tree_sitter {
typedef enum {
LexActionTypeAccept,
LexActionTypeError,
LexActionTypeAdvance
} LexActionType;
2014-03-09 19:49:35 -07:00
2014-01-11 15:14:17 -08:00
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-03-09 19:49:35 -07:00
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;
};
2014-03-09 19:49:35 -07:00
2014-01-11 15:14:17 -08:00
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
};
2014-03-09 19:49:35 -07:00
typedef long int LexStateId;
2014-03-09 19:49:35 -07:00
2014-01-11 15:14:17 -08:00
class LexTable {
public:
static const LexStateId ERROR_STATE_ID;
LexStateId add_state();
void add_action(LexStateId state_id, rules::CharacterSet rule, LexAction action);
void add_default_action(LexStateId state_id, LexAction action);
2014-03-09 19:49:35 -07:00
2014-01-11 15:14:17 -08:00
std::vector<LexState> states;
LexState error_state;
2014-01-11 15:14:17 -08:00
};
}
2014-03-09 22:05:17 -07:00
#endif // COMPILER_LEX_TABLE_H_