From ba8f9797cb4f0c06d6a9ff11b51470a15eb81ee0 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 18 Apr 2015 20:06:23 +0200 Subject: Allow specifying actions for groups terminals Also, more refactoring. --- src/grammar.hpp | 8 ++++---- src/parse.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++---------- src/parse.y | 8 +++----- 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/grammar.hpp b/src/grammar.hpp index 6e766c7..41c6316 100644 --- a/src/grammar.hpp +++ b/src/grammar.hpp @@ -48,14 +48,14 @@ struct grammar_t { std::vector> extra_args; - void add_rule(const rule_t &rule) { + void add_rule(const item_t &item, const std::vector> &variables, const std::string &action) { if (rules.empty()) { item_t init(""); - init.get_rhs().push_back(symbol_t::make_nonterm(rule.item.get_lhs())); - rules.emplace_back(solar::rule_t {std::move(init), std::vector>(), std::string()}); + init.get_rhs().push_back(symbol_t::make_nonterm(item.get_lhs())); + rules.push_back({std::move(init), std::vector>(), std::string()}); } - rules.push_back(rule); + rules.push_back({item, variables, action}); } const std::string & get_nonterm_type(const std::string &sym) const { diff --git a/src/parse.cpp b/src/parse.cpp index 88245bf..05fad4d 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -76,14 +76,12 @@ static inline void parse_reduce_9(std::string *type, std::string *name, __attrib } static inline void parse_reduce_10(std::string *lhs, rhs_t *rhs, std::string *action, __attribute__((unused)) grammar_t *grammar) { - grammar->add_rule({item_t(*lhs, rhs->first), rhs->second, *action}); + grammar->add_rule(item_t(*lhs, rhs->first), rhs->second, *action); } -static inline void parse_reduce_11(std::string *lhs, unsigned char c1, unsigned char c2, __attribute__((unused)) grammar_t *grammar) { - vars_t vars(1); - +static inline void parse_reduce_11(std::string *lhs, unsigned char c1, unsigned char c2, std::string *action, __attribute__((unused)) grammar_t *grammar) { for (unsigned int c = c1; c <= c2; c++) - grammar->add_rule({item_t(*lhs, {symbol_t::make_char(c)}), vars, std::string()}); + grammar->add_rule(item_t(*lhs, {symbol_t::make_char(c)}), vars_t(1), *action); } static inline rhs_t * parse_reduce_12(__attribute__((unused)) grammar_t *grammar) {return new rhs_t;} @@ -536,7 +534,16 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus parser->top -= 1; result.symbol_action = parse_reduce_20(parser->stack[parser->top + 0].value.token.str, grammar); parser->stack[parser->top].value.symbol_action = result.symbol_action; - parser->stack[++parser->top].state = 27; + switch (parser->stack[parser->top].state) { + case 19: + parser->stack[++parser->top].state = 27; + break; + + case 79: + parser->stack[++parser->top].state = 83; + break; + + } } break; @@ -567,7 +574,16 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus result.symbol_action = parse_reduce_21(parser->stack[parser->top + 0].value.token.str, grammar); delete(parser->stack[parser->top + 0].value.token.str); parser->stack[parser->top].value.symbol_action = result.symbol_action; - parser->stack[++parser->top].state = 27; + switch (parser->stack[parser->top].state) { + case 19: + parser->stack[++parser->top].state = 27; + break; + + case 79: + parser->stack[++parser->top].state = 83; + break; + + } } break; @@ -629,7 +645,16 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus parser->top -= 1; result.symbol_action = parse_reduce_19(grammar); parser->stack[parser->top].value.symbol_action = result.symbol_action; - parser->stack[++parser->top].state = 27; + switch (parser->stack[parser->top].state) { + case 19: + parser->stack[++parser->top].state = 27; + break; + + case 79: + parser->stack[++parser->top].state = 83; + break; + + } } break; @@ -1162,8 +1187,16 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus case 79: switch (token) { + case TOK_BLOCK: + parser->stack[++parser->top].state = 30; + return 1; + + case TOK_SQBLOCK: + parser->stack[++parser->top].state = 32; + return 1; + case ';': - parser->stack[++parser->top].state = 83; + parser->stack[++parser->top].state = 36; return 1; default: @@ -1212,8 +1245,9 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus switch (token) { default: parser->top -= 11; - parse_reduce_11(parser->stack[parser->top + 0].value.token.str, parser->stack[parser->top + 4].value.token.c, parser->stack[parser->top + 8].value.token.c, grammar); + parse_reduce_11(parser->stack[parser->top + 0].value.token.str, parser->stack[parser->top + 4].value.token.c, parser->stack[parser->top + 8].value.token.c, parser->stack[parser->top + 10].value.symbol_action, grammar); delete(parser->stack[parser->top + 0].value.token.str); + delete(parser->stack[parser->top + 10].value.symbol_action); parser->stack[++parser->top].state = 2; } break; diff --git a/src/parse.y b/src/parse.y index db959df..0b8b992 100644 --- a/src/parse.y +++ b/src/parse.y @@ -69,14 +69,12 @@ directive |= "%extra_arg" BLOCK(type) csymbol(name) { } directive |= SYMBOL(lhs) "|=" rhs(rhs) action(action) { - grammar->add_rule({item_t(*lhs, rhs->first), rhs->second, *action}); + grammar->add_rule(item_t(*lhs, rhs->first), rhs->second, *action); } -directive |= SYMBOL(lhs) "|=" '(' CHAR(c1) "..." CHAR(c2) ')' ';' { - vars_t vars(1); - +directive |= SYMBOL(lhs) "|=" '(' CHAR(c1) "..." CHAR(c2) ')' action(action) { for (unsigned int c = c1; c <= c2; c++) - grammar->add_rule({item_t(*lhs, {symbol_t::make_char(c)}), vars, std::string()}); + grammar->add_rule(item_t(*lhs, {symbol_t::make_char(c)}), vars_t(1), *action); } -- cgit v1.2.3