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 -V\n"
76 " -M <machine> Machine definition/instantiation to output for -V\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-2006 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 /* Main, process args and call yyparse to start scanning input. */
131 int main(int argc, char **argv)
133 ParamCheck pc("o:nmleabjkS:M:CDJRvHh?-:s", argc, argv);
134 char *inputFileName = 0;
135 char *outputFileName = 0;
137 while ( pc.check() ) {
138 switch ( pc.state ) {
139 case ParamCheck::match:
140 switch ( pc.parameter ) {
143 if ( *pc.parameterArg == 0 )
144 error() << "a zero length output file name was given" << endl;
145 else if ( outputFileName != 0 )
146 error() << "more than one output file name was given" << endl;
148 /* Ok, remember the output file name. */
149 outputFileName = pc.parameterArg;
153 /* Minimization, mostly hidden options. */
155 minimizeOpt = MinimizeNone;
158 minimizeOpt = MinimizeEnd;
161 minimizeOpt = MinimizeMostOps;
164 minimizeOpt = MinimizeEveryOp;
167 minimizeLevel = MinimizeApprox;
170 minimizeLevel = MinimizeStable;
173 minimizeLevel = MinimizePartition1;
176 minimizeLevel = MinimizePartition2;
181 if ( *pc.parameterArg == 0 )
182 error() << "please specify an argument to -S" << endl;
183 else if ( machineSpec != 0 )
184 error() << "more than one -S argument was given" << endl;
186 /* Ok, remember the path to the machine to generate. */
187 machineSpec = pc.parameterArg;
193 if ( *pc.parameterArg == 0 )
194 error() << "please specify an argument to -M" << endl;
195 else if ( machineName != 0 )
196 error() << "more than one -M argument was given" << endl;
198 /* Ok, remember the machine name to generate. */
199 machineName = pc.parameterArg;
203 /* Host language types. */
205 hostLangType = CCode;
206 hostLang = &hostLangC;
209 hostLangType = DCode;
210 hostLang = &hostLangD;
213 hostLangType = JavaCode;
214 hostLang = &hostLangJava;
217 hostLangType = RubyCode;
218 hostLang = &hostLangRuby;
221 /* Version and help. */
225 case 'H': case 'h': case '?':
229 printStatistics = true;
232 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
236 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
241 error() << "--" << pc.parameterArg <<
242 " is an invalid argument" << endl;
247 case ParamCheck::invalid:
248 error() << "-" << pc.parameter << " is an invalid argument" << endl;
251 case ParamCheck::noparam:
252 /* It is interpreted as an input file. */
253 if ( *pc.curArg == 0 )
254 error() << "a zero length input file name was given" << endl;
255 else if ( inputFileName != 0 )
256 error() << "more than one input file name was given" << endl;
258 /* OK, Remember the filename. */
259 inputFileName = pc.curArg;
265 /* Bail on above errors. */
266 if ( gblErrorCount > 0 )
269 /* Make sure we are not writing to the same file as the input file. */
270 if ( inputFileName != 0 && outputFileName != 0 &&
271 strcmp( inputFileName, outputFileName ) == 0 )
273 error() << "output file \"" << outputFileName <<
274 "\" is the same as the input file" << endl;
277 /* Open the input file for reading. */
279 if ( inputFileName != 0 ) {
280 /* Open the input file for reading. */
281 ifstream *inFile = new ifstream( inputFileName );
283 if ( ! inFile->is_open() )
284 error() << "could not open " << inputFileName << " for reading" << endl;
287 inputFileName = "<stdin>";
292 /* Bail on above errors. */
293 if ( gblErrorCount > 0 )
296 std::ostringstream outputBuffer;
298 if ( machineSpec == 0 && machineName == 0 )
299 outputBuffer << "<host line=\"1\" col=\"1\">";
301 Scanner scanner( inputFileName, *inStream, outputBuffer, 0, 0, 0 );
304 /* Finished, final check for errors.. */
305 if ( gblErrorCount > 0 )
308 /* Now send EOF to all parsers. */
309 terminateAllParsers();
311 /* Finished, final check for errors.. */
312 if ( gblErrorCount > 0 )
315 if ( machineSpec == 0 && machineName == 0 )
316 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 )