tree-sitter/src/compiler/parse_table.h

105 lines
2.9 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"
#include "compiler/rules/metadata.h"
#include "compiler/precedence_range.h"
#include "compiler/syntax_grammar.h"
2014-01-11 15:14:17 -08:00
namespace tree_sitter {
typedef uint64_t ParseStateId;
enum ParseActionType {
ParseActionTypeError,
ParseActionTypeShift,
ParseActionTypeReduce,
ParseActionTypeAccept,
ParseActionTypeRecover,
};
class ParseAction {
ParseAction(ParseActionType type, ParseStateId state_index,
rules::Symbol symbol, size_t consumed_symbol_count,
PrecedenceRange range, rules::Associativity, const Production *);
public:
ParseAction();
static ParseAction Accept();
static ParseAction Error();
static ParseAction Shift(ParseStateId state_index, PrecedenceRange precedence);
static ParseAction Recover(ParseStateId state_index);
static ParseAction Reduce(rules::Symbol symbol, size_t consumed_symbol_count,
int precedence, rules::Associativity,
const Production &);
static ParseAction ShiftExtra();
static ParseAction ReduceExtra(rules::Symbol symbol);
bool operator==(const ParseAction &) const;
bool operator<(const ParseAction &) const;
ParseActionType type;
bool extra;
bool fragile;
rules::Symbol symbol;
ParseStateId state_index;
size_t consumed_symbol_count;
PrecedenceRange precedence_range;
rules::Associativity associativity;
const Production *production;
};
struct ParseTableEntry {
std::vector<ParseAction> actions;
bool reusable;
bool depends_on_lookahead;
2014-03-09 19:49:35 -07:00
ParseTableEntry();
ParseTableEntry(const std::vector<ParseAction> &, bool, bool);
bool operator==(const ParseTableEntry &other) const;
inline bool operator!=(const ParseTableEntry &other) const {
return !operator==(other);
}
};
class ParseState {
public:
ParseState();
std::set<rules::Symbol> expected_inputs() const;
bool operator==(const ParseState &) const;
bool merge(const ParseState &);
void each_advance_action(std::function<void(ParseAction *)>);
std::map<rules::Symbol, ParseTableEntry> entries;
LexStateId lex_state_id;
};
struct ParseTableSymbolMetadata {
bool extra;
bool structural;
std::set<rules::Symbol> compatible_symbols;
};
class ParseTable {
public:
std::set<rules::Symbol> all_symbols() const;
ParseStateId add_state();
ParseAction &set_action(ParseStateId state_id, rules::Symbol symbol,
2015-07-27 18:29:48 -07:00
ParseAction action);
ParseAction &add_action(ParseStateId state_id, rules::Symbol symbol,
2015-07-27 18:29:48 -07:00
ParseAction action);
bool merge_state(size_t i, size_t j);
std::vector<ParseState> states;
std::map<rules::Symbol, ParseTableSymbolMetadata> symbols;
};
} // namespace tree_sitter
2014-01-11 15:14:17 -08:00
2014-03-09 22:05:17 -07:00
#endif // COMPILER_PARSE_TABLE_H_