From 2612c36499a77ff791ac6b2080cb681947f203da Mon Sep 17 00:00:00 2001 From: Ulya Trofimovich Date: Sat, 24 Dec 2016 12:00:59 +0000 Subject: [PATCH] Reuse 'Code' data struct for setup code. --- re2c/bootstrap/src/parse/lex.cc | 2 +- re2c/bootstrap/src/parse/parser.cc | 22 +++++++++++----------- re2c/src/ir/regexp/regexp.h | 3 +-- re2c/src/ir/rule.cc | 2 ++ re2c/src/ir/rule.h | 15 +++++++++++++-- re2c/src/parse/parser.h | 4 ++-- re2c/src/parse/parser.ypp | 22 +++++++++++----------- 7 files changed, 41 insertions(+), 29 deletions(-) diff --git a/re2c/bootstrap/src/parse/lex.cc b/re2c/bootstrap/src/parse/lex.cc index 5144e5da..4f7f942e 100644 --- a/re2c/bootstrap/src/parse/lex.cc +++ b/re2c/bootstrap/src/parse/lex.cc @@ -1,4 +1,4 @@ -/* Generated by re2c 0.16 on Sat Dec 24 11:18:49 2016 */ +/* Generated by re2c 0.16 on Sat Dec 24 11:38:35 2016 */ #line 1 "../src/parse/lex.re" #include "src/util/c99_stdint.h" #include diff --git a/re2c/bootstrap/src/parse/parser.cc b/re2c/bootstrap/src/parse/parser.cc index d45910e1..880aefc3 100644 --- a/re2c/bootstrap/src/parse/parser.cc +++ b/re2c/bootstrap/src/parse/parser.cc @@ -116,6 +116,7 @@ void yyerror(Scanner &in, context_t&, const char*); static void parse_cleanup(re2c::context_t &context) { RegExp::flist.clear(); + Code::flist.clear(); Range::vFreeList.clear(); RangeSuffix::freeList.clear(); context.clear(); @@ -157,7 +158,7 @@ static void check(const context_t &context, bool cflag) l = std::min(l, context.spec_all[0]->code->fline); } if (!setups.empty()) { - l = std::min(l, setups.begin()->second.first); + l = std::min(l, setups.begin()->second->fline); } if (context.startup) { l = std::min(l, context.startup->fline); @@ -177,7 +178,7 @@ static void check(const context_t &context, bool cflag) const std::string c = i->first; if (c != "*" && specs.find(c) == specs.end()) { error("line %u: setup for non existing condition '%s' found", - i->second.first, c.c_str()); + i->second->fline, c.c_str()); exit(1); } } @@ -186,7 +187,7 @@ static void check(const context_t &context, bool cflag) if (i != setups.end()) { error("line %u: setup for all conditions '' is illegal " "if setup for each condition is defined explicitly", - i->second.first); + i->second->fline); exit(1); } } @@ -199,14 +200,14 @@ static void delay_default(Spec &spec) std::stable_partition(spec.begin(), spec.end(), RegExpRule::isnt_def); } -static void make_rule(context_t &context, RegExpRule *rule, Code *code) +static void make_rule(context_t &context, RegExpRule *rule, const Code *code) { rule->code = code; context.specMap[""].push_back(rule); } static void make_cond(context_t &context, CondList *clist, - RegExpRule *rule, Code *code) + RegExpRule *rule, const Code *code) { rule->code = code; for(CondList::const_iterator i = clist->begin(); i != clist->end(); ++i) { @@ -219,13 +220,13 @@ static void make_cond(context_t &context, CondList *clist, delete clist; } -static void make_star(context_t &context, RegExpRule *rule, Code *code) +static void make_star(context_t &context, RegExpRule *rule, const Code *code) { rule->code = code; context.spec_all.push_back(rule); } -static void make_zero(context_t &context, Code *code) +static void make_zero(context_t &context, const Code *code) { if (context.startup) { error("line %u: startup code is already defined at line %u", @@ -247,9 +248,8 @@ static void make_setup(Scanner &in, SetupMap &ruleSetupMap, if (ruleSetupMap.find(*i) != ruleSetupMap.end()) { in.fatalf_at(code->fline, "code to setup rule '%s' is already defined", i->c_str()); } - ruleSetupMap[*i] = std::make_pair(code->fline, code->text); + ruleSetupMap[*i] = code; } - delete code; delete clist; } @@ -258,10 +258,10 @@ static std::string find_setup_rule(const SetupMap &map, const std::string &key) SetupMap::const_iterator e = map.end(), i; i = map.find(key); - if (i != e) return i->second.second; + if (i != e) return i->second->text; i = map.find("*"); - if (i != e) return i->second.second; + if (i != e) return i->second->text; return ""; } diff --git a/re2c/src/ir/regexp/regexp.h b/re2c/src/ir/regexp/regexp.h index d37439b8..94f86484 100644 --- a/re2c/src/ir/regexp/regexp.h +++ b/re2c/src/ir/regexp/regexp.h @@ -108,7 +108,7 @@ struct RegExpRule static free_list flist; const RegExp *re; - Code *code; + const Code *code; bool def; RegExpRule(const RegExp *r, bool d) @@ -120,7 +120,6 @@ struct RegExpRule } ~RegExpRule() { - delete code; flist.erase(this); } static bool is_def(const RegExpRule *r) { return r->def; } diff --git a/re2c/src/ir/rule.cc b/re2c/src/ir/rule.cc index 6b55421d..aa9fa19c 100644 --- a/re2c/src/ir/rule.cc +++ b/re2c/src/ir/rule.cc @@ -5,6 +5,8 @@ namespace re2c { +free_list Code::flist; + const size_t Rule::NONE = std::numeric_limits::max(); } // namespace re2c diff --git a/re2c/src/ir/rule.h b/re2c/src/ir/rule.h index fbe3c89b..b2a5d67a 100644 --- a/re2c/src/ir/rule.h +++ b/re2c/src/ir/rule.h @@ -6,6 +6,7 @@ #include #include "src/ir/tag.h" +#include "src/util/free_list.h" #include "src/util/forbid_copy.h" namespace re2c @@ -13,6 +14,8 @@ namespace re2c struct Code { + static free_list flist; + std::string fname; uint32_t fline; bool autogen; @@ -25,14 +28,22 @@ struct Code , autogen(true) , text("") , cond("") - {} + { + flist.insert(this); + } Code(const std::string &file, uint32_t line, const char *s, size_t slen) : fname(file) , fline(line) , autogen(false) , text(s, slen) , cond("") - {} + { + flist.insert(this); + } + ~Code() + { + flist.erase(this); + } }; struct Rule diff --git a/re2c/src/parse/parser.h b/re2c/src/parse/parser.h index b6cde817..36524248 100644 --- a/re2c/src/parse/parser.h +++ b/re2c/src/parse/parser.h @@ -20,7 +20,7 @@ void parse(Scanner &, Output &); typedef std::set CondList; typedef std::vector Spec; typedef std::map SpecMap; -typedef std::map > SetupMap; +typedef std::map SetupMap; typedef std::map symbol_table_t; typedef std::map > dfa_map_t; @@ -30,7 +30,7 @@ struct context_t SpecMap specMap; Spec spec_all; SetupMap ruleSetupMap; - Code *startup; + const Code *startup; symbol_table_t symbol_table; context_t() diff --git a/re2c/src/parse/parser.ypp b/re2c/src/parse/parser.ypp index be0d93e4..d18bb79e 100644 --- a/re2c/src/parse/parser.ypp +++ b/re2c/src/parse/parser.ypp @@ -48,6 +48,7 @@ void yyerror(Scanner &in, context_t&, const char*); static void parse_cleanup(re2c::context_t &context) { RegExp::flist.clear(); + Code::flist.clear(); Range::vFreeList.clear(); RangeSuffix::freeList.clear(); context.clear(); @@ -89,7 +90,7 @@ static void check(const context_t &context, bool cflag) l = std::min(l, context.spec_all[0]->code->fline); } if (!setups.empty()) { - l = std::min(l, setups.begin()->second.first); + l = std::min(l, setups.begin()->second->fline); } if (context.startup) { l = std::min(l, context.startup->fline); @@ -109,7 +110,7 @@ static void check(const context_t &context, bool cflag) const std::string c = i->first; if (c != "*" && specs.find(c) == specs.end()) { error("line %u: setup for non existing condition '%s' found", - i->second.first, c.c_str()); + i->second->fline, c.c_str()); exit(1); } } @@ -118,7 +119,7 @@ static void check(const context_t &context, bool cflag) if (i != setups.end()) { error("line %u: setup for all conditions '' is illegal " "if setup for each condition is defined explicitly", - i->second.first); + i->second->fline); exit(1); } } @@ -131,14 +132,14 @@ static void delay_default(Spec &spec) std::stable_partition(spec.begin(), spec.end(), RegExpRule::isnt_def); } -static void make_rule(context_t &context, RegExpRule *rule, Code *code) +static void make_rule(context_t &context, RegExpRule *rule, const Code *code) { rule->code = code; context.specMap[""].push_back(rule); } static void make_cond(context_t &context, CondList *clist, - RegExpRule *rule, Code *code) + RegExpRule *rule, const Code *code) { rule->code = code; for(CondList::const_iterator i = clist->begin(); i != clist->end(); ++i) { @@ -151,13 +152,13 @@ static void make_cond(context_t &context, CondList *clist, delete clist; } -static void make_star(context_t &context, RegExpRule *rule, Code *code) +static void make_star(context_t &context, RegExpRule *rule, const Code *code) { rule->code = code; context.spec_all.push_back(rule); } -static void make_zero(context_t &context, Code *code) +static void make_zero(context_t &context, const Code *code) { if (context.startup) { error("line %u: startup code is already defined at line %u", @@ -179,9 +180,8 @@ static void make_setup(Scanner &in, SetupMap &ruleSetupMap, if (ruleSetupMap.find(*i) != ruleSetupMap.end()) { in.fatalf_at(code->fline, "code to setup rule '%s' is already defined", i->c_str()); } - ruleSetupMap[*i] = std::make_pair(code->fline, code->text); + ruleSetupMap[*i] = code; } - delete code; delete clist; } @@ -190,10 +190,10 @@ static std::string find_setup_rule(const SetupMap &map, const std::string &key) SetupMap::const_iterator e = map.end(), i; i = map.find(key); - if (i != e) return i->second.second; + if (i != e) return i->second->text; i = map.find("*"); - if (i != e) return i->second.second; + if (i != e) return i->second->text; return ""; } -- 2.40.0