From 8ca48cc3abef6eea4c1a15e61ce4c268978d98e9 Mon Sep 17 00:00:00 2001 From: thurston Date: Sun, 31 Aug 2008 16:44:13 +0000 Subject: [PATCH] Backend main functions are now factored out. git-svn-id: http://svn.complang.org/ragel/trunk@465 052ea7fc-9027-0410-9066-f65837a77df0 --- ragel/Makefile.in | 4 +-- ragel/main.cpp | 64 ++++++++++++++++++++++++-------------- ragel/ragel.h | 3 ++ rlgen-cd/main.cpp | 86 +++++---------------------------------------------- rlgen-csharp/main.cpp | 36 --------------------- rlgen-dot/main.cpp | 68 ++-------------------------------------- rlgen-java/main.cpp | 69 ++--------------------------------------- rlgen-ruby/main.cpp | 77 ++------------------------------------------- 8 files changed, 60 insertions(+), 347 deletions(-) diff --git a/ragel/Makefile.in b/ragel/Makefile.in index 4185933..5e54e57 100644 --- a/ragel/Makefile.in +++ b/ragel/Makefile.in @@ -32,12 +32,12 @@ CC_SRCS = \ GEN_SRC = rlscan.cpp rlparse.h rlparse.cpp RAGEL_LIBS = ../common/common.a \ + ../redfsm/redfsm.a \ ../rlgen-cd/rlgen-cd.a \ ../rlgen-java/rlgen-java.a \ ../rlgen-ruby/rlgen-ruby.a \ ../rlgen-csharp/rlgen-csharp.a \ - ../rlgen-dot/rlgen-dot.a \ - ../redfsm/redfsm.a + ../rlgen-dot/rlgen-dot.a LIBS = $(RAGEL_LIBS) MINGW_LIBS = -lpsapi diff --git a/ragel/main.cpp b/ragel/main.cpp index 19c6bce..3121420 100644 --- a/ragel/main.cpp +++ b/ragel/main.cpp @@ -84,13 +84,18 @@ CodeStyleEnum codeStyle = GenTables; int numSplitPartitions = 0; bool noLineDirectives = false; + bool displayPrintables = false; +bool graphvizDone = false; /* Target ruby impl */ RubyImplEnum rubyImpl = MRI; ArgsVector includePaths; +istream *inStream = 0; +ostream *outStream = 0; +output_filter *outFilter = 0; const char *outputFileName = 0; /* Print a summary of the options. */ @@ -548,33 +553,44 @@ void cleanExit( const char *intermed, int status ) exit( status ); } -int cd_main( const char *xmlInputFileName ); -int java_main( const char *xmlInputFileName ); -int ruby_main( const char *xmlInputFileName ); -int csharp_main( const char *xmlInputFileName ); -int dot_main( const char *xmlInputFileName ); - void backend( const char *intermed ) { + const char *xmlInputFileName = intermed; + + bool wantComplete = true; + bool outputActive = true; + + /* Open the input file for reading. */ + ifstream *inFile = new ifstream( xmlInputFileName ); + inStream = inFile; + if ( ! inFile->is_open() ) + error() << "could not open " << xmlInputFileName << " for reading" << endl; + + /* Bail on above error. */ + if ( gblErrorCount > 0 ) + exit(1); + /* Locate the backend program */ - if ( generateDot ) - dot_main( intermed ); - else { - switch ( hostLang->lang ) { - case HostLang::C: - case HostLang::D: - cd_main( intermed ); - break; - case HostLang::Java: - java_main( intermed ); - break; - case HostLang::Ruby: - ruby_main( intermed ); - break; - case HostLang::CSharp: - csharp_main( intermed ); - break; - } + if ( generateDot ) { + wantComplete = false; + outputActive = false; + } + + xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete ); + + /* If writing to a file, delete the ostream, causing it to flush. + * Standard out is flushed automatically. */ + if ( outputFileName != 0 ) { + delete outStream; + delete outFilter; + } + + /* Finished, final check for errors.. */ + if ( gblErrorCount > 0 ) { + /* If we opened an output file, remove it. */ + if ( outputFileName != 0 ) + unlink( outputFileName ); + exit(1); } } diff --git a/ragel/ragel.h b/ragel/ragel.h index 125ceb7..d3f0beb 100644 --- a/ragel/ragel.h +++ b/ragel/ragel.h @@ -87,4 +87,7 @@ void xmlEscapeHost( std::ostream &out, char *data, long len ); typedef Vector ArgsVector; extern ArgsVector includePaths; +int xml_parse( std::istream &input, const char *fileName, + bool outputActive, bool wantComplete ); + #endif /* _RAGEL_H */ diff --git a/rlgen-cd/main.cpp b/rlgen-cd/main.cpp index a389afd..f4a0d4f 100644 --- a/rlgen-cd/main.cpp +++ b/rlgen-cd/main.cpp @@ -56,54 +56,17 @@ using std::endl; extern CodeStyleEnum codeStyle; /* Io globals. */ -istream *inStream = 0; -ostream *outStream = 0; -output_filter *outFilter = 0; +extern istream *inStream; +extern ostream *outStream; +extern output_filter *outFilter; extern const char *outputFileName; /* Graphviz dot file generation. */ -bool graphvizDone = false; +extern bool graphvizDone; extern int numSplitPartitions; extern bool noLineDirectives; -/* Print a summary of the options. */ -void cd_usage() -{ - cout << -"usage: " PROGNAME " [options] file\n" -"general:\n" -" -h, -H, -?, --help Print this usage and exit\n" -" -v, --version Print version information and exit\n" -" -o Write output to \n" -"code generation options:\n" -" -L Inhibit writing of #line directives\n" -"generated code style:\n" -" -T0 Table driven FSM (default)\n" -" -T1 Faster table driven FSM\n" -" -F0 Flat table driven FSM\n" -" -F1 Faster flat table-driven FSM\n" -" -G0 Goto-driven FSM\n" -" -G1 Faster goto-driven FSM\n" -" -G2 Really fast goto-driven FSM\n" -" -P N-Way Split really fast goto-driven FSM\n" - ; -} - -/* Print version information. */ -void cd_version() -{ - cout << "Ragel Code Generator for C, C++, Objective-C and D" << endl << - "Version " VERSION << ", " PUBDATE << endl << - "Copyright (c) 2001-2007 by Adrian Thurston" << endl; -} - -ostream &cd_error() -{ - gblErrorCount += 1; - cerr << PROGNAME ": "; - return cerr; -} /* * Callbacks invoked by the XML data parser. @@ -113,7 +76,7 @@ ostream &cd_error() ostream *cdOpenOutput( char *inputFile ) { if ( hostLang->lang != HostLang::C && hostLang->lang != HostLang::D ) { - cd_error() << "this code generator is for C and D only" << endl; + error() << "this code generator is for C and D only" << endl; exit(1); } @@ -136,7 +99,7 @@ ostream *cdOpenOutput( char *inputFile ) /* Make sure we are not writing to the same file as the input file. */ if ( outputFileName != 0 && strcmp( inputFile, outputFileName ) == 0 ) { - cd_error() << "output file \"" << outputFileName << + error() << "output file \"" << outputFileName << "\" is the same as the input file" << endl; } @@ -145,7 +108,7 @@ ostream *cdOpenOutput( char *inputFile ) outFilter = new output_filter( outputFileName ); outFilter->open( outputFileName, ios::out|ios::trunc ); if ( !outFilter->is_open() ) { - cd_error() << "error opening " << outputFileName << " for writing" << endl; + error() << "error opening " << outputFileName << " for writing" << endl; exit(1); } @@ -233,38 +196,3 @@ CodeGenData *cdMakeCodeGen( char *sourceFileName, char *fsmName, return codeGen; } -/* Main, process args and call yyparse to start scanning input. */ -int cd_main( const char *xmlInputFileName ) -{ - /* Open the input file for reading. */ - ifstream *inFile = new ifstream( xmlInputFileName ); - inStream = inFile; - if ( ! inFile->is_open() ) - cd_error() << "could not open " << xmlInputFileName << " for reading" << endl; - - /* Bail on above errors. */ - if ( gblErrorCount > 0 ) - exit(1); - - bool wantComplete = true; - bool outputActive = true; - - /* Parse the input! */ - xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete ); - - /* If writing to a file, delete the ostream, causing it to flush. - * Standard out is flushed automatically. */ - if ( outputFileName != 0 ) { - delete outStream; - delete outFilter; - } - - /* Finished, final check for errors.. */ - if ( gblErrorCount > 0 ) { - /* If we opened an output file, remove it. */ - if ( outputFileName != 0 ) - unlink( outputFileName ); - exit(1); - } - return 0; -} diff --git a/rlgen-csharp/main.cpp b/rlgen-csharp/main.cpp index 1c67390..1a78848 100644 --- a/rlgen-csharp/main.cpp +++ b/rlgen-csharp/main.cpp @@ -189,39 +189,3 @@ CodeGenData *csharpMakeCodeGen( char *sourceFileName, char *fsmName, return codeGen; } - -/* Main, process args and call yyparse to start scanning input. */ -int csharp_main( const char *xmlInputFileName ) -{ - /* Open the input file for reading. */ - ifstream *inFile = new ifstream( xmlInputFileName ); - inStream = inFile; - if ( ! inFile->is_open() ) - csharp_error() << "could not open " << xmlInputFileName << " for reading" << endl; - - /* Bail on above errors. */ - if ( gblErrorCount > 0 ) - exit(1); - - bool wantComplete = true; - bool outputActive = true; - - /* Parse the input! */ - xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete ); - - /* If writing to a file, delete the ostream, causing it to flush. - * Standard out is flushed automatically. */ - if ( outputFileName != 0 ) { - delete outStream; - delete outFilter; - } - - /* Finished, final check for errors.. */ - if ( gblErrorCount > 0 ) { - /* If we opened an output file, remove it. */ - if ( outputFileName != 0 ) - unlink( outputFileName ); - exit(1); - } - return 0; -} diff --git a/rlgen-dot/main.cpp b/rlgen-dot/main.cpp index d2f19d8..20560c2 100644 --- a/rlgen-dot/main.cpp +++ b/rlgen-dot/main.cpp @@ -56,33 +56,6 @@ extern bool displayPrintables; extern int numSplitPartitions; /* Print a summary of the options. */ -void dot_usage() -{ - cout << -"usage: " PROGNAME " [options] file\n" -"general:\n" -" -h, -H, -?, --help Print this usage and exit\n" -" -v, --version Print version information and exit\n" -" -o Write output to \n" -"output:\n" -" -p Display printable characters on labels\n" - ; -} - -/* Print version information. */ -void dot_version() -{ - cout << "Ragel Code Generator for Graphviz" << endl << - "Version " VERSION << ", " PUBDATE << endl << - "Copyright (c) 2001-2007 by Adrian Thurston" << endl; -} - -ostream &dot_error() -{ - gblErrorCount += 1; - cerr << PROGNAME ": "; - return cerr; -} /* * Callbacks invoked by the XML data parser. @@ -93,7 +66,7 @@ ostream *dotOpenOutput( char *inputFile ) { /* Make sure we are not writing to the same file as the input file. */ if ( outputFileName != 0 && strcmp( inputFile, outputFileName ) == 0 ) { - dot_error() << "output file \"" << outputFileName << + error() << "output file \"" << outputFileName << "\" is the same as the input file" << endl; } @@ -102,7 +75,7 @@ ostream *dotOpenOutput( char *inputFile ) outFilter = new output_filter( outputFileName ); outFilter->open( outputFileName, ios::out|ios::trunc ); if ( !outFilter->is_open() ) { - dot_error() << "error opening " << outputFileName << " for writing" << endl; + error() << "error opening " << outputFileName << " for writing" << endl; exit(1); } @@ -128,40 +101,3 @@ CodeGenData *dotMakeCodeGen( char *sourceFileName, char *fsmName, return codeGen; } - - -/* Main, process args and call yyparse to start scanning input. */ -int dot_main( const char *xmlInputFileName ) -{ - /* Open the input file for reading. */ - ifstream *inFile = new ifstream( xmlInputFileName ); - inStream = inFile; - if ( ! inFile->is_open() ) - dot_error() << "could not open " << xmlInputFileName << " for reading" << endl; - - /* Bail on above errors. */ - if ( gblErrorCount > 0 ) - exit(1); - - bool wantComplete = false; - bool outputActive = false; - - /* Parse the input! */ - xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete ); - - /* If writing to a file, delete the ostream, causing it to flush. - * Standard out is flushed automatically. */ - if ( outputFileName != 0 ) { - delete outStream; - delete outFilter; - } - - /* Finished, final check for errors.. */ - if ( gblErrorCount > 0 ) { - /* If we opened an output file, remove it. */ - if ( outputFileName != 0 ) - unlink( outputFileName ); - exit(1); - } - return 0; -} diff --git a/rlgen-java/main.cpp b/rlgen-java/main.cpp index 06398c2..ae4559a 100644 --- a/rlgen-java/main.cpp +++ b/rlgen-java/main.cpp @@ -51,33 +51,6 @@ extern const char *outputFileName; extern int numSplitPartitions; -/* Print a summary of the options. */ -void java_usage() -{ - cout << -"usage: " PROGNAME " [options] file\n" -"general:\n" -" -h, -H, -?, --help Print this usage and exit\n" -" -v, --version Print version information and exit\n" -" -o Write output to \n" - ; -} - -/* Print version information. */ -void java_version() -{ - cout << "Ragel Code Generator for Java" << endl << - "Version " VERSION << ", " PUBDATE << endl << - "Copyright (c) 2001-2007 by Adrian Thurston" << endl; -} - -ostream &java_error() -{ - gblErrorCount += 1; - cerr << PROGNAME ": "; - return cerr; -} - /* * Callbacks invoked by the XML data parser. */ @@ -86,7 +59,7 @@ ostream &java_error() ostream *javaOpenOutput( char *inputFile ) { if ( hostLang->lang != HostLang::Java ) { - java_error() << "this code generator is for Java only" << endl; + error() << "this code generator is for Java only" << endl; exit(1); } @@ -102,7 +75,7 @@ ostream *javaOpenOutput( char *inputFile ) /* Make sure we are not writing to the same file as the input file. */ if ( outputFileName != 0 && strcmp( inputFile, outputFileName ) == 0 ) { - java_error() << "output file \"" << outputFileName << + error() << "output file \"" << outputFileName << "\" is the same as the input file" << endl; } @@ -111,7 +84,7 @@ ostream *javaOpenOutput( char *inputFile ) outFilter = new output_filter( outputFileName ); outFilter->open( outputFileName, ios::out|ios::trunc ); if ( !outFilter->is_open() ) { - java_error() << "error opening " << outputFileName << " for writing" << endl; + error() << "error opening " << outputFileName << " for writing" << endl; exit(1); } @@ -137,39 +110,3 @@ CodeGenData *javaMakeCodeGen( char *sourceFileName, char *fsmName, return codeGen; } - -/* Main, process args and call yyparse to start scanning input. */ -int java_main( const char *xmlInputFileName ) -{ - /* Open the input file for reading. */ - ifstream *inFile = new ifstream( xmlInputFileName ); - inStream = inFile; - if ( ! inFile->is_open() ) - java_error() << "could not open " << xmlInputFileName << " for reading" << endl; - - /* Bail on above errors. */ - if ( gblErrorCount > 0 ) - exit(1); - - bool wantComplete = true; - bool outputActive = true; - - /* Parse the input! */ - xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete ); - - /* If writing to a file, delete the ostream, causing it to flush. - * Standard out is flushed automatically. */ - if ( outputFileName != 0 ) { - delete outStream; - delete outFilter; - } - - /* Finished, final check for errors.. */ - if ( gblErrorCount > 0 ) { - /* If we opened an output file, remove it. */ - if ( outputFileName != 0 ) - unlink( outputFileName ); - exit(1); - } - return 0; -} diff --git a/rlgen-ruby/main.cpp b/rlgen-ruby/main.cpp index 7e89968..401874c 100644 --- a/rlgen-ruby/main.cpp +++ b/rlgen-ruby/main.cpp @@ -65,41 +65,6 @@ extern bool graphvizDone; extern int numSplitPartitions; -/* Print a summary of the options. */ -void ruby_usage() -{ - cout << -"usage: " PROGNAME " [options] file\n" -"general:\n" -" -h, -H, -?, --help Print this usage and exit\n" -" -v, --version Print version information and exit\n" -" -o Write output to \n" -" --rbx Allow to use Rubinius asm features\n" -"generated code style:\n" -" -T0 Table driven FSM (default)\n" -" -T1 Faster table driven FSM\n" -" -F0 Flat table driven FSM\n" -" -F1 Faster flat table-driven FSM\n" -" -G0 Goto-driven FSM\n" - ; -} - -/* Print version information. */ -void ruby_version() -{ - cout << "Ragel Code Generator for Ruby" << endl << - "Version " VERSION << ", " PUBDATE << endl << - "Copyright (c) 2001-2007 by Adrian Thurston" << endl << - "Copyright (c) 2007 by Victor Hugo Borja" << endl; -} - -ostream &ruby_error() -{ - gblErrorCount += 1; - cerr << PROGNAME ": "; - return cerr; -} - /* * Callbacks invoked by the XML data parser. */ @@ -108,7 +73,7 @@ ostream &ruby_error() ostream *rubyOpenOutput( char *inputFile ) { if ( hostLang->lang != HostLang::Ruby ) { - ruby_error() << "this code generator is for Ruby only" << endl; + error() << "this code generator is for Ruby only" << endl; exit(1); } @@ -124,7 +89,7 @@ ostream *rubyOpenOutput( char *inputFile ) /* Make sure we are not writing to the same file as the input file. */ if ( outputFileName != 0 && strcmp( inputFile, outputFileName ) == 0 ) { - ruby_error() << "output file \"" << outputFileName << + error() << "output file \"" << outputFileName << "\" is the same as the input file" << endl; } @@ -133,7 +98,7 @@ ostream *rubyOpenOutput( char *inputFile ) outFilter = new output_filter( outputFileName ); outFilter->open( outputFileName, ios::out|ios::trunc ); if ( !outFilter->is_open() ) { - ruby_error() << "error opening " << outputFileName << " for writing" << endl; + error() << "error opening " << outputFileName << " for writing" << endl; exit(1); } @@ -188,42 +153,6 @@ CodeGenData *rubyMakeCodeGen( char *sourceFileName, char *fsmName, return codeGen; } -/* Main, process args and call yyparse to start scanning input. */ -int ruby_main( const char *xmlInputFileName ) -{ - /* Open the input file for reading. */ - ifstream *inFile = new ifstream( xmlInputFileName ); - inStream = inFile; - if ( ! inFile->is_open() ) - ruby_error() << "could not open " << xmlInputFileName << " for reading" << endl; - - /* Bail on above errors. */ - if ( gblErrorCount > 0 ) - exit(1); - - bool wantComplete = true; - bool outputActive = true; - - /* Parse the input! */ - xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete ); - - /* If writing to a file, delete the ostream, causing it to flush. - * Standard out is flushed automatically. */ - if ( outputFileName != 0 ) { - delete outStream; - delete outFilter; - } - - /* Finished, final check for errors.. */ - if ( gblErrorCount > 0 ) { - /* If we opened an output file, remove it. */ - if ( outputFileName != 0 ) - unlink( outputFileName ); - exit(1); - } - return 0; -} - /* * Local Variables: * mode: c++ -- 2.7.4