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>
|
2014-02-10 18:38:01 -08:00
|
|
|
#include <set>
|
2014-03-09 21:37:21 -07:00
|
|
|
#include <string>
|
2014-04-28 20:43:27 -07:00
|
|
|
#include "compiler/rules/symbol.h"
|
2014-03-09 21:37:21 -07:00
|
|
|
#include "compiler/rules/character_set.h"
|
2014-01-11 15:14:17 -08:00
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
|
LexActionTypeError,
|
|
|
|
|
LexActionTypeAccept,
|
|
|
|
|
LexActionTypeAdvance
|
|
|
|
|
} LexActionType;
|
|
|
|
|
|
|
|
|
|
class LexAction {
|
|
|
|
|
LexAction(LexActionType type, size_t state_index, rules::Symbol symbol,
|
|
|
|
|
std::set<int> precedence_values);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
LexAction();
|
|
|
|
|
static LexAction Accept(rules::Symbol symbol, int precedence);
|
|
|
|
|
static LexAction Error();
|
|
|
|
|
static LexAction Advance(size_t state_index, std::set<int> precedence_values);
|
|
|
|
|
bool operator==(const LexAction &action) const;
|
|
|
|
|
|
|
|
|
|
LexActionType type;
|
|
|
|
|
rules::Symbol symbol;
|
|
|
|
|
size_t state_index;
|
|
|
|
|
std::set<int> precedence_values;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace tree_sitter
|
2014-01-11 15:14:17 -08:00
|
|
|
|
|
|
|
|
namespace std {
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
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) ^
|
|
|
|
|
hash<size_t>()(action.state_index));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace std
|
2014-01-11 15:14:17 -08:00
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
class LexState {
|
|
|
|
|
public:
|
2015-02-13 22:16:27 -08:00
|
|
|
LexState();
|
2014-07-20 21:43:27 -07:00
|
|
|
std::map<rules::CharacterSet, LexAction> actions;
|
|
|
|
|
LexAction default_action;
|
|
|
|
|
std::set<rules::CharacterSet> expected_inputs() const;
|
2014-09-02 07:41:29 -07:00
|
|
|
bool is_token_start;
|
2014-07-20 21:43:27 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef int64_t LexStateId;
|
|
|
|
|
|
|
|
|
|
class LexTable {
|
|
|
|
|
public:
|
|
|
|
|
static const LexStateId ERROR_STATE_ID;
|
|
|
|
|
LexStateId add_state();
|
|
|
|
|
LexState &state(LexStateId state_id);
|
|
|
|
|
|
|
|
|
|
std::vector<LexState> states;
|
|
|
|
|
LexState error_state;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace tree_sitter
|
2014-01-11 15:14:17 -08:00
|
|
|
|
2014-03-09 22:05:17 -07:00
|
|
|
#endif // COMPILER_LEX_TABLE_H_
|