2 * Copyright 2001-2005 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
33 /* Parameters and output. */
50 istream *inStream = 0;
51 ostream *outStream = 0;
52 char *outputFileName = 0;
54 /* Controls minimization. */
55 MinimizeLevel minimizeLevel = MinimizePartition2;
56 MinimizeOpt minimizeOpt = MinimizeMostOps;
58 /* Graphviz dot file generation. */
59 char *machineSpec = 0, *machineName = 0;
60 bool machineSpecFound = false;
62 bool printStatistics = false;
64 /* Print a summary of the options. */
68 "usage: ragel [options] file\n"
70 " -h, -H, -?, --help Print this usage and exit\n"
71 " -v, --version Print version information and exit\n"
72 " -o <file> Write output to <file>\n"
73 " -s Print some statistics on stderr\n"
75 " -n Do not perform minimization\n"
76 " -m Minimize at the end of the compilation\n"
77 " -l Minimize after most operations (default)\n"
78 " -e Minimize after every operation\n"
79 "machine selection:\n"
80 " -S <spec> FSM specification to output for -V\n"
81 " -M <machine> Machine definition/instantiation to output for -V\n"
83 " -C The host language is C, C++, Obj-C or Obj-C++ (default)\n"
84 " -D The host language is D\n"
85 " -J The host language is Java\n"
89 /* Print version information. */
92 cout << "Ragel State Machine Compiler version " VERSION << " " PUBDATE << endl <<
93 "Copyright (c) 2001-2006 by Adrian Thurston" << endl;
96 /* Total error count. */
97 int gblErrorCount = 0;
99 /* Print the opening to a program error, then return the error stream. */
103 cerr << PROGNAME ": ";
108 /* Print the opening to a warning in the input, then return the error ostream. */
109 ostream &warning( const InputLoc &loc )
111 cerr << loc.fileName << ":" << loc.line << ":" <<
112 loc.col << ": warning: ";
116 void escapeLineDirectivePath( std::ostream &out, char *path )
118 for ( char *pc = path; *pc != 0; pc++ ) {
126 /* Main, process args and call yyparse to start scanning input. */
127 int main(int argc, char **argv)
129 ParamCheck pc("o:nmleabjkS:M:CDJvHh?-:s", argc, argv);
130 char *inputFileName = 0;
132 while ( pc.check() ) {
133 switch ( pc.state ) {
134 case ParamCheck::match:
135 switch ( pc.parameter ) {
138 if ( *pc.parameterArg == 0 )
139 error() << "a zero length output file name was given" << endl;
140 else if ( outputFileName != 0 )
141 error() << "more than one output file name was given" << endl;
143 /* Ok, remember the output file name. */
144 outputFileName = pc.parameterArg;
148 /* Minimization, mostly hidden options. */
150 minimizeOpt = MinimizeNone;
153 minimizeOpt = MinimizeEnd;
156 minimizeOpt = MinimizeMostOps;
159 minimizeOpt = MinimizeEveryOp;
162 minimizeLevel = MinimizeApprox;
165 minimizeLevel = MinimizeStable;
168 minimizeLevel = MinimizePartition1;
171 minimizeLevel = MinimizePartition2;
176 if ( *pc.parameterArg == 0 )
177 error() << "please specify an argument to -S" << endl;
178 else if ( machineSpec != 0 )
179 error() << "more than one -S argument was given" << endl;
181 /* Ok, remember the path to the machine to generate. */
182 machineSpec = pc.parameterArg;
188 if ( *pc.parameterArg == 0 )
189 error() << "please specify an argument to -M" << endl;
190 else if ( machineName != 0 )
191 error() << "more than one -M argument was given" << endl;
193 /* Ok, remember the machine name to generate. */
194 machineName = pc.parameterArg;
198 /* Host language types. */
200 hostLangType = CCode;
201 hostLang = &hostLangC;
204 hostLangType = DCode;
205 hostLang = &hostLangD;
208 hostLangType = JavaCode;
209 hostLang = &hostLangJava;
212 /* Version and help. */
216 case 'H': case 'h': case '?':
220 printStatistics = true;
223 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
227 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
232 error() << "--" << pc.parameterArg <<
233 " is an invalid argument" << endl;
238 case ParamCheck::invalid:
239 error() << "-" << pc.parameter << " is an invalid argument" << endl;
242 case ParamCheck::noparam:
243 /* It is interpreted as an input file. */
244 if ( *pc.curArg == 0 )
245 error() << "a zero length input file name was given" << endl;
246 else if ( inputFileName != 0 )
247 error() << "more than one input file name was given" << endl;
249 /* OK, Remember the filename. */
250 inputFileName = pc.curArg;
256 /* Bail on above errors. */
257 if ( gblErrorCount > 0 )
260 /* Make sure we are not writing to the same file as the input file. */
261 if ( inputFileName != 0 && outputFileName != 0 &&
262 strcmp( inputFileName, outputFileName ) == 0 )
264 error() << "output file \"" << outputFileName <<
265 "\" is the same as the input file" << endl;
268 /* Open the input file for reading. */
269 if ( inputFileName != 0 ) {
270 /* Open the input file for reading. */
271 ifstream *inFile = new ifstream( inputFileName );
273 if ( ! inFile->is_open() )
274 error() << "could not open " << inputFileName << " for reading" << endl;
277 inputFileName = "<stdin>";
282 /* Bail on above errors. */
283 if ( gblErrorCount > 0 )
286 std::ostringstream outputBuffer;
287 outStream = &outputBuffer;
289 if ( machineSpec == 0 && machineName == 0 )
290 *outStream << "<host line=\"1\" col=\"1\">";
292 scan( inputFileName, *inStream );
294 /* Finished, final check for errors.. */
295 if ( gblErrorCount > 0 )
298 /* Now send EOF to all parsers. */
299 terminateAllParsers();
301 /* Finished, final check for errors.. */
302 if ( gblErrorCount > 0 )
305 if ( machineSpec == 0 && machineName == 0 )
306 *outStream << "</host>\n";
310 if ( gblErrorCount > 0 )
313 ostream *outputFile = 0;
314 if ( outputFileName != 0 )
315 outputFile = new ofstream( outputFileName );
319 /* Write the machines, then the surrounding code. */
320 writeMachines( *outputFile, outputBuffer.str(), inputFileName );
322 if ( outputFileName != 0 )