11b96596fbf213ce548f59f20e0b206728d41742
[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 /* If any forward slash is found in argv0 then it is assumed that the path is
131  * explicit and the path to the backend executable should be derived from
132  * that. If no forward slash is found it is assumed the file is being run from
133  * the installed location. The PREFIX supplied during configuration is used.
134  * */
135 char **makePathChecks( const char *argv0, const char *progName )
136 {
137         char **result = new char*[3];
138         const char *lastSlash = strrchr( argv0, '/' );
139         int numChecks = 0;
140
141         if ( lastSlash != 0 ) {
142                 char *path = strdup( argv0 );
143                 int givenPathLen = (lastSlash - argv0) + 1;
144                 path[givenPathLen] = 0;
145
146                 int progNameLen = strlen(progName);
147                 int length = givenPathLen + progNameLen + 1;
148                 char *check = new char[length];
149                 sprintf( check, "%s%s", path, progName );
150                 result[numChecks++] = check;
151
152                 length = givenPathLen + 3 + progNameLen + 1 + progNameLen + 1;
153                 check = new char[length];
154                 sprintf( check, "%s../%s/%s", path, progName, progName );
155                 result[numChecks++] = check;
156         }
157         else {
158                 int prefixLen = strlen(PREFIX);
159                 int progNameLen = strlen(progName);
160                 int length = prefixLen + 5 + progNameLen + 1;
161                 char *check = new char[length];
162
163                 sprintf( check, PREFIX "/bin/%s", progName );
164                 result[numChecks++] = check;
165         }
166
167         result[numChecks] = 0;
168         return result;
169 }
170
171
172 void execBackend( const char *argv0 )
173 {
174         /* Locate the backend program */
175         const char *progName = 0;
176         switch ( hostLang->lang ) {
177                 case HostLang::C:
178                 case HostLang::D:
179                         progName = "rlgen-cd";
180                         break;
181                 case HostLang::Java:
182                         progName = "rlgen-java";
183                         break;
184                 case HostLang::Ruby:
185                         progName = "rlgen-ruby";
186                         break;
187         }
188
189         char **pathChecks = makePathChecks( argv0, progName );
190         while ( *pathChecks != 0 ) {
191                 pathChecks += 1;
192         }
193 }
194
195 /* Main, process args and call yyparse to start scanning input. */
196 int main(int argc, char **argv)
197 {
198         ParamCheck pc("o:nmleabjkS:M:CDJRvHh?-:s", argc, argv);
199         char *inputFileName = 0;
200         char *outputFileName = 0;
201
202         while ( pc.check() ) {
203                 switch ( pc.state ) {
204                 case ParamCheck::match:
205                         switch ( pc.parameter ) {
206                         /* Output. */
207                         case 'o':
208                                 if ( *pc.parameterArg == 0 )
209                                         error() << "a zero length output file name was given" << endl;
210                                 else if ( outputFileName != 0 )
211                                         error() << "more than one output file name was given" << endl;
212                                 else {
213                                         /* Ok, remember the output file name. */
214                                         outputFileName = pc.parameterArg;
215                                 }
216                                 break;
217
218                         /* Minimization, mostly hidden options. */
219                         case 'n':
220                                 minimizeOpt = MinimizeNone;
221                                 break;
222                         case 'm':
223                                 minimizeOpt = MinimizeEnd;
224                                 break;
225                         case 'l':
226                                 minimizeOpt = MinimizeMostOps;
227                                 break;
228                         case 'e':
229                                 minimizeOpt = MinimizeEveryOp;
230                                 break;
231                         case 'a':
232                                 minimizeLevel = MinimizeApprox;
233                                 break;
234                         case 'b':
235                                 minimizeLevel = MinimizeStable;
236                                 break;
237                         case 'j':
238                                 minimizeLevel = MinimizePartition1;
239                                 break;
240                         case 'k':
241                                 minimizeLevel = MinimizePartition2;
242                                 break;
243
244                         /* Machine spec. */
245                         case 'S':
246                                 if ( *pc.parameterArg == 0 )
247                                         error() << "please specify an argument to -S" << endl;
248                                 else if ( machineSpec != 0 )
249                                         error() << "more than one -S argument was given" << endl;
250                                 else {
251                                         /* Ok, remember the path to the machine to generate. */
252                                         machineSpec = pc.parameterArg;
253                                 }
254                                 break;
255
256                         /* Machine path. */
257                         case 'M':
258                                 if ( *pc.parameterArg == 0 )
259                                         error() << "please specify an argument to -M" << endl;
260                                 else if ( machineName != 0 )
261                                         error() << "more than one -M argument was given" << endl;
262                                 else {
263                                         /* Ok, remember the machine name to generate. */
264                                         machineName = pc.parameterArg;
265                                 }
266                                 break;
267
268                         /* Host language types. */
269                         case 'C':
270                                 hostLang = &hostLangC;
271                                 break;
272                         case 'D':
273                                 hostLang = &hostLangD;
274                                 break;
275                         case 'J':
276                                 hostLang = &hostLangJava;
277                                 break;
278                         case 'R':
279                                 hostLang = &hostLangRuby;
280                                 break;
281
282                         /* Version and help. */
283                         case 'v':
284                                 version();
285                                 exit(0);
286                         case 'H': case 'h': case '?':
287                                 usage();
288                                 exit(0);
289                         case 's':
290                                 printStatistics = true;
291                                 break;
292                         case '-':
293                                 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
294                                         usage();
295                                         exit(0);
296                                 }
297                                 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
298                                         version();
299                                         exit(0);
300                                 }
301                                 else {
302                                         error() << "--" << pc.parameterArg << 
303                                                         " is an invalid argument" << endl;
304                                 }
305                         }
306                         break;
307
308                 case ParamCheck::invalid:
309                         error() << "-" << pc.parameter << " is an invalid argument" << endl;
310                         break;
311
312                 case ParamCheck::noparam:
313                         /* It is interpreted as an input file. */
314                         if ( *pc.curArg == 0 )
315                                 error() << "a zero length input file name was given" << endl;
316                         else if ( inputFileName != 0 )
317                                 error() << "more than one input file name was given" << endl;
318                         else {
319                                 /* OK, Remember the filename. */
320                                 inputFileName = pc.curArg;
321                         }
322                         break;
323                 }
324         }
325
326         /* Bail on above errors. */
327         if ( gblErrorCount > 0 )
328                 exit(1);
329
330         /* Make sure we are not writing to the same file as the input file. */
331         if ( inputFileName != 0 && outputFileName != 0 && 
332                         strcmp( inputFileName, outputFileName  ) == 0 )
333         {
334                 error() << "output file \"" << outputFileName  << 
335                                 "\" is the same as the input file" << endl;
336         }
337
338         /* Open the input file for reading. */
339         istream *inStream;
340         if ( inputFileName != 0 ) {
341                 /* Open the input file for reading. */
342                 ifstream *inFile = new ifstream( inputFileName );
343                 inStream = inFile;
344                 if ( ! inFile->is_open() )
345                         error() << "could not open " << inputFileName << " for reading" << endl;
346         }
347         else {
348                 inputFileName = "<stdin>";
349                 inStream = &cin;
350         }
351
352
353         /* Bail on above errors. */
354         if ( gblErrorCount > 0 )
355                 exit(1);
356
357         std::ostringstream outputBuffer;
358
359         if ( machineSpec == 0 && machineName == 0 )
360                 outputBuffer << "<host line=\"1\" col=\"1\">";
361
362         Scanner scanner( inputFileName, *inStream, outputBuffer, 0, 0, 0, false );
363         scanner.do_scan();
364
365         /* Finished, final check for errors.. */
366         if ( gblErrorCount > 0 )
367                 return 1;
368         
369         /* Now send EOF to all parsers. */
370         terminateAllParsers();
371
372         /* Finished, final check for errors.. */
373         if ( gblErrorCount > 0 )
374                 return 1;
375
376         if ( machineSpec == 0 && machineName == 0 )
377                 outputBuffer << "</host>\n";
378
379         if ( gblErrorCount > 0 )
380                 return 1;
381         
382         ostream *outputFile = 0;
383         if ( outputFileName != 0 )
384                 outputFile = new ofstream( outputFileName );
385         else
386                 outputFile = &cout;
387
388         /* Write the machines, then the surrounding code. */
389         writeMachines( *outputFile, outputBuffer.str(), inputFileName );
390
391         if ( outputFileName != 0 )
392                 delete outputFile;
393
394         execBackend( argv[0] );
395
396         return 0;
397 }