tree-sitter/src/compiler/parse_table.h

77 lines
2.1 KiB
C
Raw Normal View History

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
#include <map>
#include <set>
2014-03-09 22:45:33 -07:00
#include <utility>
#include <vector>
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 {
ParseActionTypeError,
ParseActionTypeAccept,
2014-01-11 15:14:17 -08:00
ParseActionTypeShift,
ParseActionTypeReduce,
} ParseActionType;
2014-03-09 19:49:35 -07:00
2014-01-11 15:14:17 -08:00
class ParseAction {
2014-03-09 23:51:33 -07:00
ParseAction(ParseActionType type,
size_t state_index,
rules::Symbol symbol,
size_t consumed_symbol_count);
2014-01-11 15:14:17 -08:00
public:
static ParseAction Accept();
static ParseAction Error();
static ParseAction Shift(size_t state_index);
static ParseAction Reduce(rules::Symbol symbol, size_t consumed_symbol_count);
2014-01-11 15:14:17 -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;
rules::Symbol symbol;
2014-01-11 15:14:17 -08:00
size_t state_index;
size_t consumed_symbol_count;
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) ^
hash<tree_sitter::rules::Symbol>()(action.symbol) ^
2014-01-11 15:14:17 -08:00
hash<size_t>()(action.state_index) ^
hash<size_t>()(action.consumed_symbol_count));
2014-01-11 15:14:17 -08:00
}
};
}
namespace tree_sitter {
class ParseState {
public:
ParseState();
std::map<rules::Symbol, ParseAction> actions;
std::set<rules::Symbol> expected_inputs() const;
LexStateId lex_state_id;
2014-01-11 15:14:17 -08:00
};
2014-03-09 19:49:35 -07:00
2014-03-09 22:58:17 -07:00
typedef uint64_t 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:
2014-03-09 22:58:17 -07:00
uint64_t add_state();
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;
std::set<rules::Symbol> symbols;
2014-01-11 15:14:17 -08:00
};
}
2014-03-09 22:05:17 -07:00
#endif // COMPILER_PARSE_TABLE_H_