Enabled the location printing in the warning function.
[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
38 #include "common.cpp"
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 /* Io globals. */
50 istream *inStream = 0;
51 ostream *outStream = 0;
52 char *outputFileName = 0;
53
54 /* Controls minimization. */
55 MinimizeLevel minimizeLevel = MinimizePartition2;
56 MinimizeOpt minimizeOpt = MinimizeMostOps;
57
58 /* Graphviz dot file generation. */
59 char *machineSpec = 0, *machineName = 0;
60 bool machineSpecFound = false;
61
62 bool printStatistics = false;
63
64 /* Print a summary of the options. */
65 void usage()
66 {
67         cout <<
68 "usage: ragel [options] file\n"
69 "general:\n"
70 "   -h, -H, -?, --help   Print this usage and exit\n"
71 "   -v, --version        Print version information and exit\n"
72 "   -o <file>            Write output to <file>\n"
73 "   -s                   Print some statistics on stderr\n"
74 "fsm minimization:\n"
75 "   -n                   Do not perform minimization\n"
76 "   -m                   Minimize at the end of the compilation\n"
77 "   -l                   Minimize after most operations (default)\n"
78 "   -e                   Minimize after every operation\n"
79 "machine selection:\n"
80 "   -S <spec>            FSM specification to output for -V\n"
81 "   -M <machine>         Machine definition/instantiation to output for -V\n"
82 "host language:\n"
83 "   -C                   The host language is C, C++, Obj-C or Obj-C++ (default)\n"
84 "   -D                   The host language is D\n"
85 "   -J                   The host language is Java\n"
86         ;       
87 }
88
89 /* Print version information. */
90 void version()
91 {
92         cout << "Ragel State Machine Compiler version " VERSION << " " PUBDATE << endl <<
93                         "Copyright (c) 2001-2006 by Adrian Thurston" << endl;
94 }
95
96 /* Total error count. */
97 int gblErrorCount = 0;
98
99 /* Print the opening to a program error, then return the error stream. */
100 ostream &error()
101 {
102         gblErrorCount += 1;
103         cerr << PROGNAME ": ";
104         return cerr;
105 }
106
107
108 /* Print the opening to a warning in the input, then return the error ostream. */
109 ostream &warning( const InputLoc &loc )
110 {
111         cerr << loc.fileName << ":" << loc.line << ":" << 
112                         loc.col << ": warning: ";
113         return cerr;
114 }
115
116 void escapeLineDirectivePath( std::ostream &out, char *path )
117 {
118         for ( char *pc = path; *pc != 0; pc++ ) {
119                 if ( *pc == '\\' )
120                         out << "\\\\";
121                 else
122                         out << *pc;
123         }
124 }
125
126 /* Main, process args and call yyparse to start scanning input. */
127 int main(int argc, char **argv)
128 {
129         ParamCheck pc("o:nmleabjkS:M:CDJvHh?-:s", argc, argv);
130         char *inputFileName = 0;
131
132         while ( pc.check() ) {
133                 switch ( pc.state ) {
134                 case ParamCheck::match:
135                         switch ( pc.parameter ) {
136                         /* Output. */
137                         case 'o':
138                                 if ( *pc.parameterArg == 0 )
139                                         error() << "a zero length output file name was given" << endl;
140                                 else if ( outputFileName != 0 )
141                                         error() << "more than one output file name was given" << endl;
142                                 else {
143                                         /* Ok, remember the output file name. */
144                                         outputFileName = pc.parameterArg;
145                                 }
146                                 break;
147
148                         /* Minimization, mostly hidden options. */
149                         case 'n':
150                                 minimizeOpt = MinimizeNone;
151                                 break;
152                         case 'm':
153                                 minimizeOpt = MinimizeEnd;
154                                 break;
155                         case 'l':
156                                 minimizeOpt = MinimizeMostOps;
157                                 break;
158                         case 'e':
159                                 minimizeOpt = MinimizeEveryOp;
160                                 break;
161                         case 'a':
162                                 minimizeLevel = MinimizeApprox;
163                                 break;
164                         case 'b':
165                                 minimizeLevel = MinimizeStable;
166                                 break;
167                         case 'j':
168                                 minimizeLevel = MinimizePartition1;
169                                 break;
170                         case 'k':
171                                 minimizeLevel = MinimizePartition2;
172                                 break;
173
174                         /* Machine spec. */
175                         case 'S':
176                                 if ( *pc.parameterArg == 0 )
177                                         error() << "please specify an argument to -S" << endl;
178                                 else if ( machineSpec != 0 )
179                                         error() << "more than one -S argument was given" << endl;
180                                 else {
181                                         /* Ok, remember the path to the machine to generate. */
182                                         machineSpec = pc.parameterArg;
183                                 }
184                                 break;
185
186                         /* Machine path. */
187                         case 'M':
188                                 if ( *pc.parameterArg == 0 )
189                                         error() << "please specify an argument to -M" << endl;
190                                 else if ( machineName != 0 )
191                                         error() << "more than one -M argument was given" << endl;
192                                 else {
193                                         /* Ok, remember the machine name to generate. */
194                                         machineName = pc.parameterArg;
195                                 }
196                                 break;
197
198                         /* Host language types. */
199                         case 'C':
200                                 hostLangType = CCode;
201                                 hostLang = &hostLangC;
202                                 break;
203                         case 'D':
204                                 hostLangType = DCode;
205                                 hostLang = &hostLangD;
206                                 break;
207                         case 'J':
208                                 hostLangType = JavaCode;
209                                 hostLang = &hostLangJava;
210                                 break;
211
212                         /* Version and help. */
213                         case 'v':
214                                 version();
215                                 exit(0);
216                         case 'H': case 'h': case '?':
217                                 usage();
218                                 exit(0);
219                         case 's':
220                                 printStatistics = true;
221                                 break;
222                         case '-':
223                                 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
224                                         usage();
225                                         exit(0);
226                                 }
227                                 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
228                                         version();
229                                         exit(0);
230                                 }
231                                 else {
232                                         error() << "--" << pc.parameterArg << 
233                                                         " is an invalid argument" << endl;
234                                 }
235                         }
236                         break;
237
238                 case ParamCheck::invalid:
239                         error() << "-" << pc.parameter << " is an invalid argument" << endl;
240                         break;
241
242                 case ParamCheck::noparam:
243                         /* It is interpreted as an input file. */
244                         if ( *pc.curArg == 0 )
245                                 error() << "a zero length input file name was given" << endl;
246                         else if ( inputFileName != 0 )
247                                 error() << "more than one input file name was given" << endl;
248                         else {
249                                 /* OK, Remember the filename. */
250                                 inputFileName = pc.curArg;
251                         }
252                         break;
253                 }
254         }
255
256         /* Bail on above errors. */
257         if ( gblErrorCount > 0 )
258                 exit(1);
259
260         /* Make sure we are not writing to the same file as the input file. */
261         if ( inputFileName != 0 && outputFileName != 0 && 
262                         strcmp( inputFileName, outputFileName  ) == 0 )
263         {
264                 error() << "output file \"" << outputFileName  << 
265                                 "\" is the same as the input file" << endl;
266         }
267
268         /* Open the input file for reading. */
269         if ( inputFileName != 0 ) {
270                 /* Open the input file for reading. */
271                 ifstream *inFile = new ifstream( inputFileName );
272                 inStream = inFile;
273                 if ( ! inFile->is_open() )
274                         error() << "could not open " << inputFileName << " for reading" << endl;
275         }
276         else {
277                 inputFileName = "<stdin>";
278                 inStream = &cin;
279         }
280
281
282         /* Bail on above errors. */
283         if ( gblErrorCount > 0 )
284                 exit(1);
285
286         std::ostringstream outputBuffer;
287         outStream = &outputBuffer;
288
289         if ( machineSpec == 0 && machineName == 0 )
290                 *outStream << "<host line=\"1\" col=\"1\">";
291
292         scan( inputFileName, *inStream );
293
294         /* Finished, final check for errors.. */
295         if ( gblErrorCount > 0 )
296                 return 1;
297         
298         /* Now send EOF to all parsers. */
299         terminateAllParsers();
300
301         /* Finished, final check for errors.. */
302         if ( gblErrorCount > 0 )
303                 return 1;
304
305         if ( machineSpec == 0 && machineName == 0 )
306                 *outStream << "</host>\n";
307
308         checkMachines();
309
310         if ( gblErrorCount > 0 )
311                 return 1;
312         
313         ostream *outputFile = 0;
314         if ( outputFileName != 0 )
315                 outputFile = new ofstream( outputFileName );
316         else
317                 outputFile = &cout;
318
319         /* Write the machines, then the surrounding code. */
320         writeMachines( *outputFile, outputBuffer.str(), inputFileName );
321
322         if ( outputFileName != 0 )
323                 delete outputFile;
324
325         return 0;
326 }