From: thurston Date: Sun, 4 Feb 2007 23:54:47 +0000 (+0000) Subject: The conversion from the string language type which comes in from the XML file X-Git-Tag: 2.0_alpha~409 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=13e1fa3ebeb1c7e8d137ac5862c00c724bd6e3c2;p=external%2Fragel.git The conversion from the string language type which comes in from the XML file 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 --- diff --git a/redfsm/gendata.cpp b/redfsm/gendata.cpp index e8a6ca6..475af26 100644 --- a/redfsm/gendata.cpp +++ b/redfsm/gendata.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2005-2006 Adrian Thurston + * Copyright 2005-2007 Adrian Thurston */ /* 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"; - } -} - diff --git a/redfsm/gendata.h b/redfsm/gendata.h index e0ee6df..a195dab 100644 --- a/redfsm/gendata.h +++ b/redfsm/gendata.h @@ -1,5 +1,5 @@ /* - * Copyright 2005-2006 Adrian Thurston + * Copyright 2005-2007 Adrian Thurston */ /* This file is part of Ragel. @@ -39,8 +39,27 @@ struct CodeGenData; typedef AvlMap CodeGenMap; typedef AvlMapEl 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 */ diff --git a/redfsm/xmlparse.kl b/redfsm/xmlparse.kl index 0d90418..15b4a11 100644 --- a/redfsm/xmlparse.kl +++ b/redfsm/xmlparse.kl @@ -64,7 +64,28 @@ tag_ragel_head: TAG_ragel if ( langAttr == 0 ) error($1->loc) << "tag 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 ); } } }; diff --git a/rlcodegen/fsmcodegen.cpp b/rlcodegen/fsmcodegen.cpp index 888f534..42314bb 100644 --- a/rlcodegen/fsmcodegen.cpp +++ b/rlcodegen/fsmcodegen.cpp @@ -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(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(sbuf); - lineDirective( out, filter->fileName, filter->line + 1 ); -} - diff --git a/rlcodegen/main.cpp b/rlcodegen/main.cpp index 7b06d14..02918df 100644 --- a/rlcodegen/main.cpp +++ b/rlcodegen/main.cpp @@ -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. */ diff --git a/rlgen-java/javacodegen.cpp b/rlgen-java/javacodegen.cpp index 996ac38..2446f8f 100644 --- a/rlgen-java/javacodegen.cpp +++ b/rlgen-java/javacodegen.cpp @@ -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 << "; " << diff --git a/rlgen-java/main.cpp b/rlgen-java/main.cpp index caf8405..e15ef06 100644 --- a/rlgen-java/main.cpp +++ b/rlgen-java/main.cpp @@ -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 diff --git a/rlgen-ruby/main.cpp b/rlgen-ruby/main.cpp index 3f238d5..bb7d15c 100644 --- a/rlgen-ruby/main.cpp +++ b/rlgen-ruby/main.cpp @@ -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 diff --git a/rlgen-ruby/rubycodegen.cpp b/rlgen-ruby/rubycodegen.cpp index b35ecbf..202fa51 100644 --- a/rlgen-ruby/rubycodegen.cpp +++ b/rlgen-ruby/rubycodegen.cpp @@ -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();