From 59db91599c0097b80a7ed61c6bb71e046f4d9f54 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 17 Apr 2015 22:27:25 +0200 Subject: Move initial rule handling to grammer_t --- src/grammar.hpp | 10 ++++++++++ src/parse.cpp | 8 +------- src/parse.y | 8 +------- 3 files changed, 12 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/grammar.hpp b/src/grammar.hpp index b54b46d..cc982b6 100644 --- a/src/grammar.hpp +++ b/src/grammar.hpp @@ -47,6 +47,16 @@ struct grammar_t { std::vector> extra_args; + void add_rule(const rule_t &rule) { + 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()}); + } + + rules.push_back(rule); + } + const std::string & get_nonterm_type(const std::string &sym) const { static const std::string empty; diff --git a/src/parse.cpp b/src/parse.cpp index 9a24c03..bad9e24 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -80,13 +80,7 @@ static inline void parse_reduce_8(std::string *type, std::string *name, __attrib } 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); + grammar->add_rule(*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};} diff --git a/src/parse.y b/src/parse.y index 56fc567..8d8a081 100644 --- a/src/parse.y +++ b/src/parse.y @@ -94,13 +94,7 @@ directive |= "%extra_arg" BLOCK(type) varname(name) { } directive |= rule(rule) { - 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); + grammar->add_rule(*rule); } -- cgit v1.2.3