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