2 * Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
5 /* This file is part of Ragel.
7 * Ragel is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * Ragel is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Ragel; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 /* Code generators. */
37 #include "tabcodegen.h"
38 #include "ftabcodegen.h"
39 #include "flatcodegen.h"
40 #include "fflatcodegen.h"
41 #include "gotocodegen.h"
42 #include "fgotocodegen.h"
43 #include "ipgotocodegen.h"
44 #include "splitcodegen.h"
55 /* Target language and output style. */
56 extern CodeStyleEnum codeStyle;
59 istream *inStream = 0;
60 ostream *outStream = 0;
61 output_filter *outFilter = 0;
62 extern const char *outputFileName;
64 /* Graphviz dot file generation. */
65 bool graphvizDone = false;
67 extern int numSplitPartitions;
68 extern bool noLineDirectives;
70 /* Print a summary of the options. */
74 "usage: " PROGNAME " [options] file\n"
76 " -h, -H, -?, --help Print this usage and exit\n"
77 " -v, --version Print version information and exit\n"
78 " -o <file> Write output to <file>\n"
79 "code generation options:\n"
80 " -L Inhibit writing of #line directives\n"
81 "generated code style:\n"
82 " -T0 Table driven FSM (default)\n"
83 " -T1 Faster table driven FSM\n"
84 " -F0 Flat table driven FSM\n"
85 " -F1 Faster flat table-driven FSM\n"
86 " -G0 Goto-driven FSM\n"
87 " -G1 Faster goto-driven FSM\n"
88 " -G2 Really fast goto-driven FSM\n"
89 " -P<N> N-Way Split really fast goto-driven FSM\n"
93 /* Print version information. */
96 cout << "Ragel Code Generator for C, C++, Objective-C and D" << endl <<
97 "Version " VERSION << ", " PUBDATE << endl <<
98 "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
104 cerr << PROGNAME ": ";
109 * Callbacks invoked by the XML data parser.
112 /* Invoked by the parser when the root element is opened. */
113 ostream *cdOpenOutput( char *inputFile )
115 if ( hostLang->lang != HostLang::C && hostLang->lang != HostLang::D ) {
116 cd_error() << "this code generator is for C and D only" << endl;
120 /* If the output format is code and no output file name is given, then
122 if ( outputFileName == 0 ) {
123 char *ext = findFileExtension( inputFile );
124 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
125 outputFileName = fileNameFromStem( inputFile, ".h" );
127 const char *defExtension = 0;
128 switch ( hostLang->lang ) {
129 case HostLang::C: defExtension = ".c"; break;
130 case HostLang::D: defExtension = ".d"; break;
133 outputFileName = fileNameFromStem( inputFile, defExtension );
137 /* Make sure we are not writing to the same file as the input file. */
138 if ( outputFileName != 0 && strcmp( inputFile, outputFileName ) == 0 ) {
139 cd_error() << "output file \"" << outputFileName <<
140 "\" is the same as the input file" << endl;
143 if ( outputFileName != 0 ) {
144 /* Create the filter on the output and open it. */
145 outFilter = new output_filter( outputFileName );
146 outFilter->open( outputFileName, ios::out|ios::trunc );
147 if ( !outFilter->is_open() ) {
148 cd_error() << "error opening " << outputFileName << " for writing" << endl;
152 /* Open the output stream, attaching it to the filter. */
153 outStream = new ostream( outFilter );
156 /* Writing out ot std out. */
162 /* Invoked by the parser when a ragel definition is opened. */
163 CodeGenData *cdMakeCodeGen( char *sourceFileName, char *fsmName,
164 ostream &out, bool wantComplete )
166 CodeGenData *codeGen = 0;
167 switch ( hostLang->lang ) {
169 switch ( codeStyle ) {
171 codeGen = new CTabCodeGen(out);
174 codeGen = new CFTabCodeGen(out);
177 codeGen = new CFlatCodeGen(out);
180 codeGen = new CFFlatCodeGen(out);
183 codeGen = new CGotoCodeGen(out);
186 codeGen = new CFGotoCodeGen(out);
189 codeGen = new CIpGotoCodeGen(out);
192 codeGen = new CSplitCodeGen(out);
198 switch ( codeStyle ) {
200 codeGen = new DTabCodeGen(out);
203 codeGen = new DFTabCodeGen(out);
206 codeGen = new DFlatCodeGen(out);
209 codeGen = new DFFlatCodeGen(out);
212 codeGen = new DGotoCodeGen(out);
215 codeGen = new DFGotoCodeGen(out);
218 codeGen = new DIpGotoCodeGen(out);
221 codeGen = new DSplitCodeGen(out);
229 codeGen->sourceFileName = sourceFileName;
230 codeGen->fsmName = fsmName;
231 codeGen->wantComplete = wantComplete;
236 /* Main, process args and call yyparse to start scanning input. */
237 int cd_main( const char *xmlInputFileName )
239 /* Open the input file for reading. */
240 ifstream *inFile = new ifstream( xmlInputFileName );
242 if ( ! inFile->is_open() )
243 cd_error() << "could not open " << xmlInputFileName << " for reading" << endl;
245 /* Bail on above errors. */
246 if ( gblErrorCount > 0 )
249 bool wantComplete = true;
250 bool outputActive = true;
252 /* Parse the input! */
253 xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete );
255 /* If writing to a file, delete the ostream, causing it to flush.
256 * Standard out is flushed automatically. */
257 if ( outputFileName != 0 ) {
262 /* Finished, final check for errors.. */
263 if ( gblErrorCount > 0 ) {
264 /* If we opened an output file, remove it. */
265 if ( outputFileName != 0 )
266 unlink( outputFileName );