]> granicus.if.org Git - re2c/commitdiff
Reuse 'Code' data struct for setup code.
authorUlya Trofimovich <skvadrik@gmail.com>
Sat, 24 Dec 2016 12:00:59 +0000 (12:00 +0000)
committerUlya Trofimovich <skvadrik@gmail.com>
Sat, 24 Dec 2016 12:00:59 +0000 (12:00 +0000)
re2c/bootstrap/src/parse/lex.cc
re2c/bootstrap/src/parse/parser.cc
re2c/src/ir/regexp/regexp.h
re2c/src/ir/rule.cc
re2c/src/ir/rule.h
re2c/src/parse/parser.h
re2c/src/parse/parser.ypp

index 5144e5da0d327620a6d8f5e47d4b9028362dfddf..4f7f942e50117b7308e7776cac944412e2761a12 100644 (file)
@@ -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 <stddef.h>
index d45910e14f321e3b49192e3ae4ddf84c2b3f37c1..880aefc3a7ddfe2d9ef74114a78fdd92b9e0ae2b 100644 (file)
@@ -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 "";
 }
index d37439b81e9e95c458c3d9b6e7e3238e6ce26b4b..94f864841b154c5e44663f92855ee0b00d5e7789 100644 (file)
@@ -108,7 +108,7 @@ struct RegExpRule
        static free_list<RegExpRule*> 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; }
index 6b55421d60828cbabe678b329d2458934bf4e613..aa9fa19cf1d44ddb9dd22519b5d94fedb62dbb11 100644 (file)
@@ -5,6 +5,8 @@
 namespace re2c
 {
 
+free_list<Code*> Code::flist;
+
 const size_t Rule::NONE = std::numeric_limits<size_t>::max();
 
 } // namespace re2c
index fbe3c89b4925ba92cae8c385978df078505c0fcd..b2a5d67a73ba0a063c0d99f050acafd37e1191e0 100644 (file)
@@ -6,6 +6,7 @@
 #include <string>
 
 #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<Code*> 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
index b6cde817165abf10750c8b78ea2259b2cf91e226..36524248116f1b18ebf6a70d548837e09b66e468 100644 (file)
@@ -20,7 +20,7 @@ void parse(Scanner &, Output &);
 typedef std::set<std::string> CondList;
 typedef std::vector<const RegExpRule*> Spec;
 typedef std::map<std::string, Spec> SpecMap;
-typedef std::map<std::string, std::pair<uint32_t, std::string> > SetupMap;
+typedef std::map<std::string, const Code*> SetupMap;
 typedef std::map<std::string, const RegExp *> symbol_table_t;
 typedef std::map<std::string, smart_ptr<DFA> > 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()
index be0d93e461183f0db0d03f21197478342a91b16f..d18bb79ef68b0f5ec95dd03695224731ebb4c80c 100644 (file)
@@ -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 "";
 }