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"
33 void InputData::generateSpecificReduced()
35 if ( parserDict.length() > 0 ) {
36 /* There is either a machine spec or machine name given. */
37 ParseData *parseData = 0;
38 GraphDictEl *graphDictEl = 0;
40 /* Traverse the sections, break out when we find a section/machine
41 * that matches the one specified. */
42 for ( ParserDict::Iter parser = parserDict; parser.lte(); parser++ ) {
43 ParseData *checkPd = parser->value->pd;
44 if ( machineSpec == 0 || strcmp( checkPd->sectionName, machineSpec ) == 0 ) {
45 GraphDictEl *checkGdEl = 0;
46 if ( machineName == 0 || (checkGdEl =
47 checkPd->graphDict.find( machineName )) != 0 )
49 /* Have a machine spec and/or machine name that matches
50 * the -M/-S options. */
52 graphDictEl = checkGdEl;
59 error() << "could not locate machine specified with -S and/or -M" << endl;
61 /* Section/Machine to emit was found. Prepare and emit it. */
62 parseData->prepareMachineGen( graphDictEl );
63 if ( gblErrorCount == 0 )
64 parseData->generateReduced( *this );
72 void InputData::openOutput()
75 outStream = dotOpenOutput( inputFileName );
76 else if ( hostLang->lang == HostLang::C )
77 outStream = cdOpenOutput( inputFileName );
78 else if ( hostLang->lang == HostLang::D )
79 outStream = cdOpenOutput( inputFileName );
80 else if ( hostLang->lang == HostLang::Java )
81 outStream = javaOpenOutput( inputFileName );
82 else if ( hostLang->lang == HostLang::Ruby )
83 outStream = rubyOpenOutput( inputFileName );
84 else if ( hostLang->lang == HostLang::CSharp )
85 outStream = csharpOpenOutput( inputFileName );
91 void InputData::prepareMachineGen()
93 /* No machine spec or machine name given. Generate everything. */
94 for ( ParserDict::Iter parser = parserDict; parser.lte(); parser++ ) {
95 ParseData *pd = parser->value->pd;
96 if ( pd->instanceList.length() > 0 )
97 pd->prepareMachineGen( 0 );
101 void InputData::generateReduced()
103 for ( ParserDict::Iter parser = parserDict; parser.lte(); parser++ ) {
104 ParseData *pd = parser->value->pd;
105 if ( pd->instanceList.length() > 0 )
106 pd->generateReduced( *this );
110 void InputData::writeOutput()
112 for ( InputItemList::Iter ii = inputItems; ii.lte(); ii++ ) {
113 if ( ii->type == InputItem::Write ) {
114 CodeGenMapEl *mapEl = codeGenMap.find( (char*)ii->name.c_str() );
115 CodeGenData *cgd = mapEl->value;
116 ::keyOps = &cgd->thisKeyOps;
118 cgd->writeStatement( ii->loc, ii->writeArgs.length()-1, ii->writeArgs.data );
120 else /*if ( /!generateDot )*/ {
122 lineDirective( *outStream, inputFileName, ii->loc.line );
123 *outStream << ii->data.str();