2 * Copyright 2007 Victor Hugo Borja <vic@rubyforge.org>
3 * Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
6 /* This file is part of Ragel.
8 * Ragel is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * Ragel is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Ragel; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include "rlgen-ruby.h"
36 #include "ruby-tabcodegen.h"
37 #include "ruby-ftabcodegen.h"
38 #include "ruby-flatcodegen.h"
39 #include "ruby-fflatcodegen.h"
40 #include "rbx-gotocodegen.h"
51 /* Target ruby impl */
52 extern RubyImplEnum rubyImpl;
54 /* Target language and output style. */
55 extern CodeStyleEnum codeStyle;
58 extern istream *inStream;
59 extern ostream *outStream;
60 extern output_filter *outFilter;
61 extern const char *outputFileName;
63 /* Graphviz dot file generation. */
64 extern bool graphvizDone;
66 extern int numSplitPartitions;
69 * Callbacks invoked by the XML data parser.
72 /* Invoked by the parser when the root element is opened. */
73 ostream *rubyOpenOutput( char *inputFile )
75 if ( hostLang->lang != HostLang::Ruby ) {
76 error() << "this code generator is for Ruby only" << endl;
80 /* If the output format is code and no output file name is given, then
82 if ( outputFileName == 0 ) {
83 char *ext = findFileExtension( inputFile );
84 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
85 outputFileName = fileNameFromStem( inputFile, ".h" );
87 outputFileName = fileNameFromStem( inputFile, ".rb" );
90 /* Make sure we are not writing to the same file as the input file. */
91 if ( outputFileName != 0 && strcmp( inputFile, outputFileName ) == 0 ) {
92 error() << "output file \"" << outputFileName <<
93 "\" is the same as the input file" << endl;
96 if ( outputFileName != 0 ) {
97 /* Create the filter on the output and open it. */
98 outFilter = new output_filter( outputFileName );
99 outFilter->open( outputFileName, ios::out|ios::trunc );
100 if ( !outFilter->is_open() ) {
101 error() << "error opening " << outputFileName << " for writing" << endl;
105 /* Open the output stream, attaching it to the filter. */
106 outStream = new ostream( outFilter );
109 /* Writing out ot std out. */
115 /* Invoked by the parser when a ragel definition is opened. */
116 CodeGenData *rubyMakeCodeGen( char *sourceFileName, char *fsmName,
117 ostream &out, bool wantComplete )
119 CodeGenData *codeGen = 0;
120 switch ( codeStyle ) {
122 codeGen = new RubyTabCodeGen(out);
125 codeGen = new RubyFTabCodeGen(out);
128 codeGen = new RubyFlatCodeGen(out);
131 codeGen = new RubyFFlatCodeGen(out);
134 if ( rubyImpl == Rubinius ) {
135 codeGen = new RbxGotoCodeGen(out);
137 cout << "Goto style is still _very_ experimental "
138 "and only supported using Rubinius.\n"
139 "You may want to enable the --rbx flag "
140 " to give it a try.\n";
145 cout << "Invalid code style\n";
149 codeGen->sourceFileName = sourceFileName;
150 codeGen->fsmName = fsmName;
151 codeGen->wantComplete = wantComplete;
159 * indent-tabs-mode: 1
160 * c-file-style: "bsd"