Applied a patch from Andrei Polushin for setting the error message format.
[external/ragel.git] / rlgen-csharp / 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-csharp.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         ;       
90 }
91
92 /* Print version information. */
93 void version()
94 {
95         cout << "Ragel Code Generator for C#" << endl <<
96                         "Version " VERSION << ", " PUBDATE << endl <<
97                         "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
98 }
99
100 ostream &error()
101 {
102         gblErrorCount += 1;
103         cerr << PROGNAME ": ";
104         return cerr;
105 }
106
107 /*
108  * Callbacks invoked by the XML data parser.
109  */
110
111 /* Invoked by the parser when the root element is opened. */
112 ostream *openOutput( char *inputFile )
113 {
114         if ( hostLang->lang != HostLang::CSharp ) {
115                 error() << "this code generator is for C# only" << endl;
116                 exit(1);
117         }
118
119         /* If the output format is code and no output file name is given, then
120          * make a default. */
121         if ( outputFileName == 0 ) {
122                 char *ext = findFileExtension( inputFile );
123                 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
124                         outputFileName = fileNameFromStem( inputFile, ".h" );
125                 else
126                         outputFileName = fileNameFromStem( inputFile, ".cs" );
127         }
128
129         /* Make sure we are not writing to the same file as the input file. */
130         if ( outputFileName != 0 && strcmp( inputFile, outputFileName  ) == 0 ) {
131                 error() << "output file \"" << outputFileName  << 
132                                 "\" is the same as the input file" << endl;
133         }
134
135         if ( outputFileName != 0 ) {
136                 /* Create the filter on the output and open it. */
137                 outFilter = new output_filter( outputFileName );
138                 outFilter->open( outputFileName, ios::out|ios::trunc );
139                 if ( !outFilter->is_open() ) {
140                         error() << "error opening " << outputFileName << " for writing" << endl;
141                         exit(1);
142                 }
143
144                 /* Open the output stream, attaching it to the filter. */
145                 outStream = new ostream( outFilter );
146         }
147         else {
148                 /* Writing out ot std out. */
149                 outStream = &cout;
150         }
151         return outStream;
152 }
153
154 /* Invoked by the parser when a ragel definition is opened. */
155 CodeGenData *makeCodeGen( char *sourceFileName, char *fsmName, 
156                 ostream &out, bool wantComplete )
157 {
158         CodeGenData *codeGen = 0;
159
160         switch ( codeStyle ) {
161         case GenTables:
162                 codeGen = new CSharpTabCodeGen(out);
163                 break;
164         case GenFTables:
165                 codeGen = new CSharpFTabCodeGen(out);
166                 break;
167         case GenFlat:
168                 codeGen = new CSharpFlatCodeGen(out);
169                 break;
170         case GenFFlat:
171                 codeGen = new CSharpFFlatCodeGen(out);
172                 break;
173         case GenGoto:
174                 codeGen = new CSharpGotoCodeGen(out);
175                 break;
176         case GenFGoto:
177                 codeGen = new CSharpFGotoCodeGen(out);
178                 break;
179         case GenIpGoto:
180                 codeGen = new CSharpIpGotoCodeGen(out);
181                 break;
182         case GenSplit:
183                 codeGen = new CSharpSplitCodeGen(out);
184                 break;
185         }
186
187         codeGen->sourceFileName = sourceFileName;
188         codeGen->fsmName = fsmName;
189         codeGen->wantComplete = wantComplete;
190
191         return codeGen;
192 }
193
194
195
196 /* Main, process args and call yyparse to start scanning input. */
197 int main(int argc, char **argv)
198 {
199 //      ParamCheck pc("-:Hh?vLo:T:F:G:P:", argc, argv);
200         ParamCheck pc("-:Hh?vLo:T:F:G:", argc, argv);
201         char *xmlInputFileName = 0;
202
203         while ( pc.check() ) {
204                 switch ( pc.state ) {
205                 case ParamCheck::match:
206                         switch ( pc.parameter ) {
207                         /* Output. */
208                         case 'o':
209                                 if ( *pc.paramArg == 0 )
210                                         error() << "a zero length output file name was given" << endl;
211                                 else if ( outputFileName != 0 )
212                                         error() << "more than one output file name was given" << endl;
213                                 else {
214                                         /* Ok, remember the output file name. */
215                                         outputFileName = pc.paramArg;
216                                 }
217                                 break;
218
219                         case 'L':
220                                 noLineDirectives = true;
221                                 break;
222
223                         /* Code style. */
224                         case 'T':
225                                 if ( pc.paramArg[0] == '0' )
226                                         codeStyle = GenTables;
227                                 else if ( pc.paramArg[0] == '1' )
228                                         codeStyle = GenFTables;
229                                 else {
230                                         error() << "-T" << pc.paramArg[0] << 
231                                                         " is an invalid argument" << endl;
232                                         exit(1);
233                                 }
234                                 break;
235                         case 'F':
236                                 if ( pc.paramArg[0] == '0' )
237                                         codeStyle = GenFlat;
238                                 else if ( pc.paramArg[0] == '1' )
239                                         codeStyle = GenFFlat;
240                                 else {
241                                         error() << "-F" << pc.paramArg[0] << 
242                                                         " is an invalid argument" << endl;
243                                         exit(1);
244                                 }
245                                 break;
246                         case 'G':
247                                 if ( pc.paramArg[0] == '0' )
248                                         codeStyle = GenGoto;
249                                 else if ( pc.paramArg[0] == '1' )
250                                         codeStyle = GenFGoto;
251                                 else if ( pc.paramArg[0] == '2' )
252                                         codeStyle = GenIpGoto;
253                                 else {
254                                         error() << "-G" << pc.paramArg[0] << 
255                                                         " is an invalid argument" << endl;
256                                         exit(1);
257                                 }
258                                 break;
259                         case 'P':
260                                 codeStyle = GenSplit;
261                                 numSplitPartitions = atoi( pc.paramArg );
262                                 break;
263
264                         /* Version and help. */
265                         case 'v':
266                                 version();
267                                 exit(0);
268                         case 'H': case 'h': case '?':
269                                 usage();
270                                 exit(0);
271                         case '-':
272                                 if ( strcmp(pc.paramArg, "help") == 0 ) {
273                                         usage();
274                                         exit(0);
275                                 }
276                                 else if ( strcmp(pc.paramArg, "version") == 0 ) {
277                                         version();
278                                         exit(0);
279                                 }
280                                 else {
281                                         error() << "--" << pc.paramArg << 
282                                                         " is an invalid argument" << endl;
283                                         break;
284                                 }
285                         }
286                         break;
287
288                 case ParamCheck::invalid:
289                         error() << "-" << pc.parameter << " is an invalid argument" << endl;
290                         break;
291
292                 case ParamCheck::noparam:
293                         if ( *pc.curArg == 0 )
294                                 error() << "a zero length input file name was given" << endl;
295                         else if ( xmlInputFileName != 0 )
296                                 error() << "more than one input file name was given" << endl;
297                         else {
298                                 /* OK, Remember the filename. */
299                                 xmlInputFileName = pc.curArg;
300                         }
301                         break;
302                 }
303         }
304
305         /* Bail on above errors. */
306         if ( gblErrorCount > 0 )
307                 exit(1);
308
309         /* Open the input file for reading. */
310         if ( xmlInputFileName != 0 ) {
311                 /* Open the input file for reading. */
312                 ifstream *inFile = new ifstream( xmlInputFileName );
313                 inStream = inFile;
314                 if ( ! inFile->is_open() )
315                         error() << "could not open " << xmlInputFileName << " for reading" << endl;
316         }
317         else {
318                 xmlInputFileName = strdup("<stdin>");
319                 inStream = &cin;
320         }
321
322         /* Bail on above errors. */
323         if ( gblErrorCount > 0 )
324                 exit(1);
325
326         bool wantComplete = true;
327         bool outputActive = true;
328
329         /* Parse the input! */
330         xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete );
331
332         /* If writing to a file, delete the ostream, causing it to flush.
333          * Standard out is flushed automatically. */
334         if ( outputFileName != 0 ) {
335                 delete outStream;
336                 delete outFilter;
337         }
338
339         /* Finished, final check for errors.. */
340         if ( gblErrorCount > 0 ) {
341                 /* If we opened an output file, remove it. */
342                 if ( outputFileName != 0 )
343                         unlink( outputFileName );
344                 exit(1);
345         }
346         return 0;
347 }