The conversion from the string language type which comes in from the XML file
authorthurston <thurston@052ea7fc-9027-0410-9066-f65837a77df0>
Sun, 4 Feb 2007 23:54:47 +0000 (23:54 +0000)
committerthurston <thurston@052ea7fc-9027-0410-9066-f65837a77df0>
Sun, 4 Feb 2007 23:54:47 +0000 (23:54 +0000)
to the hostLangType and hostLang variables is now handled by the redfsm.a
library. The code generators can use hostLang variable to check that the host
language is correct.

Adapted the line directive writing for Java and Ruby. In these languages a
comment which indicates the line number is written.

git-svn-id: http://svn.complang.org/ragel/trunk@77 052ea7fc-9027-0410-9066-f65837a77df0

redfsm/gendata.cpp
redfsm/gendata.h
redfsm/xmlparse.kl
rlcodegen/fsmcodegen.cpp
rlcodegen/main.cpp
rlgen-java/javacodegen.cpp
rlgen-java/main.cpp
rlgen-ruby/main.cpp
rlgen-ruby/rubycodegen.cpp

index e8a6ca6..475af26 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2005-2006 Adrian Thurston <thurston@cs.queensu.ca>
+ *  Copyright 2005-2007 Adrian Thurston <thurston@cs.queensu.ca>
  */
 
 /*  This file is part of Ragel.
@@ -636,21 +636,3 @@ void CodeGenData::analyzeMachine()
        /* Set the maximums of various values used for deciding types. */
        setValueLimits();
 }
-
-
-
-void lineDirective( ostream &out, char *fileName, int line )
-{
-       if ( hostLangType != JavaCode ) {
-               /* Write the preprocessor line info for to the input file. */
-               out << "#line " << line  << " \"";
-               for ( char *pc = fileName; *pc != 0; pc++ ) {
-                       if ( *pc == '\\' )
-                               out << "\\\\";
-                       else
-                               out << *pc;
-               }
-               out << "\"\n";
-       }
-}
-
index e0ee6df..a195dab 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2005-2006 Adrian Thurston <thurston@cs.queensu.ca>
+ *  Copyright 2005-2007 Adrian Thurston <thurston@cs.queensu.ca>
  */
 
 /*  This file is part of Ragel.
@@ -39,8 +39,27 @@ struct CodeGenData;
 typedef AvlMap<char *, CodeGenData*, CmpStr> CodeGenMap;
 typedef AvlMapEl<char *, CodeGenData*> CodeGenMapEl;
 
+/*
+ * The interface to the parser
+ */
+
+/* These two functions must be implemented by the code generation executable.
+ * The openOutput function is invoked when the root element is opened.  The
+ * makeCodeGen function is invoked when a ragel_def element is opened. */
+std::ostream *openOutput( char *inputFile );
+CodeGenData *makeCodeGen( char *sourceFileName, 
+               char *fsmName, ostream &out, bool wantComplete );
+
 struct CodeGenData
 {
+       /*
+        * The interface to the code generator.
+        */
+       virtual void finishRagelDef() {}
+       virtual void writeStatement( InputLoc &loc, int nargs, char **args ) {}
+
+       /********************/
+
        CodeGenData( ostream &out );
        virtual ~CodeGenData() {}
 
@@ -125,19 +144,9 @@ struct CodeGenData
        void prepareMachine();
        bool hasBeenPrepared;
 
-       /* The interface to the code generator. */
-       virtual void finishRagelDef() {}
-       virtual void writeStatement( InputLoc &loc, int nargs, char **args ) {}
 };
 
 void lineDirective( ostream &out, char *fileName, int line );
 void genLineDirective( ostream &out );
 
-/* These two functions must be implemented by the code generation executable.
- * The openOutput function is invoked when the root element is opened.  The
- * makeCodeGen function is invoked when a ragel_def element is opened. */
-std::ostream *openOutput( char *inputFile, char *language );
-CodeGenData *makeCodeGen( char *sourceFileName, 
-               char *fsmName, ostream &out, bool wantComplete );
-
 #endif /* _GENDATA_H */
index 0d90418..15b4a11 100644 (file)
@@ -64,7 +64,28 @@ tag_ragel_head: TAG_ragel
                        if ( langAttr == 0 )
                                error($1->loc) << "tag <ragel> requires a lang attribute" << endl;
                        else {
-                               outStream = openOutput( sourceFileName, langAttr->value );
+                               if ( strcmp( langAttr->value, "C" ) == 0 ) {
+                                       hostLangType = CCode;
+                                       hostLang = &hostLangC;
+                               }
+                               else if ( strcmp( langAttr->value, "D" ) == 0 ) {
+                                       hostLangType = DCode;
+                                       hostLang = &hostLangD;
+                               }
+                               else if ( strcmp( langAttr->value, "Java" ) == 0 ) {
+                                       hostLangType = JavaCode;
+                                       hostLang = &hostLangJava;
+                               }
+                               else if ( strcmp( langAttr->value, "Ruby" ) == 0 ) {
+                                       hostLangType = RubyCode;
+                                       hostLang = &hostLangRuby;
+                               }
+                               else {
+                                       error($1->loc) << "expecting lang attribute to be "
+                                                       "one of C, D, Java or Ruby" << endl;
+                               }
+
+                               outStream = openOutput( sourceFileName );
                        }
                }
        };
index 888f534..42314bb 100644 (file)
@@ -37,6 +37,27 @@ using std::string;
 using std::cerr;
 using std::endl;
 
+void lineDirective( ostream &out, char *fileName, int line )
+{
+       /* Write the preprocessor line info for to the input file. */
+       out << "#line " << line  << " \"";
+       for ( char *pc = fileName; *pc != 0; pc++ ) {
+               if ( *pc == '\\' )
+                       out << "\\\\";
+               else
+                       out << *pc;
+       }
+       out << "\"\n";
+}
+
+void genLineDirective( ostream &out )
+{
+       assert( outputFormat == OutCode );
+       std::streambuf *sbuf = out.rdbuf();
+       output_filter *filter = static_cast<output_filter*>(sbuf);
+       lineDirective( out, filter->fileName, filter->line + 1 );
+}
+
 
 /* Init code gen with in parameters. */
 FsmCodeGen::FsmCodeGen( ostream &out )
@@ -745,11 +766,3 @@ ostream &FsmCodeGen::source_error( const InputLoc &loc )
        return cerr;
 }
 
-void genLineDirective( ostream &out )
-{
-       assert( outputFormat == OutCode );
-       std::streambuf *sbuf = out.rdbuf();
-       output_filter *filter = static_cast<output_filter*>(sbuf);
-       lineDirective( out, filter->fileName, filter->line + 1 );
-}
-
index 7b06d14..02918df 100644 (file)
@@ -118,20 +118,10 @@ ostream &error()
  */
 
 /* Invoked by the parser when the root element is opened. */
-ostream *openOutput( char *inputFile, char *language )
+ostream *openOutput( char *inputFile )
 {
-       if ( strcmp( language, "C" ) == 0 ) {
-               hostLangType = CCode;
-               hostLang = &hostLangC;
-       }
-       else if ( strcmp( language, "D" ) == 0 ) {
-               hostLangType = DCode;
-               hostLang = &hostLangD;
-       }
-       else {
-               error() << PROGNAME " generates code for C and D only, you "
-                       "need a diferent code generator for " << language << endl;
-       }
+       if ( hostLangType != CCode && hostLangType != DCode )
+               error() << PROGNAME " generates code for C and D only" << endl;
 
        /* If the output format is code and no output file name is given, then
         * make a default. */
index 996ac38..2446f8f 100644 (file)
@@ -36,6 +36,19 @@ using std::string;
 using std::cerr;
 using std::endl;
 
+void lineDirective( ostream &out, char *fileName, int line )
+{
+       /* Write the preprocessor line info for to the input file. */
+       out << "// line " << line  << " \"";
+       for ( char *pc = fileName; *pc != 0; pc++ ) {
+               if ( *pc == '\\' )
+                       out << "\\\\";
+               else
+                       out << *pc;
+       }
+       out << "\"\n";
+}
+
 void genLineDirective( ostream &out )
 {
        std::streambuf *sbuf = out.rdbuf();
@@ -43,7 +56,6 @@ void genLineDirective( ostream &out )
        lineDirective( out, filter->fileName, filter->line + 1 );
 }
 
-
 void JavaTabCodeGen::GOTO( ostream &ret, int gotoDest, bool inFinish )
 {
        ret << "{" << CS() << " = " << gotoDest << "; " << 
index caf8405..e15ef06 100644 (file)
@@ -90,9 +90,9 @@ ostream &error()
  */
 
 /* Invoked by the parser when the root element is opened. */
-ostream *openOutput( char *inputFile, char *language )
+ostream *openOutput( char *inputFile )
 {
-       if ( strcmp( language, "Java" ) != 0 )
+       if ( hostLangType != JavaCode ) 
                error() << "this code generator is for Java only" << endl;
 
        /* If the output format is code and no output file name is given, then
index 3f238d5..bb7d15c 100644 (file)
@@ -90,9 +90,9 @@ ostream &error()
  */
 
 /* Invoked by the parser when the root element is opened. */
-ostream *openOutput( char *inputFile, char *language )
+ostream *openOutput( char *inputFile )
 {
-       if ( strcmp( language, "Ruby" ) != 0 )
+       if ( hostLangType != RubyCode ) 
                error() << "this code generator is for Ruby only" << endl;
 
        /* If the output format is code and no output file name is given, then
index b35ecbf..202fa51 100644 (file)
@@ -36,6 +36,21 @@ using std::string;
 using std::cerr;
 using std::endl;
 
+void lineDirective( ostream &out, char *fileName, int line )
+{
+       if ( hostLangType != JavaCode ) {
+               /* Write a comment containing line info. */
+               out << "# line " << line  << " \"";
+               for ( char *pc = fileName; *pc != 0; pc++ ) {
+                       if ( *pc == '\\' )
+                               out << "\\\\";
+                       else
+                               out << *pc;
+               }
+               out << "\"\n";
+       }
+}
+
 void genLineDirective( ostream &out )
 {
        std::streambuf *sbuf = out.rdbuf();