]> granicus.if.org Git - re2c/commitdiff
Continued adding "--skeleton" switch.
authorUlya Trofimovich <skvadrik@gmail.com>
Fri, 3 Apr 2015 21:30:13 +0000 (22:30 +0100)
committerUlya Trofimovich <skvadrik@gmail.com>
Fri, 3 Apr 2015 21:30:13 +0000 (22:30 +0100)
Tried to sort out various DFA states and how they influence code
generation. Added explicit type field to all actions.

re2c/code.cc
re2c/dfa.h

index 1a3827f640a797789bb305fab838ccbce1b469a4..3e5a4defa07df11f639507a4df13b95a8fefe4ba 100644 (file)
@@ -439,22 +439,11 @@ void Save::emit(Output & output, uint ind, bool &readCh, const std::string&) con
        }
 }
 
-Move::Move(State *s) : Action(s)
-{
-       ;
-}
-
 void Move::emit(Output &, uint, bool &, const std::string&) const
 {
        ;
 }
 
-Accept::Accept(State *x, uint n, uint *s, State **r)
-               : Action(x), nRules(n), saves(s), rules(r)
-{
-       ;
-}
-
 void Accept::genRuleMap()
 {
        for (uint i = 0; i < nRules; ++i)
@@ -564,11 +553,6 @@ void Accept::emit(Output & output, uint ind, bool &readCh, const std::string &)
        }
 }
 
-Rule::Rule(State *s, RuleOp *r) : Action(s), rule(r)
-{
-       ;
-}
-
 void Rule::emit(Output & output, uint ind, bool &, const std::string& condName) const
 {
        OutputFile & o = output.source;
@@ -1136,31 +1120,49 @@ void DFA::output_skeleton_epilog (OutputFile & o, uint ind)
 
 static void generate_data (State * s, bool def, const std::vector<std::vector<uint> > & xs, std::vector<std::pair<std::vector<uint>, bool> > & ys)
 {
-       if (s->go.nSpans <= 1)
+       switch (s->action->type)
        {
-               for (uint i = 0; i < xs.size (); ++i)
-               {
-                       ys.push_back (std::make_pair (std::vector<uint> (xs[i]), def));
-               }
-       }
-       else if (s->generated)
-       {
-               return;
-       }
-       else
-       {
-               s->generated = true;
-               for (uint i = 0; i < s->go.nSpans; ++i)
-               {
-                       std::vector<std::vector<uint> > zs;
-                       for (uint j = 0; j < xs.size (); ++j)
+               case Action::NONE:
+                       assert (false);
+                       break;
+               case Action::RULE:
+               case Action::ACCEPT:
+                       for (uint i = 0; i < xs.size (); ++i)
                        {
-                               std::vector<uint> z (xs[j]);
-                               z.push_back (s->go.span[i].ub - 1);
-                               zs.push_back (z);
+                               ys.push_back (std::make_pair (std::vector<uint> (xs[i]), def));
+                               if (!def)
+                               {
+                                       ys.back ().first.pop_back ();
+                               }
                        }
-                       generate_data (s->go.span[i].to, s->go.span[i].is_default, zs, ys);
-               }
+                       break;
+               case Action::MOVE:
+                       if (!s->generated)
+                       {
+                               s->generated = true;
+                               generate_data (s->go.span[0].to, s->go.span[0].is_default, xs, ys);
+                       }
+                       break;
+               case Action::MATCH:
+               case Action::ENTER:
+               case Action::INITIAL:
+               case Action::SAVE:
+                       if (!s->generated)
+                       {
+                               s->generated = true;
+                               for (uint i = 0; i < s->go.nSpans; ++i)
+                               {
+                                       std::vector<std::vector<uint> > zs;
+                                       for (uint j = 0; j < xs.size (); ++j)
+                                       {
+                                               std::vector<uint> z (xs[j]);
+                                               z.push_back (s->go.span[i].ub - 1);
+                                               zs.push_back (z);
+                                       }
+                                       generate_data (s->go.span[i].to, s->go.span[i].is_default, zs, ys);
+                               }
+                       }
+                       break;
        }
 }
 
@@ -1207,11 +1209,6 @@ void DFA::emit(Output & output, uint& ind, const RegExpMap* specMap, const std::
 {
        OutputFile & o = output.source;
 
-       if (flag_skeleton)
-       {
-               output_skeleton_prolog (output, ind);
-       }
-
        bool bProlog = (!cFlag || !bWroteCondCheck);
 
        // In -c mode, the prolog needs its own label separate from start_label.
@@ -1267,6 +1264,10 @@ void DFA::emit(Output & output, uint& ind, const RegExpMap* specMap, const std::
        }
        next_fill_index = save_fill_index;
 
+       if (flag_skeleton)
+       {
+               output_skeleton_prolog (output, ind);
+       }
        // Generate prolog
        if (bProlog)
        {
@@ -1374,6 +1375,10 @@ void DFA::emit(Output & output, uint& ind, const RegExpMap* specMap, const std::
        {
                o << indent(--ind) << "}\n";
        }
+       if (flag_skeleton)
+       {
+               output_skeleton_epilog (o, ind);
+       }
 
        // Cleanup
        if (BitMap::first)
@@ -1383,11 +1388,6 @@ void DFA::emit(Output & output, uint& ind, const RegExpMap* specMap, const std::
        }
 
        bUseStartLabel = false;
-
-       if (flag_skeleton)
-       {
-               output_skeleton_epilog (o, ind);
-       }
 }
 
 static void output_state_goto_sub (std::ostream & o, uint ind, uint start_label, int cMin, int cMax)
index d528ec3ddac9b4eabd3df83656804f2b556563e2..6b9c34dc1a9805a47f1d4947929c6d606832572f 100644 (file)
@@ -20,6 +20,17 @@ class Action
 
 public:
        State   *state;
+       enum type_t
+       {
+               NONE,
+               MATCH,
+               ENTER,
+               INITIAL,
+               SAVE,
+               MOVE,
+               ACCEPT,
+               RULE
+       } type;
 
 public:
        Action(State*);
@@ -35,11 +46,13 @@ public:
 protected:
        Action(const Action& oth)
                : state(oth.state)
+               , type(oth.type)
        {
        }
        Action& operator = (const Action& oth)
        {
                state = oth.state;
+               type = oth.type;
                return *this;
        }
 #endif
@@ -256,7 +269,7 @@ public:
 #endif
 };
 
-inline Action::Action(State *s) : state(s)
+inline Action::Action(State *s) : state(s), type(NONE)
 {
        delete s->action;
        s->action = this;
@@ -287,7 +300,9 @@ inline bool Action::readAhead() const
 }
 
 inline Match::Match(State *s) : Action(s)
-{ }
+{
+       type = MATCH;
+}
 
 inline bool Match::isMatch() const
 {
@@ -295,10 +310,14 @@ inline bool Match::isMatch() const
 }
 
 inline Enter::Enter(State *s, uint l) : Action(s), label(l)
-{ }
+{
+       type = ENTER;
+}
 
 inline Initial::Initial(State *s, uint l, bool b) : Enter(s, l), setMarker(b)
-{ }
+{
+       type = INITIAL;
+}
 
 inline bool Initial::isInitial() const
 {
@@ -306,13 +325,31 @@ inline bool Initial::isInitial() const
 }
 
 inline Save::Save(State *s, uint i) : Match(s), selector(i)
-{ }
+{
+       type = SAVE;
+}
 
 inline bool Save::isMatch() const
 {
        return false;
 }
 
+inline Move::Move(State *s) : Action(s)
+{
+       type = MOVE;
+}
+
+inline Accept::Accept(State *x, uint n, uint *s, State **r)
+               : Action(x), nRules(n), saves(s), rules(r)
+{
+       type = ACCEPT;
+}
+
+inline Rule::Rule(State *s, RuleOp *r) : Action(s), rule(r)
+{
+       type = RULE;
+}
+
 inline bool Rule::isRule() const
 {
        return true;