Improved graphviz output and got -M and -S working again.
[external/ragel.git] / ragel / cdcodegen.cpp
index ec2ddb0..ceb0030 100644 (file)
 #include <string>
 #include <assert.h>
 
-/* Code generators. */
-#include "cdtable.h"
-#include "cdftable.h"
-#include "cdflat.h"
-#include "cdfflat.h"
-#include "cdgoto.h"
-#include "cdfgoto.h"
-#include "cdipgoto.h"
-#include "cdsplit.h"
 
 using std::ostream;
 using std::ostringstream;
@@ -54,7 +45,6 @@ using std::cerr;
 using std::endl;
 
 /* Target language and output style. */
-extern CodeStyleEnum codeStyle;
 
 /* Io globals. */
 extern istream *inStream;
@@ -68,131 +58,6 @@ extern bool graphvizDone;
 extern int numSplitPartitions;
 extern bool noLineDirectives;
 
-/* Invoked by the parser when the root element is opened. */
-ostream *cdOpenOutput( const char *inputFile )
-{
-       if ( hostLang->lang != HostLang::C && hostLang->lang != HostLang::D ) {
-               error() << "this code generator is for C and D only" << endl;
-               exit(1);
-       }
-
-       /* If the output format is code and no output file name is given, then
-        * make a default. */
-       if ( outputFileName == 0 ) {
-               const char *ext = findFileExtension( inputFile );
-               if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
-                       outputFileName = fileNameFromStem( inputFile, ".h" );
-               else {
-                       const char *defExtension = 0;
-                       switch ( hostLang->lang ) {
-                               case HostLang::C: defExtension = ".c"; break;
-                               case HostLang::D: defExtension = ".d"; break;
-                               default: break;
-                       }
-                       outputFileName = fileNameFromStem( inputFile, defExtension );
-               }
-       }
-
-       /* Make sure we are not writing to the same file as the input file. */
-       if ( outputFileName != 0 && strcmp( inputFile, outputFileName  ) == 0 ) {
-               error() << "output file \"" << outputFileName  << 
-                               "\" is the same as the input file" << endl;
-       }
-
-       if ( outputFileName != 0 ) {
-               /* Create the filter on the output and open it. */
-               outFilter = new output_filter( outputFileName );
-               outFilter->open( outputFileName, ios::out|ios::trunc );
-               if ( !outFilter->is_open() ) {
-                       error() << "error opening " << outputFileName << " for writing" << endl;
-                       exit(1);
-               }
-
-               /* Open the output stream, attaching it to the filter. */
-               outStream = new ostream( outFilter );
-       }
-       else {
-               /* Writing out ot std out. */
-               outStream = &cout;
-       }
-       return outStream;
-}
-
-/* Invoked by the parser when a ragel definition is opened. */
-CodeGenData *cdMakeCodeGen( const char *sourceFileName, const char *fsmName, 
-               ostream &out, bool wantComplete )
-{
-       CodeGenData *codeGen = 0;
-       switch ( hostLang->lang ) {
-       case HostLang::C:
-               switch ( codeStyle ) {
-               case GenTables:
-                       codeGen = new CTabCodeGen(out);
-                       break;
-               case GenFTables:
-                       codeGen = new CFTabCodeGen(out);
-                       break;
-               case GenFlat:
-                       codeGen = new CFlatCodeGen(out);
-                       break;
-               case GenFFlat:
-                       codeGen = new CFFlatCodeGen(out);
-                       break;
-               case GenGoto:
-                       codeGen = new CGotoCodeGen(out);
-                       break;
-               case GenFGoto:
-                       codeGen = new CFGotoCodeGen(out);
-                       break;
-               case GenIpGoto:
-                       codeGen = new CIpGotoCodeGen(out);
-                       break;
-               case GenSplit:
-                       codeGen = new CSplitCodeGen(out);
-                       break;
-               }
-               break;
-
-       case HostLang::D:
-               switch ( codeStyle ) {
-               case GenTables:
-                       codeGen = new DTabCodeGen(out);
-                       break;
-               case GenFTables:
-                       codeGen = new DFTabCodeGen(out);
-                       break;
-               case GenFlat:
-                       codeGen = new DFlatCodeGen(out);
-                       break;
-               case GenFFlat:
-                       codeGen = new DFFlatCodeGen(out);
-                       break;
-               case GenGoto:
-                       codeGen = new DGotoCodeGen(out);
-                       break;
-               case GenFGoto:
-                       codeGen = new DFGotoCodeGen(out);
-                       break;
-               case GenIpGoto:
-                       codeGen = new DIpGotoCodeGen(out);
-                       break;
-               case GenSplit:
-                       codeGen = new DSplitCodeGen(out);
-                       break;
-               }
-               break;
-
-       default: break;
-       }
-
-       codeGen->sourceFileName = sourceFileName;
-       codeGen->fsmName = fsmName;
-       codeGen->wantComplete = wantComplete;
-
-       return codeGen;
-}
-
-
 void cdLineDirective( ostream &out, const char *fileName, int line )
 {
        if ( noLineDirectives )
@@ -700,7 +565,7 @@ void FsmCodeGen::writeInit()
 {
        out << "        {\n";
 
-       if ( writeCS )
+       if ( !noCS )
                out << "\t" << CS() << " = " << START() << ";\n";
        
        /* If there are any calls, then the stack top needs initialization. */
@@ -718,7 +583,7 @@ void FsmCodeGen::writeInit()
 
 string FsmCodeGen::DATA_PREFIX()
 {
-       if ( dataPrefix )
+       if ( !noPrefix )
                return FSM_NAME() + "_";
        return "";
 }
@@ -759,10 +624,10 @@ void FsmCodeGen::STATE_IDS()
        if ( redFsm->startState != 0 )
                STATIC_VAR( "int", START() ) << " = " << START_STATE_ID() << ";\n";
 
-       if ( writeFirstFinal )
+       if ( !noFinal )
                STATIC_VAR( "int" , FIRST_FINAL() ) << " = " << FIRST_FINAL_STATE() << ";\n";
 
-       if ( writeErr )
+       if ( !noError )
                STATIC_VAR( "int", ERROR() ) << " = " << ERROR_STATE() << ";\n";
 
        out << "\n";
@@ -776,6 +641,20 @@ void FsmCodeGen::STATE_IDS()
        }
 }
 
+void FsmCodeGen::writeStart()
+{
+       out << START_STATE_ID();
+}
+
+void FsmCodeGen::writeFirstFinal()
+{
+       out << FIRST_FINAL_STATE();
+}
+
+void FsmCodeGen::writeError()
+{
+       out << ERROR_STATE();
+}
 
 /*
  * Language specific, but style independent code generators functions.