Applied a patch from Andrei Polushin for setting the error message format.
[external/ragel.git] / rlgen-java / main.cpp
1 /*
2  *  Copyright 2001-2007 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 ostream &error()
79 {
80         gblErrorCount += 1;
81         cerr << PROGNAME ": ";
82         return cerr;
83 }
84
85 /*
86  * Callbacks invoked by the XML data parser.
87  */
88
89 /* Invoked by the parser when the root element is opened. */
90 ostream *openOutput( char *inputFile )
91 {
92         if ( hostLang->lang != HostLang::Java ) {
93                 error() << "this code generator is for Java only" << endl;
94                 exit(1);
95         }
96
97         /* If the output format is code and no output file name is given, then
98          * make a default. */
99         if ( outputFileName == 0 ) {
100                 char *ext = findFileExtension( inputFile );
101                 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
102                         outputFileName = fileNameFromStem( inputFile, ".h" );
103                 else
104                         outputFileName = fileNameFromStem( inputFile, ".java" );
105         }
106
107         /* Make sure we are not writing to the same file as the input file. */
108         if ( outputFileName != 0 && strcmp( inputFile, outputFileName  ) == 0 ) {
109                 error() << "output file \"" << outputFileName  << 
110                                 "\" is the same as the input file" << endl;
111         }
112
113         if ( outputFileName != 0 ) {
114                 /* Create the filter on the output and open it. */
115                 outFilter = new output_filter( outputFileName );
116                 outFilter->open( outputFileName, ios::out|ios::trunc );
117                 if ( !outFilter->is_open() ) {
118                         error() << "error opening " << outputFileName << " for writing" << endl;
119                         exit(1);
120                 }
121
122                 /* Open the output stream, attaching it to the filter. */
123                 outStream = new ostream( outFilter );
124         }
125         else {
126                 /* Writing out ot std out. */
127                 outStream = &cout;
128         }
129         return outStream;
130 }
131
132 /* Invoked by the parser when a ragel definition is opened. */
133 CodeGenData *makeCodeGen( char *sourceFileName, char *fsmName, 
134                 ostream &out, bool wantComplete )
135 {
136         CodeGenData *codeGen = new JavaTabCodeGen(out);
137
138         codeGen->sourceFileName = sourceFileName;
139         codeGen->fsmName = fsmName;
140         codeGen->wantComplete = wantComplete;
141
142         return codeGen;
143 }
144
145 /* Main, process args and call yyparse to start scanning input. */
146 int main(int argc, char **argv)
147 {
148         ParamCheck pc("o:vHh?-:", argc, argv);
149         char *xmlInputFileName = 0;
150
151         while ( pc.check() ) {
152                 switch ( pc.state ) {
153                 case ParamCheck::match:
154                         switch ( pc.parameter ) {
155                         /* Output. */
156                         case 'o':
157                                 if ( *pc.paramArg == 0 )
158                                         error() << "a zero length output file name was given" << endl;
159                                 else if ( outputFileName != 0 )
160                                         error() << "more than one output file name was given" << endl;
161                                 else {
162                                         /* Ok, remember the output file name. */
163                                         outputFileName = pc.paramArg;
164                                 }
165                                 break;
166
167                         /* Version and help. */
168                         case 'v':
169                                 version();
170                                 exit(0);
171                         case 'H': case 'h': case '?':
172                                 usage();
173                                 exit(0);
174                         case '-':
175                                 if ( strcmp(pc.paramArg, "help") == 0 ) {
176                                         usage();
177                                         exit(0);
178                                 }
179                                 else if ( strcmp(pc.paramArg, "version") == 0 ) {
180                                         version();
181                                         exit(0);
182                                 }
183                                 else {
184                                         error() << "--" << pc.paramArg << 
185                                                         " is an invalid argument" << endl;
186                                         break;
187                                 }
188                         }
189                         break;
190
191                 case ParamCheck::invalid:
192                         error() << "-" << pc.parameter << " is an invalid argument" << endl;
193                         break;
194
195                 case ParamCheck::noparam:
196                         if ( *pc.curArg == 0 )
197                                 error() << "a zero length input file name was given" << endl;
198                         else if ( xmlInputFileName != 0 )
199                                 error() << "more than one input file name was given" << endl;
200                         else {
201                                 /* OK, Remember the filename. */
202                                 xmlInputFileName = pc.curArg;
203                         }
204                         break;
205                 }
206         }
207
208         /* Bail on above errors. */
209         if ( gblErrorCount > 0 )
210                 exit(1);
211
212         /* Open the input file for reading. */
213         if ( xmlInputFileName != 0 ) {
214                 /* Open the input file for reading. */
215                 ifstream *inFile = new ifstream( xmlInputFileName );
216                 inStream = inFile;
217                 if ( ! inFile->is_open() )
218                         error() << "could not open " << xmlInputFileName << " for reading" << endl;
219         }
220         else {
221                 xmlInputFileName = strdup("<stdin>");
222                 inStream = &cin;
223         }
224
225         /* Bail on above errors. */
226         if ( gblErrorCount > 0 )
227                 exit(1);
228
229         bool wantComplete = true;
230         bool outputActive = true;
231
232         /* Parse the input! */
233         xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete );
234
235         /* If writing to a file, delete the ostream, causing it to flush.
236          * Standard out is flushed automatically. */
237         if ( outputFileName != 0 ) {
238                 delete outStream;
239                 delete outFilter;
240         }
241
242         /* Finished, final check for errors.. */
243         if ( gblErrorCount > 0 ) {
244                 /* If we opened an output file, remove it. */
245                 if ( outputFileName != 0 )
246                         unlink( outputFileName );
247                 exit(1);
248         }
249         return 0;
250 }