tree-sitter/src/compiler/parse_table.h

75 lines
2.2 KiB
C
Raw Normal View History

2014-01-11 15:14:17 -08:00
#ifndef __TreeSitter__parse_table__
#define __TreeSitter__parse_table__
#include <map>
2014-01-11 15:14:17 -08:00
#include <vector>
#include <set>
2014-02-18 09:07:00 -08:00
#include "rules/symbol.h"
#include "./lex_table.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 {
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);
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;
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;
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) ^
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.child_flags.size()));
2014-01-11 15:14:17 -08:00
}
};
}
namespace tree_sitter {
class ParseState {
public:
ParseState();
std::map<rules::Symbol, std::set<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
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();
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-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
};
}
#endif