Removed arg passing from frontend to backend functions.
[ragel.git] / rlgen-csharp / main.cpp
blob1c6739058dc11d260bc294f5ed29887457fbde06
1 /*
2 * Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
3 */
5 /* This file is part of Ragel.
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.
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.
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
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <iostream>
26 #include <fstream>
27 #include <unistd.h>
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"
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"
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;
55 /* Target language and output style. */
56 extern CodeStyleEnum codeStyle;
58 /* Io globals. */
59 extern istream *inStream;
60 extern ostream *outStream;
61 extern output_filter *outFilter;
62 extern const char *outputFileName;
64 /* Graphviz dot file generation. */
65 extern bool graphvizDone;
67 extern int numSplitPartitions;
68 extern bool noLineDirectives;
70 /* Print a summary of the options. */
71 void csharp_usage()
73 cout <<
74 "usage: " PROGNAME " [options] file\n"
75 "general:\n"
76 " -h, -H, -?, --help Print this usage and exit\n"
77 " -v, --version Print version information and exit\n"
78 " -o <file> Write output to <file>\n"
79 "code generation options:\n"
80 " -L Inhibit writing of #line directives\n"
81 "generated code style:\n"
82 " -T0 Table driven FSM (default)\n"
83 " -T1 Faster table driven FSM\n"
84 " -F0 Flat table driven FSM\n"
85 " -F1 Faster flat table-driven FSM\n"
86 " -G0 Goto-driven FSM\n"
87 " -G1 Faster goto-driven FSM\n"
91 /* Print version information. */
92 void csharp_version()
94 cout << "Ragel Code Generator for C#" << endl <<
95 "Version " VERSION << ", " PUBDATE << endl <<
96 "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
99 ostream &csharp_error()
101 gblErrorCount += 1;
102 cerr << PROGNAME ": ";
103 return cerr;
107 * Callbacks invoked by the XML data parser.
110 /* Invoked by the parser when the root element is opened. */
111 ostream *csharpOpenOutput( char *inputFile )
113 if ( hostLang->lang != HostLang::CSharp ) {
114 csharp_error() << "this code generator is for C# only" << endl;
115 exit(1);
118 /* If the output format is code and no output file name is given, then
119 * make a default. */
120 if ( outputFileName == 0 ) {
121 char *ext = findFileExtension( inputFile );
122 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
123 outputFileName = fileNameFromStem( inputFile, ".h" );
124 else
125 outputFileName = fileNameFromStem( inputFile, ".cs" );
128 /* Make sure we are not writing to the same file as the input file. */
129 if ( outputFileName != 0 && strcmp( inputFile, outputFileName ) == 0 ) {
130 csharp_error() << "output file \"" << outputFileName <<
131 "\" is the same as the input file" << endl;
134 if ( outputFileName != 0 ) {
135 /* Create the filter on the output and open it. */
136 outFilter = new output_filter( outputFileName );
137 outFilter->open( outputFileName, ios::out|ios::trunc );
138 if ( !outFilter->is_open() ) {
139 csharp_error() << "error opening " << outputFileName << " for writing" << endl;
140 exit(1);
143 /* Open the output stream, attaching it to the filter. */
144 outStream = new ostream( outFilter );
146 else {
147 /* Writing out ot std out. */
148 outStream = &cout;
150 return outStream;
153 /* Invoked by the parser when a ragel definition is opened. */
154 CodeGenData *csharpMakeCodeGen( char *sourceFileName, char *fsmName,
155 ostream &out, bool wantComplete )
157 CodeGenData *codeGen = 0;
159 switch ( codeStyle ) {
160 case GenTables:
161 codeGen = new CSharpTabCodeGen(out);
162 break;
163 case GenFTables:
164 codeGen = new CSharpFTabCodeGen(out);
165 break;
166 case GenFlat:
167 codeGen = new CSharpFlatCodeGen(out);
168 break;
169 case GenFFlat:
170 codeGen = new CSharpFFlatCodeGen(out);
171 break;
172 case GenGoto:
173 codeGen = new CSharpGotoCodeGen(out);
174 break;
175 case GenFGoto:
176 codeGen = new CSharpFGotoCodeGen(out);
177 break;
178 case GenIpGoto:
179 codeGen = new CSharpIpGotoCodeGen(out);
180 break;
181 case GenSplit:
182 codeGen = new CSharpSplitCodeGen(out);
183 break;
186 codeGen->sourceFileName = sourceFileName;
187 codeGen->fsmName = fsmName;
188 codeGen->wantComplete = wantComplete;
190 return codeGen;
193 /* Main, process args and call yyparse to start scanning input. */
194 int csharp_main( const char *xmlInputFileName )
196 /* Open the input file for reading. */
197 ifstream *inFile = new ifstream( xmlInputFileName );
198 inStream = inFile;
199 if ( ! inFile->is_open() )
200 csharp_error() << "could not open " << xmlInputFileName << " for reading" << endl;
202 /* Bail on above errors. */
203 if ( gblErrorCount > 0 )
204 exit(1);
206 bool wantComplete = true;
207 bool outputActive = true;
209 /* Parse the input! */
210 xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete );
212 /* If writing to a file, delete the ostream, causing it to flush.
213 * Standard out is flushed automatically. */
214 if ( outputFileName != 0 ) {
215 delete outStream;
216 delete outFilter;
219 /* Finished, final check for errors.. */
220 if ( gblErrorCount > 0 ) {
221 /* If we opened an output file, remove it. */
222 if ( outputFileName != 0 )
223 unlink( outputFileName );
224 exit(1);
226 return 0;