2 * Copyright 2008 Adrian Thurston <thurston@complang.org>
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
24 #include "inputdata.h"
25 #include "parsedata.h"
34 void InputData::generateSpecificReduced()
36 if ( parserDict.length() > 0 ) {
37 /* There is either a machine spec or machine name given. */
38 ParseData *parseData = 0;
39 GraphDictEl *graphDictEl = 0;
41 /* Traverse the sections, break out when we find a section/machine
42 * that matches the one specified. */
43 for ( ParserDict::Iter parser = parserDict; parser.lte(); parser++ ) {
44 ParseData *checkPd = parser->value->pd;
45 if ( machineSpec == 0 || strcmp( checkPd->sectionName, machineSpec ) == 0 ) {
46 GraphDictEl *checkGdEl = 0;
47 if ( machineName == 0 || (checkGdEl =
48 checkPd->graphDict.find( machineName )) != 0 )
50 /* Have a machine spec and/or machine name that matches
51 * the -M/-S options. */
53 graphDictEl = checkGdEl;
60 error() << "could not locate machine specified with -S and/or -M" << endl;
62 /* Section/Machine to emit was found. Prepare and emit it. */
63 parseData->prepareMachineGen( graphDictEl );
64 if ( gblErrorCount == 0 )
65 parseData->generateReduced( *this );
73 /* Invoked by the parser when the root element is opened. */
74 void InputData::cdDefaultFileName( const char *inputFile )
76 /* If the output format is code and no output file name is given, then
78 if ( outputFileName == 0 ) {
79 const char *ext = findFileExtension( inputFile );
80 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
81 outputFileName = fileNameFromStem( inputFile, ".h" );
83 const char *defExtension = 0;
84 switch ( hostLang->lang ) {
85 case HostLang::C: defExtension = ".c"; break;
86 case HostLang::D: defExtension = ".d"; break;
89 outputFileName = fileNameFromStem( inputFile, defExtension );
94 /* Invoked by the parser when the root element is opened. */
95 void InputData::javaDefaultFileName( const char *inputFile )
97 /* If the output format is code and no output file name is given, then
99 if ( outputFileName == 0 )
100 outputFileName = fileNameFromStem( inputFile, ".java" );
103 /* Invoked by the parser when the root element is opened. */
104 void InputData::rubyDefaultFileName( const char *inputFile )
106 /* If the output format is code and no output file name is given, then
108 if ( outputFileName == 0 )
109 outputFileName = fileNameFromStem( inputFile, ".rb" );
112 /* Invoked by the parser when the root element is opened. */
113 void InputData::csharpDefaultFileName( const char *inputFile )
115 /* If the output format is code and no output file name is given, then
117 if ( outputFileName == 0 ) {
118 const char *ext = findFileExtension( inputFile );
119 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
120 outputFileName = fileNameFromStem( inputFile, ".h" );
122 outputFileName = fileNameFromStem( inputFile, ".cs" );
126 void InputData::openOutput()
128 if ( ! generateDot ) {
129 switch ( hostLang->lang ) {
132 cdDefaultFileName( inputFileName );
135 javaDefaultFileName( inputFileName );
138 rubyDefaultFileName( inputFileName );
140 case HostLang::CSharp:
141 csharpDefaultFileName( inputFileName );
146 /* Make sure we are not writing to the same file as the input file. */
147 if ( outputFileName != 0 ) {
148 if ( strcmp( inputFileName, outputFileName ) == 0 ) {
149 error() << "output file \"" << outputFileName <<
150 "\" is the same as the input file" << endl;
153 /* Create the filter on the output and open it. */
154 outFilter = new output_filter( outputFileName );
156 /* Open the output stream, attaching it to the filter. */
157 outStream = new ostream( outFilter );
160 /* Writing out ot std out. */
165 void InputData::openOutput2()
167 if ( outFilter != 0 ) {
168 outFilter->open( outputFileName, ios::out|ios::trunc );
169 if ( !outFilter->is_open() ) {
170 error() << "error opening " << outputFileName << " for writing" << endl;
176 void InputData::prepareMachineGen()
178 /* No machine spec or machine name given. Generate everything. */
179 for ( ParserDict::Iter parser = parserDict; parser.lte(); parser++ ) {
180 ParseData *pd = parser->value->pd;
181 if ( pd->instanceList.length() > 0 )
182 pd->prepareMachineGen( 0 );
186 void InputData::generateReduced()
188 for ( ParserDict::Iter parser = parserDict; parser.lte(); parser++ ) {
189 ParseData *pd = parser->value->pd;
190 if ( pd->instanceList.length() > 0 )
191 pd->generateReduced( *this );
195 void InputData::writeOutput()
197 for ( InputItemList::Iter ii = inputItems; ii.lte(); ii++ ) {
198 if ( ii->type == InputItem::Write ) {
199 CodeGenData *cgd = ii->pd->cgd;
200 ::keyOps = &cgd->thisKeyOps;
202 cgd->writeStatement( ii->loc, ii->writeArgs.length()-1, ii->writeArgs.data );
204 else /*if ( /!generateDot )*/ {
206 lineDirective( *outStream, inputFileName, ii->loc.line );
207 *outStream << ii->data.str();