Backend main functions are now factored out.
[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 extern CodeStyleEnum codeStyle;
57
58 /* Io globals. */
59 extern istream *inStream;
60 extern ostream *outStream;
61 extern output_filter *outFilter;
62 extern const char *outputFileName;
63
64 /* Graphviz dot file generation. */
65 extern bool graphvizDone;
66
67 extern int numSplitPartitions;
68 extern bool noLineDirectives;
69
70 /* Print a summary of the options. */
71 void csharp_usage()
72 {
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"
88         ;       
89 }
90
91 /* Print version information. */
92 void csharp_version()
93 {
94         cout << "Ragel Code Generator for C#" << endl <<
95                         "Version " VERSION << ", " PUBDATE << endl <<
96                         "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
97 }
98
99 ostream &csharp_error()
100 {
101         gblErrorCount += 1;
102         cerr << PROGNAME ": ";
103         return cerr;
104 }
105
106 /*
107  * Callbacks invoked by the XML data parser.
108  */
109
110 /* Invoked by the parser when the root element is opened. */
111 ostream *csharpOpenOutput( char *inputFile )
112 {
113         if ( hostLang->lang != HostLang::CSharp ) {
114                 csharp_error() << "this code generator is for C# only" << endl;
115                 exit(1);
116         }
117
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" );
126         }
127
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;
132         }
133
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);
141                 }
142
143                 /* Open the output stream, attaching it to the filter. */
144                 outStream = new ostream( outFilter );
145         }
146         else {
147                 /* Writing out ot std out. */
148                 outStream = &cout;
149         }
150         return outStream;
151 }
152
153 /* Invoked by the parser when a ragel definition is opened. */
154 CodeGenData *csharpMakeCodeGen( char *sourceFileName, char *fsmName, 
155                 ostream &out, bool wantComplete )
156 {
157         CodeGenData *codeGen = 0;
158
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;
184         }
185
186         codeGen->sourceFileName = sourceFileName;
187         codeGen->fsmName = fsmName;
188         codeGen->wantComplete = wantComplete;
189
190         return codeGen;
191 }