From: Ulya Trofimovich Date: Wed, 2 Sep 2015 11:39:47 +0000 (+0100) Subject: Narrowed the scope of ".data" file. X-Git-Tag: 0.15~83 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c8851a91ec64edb730d422a16a2379057447f619;p=re2c Narrowed the scope of ".data" file. --- diff --git a/re2c/src/codegen/output.cc b/re2c/src/codegen/output.cc index e7e78f4f..854d1862 100644 --- a/re2c/src/codegen/output.cc +++ b/re2c/src/codegen/output.cc @@ -343,31 +343,9 @@ HeaderFile::~HeaderFile () } } -DataFile::DataFile (const char * fn) - : file_name (fn == NULL ? "" : fn) - , file () -{ - file_name += ".data"; -} - -bool DataFile::open () -{ - file.open (file_name.c_str (), std::ofstream::out | std::ofstream::binary); - return file.is_open (); -} - -DataFile::~DataFile () -{ - if (file.is_open ()) - { - file.close (); - } -} - Output::Output (const char * source_name, const char * header_name) : source (source_name) , header (header_name) - , data (source_name) , types () , max_fill (1) {} diff --git a/re2c/src/codegen/output.h b/re2c/src/codegen/output.h index 85d79782..fa0de280 100644 --- a/re2c/src/codegen/output.h +++ b/re2c/src/codegen/output.h @@ -49,8 +49,10 @@ struct OutputBlock struct OutputFile { -private: +public: const char * file_name; + +private: FILE * file; std::vector blocks; @@ -121,21 +123,10 @@ private: FORBID_COPY (HeaderFile); }; -struct DataFile -{ - DataFile (const char * fn); - ~DataFile (); - bool open (); - - std::string file_name; - std::ofstream file; -}; - struct Output { OutputFile source; HeaderFile header; - DataFile data; std::vector types; uint32_t max_fill; diff --git a/re2c/src/codegen/skeleton/skeleton.cc b/re2c/src/codegen/skeleton/skeleton.cc index c8af5425..e2b5ed89 100644 --- a/re2c/src/codegen/skeleton/skeleton.cc +++ b/re2c/src/codegen/skeleton/skeleton.cc @@ -1,6 +1,9 @@ +#include // exit + #include "src/codegen/indent.h" #include "src/codegen/print.h" #include "src/codegen/skeleton/skeleton.h" +#include "src/conf/msg.h" #include "src/ir/regexp/regexp_rule.h" #include "src/util/allocate.h" @@ -266,8 +269,17 @@ void Skeleton::generate_paths (std::vector & results) } } -void Skeleton::emit_data (DataFile & o) +void Skeleton::emit_data (const char * fname) { + const std::string dfname = std::string (fname) + ".data"; + std::ofstream f; + f.open (dfname.c_str (), std::ofstream::out | std::ofstream::binary); + if (!f.is_open ()) + { + error ("cannot open data file: %s", dfname.c_str ()); + exit (1); + } + uint32_t ind = 0; std::string yyctype; @@ -284,19 +296,19 @@ void Skeleton::emit_data (DataFile & o) break; } - o.file << "#define " << mapCodeName["YYCTYPE"] << yyctype << "\n"; - o.file << "#define " << mapCodeName["YYPEEK"] << "() *cursor\n"; - o.file << "#define " << mapCodeName["YYSKIP"] << "() ++cursor\n"; - o.file << "#define " << mapCodeName["YYBACKUP"] << "() marker = cursor\n"; - o.file << "#define " << mapCodeName["YYBACKUPCTX"] << "() ctxmarker = cursor\n"; - o.file << "#define " << mapCodeName["YYRESTORE"] << "() cursor = marker\n"; - o.file << "#define " << mapCodeName["YYRESTORECTX"] << "() cursor = ctxmarker\n"; - o.file << "#define " << mapCodeName["YYLESSTHAN"] << "(n) (limit - cursor) < n\n"; - o.file << "#define " << mapCodeName["YYFILL"] << "(n) { break; }\n"; + f << "#define " << mapCodeName["YYCTYPE"] << yyctype << "\n"; + f << "#define " << mapCodeName["YYPEEK"] << "() *cursor\n"; + f << "#define " << mapCodeName["YYSKIP"] << "() ++cursor\n"; + f << "#define " << mapCodeName["YYBACKUP"] << "() marker = cursor\n"; + f << "#define " << mapCodeName["YYBACKUPCTX"] << "() ctxmarker = cursor\n"; + f << "#define " << mapCodeName["YYRESTORE"] << "() cursor = marker\n"; + f << "#define " << mapCodeName["YYRESTORECTX"] << "() cursor = ctxmarker\n"; + f << "#define " << mapCodeName["YYLESSTHAN"] << "(n) (limit - cursor) < n\n"; + f << "#define " << mapCodeName["YYFILL"] << "(n) { break; }\n"; - o.file << indent (ind) << "// These strings correspond to paths in DFA.\n"; - o.file << indent (ind) << "YYCTYPE data [] =\n"; - o.file << indent (ind) << "{\n"; + f << indent (ind) << "// These strings correspond to paths in DFA.\n"; + f << indent (ind) << "YYCTYPE data [] =\n"; + f << indent (ind) << "{\n"; std::vector ys; generate_paths (ys); @@ -314,53 +326,55 @@ void Skeleton::emit_data (DataFile & o) } for (size_t i = 0; i < count; ++i) { - o.file << indent (ind + 1); + f << indent (ind + 1); const size_t len = ys[i].len (); for (size_t j = 0 ; j < len; ++j) { - prtChOrHex (o.file, ys[i][j]); - o.file << ","; + prtChOrHex (f, ys[i][j]); + f << ","; } - o.file << "\n"; + f << "\n"; } - o.file << indent (ind + 1); + f << indent (ind + 1); for (size_t j = 0 ; j < max_len; ++j) // pad with YMAXFILL zeroes { - o.file << "0,"; + f << "0,"; } - o.file << "\n"; - o.file << indent (ind) << "};\n"; - o.file << indent (ind) << "const unsigned int data_size = sizeof (data) / sizeof (YYCTYPE);\n"; + f << "\n"; + f << indent (ind) << "};\n"; + f << indent (ind) << "const unsigned int data_size = sizeof (data) / sizeof (YYCTYPE);\n"; - o.file << indent (ind) << "const unsigned int count = " << count << ";\n"; + f << indent (ind) << "const unsigned int count = " << count << ";\n"; size_t pos = 0; - o.file << indent (ind) << "struct Result {\n"; - o.file << indent (ind + 1) << "unsigned int endpos;\n"; - o.file << indent (ind + 1) << "unsigned int startpos;\n"; - o.file << indent (ind + 1) << "unsigned int rule;\n"; - o.file << indent (ind + 1) << "Result (unsigned int e, unsigned int s, unsigned int r) : endpos (e), startpos (s), rule (r) {}\n"; - o.file << indent (ind) << "};\n"; - o.file << indent (ind) << "Result result [] =\n"; - o.file << indent (ind) << "{\n"; + f << indent (ind) << "struct Result {\n"; + f << indent (ind + 1) << "unsigned int endpos;\n"; + f << indent (ind + 1) << "unsigned int startpos;\n"; + f << indent (ind + 1) << "unsigned int rule;\n"; + f << indent (ind + 1) << "Result (unsigned int e, unsigned int s, unsigned int r) : endpos (e), startpos (s), rule (r) {}\n"; + f << indent (ind) << "};\n"; + f << indent (ind) << "Result result [] =\n"; + f << indent (ind) << "{\n"; for (size_t i = 0; i < count; ++i) { const size_t new_pos = pos + ys[i].len (); - o.file << indent (ind + 1) << "Result (" << pos + ys[i].len_matching () << "," << new_pos << "," << ys[i].match () << "),\n"; + f << indent (ind + 1) << "Result (" << pos + ys[i].len_matching () << "," << new_pos << "," << ys[i].match () << "),\n"; pos = new_pos; } - o.file << indent (ind) << "};\n"; + f << indent (ind) << "};\n"; + + f << indent (ind) << "const YYCTYPE * cursor = data;\n"; + f << indent (ind) << "const YYCTYPE * marker = data;\n"; + f << indent (ind) << "const YYCTYPE * ctxmarker = data;\n"; + f << indent (ind) << "const YYCTYPE * const limit = &data[data_size - 1];\n"; - o.file << indent (ind) << "const YYCTYPE * cursor = data;\n"; - o.file << indent (ind) << "const YYCTYPE * marker = data;\n"; - o.file << indent (ind) << "const YYCTYPE * ctxmarker = data;\n"; - o.file << indent (ind) << "const YYCTYPE * const limit = &data[data_size - 1];\n"; + f.close (); } -void emit_prolog (OutputFile & o, uint32_t ind, const char * data_name) +void emit_prolog (OutputFile & o, uint32_t ind) { o << indent (ind) << "#include \n"; - o << indent (ind) << "#include \"" << data_name << "\"\n"; + o << indent (ind) << "#include \"" << o.file_name << ".data" << "\"\n"; o << indent (ind) << "int main ()\n"; o << indent (ind) << "{\n"; o << indent (ind + 1) << "for (unsigned int i = 0; i < count; ++i)\n"; diff --git a/re2c/src/codegen/skeleton/skeleton.h b/re2c/src/codegen/skeleton/skeleton.h index d36993b1..ae62cb26 100644 --- a/re2c/src/codegen/skeleton/skeleton.h +++ b/re2c/src/codegen/skeleton/skeleton.h @@ -62,12 +62,12 @@ struct Skeleton ~Skeleton (); void generate_paths (std::vector & results); void warn_undefined_control_flow (uint32_t line, const std::string & cond); - void emit_data (DataFile & o); + void emit_data (const char * fname); FORBID_COPY (Skeleton); }; -void emit_prolog (OutputFile & o, uint32_t ind, const char * data_name); +void emit_prolog (OutputFile & o, uint32_t ind); void emit_epilog (OutputFile & o, uint32_t ind); } // namespace re2c diff --git a/re2c/src/ir/bytecode/bytecode.cc b/re2c/src/ir/bytecode/bytecode.cc index b38ff8c2..b893fb4e 100644 --- a/re2c/src/ir/bytecode/bytecode.cc +++ b/re2c/src/ir/bytecode/bytecode.cc @@ -61,8 +61,8 @@ smart_ptr genCode (RegExp *re, Output & output, uint32_t ind, const std::st if (flag_skeleton) { - skeleton.emit_data (output.data); - emit_prolog (output.source, ind, output.data.file_name.c_str ()); + skeleton.emit_data (output.source.file_name); + emit_prolog (output.source, ind); } dfa->prepare (output.source, output.max_fill); diff --git a/re2c/src/main.cc b/re2c/src/main.cc index 550e04c2..448b3f36 100644 --- a/re2c/src/main.cc +++ b/re2c/src/main.cc @@ -47,11 +47,6 @@ int main(int, char *argv[]) error ("cannot open header file: %s", opts.header_file); return 1; } - if (flag_skeleton && !output.data.open ()) - { - error ("cannot open data file: %s", output.data.file_name.c_str ()); - return 1; - } Scanner scanner (input, output.source); parse (scanner, output);