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