in = &i;
- output_version_time (o.source);
+ output_version_time (o.source.fragment ());
output_line_info (o.source.fragment (), in->get_cline (), in->get_fname ().c_str ());
- output_version_time (o.header);
Enc encodingOld = encoding;
}
genTypes (o, specMap);
- if (o.header.status != OutputFile::NO_FILE)
- {
- o.header.insert_line_info ();
- o.header << "\n";
- o.header.insert_types ();
- }
}
else
{
o << "#define YYMAXFILL " << max_fill << "\n";
}
-void output_line_info (OutputFragment & o, uint line_number, const char * filename)
+void output_line_info (std::ostream & o, uint line_number, const char * file_name)
{
if (!iFlag)
{
- o << "#line " << line_number << " \"" << filename << "\"\n";
+ o << "#line " << line_number << " \"" << file_name << "\"\n";
}
}
-void output_types (OutputFragment & o, const std::vector<std::string> & types)
+void output_types (std::ostream & o, uint ind, const std::vector<std::string> & types)
{
- uint ind = o.indent;
o << indent (ind++) << "enum " << mapCodeName["YYCONDTYPE"] << " {\n";
for (unsigned int i = 0; i < types.size (); ++i)
{
o << indent (--ind) << "};\n";
}
-void output_version_time (OutputFile & o)
+void output_version_time (std::ostream & o)
{
o << "/* Generated by re2c " PACKAGE_VERSION;
if (!bNoGenerationDate)
namespace re2c {
Input::Input (const char * fn)
- : status (OK)
- , file (NULL)
+ : file (NULL)
, file_name (fn)
+{}
+
+bool Input::open ()
{
if (file_name == "<stdin>")
{
else
{
file = fopen (file_name.c_str (), "rb");
- if (file == NULL)
- {
- status = FAIL_OPEN;
- }
}
+ return file != NULL;
}
Input::~Input ()
struct Input
{
- enum status_t
- { OK
- , FAIL_OPEN
- } status;
FILE * file;
std::string file_name;
Input (const char * fn);
~Input ();
+ bool open ();
};
} // namespace re2c
// set up the source stream
re2c::Input input (sourceFileName);
- if (input.status == Input::FAIL_OPEN)
+ if (!input.open ())
{
cerr << "re2c: error: cannot open " << sourceFileName << "\n";
return 1;
// set up the output streams
re2c::Output output (outputFileName, headerFileName);
- if (output.source.status == OutputFile::FAIL_OPEN)
+ if (!output.source.open ())
{
cerr << "re2c: error: cannot open " << outputFileName << "\n";
return 1;
}
- if (output.header.status == OutputFile::FAIL_OPEN)
+ if (tFlag && !output.header.open ())
{
cerr << "re2c: error: cannot open " << headerFileName << "\n";
return 1;
Scanner scanner (input, output.source);
parse (scanner, output);
- // output generated code
- output.emit ();
-
return 0;
}
}
OutputFile::OutputFile (const char * fn)
- : status (NO_FILE)
- , filename (fn)
+ : file_name (fn)
, file (NULL)
, blocks ()
, prolog_label (0)
{
- if (filename != NULL)
+ new_block ();
+}
+
+bool OutputFile::open ()
+{
+ if (strcmp (file_name, "<stdout>") == 0)
{
- if (strcmp (filename, "<stdout>") == 0)
- {
- file = stdout;
- status = OK;
- }
- else
- {
- file = fopen (filename, "wb");
- status = file == NULL
- ? FAIL_OPEN
- : OK;
- }
+ file = stdout;
}
- new_block ();
+ else
+ {
+ file = fopen (file_name, "wb");
+ }
+ return file != NULL;
}
OutputFile::~OutputFile ()
case OutputFragment::CODE:
break;
case OutputFragment::LINE_INFO:
- output_line_info (f, line_count + 1, filename);
+ output_line_info (f, line_count + 1, file_name);
break;
case OutputFragment::STATE_GOTO:
output_state_goto (f, prolog_label);
break;
case OutputFragment::TYPES:
- output_types (f, types);
+ output_types (f, f.indent, types);
break;
case OutputFragment::YYACCEPT_INIT:
output_yyaccept_init (f, b.used_yyaccept);
}
}
-void Output::emit ()
+HeaderFile::HeaderFile (const char * fn)
+ : stream ()
+ , file_name (fn)
+ , file (NULL)
+{}
+
+bool HeaderFile::open ()
+{
+ file = fopen (file_name, "wb");
+ return file != NULL;
+}
+
+void HeaderFile::emit (const std::vector<std::string> & types)
+{
+ output_version_time (stream);
+ output_line_info (stream, 3, file_name);
+ stream << "\n";
+ output_types (stream, 0, types);
+}
+
+HeaderFile::~HeaderFile ()
+{
+ if (file != NULL)
+ {
+ std::string content = stream.str ();
+ fwrite (content.c_str (), 1, content.size (), file);
+ fclose (file);
+ }
+}
+
+Output::Output (const char * source_name, const char * header_name)
+ : source (source_name)
+ , header (header_name)
+ , types ()
+ , max_fill (1)
+{}
+
+Output::~Output ()
{
source.emit (types, max_fill);
- header.emit (types, max_fill);
+ header.emit (types);
}
} // namespace re2c
struct OutputFile
{
- enum status_t
- { NO_FILE
- , OK
- , FAIL_OPEN
- } status;
-
- OutputFile (const char * filename);
+ OutputFile (const char * fn);
~OutputFile ();
+ bool open ();
OutputFragment & fragment ();
void write (const char * s, std::streamsize n);
void insert_line_info ();
friend OutputFile & operator << (OutputFile & o, const Setw & s);
private:
- const char * filename;
+ const char * file_name;
FILE * file;
std::vector<OutputBlock *> blocks;
uint prolog_label;
void insert_code ();
};
+struct HeaderFile
+{
+ HeaderFile (const char * fn);
+ ~HeaderFile ();
+ bool open ();
+ void emit (const std::vector<std::string> & types);
+
+private:
+ std::ostringstream stream;
+ const char * file_name;
+ FILE * file;
+};
+
struct Output
{
OutputFile source;
- OutputFile header;
+ HeaderFile header;
std::vector<std::string> types;
uint max_fill;
- Output (const char * source_name, const char * header_name)
- : source (source_name)
- , header (header_name)
- , types ()
- , max_fill (1)
- {}
-
- void emit ();
+ Output (const char * source_name, const char * header_name);
+ ~Output ();
};
} // namespace re2c
in = &i;
- output_version_time (o.source);
+ output_version_time (o.source.fragment ());
output_line_info (o.source.fragment (), in->get_cline (), in->get_fname ().c_str ());
- output_version_time (o.header);
Enc encodingOld = encoding;
}
genTypes (o, specMap);
- if (o.header.status != OutputFile::NO_FILE)
- {
- o.header.insert_line_info ();
- o.header << "\n";
- o.header.insert_types ();
- }
}
else
{
extern void genTypes(Output &, const RegExpMap&);
extern void output_state_goto (OutputFragment &, uint);
-extern void output_types (OutputFragment &, const std::vector<std::string> &);
-extern void output_version_time (OutputFile &);
+extern void output_types (std::ostream &, uint, const std::vector<std::string> &);
+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_line_info (OutputFragment &, uint, const char *);
+extern void output_line_info (std::ostream &, uint, const char *);
extern RegExp *mkDiff(RegExp*, RegExp*);
extern RegExp *mkAlt(RegExp*, RegExp*);