#ifndef __TreeSitter__parse_table__ #define __TreeSitter__parse_table__ #include #include #include #include "rule.h" namespace tree_sitter { namespace lr { typedef enum { ParseActionTypeAccept, ParseActionTypeAdvance, ParseActionTypeError, ParseActionTypeShift, ParseActionTypeReduce, } ParseActionType; class ParseAction { public: ParseAction(); ParseAction(ParseActionType type, size_t state_index, std::string symbol_name, size_t child_symbol_count); bool operator==(const ParseAction &action) const; static ParseAction Accept(); static ParseAction Advance(size_t state_index); static ParseAction Error(); static ParseAction Shift(size_t state_index); static ParseAction Reduce(std::string symbol_name, size_t child_symbol_count); ParseActionType type; size_t child_symbol_count; std::string symbol_name; size_t state_index; }; std::ostream& operator<<(std::ostream &stream, const ParseAction &item); class ParseState { public: ParseState(); std::unordered_map> actions; }; class ParseTable { public: std::vector states; const std::unordered_map symbol_ids; const std::vector symbol_names; ParseTable(std::vector rule_names); ParseState starting_state() const; ParseState get_state(size_t index) const; ParseAction action_for(size_t state_index, std::string symbol_name) const; std::unordered_map> actions_for(size_t state_index) const; size_t add_state(); void add_action(size_t state_index, std::string symbol_name, ParseAction action); static const std::string START; static const std::string END_OF_INPUT; }; } } namespace std { template<> struct hash { size_t operator()(const tree_sitter::lr::ParseAction &action) const { return ( hash()(action.type) ^ hash()(action.symbol_name) ^ hash()(action.state_index) ^ hash()(action.child_symbol_count)); } }; } #endif