From 1a79d217af4b7403b6567c269bf115bb1fad9495 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 11 Apr 2015 03:18:22 +0200 Subject: Allow skipping the destructor selectively --- src/parse.cpp | 276 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 159 insertions(+), 117 deletions(-) (limited to 'src/parse.cpp') diff --git a/src/parse.cpp b/src/parse.cpp index 4f2aece..0c2a7c4 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -1,7 +1,8 @@ #include "parse.hpp" -typedef std::pair, std::vector> rhs_t; +typedef std::vector> vars_t; +typedef std::pair, vars_t> rhs_t; static inline void free_string(std::string *v) { @@ -16,6 +17,10 @@ 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; @@ -77,21 +82,17 @@ static inline void parse_reduce_9(solar::rule_t * rule, __attribute__((unused)) 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), std::vector(), std::string()}); + 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) { - auto *ret = new solar::rule_t {solar::item_t(*lhs, rhs->first), rhs->second, *action}; - delete rhs; - return ret; -} +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_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); @@ -102,12 +103,19 @@ static inline rhs_t * parse_reduce_12(rhs_t * rhs, solar::symbol_t * sym, __attr 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.push_back(*var); + 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_14(rhs_t * rhs, std::string * str, __attribute__((unused)) solar::grammar_t * grammar) { +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(); @@ -116,45 +124,25 @@ static inline rhs_t * parse_reduce_14(rhs_t * rhs, std::string * str, __attribut return rhs; } -static inline std::string * parse_reduce_15(__attribute__((unused)) solar::grammar_t * grammar) { - return new std::string; -} +static inline std::string * parse_reduce_16(__attribute__((unused)) solar::grammar_t * grammar) {return new std::string;} -static inline std::string * parse_reduce_16(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) { - return new std::string(*v); -} +static inline std::string * parse_reduce_17(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) {return v;} -static inline std::string * parse_reduce_17(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) { - return new std::string("\n\treturn " + *v + ";\n"); -} +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_18(solar::symbol_t * v, __attribute__((unused)) solar::grammar_t * grammar) { - return new solar::symbol_t(*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_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_20(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_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_21(char v, __attribute__((unused)) solar::grammar_t * grammar) { - return new solar::symbol_t(solar::symbol_t::make_char(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_22(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) { - return new std::string(*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_23(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) { - return new std::string(*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_24(std::string * v, __attribute__((unused)) solar::grammar_t * grammar) { - return new std::string(*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; @@ -468,6 +456,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus 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; @@ -493,16 +482,15 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus switch (token) { default: parser->top -= 1; - result.symbol_symbol = parse_reduce_18(parser->stack[parser->top + 0].value.symbol_term, grammar); - free_symbol(parser->stack[parser->top + 0].value.symbol_term); + 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 66: - parser->stack[++parser->top].state = 68; + case 69: + parser->stack[++parser->top].state = 71; break; } @@ -513,8 +501,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus switch (token) { default: parser->top -= 1; - result.symbol_action = parse_reduce_16(parser->stack[parser->top + 0].value.token.str, grammar); - free_string(parser->stack[parser->top + 0].value.token.str); + 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; } @@ -524,11 +511,11 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus switch (token) { default: parser->top -= 1; - result.symbol_term = parse_reduce_21(parser->stack[parser->top + 0].value.token.c, grammar); + 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 66: + case 69: parser->stack[++parser->top].state = 26; break; @@ -544,7 +531,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus switch (token) { default: parser->top -= 1; - result.symbol_action = parse_reduce_17(parser->stack[parser->top + 0].value.token.str, grammar); + 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; @@ -555,7 +542,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus switch (token) { default: parser->top -= 2; - result.symbol_rhs = parse_reduce_14(parser->stack[parser->top + 0].value.symbol_rhs, parser->stack[parser->top + 1].value.token.str, grammar); + 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; @@ -574,8 +561,8 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus parser->stack[++parser->top].state = 25; break; - case 66: - parser->stack[++parser->top].state = 68; + case 69: + parser->stack[++parser->top].state = 71; break; } @@ -586,12 +573,12 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus switch (token) { default: parser->top -= 1; - result.symbol_term = parse_reduce_20(parser->stack[parser->top + 0].value.token.str, grammar); + 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 66: + case 69: parser->stack[++parser->top].state = 26; break; @@ -607,7 +594,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus switch (token) { default: parser->top -= 1; - result.symbol_action = parse_reduce_15(grammar); + result.symbol_action = parse_reduce_16(grammar); parser->stack[parser->top].value.symbol_action = result.symbol_action; parser->stack[++parser->top].state = 24; } @@ -690,6 +677,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus parser->stack[++parser->top].state = 49; return 1; + case '=': + parser->stack[++parser->top].state = 50; + return 1; + default: return -1; } @@ -698,7 +689,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus case 40: switch (token) { case 'u': - parser->stack[++parser->top].state = 50; + parser->stack[++parser->top].state = 51; return 1; default: @@ -709,7 +700,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus case 41: switch (token) { case '_': - parser->stack[++parser->top].state = 51; + parser->stack[++parser->top].state = 52; return 1; default: @@ -720,7 +711,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus case 42: switch (token) { case 'r': - parser->stack[++parser->top].state = 52; + parser->stack[++parser->top].state = 53; return 1; default: @@ -731,7 +722,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus case 43: switch (token) { case 'e': - parser->stack[++parser->top].state = 53; + parser->stack[++parser->top].state = 54; return 1; default: @@ -742,7 +733,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus case 44: switch (token) { case TOK_BLOCK: - parser->stack[++parser->top].state = 54; + parser->stack[++parser->top].state = 55; return 1; default: @@ -753,7 +744,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus case 45: switch (token) { case TOK_BLOCK: - parser->stack[++parser->top].state = 55; + parser->stack[++parser->top].state = 56; return 1; default: @@ -764,7 +755,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus case 46: switch (token) { case ')': - parser->stack[++parser->top].state = 56; + parser->stack[++parser->top].state = 57; return 1; default: @@ -776,24 +767,27 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus switch (token) { default: parser->top -= 1; - result.symbol_varname = parse_reduce_24(parser->stack[parser->top + 0].value.token.str, grammar); - free_string(parser->stack[parser->top + 0].value.token.str); + 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 54: - parser->stack[++parser->top].state = 61; + case 50: + parser->stack[++parser->top].state = 58; + break; + + case 55: + parser->stack[++parser->top].state = 63; break; - case 67: - parser->stack[++parser->top].state = 69; + case 70: + parser->stack[++parser->top].state = 72; break; - case 68: - parser->stack[++parser->top].state = 70; + case 71: + parser->stack[++parser->top].state = 73; break; } @@ -804,24 +798,27 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus switch (token) { default: parser->top -= 1; - result.symbol_varname = parse_reduce_22(parser->stack[parser->top + 0].value.token.str, grammar); - free_string(parser->stack[parser->top + 0].value.token.str); + 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 54: - parser->stack[++parser->top].state = 61; + case 50: + parser->stack[++parser->top].state = 58; + break; + + case 55: + parser->stack[++parser->top].state = 63; break; - case 67: - parser->stack[++parser->top].state = 69; + case 70: + parser->stack[++parser->top].state = 72; break; - case 68: - parser->stack[++parser->top].state = 70; + case 71: + parser->stack[++parser->top].state = 73; break; } @@ -832,24 +829,27 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus switch (token) { default: parser->top -= 1; - result.symbol_varname = parse_reduce_23(parser->stack[parser->top + 0].value.token.str, grammar); - free_string(parser->stack[parser->top + 0].value.token.str); + 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 54: - parser->stack[++parser->top].state = 61; + case 50: + parser->stack[++parser->top].state = 58; + break; + + case 55: + parser->stack[++parser->top].state = 63; break; - case 67: - parser->stack[++parser->top].state = 69; + case 70: + parser->stack[++parser->top].state = 72; break; - case 68: - parser->stack[++parser->top].state = 70; + case 71: + parser->stack[++parser->top].state = 73; break; } @@ -858,8 +858,16 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus case 50: switch (token) { - case 'c': - parser->stack[++parser->top].state = 57; + 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: @@ -869,8 +877,8 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus case 51: switch (token) { - case 'a': - parser->stack[++parser->top].state = 58; + case 'c': + parser->stack[++parser->top].state = 59; return 1; default: @@ -880,8 +888,8 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus case 52: switch (token) { - case TOK_BLOCK: - parser->stack[++parser->top].state = 59; + case 'a': + parser->stack[++parser->top].state = 60; return 1; default: @@ -892,7 +900,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus case 53: switch (token) { case TOK_BLOCK: - parser->stack[++parser->top].state = 60; + parser->stack[++parser->top].state = 61; return 1; default: @@ -901,6 +909,17 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus 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; @@ -919,7 +938,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 55: + case 56: switch (token) { default: parser->top -= 7; @@ -930,7 +949,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 56: + case 57: switch (token) { default: parser->top -= 5; @@ -942,10 +961,21 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 57: + 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 = 62; + parser->stack[++parser->top].state = 65; return 1; default: @@ -953,10 +983,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 58: + case 60: switch (token) { case 'r': - parser->stack[++parser->top].state = 63; + parser->stack[++parser->top].state = 66; return 1; default: @@ -964,7 +994,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 59: + case 61: switch (token) { default: parser->top -= 8; @@ -974,7 +1004,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 60: + case 62: switch (token) { default: parser->top -= 8; @@ -984,7 +1014,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 61: + case 63: switch (token) { default: parser->top -= 8; @@ -996,10 +1026,22 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 62: + 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 = 64; + parser->stack[++parser->top].state = 67; return 1; default: @@ -1007,10 +1049,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 63: + case 66: switch (token) { case 'g': - parser->stack[++parser->top].state = 65; + parser->stack[++parser->top].state = 68; return 1; default: @@ -1018,10 +1060,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 64: + case 67: switch (token) { case 'r': - parser->stack[++parser->top].state = 66; + parser->stack[++parser->top].state = 69; return 1; default: @@ -1029,10 +1071,10 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 65: + case 68: switch (token) { case TOK_BLOCK: - parser->stack[++parser->top].state = 67; + parser->stack[++parser->top].state = 70; return 1; default: @@ -1040,7 +1082,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 66: + case 69: switch (token) { case TOK_CHAR: parser->stack[++parser->top].state = 28; @@ -1059,7 +1101,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 67: + case 70: switch (token) { case TOK_SYMBOL: parser->stack[++parser->top].state = 47; @@ -1078,7 +1120,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 68: + case 71: switch (token) { case TOK_SYMBOL: parser->stack[++parser->top].state = 47; @@ -1097,7 +1139,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 69: + case 72: switch (token) { default: parser->top -= 12; @@ -1108,7 +1150,7 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus } break; - case 70: + case 73: switch (token) { default: parser->top -= 13; -- cgit v1.2.3