From: Ulya Trofimovich Date: Mon, 2 Mar 2015 13:54:11 +0000 (+0000) Subject: Hided 'OutputFile' structure. X-Git-Tag: 0.15~373 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d5f999b792669e7725f63d2d174a0f2ab1c082a8;p=re2c Hided 'OutputFile' structure. --- diff --git a/re2c/bootstrap/parser.cc b/re2c/bootstrap/parser.cc index 5bfba40d..edae1746 100644 --- a/re2c/bootstrap/parser.cc +++ b/re2c/bootstrap/parser.cc @@ -2233,8 +2233,8 @@ void parse(Scanner& i, Output & o) in = &i; - output_version_time (o.source.fragment ()); - output_line_info (o.source.fragment (), in->get_cline (), in->get_fname ().c_str ()); + o.source.write_version_time (); + o.source.write_line_info (in->get_cline (), in->get_fname ().c_str ()); Enc encodingOld = encoding; @@ -2407,7 +2407,7 @@ void parse(Scanner& i, Output & o) } } } - output_line_info (o.source.fragment (), in->get_cline (), in->get_fname ().c_str ()); + o.source.write_line_info (in->get_cline (), in->get_fname ().c_str ()); /* restore original char handling mode*/ encoding = encodingOld; } diff --git a/re2c/bootstrap/scanner.cc b/re2c/bootstrap/scanner.cc index ea7dbc5d..fcdc2f92 100644 --- a/re2c/bootstrap/scanner.cc +++ b/re2c/bootstrap/scanner.cc @@ -1,4 +1,4 @@ -/* Generated by re2c 0.14.1.dev on Fri Feb 27 16:41:28 2015*/ +/* Generated by re2c 0.14.1.dev on Mon Mar 2 13:32:16 2015*/ #include #include #include @@ -384,7 +384,7 @@ yy74: out << "\n"; out.insert_types (); out << "\n"; - output_line_info (out.fragment (), cline, get_fname ().c_str ()); + out.write_line_info (cline, get_fname ().c_str ()); } goto echo; } @@ -400,7 +400,7 @@ yy85: if (ignore_cnt) { out << "\n"; - output_line_info (out.fragment (), cline, get_fname ().c_str ()); + out.write_line_info (cline, get_fname ().c_str ()); } ignore_eoc = false; ignore_cnt = 0; @@ -423,7 +423,7 @@ yy87: { if (ignore_cnt) { - output_line_info (out.fragment (), cline, get_fname ().c_str ()); + out.write_line_info (cline, get_fname ().c_str ()); } ignore_eoc = false; ignore_cnt = 0; diff --git a/re2c/code.cc b/re2c/code.cc index b7d828b6..2dd7239d 100644 --- a/re2c/code.cc +++ b/re2c/code.cc @@ -298,11 +298,11 @@ void BitMap::gen(OutputFile & o, uint ind, uint lb, uint ub) if (yybmHexTable) { - prtHex(o.fragment (), bm[j]); + o.write_hex (bm[j]); } else { - o << Setw (3) << (uint)bm[j]; + o.write_uint_width (bm[j], 3); } o << ", "; } @@ -346,7 +346,7 @@ static void genIf(OutputFile & o, uint ind, const char *cmp, uint v, bool &readC } o << " " << cmp << " "; - prtChOrHex(o.fragment (), v); + o.write_char_hex (v); o << ") "; } @@ -707,7 +707,7 @@ void Rule::emit(Output & output, uint ind, bool &, const std::string& condName) o << indent(ind) << yySetupRule << "\n"; } - output_line_info (o.fragment (), rule->code->line, rule->code->source.c_str ()); + o.write_line_info (rule->code->line, rule->code->source.c_str ()); o << indent(ind); if (rule->code->autogen) { @@ -820,17 +820,17 @@ static void printDotCharInterval(OutputFile & o, uint lastPrintableChar, uint ch o << "]\n"; o << from->label << " -> " << to->label; o << " [label="; - prtChOrHex(o.fragment (), ++chr); + o.write_char_hex (++chr); } } else { - prtChOrHex(o.fragment (), chr); + o.write_char_hex (chr); } } else { - prtChOrHex(o.fragment (), chr); + o.write_char_hex (chr); } o << "]"; @@ -874,7 +874,7 @@ static bool genCases(OutputFile & o, uint ind, uint lb, Span *s, bool &newLine, else { o << indent(ind) << "case "; - prtChOrHex(o.fragment (), lb); + o.write_char_hex (lb); o << ":"; if (dFlag && encoding.is(Enc::EBCDIC)) { @@ -1248,7 +1248,7 @@ void Go::genGoto(OutputFile & o, uint ind, const State *from, const State *next, o << "if (" << mapCodeName["yybm"] << "[" << b->i << "+" << sYych << "] & "; if (yybmHexTable) { - prtHex(o.fragment (), b->m); + o.write_hex (b->m); } else { @@ -1908,7 +1908,7 @@ void DFA::emit(Output & output, uint& ind, const RegExpMap* specMap, const std:: bUseStartLabel = false; } -static void output_state_goto_sub (OutputFragment & o, uint ind, uint start_label, int cMin, int cMax) +static void output_state_goto_sub (std::ostream & o, uint ind, uint start_label, int cMin, int cMax) { if (cMin == cMax) { @@ -1933,9 +1933,8 @@ static void output_state_goto_sub (OutputFragment & o, uint ind, uint start_labe } } -void output_state_goto (OutputFragment & o, uint start_label) +void output_state_goto (std::ostream & o, uint ind, uint start_label) { - uint ind = o.indent; if (gFlag) { o << indent(ind++) << "static void *" << mapCodeName["yystable"] << "[" << "] = {\n"; @@ -2094,23 +2093,23 @@ void genTypes(Output & output, const RegExpMap& specMap) } } -void output_yyaccept_init (OutputFragment & o, bool used_yyaccept) +void output_yyaccept_init (std::ostream & o, uint ind, bool used_yyaccept) { if (used_yyaccept) { - o << indent (o.indent) << "unsigned int " << mapCodeName["yyaccept"] << " = 0;\n"; + o << indent (ind) << "unsigned int " << mapCodeName["yyaccept"] << " = 0;\n"; } } -void output_yyaccept_selector (OutputFragment & o, bool used_yyaccept) +void output_yyaccept_selector (std::ostream & o, uint ind, bool used_yyaccept, uint yyaccept_selector) { if (used_yyaccept) { - o << indent (o.indent) << mapCodeName["yyaccept"] << " = " << o.info.yyaccept_selector << ";\n"; + o << indent (ind) << mapCodeName["yyaccept"] << " = " << yyaccept_selector << ";\n"; } } -void output_yymaxfill (OutputFragment & o, uint max_fill) +void output_yymaxfill (std::ostream & o, uint max_fill) { o << "#define YYMAXFILL " << max_fill << "\n"; } diff --git a/re2c/output.cc b/re2c/output.cc index e4e18357..2ef17419 100644 --- a/re2c/output.cc +++ b/re2c/output.cc @@ -2,15 +2,23 @@ #include #include "output.h" +#include "print.h" #include "re.h" namespace re2c { +OutputFragment::OutputFragment (type_t t, uint i) + : type (t) + , stream () + , info () + , indent (i) +{} + uint OutputFragment::count_lines () { uint lines = 0; - const std::string content = str (); + const std::string content = stream.str (); const char * p = content.c_str (); for (uint i = 0; i < content.size (); ++i) { @@ -71,43 +79,62 @@ OutputFile::~OutputFile () } } -OutputFragment & OutputFile::fragment () +std::ostream & OutputFile::stream () { - return * blocks.back ()->fragments.back (); + return blocks.back ()->fragments.back ()->stream; } void OutputFile::write (const char * s, std::streamsize n) { - fragment ().write (s, n); + stream ().write (s, n); +} + +void OutputFile::write_hex (uint n) +{ + prtHex (stream (), n); +} + +void OutputFile::write_char_hex (uint n) +{ + prtChOrHex (stream (), n); +} + +void OutputFile::write_uint_width (uint n, uint w) +{ + stream () << std::setw (w) << n; +} + +void OutputFile::write_line_info (uint l, const char * fn) +{ + output_line_info (stream (), l, fn); +} + +void OutputFile::write_version_time () +{ + output_version_time (stream ()); } OutputFile & operator << (OutputFile & u, uint n) { - u.fragment () << n; + u.stream () << n; return u; } OutputFile & operator << (OutputFile & u, const std::string & s) { - u.fragment () << s; + u.stream () << s; return u; } OutputFile & operator << (OutputFile & u, const char * s) { - u.fragment () << s; + u.stream () << s; return u; } OutputFile & operator << (OutputFile & u, const Str & s) { - u.fragment () << s; - return u; -} - -OutputFile & operator << (OutputFile & u, const Setw & s) -{ - u.fragment () << std::setw (s.width); + u.stream () << s; return u; } @@ -195,25 +222,25 @@ void OutputFile::emit case OutputFragment::CODE: break; case OutputFragment::LINE_INFO: - output_line_info (f, line_count + 1, file_name); + output_line_info (f.stream, line_count + 1, file_name); break; case OutputFragment::STATE_GOTO: - output_state_goto (f, prolog_label); + output_state_goto (f.stream, f.indent, prolog_label); break; case OutputFragment::TYPES: - output_types (f, f.indent, types); + output_types (f.stream, f.indent, types); break; case OutputFragment::YYACCEPT_INIT: - output_yyaccept_init (f, b.used_yyaccept); + output_yyaccept_init (f.stream, f.indent, b.used_yyaccept); break; case OutputFragment::YYACCEPT_SELECTOR: - output_yyaccept_selector (f, b.used_yyaccept); + output_yyaccept_selector (f.stream, f.indent, b.used_yyaccept, f.info.yyaccept_selector); break; case OutputFragment::YYMAXFILL: - output_yymaxfill (f, max_fill); + output_yymaxfill (f.stream, max_fill); break; } - std::string content = f.str (); + std::string content = f.stream.str (); fwrite (content.c_str (), 1, content.size (), file); line_count += f.count_lines (); } diff --git a/re2c/output.h b/re2c/output.h index 4379ee39..7bed5802 100644 --- a/re2c/output.h +++ b/re2c/output.h @@ -12,16 +12,7 @@ namespace re2c struct Str; -struct Setw -{ - uint width; - - Setw (uint n) - : width (n) - {} -}; - -struct OutputFragment : public std::ostringstream +struct OutputFragment { enum type_t { CODE @@ -40,16 +31,11 @@ struct OutputFragment : public std::ostringstream }; type_t type; + std::ostringstream stream; info_t info; uint indent; - OutputFragment (type_t t, uint i) - : std::ostringstream () - , type (t) - , info () - , indent (i) - {} - + OutputFragment (type_t t, uint i); uint count_lines (); }; @@ -66,24 +52,33 @@ struct OutputFile { OutputFile (const char * fn); ~OutputFile (); + bool open (); - OutputFragment & fragment (); + + void new_block (); + void write (const char * s, std::streamsize n); + void write_hex (uint n); + void write_char_hex (uint n); + void write_uint_width (uint n, uint w); + void write_line_info (uint l, const char * fn); + void write_version_time (); + friend OutputFile & operator << (OutputFile & o, uint n); + friend OutputFile & operator << (OutputFile & o, const std::string & s); + friend OutputFile & operator << (OutputFile & o, const char * s); + friend OutputFile & operator << (OutputFile & o, const Str & s); + void insert_line_info (); void insert_state_goto (uint ind, uint start_label); void insert_types (); void insert_yyaccept_init (uint ind); void insert_yyaccept_selector (uint ind, uint selector); void insert_yymaxfill (); - void new_block (); + void set_used_yyaccept (); bool get_used_yyaccept () const; + void emit (const std::vector & types, uint max_fill); - friend OutputFile & operator << (OutputFile & o, uint n); - friend OutputFile & operator << (OutputFile & o, const std::string & s); - friend OutputFile & operator << (OutputFile & o, const char * s); - friend OutputFile & operator << (OutputFile & o, const Str & s); - friend OutputFile & operator << (OutputFile & o, const Setw & s); private: const char * file_name; @@ -91,6 +86,7 @@ private: std::vector blocks; uint prolog_label; + std::ostream & stream (); void insert_code (); }; diff --git a/re2c/parser.y b/re2c/parser.y index 5766befa..3a9d050d 100644 --- a/re2c/parser.y +++ b/re2c/parser.y @@ -513,8 +513,8 @@ void parse(Scanner& i, Output & o) in = &i; - output_version_time (o.source.fragment ()); - output_line_info (o.source.fragment (), in->get_cline (), in->get_fname ().c_str ()); + o.source.write_version_time (); + o.source.write_line_info (in->get_cline (), in->get_fname ().c_str ()); Enc encodingOld = encoding; @@ -687,7 +687,7 @@ void parse(Scanner& i, Output & o) } } } - output_line_info (o.source.fragment (), in->get_cline (), in->get_fname ().c_str ()); + o.source.write_line_info (in->get_cline (), in->get_fname ().c_str ()); /* restore original char handling mode*/ encoding = encodingOld; } diff --git a/re2c/re.h b/re2c/re.h index 3a61330f..9605fb6a 100644 --- a/re2c/re.h +++ b/re2c/re.h @@ -396,12 +396,12 @@ extern void genCondTable(OutputFile &, uint, const RegExpMap&); extern void genCondGoto(OutputFile &, uint, const RegExpMap&); extern void genTypes(Output &, const RegExpMap&); -extern void output_state_goto (OutputFragment &, uint); +extern void output_state_goto (std::ostream &, uint, uint); extern void output_types (std::ostream &, uint, const std::vector &); extern void output_version_time (std::ostream &); -extern void output_yyaccept_init (OutputFragment &, bool); -extern void output_yyaccept_selector (OutputFragment &, bool); -extern void output_yymaxfill (OutputFragment &, uint); +extern void output_yyaccept_init (std::ostream &, uint, bool); +extern void output_yyaccept_selector (std::ostream &, uint, bool, uint); +extern void output_yymaxfill (std::ostream &, uint); extern void output_line_info (std::ostream &, uint, const char *); extern RegExp *mkDiff(RegExp*, RegExp*); diff --git a/re2c/scanner.re b/re2c/scanner.re index f1c9c8bb..f07a88cb 100644 --- a/re2c/scanner.re +++ b/re2c/scanner.re @@ -130,7 +130,7 @@ echo: out << "\n"; out.insert_types (); out << "\n"; - output_line_info (out.fragment (), cline, get_fname ().c_str ()); + out.write_line_info (cline, get_fname ().c_str ()); } goto echo; } @@ -140,7 +140,7 @@ echo: { if (ignore_cnt) { - output_line_info (out.fragment (), cline, get_fname ().c_str ()); + out.write_line_info (cline, get_fname ().c_str ()); } ignore_eoc = false; ignore_cnt = 0; @@ -158,7 +158,7 @@ echo: if (ignore_cnt) { out << "\n"; - output_line_info (out.fragment (), cline, get_fname ().c_str ()); + out.write_line_info (cline, get_fname ().c_str ()); } ignore_eoc = false; ignore_cnt = 0;