Updated the help output. Moved some code from InputData:: to main.
[external/ragel.git] / ragel / inputdata.cpp
1 /*
2  *  Copyright 2008 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 "ragel.h"
23 #include "common.h"
24 #include "inputdata.h"
25 #include "parsedata.h"
26 #include "rlparse.h"
27 #include <iostream>
28
29 using std::cout;
30 using std::cerr;
31 using std::endl;
32
33 void InputData::generateSpecificReduced()
34 {
35         if ( parserDict.length() > 0 ) {
36                 /* There is either a machine spec or machine name given. */
37                 ParseData *parseData = 0;
38                 GraphDictEl *graphDictEl = 0;
39
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 )
48                                 {
49                                         /* Have a machine spec and/or machine name that matches
50                                          * the -M/-S options. */
51                                         parseData = checkPd;
52                                         graphDictEl = checkGdEl;
53                                         break;
54                                 }
55                         }
56                 }
57
58                 if ( parseData == 0 )
59                         error() << "could not locate machine specified with -S and/or -M" << endl;
60                 else {
61                         /* Section/Machine to emit was found. Prepare and emit it. */
62                         parseData->prepareMachineGen( graphDictEl );
63                         if ( gblErrorCount == 0 )
64                                 parseData->generateReduced( *this );
65                 }
66         }
67
68         writeOutput();
69 }
70
71
72 void InputData::openOutput()
73 {
74         if ( generateDot )
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 );
86         else {
87                 assert( false );
88         }
89 }
90
91 void InputData::prepareMachineGen()
92 {
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 );
98         }
99 }
100
101 void InputData::generateReduced()
102 {
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 );
107         }
108 }
109
110 void InputData::writeOutput()
111 {
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;
117
118                         cgd->writeStatement( ii->loc, ii->writeArgs.length()-1, ii->writeArgs.data );
119                 }
120                 else /*if ( /!generateDot )*/ {
121                         *outStream << '\n';
122                         lineDirective( *outStream, inputFileName, ii->loc.line );
123                         *outStream << ii->data.str();
124                 }
125         }
126 }
127