2014-03-09 22:05:17 -07:00
|
|
|
#ifndef COMPILER_PARSE_TABLE_H_
|
|
|
|
|
#define COMPILER_PARSE_TABLE_H_
|
2014-01-11 15:14:17 -08:00
|
|
|
|
2014-02-10 18:38:01 -08: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 "compiler/lex_table.h"
|
|
|
|
|
#include "compiler/rules/symbol.h"
|
2014-01-11 15:14:17 -08:00
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
|
|
|
|
typedef enum {
|
|
|
|
|
ParseActionTypeAccept,
|
|
|
|
|
ParseActionTypeError,
|
|
|
|
|
ParseActionTypeShift,
|
|
|
|
|
ParseActionTypeReduce,
|
|
|
|
|
} ParseActionType;
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-11 15:14:17 -08:00
|
|
|
class ParseAction {
|
2014-01-28 22:09:37 -08:00
|
|
|
ParseAction(ParseActionType type, size_t state_index, rules::Symbol symbol, const std::vector<bool> &child_flags);
|
2014-01-11 15:14:17 -08:00
|
|
|
public:
|
|
|
|
|
static ParseAction Accept();
|
|
|
|
|
static ParseAction Error();
|
|
|
|
|
static ParseAction Shift(size_t state_index);
|
2014-01-28 22:09:37 -08:00
|
|
|
static ParseAction Reduce(rules::Symbol symbol, const std::vector<bool> &child_flags);
|
2014-01-11 15:14:17 -08:00
|
|
|
bool operator==(const ParseAction &action) const;
|
2014-02-10 18:38:01 -08:00
|
|
|
bool operator<(const ParseAction &action) const;
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-11 15:14:17 -08:00
|
|
|
ParseActionType type;
|
2014-01-27 12:40:06 -08:00
|
|
|
rules::Symbol symbol;
|
2014-01-11 15:14:17 -08:00
|
|
|
size_t state_index;
|
2014-02-18 09:07:00 -08:00
|
|
|
std::vector<bool> child_flags;
|
2014-01-11 15:14:17 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream &stream, const ParseAction &item);
|
|
|
|
|
}
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-11 15:14:17 -08:00
|
|
|
namespace std {
|
|
|
|
|
template<>
|
|
|
|
|
struct hash<tree_sitter::ParseAction> {
|
|
|
|
|
size_t operator()(const tree_sitter::ParseAction &action) const {
|
|
|
|
|
return (
|
|
|
|
|
hash<int>()(action.type) ^
|
2014-01-27 12:40:06 -08:00
|
|
|
hash<tree_sitter::rules::Symbol>()(action.symbol) ^
|
2014-01-11 15:14:17 -08:00
|
|
|
hash<size_t>()(action.state_index) ^
|
2014-01-28 22:09:37 -08:00
|
|
|
hash<size_t>()(action.child_flags.size()));
|
2014-01-11 15:14:17 -08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
|
|
|
|
class ParseState {
|
|
|
|
|
public:
|
|
|
|
|
ParseState();
|
2014-02-10 18:38:01 -08:00
|
|
|
std::map<rules::Symbol, std::set<ParseAction>> actions;
|
|
|
|
|
std::set<rules::Symbol> expected_inputs() const;
|
2014-02-24 18:42:54 -08:00
|
|
|
LexStateId lex_state_id;
|
2014-01-11 15:14:17 -08:00
|
|
|
};
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-02-24 18:42:54 -08:00
|
|
|
typedef unsigned long int ParseStateId;
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-26 16:38:41 -08:00
|
|
|
std::ostream& operator<<(std::ostream &stream, const ParseState &state);
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-11 15:14:17 -08:00
|
|
|
class ParseTable {
|
|
|
|
|
public:
|
|
|
|
|
size_t add_state();
|
2014-02-24 18:42:54 -08:00
|
|
|
void add_action(ParseStateId state_id, rules::Symbol symbol, ParseAction action);
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-11 15:14:17 -08:00
|
|
|
std::vector<ParseState> states;
|
2014-02-10 18:38:01 -08:00
|
|
|
std::set<rules::Symbol> symbols;
|
2014-02-26 19:03:43 -08:00
|
|
|
std::map<ParseStateId, std::pair<ParseStateId, std::set<rules::Symbol>>> error_table;
|
2014-01-11 15:14:17 -08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-09 22:05:17 -07:00
|
|
|
#endif // COMPILER_PARSE_TABLE_H_
|