Updated documentation for -S and -M.
[external/ragel.git] / ragel / main.cpp
1 /*
2  *  Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
3  */
4
5 /*  This file is part of Ragel.
6  *
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.
11  * 
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.
16  * 
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 
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <iostream>
26 #include <fstream>
27 #include <unistd.h>
28 #include <sstream>
29
30 /* Parsing. */
31 #include "ragel.h"
32 #include "rlscan.h"
33
34 /* Parameters and output. */
35 #include "pcheck.h"
36 #include "vector.h"
37 #include "version.h"
38 #include "common.h"
39
40 using std::istream;
41 using std::ostream;
42 using std::ifstream;
43 using std::ofstream;
44 using std::cin;
45 using std::cout;
46 using std::cerr;
47 using std::endl;
48
49 /* Controls minimization. */
50 MinimizeLevel minimizeLevel = MinimizePartition2;
51 MinimizeOpt minimizeOpt = MinimizeMostOps;
52
53 /* Graphviz dot file generation. */
54 char *machineSpec = 0, *machineName = 0;
55 bool machineSpecFound = false;
56
57 bool printStatistics = false;
58
59 /* Print a summary of the options. */
60 void usage()
61 {
62         cout <<
63 "usage: ragel [options] file\n"
64 "general:\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"
69 "fsm minimization:\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"
77 "host language:\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"
82         ;       
83 }
84
85 /* Print version information. */
86 void version()
87 {
88         cout << "Ragel State Machine Compiler version " VERSION << " " PUBDATE << endl <<
89                         "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
90 }
91
92 /* Total error count. */
93 int gblErrorCount = 0;
94
95 /* Print the opening to a warning in the input, then return the error ostream. */
96 ostream &warning( const InputLoc &loc )
97 {
98         assert( loc.fileName != 0 );
99         cerr << loc.fileName << ":" << loc.line << ":" << 
100                         loc.col << ": warning: ";
101         return cerr;
102 }
103
104 /* Print the opening to a program error, then return the error stream. */
105 ostream &error()
106 {
107         gblErrorCount += 1;
108         cerr << PROGNAME ": ";
109         return cerr;
110 }
111
112 ostream &error( const InputLoc &loc )
113 {
114         gblErrorCount += 1;
115         assert( loc.fileName != 0 );
116         cerr << loc.fileName << ":" << loc.line << ": ";
117         return cerr;
118 }
119
120 void escapeLineDirectivePath( std::ostream &out, char *path )
121 {
122         for ( char *pc = path; *pc != 0; pc++ ) {
123                 if ( *pc == '\\' )
124                         out << "\\\\";
125                 else
126                         out << *pc;
127         }
128 }
129
130 /* Main, process args and call yyparse to start scanning input. */
131 int main(int argc, char **argv)
132 {
133         ParamCheck pc("o:nmleabjkS:M:CDJRvHh?-:s", argc, argv);
134         char *inputFileName = 0;
135         char *outputFileName = 0;
136
137         while ( pc.check() ) {
138                 switch ( pc.state ) {
139                 case ParamCheck::match:
140                         switch ( pc.parameter ) {
141                         /* Output. */
142                         case 'o':
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;
147                                 else {
148                                         /* Ok, remember the output file name. */
149                                         outputFileName = pc.parameterArg;
150                                 }
151                                 break;
152
153                         /* Minimization, mostly hidden options. */
154                         case 'n':
155                                 minimizeOpt = MinimizeNone;
156                                 break;
157                         case 'm':
158                                 minimizeOpt = MinimizeEnd;
159                                 break;
160                         case 'l':
161                                 minimizeOpt = MinimizeMostOps;
162                                 break;
163                         case 'e':
164                                 minimizeOpt = MinimizeEveryOp;
165                                 break;
166                         case 'a':
167                                 minimizeLevel = MinimizeApprox;
168                                 break;
169                         case 'b':
170                                 minimizeLevel = MinimizeStable;
171                                 break;
172                         case 'j':
173                                 minimizeLevel = MinimizePartition1;
174                                 break;
175                         case 'k':
176                                 minimizeLevel = MinimizePartition2;
177                                 break;
178
179                         /* Machine spec. */
180                         case 'S':
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;
185                                 else {
186                                         /* Ok, remember the path to the machine to generate. */
187                                         machineSpec = pc.parameterArg;
188                                 }
189                                 break;
190
191                         /* Machine path. */
192                         case 'M':
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;
197                                 else {
198                                         /* Ok, remember the machine name to generate. */
199                                         machineName = pc.parameterArg;
200                                 }
201                                 break;
202
203                         /* Host language types. */
204                         case 'C':
205                                 hostLang = &hostLangC;
206                                 break;
207                         case 'D':
208                                 hostLang = &hostLangD;
209                                 break;
210                         case 'J':
211                                 hostLang = &hostLangJava;
212                                 break;
213                         case 'R':
214                                 hostLang = &hostLangRuby;
215                                 break;
216
217                         /* Version and help. */
218                         case 'v':
219                                 version();
220                                 exit(0);
221                         case 'H': case 'h': case '?':
222                                 usage();
223                                 exit(0);
224                         case 's':
225                                 printStatistics = true;
226                                 break;
227                         case '-':
228                                 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
229                                         usage();
230                                         exit(0);
231                                 }
232                                 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
233                                         version();
234                                         exit(0);
235                                 }
236                                 else {
237                                         error() << "--" << pc.parameterArg << 
238                                                         " is an invalid argument" << endl;
239                                 }
240                         }
241                         break;
242
243                 case ParamCheck::invalid:
244                         error() << "-" << pc.parameter << " is an invalid argument" << endl;
245                         break;
246
247                 case ParamCheck::noparam:
248                         /* It is interpreted as an input file. */
249                         if ( *pc.curArg == 0 )
250                                 error() << "a zero length input file name was given" << endl;
251                         else if ( inputFileName != 0 )
252                                 error() << "more than one input file name was given" << endl;
253                         else {
254                                 /* OK, Remember the filename. */
255                                 inputFileName = pc.curArg;
256                         }
257                         break;
258                 }
259         }
260
261         /* Bail on above errors. */
262         if ( gblErrorCount > 0 )
263                 exit(1);
264
265         /* Make sure we are not writing to the same file as the input file. */
266         if ( inputFileName != 0 && outputFileName != 0 && 
267                         strcmp( inputFileName, outputFileName  ) == 0 )
268         {
269                 error() << "output file \"" << outputFileName  << 
270                                 "\" is the same as the input file" << endl;
271         }
272
273         /* Open the input file for reading. */
274         istream *inStream;
275         if ( inputFileName != 0 ) {
276                 /* Open the input file for reading. */
277                 ifstream *inFile = new ifstream( inputFileName );
278                 inStream = inFile;
279                 if ( ! inFile->is_open() )
280                         error() << "could not open " << inputFileName << " for reading" << endl;
281         }
282         else {
283                 inputFileName = "<stdin>";
284                 inStream = &cin;
285         }
286
287
288         /* Bail on above errors. */
289         if ( gblErrorCount > 0 )
290                 exit(1);
291
292         std::ostringstream outputBuffer;
293
294         if ( machineSpec == 0 && machineName == 0 )
295                 outputBuffer << "<host line=\"1\" col=\"1\">";
296
297         Scanner scanner( inputFileName, *inStream, outputBuffer, 0, 0, 0, false );
298         scanner.do_scan();
299
300         /* Finished, final check for errors.. */
301         if ( gblErrorCount > 0 )
302                 return 1;
303         
304         /* Now send EOF to all parsers. */
305         terminateAllParsers();
306
307         /* Finished, final check for errors.. */
308         if ( gblErrorCount > 0 )
309                 return 1;
310
311         if ( machineSpec == 0 && machineName == 0 )
312                 outputBuffer << "</host>\n";
313
314         if ( gblErrorCount > 0 )
315                 return 1;
316         
317         ostream *outputFile = 0;
318         if ( outputFileName != 0 )
319                 outputFile = new ofstream( outputFileName );
320         else
321                 outputFile = &cout;
322
323         /* Write the machines, then the surrounding code. */
324         writeMachines( *outputFile, outputBuffer.str(), inputFileName );
325
326         if ( outputFileName != 0 )
327                 delete outputFile;
328
329         return 0;
330 }