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;
68 /* Print a summary of the options. */
72 "usage: " PROGNAME " [options] file\n"
74 " -h, -H, -?, --help Print this usage and exit\n"
75 " -v, --version Print version information and exit\n"
76 " -o <file> Write output to <file>\n"
77 " --rbx Allow to use Rubinius asm features\n"
78 "generated code style:\n"
79 " -T0 Table driven FSM (default)\n"
80 " -T1 Faster table driven FSM\n"
81 " -F0 Flat table driven FSM\n"
82 " -F1 Faster flat table-driven FSM\n"
83 " -G0 Goto-driven FSM\n"
87 /* Print version information. */
90 cout << "Ragel Code Generator for Ruby" << endl <<
91 "Version " VERSION << ", " PUBDATE << endl <<
92 "Copyright (c) 2001-2007 by Adrian Thurston" << endl <<
93 "Copyright (c) 2007 by Victor Hugo Borja" << endl;
99 cerr << PROGNAME ": ";
104 * Callbacks invoked by the XML data parser.
107 /* Invoked by the parser when the root element is opened. */
108 ostream *rubyOpenOutput( char *inputFile )
110 if ( hostLang->lang != HostLang::Ruby ) {
111 ruby_error() << "this code generator is for Ruby only" << endl;
115 /* If the output format is code and no output file name is given, then
117 if ( outputFileName == 0 ) {
118 char *ext = findFileExtension( inputFile );
119 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
120 outputFileName = fileNameFromStem( inputFile, ".h" );
122 outputFileName = fileNameFromStem( inputFile, ".rb" );
125 /* Make sure we are not writing to the same file as the input file. */
126 if ( outputFileName != 0 && strcmp( inputFile, outputFileName ) == 0 ) {
127 ruby_error() << "output file \"" << outputFileName <<
128 "\" is the same as the input file" << endl;
131 if ( outputFileName != 0 ) {
132 /* Create the filter on the output and open it. */
133 outFilter = new output_filter( outputFileName );
134 outFilter->open( outputFileName, ios::out|ios::trunc );
135 if ( !outFilter->is_open() ) {
136 ruby_error() << "error opening " << outputFileName << " for writing" << endl;
140 /* Open the output stream, attaching it to the filter. */
141 outStream = new ostream( outFilter );
144 /* Writing out ot std out. */
150 /* Invoked by the parser when a ragel definition is opened. */
151 CodeGenData *rubyMakeCodeGen( char *sourceFileName, char *fsmName,
152 ostream &out, bool wantComplete )
154 CodeGenData *codeGen = 0;
155 switch ( codeStyle ) {
157 codeGen = new RubyTabCodeGen(out);
160 codeGen = new RubyFTabCodeGen(out);
163 codeGen = new RubyFlatCodeGen(out);
166 codeGen = new RubyFFlatCodeGen(out);
169 if ( rubyImpl == Rubinius ) {
170 codeGen = new RbxGotoCodeGen(out);
172 cout << "Goto style is still _very_ experimental "
173 "and only supported using Rubinius.\n"
174 "You may want to enable the --rbx flag "
175 " to give it a try.\n";
180 cout << "Invalid code style\n";
184 codeGen->sourceFileName = sourceFileName;
185 codeGen->fsmName = fsmName;
186 codeGen->wantComplete = wantComplete;
191 /* Main, process args and call yyparse to start scanning input. */
192 int ruby_main( const char *xmlInputFileName )
194 /* Open the input file for reading. */
195 ifstream *inFile = new ifstream( xmlInputFileName );
197 if ( ! inFile->is_open() )
198 ruby_error() << "could not open " << xmlInputFileName << " for reading" << endl;
200 /* Bail on above errors. */
201 if ( gblErrorCount > 0 )
204 bool wantComplete = true;
205 bool outputActive = true;
207 /* Parse the input! */
208 xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete );
210 /* If writing to a file, delete the ostream, causing it to flush.
211 * Standard out is flushed automatically. */
212 if ( outputFileName != 0 ) {
217 /* Finished, final check for errors.. */
218 if ( gblErrorCount > 0 ) {
219 /* If we opened an output file, remove it. */
220 if ( outputFileName != 0 )
221 unlink( outputFileName );
230 * indent-tabs-mode: 1
231 * c-file-style: "bsd"