]> granicus.if.org Git - re2c/commitdiff
libre2c: simplified construction of configurations.
authorUlya Trofimovich <skvadrik@gmail.com>
Mon, 18 Feb 2019 22:15:50 +0000 (22:15 +0000)
committerUlya Trofimovich <skvadrik@gmail.com>
Mon, 18 Feb 2019 22:15:50 +0000 (22:15 +0000)
re2c/lib/regex_impl.h
re2c/lib/regexec_nfa_leftmost.cc
re2c/lib/regexec_nfa_posix.cc
re2c/lib/regexec_nfa_posix_trie.cc

index 82401848843b310092ba3e6717f057cc04606f41..4cfd0a0d6947a6cd3e6e0aa822e14dd4806219e3 100644 (file)
@@ -41,6 +41,10 @@ struct conf_t
     nfa_state_t *state;
     uint32_t origin;
     int32_t thist;
+
+    inline conf_t(): state(NULL), origin(0), thist(history_t::ROOT) {}
+    inline conf_t(nfa_state_t *s, uint32_t o, int32_t h)
+        : state(s), origin(o), thist(h) {}
 };
 
 struct ran_or_fin_t
index 7f2bff6940bfbde2e1e3dcfbe9a2a7eff308f9c1..70432b50a59e5c02e9e4032eb90c64604f0e4d36 100644 (file)
@@ -19,7 +19,7 @@ int regexec_nfa_leftmost(const regex_t *preg, const char *string
 {
     simctx_t ctx(preg, string);
 
-    const conf_t c0 = {ctx.nfa->root, 0, history_t::ROOT};
+    const conf_t c0(ctx.nfa->root, 0, history_t::ROOT);
     ctx.reach.push_back(c0);
     closure_leftmost(ctx);
 
@@ -75,7 +75,7 @@ void reach_on_symbol(simctx_t &ctx, uint32_t sym)
         if (s->type == nfa_state_t::RAN) {
             for (const Range *r = s->ran.ran; r; r = r->next()) {
                 if (r->lower() <= sym && sym < r->upper()) {
-                    conf_t c = {s->ran.out, s->coreid, history_t::ROOT};
+                    conf_t c(s->ran.out, s->coreid, history_t::ROOT);
                     reach.push_back(c);
                     update_offsets(ctx, *i);
                     break;
@@ -97,9 +97,11 @@ void closure_leftmost(simctx_t &ctx)
     state.clear();
     for (; !wl.empty(); ) {
 
-        conf_t x = wl.back();
-        wl.pop_back();
+        const conf_t &x = wl.back();
         nfa_state_t *n = x.state;
+        const uint32_t o = x.origin;
+        const int32_t h = x.thist;
+        wl.pop_back();
 
         if (n->clos != NOCLOS) continue;
 
@@ -108,19 +110,15 @@ void closure_leftmost(simctx_t &ctx)
 
         switch (n->type) {
             case nfa_state_t::NIL:
-                x.state = n->nil.out;
-                wl.push_back(x);
+                wl.push_back(conf_t(n->nil.out, o, h));
                 break;
             case nfa_state_t::ALT:
-                x.state = n->alt.out2;
-                wl.push_back(x);
-                x.state = n->alt.out1;
-                wl.push_back(x);
+                wl.push_back(conf_t(n->alt.out2, o, h));
+                wl.push_back(conf_t(n->alt.out1, o, h));
                 break;
             case nfa_state_t::TAG:
-                x.state = n->tag.out;
-                x.thist = ctx.hist.push(x.thist, ctx.step, n->tag.info, x.origin);
-                wl.push_back(x);
+                wl.push_back(conf_t(n->tag.out, o
+                    , ctx.hist.push(h, ctx.step, n->tag.info, o)));
                 break;
             default:
                 break;
index 6b23e6be65c244d653e235ccd9477a9b03abe80c..6b03498ebee306d5ed82f44edb4f0cd0d2cf6257 100644 (file)
@@ -40,7 +40,7 @@ int regexec_nfa_posix(const regex_t *preg, const char *string
     simctx_t ctx(preg, string);
     const nfa_t *nfa = ctx.nfa;
 
-    const conf_t c0 = {nfa->root, 0, history_t::ROOT};
+    const conf_t c0(nfa->root, 0, history_t::ROOT);
     ctx.reach.push_back(c0);
     closure_posix(ctx);
 
@@ -102,7 +102,7 @@ void reach_on_symbol(simctx_t &ctx, uint32_t sym)
         if (s->type == nfa_state_t::RAN) {
             for (const Range *r = s->ran.ran; r; r = r->next()) {
                 if (r->lower() <= sym && sym < r->upper()) {
-                    conf_t c = {s->ran.out, s->coreid, history_t::ROOT};
+                    const conf_t c(s->ran.out, s->coreid, history_t::ROOT);
                     reach.push_back(c);
                     state[j++] = *i;
                     break;
@@ -190,38 +190,39 @@ inline bool cmp_gor1_t::operator()(const conf_t &x, const conf_t &y) const
 bool scan(simctx_t &ctx, nfa_state_t *q, bool all)
 {
     bool any = false;
-    conf_t x = ctx.state[q->clos];
+
+    const conf_t &x = ctx.state[q->clos];
+    const uint32_t o = x.origin;
+    const int32_t h = x.thist;
+
     switch (q->type) {
         case nfa_state_t::NIL:
             if (q->arcidx == 0) {
-                x.state = q->nil.out;
-                any |= relax_gor1(ctx, x);
+                any |= relax_gor1(ctx, conf_t(q->nil.out, o, h));
                 ++q->arcidx;
             }
             break;
         case nfa_state_t::ALT:
             if (q->arcidx == 0) {
-                x.state = q->alt.out1;
-                any |= relax_gor1(ctx, x);
+                any |= relax_gor1(ctx, conf_t(q->alt.out1, o, h));
                 ++q->arcidx;
             }
             if (q->arcidx == 1 && (!any || all)) {
-                x.state = q->alt.out2;
-                any |= relax_gor1(ctx, x);
+                any |= relax_gor1(ctx, conf_t(q->alt.out2, o, h));
                 ++q->arcidx;
             }
             break;
         case nfa_state_t::TAG:
             if (q->arcidx == 0) {
-                x.state = q->tag.out;
-                x.thist = ctx.hist.push(x.thist, ctx.step, q->tag.info, x.origin);
-                any |= relax_gor1(ctx, x);
+                any |= relax_gor1(ctx, conf_t(q->tag.out, o
+                    , ctx.hist.push(h, ctx.step, q->tag.info, o)));
                 ++q->arcidx;
             }
             break;
         default:
             break;
     }
+
     return any;
 }
 
@@ -270,23 +271,22 @@ void closure_posix_gtop(simctx_t &ctx)
         nfa_state_t *q = heap.top();
         heap.pop();
         q->active = 0;
-        conf_t x = state[q->clos];
+
+        const conf_t &x = state[q->clos];
+        const uint32_t o = x.origin;
+        const int32_t h = x.thist;
 
         switch (q->type) {
             case nfa_state_t::NIL:
-                x.state = q->nil.out;
-                relax_gtop(ctx, x);
+                relax_gtop(ctx, conf_t(q->nil.out, o, h));
                 break;
             case nfa_state_t::ALT:
-                x.state = q->alt.out1;
-                relax_gtop(ctx, x);
-                x.state = q->alt.out2;
-                relax_gtop(ctx, x);
+                relax_gtop(ctx, conf_t(q->alt.out1, o, h));
+                relax_gtop(ctx, conf_t(q->alt.out2, o, h));
                 break;
             case nfa_state_t::TAG:
-                x.state = q->tag.out;
-                x.thist = ctx.hist.push(x.thist, ctx.step, q->tag.info, x.origin);
-                relax_gtop(ctx, x);
+                relax_gtop(ctx, conf_t(q->tag.out, o
+                    , ctx.hist.push(h, ctx.step, q->tag.info, o)));
                 break;
             default:
                 break;
index f251436b840b62df7f2464ff5bb82e1fca88c0cc..68422656682ef0ff0e6e11d28ba482b83f588c21 100644 (file)
@@ -56,7 +56,7 @@ int regexec_nfa_posix_trie(const regex_t *preg, const char *string
     const nfa_t *nfa = ctx.nfa;
     confset_t &state = ctx.state;
 
-    const conf_t c0 = {nfa->root, index(nfa, nfa->root), history_t::ROOT};
+    const conf_t c0(nfa->root, index(nfa, nfa->root), history_t::ROOT);
     ctx.reach.push_back(c0);
     closure_posix(ctx);
 
@@ -94,7 +94,7 @@ void reach_on_symbol(simctx_t &ctx, uint32_t sym)
         if (s->type == nfa_state_t::RAN) {
             for (const Range *r = s->ran.ran; r; r = r->next()) {
                 if (r->lower() <= sym && sym < r->upper()) {
-                    conf_t c = {s->ran.out, index(nfa, s), i->thist};
+                    const conf_t c(s->ran.out, index(nfa, s), i->thist);
                     reach.push_back(c);
                     break;
                 }
@@ -119,23 +119,22 @@ void closure_posix(simctx_t &ctx)
         nfa_state_t *q = heap.top();
         heap.pop();
         q->active = 0;
-        conf_t x = state[q->clos];
+
+        const conf_t &x = state[q->clos];
+        const uint32_t o = x.origin;
+        const int32_t h = x.thist;
 
         switch (q->type) {
             case nfa_state_t::NIL:
-                x.state = q->nil.out;
-                relax(ctx, x);
+                relax(ctx, conf_t(q->nil.out, o, h));
                 break;
             case nfa_state_t::ALT:
-                x.state = q->alt.out1;
-                relax(ctx, x);
-                x.state = q->alt.out2;
-                relax(ctx, x);
+                relax(ctx, conf_t(q->alt.out1, o, h));
+                relax(ctx, conf_t(q->alt.out2, o, h));
                 break;
             case nfa_state_t::TAG:
-                x.state = q->tag.out;
-                x.thist = ctx.hist.push(x.thist, ctx.step, q->tag.info, x.origin);
-                relax(ctx, x);
+                relax(ctx, conf_t(q->tag.out, o
+                    , ctx.hist.push(h, ctx.step, q->tag.info, o)));
                 break;
             case nfa_state_t::FIN:
                 ctx.marker = ctx.cursor + 1;