2 * Copyright 2001-2007 Adrian Thurston <thurston@complang.org>
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
30 #include <sys/types.h>
43 #define S_IRUSR _S_IREAD
44 #define S_IWUSR _S_IWRITE
52 /* Parameters and output. */
57 #include "inputdata.h"
68 using std::streamsize;
70 /* Controls minimization. */
71 MinimizeLevel minimizeLevel = MinimizePartition2;
72 MinimizeOpt minimizeOpt = MinimizeMostOps;
74 /* Graphviz dot file generation. */
75 const char *machineSpec = 0, *machineName = 0;
76 bool machineSpecFound = false;
77 bool wantDupsRemoved = true;
79 bool printStatistics = false;
80 bool generateXML = false;
81 bool generateDot = false;
83 /* Target language and output style. */
84 CodeStyle codeStyle = GenTables;
86 int numSplitPartitions = 0;
87 bool noLineDirectives = false;
89 bool displayPrintables = false;
91 /* Target ruby impl */
92 RubyImplEnum rubyImpl = MRI;
94 /* Print a summary of the options. */
98 "usage: ragel [options] file\n"
100 " -h, -H, -?, --help Print this usage and exit\n"
101 " -v, --version Print version information and exit\n"
102 " -o <file> Write output to <file>\n"
103 " -s Print some statistics on stderr\n"
104 " -d Do not remove duplicates from action lists\n"
105 " -I <dir> Add <dir> to the list of directories to search\n"
106 " for included an imported files\n"
107 "error reporting format:\n"
108 " --error-format=gnu file:line:column: message (default)\n"
109 " --error-format=msvc file(line,column): message\n"
110 "fsm minimization:\n"
111 " -n Do not perform minimization\n"
112 " -m Minimize at the end of the compilation\n"
113 " -l Minimize after most operations (default)\n"
114 " -e Minimize after every operation\n"
116 " -x Run the frontend only: emit XML intermediate format\n"
117 " -V Generate a dot file for Graphviz\n"
118 " -p Display printable characters on labels\n"
119 " -S <spec> FSM specification to output (for graphviz output)\n"
120 " -M <machine> Machine definition/instantiation to output (for graphviz output)\n"
122 " -C The host language is C, C++, Obj-C or Obj-C++ (default)\n"
123 " -D The host language is D\n"
124 " -J The host language is Java\n"
125 " -R The host language is Ruby\n"
126 " -A The host language is C#\n"
127 "line direcives: (C/D/C#)\n"
128 " -L Inhibit writing of #line directives\n"
129 "code style: (C/D/Java/Ruby/C#)\n"
130 " -T0 Table driven FSM (default)\n"
131 "code style: (C/D/Ruby/C#)\n"
132 " -T1 Faster table driven FSM\n"
133 " -F0 Flat table driven FSM\n"
134 " -F1 Faster flat table-driven FSM\n"
135 "code style: (C/D/C#)\n"
136 " -G0 Goto-driven FSM\n"
137 " -G1 Faster goto-driven FSM\n"
138 "code style: (C/D)\n"
139 " -G2 Really fast goto-driven FSM\n"
140 " -P<N> N-Way Split really fast goto-driven FSM\n"
146 /* Print version information and exit. */
149 cout << "Ragel State Machine Compiler version " VERSION << " " PUBDATE << endl <<
150 "Copyright (c) 2001-2009 by Adrian Thurston" << endl;
154 /* Error reporting format. */
155 ErrorFormat errorFormat = ErrorFormatGNU;
157 InputLoc makeInputLoc( const char *fileName, int line, int col)
159 InputLoc loc = { fileName, line, col };
163 ostream &operator<<( ostream &out, const InputLoc &loc )
165 assert( loc.fileName != 0 );
166 switch ( errorFormat ) {
167 case ErrorFormatMSVC:
168 out << loc.fileName << "(" << loc.line;
170 out << "," << loc.col;
175 out << loc.fileName << ":" << loc.line;
177 out << ":" << loc.col;
183 /* Total error count. */
184 int gblErrorCount = 0;
186 /* Print the opening to a warning in the input, then return the error ostream. */
187 ostream &warning( const InputLoc &loc )
189 cerr << loc << ": warning: ";
193 /* Print the opening to a program error, then return the error stream. */
197 cerr << PROGNAME ": ";
201 ostream &error( const InputLoc &loc )
208 void escapeLineDirectivePath( std::ostream &out, char *path )
210 for ( char *pc = path; *pc != 0; pc++ ) {
218 void processArgs( int argc, const char **argv, InputData &id )
220 ParamCheck pc("xo:dnmleabjkS:M:I:CDJRAvHh?-:sT:F:G:P:LpV", argc, argv);
222 /* FIXME: Need to check code styles VS langauge. */
224 while ( pc.check() ) {
225 switch ( pc.state ) {
226 case ParamCheck::match:
227 switch ( pc.parameter ) {
238 if ( *pc.paramArg == 0 )
239 error() << "a zero length output file name was given" << endl;
240 else if ( id.outputFileName != 0 )
241 error() << "more than one output file name was given" << endl;
243 /* Ok, remember the output file name. */
244 id.outputFileName = pc.paramArg;
248 /* Flag for turning off duplicate action removal. */
250 wantDupsRemoved = false;
253 /* Minimization, mostly hidden options. */
255 minimizeOpt = MinimizeNone;
258 minimizeOpt = MinimizeEnd;
261 minimizeOpt = MinimizeMostOps;
264 minimizeOpt = MinimizeEveryOp;
267 minimizeLevel = MinimizeApprox;
270 minimizeLevel = MinimizeStable;
273 minimizeLevel = MinimizePartition1;
276 minimizeLevel = MinimizePartition2;
281 if ( *pc.paramArg == 0 )
282 error() << "please specify an argument to -S" << endl;
283 else if ( machineSpec != 0 )
284 error() << "more than one -S argument was given" << endl;
286 /* Ok, remember the path to the machine to generate. */
287 machineSpec = pc.paramArg;
293 if ( *pc.paramArg == 0 )
294 error() << "please specify an argument to -M" << endl;
295 else if ( machineName != 0 )
296 error() << "more than one -M argument was given" << endl;
298 /* Ok, remember the machine name to generate. */
299 machineName = pc.paramArg;
304 if ( *pc.paramArg == 0 )
305 error() << "please specify an argument to -I" << endl;
307 id.includePaths.append( pc.paramArg );
311 /* Host language types. */
313 hostLang = &hostLangC;
316 hostLang = &hostLangD;
319 hostLang = &hostLangJava;
322 hostLang = &hostLangRuby;
325 hostLang = &hostLangCSharp;
328 /* Version and help. */
332 case 'H': case 'h': case '?':
336 printStatistics = true;
339 char *eq = strchr( pc.paramArg, '=' );
344 if ( strcmp( pc.paramArg, "help" ) == 0 )
346 else if ( strcmp( pc.paramArg, "version" ) == 0 )
348 else if ( strcmp( pc.paramArg, "error-format" ) == 0 ) {
350 error() << "expecting '=value' for error-format" << endl;
351 else if ( strcmp( eq, "gnu" ) == 0 )
352 errorFormat = ErrorFormatGNU;
353 else if ( strcmp( eq, "msvc" ) == 0 )
354 errorFormat = ErrorFormatMSVC;
356 error() << "invalid value for error-format" << endl;
358 else if ( strcmp( pc.paramArg, "rbx" ) == 0 )
361 error() << "--" << pc.paramArg <<
362 " is an invalid argument" << endl;
367 /* Passthrough args. */
369 if ( pc.paramArg[0] == '0' )
370 codeStyle = GenTables;
371 else if ( pc.paramArg[0] == '1' )
372 codeStyle = GenFTables;
374 error() << "-T" << pc.paramArg[0] <<
375 " is an invalid argument" << endl;
380 if ( pc.paramArg[0] == '0' )
382 else if ( pc.paramArg[0] == '1' )
383 codeStyle = GenFFlat;
385 error() << "-F" << pc.paramArg[0] <<
386 " is an invalid argument" << endl;
391 if ( pc.paramArg[0] == '0' )
393 else if ( pc.paramArg[0] == '1' )
394 codeStyle = GenFGoto;
395 else if ( pc.paramArg[0] == '2' )
396 codeStyle = GenIpGoto;
398 error() << "-G" << pc.paramArg[0] <<
399 " is an invalid argument" << endl;
404 codeStyle = GenSplit;
405 numSplitPartitions = atoi( pc.paramArg );
409 displayPrintables = true;
413 noLineDirectives = true;
418 case ParamCheck::invalid:
419 error() << "-" << pc.parameter << " is an invalid argument" << endl;
422 case ParamCheck::noparam:
423 /* It is interpreted as an input file. */
424 if ( *pc.curArg == 0 )
425 error() << "a zero length input file name was given" << endl;
426 else if ( id.inputFileName != 0 )
427 error() << "more than one input file name was given" << endl;
429 /* OK, Remember the filename. */
430 id.inputFileName = pc.curArg;
437 void process( InputData &id )
439 /* Open the input file for reading. */
440 assert( id.inputFileName != 0 );
441 ifstream *inFile = new ifstream( id.inputFileName );
442 if ( ! inFile->is_open() )
443 error() << "could not open " << id.inputFileName << " for reading" << endp;
445 /* Used for just a few things. */
446 std::ostringstream hostData;
448 /* Make the first input item. */
449 InputItem *firstInputItem = new InputItem;
450 firstInputItem->type = InputItem::HostData;
451 firstInputItem->loc.line = 1;
452 firstInputItem->loc.col = 1;
453 id.inputItems.append( firstInputItem );
455 Scanner scanner( id, id.inputFileName, *inFile, 0, 0, 0, false );
458 /* Finished, final check for errors.. */
459 if ( gblErrorCount > 0 )
462 /* Now send EOF to all parsers. */
463 id.terminateAllParsers();
465 /* Bail on above error. */
466 if ( gblErrorCount > 0 )
469 /* Locate the backend program */
470 /* Compiles machines. */
471 id.prepareMachineGen();
473 if ( gblErrorCount > 0 )
476 id.makeOutputStream();
478 /* Generates the reduced machine, which we use to write output. */
479 if ( !generateXML ) {
480 id.generateReduced();
482 if ( gblErrorCount > 0 )
487 * From this point on we should not be reporting any errors.
493 /* Close the input and the intermediate file. */
496 /* If writing to a file, delete the ostream, causing it to flush.
497 * Standard out is flushed automatically. */
498 if ( id.outputFileName != 0 ) {
503 assert( gblErrorCount == 0 );
506 char *makeIntermedTemplate( const char *baseFileName )
509 const char *templ = "ragel-XXXXXX.xml";
510 const char *lastSlash = strrchr( baseFileName, '/' );
511 if ( lastSlash == 0 ) {
512 result = new char[strlen(templ)+1];
513 strcpy( result, templ );
516 int baseLen = lastSlash - baseFileName + 1;
517 result = new char[baseLen + strlen(templ) + 1];
518 memcpy( result, baseFileName, baseLen );
519 strcpy( result+baseLen, templ );
524 /* Main, process args and call yyparse to start scanning input. */
525 int main( int argc, const char **argv )
529 processArgs( argc, argv, id );
531 /* Require an input file. If we use standard in then we won't have a file
532 * name on which to base the output. */
533 if ( id.inputFileName == 0 )
534 error() << "no input file given" << endl;
536 /* Bail on argument processing errors. */
537 if ( gblErrorCount > 0 )
540 /* Make sure we are not writing to the same file as the input file. */
541 if ( id.inputFileName != 0 && id.outputFileName != 0 &&
542 strcmp( id.inputFileName, id.outputFileName ) == 0 )
544 error() << "output file \"" << id.outputFileName <<
545 "\" is the same as the input file" << endp;