Removed arg passing from frontend to backend functions.
[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 #include <unistd.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <errno.h>
34
35 #ifdef _WIN32
36 #include <windows.h>
37 #include <psapi.h>
38 #include <time.h>
39 #include <io.h>
40 #include <process.h>
41
42 #if _MSC_VER
43 #define S_IRUSR _S_IREAD
44 #define S_IWUSR _S_IWRITE
45 #endif
46 #endif
47
48 /* Parsing. */
49 #include "ragel.h"
50 #include "rlscan.h"
51
52 /* Parameters and output. */
53 #include "pcheck.h"
54 #include "vector.h"
55 #include "version.h"
56 #include "common.h"
57
58 using std::istream;
59 using std::ostream;
60 using std::ifstream;
61 using std::ofstream;
62 using std::cin;
63 using std::cout;
64 using std::cerr;
65 using std::endl;
66 using std::ios;
67 using std::streamsize;
68
69 /* Controls minimization. */
70 MinimizeLevel minimizeLevel = MinimizePartition2;
71 MinimizeOpt minimizeOpt = MinimizeMostOps;
72
73 /* Graphviz dot file generation. */
74 const char *machineSpec = 0, *machineName = 0;
75 bool machineSpecFound = false;
76 bool wantDupsRemoved = true;
77
78 bool printStatistics = false;
79 bool frontendOnly = false;
80 bool generateDot = false;
81
82 /* Target language and output style. */
83 CodeStyleEnum codeStyle = GenTables;
84
85 int numSplitPartitions = 0;
86 bool noLineDirectives = false;
87 bool displayPrintables = false;
88
89 /* Target ruby impl */
90 RubyImplEnum rubyImpl = MRI;
91
92 ArgsVector includePaths;
93
94 const char *outputFileName = 0;
95
96 /* Print a summary of the options. */
97 void usage()
98 {
99         cout <<
100 "usage: ragel [options] file\n"
101 "general:\n"
102 "   -h, -H, -?, --help   Print this usage and exit\n"
103 "   -v, --version        Print version information and exit\n"
104 "   -o <file>            Write output to <file>\n"
105 "   -s                   Print some statistics on stderr\n"
106 "   -d                   Do not remove duplicates from action lists\n"
107 "   -I <dir>             Add <dir> to the list of directories to search\n"
108 "                        for included an imported files\n"
109 "error reporting format:\n"
110 "   --error-format=gnu   file:line:column: message (default)\n"
111 "   --error-format=msvc  file(line,column): message\n"
112 "fsm minimization:\n"
113 "   -n                   Do not perform minimization\n"
114 "   -m                   Minimize at the end of the compilation\n"
115 "   -l                   Minimize after most operations (default)\n"
116 "   -e                   Minimize after every operation\n"
117 "visualization:\n"
118 "   -x                   Run the frontend only: emit XML intermediate format\n"
119 "   -V                   Generate a dot file for Graphviz\n"
120 "   -p                   Display printable characters on labels\n"
121 "   -S <spec>            FSM specification to output (for rlgen-dot)\n"
122 "   -M <machine>         Machine definition/instantiation to output (for rlgen-dot)\n"
123 "host language:\n"
124 "   -C                   The host language is C, C++, Obj-C or Obj-C++ (default)\n"
125 "   -D                   The host language is D\n"
126 "   -J                   The host language is Java\n"
127 "   -R                   The host language is Ruby\n"
128 "   -A                   The host language is C#\n"
129 "line direcives: (C/D/C# only)\n"
130 "   -L                   Inhibit writing of #line directives\n"
131 "code style: (C/Ruby/C# only)\n"
132 "   -T0                  Table driven FSM (default)\n"
133 "   -T1                  Faster table driven FSM\n"
134 "   -F0                  Flat table driven FSM\n"
135 "   -F1                  Faster flat table-driven FSM\n"
136 "code style: (C/C# only)\n"
137 "   -G0                  Goto-driven FSM\n"
138 "   -G1                  Faster goto-driven FSM\n"
139 "code style: (C only)\n"
140 "   -G2                  Really fast goto-driven FSM\n"
141 "   -P<N>                N-Way Split really fast goto-driven FSM\n"
142         ;       
143
144         exit(0);
145 }
146
147 /* Print version information and exit. */
148 void version()
149 {
150         cout << "Ragel State Machine Compiler version " VERSION << " " PUBDATE << endl <<
151                         "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
152         exit(0);
153 }
154
155 /* Error reporting format. */
156 ErrorFormat errorFormat = ErrorFormatGNU;
157
158 InputLoc makeInputLoc( const char *fileName, int line, int col)
159 {
160         InputLoc loc = { fileName, line, col };
161         return loc;
162 }
163
164 ostream &operator<<( ostream &out, const InputLoc &loc )
165 {
166         assert( loc.fileName != 0 );
167         switch ( errorFormat ) {
168         case ErrorFormatMSVC:
169                 out << loc.fileName << "(" << loc.line;
170                 if ( loc.col )
171                         out << "," << loc.col;
172                 out << ")";
173                 break;
174
175         default:
176                 out << loc.fileName << ":" << loc.line;
177                 if ( loc.col )
178                         out << ":" << loc.col;
179                 break;
180         }
181         return out;
182 }
183
184 /* Total error count. */
185 int gblErrorCount = 0;
186
187 /* Print the opening to a warning in the input, then return the error ostream. */
188 ostream &warning( const InputLoc &loc )
189 {
190         cerr << loc << ": warning: ";
191         return cerr;
192 }
193
194 /* Print the opening to a program error, then return the error stream. */
195 ostream &error()
196 {
197         gblErrorCount += 1;
198         cerr << PROGNAME ": ";
199         return cerr;
200 }
201
202 ostream &error( const InputLoc &loc )
203 {
204         gblErrorCount += 1;
205         cerr << loc << ": ";
206         return cerr;
207 }
208
209 void escapeLineDirectivePath( std::ostream &out, char *path )
210 {
211         for ( char *pc = path; *pc != 0; pc++ ) {
212                 if ( *pc == '\\' )
213                         out << "\\\\";
214                 else
215                         out << *pc;
216         }
217 }
218
219 void processArgs( int argc, const char **argv, const char *&inputFileName )
220 {
221         ParamCheck pc("xo:dnmleabjkS:M:I:CDJRAvHh?-:sT:F:G:P:LpV", argc, argv);
222
223         /* FIXME: Need to check code styles VS langauge. */
224
225         while ( pc.check() ) {
226                 switch ( pc.state ) {
227                 case ParamCheck::match:
228                         switch ( pc.parameter ) {
229                         case 'V':
230                                 generateDot = true;
231                                 break;
232
233                         case 'x':
234                                 frontendOnly = true;
235                                 break;
236
237                         /* Output. */
238                         case 'o':
239                                 if ( *pc.paramArg == 0 )
240                                         error() << "a zero length output file name was given" << endl;
241                                 else if ( outputFileName != 0 )
242                                         error() << "more than one output file name was given" << endl;
243                                 else {
244                                         /* Ok, remember the output file name. */
245                                         outputFileName = pc.paramArg;
246                                 }
247                                 break;
248
249                         /* Flag for turning off duplicate action removal. */
250                         case 'd':
251                                 wantDupsRemoved = false;
252                                 break;
253
254                         /* Minimization, mostly hidden options. */
255                         case 'n':
256                                 minimizeOpt = MinimizeNone;
257                                 break;
258                         case 'm':
259                                 minimizeOpt = MinimizeEnd;
260                                 break;
261                         case 'l':
262                                 minimizeOpt = MinimizeMostOps;
263                                 break;
264                         case 'e':
265                                 minimizeOpt = MinimizeEveryOp;
266                                 break;
267                         case 'a':
268                                 minimizeLevel = MinimizeApprox;
269                                 break;
270                         case 'b':
271                                 minimizeLevel = MinimizeStable;
272                                 break;
273                         case 'j':
274                                 minimizeLevel = MinimizePartition1;
275                                 break;
276                         case 'k':
277                                 minimizeLevel = MinimizePartition2;
278                                 break;
279
280                         /* Machine spec. */
281                         case 'S':
282                                 if ( *pc.paramArg == 0 )
283                                         error() << "please specify an argument to -S" << endl;
284                                 else if ( machineSpec != 0 )
285                                         error() << "more than one -S argument was given" << endl;
286                                 else {
287                                         /* Ok, remember the path to the machine to generate. */
288                                         machineSpec = pc.paramArg;
289                                 }
290                                 break;
291
292                         /* Machine path. */
293                         case 'M':
294                                 if ( *pc.paramArg == 0 )
295                                         error() << "please specify an argument to -M" << endl;
296                                 else if ( machineName != 0 )
297                                         error() << "more than one -M argument was given" << endl;
298                                 else {
299                                         /* Ok, remember the machine name to generate. */
300                                         machineName = pc.paramArg;
301                                 }
302                                 break;
303
304                         case 'I':
305                                 if ( *pc.paramArg == 0 )
306                                         error() << "please specify an argument to -I" << endl;
307                                 else {
308                                         includePaths.append( pc.paramArg );
309                                 }
310                                 break;
311
312                         /* Host language types. */
313                         case 'C':
314                                 hostLang = &hostLangC;
315                                 break;
316                         case 'D':
317                                 hostLang = &hostLangD;
318                                 break;
319                         case 'J':
320                                 hostLang = &hostLangJava;
321                                 break;
322                         case 'R':
323                                 hostLang = &hostLangRuby;
324                                 break;
325                         case 'A':
326                                 hostLang = &hostLangCSharp;
327                                 break;
328
329                         /* Version and help. */
330                         case 'v':
331                                 version();
332                                 break;
333                         case 'H': case 'h': case '?':
334                                 usage();
335                                 break;
336                         case 's':
337                                 printStatistics = true;
338                                 break;
339                         case '-': {
340                                 char *eq = strchr( pc.paramArg, '=' );
341
342                                 if ( eq != 0 )
343                                         *eq++ = 0;
344
345                                 if ( strcmp( pc.paramArg, "help" ) == 0 )
346                                         usage();
347                                 else if ( strcmp( pc.paramArg, "version" ) == 0 )
348                                         version();
349                                 else if ( strcmp( pc.paramArg, "error-format" ) == 0 ) {
350                                         if ( eq == 0 )
351                                                 error() << "expecting '=value' for error-format" << endl;
352                                         else if ( strcmp( eq, "gnu" ) == 0 )
353                                                 errorFormat = ErrorFormatGNU;
354                                         else if ( strcmp( eq, "msvc" ) == 0 )
355                                                 errorFormat = ErrorFormatMSVC;
356                                         else
357                                                 error() << "invalid value for error-format" << endl;
358                                 }
359                                 else if ( strcmp( pc.paramArg, "rbx" ) == 0 )
360                                         rubyImpl = Rubinius;
361                                 else {
362                                         error() << "--" << pc.paramArg << 
363                                                         " is an invalid argument" << endl;
364                                 }
365                                 break;
366                         }
367
368                         /* Passthrough args. */
369                         case 'T': 
370                                 if ( pc.paramArg[0] == '0' )
371                                         codeStyle = GenTables;
372                                 else if ( pc.paramArg[0] == '1' )
373                                         codeStyle = GenFTables;
374                                 else {
375                                         error() << "-T" << pc.paramArg[0] << 
376                                                         " is an invalid argument" << endl;
377                                         exit(1);
378                                 }
379                                 break;
380                         case 'F': 
381                                 if ( pc.paramArg[0] == '0' )
382                                         codeStyle = GenFlat;
383                                 else if ( pc.paramArg[0] == '1' )
384                                         codeStyle = GenFFlat;
385                                 else {
386                                         error() << "-F" << pc.paramArg[0] << 
387                                                         " is an invalid argument" << endl;
388                                         exit(1);
389                                 }
390                                 break;
391                         case 'G': 
392                                 if ( pc.paramArg[0] == '0' )
393                                         codeStyle = GenGoto;
394                                 else if ( pc.paramArg[0] == '1' )
395                                         codeStyle = GenFGoto;
396                                 else if ( pc.paramArg[0] == '2' )
397                                         codeStyle = GenIpGoto;
398                                 else {
399                                         error() << "-G" << pc.paramArg[0] << 
400                                                         " is an invalid argument" << endl;
401                                         exit(1);
402                                 }
403                                 break;
404                         case 'P':
405                                 codeStyle = GenSplit;
406                                 numSplitPartitions = atoi( pc.paramArg );
407                                 break;
408
409                         case 'p':
410                                 displayPrintables = true;
411                                 break;
412
413                         case 'L':
414                                 noLineDirectives = true;
415                                 break;
416                         }
417                         break;
418
419                 case ParamCheck::invalid:
420                         error() << "-" << pc.parameter << " is an invalid argument" << endl;
421                         break;
422
423                 case ParamCheck::noparam:
424                         /* It is interpreted as an input file. */
425                         if ( *pc.curArg == 0 )
426                                 error() << "a zero length input file name was given" << endl;
427                         else if ( inputFileName != 0 )
428                                 error() << "more than one input file name was given" << endl;
429                         else {
430                                 /* OK, Remember the filename. */
431                                 inputFileName = pc.curArg;
432                         }
433                         break;
434                 }
435         }
436 }
437
438 int frontend( const char *inputFileName, const char *intermed )
439 {
440         /* Open the input file for reading. */
441         assert( inputFileName != 0 );
442         ifstream *inFile = new ifstream( inputFileName );
443         istream *inStream = inFile;
444         if ( ! inFile->is_open() )
445                 error() << "could not open " << inputFileName << " for reading" << endp;
446
447         /* Used for just a few things. */
448         std::ostringstream hostData;
449
450         if ( machineSpec == 0 && machineName == 0 )
451                 hostData << "<host line=\"1\" col=\"1\">";
452
453         Scanner scanner( inputFileName, *inStream, hostData, 0, 0, 0, false );
454         scanner.do_scan();
455
456         /* Finished, final check for errors.. */
457         if ( gblErrorCount > 0 )
458                 return 1;
459         
460         /* Now send EOF to all parsers. */
461         terminateAllParsers();
462
463         /* Finished, final check for errors.. */
464         if ( gblErrorCount > 0 )
465                 return 1;
466
467         if ( machineSpec == 0 && machineName == 0 )
468                 hostData << "</host>\n";
469
470         if ( gblErrorCount > 0 )
471                 return 1;
472         
473         ostream *outputFile = new ofstream( intermed );
474
475         /* Write the machines, then the surrounding code. */
476         writeMachines( *outputFile, hostData.str(), inputFileName );
477
478         /* Close the intermediate file. */
479         delete outputFile;
480
481         return gblErrorCount > 0;
482 }
483
484 char *makeIntermedTemplate( const char *baseFileName )
485 {
486         char *result = 0;
487         const char *templ = "ragel-XXXXXX.xml";
488         char *lastSlash = strrchr( baseFileName, '/' );
489         if ( lastSlash == 0 ) {
490                 result = new char[strlen(templ)+1];
491                 strcpy( result, templ );
492         }
493         else {
494                 int baseLen = lastSlash - baseFileName + 1;
495                 result = new char[baseLen + strlen(templ) + 1];
496                 memcpy( result, baseFileName, baseLen );
497                 strcpy( result+baseLen, templ );
498         }
499         return result;
500 };
501
502 const char *openIntermed( const char *inputFileName, const char *outputFileName )
503 {
504         srand(time(0));
505         const char *result = 0;
506
507         /* Which filename do we use as the base? */
508         const char *baseFileName = outputFileName != 0 ? outputFileName : inputFileName;
509
510         /* The template for the intermediate file name. */
511         const char *intermedFileName = makeIntermedTemplate( baseFileName );
512
513         /* Randomize the name and try to open. */
514         char fnChars[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
515         char *firstX = strrchr( intermedFileName, 'X' ) - 5;
516         for ( int tries = 0; tries < 20; tries++ ) {
517                 /* Choose a random name. */
518                 for ( int x = 0; x < 6; x++ )
519                         firstX[x] = fnChars[rand() % 52];
520
521                 /* Try to open the file. */
522                 int fd = ::open( intermedFileName, O_WRONLY|O_EXCL|O_CREAT, S_IRUSR|S_IWUSR );
523
524                 if ( fd > 0 ) {
525                         /* Success. Close the file immediately and return the name for use
526                          * by the child processes. */
527                         ::close( fd );
528                         result = intermedFileName;
529                         break;
530                 }
531
532                 if ( errno == EACCES ) {
533                         error() << "failed to open temp file " << intermedFileName << 
534                                         ", access denied" << endp;
535                 }
536         }
537
538         if ( result == 0 )
539                 error() << "abnormal error: cannot find unique name for temp file" << endp;
540
541         return result;
542 }
543
544
545 void cleanExit( const char *intermed, int status )
546 {
547         unlink( intermed );
548         exit( status );
549 }
550
551 int cd_main( const char *xmlInputFileName );
552 int java_main( const char *xmlInputFileName );
553 int ruby_main( const char *xmlInputFileName );
554 int csharp_main( const char *xmlInputFileName );
555 int dot_main( const char *xmlInputFileName );
556
557 void backend( const char *intermed )
558 {
559         /* Locate the backend program */
560         if ( generateDot )
561                 dot_main( intermed );
562         else {
563                 switch ( hostLang->lang ) {
564                         case HostLang::C:
565                         case HostLang::D:
566                                 cd_main( intermed );
567                                 break;
568                         case HostLang::Java:
569                                 java_main( intermed );
570                                 break;
571                         case HostLang::Ruby:
572                                 ruby_main( intermed );
573                                 break;
574                         case HostLang::CSharp:
575                                 csharp_main( intermed );
576                                 break;
577                 }
578         }
579 }
580
581 /* Main, process args and call yyparse to start scanning input. */
582 int main( int argc, const char **argv )
583 {
584         const char *inputFileName = 0;
585         processArgs( argc, argv, inputFileName );
586
587         /* If -M or -S are given and we're not generating a dot file then invoke
588          * the frontend. These options are not useful with code generators. */
589         if ( machineName != 0 || machineSpec != 0 ) {
590                 if ( !generateDot )
591                         frontendOnly = true;
592         }
593
594         /* Require an input file. If we use standard in then we won't have a file
595          * name on which to base the output. */
596         if ( inputFileName == 0 )
597                 error() << "no input file given" << endl;
598
599         /* Bail on argument processing errors. */
600         if ( gblErrorCount > 0 )
601                 exit(1);
602
603         /* Make sure we are not writing to the same file as the input file. */
604         if ( inputFileName != 0 && outputFileName != 0 && 
605                         strcmp( inputFileName, outputFileName  ) == 0 )
606         {
607                 error() << "output file \"" << outputFileName  << 
608                                 "\" is the same as the input file" << endp;
609         }
610
611         const char *intermed = openIntermed( inputFileName, outputFileName );
612         frontend( inputFileName, intermed );
613         backend( intermed );
614
615         /* Clean up the intermediate. */
616         cleanExit( intermed, 0 );
617
618         return 0;
619 }