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