From 32feb0eaf39a12578be94160475ed47a43f2851b Mon Sep 17 00:00:00 2001 From: Ulya Trofimovich Date: Tue, 4 Aug 2015 11:10:54 +0100 Subject: [PATCH] Added simple struct to store locations of parsed elements in source file. --- re2c/Makefile.am | 1 + re2c/src/codegen/emit_action.cc | 4 ++-- re2c/src/codegen/prepare_dfa.cc | 2 +- re2c/src/parse/loc.h | 24 +++++++++++++++++++++ re2c/src/parse/parser.ypp | 10 ++++----- re2c/src/parse/token.h | 37 +++++++++++---------------------- 6 files changed, 45 insertions(+), 33 deletions(-) create mode 100644 re2c/src/parse/loc.h diff --git a/re2c/Makefile.am b/re2c/Makefile.am index 9fed2b61..d27ff3b3 100644 --- a/re2c/Makefile.am +++ b/re2c/Makefile.am @@ -52,6 +52,7 @@ SRC_HDR = \ src/globals.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 \ diff --git a/re2c/src/codegen/emit_action.cc b/re2c/src/codegen/emit_action.cc index f5240532..1d33c924 100644 --- a/re2c/src/codegen/emit_action.cc +++ b/re2c/src/codegen/emit_action.cc @@ -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->source << ":" << rule->code->line << "\"]\n"; + o << s->label << " [label=\"" << rule->code->loc.filename << ":" << rule->code->loc.line << "\"]\n"; return; } @@ -253,7 +253,7 @@ void emit_rule (OutputFile & o, uint32_t ind, const State * const s, const RuleO o << indent(ind) << yySetupRule << "\n"; } - o.write_line_info (rule->code->line, rule->code->source.c_str ()); + o.write_line_info (rule->code->loc.line, rule->code->loc.filename.c_str ()); o << indent(ind); if (flag_skeleton) { diff --git a/re2c/src/codegen/prepare_dfa.cc b/re2c/src/codegen/prepare_dfa.cc index 8322a6da..6ea355e7 100644 --- a/re2c/src/codegen/prepare_dfa.cc +++ b/re2c/src/codegen/prepare_dfa.cc @@ -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->line); + warn.match_empty_string (head->rule->code->loc.line); } // split ``base'' states into two parts diff --git a/re2c/src/parse/loc.h b/re2c/src/parse/loc.h new file mode 100644 index 00000000..b3d4277a --- /dev/null +++ b/re2c/src/parse/loc.h @@ -0,0 +1,24 @@ +#ifndef _RE2C_PARSE_LOC_ +#define _RE2C_PARSE_LOC_ + +#include + +#include "src/util/c99_stdint.h" + +namespace re2c +{ + +struct Loc +{ + std::string filename; + uint32_t line; + + inline Loc (const std::string & f, uint32_t l) + : filename (f) + , line (l) + {} +}; + +} // namespace re2c + +#endif // _RE2C_PARSE_LOC_ diff --git a/re2c/src/parse/parser.ypp b/re2c/src/parse/parser.ypp index b00a344a..c8a263b4 100644 --- a/re2c/src/parse/parser.ypp +++ b/re2c/src/parse/parser.ypp @@ -105,9 +105,9 @@ void setup_rule(CondList *clist, Token *code) { if (ruleSetupMap.find(*it) != ruleSetupMap.end()) { - in->fatalf_at(code->line, "code to setup rule '%s' is already defined", it->c_str()); + in->fatalf_at(code->loc.line, "code to setup rule '%s' is already defined", it->c_str()); } - ruleSetupMap[*it] = std::make_pair(code->line, code->text); + ruleSetupMap[*it] = std::make_pair(code->loc.line, code->text); } delete clist; delete code; @@ -122,7 +122,7 @@ void default_rule(CondList *clist, Token *code) { if (ruleDefaultMap.find(*it) != ruleDefaultMap.end()) { - in->fatalf_at(code->line, "code to default rule '%s' is already defined", it->c_str()); + in->fatalf_at(code->loc.line, "code to default rule '%s' is already defined", it->c_str()); } ruleDefaultMap[*it] = code; } @@ -255,7 +255,7 @@ rule: | '<' STAR '>' expr look newcond CODE { context_check(NULL); - Token *token = new Token($7, $7->source, $7->line); + 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)); delete $6; @@ -292,7 +292,7 @@ rule: { in->fatal("code to handle illegal condition already defined"); } - Token *token = new Token($3, $3->source, $3->line); + 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); delete $2; diff --git a/re2c/src/parse/token.h b/re2c/src/parse/token.h index 6f286335..125925ab 100644 --- a/re2c/src/parse/token.h +++ b/re2c/src/parse/token.h @@ -1,6 +1,7 @@ #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" @@ -10,38 +11,24 @@ namespace re2c class Token { public: + const Loc loc; const std::string text; - const std::string source; - uint32_t line; const bool autogen; - Token (const char *, uint32_t, const std::string &, uint32_t); - Token (const Token *, const std::string &, uint32_t); + 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); }; -inline Token::Token (const char * t, uint32_t t_len, const std::string & s, uint32_t l) - : text (t, t_len) - , source (s) - , line (l) - , autogen (false) -{} - -inline Token::Token (const Token * t, const std::string & s, uint32_t l) - : text (t ? t->text : "") - , source (t ? t->source : s) - , line (t ? t->line : l) - , autogen (t == NULL) -{} - -inline Token::Token (const Token & t) - : text (t.text) - , source (t.source) - , line (t.line) - , autogen (t.autogen) -{} - } // namespace re2c #endif // _RE2C_PARSE_TOKEN_ -- 2.40.0