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"
|
2015-10-05 16:05:19 -07:00
|
|
|
#include "compiler/precedence_range.h"
|
2015-10-17 22:54:56 -07:00
|
|
|
#include "compiler/syntax_grammar.h"
|
2014-01-11 15:14:17 -08:00
|
|
|
|
|
|
|
|
namespace tree_sitter {
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
typedef uint64_t ParseStateId;
|
|
|
|
|
|
2016-06-21 07:28:04 -07:00
|
|
|
enum ParseActionType {
|
2014-07-20 21:43:27 -07:00
|
|
|
ParseActionTypeError,
|
2015-06-28 16:22:31 -05:00
|
|
|
ParseActionTypeShift,
|
2014-07-20 21:43:27 -07:00
|
|
|
ParseActionTypeReduce,
|
|
|
|
|
ParseActionTypeAccept,
|
2016-06-27 14:07:47 -07:00
|
|
|
ParseActionTypeRecover,
|
2016-06-21 07:28:04 -07:00
|
|
|
};
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
class ParseAction {
|
|
|
|
|
ParseAction(ParseActionType type, ParseStateId state_index,
|
|
|
|
|
rules::Symbol symbol, size_t consumed_symbol_count,
|
2015-10-17 22:54:56 -07:00
|
|
|
PrecedenceRange range, rules::Associativity, const Production *);
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
ParseAction();
|
|
|
|
|
static ParseAction Accept();
|
|
|
|
|
static ParseAction Error();
|
2015-10-05 16:05:19 -07:00
|
|
|
static ParseAction Shift(ParseStateId state_index, PrecedenceRange precedence);
|
2016-06-27 14:07:47 -07:00
|
|
|
static ParseAction Recover(ParseStateId state_index);
|
2014-07-20 21:43:27 -07:00
|
|
|
static ParseAction Reduce(rules::Symbol symbol, size_t consumed_symbol_count,
|
2015-10-13 11:23:02 -07:00
|
|
|
int precedence, rules::Associativity,
|
2015-10-17 22:54:56 -07:00
|
|
|
const Production &);
|
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;
|
2015-12-17 12:48:55 -08:00
|
|
|
bool extra;
|
|
|
|
|
bool fragile;
|
2014-07-20 21:43:27 -07:00
|
|
|
rules::Symbol symbol;
|
|
|
|
|
ParseStateId state_index;
|
|
|
|
|
size_t consumed_symbol_count;
|
2015-10-05 16:05:19 -07:00
|
|
|
PrecedenceRange precedence_range;
|
2015-10-13 11:23:02 -07:00
|
|
|
rules::Associativity associativity;
|
2015-10-17 22:54:56 -07:00
|
|
|
const Production *production;
|
2014-07-20 21:43:27 -07:00
|
|
|
};
|
|
|
|
|
|
2016-06-21 07:28:04 -07:00
|
|
|
struct ParseTableEntry {
|
|
|
|
|
std::vector<ParseAction> actions;
|
|
|
|
|
bool reusable;
|
|
|
|
|
bool depends_on_lookahead;
|
2014-03-09 19:49:35 -07:00
|
|
|
|
2016-06-21 07:28:04 -07:00
|
|
|
ParseTableEntry();
|
|
|
|
|
ParseTableEntry(const std::vector<ParseAction> &, bool, bool);
|
|
|
|
|
bool operator==(const ParseTableEntry &other) const;
|
2016-06-29 09:54:08 -07:00
|
|
|
|
|
|
|
|
inline bool operator!=(const ParseTableEntry &other) const {
|
|
|
|
|
return !operator==(other);
|
|
|
|
|
}
|
2014-07-20 21:43:27 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class ParseState {
|
|
|
|
|
public:
|
|
|
|
|
ParseState();
|
|
|
|
|
std::set<rules::Symbol> expected_inputs() const;
|
2015-12-20 15:26:35 -08:00
|
|
|
bool operator==(const ParseState &) const;
|
2016-06-29 09:54:08 -07:00
|
|
|
bool merge(const ParseState &);
|
2016-01-22 22:16:47 -07:00
|
|
|
void each_advance_action(std::function<void(ParseAction *)>);
|
2015-12-20 15:26:35 -08:00
|
|
|
|
2016-06-21 07:28:04 -07:00
|
|
|
std::map<rules::Symbol, ParseTableEntry> entries;
|
2014-07-20 21:43:27 -07:00
|
|
|
LexStateId lex_state_id;
|
|
|
|
|
};
|
|
|
|
|
|
2015-12-02 07:36:31 -08:00
|
|
|
struct ParseTableSymbolMetadata {
|
2016-06-21 07:28:04 -07:00
|
|
|
bool extra;
|
2015-12-02 07:36:31 -08:00
|
|
|
bool structural;
|
2016-06-29 09:54:08 -07:00
|
|
|
std::set<rules::Symbol> compatible_symbols;
|
2015-12-02 07:36:31 -08:00
|
|
|
};
|
|
|
|
|
|
2014-07-20 21:43:27 -07:00
|
|
|
class ParseTable {
|
|
|
|
|
public:
|
2015-12-02 07:36:31 -08:00
|
|
|
std::set<rules::Symbol> all_symbols() const;
|
2014-07-20 21:43:27 -07:00
|
|
|
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);
|
2016-06-29 09:54:08 -07:00
|
|
|
bool merge_state(size_t i, size_t j);
|
2014-07-20 21:43:27 -07:00
|
|
|
|
|
|
|
|
std::vector<ParseState> states;
|
2015-12-02 07:36:31 -08:00
|
|
|
std::map<rules::Symbol, ParseTableSymbolMetadata> symbols;
|
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_
|