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. */
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"
83 /* Print version information. */
86 cout << "Ragel State Machine Compiler version " VERSION << " " PUBDATE << endl <<
87 "Copyright (c) 2001-2006 by Adrian Thurston" << endl;
90 /* Total error count. */
91 int gblErrorCount = 0;
93 /* Print the opening to a warning in the input, then return the error ostream. */
94 ostream &warning( const InputLoc &loc )
96 assert( loc.fileName != 0 );
97 cerr << loc.fileName << ":" << loc.line << ":" <<
98 loc.col << ": warning: ";
102 /* Print the opening to a program error, then return the error stream. */
106 cerr << PROGNAME ": ";
110 ostream &error( const InputLoc &loc )
113 assert( loc.fileName != 0 );
114 cerr << loc.fileName << ":" << loc.line << ": ";
118 void escapeLineDirectivePath( std::ostream &out, char *path )
120 for ( char *pc = path; *pc != 0; pc++ ) {
128 /* Main, process args and call yyparse to start scanning input. */
129 int main(int argc, char **argv)
131 ParamCheck pc("o:nmleabjkS:M:CDJvHh?-:s", argc, argv);
132 char *inputFileName = 0;
133 char *outputFileName = 0;
135 while ( pc.check() ) {
136 switch ( pc.state ) {
137 case ParamCheck::match:
138 switch ( pc.parameter ) {
141 if ( *pc.parameterArg == 0 )
142 error() << "a zero length output file name was given" << endl;
143 else if ( outputFileName != 0 )
144 error() << "more than one output file name was given" << endl;
146 /* Ok, remember the output file name. */
147 outputFileName = pc.parameterArg;
151 /* Minimization, mostly hidden options. */
153 minimizeOpt = MinimizeNone;
156 minimizeOpt = MinimizeEnd;
159 minimizeOpt = MinimizeMostOps;
162 minimizeOpt = MinimizeEveryOp;
165 minimizeLevel = MinimizeApprox;
168 minimizeLevel = MinimizeStable;
171 minimizeLevel = MinimizePartition1;
174 minimizeLevel = MinimizePartition2;
179 if ( *pc.parameterArg == 0 )
180 error() << "please specify an argument to -S" << endl;
181 else if ( machineSpec != 0 )
182 error() << "more than one -S argument was given" << endl;
184 /* Ok, remember the path to the machine to generate. */
185 machineSpec = pc.parameterArg;
191 if ( *pc.parameterArg == 0 )
192 error() << "please specify an argument to -M" << endl;
193 else if ( machineName != 0 )
194 error() << "more than one -M argument was given" << endl;
196 /* Ok, remember the machine name to generate. */
197 machineName = pc.parameterArg;
201 /* Host language types. */
203 hostLangType = CCode;
204 hostLang = &hostLangC;
207 hostLangType = DCode;
208 hostLang = &hostLangD;
211 hostLangType = JavaCode;
212 hostLang = &hostLangJava;
215 /* Version and help. */
219 case 'H': case 'h': case '?':
223 printStatistics = true;
226 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
230 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
235 error() << "--" << pc.parameterArg <<
236 " is an invalid argument" << endl;
241 case ParamCheck::invalid:
242 error() << "-" << pc.parameter << " is an invalid argument" << endl;
245 case ParamCheck::noparam:
246 /* It is interpreted as an input file. */
247 if ( *pc.curArg == 0 )
248 error() << "a zero length input file name was given" << endl;
249 else if ( inputFileName != 0 )
250 error() << "more than one input file name was given" << endl;
252 /* OK, Remember the filename. */
253 inputFileName = pc.curArg;
259 /* Bail on above errors. */
260 if ( gblErrorCount > 0 )
263 /* Make sure we are not writing to the same file as the input file. */
264 if ( inputFileName != 0 && outputFileName != 0 &&
265 strcmp( inputFileName, outputFileName ) == 0 )
267 error() << "output file \"" << outputFileName <<
268 "\" is the same as the input file" << endl;
271 /* Open the input file for reading. */
273 if ( inputFileName != 0 ) {
274 /* Open the input file for reading. */
275 ifstream *inFile = new ifstream( inputFileName );
277 if ( ! inFile->is_open() )
278 error() << "could not open " << inputFileName << " for reading" << endl;
281 inputFileName = "<stdin>";
286 /* Bail on above errors. */
287 if ( gblErrorCount > 0 )
290 std::ostringstream outputBuffer;
292 if ( machineSpec == 0 && machineName == 0 )
293 outputBuffer << "<host line=\"1\" col=\"1\">";
295 scan( inputFileName, *inStream, outputBuffer );
297 /* Finished, final check for errors.. */
298 if ( gblErrorCount > 0 )
301 /* Now send EOF to all parsers. */
302 terminateAllParsers();
304 /* Finished, final check for errors.. */
305 if ( gblErrorCount > 0 )
308 if ( machineSpec == 0 && machineName == 0 )
309 outputBuffer << "</host>\n";
313 if ( gblErrorCount > 0 )
316 ostream *outputFile = 0;
317 if ( outputFileName != 0 )
318 outputFile = new ofstream( outputFileName );
322 /* Write the machines, then the surrounding code. */
323 writeMachines( *outputFile, outputBuffer.str(), inputFileName );
325 if ( outputFileName != 0 )