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
30 #include <sys/types.h>
43 #define S_IRUSR _S_IREAD
44 #define S_IWUSR _S_IWRITE
52 /* Parameters and output. */
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 frontendOnly = false;
81 bool generateDot = false;
83 /* Target language and output style. */
84 CodeStyleEnum codeStyle = GenTables;
86 int numSplitPartitions = 0;
87 bool noLineDirectives = false;
89 bool displayPrintables = false;
90 bool graphvizDone = false;
92 /* Target ruby impl */
93 RubyImplEnum rubyImpl = MRI;
95 ArgsVector includePaths;
97 istream *inStream = 0;
98 ostream *outStream = 0;
99 output_filter *outFilter = 0;
100 const char *outputFileName = 0;
102 /* Print a summary of the options. */
106 "usage: ragel [options] file\n"
108 " -h, -H, -?, --help Print this usage and exit\n"
109 " -v, --version Print version information and exit\n"
110 " -o <file> Write output to <file>\n"
111 " -s Print some statistics on stderr\n"
112 " -d Do not remove duplicates from action lists\n"
113 " -I <dir> Add <dir> to the list of directories to search\n"
114 " for included an imported files\n"
115 "error reporting format:\n"
116 " --error-format=gnu file:line:column: message (default)\n"
117 " --error-format=msvc file(line,column): message\n"
118 "fsm minimization:\n"
119 " -n Do not perform minimization\n"
120 " -m Minimize at the end of the compilation\n"
121 " -l Minimize after most operations (default)\n"
122 " -e Minimize after every operation\n"
124 " -x Run the frontend only: emit XML intermediate format\n"
125 " -V Generate a dot file for Graphviz\n"
126 " -p Display printable characters on labels\n"
127 " -S <spec> FSM specification to output (for rlgen-dot)\n"
128 " -M <machine> Machine definition/instantiation to output (for rlgen-dot)\n"
130 " -C The host language is C, C++, Obj-C or Obj-C++ (default)\n"
131 " -D The host language is D\n"
132 " -J The host language is Java\n"
133 " -R The host language is Ruby\n"
134 " -A The host language is C#\n"
135 "line direcives: (C/D/C# only)\n"
136 " -L Inhibit writing of #line directives\n"
137 "code style: (C/Ruby/C# only)\n"
138 " -T0 Table driven FSM (default)\n"
139 " -T1 Faster table driven FSM\n"
140 " -F0 Flat table driven FSM\n"
141 " -F1 Faster flat table-driven FSM\n"
142 "code style: (C/C# only)\n"
143 " -G0 Goto-driven FSM\n"
144 " -G1 Faster goto-driven FSM\n"
145 "code style: (C only)\n"
146 " -G2 Really fast goto-driven FSM\n"
147 " -P<N> N-Way Split really fast goto-driven FSM\n"
153 /* Print version information and exit. */
156 cout << "Ragel State Machine Compiler version " VERSION << " " PUBDATE << endl <<
157 "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
161 /* Error reporting format. */
162 ErrorFormat errorFormat = ErrorFormatGNU;
164 InputLoc makeInputLoc( const char *fileName, int line, int col)
166 InputLoc loc = { fileName, line, col };
170 ostream &operator<<( ostream &out, const InputLoc &loc )
172 assert( loc.fileName != 0 );
173 switch ( errorFormat ) {
174 case ErrorFormatMSVC:
175 out << loc.fileName << "(" << loc.line;
177 out << "," << loc.col;
182 out << loc.fileName << ":" << loc.line;
184 out << ":" << loc.col;
190 /* Total error count. */
191 int gblErrorCount = 0;
193 /* Print the opening to a warning in the input, then return the error ostream. */
194 ostream &warning( const InputLoc &loc )
196 cerr << loc << ": warning: ";
200 /* Print the opening to a program error, then return the error stream. */
204 cerr << PROGNAME ": ";
208 ostream &error( const InputLoc &loc )
215 void escapeLineDirectivePath( std::ostream &out, char *path )
217 for ( char *pc = path; *pc != 0; pc++ ) {
225 void processArgs( int argc, const char **argv, const char *&inputFileName )
227 ParamCheck pc("xo:dnmleabjkS:M:I:CDJRAvHh?-:sT:F:G:P:LpV", argc, argv);
229 /* FIXME: Need to check code styles VS langauge. */
231 while ( pc.check() ) {
232 switch ( pc.state ) {
233 case ParamCheck::match:
234 switch ( pc.parameter ) {
245 if ( *pc.paramArg == 0 )
246 error() << "a zero length output file name was given" << endl;
247 else if ( outputFileName != 0 )
248 error() << "more than one output file name was given" << endl;
250 /* Ok, remember the output file name. */
251 outputFileName = pc.paramArg;
255 /* Flag for turning off duplicate action removal. */
257 wantDupsRemoved = false;
260 /* Minimization, mostly hidden options. */
262 minimizeOpt = MinimizeNone;
265 minimizeOpt = MinimizeEnd;
268 minimizeOpt = MinimizeMostOps;
271 minimizeOpt = MinimizeEveryOp;
274 minimizeLevel = MinimizeApprox;
277 minimizeLevel = MinimizeStable;
280 minimizeLevel = MinimizePartition1;
283 minimizeLevel = MinimizePartition2;
288 if ( *pc.paramArg == 0 )
289 error() << "please specify an argument to -S" << endl;
290 else if ( machineSpec != 0 )
291 error() << "more than one -S argument was given" << endl;
293 /* Ok, remember the path to the machine to generate. */
294 machineSpec = pc.paramArg;
300 if ( *pc.paramArg == 0 )
301 error() << "please specify an argument to -M" << endl;
302 else if ( machineName != 0 )
303 error() << "more than one -M argument was given" << endl;
305 /* Ok, remember the machine name to generate. */
306 machineName = pc.paramArg;
311 if ( *pc.paramArg == 0 )
312 error() << "please specify an argument to -I" << endl;
314 includePaths.append( pc.paramArg );
318 /* Host language types. */
320 hostLang = &hostLangC;
323 hostLang = &hostLangD;
326 hostLang = &hostLangJava;
329 hostLang = &hostLangRuby;
332 hostLang = &hostLangCSharp;
335 /* Version and help. */
339 case 'H': case 'h': case '?':
343 printStatistics = true;
346 char *eq = strchr( pc.paramArg, '=' );
351 if ( strcmp( pc.paramArg, "help" ) == 0 )
353 else if ( strcmp( pc.paramArg, "version" ) == 0 )
355 else if ( strcmp( pc.paramArg, "error-format" ) == 0 ) {
357 error() << "expecting '=value' for error-format" << endl;
358 else if ( strcmp( eq, "gnu" ) == 0 )
359 errorFormat = ErrorFormatGNU;
360 else if ( strcmp( eq, "msvc" ) == 0 )
361 errorFormat = ErrorFormatMSVC;
363 error() << "invalid value for error-format" << endl;
365 else if ( strcmp( pc.paramArg, "rbx" ) == 0 )
368 error() << "--" << pc.paramArg <<
369 " is an invalid argument" << endl;
374 /* Passthrough args. */
376 if ( pc.paramArg[0] == '0' )
377 codeStyle = GenTables;
378 else if ( pc.paramArg[0] == '1' )
379 codeStyle = GenFTables;
381 error() << "-T" << pc.paramArg[0] <<
382 " is an invalid argument" << endl;
387 if ( pc.paramArg[0] == '0' )
389 else if ( pc.paramArg[0] == '1' )
390 codeStyle = GenFFlat;
392 error() << "-F" << pc.paramArg[0] <<
393 " is an invalid argument" << endl;
398 if ( pc.paramArg[0] == '0' )
400 else if ( pc.paramArg[0] == '1' )
401 codeStyle = GenFGoto;
402 else if ( pc.paramArg[0] == '2' )
403 codeStyle = GenIpGoto;
405 error() << "-G" << pc.paramArg[0] <<
406 " is an invalid argument" << endl;
411 codeStyle = GenSplit;
412 numSplitPartitions = atoi( pc.paramArg );
416 displayPrintables = true;
420 noLineDirectives = true;
425 case ParamCheck::invalid:
426 error() << "-" << pc.parameter << " is an invalid argument" << endl;
429 case ParamCheck::noparam:
430 /* It is interpreted as an input file. */
431 if ( *pc.curArg == 0 )
432 error() << "a zero length input file name was given" << endl;
433 else if ( inputFileName != 0 )
434 error() << "more than one input file name was given" << endl;
436 /* OK, Remember the filename. */
437 inputFileName = pc.curArg;
444 void process( const char *inputFileName, const char *intermed )
446 /* Open the input file for reading. */
447 assert( inputFileName != 0 );
448 ifstream *inFile = new ifstream( inputFileName );
449 istream *inStream = inFile;
450 if ( ! inFile->is_open() )
451 error() << "could not open " << inputFileName << " for reading" << endp;
453 /* Used for just a few things. */
454 std::ostringstream hostData;
456 if ( machineSpec == 0 && machineName == 0 )
457 hostData << "<host line=\"1\" col=\"1\">";
459 Scanner scanner( inputFileName, *inStream, hostData, 0, 0, 0, false );
462 /* Finished, final check for errors.. */
463 if ( gblErrorCount > 0 )
466 /* Now send EOF to all parsers. */
467 terminateAllParsers();
469 /* Finished, final check for errors.. */
470 if ( gblErrorCount > 0 )
473 if ( machineSpec == 0 && machineName == 0 )
474 hostData << "</host>\n";
476 if ( gblErrorCount > 0 )
479 ostream *outputFile = new ofstream( intermed );
481 /* Write the machines, then the surrounding code. */
482 writeMachines( *outputFile, hostData.str(), inputFileName );
484 /* Close the input and the intermediate file. */
488 /* Bail on above error. */
489 if ( gblErrorCount > 0 )
492 const char *xmlInputFileName = intermed;
494 bool wantComplete = true;
495 bool outputActive = true;
497 /* Open the input file for reading. */
498 inFile = new ifstream( xmlInputFileName );
500 if ( ! inFile->is_open() )
501 error() << "could not open " << xmlInputFileName << " for reading" << endl;
503 /* Bail on above error. */
504 if ( gblErrorCount > 0 )
507 /* Locate the backend program */
509 wantComplete = false;
510 outputActive = false;
513 XmlScanner xmlScanner( xmlInputFileName, *inStream );
514 XmlParser xmlParser( xmlInputFileName, outputActive, wantComplete );
517 xml_parse( *inStream, xmlInputFileName,
518 outputActive, wantComplete,
519 xmlScanner, xmlParser );
521 /* If writing to a file, delete the ostream, causing it to flush.
522 * Standard out is flushed automatically. */
523 if ( outputFileName != 0 ) {
528 /* Finished, final check for errors.. */
529 if ( gblErrorCount > 0 ) {
530 /* If we opened an output file, remove it. */
531 if ( outputFileName != 0 )
532 unlink( outputFileName );
537 char *makeIntermedTemplate( const char *baseFileName )
540 const char *templ = "ragel-XXXXXX.xml";
541 char *lastSlash = strrchr( baseFileName, '/' );
542 if ( lastSlash == 0 ) {
543 result = new char[strlen(templ)+1];
544 strcpy( result, templ );
547 int baseLen = lastSlash - baseFileName + 1;
548 result = new char[baseLen + strlen(templ) + 1];
549 memcpy( result, baseFileName, baseLen );
550 strcpy( result+baseLen, templ );
555 const char *openIntermed( const char *inputFileName, const char *outputFileName )
558 const char *result = 0;
560 /* Which filename do we use as the base? */
561 const char *baseFileName = outputFileName != 0 ? outputFileName : inputFileName;
563 /* The template for the intermediate file name. */
564 const char *intermedFileName = makeIntermedTemplate( baseFileName );
566 /* Randomize the name and try to open. */
567 char fnChars[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
568 char *firstX = strrchr( intermedFileName, 'X' ) - 5;
569 for ( int tries = 0; tries < 20; tries++ ) {
570 /* Choose a random name. */
571 for ( int x = 0; x < 6; x++ )
572 firstX[x] = fnChars[rand() % 52];
574 /* Try to open the file. */
575 int fd = ::open( intermedFileName, O_WRONLY|O_EXCL|O_CREAT, S_IRUSR|S_IWUSR );
578 /* Success. Close the file immediately and return the name for use
579 * by the child processes. */
581 result = intermedFileName;
585 if ( errno == EACCES ) {
586 error() << "failed to open temp file " << intermedFileName <<
587 ", access denied" << endp;
592 error() << "abnormal error: cannot find unique name for temp file" << endp;
598 void cleanExit( const char *intermed, int status )
604 /* Main, process args and call yyparse to start scanning input. */
605 int main( int argc, const char **argv )
607 const char *inputFileName = 0;
608 processArgs( argc, argv, inputFileName );
610 /* If -M or -S are given and we're not generating a dot file then invoke
611 * the frontend. These options are not useful with code generators. */
612 if ( machineName != 0 || machineSpec != 0 ) {
617 /* Require an input file. If we use standard in then we won't have a file
618 * name on which to base the output. */
619 if ( inputFileName == 0 )
620 error() << "no input file given" << endl;
622 /* Bail on argument processing errors. */
623 if ( gblErrorCount > 0 )
626 /* Make sure we are not writing to the same file as the input file. */
627 if ( inputFileName != 0 && outputFileName != 0 &&
628 strcmp( inputFileName, outputFileName ) == 0 )
630 error() << "output file \"" << outputFileName <<
631 "\" is the same as the input file" << endp;
634 const char *intermed = openIntermed( inputFileName, outputFileName );
635 process( inputFileName, intermed );
637 /* Clean up the intermediate. */
638 cleanExit( intermed, 0 );