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
34 /* Parameters and output. */
49 /* Controls minimization. */
50 MinimizeLevel minimizeLevel = MinimizePartition2;
51 MinimizeOpt minimizeOpt = MinimizeMostOps;
53 /* Graphviz dot file generation. */
54 char *machineSpec = 0, *machineName = 0;
55 bool machineSpecFound = false;
57 bool printStatistics = false;
59 /* Print a summary of the options. */
63 "usage: ragel [options] file\n"
65 " -h, -H, -?, --help Print this usage and exit\n"
66 " -v, --version Print version information and exit\n"
67 " -o <file> Write output to <file>\n"
68 " -s Print some statistics on stderr\n"
70 " -n Do not perform minimization\n"
71 " -m Minimize at the end of the compilation\n"
72 " -l Minimize after most operations (default)\n"
73 " -e Minimize after every operation\n"
74 "machine selection:\n"
75 " -S <spec> FSM specification to output (for rlgen-dot)\n"
76 " -M <machine> Machine definition/instantiation to output (for rlgen-dot)\n"
78 " -C The host language is C, C++, Obj-C or Obj-C++ (default)\n"
79 " -D The host language is D\n"
80 " -J The host language is Java\n"
81 " -R The host language is Ruby\n"
85 /* Print version information. */
88 cout << "Ragel State Machine Compiler version " VERSION << " " PUBDATE << endl <<
89 "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
92 /* Total error count. */
93 int gblErrorCount = 0;
95 /* Print the opening to a warning in the input, then return the error ostream. */
96 ostream &warning( const InputLoc &loc )
98 assert( loc.fileName != 0 );
99 cerr << loc.fileName << ":" << loc.line << ":" <<
100 loc.col << ": warning: ";
104 /* Print the opening to a program error, then return the error stream. */
108 cerr << PROGNAME ": ";
112 ostream &error( const InputLoc &loc )
115 assert( loc.fileName != 0 );
116 cerr << loc.fileName << ":" << loc.line << ": ";
120 void escapeLineDirectivePath( std::ostream &out, char *path )
122 for ( char *pc = path; *pc != 0; pc++ ) {
130 /* If any forward slash is found in argv0 then it is assumed that the path is
131 * explicit and the path to the backend executable should be derived from
132 * that. If no forward slash is found it is assumed the file is being run from
133 * the installed location. The PREFIX supplied during configuration is used.
135 char **makePathChecks( const char *argv0, const char *progName )
137 char **result = new char*[3];
138 const char *lastSlash = strrchr( argv0, '/' );
141 if ( lastSlash != 0 ) {
142 char *path = strdup( argv0 );
143 int givenPathLen = (lastSlash - argv0) + 1;
144 path[givenPathLen] = 0;
146 int progNameLen = strlen(progName);
147 int length = givenPathLen + progNameLen + 1;
148 char *check = new char[length];
149 sprintf( check, "%s%s", path, progName );
150 result[numChecks++] = check;
152 length = givenPathLen + 3 + progNameLen + 1 + progNameLen + 1;
153 check = new char[length];
154 sprintf( check, "%s../%s/%s", path, progName, progName );
155 result[numChecks++] = check;
158 int prefixLen = strlen(PREFIX);
159 int progNameLen = strlen(progName);
160 int length = prefixLen + 5 + progNameLen + 1;
161 char *check = new char[length];
163 sprintf( check, PREFIX "/bin/%s", progName );
164 result[numChecks++] = check;
167 result[numChecks] = 0;
172 void execBackend( const char *argv0 )
174 /* Locate the backend program */
175 const char *progName = 0;
176 switch ( hostLang->lang ) {
179 progName = "rlgen-cd";
182 progName = "rlgen-java";
185 progName = "rlgen-ruby";
189 char **pathChecks = makePathChecks( argv0, progName );
190 while ( *pathChecks != 0 ) {
195 /* Main, process args and call yyparse to start scanning input. */
196 int main(int argc, char **argv)
198 ParamCheck pc("o:nmleabjkS:M:CDJRvHh?-:s", argc, argv);
199 char *inputFileName = 0;
200 char *outputFileName = 0;
202 while ( pc.check() ) {
203 switch ( pc.state ) {
204 case ParamCheck::match:
205 switch ( pc.parameter ) {
208 if ( *pc.parameterArg == 0 )
209 error() << "a zero length output file name was given" << endl;
210 else if ( outputFileName != 0 )
211 error() << "more than one output file name was given" << endl;
213 /* Ok, remember the output file name. */
214 outputFileName = pc.parameterArg;
218 /* Minimization, mostly hidden options. */
220 minimizeOpt = MinimizeNone;
223 minimizeOpt = MinimizeEnd;
226 minimizeOpt = MinimizeMostOps;
229 minimizeOpt = MinimizeEveryOp;
232 minimizeLevel = MinimizeApprox;
235 minimizeLevel = MinimizeStable;
238 minimizeLevel = MinimizePartition1;
241 minimizeLevel = MinimizePartition2;
246 if ( *pc.parameterArg == 0 )
247 error() << "please specify an argument to -S" << endl;
248 else if ( machineSpec != 0 )
249 error() << "more than one -S argument was given" << endl;
251 /* Ok, remember the path to the machine to generate. */
252 machineSpec = pc.parameterArg;
258 if ( *pc.parameterArg == 0 )
259 error() << "please specify an argument to -M" << endl;
260 else if ( machineName != 0 )
261 error() << "more than one -M argument was given" << endl;
263 /* Ok, remember the machine name to generate. */
264 machineName = pc.parameterArg;
268 /* Host language types. */
270 hostLang = &hostLangC;
273 hostLang = &hostLangD;
276 hostLang = &hostLangJava;
279 hostLang = &hostLangRuby;
282 /* Version and help. */
286 case 'H': case 'h': case '?':
290 printStatistics = true;
293 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
297 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
302 error() << "--" << pc.parameterArg <<
303 " is an invalid argument" << endl;
308 case ParamCheck::invalid:
309 error() << "-" << pc.parameter << " is an invalid argument" << endl;
312 case ParamCheck::noparam:
313 /* It is interpreted as an input file. */
314 if ( *pc.curArg == 0 )
315 error() << "a zero length input file name was given" << endl;
316 else if ( inputFileName != 0 )
317 error() << "more than one input file name was given" << endl;
319 /* OK, Remember the filename. */
320 inputFileName = pc.curArg;
326 /* Bail on above errors. */
327 if ( gblErrorCount > 0 )
330 /* Make sure we are not writing to the same file as the input file. */
331 if ( inputFileName != 0 && outputFileName != 0 &&
332 strcmp( inputFileName, outputFileName ) == 0 )
334 error() << "output file \"" << outputFileName <<
335 "\" is the same as the input file" << endl;
338 /* Open the input file for reading. */
340 if ( inputFileName != 0 ) {
341 /* Open the input file for reading. */
342 ifstream *inFile = new ifstream( inputFileName );
344 if ( ! inFile->is_open() )
345 error() << "could not open " << inputFileName << " for reading" << endl;
348 inputFileName = "<stdin>";
353 /* Bail on above errors. */
354 if ( gblErrorCount > 0 )
357 std::ostringstream outputBuffer;
359 if ( machineSpec == 0 && machineName == 0 )
360 outputBuffer << "<host line=\"1\" col=\"1\">";
362 Scanner scanner( inputFileName, *inStream, outputBuffer, 0, 0, 0, false );
365 /* Finished, final check for errors.. */
366 if ( gblErrorCount > 0 )
369 /* Now send EOF to all parsers. */
370 terminateAllParsers();
372 /* Finished, final check for errors.. */
373 if ( gblErrorCount > 0 )
376 if ( machineSpec == 0 && machineName == 0 )
377 outputBuffer << "</host>\n";
379 if ( gblErrorCount > 0 )
382 ostream *outputFile = 0;
383 if ( outputFileName != 0 )
384 outputFile = new ofstream( outputFileName );
388 /* Write the machines, then the surrounding code. */
389 writeMachines( *outputFile, outputBuffer.str(), inputFileName );
391 if ( outputFileName != 0 )
394 execBackend( argv[0] );