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. */
67 using std::streamsize;
69 /* Controls minimization. */
70 MinimizeLevel minimizeLevel = MinimizePartition2;
71 MinimizeOpt minimizeOpt = MinimizeMostOps;
73 /* Graphviz dot file generation. */
74 const char *machineSpec = 0, *machineName = 0;
75 bool machineSpecFound = false;
76 bool wantDupsRemoved = true;
78 bool printStatistics = false;
79 bool frontendOnly = false;
80 bool generateDot = false;
82 /* Target language and output style. */
83 CodeStyleEnum codeStyle = GenTables;
85 int numSplitPartitions = 0;
86 bool noLineDirectives = false;
88 bool displayPrintables = false;
89 bool graphvizDone = false;
91 /* Target ruby impl */
92 RubyImplEnum rubyImpl = MRI;
94 ArgsVector includePaths;
96 istream *inStream = 0;
97 ostream *outStream = 0;
98 output_filter *outFilter = 0;
99 const char *outputFileName = 0;
101 /* Print a summary of the options. */
105 "usage: ragel [options] file\n"
107 " -h, -H, -?, --help Print this usage and exit\n"
108 " -v, --version Print version information and exit\n"
109 " -o <file> Write output to <file>\n"
110 " -s Print some statistics on stderr\n"
111 " -d Do not remove duplicates from action lists\n"
112 " -I <dir> Add <dir> to the list of directories to search\n"
113 " for included an imported files\n"
114 "error reporting format:\n"
115 " --error-format=gnu file:line:column: message (default)\n"
116 " --error-format=msvc file(line,column): message\n"
117 "fsm minimization:\n"
118 " -n Do not perform minimization\n"
119 " -m Minimize at the end of the compilation\n"
120 " -l Minimize after most operations (default)\n"
121 " -e Minimize after every operation\n"
123 " -x Run the frontend only: emit XML intermediate format\n"
124 " -V Generate a dot file for Graphviz\n"
125 " -p Display printable characters on labels\n"
126 " -S <spec> FSM specification to output (for rlgen-dot)\n"
127 " -M <machine> Machine definition/instantiation to output (for rlgen-dot)\n"
129 " -C The host language is C, C++, Obj-C or Obj-C++ (default)\n"
130 " -D The host language is D\n"
131 " -J The host language is Java\n"
132 " -R The host language is Ruby\n"
133 " -A The host language is C#\n"
134 "line direcives: (C/D/C# only)\n"
135 " -L Inhibit writing of #line directives\n"
136 "code style: (C/Ruby/C# only)\n"
137 " -T0 Table driven FSM (default)\n"
138 " -T1 Faster table driven FSM\n"
139 " -F0 Flat table driven FSM\n"
140 " -F1 Faster flat table-driven FSM\n"
141 "code style: (C/C# only)\n"
142 " -G0 Goto-driven FSM\n"
143 " -G1 Faster goto-driven FSM\n"
144 "code style: (C only)\n"
145 " -G2 Really fast goto-driven FSM\n"
146 " -P<N> N-Way Split really fast goto-driven FSM\n"
152 /* Print version information and exit. */
155 cout << "Ragel State Machine Compiler version " VERSION << " " PUBDATE << endl <<
156 "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
160 /* Error reporting format. */
161 ErrorFormat errorFormat = ErrorFormatGNU;
163 InputLoc makeInputLoc( const char *fileName, int line, int col)
165 InputLoc loc = { fileName, line, col };
169 ostream &operator<<( ostream &out, const InputLoc &loc )
171 assert( loc.fileName != 0 );
172 switch ( errorFormat ) {
173 case ErrorFormatMSVC:
174 out << loc.fileName << "(" << loc.line;
176 out << "," << loc.col;
181 out << loc.fileName << ":" << loc.line;
183 out << ":" << loc.col;
189 /* Total error count. */
190 int gblErrorCount = 0;
192 /* Print the opening to a warning in the input, then return the error ostream. */
193 ostream &warning( const InputLoc &loc )
195 cerr << loc << ": warning: ";
199 /* Print the opening to a program error, then return the error stream. */
203 cerr << PROGNAME ": ";
207 ostream &error( const InputLoc &loc )
214 void escapeLineDirectivePath( std::ostream &out, char *path )
216 for ( char *pc = path; *pc != 0; pc++ ) {
224 void processArgs( int argc, const char **argv, const char *&inputFileName )
226 ParamCheck pc("xo:dnmleabjkS:M:I:CDJRAvHh?-:sT:F:G:P:LpV", argc, argv);
228 /* FIXME: Need to check code styles VS langauge. */
230 while ( pc.check() ) {
231 switch ( pc.state ) {
232 case ParamCheck::match:
233 switch ( pc.parameter ) {
244 if ( *pc.paramArg == 0 )
245 error() << "a zero length output file name was given" << endl;
246 else if ( outputFileName != 0 )
247 error() << "more than one output file name was given" << endl;
249 /* Ok, remember the output file name. */
250 outputFileName = pc.paramArg;
254 /* Flag for turning off duplicate action removal. */
256 wantDupsRemoved = false;
259 /* Minimization, mostly hidden options. */
261 minimizeOpt = MinimizeNone;
264 minimizeOpt = MinimizeEnd;
267 minimizeOpt = MinimizeMostOps;
270 minimizeOpt = MinimizeEveryOp;
273 minimizeLevel = MinimizeApprox;
276 minimizeLevel = MinimizeStable;
279 minimizeLevel = MinimizePartition1;
282 minimizeLevel = MinimizePartition2;
287 if ( *pc.paramArg == 0 )
288 error() << "please specify an argument to -S" << endl;
289 else if ( machineSpec != 0 )
290 error() << "more than one -S argument was given" << endl;
292 /* Ok, remember the path to the machine to generate. */
293 machineSpec = pc.paramArg;
299 if ( *pc.paramArg == 0 )
300 error() << "please specify an argument to -M" << endl;
301 else if ( machineName != 0 )
302 error() << "more than one -M argument was given" << endl;
304 /* Ok, remember the machine name to generate. */
305 machineName = pc.paramArg;
310 if ( *pc.paramArg == 0 )
311 error() << "please specify an argument to -I" << endl;
313 includePaths.append( pc.paramArg );
317 /* Host language types. */
319 hostLang = &hostLangC;
322 hostLang = &hostLangD;
325 hostLang = &hostLangJava;
328 hostLang = &hostLangRuby;
331 hostLang = &hostLangCSharp;
334 /* Version and help. */
338 case 'H': case 'h': case '?':
342 printStatistics = true;
345 char *eq = strchr( pc.paramArg, '=' );
350 if ( strcmp( pc.paramArg, "help" ) == 0 )
352 else if ( strcmp( pc.paramArg, "version" ) == 0 )
354 else if ( strcmp( pc.paramArg, "error-format" ) == 0 ) {
356 error() << "expecting '=value' for error-format" << endl;
357 else if ( strcmp( eq, "gnu" ) == 0 )
358 errorFormat = ErrorFormatGNU;
359 else if ( strcmp( eq, "msvc" ) == 0 )
360 errorFormat = ErrorFormatMSVC;
362 error() << "invalid value for error-format" << endl;
364 else if ( strcmp( pc.paramArg, "rbx" ) == 0 )
367 error() << "--" << pc.paramArg <<
368 " is an invalid argument" << endl;
373 /* Passthrough args. */
375 if ( pc.paramArg[0] == '0' )
376 codeStyle = GenTables;
377 else if ( pc.paramArg[0] == '1' )
378 codeStyle = GenFTables;
380 error() << "-T" << pc.paramArg[0] <<
381 " is an invalid argument" << endl;
386 if ( pc.paramArg[0] == '0' )
388 else if ( pc.paramArg[0] == '1' )
389 codeStyle = GenFFlat;
391 error() << "-F" << pc.paramArg[0] <<
392 " is an invalid argument" << endl;
397 if ( pc.paramArg[0] == '0' )
399 else if ( pc.paramArg[0] == '1' )
400 codeStyle = GenFGoto;
401 else if ( pc.paramArg[0] == '2' )
402 codeStyle = GenIpGoto;
404 error() << "-G" << pc.paramArg[0] <<
405 " is an invalid argument" << endl;
410 codeStyle = GenSplit;
411 numSplitPartitions = atoi( pc.paramArg );
415 displayPrintables = true;
419 noLineDirectives = true;
424 case ParamCheck::invalid:
425 error() << "-" << pc.parameter << " is an invalid argument" << endl;
428 case ParamCheck::noparam:
429 /* It is interpreted as an input file. */
430 if ( *pc.curArg == 0 )
431 error() << "a zero length input file name was given" << endl;
432 else if ( inputFileName != 0 )
433 error() << "more than one input file name was given" << endl;
435 /* OK, Remember the filename. */
436 inputFileName = pc.curArg;
443 int frontend( const char *inputFileName, const char *intermed )
445 /* Open the input file for reading. */
446 assert( inputFileName != 0 );
447 ifstream *inFile = new ifstream( inputFileName );
448 istream *inStream = inFile;
449 if ( ! inFile->is_open() )
450 error() << "could not open " << inputFileName << " for reading" << endp;
452 /* Used for just a few things. */
453 std::ostringstream hostData;
455 if ( machineSpec == 0 && machineName == 0 )
456 hostData << "<host line=\"1\" col=\"1\">";
458 Scanner scanner( inputFileName, *inStream, hostData, 0, 0, 0, false );
461 /* Finished, final check for errors.. */
462 if ( gblErrorCount > 0 )
465 /* Now send EOF to all parsers. */
466 terminateAllParsers();
468 /* Finished, final check for errors.. */
469 if ( gblErrorCount > 0 )
472 if ( machineSpec == 0 && machineName == 0 )
473 hostData << "</host>\n";
475 if ( gblErrorCount > 0 )
478 ostream *outputFile = new ofstream( intermed );
480 /* Write the machines, then the surrounding code. */
481 writeMachines( *outputFile, hostData.str(), inputFileName );
483 /* Close the intermediate file. */
486 return gblErrorCount > 0;
489 char *makeIntermedTemplate( const char *baseFileName )
492 const char *templ = "ragel-XXXXXX.xml";
493 char *lastSlash = strrchr( baseFileName, '/' );
494 if ( lastSlash == 0 ) {
495 result = new char[strlen(templ)+1];
496 strcpy( result, templ );
499 int baseLen = lastSlash - baseFileName + 1;
500 result = new char[baseLen + strlen(templ) + 1];
501 memcpy( result, baseFileName, baseLen );
502 strcpy( result+baseLen, templ );
507 const char *openIntermed( const char *inputFileName, const char *outputFileName )
510 const char *result = 0;
512 /* Which filename do we use as the base? */
513 const char *baseFileName = outputFileName != 0 ? outputFileName : inputFileName;
515 /* The template for the intermediate file name. */
516 const char *intermedFileName = makeIntermedTemplate( baseFileName );
518 /* Randomize the name and try to open. */
519 char fnChars[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
520 char *firstX = strrchr( intermedFileName, 'X' ) - 5;
521 for ( int tries = 0; tries < 20; tries++ ) {
522 /* Choose a random name. */
523 for ( int x = 0; x < 6; x++ )
524 firstX[x] = fnChars[rand() % 52];
526 /* Try to open the file. */
527 int fd = ::open( intermedFileName, O_WRONLY|O_EXCL|O_CREAT, S_IRUSR|S_IWUSR );
530 /* Success. Close the file immediately and return the name for use
531 * by the child processes. */
533 result = intermedFileName;
537 if ( errno == EACCES ) {
538 error() << "failed to open temp file " << intermedFileName <<
539 ", access denied" << endp;
544 error() << "abnormal error: cannot find unique name for temp file" << endp;
550 void cleanExit( const char *intermed, int status )
556 void backend( const char *intermed )
558 const char *xmlInputFileName = intermed;
560 bool wantComplete = true;
561 bool outputActive = true;
563 /* Open the input file for reading. */
564 ifstream *inFile = new ifstream( xmlInputFileName );
566 if ( ! inFile->is_open() )
567 error() << "could not open " << xmlInputFileName << " for reading" << endl;
569 /* Bail on above error. */
570 if ( gblErrorCount > 0 )
573 /* Locate the backend program */
575 wantComplete = false;
576 outputActive = false;
579 xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete );
581 /* If writing to a file, delete the ostream, causing it to flush.
582 * Standard out is flushed automatically. */
583 if ( outputFileName != 0 ) {
588 /* Finished, final check for errors.. */
589 if ( gblErrorCount > 0 ) {
590 /* If we opened an output file, remove it. */
591 if ( outputFileName != 0 )
592 unlink( outputFileName );
597 /* Main, process args and call yyparse to start scanning input. */
598 int main( int argc, const char **argv )
600 const char *inputFileName = 0;
601 processArgs( argc, argv, inputFileName );
603 /* If -M or -S are given and we're not generating a dot file then invoke
604 * the frontend. These options are not useful with code generators. */
605 if ( machineName != 0 || machineSpec != 0 ) {
610 /* Require an input file. If we use standard in then we won't have a file
611 * name on which to base the output. */
612 if ( inputFileName == 0 )
613 error() << "no input file given" << endl;
615 /* Bail on argument processing errors. */
616 if ( gblErrorCount > 0 )
619 /* Make sure we are not writing to the same file as the input file. */
620 if ( inputFileName != 0 && outputFileName != 0 &&
621 strcmp( inputFileName, outputFileName ) == 0 )
623 error() << "output file \"" << outputFileName <<
624 "\" is the same as the input file" << endp;
627 const char *intermed = openIntermed( inputFileName, outputFileName );
628 frontend( inputFileName, intermed );
631 /* Clean up the intermediate. */
632 cleanExit( intermed, 0 );