Need to unset the final state status of the start state in kleene star.
[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 #ifndef WIN32
36 #include <sys/wait.h>
37 #else
38 #include <windows.h>
39 #include <psapi.h>
40 #endif
41
42 /* Parsing. */
43 #include "ragel.h"
44 #include "rlscan.h"
45
46 /* Parameters and output. */
47 #include "pcheck.h"
48 #include "vector.h"
49 #include "version.h"
50 #include "common.h"
51
52 using std::istream;
53 using std::ostream;
54 using std::ifstream;
55 using std::ofstream;
56 using std::cin;
57 using std::cout;
58 using std::cerr;
59 using std::endl;
60 using std::ios;
61 using std::streamsize;
62
63 /* Controls minimization. */
64 MinimizeLevel minimizeLevel = MinimizePartition2;
65 MinimizeOpt minimizeOpt = MinimizeMostOps;
66
67 /* Graphviz dot file generation. */
68 char *machineSpec = 0, *machineName = 0;
69 bool machineSpecFound = false;
70 bool wantDupsRemoved = true;
71
72 bool printStatistics = false;
73 bool frontendOnly = false;
74 bool generateDot = false;
75
76 typedef Vector<const char *> ArgsVector;
77 ArgsVector frontendArgs;
78 ArgsVector backendArgs;
79
80 /* Print a summary of the options. */
81 void usage()
82 {
83         cout <<
84 "usage: ragel [options] file\n"
85 "general:\n"
86 "   -h, -H, -?, --help   Print this usage and exit\n"
87 "   -v, --version        Print version information and exit\n"
88 "   -o <file>            Write output to <file>\n"
89 "   -s                   Print some statistics on stderr\n"
90 "fsm minimization:\n"
91 "   -n                   Do not perform minimization\n"
92 "   -m                   Minimize at the end of the compilation\n"
93 "   -l                   Minimize after most operations (default)\n"
94 "   -e                   Minimize after every operation\n"
95 "visualization:\n"
96 "   -x                   Run the frontend only: emit XML intermediate format\n"
97 "   -V                   Generate a dot file for Graphviz\n"
98 "   -p                   Display printable characters on labels\n"
99 "   -S <spec>            FSM specification to output (for rlgen-dot)\n"
100 "   -M <machine>         Machine definition/instantiation to output (for rlgen-dot)\n"
101 "host language:\n"
102 "   -C                   The host language is C, C++, Obj-C or Obj-C++ (default)\n"
103 "   -D                   The host language is D\n"
104 "   -J                   The host language is Java\n"
105 "   -R                   The host language is Ruby\n"
106 "line direcives: (C/D only)\n"
107 "   -L                   Inhibit writing of #line directives\n"
108 "code style: (C/Ruby only)\n"
109 "   -T0                  Table driven FSM (default)\n"
110 "   -T1                  Faster table driven FSM\n"
111 "   -F0                  Flat table driven FSM\n"
112 "   -F1                  Faster flat table-driven FSM\n"
113 "code style: (C only)\n"
114 "   -G0                  Goto-driven FSM\n"
115 "   -G1                  Faster goto-driven FSM\n"
116 "   -G2                  Really fast goto-driven FSM\n"
117 "   -P<N>                N-Way Split really fast goto-driven FSM\n"
118         ;       
119 }
120
121 /* Print version information. */
122 void version()
123 {
124         cout << "Ragel State Machine Compiler version " VERSION << " " PUBDATE << endl <<
125                         "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
126 }
127
128 /* Total error count. */
129 int gblErrorCount = 0;
130
131 /* Print the opening to a warning in the input, then return the error ostream. */
132 ostream &warning( const InputLoc &loc )
133 {
134         assert( loc.fileName != 0 );
135         cerr << loc.fileName << ":" << loc.line << ":" << 
136                         loc.col << ": warning: ";
137         return cerr;
138 }
139
140 /* Print the opening to a program error, then return the error stream. */
141 ostream &error()
142 {
143         gblErrorCount += 1;
144         cerr << PROGNAME ": ";
145         return cerr;
146 }
147
148 ostream &error( const InputLoc &loc )
149 {
150         gblErrorCount += 1;
151         assert( loc.fileName != 0 );
152         cerr << loc.fileName << ":" << loc.line << ": ";
153         return cerr;
154 }
155
156 void escapeLineDirectivePath( std::ostream &out, char *path )
157 {
158         for ( char *pc = path; *pc != 0; pc++ ) {
159                 if ( *pc == '\\' )
160                         out << "\\\\";
161                 else
162                         out << *pc;
163         }
164 }
165
166 void processArgs( int argc, char **argv, char *&inputFileName, char *&outputFileName )
167 {
168         ParamCheck pc("xo:dnmleabjkS:M:CDJRvHh?-:sT:F:G:P:LpV", argc, argv);
169
170         while ( pc.check() ) {
171                 switch ( pc.state ) {
172                 case ParamCheck::match:
173                         switch ( pc.parameter ) {
174                         case 'V':
175                                 generateDot = true;
176                                 break;
177
178                         case 'x':
179                                 frontendOnly = true;
180                                 break;
181
182                         /* Output. */
183                         case 'o':
184                                 if ( *pc.parameterArg == 0 )
185                                         error() << "a zero length output file name was given" << endl;
186                                 else if ( outputFileName != 0 )
187                                         error() << "more than one output file name was given" << endl;
188                                 else {
189                                         /* Ok, remember the output file name. */
190                                         outputFileName = pc.parameterArg;
191                                 }
192                                 break;
193
194                         /* Minimization, mostly hidden options. */
195                         case 'd':
196                                 wantDupsRemoved = false;
197                                 frontendArgs.append( "-d" );
198                                 break;
199
200                         /* Minimization, mostly hidden options. */
201                         case 'n':
202                                 minimizeOpt = MinimizeNone;
203                                 frontendArgs.append( "-n" );
204                                 break;
205                         case 'm':
206                                 minimizeOpt = MinimizeEnd;
207                                 frontendArgs.append( "-m" );
208                                 break;
209                         case 'l':
210                                 minimizeOpt = MinimizeMostOps;
211                                 frontendArgs.append( "-l" );
212                                 break;
213                         case 'e':
214                                 minimizeOpt = MinimizeEveryOp;
215                                 frontendArgs.append( "-e" );
216                                 break;
217                         case 'a':
218                                 minimizeLevel = MinimizeApprox;
219                                 frontendArgs.append( "-a" );
220                                 break;
221                         case 'b':
222                                 minimizeLevel = MinimizeStable;
223                                 frontendArgs.append( "-b" );
224                                 break;
225                         case 'j':
226                                 minimizeLevel = MinimizePartition1;
227                                 frontendArgs.append( "-j" );
228                                 break;
229                         case 'k':
230                                 minimizeLevel = MinimizePartition2;
231                                 frontendArgs.append( "-k" );
232                                 break;
233
234                         /* Machine spec. */
235                         case 'S':
236                                 if ( *pc.parameterArg == 0 )
237                                         error() << "please specify an argument to -S" << endl;
238                                 else if ( machineSpec != 0 )
239                                         error() << "more than one -S argument was given" << endl;
240                                 else {
241                                         /* Ok, remember the path to the machine to generate. */
242                                         machineSpec = pc.parameterArg;
243                                         frontendArgs.append( "-S" );
244                                         frontendArgs.append( pc.parameterArg );
245                                 }
246                                 break;
247
248                         /* Machine path. */
249                         case 'M':
250                                 if ( *pc.parameterArg == 0 )
251                                         error() << "please specify an argument to -M" << endl;
252                                 else if ( machineName != 0 )
253                                         error() << "more than one -M argument was given" << endl;
254                                 else {
255                                         /* Ok, remember the machine name to generate. */
256                                         machineName = pc.parameterArg;
257                                         frontendArgs.append( "-M" );
258                                         frontendArgs.append( pc.parameterArg );
259                                 }
260                                 break;
261
262                         /* Host language types. */
263                         case 'C':
264                                 hostLang = &hostLangC;
265                                 frontendArgs.append( "-C" );
266                                 break;
267                         case 'D':
268                                 hostLang = &hostLangD;
269                                 frontendArgs.append( "-D" );
270                                 break;
271                         case 'J':
272                                 hostLang = &hostLangJava;
273                                 frontendArgs.append( "-J" );
274                                 break;
275                         case 'R':
276                                 hostLang = &hostLangRuby;
277                                 frontendArgs.append( "-R" );
278                                 break;
279
280                         /* Version and help. */
281                         case 'v':
282                                 version();
283                                 exit(0);
284                         case 'H': case 'h': case '?':
285                                 usage();
286                                 exit(0);
287                         case 's':
288                                 printStatistics = true;
289                                 frontendArgs.append( "-s" );
290                                 break;
291                         case '-':
292                                 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
293                                         usage();
294                                         exit(0);
295                                 }
296                                 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
297                                         version();
298                                         exit(0);
299                                 }
300                                 else {
301                                         error() << "--" << pc.parameterArg << 
302                                                         " is an invalid argument" << endl;
303                                 }
304
305                         /* Passthrough args. */
306                         case 'T': 
307                                 backendArgs.append( "-T" );
308                                 backendArgs.append( pc.parameterArg );
309                                 break;
310                         case 'F': 
311                                 backendArgs.append( "-F" );
312                                 backendArgs.append( pc.parameterArg );
313                                 break;
314                         case 'G': 
315                                 backendArgs.append( "-G" );
316                                 backendArgs.append( pc.parameterArg );
317                                 break;
318                         case 'P':
319                                 backendArgs.append( "-P" );
320                                 backendArgs.append( pc.parameterArg );
321                                 break;
322                         case 'p':
323                                 backendArgs.append( "-p" );
324                                 break;
325                         case 'L':
326                                 backendArgs.append( "-L" );
327                                 break;
328                         }
329                         break;
330
331                 case ParamCheck::invalid:
332                         error() << "-" << pc.parameter << " is an invalid argument" << endl;
333                         break;
334
335                 case ParamCheck::noparam:
336                         /* It is interpreted as an input file. */
337                         if ( *pc.curArg == 0 )
338                                 error() << "a zero length input file name was given" << endl;
339                         else if ( inputFileName != 0 )
340                                 error() << "more than one input file name was given" << endl;
341                         else {
342                                 /* OK, Remember the filename. */
343                                 inputFileName = pc.curArg;
344                         }
345                         break;
346                 }
347         }
348 }
349
350 int frontend( char *inputFileName, char *outputFileName )
351 {
352         /* Open the input file for reading. */
353         assert( inputFileName != 0 );
354         ifstream *inFile = new ifstream( inputFileName );
355         istream *inStream = inFile;
356         if ( ! inFile->is_open() )
357                 error() << "could not open " << inputFileName << " for reading" << endp;
358
359         /* Used for just a few things. */
360         std::ostringstream hostData;
361
362         if ( machineSpec == 0 && machineName == 0 )
363                 hostData << "<host line=\"1\" col=\"1\">";
364
365         Scanner scanner( inputFileName, *inStream, hostData, 0, 0, 0, false );
366         scanner.do_scan();
367
368         /* Finished, final check for errors.. */
369         if ( gblErrorCount > 0 )
370                 return 1;
371         
372         /* Now send EOF to all parsers. */
373         terminateAllParsers();
374
375         /* Finished, final check for errors.. */
376         if ( gblErrorCount > 0 )
377                 return 1;
378
379         if ( machineSpec == 0 && machineName == 0 )
380                 hostData << "</host>\n";
381
382         if ( gblErrorCount > 0 )
383                 return 1;
384         
385         ostream *outputFile = 0;
386         if ( outputFileName != 0 )
387                 outputFile = new ofstream( outputFileName );
388         else
389                 outputFile = &cout;
390
391         /* Write the machines, then the surrounding code. */
392         writeMachines( *outputFile, hostData.str(), inputFileName );
393
394         /* Close the intermediate file. */
395         if ( outputFileName != 0 )
396                 delete outputFile;
397
398         return gblErrorCount > 0;
399 }
400
401 char *makeIntermedTemplate( char *baseFileName )
402 {
403         char *result = 0;
404         const char *templ = "ragel-XXXXXX.xml";
405         char *lastSlash = strrchr( baseFileName, '/' );
406         if ( lastSlash == 0 ) {
407                 result = new char[strlen(templ)+1];
408                 strcpy( result, templ );
409         }
410         else {
411                 int baseLen = lastSlash - baseFileName + 1;
412                 result = new char[baseLen + strlen(templ) + 1];
413                 memcpy( result, baseFileName, baseLen );
414                 strcpy( result+baseLen, templ );
415         }
416         return result;
417 };
418
419 char *openIntermed( char *inputFileName, char *outputFileName )
420 {
421         srand(time(0));
422         char *result = 0;
423
424         /* Which filename do we use as the base? */
425         char *baseFileName = outputFileName != 0 ? outputFileName : inputFileName;
426
427         /* The template for the intermediate file name. */
428         char *intermedFileName = makeIntermedTemplate( baseFileName );
429
430         /* Randomize the name and try to open. */
431         char fnChars[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
432         char *firstX = strrchr( intermedFileName, 'X' ) - 5;
433         for ( int tries = 0; tries < 20; tries++ ) {
434                 /* Choose a random name. */
435                 for ( int x = 0; x < 6; x++ )
436                         firstX[x] = fnChars[rand() % 52];
437
438                 /* Try to open the file. */
439                 int fd = ::open( intermedFileName, O_WRONLY|O_EXCL|O_CREAT, S_IRUSR|S_IWUSR );
440
441                 if ( fd > 0 ) {
442                         /* Success. Close the file immediately and return the name for use
443                          * by the child processes. */
444                         ::close( fd );
445                         result = intermedFileName;
446                         break;
447                 }
448
449                 if ( errno == EACCES ) {
450                         error() << "failed to open temp file " << intermedFileName << 
451                                         ", access denied" << endp;
452                 }
453         }
454
455         if ( result == 0 )
456                 error() << "abnormal error: cannot find unique name for temp file" << endp;
457
458         return result;
459 }
460
461
462 void cleanExit( char *intermed, int status )
463 {
464         unlink( intermed );
465         exit( status );
466 }
467
468 #ifndef WIN32
469
470 /* If any forward slash is found in argv0 then it is assumed that the path is
471  * explicit and the path to the backend executable should be derived from
472  * that. Whe check that location and also go up one then inside a directory of
473  * the same name in case we are executing from the source tree. If no forward
474  * slash is found it is assumed the file is being run from the installed
475  * location. The PREFIX supplied during configuration is used. */
476 char **makePathChecksUnix( const char *argv0, const char *progName )
477 {
478         char **result = new char*[3];
479         const char *lastSlash = strrchr( argv0, '/' );
480         int numChecks = 0;
481
482         if ( lastSlash != 0 ) {
483                 char *path = strdup( argv0 );
484                 int givenPathLen = (lastSlash - argv0) + 1;
485                 path[givenPathLen] = 0;
486
487                 int progNameLen = strlen(progName);
488                 int length = givenPathLen + progNameLen + 1;
489                 char *check = new char[length];
490                 sprintf( check, "%s%s", path, progName );
491                 result[numChecks++] = check;
492
493                 length = givenPathLen + 3 + progNameLen + 1 + progNameLen + 1;
494                 check = new char[length];
495                 sprintf( check, "%s../%s/%s", path, progName, progName );
496                 result[numChecks++] = check;
497         }
498         else {
499                 int prefixLen = strlen(PREFIX);
500                 int progNameLen = strlen(progName);
501                 int length = prefixLen + 5 + progNameLen + 1;
502                 char *check = new char[length];
503
504                 sprintf( check, PREFIX "/bin/%s", progName );
505                 result[numChecks++] = check;
506         }
507
508         result[numChecks] = 0;
509         return result;
510 }
511
512
513 void forkAndExec( const char *progName, char **pathChecks, 
514                 ArgsVector &args, char *intermed )
515 {
516         pid_t pid = fork();
517         if ( pid < 0 ) {
518                 /* Error, no child created. */
519                 error() << "failed to fork for " << progName << endl;
520                 cleanExit( intermed, 1 );
521         }
522         else if ( pid == 0 ) {
523                 /* child */
524                 while ( *pathChecks != 0 ) {
525                         /* Execv does not modify argv, it just uses the const form that is
526                          * compatible with the most code. Ours not included. */
527                         execv( *pathChecks, (char *const*) args.data );
528                         pathChecks += 1;
529                 }
530                 error() << "failed to exec " << progName << endl;
531                 cleanExit( intermed, 1 );
532         }
533
534         /* Parent process, wait for the child. */
535         int status;
536         wait( &status );
537
538         /* What happened with the child. */
539         if ( ! WIFEXITED( status ) ) {
540                 error() << progName << " did not exit normally" << endl;
541                 cleanExit( intermed, 1 );
542         }
543         
544         if ( WEXITSTATUS(status) != 0 )
545                 cleanExit( intermed, WEXITSTATUS(status) );
546 }
547
548 #else
549
550 /* GetModuleFileNameEx is used to find out where the the current process's
551  * binary is. That location is searched first. If that fails then we go up one
552  * directory and look for the executable inside a directory of the same name
553  * in case we are executing from the source tree.
554  * */
555 char **makePathChecksWin( const char *progName )
556 {
557         int len = 1024;
558         char *imageFileName = new char[len];
559         HANDLE h = GetCurrentProcess();
560         len = GetModuleFileNameEx( h, NULL, imageFileName, len );
561         imageFileName[len] = 0;
562
563         char **result = new char*[3];
564         const char *lastSlash = strrchr( imageFileName, '\\' );
565         int numChecks = 0;
566
567         assert( lastSlash != 0 );
568         char *path = strdup( imageFileName );
569         int givenPathLen = (lastSlash - imageFileName) + 1;
570         path[givenPathLen] = 0;
571
572         int progNameLen = strlen(progName);
573         int length = givenPathLen + progNameLen + 1;
574         char *check = new char[length];
575         sprintf( check, "%s%s", path, progName );
576         result[numChecks++] = check;
577
578         length = givenPathLen + 3 + progNameLen + 1 + progNameLen + 1;
579         check = new char[length];
580         sprintf( check, "%s..\\%s\\%s", path, progName, progName );
581         result[numChecks++] = check;
582
583         result[numChecks] = 0;
584         return result;
585 }
586
587 void spawn( const char *progName, char **pathChecks, 
588                 ArgsVector &args, char *intermed )
589 {
590         int result = 0;
591         while ( *pathChecks != 0 ) {
592                 //cerr << "trying to execute " << *pathChecks << endl;
593                 result = _spawnv( _P_WAIT, *pathChecks, args.data );
594                 if ( result >= 0 || errno != ENOENT )
595                         break;
596                 pathChecks += 1;
597         }
598
599         if ( result < 0 ) {
600                 error() << "failed to spawn " << progName << endl;
601                 cleanExit( intermed, 1 );
602         }
603
604         if ( result > 0 )
605                 cleanExit( intermed, 1 );
606 }
607
608 #endif
609
610 void execFrontend( const char *argv0, char *inputFileName, char *intermed )
611 {
612         /* The frontend program name. */
613         const char *progName = "ragel";
614
615         frontendArgs.insert( 0, progName );
616         frontendArgs.insert( 1, "-x" );
617         frontendArgs.append( "-o" );
618         frontendArgs.append( intermed );
619         frontendArgs.append( inputFileName );
620         frontendArgs.append( 0 );
621
622 #ifndef WIN32
623         char **pathChecks = makePathChecksUnix( argv0, progName );
624         forkAndExec( progName, pathChecks, frontendArgs, intermed );
625 #else
626         char **pathChecks = makePathChecksWin( progName );
627         spawn( progName, pathChecks, frontendArgs, intermed );
628 #endif
629 }
630
631 void execBackend( const char *argv0, char *intermed, char *outputFileName )
632 {
633         /* Locate the backend program */
634         const char *progName = 0;
635         if ( generateDot )
636                 progName = "rlgen-dot";
637         else {
638                 switch ( hostLang->lang ) {
639                         case HostLang::C:
640                         case HostLang::D:
641                                 progName = "rlgen-cd";
642                                 break;
643                         case HostLang::Java:
644                                 progName = "rlgen-java";
645                                 break;
646                         case HostLang::Ruby:
647                                 progName = "rlgen-ruby";
648                                 break;
649                 }
650         }
651
652         backendArgs.insert( 0, progName );
653         if ( outputFileName != 0 ) {
654                 backendArgs.append( "-o" );
655                 backendArgs.append( outputFileName );
656         }
657         backendArgs.append( intermed );
658         backendArgs.append( 0 );
659
660 #ifndef WIN32
661         char **pathChecks = makePathChecksUnix( argv0, progName );
662         forkAndExec( progName, pathChecks, backendArgs, intermed );
663 #else
664         char **pathChecks = makePathChecksWin( progName );
665         spawn( progName, pathChecks, backendArgs, intermed );
666 #endif
667 }
668
669 /* Main, process args and call yyparse to start scanning input. */
670 int main(int argc, char **argv)
671 {
672         char *inputFileName = 0;
673         char *outputFileName = 0;
674
675         processArgs( argc, argv, inputFileName, outputFileName );
676
677         /* If -M or -S are given and we're not generating a dot file then invoke
678          * the frontend. These options are not useful with code generators. */
679         if ( machineName != 0 || machineSpec != 0 ) {
680                 if ( !generateDot )
681                         frontendOnly = true;
682         }
683
684         /* Require an input file. If we use standard in then we won't have a file
685          * name on which to base the output. */
686         if ( inputFileName == 0 )
687                 error() << "no input file given" << endl;
688
689         /* Bail on argument processing errors. */
690         if ( gblErrorCount > 0 )
691                 exit(1);
692
693         /* Make sure we are not writing to the same file as the input file. */
694         if ( inputFileName != 0 && outputFileName != 0 && 
695                         strcmp( inputFileName, outputFileName  ) == 0 )
696         {
697                 error() << "output file \"" << outputFileName  << 
698                                 "\" is the same as the input file" << endp;
699         }
700
701         if ( frontendOnly )
702                 return frontend( inputFileName, outputFileName );
703
704         char *intermed = openIntermed( inputFileName, outputFileName );
705
706         /* From here on in the cleanExit function should be used to exit. */
707
708         /* Run the frontend, then the backend processes. */
709         execFrontend( argv[0], inputFileName, intermed );
710         execBackend( argv[0], intermed, outputFileName );
711
712         /* Clean up the intermediate. */
713         cleanExit( intermed, 0 );
714
715         return 0;
716 }