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 *arg = strdup( pc.paramArg );
340 char *eq = strchr( arg, '=' );
345 if ( strcmp( arg, "help" ) == 0 )
347 else if ( strcmp( arg, "version" ) == 0 )
349 else if ( strcmp( arg, "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( arg, "rbx" ) == 0 )
362 error() << "--" << pc.paramArg <<
363 " is an invalid argument" << endl;
369 /* Passthrough args. */
371 if ( pc.paramArg[0] == '0' )
372 codeStyle = GenTables;
373 else if ( pc.paramArg[0] == '1' )
374 codeStyle = GenFTables;
376 error() << "-T" << pc.paramArg[0] <<
377 " is an invalid argument" << endl;
382 if ( pc.paramArg[0] == '0' )
384 else if ( pc.paramArg[0] == '1' )
385 codeStyle = GenFFlat;
387 error() << "-F" << pc.paramArg[0] <<
388 " is an invalid argument" << endl;
393 if ( pc.paramArg[0] == '0' )
395 else if ( pc.paramArg[0] == '1' )
396 codeStyle = GenFGoto;
397 else if ( pc.paramArg[0] == '2' )
398 codeStyle = GenIpGoto;
400 error() << "-G" << pc.paramArg[0] <<
401 " is an invalid argument" << endl;
406 codeStyle = GenSplit;
407 numSplitPartitions = atoi( pc.paramArg );
411 displayPrintables = true;
415 noLineDirectives = true;
420 case ParamCheck::invalid:
421 error() << "-" << pc.parameter << " is an invalid argument" << endl;
424 case ParamCheck::noparam:
425 /* It is interpreted as an input file. */
426 if ( *pc.curArg == 0 )
427 error() << "a zero length input file name was given" << endl;
428 else if ( id.inputFileName != 0 )
429 error() << "more than one input file name was given" << endl;
431 /* OK, Remember the filename. */
432 id.inputFileName = pc.curArg;
439 void process( InputData &id )
441 /* Open the input file for reading. */
442 assert( id.inputFileName != 0 );
443 ifstream *inFile = new ifstream( id.inputFileName );
444 if ( ! inFile->is_open() )
445 error() << "could not open " << id.inputFileName << " for reading" << endp;
447 /* Used for just a few things. */
448 std::ostringstream hostData;
450 /* Make the first input item. */
451 InputItem *firstInputItem = new InputItem;
452 firstInputItem->type = InputItem::HostData;
453 firstInputItem->loc.fileName = id.inputFileName;
454 firstInputItem->loc.line = 1;
455 firstInputItem->loc.col = 1;
456 id.inputItems.append( firstInputItem );
458 Scanner scanner( id, id.inputFileName, *inFile, 0, 0, 0, false );
461 /* Finished, final check for errors.. */
462 if ( gblErrorCount > 0 )
465 /* Now send EOF to all parsers. */
466 id.terminateAllParsers();
468 /* Bail on above error. */
469 if ( gblErrorCount > 0 )
472 /* Locate the backend program */
473 /* Compiles machines. */
474 id.prepareMachineGen();
476 if ( gblErrorCount > 0 )
479 id.makeOutputStream();
481 /* Generates the reduced machine, which we use to write output. */
482 if ( !generateXML ) {
483 id.generateReduced();
485 if ( gblErrorCount > 0 )
489 id.verifyWritesHaveData();
490 if ( gblErrorCount > 0 )
494 * From this point on we should not be reporting any errors.
500 /* Close the input and the intermediate file. */
503 /* If writing to a file, delete the ostream, causing it to flush.
504 * Standard out is flushed automatically. */
505 if ( id.outputFileName != 0 ) {
510 assert( gblErrorCount == 0 );
513 char *makeIntermedTemplate( const char *baseFileName )
516 const char *templ = "ragel-XXXXXX.xml";
517 const char *lastSlash = strrchr( baseFileName, '/' );
518 if ( lastSlash == 0 ) {
519 result = new char[strlen(templ)+1];
520 strcpy( result, templ );
523 int baseLen = lastSlash - baseFileName + 1;
524 result = new char[baseLen + strlen(templ) + 1];
525 memcpy( result, baseFileName, baseLen );
526 strcpy( result+baseLen, templ );
531 /* Main, process args and call yyparse to start scanning input. */
532 int main( int argc, const char **argv )
536 processArgs( argc, argv, id );
538 /* Require an input file. If we use standard in then we won't have a file
539 * name on which to base the output. */
540 if ( id.inputFileName == 0 )
541 error() << "no input file given" << endl;
543 /* Bail on argument processing errors. */
544 if ( gblErrorCount > 0 )
547 /* Make sure we are not writing to the same file as the input file. */
548 if ( id.inputFileName != 0 && id.outputFileName != 0 &&
549 strcmp( id.inputFileName, id.outputFileName ) == 0 )
551 error() << "output file \"" << id.outputFileName <<
552 "\" is the same as the input file" << endp;