#include "parse.hpp" typedef std::vector> vars_t; typedef std::pair, vars_t> rhs_t; static inline void free_string(std::string *v) { delete v; } static inline void free_symbol(solar::symbol_t *v) { delete v; } static inline void free_rule(solar::rule_t *v) { delete v; } static inline void free_rhs(rhs_t *v) { delete v; } typedef union parse_symbol_value { parse_token_value_t token; std::string * symbol_action; rhs_t * symbol_rhs; solar::rule_t * symbol_rule; solar::symbol_t * symbol_symbol; solar::symbol_t * symbol_term; std::string * symbol_varname; } parse_symbol_value_t; typedef struct parse_context_state { unsigned state; parse_symbol_value_t value; } parse_context_state_t; struct parse_context { unsigned top; parse_context_state_t stack[100]; }; parse_context_t * parse_alloc(void *(*alloc_func)(size_t)) { parse_context_t *parser = (parse_context_t *)alloc_func(sizeof(parse_context_t)); parser->top = 0; parser->stack[0].state = 0; return parser; } void parse_free(parse_context_t *parser, void (*free_func)(void *)) { free_func(parser); } static inline void parse_reduce_3(std::string * nonterm, std::string * type, __attribute__((unused)) solar::grammar_t * grammar) { grammar->nonterm_types.insert(std::make_pair(*nonterm, *type)); } static inline void parse_reduce_4(solar::symbol_t * term, std::string * type, std::string * name, __attribute__((unused)) solar::grammar_t * grammar) { grammar->term_types.insert(std::make_pair(*term, std::make_pair(*type, *name))); } static inline void parse_reduce_5(solar::symbol_t * sym, std::string * name, __attribute__((unused)) solar::grammar_t * grammar) { grammar->destructors.insert(std::make_pair(*sym, *name)); } static inline void parse_reduce_6(std::string * block, __attribute__((unused)) solar::grammar_t * grammar) { grammar->source_block = *block; } static inline void parse_reduce_7(std::string * block, __attribute__((unused)) solar::grammar_t * grammar) { grammar->header_block = *block; } static inline void parse_reduce_8(std::string * type, std::string * name, __attribute__((unused)) solar::grammar_t * grammar) { grammar->extra_args.push_back(std::make_pair(*type, *name)); } static inline void parse_reduce_9(solar::rule_t * rule, __attribute__((unused)) solar::grammar_t * grammar) { if (grammar->rules.empty()) { solar::item_t init(""); init.get_rhs().push_back(solar::symbol_t::make_nonterm(rule->item.get_lhs().c_str())); grammar->rules.emplace_back(solar::rule_t {std::move(init), vars_t(), std::string()}); } grammar->rules.push_back(*rule); } static inline solar::rule_t * parse_reduce_10(std::string * lhs, rhs_t * rhs, std::string * action, __attribute__((unused)) solar::grammar_t * grammar) {return new solar::rule_t {solar::item_t(*lhs, rhs->first), rhs->second, *action} ;} static inline rhs_t * parse_reduce_11(__attribute__((unused)) solar::grammar_t * grammar) {return new rhs_t();} static inline rhs_t * parse_reduce_12(rhs_t * rhs, solar::symbol_t * sym, __attribute__((unused)) solar::grammar_t * grammar) { rhs->first.push_back(*sym); rhs->second.emplace_back(); return rhs; } static inline rhs_t * parse_reduce_13(rhs_t * rhs, solar::symbol_t * sym, std::string * var, __attribute__((unused)) solar::grammar_t * grammar) { rhs->first.push_back(*sym); rhs->second.emplace_back(*var, true); return rhs; } static inline rhs_t * parse_reduce_14(rhs_t * rhs, solar::symbol_t * sym, std::string * var, __attribute__((unused)) solar::grammar_t * grammar) { rhs->first.push_back(*sym); rhs->second.emplace_back(*var, false); return rhs; } static inline rhs_t * parse_reduce_15(rhs_t * rhs, std::string * str, __attribute__((unused)) solar::grammar_t * grammar) { for (char c : *str) { rhs->first.push_back(solar::symbol_t::make_char(c)); rhs->second.emplace_back(); } return rhs; } static inline std::string * parse_reduce_16(__attribute__((unused)) solar::grammar_t * grammar) {return new std::string;} static inline std::string * parse_reduce_17(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) {return v;} static inline std::string * parse_reduce_18(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) {return new std::string("return " + *v + ";");} static inline solar::symbol_t * parse_reduce_19(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) {return new solar::symbol_t(solar::symbol_t::make_nonterm(*v));} static inline solar::symbol_t * parse_reduce_20(solar::symbol_t * v, __attribute__((unused)) solar::grammar_t * grammar) {return v;} static inline solar::symbol_t * parse_reduce_21(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) {return new solar::symbol_t(solar::symbol_t::make_term(*v));} static inline solar::symbol_t * parse_reduce_22(char v, __attribute__((unused)) solar::grammar_t * grammar) {return new solar::symbol_t(solar::symbol_t::make_char(v));} static inline std::string * parse_reduce_23(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) {return v;} static inline std::string * parse_reduce_24(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) {return v;} static inline std::string * parse_reduce_25(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) {return v;} static int parse_do_push(parse_context_t *parser, int token, __attribute__((unused)) solar::grammar_t * grammar) { parse_symbol_value_t result; while (1) { switch (parser->stack[parser->top].state) { case 0: switch (token) { default: parser->stack[++parser->top].state = 1; } break; case 1: switch (token) { case 0: return 0; case TOK_SYMBOL_LC: parser->stack[++parser->top].state = 4; return 1; case '%': parser->stack[++parser->top].state = 5; return 1; default: return -1; } break; case 2: switch (token) { default: parser->top -= 2; parser->stack[++parser->top].state = 1; } break; case 3: switch (token) { default: parser->top -= 1; parse_reduce_9(parser->stack[parser->top + 0].value.symbol_rule, grammar); free_rule(parser->stack[parser->top + 0].value.symbol_rule); parser->stack[++parser->top].state = 2; } break; case 4: switch (token) { case '|': parser->stack[++parser->top].state = 6; return 1; default: return -1; } break; case 5: switch (token) { case 'd': parser->stack[++parser->top].state = 7; return 1; case 'e': parser->stack[++parser->top].state = 8; return 1; case 'h': parser->stack[++parser->top].state = 9; return 1; case 's': parser->stack[++parser->top].state = 10; return 1; case 't': parser->stack[++parser->top].state = 11; return 1; default: return -1; } break; case 6: switch (token) { case '=': parser->stack[++parser->top].state = 12; return 1; default: return -1; } break; case 7: switch (token) { case 'e': parser->stack[++parser->top].state = 13; return 1; default: return -1; } break; case 8: switch (token) { case 'x': parser->stack[++parser->top].state = 14; return 1; default: return -1; } break; case 9: switch (token) { case 'e': parser->stack[++parser->top].state = 15; return 1; default: return -1; } break; case 10: switch (token) { case 'o': parser->stack[++parser->top].state = 16; return 1; default: return -1; } break; case 11: switch (token) { case 'y': parser->stack[++parser->top].state = 17; return 1; default: return -1; } break; case 12: switch (token) { default: result.symbol_rhs = parse_reduce_11(grammar); parser->stack[parser->top].value.symbol_rhs = result.symbol_rhs; parser->stack[++parser->top].state = 18; } break; case 13: switch (token) { case 's': parser->stack[++parser->top].state = 19; return 1; default: return -1; } break; case 14: switch (token) { case 't': parser->stack[++parser->top].state = 20; return 1; default: return -1; } break; case 15: switch (token) { case 'a': parser->stack[++parser->top].state = 21; return 1; default: return -1; } break; case 16: switch (token) { case 'u': parser->stack[++parser->top].state = 22; return 1; default: return -1; } break; case 17: switch (token) { case 'p': parser->stack[++parser->top].state = 23; return 1; default: return -1; } break; case 18: switch (token) { case TOK_BLOCK: parser->stack[++parser->top].state = 27; return 1; case TOK_CHAR: parser->stack[++parser->top].state = 28; return 1; case TOK_SQBLOCK: parser->stack[++parser->top].state = 29; return 1; case TOK_STRING: parser->stack[++parser->top].state = 30; return 1; case TOK_SYMBOL_LC: parser->stack[++parser->top].state = 31; return 1; case TOK_SYMBOL_UC: parser->stack[++parser->top].state = 32; return 1; case ';': parser->stack[++parser->top].state = 33; return 1; default: return -1; } break; case 19: switch (token) { case 't': parser->stack[++parser->top].state = 34; return 1; default: return -1; } break; case 20: switch (token) { case 'r': parser->stack[++parser->top].state = 35; return 1; default: return -1; } break; case 21: switch (token) { case 'd': parser->stack[++parser->top].state = 36; return 1; default: return -1; } break; case 22: switch (token) { case 'r': parser->stack[++parser->top].state = 37; return 1; default: return -1; } break; case 23: switch (token) { case 'e': parser->stack[++parser->top].state = 38; return 1; default: return -1; } break; case 24: switch (token) { default: parser->top -= 5; result.symbol_rule = parse_reduce_10(parser->stack[parser->top + 0].value.token.str, parser->stack[parser->top + 3].value.symbol_rhs, parser->stack[parser->top + 4].value.symbol_action, grammar); free_string(parser->stack[parser->top + 0].value.token.str); free_rhs(parser->stack[parser->top + 3].value.symbol_rhs); free_string(parser->stack[parser->top + 4].value.symbol_action); parser->stack[parser->top].value.symbol_rule = result.symbol_rule; parser->stack[++parser->top].state = 3; } break; case 25: switch (token) { case '(': parser->stack[++parser->top].state = 39; return 1; default: parser->top -= 2; result.symbol_rhs = parse_reduce_12(parser->stack[parser->top + 0].value.symbol_rhs, parser->stack[parser->top + 1].value.symbol_symbol, grammar); free_symbol(parser->stack[parser->top + 1].value.symbol_symbol); parser->stack[parser->top].value.symbol_rhs = result.symbol_rhs; parser->stack[++parser->top].state = 18; } break; case 26: switch (token) { default: parser->top -= 1; result.symbol_symbol = parse_reduce_20(parser->stack[parser->top + 0].value.symbol_term, grammar); parser->stack[parser->top].value.symbol_symbol = result.symbol_symbol; switch (parser->stack[parser->top].state) { case 18: parser->stack[++parser->top].state = 25; break; case 69: parser->stack[++parser->top].state = 71; break; } } break; case 27: switch (token) { default: parser->top -= 1; result.symbol_action = parse_reduce_17(parser->stack[parser->top + 0].value.token.str, grammar); parser->stack[parser->top].value.symbol_action = result.symbol_action; parser->stack[++parser->top].state = 24; } break; case 28: switch (token) { default: parser->top -= 1; result.symbol_term = parse_reduce_22(parser->stack[parser->top + 0].value.token.c, grammar); parser->stack[parser->top].value.symbol_term = result.symbol_term; switch (parser->stack[parser->top].state) { case 18: case 69: parser->stack[++parser->top].state = 26; break; case 38: parser->stack[++parser->top].state = 44; break; } } break; case 29: switch (token) { default: parser->top -= 1; result.symbol_action = parse_reduce_18(parser->stack[parser->top + 0].value.token.str, grammar); free_string(parser->stack[parser->top + 0].value.token.str); parser->stack[parser->top].value.symbol_action = result.symbol_action; parser->stack[++parser->top].state = 24; } break; case 30: switch (token) { default: parser->top -= 2; result.symbol_rhs = parse_reduce_15(parser->stack[parser->top + 0].value.symbol_rhs, parser->stack[parser->top + 1].value.token.str, grammar); free_string(parser->stack[parser->top + 1].value.token.str); parser->stack[parser->top].value.symbol_rhs = result.symbol_rhs; parser->stack[++parser->top].state = 18; } break; case 31: switch (token) { default: parser->top -= 1; result.symbol_symbol = parse_reduce_19(parser->stack[parser->top + 0].value.token.str, grammar); free_string(parser->stack[parser->top + 0].value.token.str); parser->stack[parser->top].value.symbol_symbol = result.symbol_symbol; switch (parser->stack[parser->top].state) { case 18: parser->stack[++parser->top].state = 25; break; case 69: parser->stack[++parser->top].state = 71; break; } } break; case 32: switch (token) { default: parser->top -= 1; result.symbol_term = parse_reduce_21(parser->stack[parser->top + 0].value.token.str, grammar); free_string(parser->stack[parser->top + 0].value.token.str); parser->stack[parser->top].value.symbol_term = result.symbol_term; switch (parser->stack[parser->top].state) { case 18: case 69: parser->stack[++parser->top].state = 26; break; case 38: parser->stack[++parser->top].state = 44; break; } } break; case 33: switch (token) { default: parser->top -= 1; result.symbol_action = parse_reduce_16(grammar); parser->stack[parser->top].value.symbol_action = result.symbol_action; parser->stack[++parser->top].state = 24; } break; case 34: switch (token) { case 'r': parser->stack[++parser->top].state = 40; return 1; default: return -1; } break; case 35: switch (token) { case 'a': parser->stack[++parser->top].state = 41; return 1; default: return -1; } break; case 36: switch (token) { case 'e': parser->stack[++parser->top].state = 42; return 1; default: return -1; } break; case 37: switch (token) { case 'c': parser->stack[++parser->top].state = 43; return 1; default: return -1; } break; case 38: switch (token) { case TOK_CHAR: parser->stack[++parser->top].state = 28; return 1; case TOK_SYMBOL_UC: parser->stack[++parser->top].state = 32; return 1; case TOK_SYMBOL_LC: parser->stack[++parser->top].state = 45; return 1; default: return -1; } break; case 39: switch (token) { case TOK_SYMBOL: parser->stack[++parser->top].state = 47; return 1; case TOK_SYMBOL_LC: parser->stack[++parser->top].state = 48; return 1; case TOK_SYMBOL_UC: parser->stack[++parser->top].state = 49; return 1; case '=': parser->stack[++parser->top].state = 50; return 1; default: return -1; } break; case 40: switch (token) { case 'u': parser->stack[++parser->top].state = 51; return 1; default: return -1; } break; case 41: switch (token) { case '_': parser->stack[++parser->top].state = 52; return 1; default: return -1; } break; case 42: switch (token) { case 'r': parser->stack[++parser->top].state = 53; return 1; default: return -1; } break; case 43: switch (token) { case 'e': parser->stack[++parser->top].state = 54; return 1; default: return -1; } break; case 44: switch (token) { case TOK_BLOCK: parser->stack[++parser->top].state = 55; return 1; default: return -1; } break; case 45: switch (token) { case TOK_BLOCK: parser->stack[++parser->top].state = 56; return 1; default: return -1; } break; case 46: switch (token) { case ')': parser->stack[++parser->top].state = 57; return 1; default: return -1; } break; case 47: switch (token) { default: parser->top -= 1; result.symbol_varname = parse_reduce_25(parser->stack[parser->top + 0].value.token.str, grammar); parser->stack[parser->top].value.symbol_varname = result.symbol_varname; switch (parser->stack[parser->top].state) { case 39: parser->stack[++parser->top].state = 46; break; case 50: parser->stack[++parser->top].state = 58; break; case 55: parser->stack[++parser->top].state = 63; break; case 70: parser->stack[++parser->top].state = 72; break; case 71: parser->stack[++parser->top].state = 73; break; } } break; case 48: switch (token) { default: parser->top -= 1; result.symbol_varname = parse_reduce_23(parser->stack[parser->top + 0].value.token.str, grammar); parser->stack[parser->top].value.symbol_varname = result.symbol_varname; switch (parser->stack[parser->top].state) { case 39: parser->stack[++parser->top].state = 46; break; case 50: parser->stack[++parser->top].state = 58; break; case 55: parser->stack[++parser->top].state = 63; break; case 70: parser->stack[++parser->top].state = 72; break; case 71: parser->stack[++parser->top].state = 73; break; } } break; case 49: switch (token) { default: parser->top -= 1; result.symbol_varname = parse_reduce_24(parser->stack[parser->top + 0].value.token.str, grammar); parser->stack[parser->top].value.symbol_varname = result.symbol_varname; switch (parser->stack[parser->top].state) { case 39: parser->stack[++parser->top].state = 46; break; case 50: parser->stack[++parser->top].state = 58; break; case 55: parser->stack[++parser->top].state = 63; break; case 70: parser->stack[++parser->top].state = 72; break; case 71: parser->stack[++parser->top].state = 73; break; } } break; case 50: switch (token) { case TOK_SYMBOL: parser->stack[++parser->top].state = 47; return 1; case TOK_SYMBOL_LC: parser->stack[++parser->top].state = 48; return 1; case TOK_SYMBOL_UC: parser->stack[++parser->top].state = 49; return 1; default: return -1; } break; case 51: switch (token) { case 'c': parser->stack[++parser->top].state = 59; return 1; default: return -1; } break; case 52: switch (token) { case 'a': parser->stack[++parser->top].state = 60; return 1; default: return -1; } break; case 53: switch (token) { case TOK_BLOCK: parser->stack[++parser->top].state = 61; return 1; default: return -1; } break; case 54: switch (token) { case TOK_BLOCK: parser->stack[++parser->top].state = 62; return 1; default: return -1; } break; case 55: switch (token) { case TOK_SYMBOL: parser->stack[++parser->top].state = 47; return 1; case TOK_SYMBOL_LC: parser->stack[++parser->top].state = 48; return 1; case TOK_SYMBOL_UC: parser->stack[++parser->top].state = 49; return 1; default: return -1; } break; case 56: switch (token) { default: parser->top -= 7; parse_reduce_3(parser->stack[parser->top + 5].value.token.str, parser->stack[parser->top + 6].value.token.str, grammar); free_string(parser->stack[parser->top + 5].value.token.str); free_string(parser->stack[parser->top + 6].value.token.str); parser->stack[++parser->top].state = 2; } break; case 57: switch (token) { default: parser->top -= 5; result.symbol_rhs = parse_reduce_13(parser->stack[parser->top + 0].value.symbol_rhs, parser->stack[parser->top + 1].value.symbol_symbol, parser->stack[parser->top + 3].value.symbol_varname, grammar); free_symbol(parser->stack[parser->top + 1].value.symbol_symbol); free_string(parser->stack[parser->top + 3].value.symbol_varname); parser->stack[parser->top].value.symbol_rhs = result.symbol_rhs; parser->stack[++parser->top].state = 18; } break; case 58: switch (token) { case ')': parser->stack[++parser->top].state = 64; return 1; default: return -1; } break; case 59: switch (token) { case 't': parser->stack[++parser->top].state = 65; return 1; default: return -1; } break; case 60: switch (token) { case 'r': parser->stack[++parser->top].state = 66; return 1; default: return -1; } break; case 61: switch (token) { default: parser->top -= 8; parse_reduce_7(parser->stack[parser->top + 7].value.token.str, grammar); free_string(parser->stack[parser->top + 7].value.token.str); parser->stack[++parser->top].state = 2; } break; case 62: switch (token) { default: parser->top -= 8; parse_reduce_6(parser->stack[parser->top + 7].value.token.str, grammar); free_string(parser->stack[parser->top + 7].value.token.str); parser->stack[++parser->top].state = 2; } break; case 63: switch (token) { default: parser->top -= 8; parse_reduce_4(parser->stack[parser->top + 5].value.symbol_term, parser->stack[parser->top + 6].value.token.str, parser->stack[parser->top + 7].value.symbol_varname, grammar); free_symbol(parser->stack[parser->top + 5].value.symbol_term); free_string(parser->stack[parser->top + 6].value.token.str); free_string(parser->stack[parser->top + 7].value.symbol_varname); parser->stack[++parser->top].state = 2; } break; case 64: switch (token) { default: parser->top -= 6; result.symbol_rhs = parse_reduce_14(parser->stack[parser->top + 0].value.symbol_rhs, parser->stack[parser->top + 1].value.symbol_symbol, parser->stack[parser->top + 4].value.symbol_varname, grammar); free_symbol(parser->stack[parser->top + 1].value.symbol_symbol); free_string(parser->stack[parser->top + 4].value.symbol_varname); parser->stack[parser->top].value.symbol_rhs = result.symbol_rhs; parser->stack[++parser->top].state = 18; } break; case 65: switch (token) { case 'o': parser->stack[++parser->top].state = 67; return 1; default: return -1; } break; case 66: switch (token) { case 'g': parser->stack[++parser->top].state = 68; return 1; default: return -1; } break; case 67: switch (token) { case 'r': parser->stack[++parser->top].state = 69; return 1; default: return -1; } break; case 68: switch (token) { case TOK_BLOCK: parser->stack[++parser->top].state = 70; return 1; default: return -1; } break; case 69: switch (token) { case TOK_CHAR: parser->stack[++parser->top].state = 28; return 1; case TOK_SYMBOL_LC: parser->stack[++parser->top].state = 31; return 1; case TOK_SYMBOL_UC: parser->stack[++parser->top].state = 32; return 1; default: return -1; } break; case 70: switch (token) { case TOK_SYMBOL: parser->stack[++parser->top].state = 47; return 1; case TOK_SYMBOL_LC: parser->stack[++parser->top].state = 48; return 1; case TOK_SYMBOL_UC: parser->stack[++parser->top].state = 49; return 1; default: return -1; } break; case 71: switch (token) { case TOK_SYMBOL: parser->stack[++parser->top].state = 47; return 1; case TOK_SYMBOL_LC: parser->stack[++parser->top].state = 48; return 1; case TOK_SYMBOL_UC: parser->stack[++parser->top].state = 49; return 1; default: return -1; } break; case 72: switch (token) { default: parser->top -= 12; parse_reduce_8(parser->stack[parser->top + 10].value.token.str, parser->stack[parser->top + 11].value.symbol_varname, grammar); free_string(parser->stack[parser->top + 10].value.token.str); free_string(parser->stack[parser->top + 11].value.symbol_varname); parser->stack[++parser->top].state = 2; } break; case 73: switch (token) { default: parser->top -= 13; parse_reduce_5(parser->stack[parser->top + 11].value.symbol_symbol, parser->stack[parser->top + 12].value.symbol_varname, grammar); free_symbol(parser->stack[parser->top + 11].value.symbol_symbol); free_string(parser->stack[parser->top + 12].value.symbol_varname); parser->stack[++parser->top].state = 2; } break; } } } int parse_push(parse_context_t *parser, int token, const parse_token_value_t *value, __attribute__((unused)) solar::grammar_t * grammar) { int ret = parse_do_push(parser, token, grammar); if (ret > 0) parser->stack[parser->top-1].value.token = *value; return ret; }