From: Ulya Trofimovich Date: Fri, 3 Apr 2015 21:30:13 +0000 (+0100) Subject: Continued adding "--skeleton" switch. X-Git-Tag: 0.15~320 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=86d9f2f08c7cb3a018b267c7ab4d587f20855600;p=re2c Continued adding "--skeleton" switch. Tried to sort out various DFA states and how they influence code generation. Added explicit type field to all actions. --- diff --git a/re2c/code.cc b/re2c/code.cc index 1a3827f6..3e5a4def 100644 --- a/re2c/code.cc +++ b/re2c/code.cc @@ -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 > & xs, std::vector, 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 (xs[i]), def)); - } - } - else if (s->generated) - { - return; - } - else - { - s->generated = true; - for (uint i = 0; i < s->go.nSpans; ++i) - { - std::vector > 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 z (xs[j]); - z.push_back (s->go.span[i].ub - 1); - zs.push_back (z); + ys.push_back (std::make_pair (std::vector (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 > zs; + for (uint j = 0; j < xs.size (); ++j) + { + std::vector 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) diff --git a/re2c/dfa.h b/re2c/dfa.h index d528ec3d..6b9c34dc 100644 --- a/re2c/dfa.h +++ b/re2c/dfa.h @@ -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;