removed unused function
[external/ragel.git] / rlgen-cd / main.cpp
1 /*
2  *  Copyright 2001-2007 Adrian Thurston <thurston@complang.org>
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 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
71 /*
72  * Callbacks invoked by the XML data parser.
73  */
74
75 /* Invoked by the parser when the root element is opened. */
76 ostream *cdOpenOutput( const char *inputFile )
77 {
78         if ( hostLang->lang != HostLang::C && hostLang->lang != HostLang::D ) {
79                 error() << "this code generator is for C and D only" << endl;
80                 exit(1);
81         }
82
83         /* If the output format is code and no output file name is given, then
84          * make a default. */
85         if ( outputFileName == 0 ) {
86                 const char *ext = findFileExtension( inputFile );
87                 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
88                         outputFileName = fileNameFromStem( inputFile, ".h" );
89                 else {
90                         const char *defExtension = 0;
91                         switch ( hostLang->lang ) {
92                                 case HostLang::C: defExtension = ".c"; break;
93                                 case HostLang::D: defExtension = ".d"; break;
94                                 default: break;
95                         }
96                         outputFileName = fileNameFromStem( inputFile, defExtension );
97                 }
98         }
99
100         /* Make sure we are not writing to the same file as the input file. */
101         if ( outputFileName != 0 && strcmp( inputFile, outputFileName  ) == 0 ) {
102                 error() << "output file \"" << outputFileName  << 
103                                 "\" is the same as the input file" << endl;
104         }
105
106         if ( outputFileName != 0 ) {
107                 /* Create the filter on the output and open it. */
108                 outFilter = new output_filter( outputFileName );
109                 outFilter->open( outputFileName, ios::out|ios::trunc );
110                 if ( !outFilter->is_open() ) {
111                         error() << "error opening " << outputFileName << " for writing" << endl;
112                         exit(1);
113                 }
114
115                 /* Open the output stream, attaching it to the filter. */
116                 outStream = new ostream( outFilter );
117         }
118         else {
119                 /* Writing out ot std out. */
120                 outStream = &cout;
121         }
122         return outStream;
123 }
124
125 /* Invoked by the parser when a ragel definition is opened. */
126 CodeGenData *cdMakeCodeGen( const char *sourceFileName, const char *fsmName, 
127                 ostream &out, bool wantComplete )
128 {
129         CodeGenData *codeGen = 0;
130         switch ( hostLang->lang ) {
131         case HostLang::C:
132                 switch ( codeStyle ) {
133                 case GenTables:
134                         codeGen = new CTabCodeGen(out);
135                         break;
136                 case GenFTables:
137                         codeGen = new CFTabCodeGen(out);
138                         break;
139                 case GenFlat:
140                         codeGen = new CFlatCodeGen(out);
141                         break;
142                 case GenFFlat:
143                         codeGen = new CFFlatCodeGen(out);
144                         break;
145                 case GenGoto:
146                         codeGen = new CGotoCodeGen(out);
147                         break;
148                 case GenFGoto:
149                         codeGen = new CFGotoCodeGen(out);
150                         break;
151                 case GenIpGoto:
152                         codeGen = new CIpGotoCodeGen(out);
153                         break;
154                 case GenSplit:
155                         codeGen = new CSplitCodeGen(out);
156                         break;
157                 }
158                 break;
159
160         case HostLang::D:
161                 switch ( codeStyle ) {
162                 case GenTables:
163                         codeGen = new DTabCodeGen(out);
164                         break;
165                 case GenFTables:
166                         codeGen = new DFTabCodeGen(out);
167                         break;
168                 case GenFlat:
169                         codeGen = new DFlatCodeGen(out);
170                         break;
171                 case GenFFlat:
172                         codeGen = new DFFlatCodeGen(out);
173                         break;
174                 case GenGoto:
175                         codeGen = new DGotoCodeGen(out);
176                         break;
177                 case GenFGoto:
178                         codeGen = new DFGotoCodeGen(out);
179                         break;
180                 case GenIpGoto:
181                         codeGen = new DIpGotoCodeGen(out);
182                         break;
183                 case GenSplit:
184                         codeGen = new DSplitCodeGen(out);
185                         break;
186                 }
187                 break;
188
189         default: break;
190         }
191
192         codeGen->sourceFileName = sourceFileName;
193         codeGen->fsmName = fsmName;
194         codeGen->wantComplete = wantComplete;
195
196         return codeGen;
197 }
198