Allow specifying actions for groups terminals

Also, more refactoring.
This commit is contained in:
Matthias Schiffer 2015-04-18 20:06:23 +02:00
parent 30c2141182
commit ba8f9797cb
3 changed files with 51 additions and 19 deletions

View file

@ -48,14 +48,14 @@ struct grammar_t {
std::vector<std::pair<std::string, std::string>> extra_args; std::vector<std::pair<std::string, std::string>> extra_args;
void add_rule(const rule_t &rule) { void add_rule(const item_t &item, const std::vector<std::pair<std::string, bool>> &variables, const std::string &action) {
if (rules.empty()) { if (rules.empty()) {
item_t init(""); item_t init("");
init.get_rhs().push_back(symbol_t::make_nonterm(rule.item.get_lhs())); init.get_rhs().push_back(symbol_t::make_nonterm(item.get_lhs()));
rules.emplace_back(solar::rule_t {std::move(init), std::vector<std::pair<std::string, bool>>(), std::string()}); rules.push_back({std::move(init), std::vector<std::pair<std::string, bool>>(), std::string()});
} }
rules.push_back(rule); rules.push_back({item, variables, action});
} }
const std::string & get_nonterm_type(const std::string &sym) const { const std::string & get_nonterm_type(const std::string &sym) const {

View file

@ -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) { 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) { static inline void parse_reduce_11(std::string *lhs, unsigned char c1, unsigned char c2, std::string *action, __attribute__((unused)) grammar_t *grammar) {
vars_t vars(1);
for (unsigned int c = c1; c <= c2; c++) 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;} 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; parser->top -= 1;
result.symbol_action = parse_reduce_20(parser->stack[parser->top + 0].value.token.str, grammar); 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].value.symbol_action = result.symbol_action;
switch (parser->stack[parser->top].state) {
case 19:
parser->stack[++parser->top].state = 27; parser->stack[++parser->top].state = 27;
break;
case 79:
parser->stack[++parser->top].state = 83;
break;
}
} }
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); result.symbol_action = parse_reduce_21(parser->stack[parser->top + 0].value.token.str, grammar);
delete(parser->stack[parser->top + 0].value.token.str); delete(parser->stack[parser->top + 0].value.token.str);
parser->stack[parser->top].value.symbol_action = result.symbol_action; parser->stack[parser->top].value.symbol_action = result.symbol_action;
switch (parser->stack[parser->top].state) {
case 19:
parser->stack[++parser->top].state = 27; parser->stack[++parser->top].state = 27;
break;
case 79:
parser->stack[++parser->top].state = 83;
break;
}
} }
break; break;
@ -629,7 +645,16 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
parser->top -= 1; parser->top -= 1;
result.symbol_action = parse_reduce_19(grammar); result.symbol_action = parse_reduce_19(grammar);
parser->stack[parser->top].value.symbol_action = result.symbol_action; parser->stack[parser->top].value.symbol_action = result.symbol_action;
switch (parser->stack[parser->top].state) {
case 19:
parser->stack[++parser->top].state = 27; parser->stack[++parser->top].state = 27;
break;
case 79:
parser->stack[++parser->top].state = 83;
break;
}
} }
break; break;
@ -1162,8 +1187,16 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
case 79: case 79:
switch (token) { 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 ';': case ';':
parser->stack[++parser->top].state = 83; parser->stack[++parser->top].state = 36;
return 1; return 1;
default: default:
@ -1212,8 +1245,9 @@ static int parse_do_push(parse_context_t *parser, int token, __attribute__((unus
switch (token) { switch (token) {
default: default:
parser->top -= 11; 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 + 0].value.token.str);
delete(parser->stack[parser->top + 10].value.symbol_action);
parser->stack[++parser->top].state = 2; parser->stack[++parser->top].state = 2;
} }
break; break;

View file

@ -69,14 +69,12 @@ directive |= "%extra_arg" BLOCK(type) csymbol(name) {
} }
directive |= SYMBOL(lhs) "|=" rhs(rhs) action(action) { 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) ')' ';' { directive |= SYMBOL(lhs) "|=" '(' CHAR(c1) "..." CHAR(c2) ')' action(action) {
vars_t vars(1);
for (unsigned int c = c1; c <= c2; c++) 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);
} }