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