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
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;
72 * Callbacks invoked by the XML data parser.
75 /* Invoked by the parser when the root element is opened. */
76 ostream *cdOpenOutput( char *inputFile )
78 if ( hostLang->lang != HostLang::C && hostLang->lang != HostLang::D ) {
79 error() << "this code generator is for C and D only" << endl;
83 /* If the output format is code and no output file name is given, then
85 if ( outputFileName == 0 ) {
86 char *ext = findFileExtension( inputFile );
87 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
88 outputFileName = fileNameFromStem( inputFile, ".h" );
90 const char *defExtension = 0;
91 switch ( hostLang->lang ) {
92 case HostLang::C: defExtension = ".c"; break;
93 case HostLang::D: defExtension = ".d"; break;
96 outputFileName = fileNameFromStem( inputFile, defExtension );
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;
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;
115 /* Open the output stream, attaching it to the filter. */
116 outStream = new ostream( outFilter );
119 /* Writing out ot std out. */
125 /* Invoked by the parser when a ragel definition is opened. */
126 CodeGenData *cdMakeCodeGen( char *sourceFileName, char *fsmName,
127 ostream &out, bool wantComplete )
129 CodeGenData *codeGen = 0;
130 switch ( hostLang->lang ) {
132 switch ( codeStyle ) {
134 codeGen = new CTabCodeGen(out);
137 codeGen = new CFTabCodeGen(out);
140 codeGen = new CFlatCodeGen(out);
143 codeGen = new CFFlatCodeGen(out);
146 codeGen = new CGotoCodeGen(out);
149 codeGen = new CFGotoCodeGen(out);
152 codeGen = new CIpGotoCodeGen(out);
155 codeGen = new CSplitCodeGen(out);
161 switch ( codeStyle ) {
163 codeGen = new DTabCodeGen(out);
166 codeGen = new DFTabCodeGen(out);
169 codeGen = new DFlatCodeGen(out);
172 codeGen = new DFFlatCodeGen(out);
175 codeGen = new DGotoCodeGen(out);
178 codeGen = new DFGotoCodeGen(out);
181 codeGen = new DIpGotoCodeGen(out);
184 codeGen = new DSplitCodeGen(out);
192 codeGen->sourceFileName = sourceFileName;
193 codeGen->fsmName = fsmName;
194 codeGen->wantComplete = wantComplete;