summaryrefslogtreecommitdiff
path: root/ragel/inputdata.cpp
blob: 7d8322ff70cf0376ae5c39aa050ca7a568cb780c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 *  Copyright 2008 Adrian Thurston <thurston@complang.org>
 */

/*  This file is part of Ragel.
 *
 *  Ragel is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 * 
 *  Ragel is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with Ragel; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

#include "ragel.h"
#include "common.h"
#include "inputdata.h"
#include "parsedata.h"
#include "rlparse.h"
#include <iostream>
#include "dotcodegen.h"

using std::cout;
using std::cerr;
using std::endl;
using std::ios;

/* Invoked by the parser when the root element is opened. */
void InputData::cdDefaultFileName( const char *inputFile )
{
	/* If the output format is code and no output file name is given, then
	 * make a default. */
	if ( outputFileName == 0 ) {
		const char *ext = findFileExtension( inputFile );
		if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
			outputFileName = fileNameFromStem( inputFile, ".h" );
		else {
			const char *defExtension = 0;
			switch ( hostLang->lang ) {
				case HostLang::C: defExtension = ".c"; break;
				case HostLang::D: defExtension = ".d"; break;
				case HostLang::D2: defExtension = ".d"; break;
				default: break;
			}
			outputFileName = fileNameFromStem( inputFile, defExtension );
		}
	}
}

/* Invoked by the parser when the root element is opened. */
void InputData::goDefaultFileName( const char *inputFile )
{
	/* If the output format is code and no output file name is given, then
	 * make a default. */
	if ( outputFileName == 0 )
		outputFileName = fileNameFromStem( inputFile, ".go" );
}

/* Invoked by the parser when the root element is opened. */
void InputData::javaDefaultFileName( const char *inputFile )
{
	/* If the output format is code and no output file name is given, then
	 * make a default. */
	if ( outputFileName == 0 )
		outputFileName = fileNameFromStem( inputFile, ".java" );
}

/* Invoked by the parser when the root element is opened. */
void InputData::rubyDefaultFileName( const char *inputFile )
{
	/* If the output format is code and no output file name is given, then
	 * make a default. */
	if ( outputFileName == 0 )
		outputFileName = fileNameFromStem( inputFile, ".rb" );
}

/* Invoked by the parser when the root element is opened. */
void InputData::csharpDefaultFileName( const char *inputFile )
{
	/* If the output format is code and no output file name is given, then
	 * make a default. */
	if ( outputFileName == 0 ) {
		const char *ext = findFileExtension( inputFile );
		if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
			outputFileName = fileNameFromStem( inputFile, ".h" );
		else
			outputFileName = fileNameFromStem( inputFile, ".cs" );
	}
}

/* Invoked by the parser when the root element is opened. */
void InputData::ocamlDefaultFileName( const char *inputFile )
{
	/* If the output format is code and no output file name is given, then
	 * make a default. */
	if ( outputFileName == 0 )
		outputFileName = fileNameFromStem( inputFile, ".ml" );
}

void InputData::makeOutputStream()
{
	if ( ! generateDot && ! generateXML ) {
		switch ( hostLang->lang ) {
			case HostLang::C:
			case HostLang::D:
			case HostLang::D2:
				cdDefaultFileName( inputFileName );
				break;
			case HostLang::Java:
				javaDefaultFileName( inputFileName );
				break;
			case HostLang::Go:
				goDefaultFileName( inputFileName );
				break;
			case HostLang::Ruby:
				rubyDefaultFileName( inputFileName );
				break;
			case HostLang::CSharp:
				csharpDefaultFileName( inputFileName );
				break;
			case HostLang::OCaml:
				ocamlDefaultFileName( inputFileName );
				break;
		}
	}

	/* Make sure we are not writing to the same file as the input file. */
	if ( outputFileName != 0 ) {
		if ( strcmp( inputFileName, outputFileName  ) == 0 ) {
			error() << "output file \"" << outputFileName  << 
					"\" is the same as the input file" << endl;
		}

		/* Create the filter on the output and open it. */
		outFilter = new output_filter( outputFileName );

		/* Open the output stream, attaching it to the filter. */
		outStream = new ostream( outFilter );
	}
	else {
		/* Writing out ot std out. */
		outStream = &cout;
	}
}

void InputData::openOutput()
{
	if ( outFilter != 0 ) {
		outFilter->open( outputFileName, ios::out|ios::trunc );
		if ( !outFilter->is_open() ) {
			error() << "error opening " << outputFileName << " for writing" << endl;
			exit(1);
		}
	}
}

void InputData::prepareMachineGen()
{
	if ( generateDot ) {
		/* Locate a machine spec to generate dot output for. We can only emit.
		 * Dot takes one graph at a time. */
		if ( machineSpec != 0 ) {
			/* Machine specified. */
			ParserDictEl *pdEl = parserDict.find( machineSpec );
			if ( pdEl == 0 )
				error() << "could not locate machine specified with -S and/or -M" << endp;
			dotGenParser = pdEl->value;
		}
		else { 
			/* No machine spec given, just use the first one. */
			if ( parserList.length() == 0 )
				error() << "no machine specification to generate graphviz output" << endp;

			dotGenParser = parserList.head;
		}

		GraphDictEl *gdEl = 0;

		if ( machineName != 0 ) {
			gdEl = dotGenParser->pd->graphDict.find( machineName );
			if ( gdEl == 0 )
				error() << "machine definition/instantiation not found" << endp;
		}
		else {
			/* We are using the whole machine spec. Need to make sure there
			 * are instances in the spec. */
			if ( dotGenParser->pd->instanceList.length() == 0 )
				error() << "no machine instantiations to generate graphviz output" << endp;
		}

		dotGenParser->pd->prepareMachineGen( gdEl );
	}
	else {
		/* No machine spec or machine name given. Generate everything. */
		for ( ParserDict::Iter parser = parserDict; parser.lte(); parser++ ) {
			ParseData *pd = parser->value->pd;
			if ( pd->instanceList.length() > 0 )
				pd->prepareMachineGen( 0 );
		}
	}
}

void InputData::generateReduced()
{
	if ( generateDot )
		dotGenParser->pd->generateReduced( *this );
	else {
		for ( ParserDict::Iter parser = parserDict; parser.lte(); parser++ ) {
			ParseData *pd = parser->value->pd;
			if ( pd->instanceList.length() > 0 )
				pd->generateReduced( *this );
		}
	}
}

/* Send eof to all parsers. */
void InputData::terminateAllParsers( )
{
	/* FIXME: a proper token is needed here. Suppose we should use the
	 * location of EOF in the last file that the parser was referenced in. */
	InputLoc loc;
	loc.fileName = "<EOF>";
	loc.line = 0;
	loc.col = 0;
	for ( ParserDict::Iter pdel = parserDict; pdel.lte(); pdel++ )
		pdel->value->token( loc, Parser_tk_eof, 0, 0 );
}

void InputData::verifyWritesHaveData()
{
	if ( !generateXML && !generateDot ) {
		for ( InputItemList::Iter ii = inputItems; ii.lte(); ii++ ) {
			if ( ii->type == InputItem::Write ) {
				if ( ii->pd->cgd == 0 )
					error( ii->loc ) << "no machine instantiations to write" << endl;
			}
		}
	}
}

void InputData::writeOutput()
{
	if ( generateXML )
		writeXML( *outStream );
	else if ( generateDot )
		static_cast<GraphvizDotGen*>(dotGenParser->pd->cgd)->writeDotFile();
	else {
		bool hostLineDirective = true;
		for ( InputItemList::Iter ii = inputItems; ii.lte(); ii++ ) {
			if ( ii->type == InputItem::Write ) {
				CodeGenData *cgd = ii->pd->cgd;
				::keyOps = &cgd->thisKeyOps;

				hostLineDirective = cgd->writeStatement( ii->loc,
						ii->writeArgs.length()-1, ii->writeArgs.data );
			}
			else {
				if ( hostLineDirective ) {
					/* Write statements can turn off host line directives for
					 * host sections that follow them. */
					*outStream << '\n';
					lineDirective( *outStream, inputFileName, ii->loc.line );
				}
				*outStream << ii->data.str();
				hostLineDirective = true;
			}
		}
	}
}