The code generators now exit immediately if the language is not correct. Write
[external/ragel.git] / rlgen-java / main.cpp
1 /*
2  *  Copyright 2001-2005 Adrian Thurston <thurston@cs.queensu.ca>
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 <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <iostream>
26 #include <fstream>
27 #include <unistd.h>
28
29 #include "rlgen-java.h"
30 #include "xmlparse.h"
31 #include "pcheck.h"
32 #include "vector.h"
33 #include "version.h"
34 #include "common.h"
35 #include "javacodegen.h"
36
37 using std::istream;
38 using std::ifstream;
39 using std::ostream;
40 using std::ios;
41 using std::cin;
42 using std::cout;
43 using std::cerr;
44 using std::endl;
45
46 /* Io globals. */
47 istream *inStream = 0;
48 ostream *outStream = 0;
49 output_filter *outFilter = 0;
50 char *outputFileName = 0;
51
52 /* Graphviz dot file generation. */
53 bool graphvizDone = false;
54
55 int numSplitPartitions = 0;
56 bool printPrintables = false;
57
58 /* Print a summary of the options. */
59 void usage()
60 {
61         cout <<
62 "usage: " PROGNAME " [options] file\n"
63 "general:\n"
64 "   -h, -H, -?, --help    Print this usage and exit\n"
65 "   -v, --version         Print version information and exit\n"
66 "   -o <file>             Write output to <file>\n"
67         ;       
68 }
69
70 /* Print version information. */
71 void version()
72 {
73         cout << "Ragel Code Generator for Java" << endl <<
74                         "Version " VERSION << ", " PUBDATE << endl <<
75                         "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
76 }
77
78 /* Total error count. */
79 int gblErrorCount = 0;
80
81 ostream &error()
82 {
83         gblErrorCount += 1;
84         cerr << PROGNAME ": ";
85         return cerr;
86 }
87
88 /*
89  * Callbacks invoked by the XML data parser.
90  */
91
92 /* Invoked by the parser when the root element is opened. */
93 ostream *openOutput( char *inputFile )
94 {
95         if ( hostLangType != JavaCode ) {
96                 error() << "this code generator is for Java only" << endl;
97                 exit(1);
98         }
99
100         /* If the output format is code and no output file name is given, then
101          * make a default. */
102         if ( outputFileName == 0 ) {
103                 char *ext = findFileExtension( inputFile );
104                 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
105                         outputFileName = fileNameFromStem( inputFile, ".h" );
106                 else
107                         outputFileName = fileNameFromStem( inputFile, ".java" );
108         }
109
110         /* Make sure we are not writing to the same file as the input file. */
111         if ( outputFileName != 0 && strcmp( inputFile, outputFileName  ) == 0 ) {
112                 error() << "output file \"" << outputFileName  << 
113                                 "\" is the same as the input file" << endl;
114         }
115
116         if ( outputFileName != 0 ) {
117                 /* Create the filter on the output and open it. */
118                 outFilter = new output_filter( outputFileName );
119                 outFilter->open( outputFileName, ios::out|ios::trunc );
120                 if ( !outFilter->is_open() ) {
121                         error() << "error opening " << outputFileName << " for writing" << endl;
122                         exit(1);
123                 }
124
125                 /* Open the output stream, attaching it to the filter. */
126                 outStream = new ostream( outFilter );
127         }
128         else {
129                 /* Writing out ot std out. */
130                 outStream = &cout;
131         }
132         return outStream;
133 }
134
135 /* Invoked by the parser when a ragel definition is opened. */
136 CodeGenData *makeCodeGen( char *sourceFileName, char *fsmName, 
137                 ostream &out, bool wantComplete )
138 {
139         CodeGenData *codeGen = new JavaTabCodeGen(out);
140
141         codeGen->sourceFileName = sourceFileName;
142         codeGen->fsmName = fsmName;
143         codeGen->wantComplete = wantComplete;
144
145         return codeGen;
146 }
147
148 /* Main, process args and call yyparse to start scanning input. */
149 int main(int argc, char **argv)
150 {
151         ParamCheck pc("o:vHh?-:", argc, argv);
152         char *xmlInputFileName = 0;
153
154         while ( pc.check() ) {
155                 switch ( pc.state ) {
156                 case ParamCheck::match:
157                         switch ( pc.parameter ) {
158                         /* Output. */
159                         case 'o':
160                                 if ( *pc.parameterArg == 0 )
161                                         error() << "a zero length output file name was given" << endl;
162                                 else if ( outputFileName != 0 )
163                                         error() << "more than one output file name was given" << endl;
164                                 else {
165                                         /* Ok, remember the output file name. */
166                                         outputFileName = pc.parameterArg;
167                                 }
168                                 break;
169
170                         /* Version and help. */
171                         case 'v':
172                                 version();
173                                 exit(0);
174                         case 'H': case 'h': case '?':
175                                 usage();
176                                 exit(0);
177                         case '-':
178                                 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
179                                         usage();
180                                         exit(0);
181                                 }
182                                 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
183                                         version();
184                                         exit(0);
185                                 }
186                                 else {
187                                         error() << "--" << pc.parameterArg << 
188                                                         " is an invalid argument" << endl;
189                                         break;
190                                 }
191                         }
192                         break;
193
194                 case ParamCheck::invalid:
195                         error() << "-" << pc.parameter << " is an invalid argument" << endl;
196                         break;
197
198                 case ParamCheck::noparam:
199                         if ( *pc.curArg == 0 )
200                                 error() << "a zero length input file name was given" << endl;
201                         else if ( xmlInputFileName != 0 )
202                                 error() << "more than one input file name was given" << endl;
203                         else {
204                                 /* OK, Remember the filename. */
205                                 xmlInputFileName = pc.curArg;
206                         }
207                         break;
208                 }
209         }
210
211         /* Bail on above errors. */
212         if ( gblErrorCount > 0 )
213                 exit(1);
214
215         /* Open the input file for reading. */
216         if ( xmlInputFileName != 0 ) {
217                 /* Open the input file for reading. */
218                 ifstream *inFile = new ifstream( xmlInputFileName );
219                 inStream = inFile;
220                 if ( ! inFile->is_open() )
221                         error() << "could not open " << xmlInputFileName << " for reading" << endl;
222         }
223         else {
224                 xmlInputFileName = "<stdin>";
225                 inStream = &cin;
226         }
227
228         /* Bail on above errors. */
229         if ( gblErrorCount > 0 )
230                 exit(1);
231
232         bool wantComplete = true;
233         bool outputActive = true;
234
235         /* Parse the input! */
236         xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete );
237
238         /* If writing to a file, delete the ostream, causing it to flush.
239          * Standard out is flushed automatically. */
240         if ( outputFileName != 0 ) {
241                 delete outStream;
242                 delete outFilter;
243         }
244
245         /* Finished, final check for errors.. */
246         if ( gblErrorCount > 0 ) {
247                 /* If we opened an output file, remove it. */
248                 if ( outputFileName != 0 )
249                         unlink( outputFileName );
250                 exit(1);
251         }
252         return 0;
253 }