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
33 /* Parameters and output. */
48 /* Controls minimization. */
49 MinimizeLevel minimizeLevel = MinimizePartition2;
50 MinimizeOpt minimizeOpt = MinimizeMostOps;
52 /* Graphviz dot file generation. */
53 char *machineSpec = 0, *machineName = 0;
54 bool machineSpecFound = false;
56 bool printStatistics = false;
58 /* Print a summary of the options. */
62 "usage: ragel [options] file\n"
64 " -h, -H, -?, --help Print this usage and exit\n"
65 " -v, --version Print version information and exit\n"
66 " -o <file> Write output to <file>\n"
67 " -s Print some statistics on stderr\n"
69 " -n Do not perform minimization\n"
70 " -m Minimize at the end of the compilation\n"
71 " -l Minimize after most operations (default)\n"
72 " -e Minimize after every operation\n"
73 "machine selection:\n"
74 " -S <spec> FSM specification to output for -V\n"
75 " -M <machine> Machine definition/instantiation to output for -V\n"
77 " -C The host language is C, C++, Obj-C or Obj-C++ (default)\n"
78 " -D The host language is D\n"
79 " -J The host language is Java\n"
80 " -R The host language is Ruby\n"
84 /* Print version information. */
87 cout << "Ragel State Machine Compiler version " VERSION << " " PUBDATE << endl <<
88 "Copyright (c) 2001-2006 by Adrian Thurston" << endl;
91 /* Total error count. */
92 int gblErrorCount = 0;
94 /* Print the opening to a warning in the input, then return the error ostream. */
95 ostream &warning( const InputLoc &loc )
97 assert( loc.fileName != 0 );
98 cerr << loc.fileName << ":" << loc.line << ":" <<
99 loc.col << ": warning: ";
103 /* Print the opening to a program error, then return the error stream. */
107 cerr << PROGNAME ": ";
111 ostream &error( const InputLoc &loc )
114 assert( loc.fileName != 0 );
115 cerr << loc.fileName << ":" << loc.line << ": ";
119 void escapeLineDirectivePath( std::ostream &out, char *path )
121 for ( char *pc = path; *pc != 0; pc++ ) {
129 /* Main, process args and call yyparse to start scanning input. */
130 int main(int argc, char **argv)
132 ParamCheck pc("o:nmleabjkS:M:CDJRvHh?-:s", argc, argv);
133 char *inputFileName = 0;
134 char *outputFileName = 0;
136 while ( pc.check() ) {
137 switch ( pc.state ) {
138 case ParamCheck::match:
139 switch ( pc.parameter ) {
142 if ( *pc.parameterArg == 0 )
143 error() << "a zero length output file name was given" << endl;
144 else if ( outputFileName != 0 )
145 error() << "more than one output file name was given" << endl;
147 /* Ok, remember the output file name. */
148 outputFileName = pc.parameterArg;
152 /* Minimization, mostly hidden options. */
154 minimizeOpt = MinimizeNone;
157 minimizeOpt = MinimizeEnd;
160 minimizeOpt = MinimizeMostOps;
163 minimizeOpt = MinimizeEveryOp;
166 minimizeLevel = MinimizeApprox;
169 minimizeLevel = MinimizeStable;
172 minimizeLevel = MinimizePartition1;
175 minimizeLevel = MinimizePartition2;
180 if ( *pc.parameterArg == 0 )
181 error() << "please specify an argument to -S" << endl;
182 else if ( machineSpec != 0 )
183 error() << "more than one -S argument was given" << endl;
185 /* Ok, remember the path to the machine to generate. */
186 machineSpec = pc.parameterArg;
192 if ( *pc.parameterArg == 0 )
193 error() << "please specify an argument to -M" << endl;
194 else if ( machineName != 0 )
195 error() << "more than one -M argument was given" << endl;
197 /* Ok, remember the machine name to generate. */
198 machineName = pc.parameterArg;
202 /* Host language types. */
204 hostLangType = CCode;
205 hostLang = &hostLangC;
208 hostLangType = DCode;
209 hostLang = &hostLangD;
212 hostLangType = JavaCode;
213 hostLang = &hostLangJava;
216 hostLangType = RubyCode;
217 hostLang = &hostLangRuby;
220 /* Version and help. */
224 case 'H': case 'h': case '?':
228 printStatistics = true;
231 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
235 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
240 error() << "--" << pc.parameterArg <<
241 " is an invalid argument" << endl;
246 case ParamCheck::invalid:
247 error() << "-" << pc.parameter << " is an invalid argument" << endl;
250 case ParamCheck::noparam:
251 /* It is interpreted as an input file. */
252 if ( *pc.curArg == 0 )
253 error() << "a zero length input file name was given" << endl;
254 else if ( inputFileName != 0 )
255 error() << "more than one input file name was given" << endl;
257 /* OK, Remember the filename. */
258 inputFileName = pc.curArg;
264 /* Bail on above errors. */
265 if ( gblErrorCount > 0 )
268 /* Make sure we are not writing to the same file as the input file. */
269 if ( inputFileName != 0 && outputFileName != 0 &&
270 strcmp( inputFileName, outputFileName ) == 0 )
272 error() << "output file \"" << outputFileName <<
273 "\" is the same as the input file" << endl;
276 /* Open the input file for reading. */
278 if ( inputFileName != 0 ) {
279 /* Open the input file for reading. */
280 ifstream *inFile = new ifstream( inputFileName );
282 if ( ! inFile->is_open() )
283 error() << "could not open " << inputFileName << " for reading" << endl;
286 inputFileName = "<stdin>";
291 /* Bail on above errors. */
292 if ( gblErrorCount > 0 )
295 std::ostringstream outputBuffer;
297 if ( machineSpec == 0 && machineName == 0 )
298 outputBuffer << "<host line=\"1\" col=\"1\">";
300 scan( inputFileName, *inStream, outputBuffer );
302 /* Finished, final check for errors.. */
303 if ( gblErrorCount > 0 )
306 /* Now send EOF to all parsers. */
307 terminateAllParsers();
309 /* Finished, final check for errors.. */
310 if ( gblErrorCount > 0 )
313 if ( machineSpec == 0 && machineName == 0 )
314 outputBuffer << "</host>\n";
318 if ( gblErrorCount > 0 )
321 ostream *outputFile = 0;
322 if ( outputFileName != 0 )
323 outputFile = new ofstream( outputFileName );
327 /* Write the machines, then the surrounding code. */
328 writeMachines( *outputFile, outputBuffer.str(), inputFileName );
330 if ( outputFileName != 0 )