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 CodeStyleEnum codeStyle = GenTables;
59 istream *inStream = 0;
60 ostream *outStream = 0;
61 output_filter *outFilter = 0;
62 char *outputFileName = 0;
64 /* Graphviz dot file generation. */
65 bool graphvizDone = false;
67 int numSplitPartitions = 0;
68 bool noLineDirectives = false;
69 bool printPrintables = false;
71 /* Print a summary of the options. */
75 "usage: " PROGNAME " [options] file\n"
77 " -h, -H, -?, --help Print this usage and exit\n"
78 " -v, --version Print version information and exit\n"
79 " -o <file> Write output to <file>\n"
80 "code generation options:\n"
81 " -l Inhibit writing of #line directives\n"
82 "generated code style:\n"
83 " -T0 Table driven FSM (default)\n"
84 " -T1 Faster table driven FSM\n"
85 " -F0 Flat table driven FSM\n"
86 " -F1 Faster flat table-driven FSM\n"
87 " -G0 Goto-driven FSM\n"
88 " -G1 Faster goto-driven FSM\n"
89 " -G2 Really fast goto-driven FSM\n"
90 " -P<N> N-Way Split really fast goto-driven FSM\n"
94 /* Print version information. */
97 cout << "Ragel Code Generator for C, C++, Objective-C and D" << endl <<
98 "Version " VERSION << ", " PUBDATE << endl <<
99 "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
102 /* Total error count. */
103 int gblErrorCount = 0;
108 cerr << PROGNAME ": ";
113 * Callbacks invoked by the XML data parser.
116 /* Invoked by the parser when the root element is opened. */
117 ostream *openOutput( char *inputFile )
119 if ( hostLangType != CCode && hostLangType != DCode ) {
120 error() << "this code generator is for C and D only" << endl;
124 /* If the output format is code and no output file name is given, then
126 if ( outputFileName == 0 ) {
127 char *ext = findFileExtension( inputFile );
128 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
129 outputFileName = fileNameFromStem( inputFile, ".h" );
131 char *defExtension = 0;
132 switch ( hostLangType ) {
133 case CCode: defExtension = ".c"; break;
134 case DCode: defExtension = ".d"; break;
137 outputFileName = fileNameFromStem( inputFile, defExtension );
141 /* Make sure we are not writing to the same file as the input file. */
142 if ( outputFileName != 0 && strcmp( inputFile, outputFileName ) == 0 ) {
143 error() << "output file \"" << outputFileName <<
144 "\" is the same as the input file" << endl;
147 if ( outputFileName != 0 ) {
148 /* Create the filter on the output and open it. */
149 outFilter = new output_filter( outputFileName );
150 outFilter->open( outputFileName, ios::out|ios::trunc );
151 if ( !outFilter->is_open() ) {
152 error() << "error opening " << outputFileName << " for writing" << endl;
156 /* Open the output stream, attaching it to the filter. */
157 outStream = new ostream( outFilter );
160 /* Writing out ot std out. */
166 /* Invoked by the parser when a ragel definition is opened. */
167 CodeGenData *makeCodeGen( char *sourceFileName, char *fsmName,
168 ostream &out, bool wantComplete )
170 CodeGenData *codeGen = 0;
171 switch ( hostLangType ) {
173 switch ( codeStyle ) {
175 codeGen = new CTabCodeGen(out);
178 codeGen = new CFTabCodeGen(out);
181 codeGen = new CFlatCodeGen(out);
184 codeGen = new CFFlatCodeGen(out);
187 codeGen = new CGotoCodeGen(out);
190 codeGen = new CFGotoCodeGen(out);
193 codeGen = new CIpGotoCodeGen(out);
196 codeGen = new CSplitCodeGen(out);
202 switch ( codeStyle ) {
204 codeGen = new DTabCodeGen(out);
207 codeGen = new DFTabCodeGen(out);
210 codeGen = new DFlatCodeGen(out);
213 codeGen = new DFFlatCodeGen(out);
216 codeGen = new DGotoCodeGen(out);
219 codeGen = new DFGotoCodeGen(out);
222 codeGen = new DIpGotoCodeGen(out);
225 codeGen = new DSplitCodeGen(out);
233 codeGen->sourceFileName = sourceFileName;
234 codeGen->fsmName = fsmName;
235 codeGen->wantComplete = wantComplete;
242 /* Main, process args and call yyparse to start scanning input. */
243 int main(int argc, char **argv)
245 ParamCheck pc("-:Hh?vlo:T:F:G:P:", argc, argv);
246 char *xmlInputFileName = 0;
248 while ( pc.check() ) {
249 switch ( pc.state ) {
250 case ParamCheck::match:
251 switch ( pc.parameter ) {
254 if ( *pc.parameterArg == 0 )
255 error() << "a zero length output file name was given" << endl;
256 else if ( outputFileName != 0 )
257 error() << "more than one output file name was given" << endl;
259 /* Ok, remember the output file name. */
260 outputFileName = pc.parameterArg;
265 noLineDirectives = true;
270 if ( pc.parameterArg[0] == '0' )
271 codeStyle = GenTables;
272 else if ( pc.parameterArg[0] == '1' )
273 codeStyle = GenFTables;
275 error() << "-T" << pc.parameterArg[0] <<
276 " is an invalid argument" << endl;
281 if ( pc.parameterArg[0] == '0' )
283 else if ( pc.parameterArg[0] == '1' )
284 codeStyle = GenFFlat;
286 error() << "-F" << pc.parameterArg[0] <<
287 " is an invalid argument" << endl;
292 if ( pc.parameterArg[0] == '0' )
294 else if ( pc.parameterArg[0] == '1' )
295 codeStyle = GenFGoto;
296 else if ( pc.parameterArg[0] == '2' )
297 codeStyle = GenIpGoto;
299 error() << "-G" << pc.parameterArg[0] <<
300 " is an invalid argument" << endl;
305 codeStyle = GenSplit;
306 numSplitPartitions = atoi( pc.parameterArg );
309 /* Version and help. */
313 case 'H': case 'h': case '?':
317 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
321 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
326 error() << "--" << pc.parameterArg <<
327 " is an invalid argument" << endl;
333 case ParamCheck::invalid:
334 error() << "-" << pc.parameter << " is an invalid argument" << endl;
337 case ParamCheck::noparam:
338 if ( *pc.curArg == 0 )
339 error() << "a zero length input file name was given" << endl;
340 else if ( xmlInputFileName != 0 )
341 error() << "more than one input file name was given" << endl;
343 /* OK, Remember the filename. */
344 xmlInputFileName = pc.curArg;
350 /* Bail on above errors. */
351 if ( gblErrorCount > 0 )
354 /* Open the input file for reading. */
355 if ( xmlInputFileName != 0 ) {
356 /* Open the input file for reading. */
357 ifstream *inFile = new ifstream( xmlInputFileName );
359 if ( ! inFile->is_open() )
360 error() << "could not open " << xmlInputFileName << " for reading" << endl;
363 xmlInputFileName = "<stdin>";
367 /* Bail on above errors. */
368 if ( gblErrorCount > 0 )
371 bool wantComplete = true;
372 bool outputActive = true;
374 /* Parse the input! */
375 xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete );
377 /* If writing to a file, delete the ostream, causing it to flush.
378 * Standard out is flushed automatically. */
379 if ( outputFileName != 0 ) {
384 /* Finished, final check for errors.. */
385 if ( gblErrorCount > 0 ) {
386 /* If we opened an output file, remove it. */
387 if ( outputFileName != 0 )
388 unlink( outputFileName );