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>
|
|
|
|
|
#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"
|
2014-04-28 20:43:27 -07:00
|
|
|
#include "compiler/rules/symbol.h"
|
2015-03-16 23:12:08 -07:00
|
|
|
#include "compiler/rules/metadata.h"
|
2014-01-11 15:14:17 -08:00
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
typedef uint64_t ParseStateId;
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
|
ParseActionTypeError,
|
2015-06-28 16:22:31 -05:00
|
|
|
ParseActionTypeReduceExtra,
|
2014-07-20 21:43:27 -07:00
|
|
|
ParseActionTypeShiftExtra,
|
2015-06-28 16:22:31 -05:00
|
|
|
|
|
|
|
|
ParseActionTypeShift,
|
2014-07-20 21:43:27 -07:00
|
|
|
ParseActionTypeReduce,
|
|
|
|
|
ParseActionTypeAccept,
|
|
|
|
|
} ParseActionType;
|
|
|
|
|
|
|
|
|
|
class ParseAction {
|
|
|
|
|
ParseAction(ParseActionType type, ParseStateId state_index,
|
|
|
|
|
rules::Symbol symbol, size_t consumed_symbol_count,
|
2015-09-03 17:49:20 -07:00
|
|
|
std::set<int> precedence_values, Associativity, int production_id);
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
ParseAction();
|
|
|
|
|
static ParseAction Accept();
|
|
|
|
|
static ParseAction Error();
|
|
|
|
|
static ParseAction Shift(ParseStateId state_index,
|
|
|
|
|
std::set<int> precedence_values);
|
|
|
|
|
static ParseAction Reduce(rules::Symbol symbol, size_t consumed_symbol_count,
|
2015-10-01 17:10:39 -07:00
|
|
|
int precedence, Associativity,
|
|
|
|
|
unsigned int production_id);
|
2014-07-20 21:43:27 -07:00
|
|
|
static ParseAction ShiftExtra();
|
|
|
|
|
static ParseAction ReduceExtra(rules::Symbol symbol);
|
2015-03-07 10:47:37 -08:00
|
|
|
bool operator==(const ParseAction &) const;
|
|
|
|
|
bool operator<(const ParseAction &) const;
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
ParseActionType type;
|
|
|
|
|
rules::Symbol symbol;
|
|
|
|
|
ParseStateId state_index;
|
|
|
|
|
size_t consumed_symbol_count;
|
|
|
|
|
std::set<int> precedence_values;
|
2015-09-03 17:49:20 -07:00
|
|
|
Associativity associativity;
|
2015-02-21 10:41:23 -08:00
|
|
|
int production_id;
|
2014-07-20 21:43:27 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace tree_sitter
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2014-01-11 15:14:17 -08:00
|
|
|
namespace std {
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
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) ^
|
|
|
|
|
hash<size_t>()(action.state_index) ^
|
2015-02-21 10:41:23 -08:00
|
|
|
hash<size_t>()(action.consumed_symbol_count) ^
|
|
|
|
|
hash<size_t>()(action.production_id));
|
2014-07-20 21:43:27 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace std
|
2014-01-11 15:14:17 -08:00
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
class ParseState {
|
|
|
|
|
public:
|
|
|
|
|
ParseState();
|
2015-06-28 16:22:31 -05:00
|
|
|
std::map<rules::Symbol, std::vector<ParseAction>> actions;
|
2014-07-20 21:43:27 -07:00
|
|
|
std::set<rules::Symbol> expected_inputs() const;
|
|
|
|
|
LexStateId lex_state_id;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class ParseTable {
|
|
|
|
|
public:
|
|
|
|
|
ParseStateId add_state();
|
2015-06-28 16:22:31 -05:00
|
|
|
ParseAction &set_action(ParseStateId state_id, rules::Symbol symbol,
|
2015-07-27 18:29:48 -07:00
|
|
|
ParseAction action);
|
2015-06-28 16:22:31 -05:00
|
|
|
ParseAction &add_action(ParseStateId state_id, rules::Symbol symbol,
|
2015-07-27 18:29:48 -07:00
|
|
|
ParseAction action);
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
std::vector<ParseState> states;
|
|
|
|
|
std::set<rules::Symbol> symbols;
|
2015-10-01 17:10:39 -07:00
|
|
|
std::set<std::pair<rules::Symbol, unsigned int>> fragile_production_ids;
|
2014-07-20 21:43:27 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace tree_sitter
|
2014-01-11 15:14:17 -08:00
|
|
|
|
2014-03-09 22:05:17 -07:00
|
|
|
#endif // COMPILER_PARSE_TABLE_H_
|