2 * Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
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
30 #include "rlgen-csharp.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"
55 /* Target language and output style. */
56 extern CodeStyleEnum codeStyle
;
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. */
74 "usage: " PROGNAME
" [options] file\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. */
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()
102 cerr
<< PROGNAME
": ";
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
;
118 /* If the output format is code and no output file name is given, then
120 if ( outputFileName
== 0 ) {
121 char *ext
= findFileExtension( inputFile
);
122 if ( ext
!= 0 && strcmp( ext
, ".rh" ) == 0 )
123 outputFileName
= fileNameFromStem( inputFile
, ".h" );
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
;
143 /* Open the output stream, attaching it to the filter. */
144 outStream
= new ostream( outFilter
);
147 /* Writing out ot std out. */
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
) {
161 codeGen
= new CSharpTabCodeGen(out
);
164 codeGen
= new CSharpFTabCodeGen(out
);
167 codeGen
= new CSharpFlatCodeGen(out
);
170 codeGen
= new CSharpFFlatCodeGen(out
);
173 codeGen
= new CSharpGotoCodeGen(out
);
176 codeGen
= new CSharpFGotoCodeGen(out
);
179 codeGen
= new CSharpIpGotoCodeGen(out
);
182 codeGen
= new CSharpSplitCodeGen(out
);
186 codeGen
->sourceFileName
= sourceFileName
;
187 codeGen
->fsmName
= fsmName
;
188 codeGen
->wantComplete
= wantComplete
;
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
);
199 if ( ! inFile
->is_open() )
200 csharp_error() << "could not open " << xmlInputFileName
<< " for reading" << endl
;
202 /* Bail on above errors. */
203 if ( gblErrorCount
> 0 )
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 ) {
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
);