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;
87 bool displayPrintables = false;
89 /* Target ruby impl */
90 RubyImplEnum rubyImpl = MRI;
92 ArgsVector includePaths;
94 const char *outputFileName = 0;
96 /* Print a summary of the options. */
100 "usage: ragel [options] file\n"
102 " -h, -H, -?, --help Print this usage and exit\n"
103 " -v, --version Print version information and exit\n"
104 " -o <file> Write output to <file>\n"
105 " -s Print some statistics on stderr\n"
106 " -d Do not remove duplicates from action lists\n"
107 " -I <dir> Add <dir> to the list of directories to search\n"
108 " for included an imported files\n"
109 "error reporting format:\n"
110 " --error-format=gnu file:line:column: message (default)\n"
111 " --error-format=msvc file(line,column): message\n"
112 "fsm minimization:\n"
113 " -n Do not perform minimization\n"
114 " -m Minimize at the end of the compilation\n"
115 " -l Minimize after most operations (default)\n"
116 " -e Minimize after every operation\n"
118 " -x Run the frontend only: emit XML intermediate format\n"
119 " -V Generate a dot file for Graphviz\n"
120 " -p Display printable characters on labels\n"
121 " -S <spec> FSM specification to output (for rlgen-dot)\n"
122 " -M <machine> Machine definition/instantiation to output (for rlgen-dot)\n"
124 " -C The host language is C, C++, Obj-C or Obj-C++ (default)\n"
125 " -D The host language is D\n"
126 " -J The host language is Java\n"
127 " -R The host language is Ruby\n"
128 " -A The host language is C#\n"
129 "line direcives: (C/D/C# only)\n"
130 " -L Inhibit writing of #line directives\n"
131 "code style: (C/Ruby/C# only)\n"
132 " -T0 Table driven FSM (default)\n"
133 " -T1 Faster table driven FSM\n"
134 " -F0 Flat table driven FSM\n"
135 " -F1 Faster flat table-driven FSM\n"
136 "code style: (C/C# only)\n"
137 " -G0 Goto-driven FSM\n"
138 " -G1 Faster goto-driven FSM\n"
139 "code style: (C only)\n"
140 " -G2 Really fast goto-driven FSM\n"
141 " -P<N> N-Way Split really fast goto-driven FSM\n"
147 /* Print version information and exit. */
150 cout << "Ragel State Machine Compiler version " VERSION << " " PUBDATE << endl <<
151 "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
155 /* Error reporting format. */
156 ErrorFormat errorFormat = ErrorFormatGNU;
158 InputLoc makeInputLoc( const char *fileName, int line, int col)
160 InputLoc loc = { fileName, line, col };
164 ostream &operator<<( ostream &out, const InputLoc &loc )
166 assert( loc.fileName != 0 );
167 switch ( errorFormat ) {
168 case ErrorFormatMSVC:
169 out << loc.fileName << "(" << loc.line;
171 out << "," << loc.col;
176 out << loc.fileName << ":" << loc.line;
178 out << ":" << loc.col;
184 /* Total error count. */
185 int gblErrorCount = 0;
187 /* Print the opening to a warning in the input, then return the error ostream. */
188 ostream &warning( const InputLoc &loc )
190 cerr << loc << ": warning: ";
194 /* Print the opening to a program error, then return the error stream. */
198 cerr << PROGNAME ": ";
202 ostream &error( const InputLoc &loc )
209 void escapeLineDirectivePath( std::ostream &out, char *path )
211 for ( char *pc = path; *pc != 0; pc++ ) {
219 void processArgs( int argc, const char **argv, const char *&inputFileName )
221 ParamCheck pc("xo:dnmleabjkS:M:I:CDJRAvHh?-:sT:F:G:P:LpV", argc, argv);
223 /* FIXME: Need to check code styles VS langauge. */
225 while ( pc.check() ) {
226 switch ( pc.state ) {
227 case ParamCheck::match:
228 switch ( pc.parameter ) {
239 if ( *pc.paramArg == 0 )
240 error() << "a zero length output file name was given" << endl;
241 else if ( outputFileName != 0 )
242 error() << "more than one output file name was given" << endl;
244 /* Ok, remember the output file name. */
245 outputFileName = pc.paramArg;
249 /* Flag for turning off duplicate action removal. */
251 wantDupsRemoved = false;
254 /* Minimization, mostly hidden options. */
256 minimizeOpt = MinimizeNone;
259 minimizeOpt = MinimizeEnd;
262 minimizeOpt = MinimizeMostOps;
265 minimizeOpt = MinimizeEveryOp;
268 minimizeLevel = MinimizeApprox;
271 minimizeLevel = MinimizeStable;
274 minimizeLevel = MinimizePartition1;
277 minimizeLevel = MinimizePartition2;
282 if ( *pc.paramArg == 0 )
283 error() << "please specify an argument to -S" << endl;
284 else if ( machineSpec != 0 )
285 error() << "more than one -S argument was given" << endl;
287 /* Ok, remember the path to the machine to generate. */
288 machineSpec = pc.paramArg;
294 if ( *pc.paramArg == 0 )
295 error() << "please specify an argument to -M" << endl;
296 else if ( machineName != 0 )
297 error() << "more than one -M argument was given" << endl;
299 /* Ok, remember the machine name to generate. */
300 machineName = pc.paramArg;
305 if ( *pc.paramArg == 0 )
306 error() << "please specify an argument to -I" << endl;
308 includePaths.append( pc.paramArg );
312 /* Host language types. */
314 hostLang = &hostLangC;
317 hostLang = &hostLangD;
320 hostLang = &hostLangJava;
323 hostLang = &hostLangRuby;
326 hostLang = &hostLangCSharp;
329 /* Version and help. */
333 case 'H': case 'h': case '?':
337 printStatistics = true;
340 char *eq = strchr( pc.paramArg, '=' );
345 if ( strcmp( pc.paramArg, "help" ) == 0 )
347 else if ( strcmp( pc.paramArg, "version" ) == 0 )
349 else if ( strcmp( pc.paramArg, "error-format" ) == 0 ) {
351 error() << "expecting '=value' for error-format" << endl;
352 else if ( strcmp( eq, "gnu" ) == 0 )
353 errorFormat = ErrorFormatGNU;
354 else if ( strcmp( eq, "msvc" ) == 0 )
355 errorFormat = ErrorFormatMSVC;
357 error() << "invalid value for error-format" << endl;
359 else if ( strcmp( pc.paramArg, "rbx" ) == 0 )
362 error() << "--" << pc.paramArg <<
363 " is an invalid argument" << endl;
368 /* Passthrough args. */
370 if ( pc.paramArg[0] == '0' )
371 codeStyle = GenTables;
372 else if ( pc.paramArg[0] == '1' )
373 codeStyle = GenFTables;
375 error() << "-T" << pc.paramArg[0] <<
376 " is an invalid argument" << endl;
381 if ( pc.paramArg[0] == '0' )
383 else if ( pc.paramArg[0] == '1' )
384 codeStyle = GenFFlat;
386 error() << "-F" << pc.paramArg[0] <<
387 " is an invalid argument" << endl;
392 if ( pc.paramArg[0] == '0' )
394 else if ( pc.paramArg[0] == '1' )
395 codeStyle = GenFGoto;
396 else if ( pc.paramArg[0] == '2' )
397 codeStyle = GenIpGoto;
399 error() << "-G" << pc.paramArg[0] <<
400 " is an invalid argument" << endl;
405 codeStyle = GenSplit;
406 numSplitPartitions = atoi( pc.paramArg );
410 displayPrintables = true;
414 noLineDirectives = true;
419 case ParamCheck::invalid:
420 error() << "-" << pc.parameter << " is an invalid argument" << endl;
423 case ParamCheck::noparam:
424 /* It is interpreted as an input file. */
425 if ( *pc.curArg == 0 )
426 error() << "a zero length input file name was given" << endl;
427 else if ( inputFileName != 0 )
428 error() << "more than one input file name was given" << endl;
430 /* OK, Remember the filename. */
431 inputFileName = pc.curArg;
438 int frontend( const char *inputFileName, const char *intermed )
440 /* Open the input file for reading. */
441 assert( inputFileName != 0 );
442 ifstream *inFile = new ifstream( inputFileName );
443 istream *inStream = inFile;
444 if ( ! inFile->is_open() )
445 error() << "could not open " << inputFileName << " for reading" << endp;
447 /* Used for just a few things. */
448 std::ostringstream hostData;
450 if ( machineSpec == 0 && machineName == 0 )
451 hostData << "<host line=\"1\" col=\"1\">";
453 Scanner scanner( inputFileName, *inStream, hostData, 0, 0, 0, false );
456 /* Finished, final check for errors.. */
457 if ( gblErrorCount > 0 )
460 /* Now send EOF to all parsers. */
461 terminateAllParsers();
463 /* Finished, final check for errors.. */
464 if ( gblErrorCount > 0 )
467 if ( machineSpec == 0 && machineName == 0 )
468 hostData << "</host>\n";
470 if ( gblErrorCount > 0 )
473 ostream *outputFile = new ofstream( intermed );
475 /* Write the machines, then the surrounding code. */
476 writeMachines( *outputFile, hostData.str(), inputFileName );
478 /* Close the intermediate file. */
481 return gblErrorCount > 0;
484 char *makeIntermedTemplate( const char *baseFileName )
487 const char *templ = "ragel-XXXXXX.xml";
488 char *lastSlash = strrchr( baseFileName, '/' );
489 if ( lastSlash == 0 ) {
490 result = new char[strlen(templ)+1];
491 strcpy( result, templ );
494 int baseLen = lastSlash - baseFileName + 1;
495 result = new char[baseLen + strlen(templ) + 1];
496 memcpy( result, baseFileName, baseLen );
497 strcpy( result+baseLen, templ );
502 const char *openIntermed( const char *inputFileName, const char *outputFileName )
505 const char *result = 0;
507 /* Which filename do we use as the base? */
508 const char *baseFileName = outputFileName != 0 ? outputFileName : inputFileName;
510 /* The template for the intermediate file name. */
511 const char *intermedFileName = makeIntermedTemplate( baseFileName );
513 /* Randomize the name and try to open. */
514 char fnChars[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
515 char *firstX = strrchr( intermedFileName, 'X' ) - 5;
516 for ( int tries = 0; tries < 20; tries++ ) {
517 /* Choose a random name. */
518 for ( int x = 0; x < 6; x++ )
519 firstX[x] = fnChars[rand() % 52];
521 /* Try to open the file. */
522 int fd = ::open( intermedFileName, O_WRONLY|O_EXCL|O_CREAT, S_IRUSR|S_IWUSR );
525 /* Success. Close the file immediately and return the name for use
526 * by the child processes. */
528 result = intermedFileName;
532 if ( errno == EACCES ) {
533 error() << "failed to open temp file " << intermedFileName <<
534 ", access denied" << endp;
539 error() << "abnormal error: cannot find unique name for temp file" << endp;
545 void cleanExit( const char *intermed, int status )
551 int cd_main( const char *xmlInputFileName );
552 int java_main( const char *xmlInputFileName );
553 int ruby_main( const char *xmlInputFileName );
554 int csharp_main( const char *xmlInputFileName );
555 int dot_main( const char *xmlInputFileName );
557 void backend( const char *intermed )
559 /* Locate the backend program */
561 dot_main( intermed );
563 switch ( hostLang->lang ) {
569 java_main( intermed );
572 ruby_main( intermed );
574 case HostLang::CSharp:
575 csharp_main( intermed );
581 /* Main, process args and call yyparse to start scanning input. */
582 int main( int argc, const char **argv )
584 const char *inputFileName = 0;
585 processArgs( argc, argv, inputFileName );
587 /* If -M or -S are given and we're not generating a dot file then invoke
588 * the frontend. These options are not useful with code generators. */
589 if ( machineName != 0 || machineSpec != 0 ) {
594 /* Require an input file. If we use standard in then we won't have a file
595 * name on which to base the output. */
596 if ( inputFileName == 0 )
597 error() << "no input file given" << endl;
599 /* Bail on argument processing errors. */
600 if ( gblErrorCount > 0 )
603 /* Make sure we are not writing to the same file as the input file. */
604 if ( inputFileName != 0 && outputFileName != 0 &&
605 strcmp( inputFileName, outputFileName ) == 0 )
607 error() << "output file \"" << outputFileName <<
608 "\" is the same as the input file" << endp;
611 const char *intermed = openIntermed( inputFileName, outputFileName );
612 frontend( inputFileName, intermed );
615 /* Clean up the intermediate. */
616 cleanExit( intermed, 0 );