]> granicus.if.org Git - re2c/commitdiff
Better representation for rule actions; omit line info for autogenerated actions.
authorUlya Trofimovich <skvadrik@gmail.com>
Tue, 4 Aug 2015 12:38:57 +0000 (13:38 +0100)
committerUlya Trofimovich <skvadrik@gmail.com>
Tue, 4 Aug 2015 12:38:57 +0000 (13:38 +0100)
Since there's no such code in source file, there's no sense in
pointing into it. Updated test.

12 files changed:
re2c/Makefile.am
re2c/src/codegen/emit_action.cc
re2c/src/codegen/prepare_dfa.cc
re2c/src/ir/regexp/regexp_rule.h
re2c/src/parse/code.cc [new file with mode: 0644]
re2c/src/parse/code.h [new file with mode: 0644]
re2c/src/parse/parser.h
re2c/src/parse/parser.ypp
re2c/src/parse/scanner.h
re2c/src/parse/scanner_lex.re
re2c/src/parse/token.h [deleted file]
re2c/test/condition_13.cg.c

index d27ff3b37de83b19847c255bed3250758063ee29..3e33456f6f9cdb9aadda363630a9030fdf447777 100644 (file)
@@ -50,12 +50,12 @@ SRC_HDR = \
        src/ir/regexp/regexp_close.h \
        src/ir/rule_rank.h \
        src/globals.h \
+       src/parse/code.h \
        src/parse/extop.h \
        src/parse/input.h \
        src/parse/loc.h \
        src/parse/parser.h \
        src/parse/scanner.h \
-       src/parse/token.h \
        src/util/allocate.h \
        src/util/attribute.h \
        src/util/c99_stdint.h \
@@ -109,6 +109,7 @@ SRC = \
        src/ir/regexp/regexp.cc \
        src/ir/rule_rank.cc \
        src/main.cc \
+       src/parse/code.cc \
        src/parse/input.cc \
        src/parse/scanner.cc \
        src/parse/unescape.cc \
index 1d33c92409efd74f11e047ef8ede9c3ecfec861e..bc9717e1d46be85d8d1b7760031385e5d65fd879 100644 (file)
@@ -233,7 +233,7 @@ void emit_rule (OutputFile & o, uint32_t ind, const State * const s, const RuleO
 {
        if (DFlag)
        {
-               o << s->label << " [label=\"" << rule->code->loc.filename << ":" << rule->code->loc.line << "\"]\n";
+               o << s->label << " [label=\"" << rule->code.loc.filename << ":" << rule->code.loc.line << "\"]\n";
                return;
        }
 
@@ -243,37 +243,39 @@ void emit_rule (OutputFile & o, uint32_t ind, const State * const s, const RuleO
                o << input_api.stmt_restorectx (ind);
        }
 
-       if (rule->newcond.length() && condName != rule->newcond)
-       {
-               genSetCondition(o, ind, rule->newcond);
-       }
-
-       if (!yySetupRule.empty() && !rule->code->autogen)
-       {
-               o << indent(ind) << yySetupRule << "\n";
-       }
-
-       o.write_line_info (rule->code->loc.line, rule->code->loc.filename.c_str ());
-       o << indent(ind);
        if (flag_skeleton)
        {
-               o << "{ if (cursor == &data[result[i].endpos] && result[i].rule == " << rule->rank << ") ";
-               o << "{ cursor = &data[result[i].startpos]; continue; }";
-               o << " else ";
-               o << "{ printf (\"error: %lu/%u, %u/%u, '%s'\\n\", cursor - data, result[i].endpos, result[i].rule, "
+               o << indent (ind)
+                       << "{ if (cursor == &data[result[i].endpos] && result[i].rule == " << rule->rank << ") "
+                       << "{ cursor = &data[result[i].startpos]; continue; }"
+                       << " else "
+                       << "{ printf (\"error: %lu/%u, %u/%u, '%s'\\n\", cursor - data, result[i].endpos, result[i].rule, "
                        << rule->rank
-                       << ", &data[result[i].startpos]); return 1; } }";
-       }
-       else if (rule->code->autogen)
-       {
-               o << replaceParam(condGoto, condGotoParam, condPrefix + rule->newcond);
+                       << ", &data[result[i].startpos]); return 1; } }\n";
        }
        else
        {
-               o << rule->code->text;
+               if (rule->newcond.length() && condName != rule->newcond)
+               {
+                       genSetCondition(o, ind, rule->newcond);
+               }
+
+               const bool autogen = rule->code.text.empty ();
+               if (autogen)
+               {
+                       o << indent (ind) << replaceParam(condGoto, condGotoParam, condPrefix + rule->newcond) << "\n";
+               }
+               else
+               {
+                       if (!yySetupRule.empty ())
+                       {
+                               o << indent(ind) << yySetupRule << "\n";
+                       }
+                       o.write_line_info (rule->code.loc.line, rule->code.loc.filename.c_str ());
+                       o << indent (ind) << rule->code.text << "\n";
+                       o.insert_line_info ();
+               }
        }
-       o << "\n";
-       o.insert_line_info ();
 }
 
 void need (OutputFile & o, uint32_t ind, bool & readCh, uint32_t n, bool bSetMarker)
index 6ea355e7f6c0491629bf54e732d43eec1de2acc7..1b72b7d82fbc33682b515692014862c35988532b 100644 (file)
@@ -257,7 +257,7 @@ void DFA::prepare(OutputFile & o, uint32_t & max_fill, const std::string & cond)
        // warn about not shadowed rule that matches empty string
        if (empty_rule && !stray_cunits.empty ())
        {
-               warn.match_empty_string (head->rule->code->loc.line);
+               warn.match_empty_string (head->rule->code.loc.line);
        }
 
        // split ``base'' states into two parts
index a85751ffdf5d67c0d8f2cd6b042ca7b8bf6a5349..53a61089cbd49b21c7feb6c80b69aca714e1e98f 100644 (file)
@@ -5,7 +5,7 @@
 
 #include "src/ir/regexp/regexp.h"
 #include "src/ir/rule_rank.h"
-#include "src/parse/token.h"
+#include "src/parse/code.h"
 
 namespace re2c
 {
@@ -18,10 +18,10 @@ public:
        RegExp * ctx;
        Ins * ins;
        rule_rank_t rank;
-       Token * code;
+       const Code & code;
        const std::string newcond;
 
-       inline RuleOp (RegExp * e, RegExp * c, Token * t, rule_rank_t r, InsAccess access, const std::string * n)
+       inline RuleOp (RegExp * e, RegExp * c, const Code & t, rule_rank_t r, InsAccess access, const std::string * n)
                : exp (e)
                , ctx (c)
                , ins (NULL)
@@ -31,10 +31,6 @@ public:
        {
                ins_access = access;
        }
-       inline ~RuleOp ()
-       {
-               delete code;
-       }
        void display (std::ostream & o) const;
        void split (CharSet &);
        void calcSize (Char *);
diff --git a/re2c/src/parse/code.cc b/re2c/src/parse/code.cc
new file mode 100644 (file)
index 0000000..97a865c
--- /dev/null
@@ -0,0 +1,8 @@
+#include "src/parse/code.h"
+
+namespace re2c
+{
+
+free_list<const Code *> Code::freelist;
+
+} // namespace re2c
diff --git a/re2c/src/parse/code.h b/re2c/src/parse/code.h
new file mode 100644 (file)
index 0000000..10d8738
--- /dev/null
@@ -0,0 +1,34 @@
+#ifndef _RE2C_PARSE_CODE_
+#define _RE2C_PARSE_CODE_
+
+#include "src/parse/loc.h"
+#include "src/util/c99_stdint.h"
+#include "src/util/free_list.h"
+
+namespace re2c
+{
+
+struct Code
+{
+       static free_list<const Code *> freelist;
+
+       const Loc loc;
+       const std::string text;
+
+       inline Code (const char * t, size_t t_len, const std::string & f, uint32_t l)
+               : loc (f, l)
+               , text (t, t_len)
+       {
+               freelist.insert (this);
+       }
+       inline Code (const std::string & f, uint32_t l)
+               : loc (f, l)
+               , text ()
+       {
+               freelist.insert (this);
+       }
+};
+
+} // namespace re2c
+
+#endif // _RE2C_PARSE_CODE_
index 2c973279b379e5d426e0dd2e70693d3279e0c731..7333374021e583e02facabd04c1580ca9ed8fe01 100644 (file)
@@ -18,7 +18,7 @@ extern void parse_cleanup();
 typedef std::set<std::string> CondList;
 typedef std::list<RuleOp*> RuleOpList;
 typedef std::map<std::string, std::pair<uint32_t, std::string> > SetupMap;
-typedef std::map<std::string, Token*> DefaultMap;
+typedef std::map<std::string, const Code *> DefaultMap;
 typedef std::map<std::string, RegExp *> symbol_table_t;
 
 } // namespace re2c
index 3e0fac108729a9d685e47ee13cbfd60aa140775f..5dbfc78a3ffec5f5c66f5a0f04a3972e1c5e40dd 100644 (file)
@@ -16,6 +16,7 @@
 #include "src/ir/regexp/regexp_null.h"
 #include "src/codegen/emit.h" // genTypes
 #include "src/globals.h"
+#include "src/parse/code.h"
 #include "src/parse/extop.h"
 #include "src/parse/parser.h"
 #include "src/util/c99_stdint.h"
@@ -39,7 +40,7 @@ static RuleOpList       specStar;
 static Scanner          *in = NULL;
 static Scanner::ParseMode  parseMode;
 static SetupMap            ruleSetupMap;
-static Token               *ruleDefault = NULL;
+static const Code * ruleDefault = NULL;
 static DefaultMap          ruleDefaultMap;
 static bool                foundRules;
 static symbol_table_t symbol_table;
@@ -67,7 +68,7 @@ void context_none(CondList *clist)
        in->fatal("no expression specified");
 }
 
-void context_rule(CondList *clist, RegExp *expr, RegExp *look, const std::string * newcond, Token *code)
+void context_rule(CondList *clist, RegExp *expr, RegExp *look, const std::string * newcond, const Code & code)
 {
        context_check(clist);
        const RegExp::InsAccess ins_access = clist->size() > 1
@@ -75,8 +76,7 @@ void context_rule(CondList *clist, RegExp *expr, RegExp *look, const std::string
                : RegExp::SHARED;
        for(CondList::const_iterator it = clist->begin(); it != clist->end(); ++it)
        {
-               Token *token = new Token(code, in->get_fname (), in->get_cline ());
-               RuleOp *rule = new RuleOp(expr, look, token, rank_counter.next (), ins_access, newcond);
+               RuleOp *rule = new RuleOp(expr, look, code, rank_counter.next (), ins_access, newcond);
 
                RegExpMap::iterator itRE = specMap.find(*it);
 
@@ -94,10 +94,9 @@ void context_rule(CondList *clist, RegExp *expr, RegExp *look, const std::string
        }
        delete clist;
        delete newcond;
-       delete code;
 }
 
-void setup_rule(CondList *clist, Token *code)
+void setup_rule(CondList *clist, const Code * code)
 {
        assert(clist);
        assert(code);
@@ -111,10 +110,9 @@ void setup_rule(CondList *clist, Token *code)
                ruleSetupMap[*it] = std::make_pair(code->loc.line, code->text);
        }
        delete clist;
-       delete code;
 }
 
-void default_rule(CondList *clist, Token *code)
+void default_rule(CondList *clist, const Code * code)
 {
        assert(clist);
        assert(code);
@@ -136,7 +134,7 @@ void default_rule(CondList *clist, Token *code)
 
 %union {
        re2c::RegExp    *regexp;
-       re2c::Token     *token;
+       const re2c::Code * code;
        char            op;
        int             number;
        re2c::ExtOp     extop;
@@ -150,7 +148,7 @@ void default_rule(CondList *clist, Token *code)
 %type  <op>            CLOSE   STAR    SETUP   FID_END
 %type  <op>            close
 %type  <extop>         CLOSESIZE
-%type  <token>         CODE
+%type  <code>          CODE
 %type  <regexp>        RANGE   STRING
 %type  <regexp>        rule    look    expr    diff    term    factor  primary
 %type  <str>           CONFIG  VALUE   newcond ID      FID
@@ -217,7 +215,7 @@ rule:
                        {
                                in->fatal("condition or '<*>' required when using -c switch");
                        }
-                       $$ = new RuleOp($1, $2, $3, rank_counter.next (), RegExp::SHARED, NULL);
+                       $$ = new RuleOp($1, $2, *$3, rank_counter.next (), RegExp::SHARED, NULL);
                        spec = spec? mkAlt(spec, $$) : $$;
                }
        |       STAR CODE /* default rule */
@@ -231,12 +229,13 @@ rule:
                }
        |       '<' cond '>' expr look newcond CODE
                {
-                       context_rule($2, $4, $5, $6, $7);
+                       context_rule($2, $4, $5, $6, *$7);
                }
        |       '<' cond '>' expr look ':' newcond
                {
                        assert($7);
-                       context_rule($2, $4, $5, $7, NULL);
+                       const Code * code = new Code (in->get_fname (), in->get_cline ());
+                       context_rule($2, $4, $5, $7, *code);
                }
        |       '<' cond '>' look newcond CODE
                {
@@ -256,17 +255,15 @@ rule:
        |       '<' STAR '>' expr look newcond CODE
                {
                        context_check(NULL);
-                       Token *token = new Token($7, $7->loc.filename, $7->loc.line);
-                       delete $7;
-                       specStar.push_back(new RuleOp($4, $5, token, rank_counter.next (), RegExp::PRIVATE, $6));
+                       specStar.push_back(new RuleOp($4, $5, *$7, rank_counter.next (), RegExp::PRIVATE, $6));
                        delete $6;
                }
        |       '<' STAR '>' expr look ':' newcond
                {
                        assert($7);
                        context_check(NULL);
-                       Token *token = new Token(NULL, in->get_fname (), in->get_cline ());
-                       specStar.push_back(new RuleOp($4, $5, token, rank_counter.next (), RegExp::PRIVATE, $7));
+                       const Code * code = new Code (in->get_fname (), in->get_cline ());
+                       specStar.push_back(new RuleOp($4, $5, *code, rank_counter.next (), RegExp::PRIVATE, $7));
                        delete $7;
                }
        |       '<' STAR '>' look newcond CODE
@@ -293,9 +290,7 @@ rule:
                        {
                                in->fatal("code to handle illegal condition already defined");
                        }
-                       Token *token = new Token($3, $3->loc.filename, $3->loc.line);
-                       delete $3;
-                       $$ = specNone = new RuleOp(new NullOp(), new NullOp(), token, rank_counter.next (), RegExp::SHARED, $2);
+                       $$ = specNone = new RuleOp(new NullOp(), new NullOp(), *$3, rank_counter.next (), RegExp::SHARED, $2);
                        delete $2;
                }
        |       NOCOND ':' newcond
@@ -306,8 +301,8 @@ rule:
                        {
                                in->fatal("code to handle illegal condition already defined");
                        }
-                       Token *token = new Token(NULL, in->get_fname (), in->get_cline ());
-                       $$ = specNone = new RuleOp(new NullOp(), new NullOp(), token, rank_counter.next (), RegExp::SHARED, $3);
+                       const Code * code = new Code (in->get_fname (), in->get_cline ());
+                       $$ = specNone = new RuleOp(new NullOp(), new NullOp(), *code, rank_counter.next (), RegExp::SHARED, $3);
                        delete $3;
                }
        |       SETUP STAR '>' CODE
@@ -637,7 +632,7 @@ void parse(Scanner& i, Output & o)
                                        itRuleDefault = ruleDefaultMap.find(it->first);
                                        if (itRuleDefault != ruleDefaultMap.end())
                                        {
-                                               RuleOp * def = new RuleOp(in->mkDefault(), new NullOp(), itRuleDefault->second, rank_counter.next (), RegExp::SHARED, NULL);
+                                               RuleOp * def = new RuleOp(in->mkDefault(), new NullOp(), *(itRuleDefault->second), rank_counter.next (), RegExp::SHARED, NULL);
                                                it->second.second = it->second.second ? mkAlt(def, it->second.second) : def;
                                        }
                                        else
@@ -645,7 +640,7 @@ void parse(Scanner& i, Output & o)
                                                itRuleDefault = ruleDefaultMap.find("*");
                                                if (itRuleDefault != ruleDefaultMap.end())
                                                {
-                                                       RuleOp * def = new RuleOp(in->mkDefault(), new NullOp(), itRuleDefault->second, rank_counter.next (), RegExp::SHARED, NULL);
+                                                       RuleOp * def = new RuleOp(in->mkDefault(), new NullOp(), *(itRuleDefault->second), rank_counter.next (), RegExp::SHARED, NULL);
                                                        it->second.second = it->second.second ? mkAlt(def, it->second.second) : def;
                                                }
                                        }
@@ -663,7 +658,7 @@ void parse(Scanner& i, Output & o)
                {
                        if (ruleDefault != NULL && parseMode != Scanner::Reuse)
                        {
-                               RuleOp * def = new RuleOp(in->mkDefault(), new NullOp(), ruleDefault, rank_counter.next (), RegExp::SHARED, NULL);
+                               RuleOp * def = new RuleOp(in->mkDefault(), new NullOp(), *ruleDefault, rank_counter.next (), RegExp::SHARED, NULL);
                                spec = spec ? mkAlt(def, spec) : def;
                        }
                        if (spec || !dfa_map.empty())
@@ -714,6 +709,7 @@ void parse_cleanup()
        RegExp::vFreeList.clear();
        Range::vFreeList.clear();
        RangeSuffix::freeList.clear();
+       Code::freelist.clear();
        symbol_table.clear ();
        specMap.clear();
        specStar.clear();
index bd97177f56047487bdb107f6c422ef32735582a5..3eda866ff2bd9a5fa10ce1e67d236e6a5898f26d 100644 (file)
@@ -6,8 +6,8 @@
 #include "src/codegen/output.h"
 #include "src/ir/regexp/regexp.h"
 #include "src/globals.h"
+#include "src/parse/code.h"
 #include "src/parse/input.h"
-#include "src/parse/token.h"
 #include "src/util/attribute.h"
 #include "src/util/forbid_copy.h"
 #include "src/util/substr.h"
index ad64e5431cb33e3eb60f4bacec918eb27a2a8877..e7032ed50c2be9e4c2436ca7cc2476bdd9ffdb43 100644 (file)
@@ -464,7 +464,7 @@ code:
                                        else if (--depth == 0)
                                        {
                                                cur = cursor;
-                                               yylval.token = new Token(tok, cur - tok, get_fname (), tline);
+                                               yylval.code = new Code (tok, cur - tok, get_fname (), tline);
                                                return CODE;
                                        }
                                        goto code;
@@ -506,7 +506,7 @@ code:
                                                {
                                                        --cur;
                                                }
-                                               yylval.token = new Token(tok, cur - tok, get_fname (), tline);
+                                               yylval.code = new Code (tok, cur - tok, get_fname (), tline);
                                                return CODE;
                                        }
                                        else if (cursor == eof)
diff --git a/re2c/src/parse/token.h b/re2c/src/parse/token.h
deleted file mode 100644 (file)
index 125925a..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-#ifndef _RE2C_PARSE_TOKEN_
-#define _RE2C_PARSE_TOKEN_
-
-#include "src/parse/loc.h"
-#include "src/util/c99_stdint.h"
-#include "src/util/forbid_copy.h"
-
-namespace re2c
-{
-
-class Token
-{
-public:
-       const Loc loc;
-       const std::string text;
-       const bool autogen;
-
-       inline Token (const char * t, uint32_t t_len, const std::string & s, uint32_t l)
-               : loc (s, l)
-               , text (t, t_len)
-               , autogen (false)
-       {}
-       inline Token (const Token * t, const std::string & s, uint32_t l)
-               : loc (t ? t->loc : Loc (s, l))
-               , text (t ? t->text : "")
-               , autogen (t == NULL)
-       {}
-
-       FORBID_COPY (Token);
-};
-
-} // namespace re2c
-
-#endif // _RE2C_PARSE_TOKEN_
index 7eebd5692b9d5e5d43db975e78e19e66a01959f5..f456b2265a828b0658c58bbd0026fda296077bff 100644 (file)
@@ -16,9 +16,7 @@ re2c: warning: line 9: naked default case in condition r2 (stray code units: [0x
 /* *********************************** */
 yyc_0:
        YYSETCONDITION(yycr1);
-#line 3 "condition_13.cg.re"
        goto yyc_r1;
-#line 19 "<stdout>"
 /* *********************************** */
 yyc_r1:
        if (YYLIMIT <= YYCURSOR) YYFILL(1);
@@ -35,26 +33,18 @@ yyc_r1:
 yy6:
 yy7:
        ++YYCURSOR;
-#line 5 "condition_13.cg.re"
        goto yyc_r1;
-#line 38 "<stdout>"
 yy9:
        ++YYCURSOR;
-#line 4 "condition_13.cg.re"
        goto yyc_r1;
-#line 43 "<stdout>"
 yy11:
        ++YYCURSOR;
        YYSETCONDITION(yycr2);
-#line 6 "condition_13.cg.re"
        goto yyc_r2;
-#line 49 "<stdout>"
 yy13:
        ++YYCURSOR;
        YYSETCONDITION(yycr2);
-#line 7 "condition_13.cg.re"
        goto yyc_r2;
-#line 55 "<stdout>"
 /* *********************************** */
 yyc_r2:
        if (YYLIMIT <= YYCURSOR) YYFILL(1);
@@ -70,20 +60,14 @@ yy17:
 yy18:
        ++YYCURSOR;
        YYSETCONDITION(yycr1);
-#line 5 "condition_13.cg.re"
        goto yyc_r1;
-#line 73 "<stdout>"
 yy20:
        ++YYCURSOR;
        YYSETCONDITION(yycr1);
-#line 4 "condition_13.cg.re"
        goto yyc_r1;
-#line 79 "<stdout>"
 yy22:
        ++YYCURSOR;
-#line 7 "condition_13.cg.re"
        goto yyc_r2;
-#line 84 "<stdout>"
 }
 #line 9 "condition_13.cg.re"