7cd5fa6f8281e3f84b099a3b49371fc3bb10b234
[external/ragel.git] / rlgen-cd / 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
29 #include "common.h"
30 #include "rlgen-cd.h"
31 #include "xmlparse.h"
32 #include "pcheck.h"
33 #include "vector.h"
34 #include "version.h"
35
36 /* Code generators. */
37 #include "tabcodegen.h"
38 #include "ftabcodegen.h"
39 #include "flatcodegen.h"
40 #include "fflatcodegen.h"
41 #include "gotocodegen.h"
42 #include "fgotocodegen.h"
43 #include "ipgotocodegen.h"
44 #include "splitcodegen.h"
45
46 using std::istream;
47 using std::ifstream;
48 using std::ostream;
49 using std::ios;
50 using std::cin;
51 using std::cout;
52 using std::cerr;
53 using std::endl;
54
55 /* Target language and output style. */
56 CodeStyleEnum codeStyle = GenTables;
57
58 /* Io globals. */
59 istream *inStream = 0;
60 ostream *outStream = 0;
61 output_filter *outFilter = 0;
62 char *outputFileName = 0;
63
64 /* Graphviz dot file generation. */
65 bool graphvizDone = false;
66
67 int numSplitPartitions = 0;
68 bool noLineDirectives = false;
69 bool printPrintables = false;
70
71 /* Print a summary of the options. */
72 void usage()
73 {
74         cout <<
75 "usage: " PROGNAME " [options] file\n"
76 "general:\n"
77 "   -h, -H, -?, --help    Print this usage and exit\n"
78 "   -v, --version         Print version information and exit\n"
79 "   -o <file>             Write output to <file>\n"
80 "code generation options:\n"
81 "   -L                    Inhibit writing of #line directives\n"
82 "generated code style:\n"
83 "   -T0                   Table driven FSM (default)\n"
84 "   -T1                   Faster table driven FSM\n"
85 "   -F0                   Flat table driven FSM\n"
86 "   -F1                   Faster flat table-driven FSM\n"
87 "   -G0                   Goto-driven FSM\n"
88 "   -G1                   Faster goto-driven FSM\n"
89 "   -G2                   Really fast goto-driven FSM\n"
90 "   -P<N>                 N-Way Split really fast goto-driven FSM\n"
91         ;       
92 }
93
94 /* Print version information. */
95 void version()
96 {
97         cout << "Ragel Code Generator for C, C++, Objective-C and D" << endl <<
98                         "Version " VERSION << ", " PUBDATE << endl <<
99                         "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
100 }
101
102 ostream &error()
103 {
104         gblErrorCount += 1;
105         cerr << PROGNAME ": ";
106         return cerr;
107 }
108
109 /*
110  * Callbacks invoked by the XML data parser.
111  */
112
113 /* Invoked by the parser when the root element is opened. */
114 ostream *openOutput( char *inputFile )
115 {
116         if ( hostLang->lang != HostLang::C && hostLang->lang != HostLang::D ) {
117                 error() << "this code generator is for C and D only" << endl;
118                 exit(1);
119         }
120
121         /* If the output format is code and no output file name is given, then
122          * make a default. */
123         if ( outputFileName == 0 ) {
124                 char *ext = findFileExtension( inputFile );
125                 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
126                         outputFileName = fileNameFromStem( inputFile, ".h" );
127                 else {
128                         const char *defExtension = 0;
129                         switch ( hostLang->lang ) {
130                                 case HostLang::C: defExtension = ".c"; break;
131                                 case HostLang::D: defExtension = ".d"; break;
132                                 default: break;
133                         }
134                         outputFileName = fileNameFromStem( inputFile, defExtension );
135                 }
136         }
137
138         /* Make sure we are not writing to the same file as the input file. */
139         if ( outputFileName != 0 && strcmp( inputFile, outputFileName  ) == 0 ) {
140                 error() << "output file \"" << outputFileName  << 
141                                 "\" is the same as the input file" << endl;
142         }
143
144         if ( outputFileName != 0 ) {
145                 /* Create the filter on the output and open it. */
146                 outFilter = new output_filter( outputFileName );
147                 outFilter->open( outputFileName, ios::out|ios::trunc );
148                 if ( !outFilter->is_open() ) {
149                         error() << "error opening " << outputFileName << " for writing" << endl;
150                         exit(1);
151                 }
152
153                 /* Open the output stream, attaching it to the filter. */
154                 outStream = new ostream( outFilter );
155         }
156         else {
157                 /* Writing out ot std out. */
158                 outStream = &cout;
159         }
160         return outStream;
161 }
162
163 /* Invoked by the parser when a ragel definition is opened. */
164 CodeGenData *makeCodeGen( char *sourceFileName, char *fsmName, 
165                 ostream &out, bool wantComplete )
166 {
167         CodeGenData *codeGen = 0;
168         switch ( hostLang->lang ) {
169         case HostLang::C:
170                 switch ( codeStyle ) {
171                 case GenTables:
172                         codeGen = new CTabCodeGen(out);
173                         break;
174                 case GenFTables:
175                         codeGen = new CFTabCodeGen(out);
176                         break;
177                 case GenFlat:
178                         codeGen = new CFlatCodeGen(out);
179                         break;
180                 case GenFFlat:
181                         codeGen = new CFFlatCodeGen(out);
182                         break;
183                 case GenGoto:
184                         codeGen = new CGotoCodeGen(out);
185                         break;
186                 case GenFGoto:
187                         codeGen = new CFGotoCodeGen(out);
188                         break;
189                 case GenIpGoto:
190                         codeGen = new CIpGotoCodeGen(out);
191                         break;
192                 case GenSplit:
193                         codeGen = new CSplitCodeGen(out);
194                         break;
195                 }
196                 break;
197
198         case HostLang::D:
199                 switch ( codeStyle ) {
200                 case GenTables:
201                         codeGen = new DTabCodeGen(out);
202                         break;
203                 case GenFTables:
204                         codeGen = new DFTabCodeGen(out);
205                         break;
206                 case GenFlat:
207                         codeGen = new DFlatCodeGen(out);
208                         break;
209                 case GenFFlat:
210                         codeGen = new DFFlatCodeGen(out);
211                         break;
212                 case GenGoto:
213                         codeGen = new DGotoCodeGen(out);
214                         break;
215                 case GenFGoto:
216                         codeGen = new DFGotoCodeGen(out);
217                         break;
218                 case GenIpGoto:
219                         codeGen = new DIpGotoCodeGen(out);
220                         break;
221                 case GenSplit:
222                         codeGen = new DSplitCodeGen(out);
223                         break;
224                 }
225                 break;
226
227         default: break;
228         }
229
230         codeGen->sourceFileName = sourceFileName;
231         codeGen->fsmName = fsmName;
232         codeGen->wantComplete = wantComplete;
233
234         return codeGen;
235 }
236
237
238
239 /* Main, process args and call yyparse to start scanning input. */
240 int main(int argc, char **argv)
241 {
242         ParamCheck pc("-:Hh?vLo:T:F:G:P:", argc, argv);
243         char *xmlInputFileName = 0;
244
245         while ( pc.check() ) {
246                 switch ( pc.state ) {
247                 case ParamCheck::match:
248                         switch ( pc.parameter ) {
249                         /* Output. */
250                         case 'o':
251                                 if ( *pc.parameterArg == 0 )
252                                         error() << "a zero length output file name was given" << endl;
253                                 else if ( outputFileName != 0 )
254                                         error() << "more than one output file name was given" << endl;
255                                 else {
256                                         /* Ok, remember the output file name. */
257                                         outputFileName = pc.parameterArg;
258                                 }
259                                 break;
260
261                         case 'L':
262                                 noLineDirectives = true;
263                                 break;
264
265                         /* Code style. */
266                         case 'T':
267                                 if ( pc.parameterArg[0] == '0' )
268                                         codeStyle = GenTables;
269                                 else if ( pc.parameterArg[0] == '1' )
270                                         codeStyle = GenFTables;
271                                 else {
272                                         error() << "-T" << pc.parameterArg[0] << 
273                                                         " is an invalid argument" << endl;
274                                         exit(1);
275                                 }
276                                 break;
277                         case 'F':
278                                 if ( pc.parameterArg[0] == '0' )
279                                         codeStyle = GenFlat;
280                                 else if ( pc.parameterArg[0] == '1' )
281                                         codeStyle = GenFFlat;
282                                 else {
283                                         error() << "-F" << pc.parameterArg[0] << 
284                                                         " is an invalid argument" << endl;
285                                         exit(1);
286                                 }
287                                 break;
288                         case 'G':
289                                 if ( pc.parameterArg[0] == '0' )
290                                         codeStyle = GenGoto;
291                                 else if ( pc.parameterArg[0] == '1' )
292                                         codeStyle = GenFGoto;
293                                 else if ( pc.parameterArg[0] == '2' )
294                                         codeStyle = GenIpGoto;
295                                 else {
296                                         error() << "-G" << pc.parameterArg[0] << 
297                                                         " is an invalid argument" << endl;
298                                         exit(1);
299                                 }
300                                 break;
301                         case 'P':
302                                 codeStyle = GenSplit;
303                                 numSplitPartitions = atoi( pc.parameterArg );
304                                 break;
305
306                         /* Version and help. */
307                         case 'v':
308                                 version();
309                                 exit(0);
310                         case 'H': case 'h': case '?':
311                                 usage();
312                                 exit(0);
313                         case '-':
314                                 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
315                                         usage();
316                                         exit(0);
317                                 }
318                                 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
319                                         version();
320                                         exit(0);
321                                 }
322                                 else {
323                                         error() << "--" << pc.parameterArg << 
324                                                         " is an invalid argument" << endl;
325                                         break;
326                                 }
327                         }
328                         break;
329
330                 case ParamCheck::invalid:
331                         error() << "-" << pc.parameter << " is an invalid argument" << endl;
332                         break;
333
334                 case ParamCheck::noparam:
335                         if ( *pc.curArg == 0 )
336                                 error() << "a zero length input file name was given" << endl;
337                         else if ( xmlInputFileName != 0 )
338                                 error() << "more than one input file name was given" << endl;
339                         else {
340                                 /* OK, Remember the filename. */
341                                 xmlInputFileName = pc.curArg;
342                         }
343                         break;
344                 }
345         }
346
347         /* Bail on above errors. */
348         if ( gblErrorCount > 0 )
349                 exit(1);
350
351         /* Open the input file for reading. */
352         if ( xmlInputFileName != 0 ) {
353                 /* Open the input file for reading. */
354                 ifstream *inFile = new ifstream( xmlInputFileName );
355                 inStream = inFile;
356                 if ( ! inFile->is_open() )
357                         error() << "could not open " << xmlInputFileName << " for reading" << endl;
358         }
359         else {
360                 xmlInputFileName = strdup("<stdin>");
361                 inStream = &cin;
362         }
363
364         /* Bail on above errors. */
365         if ( gblErrorCount > 0 )
366                 exit(1);
367
368         bool wantComplete = true;
369         bool outputActive = true;
370
371         /* Parse the input! */
372         xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete );
373
374         /* If writing to a file, delete the ostream, causing it to flush.
375          * Standard out is flushed automatically. */
376         if ( outputFileName != 0 ) {
377                 delete outStream;
378                 delete outFilter;
379         }
380
381         /* Finished, final check for errors.. */
382         if ( gblErrorCount > 0 ) {
383                 /* If we opened an output file, remove it. */
384                 if ( outputFileName != 0 )
385                         unlink( outputFileName );
386                 exit(1);
387         }
388         return 0;
389 }