2 * Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
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
29 #include "rlgen-java.h"
35 #include "javacodegen.h"
47 extern istream *inStream;
48 extern ostream *outStream;
49 extern output_filter *outFilter;
50 extern const char *outputFileName;
52 extern int numSplitPartitions;
53 extern bool printPrintables;
55 /* Print a summary of the options. */
59 "usage: " PROGNAME " [options] file\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"
67 /* Print version information. */
70 cout << "Ragel Code Generator for Java" << endl <<
71 "Version " VERSION << ", " PUBDATE << endl <<
72 "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
78 cerr << PROGNAME ": ";
83 * Callbacks invoked by the XML data parser.
86 /* Invoked by the parser when the root element is opened. */
87 ostream *javaOpenOutput( char *inputFile )
89 if ( hostLang->lang != HostLang::Java ) {
90 java_error() << "this code generator is for Java only" << endl;
94 /* If the output format is code and no output file name is given, then
96 if ( outputFileName == 0 ) {
97 char *ext = findFileExtension( inputFile );
98 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
99 outputFileName = fileNameFromStem( inputFile, ".h" );
101 outputFileName = fileNameFromStem( inputFile, ".java" );
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;
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;
119 /* Open the output stream, attaching it to the filter. */
120 outStream = new ostream( outFilter );
123 /* Writing out ot std out. */
129 /* Invoked by the parser when a ragel definition is opened. */
130 CodeGenData *javaMakeCodeGen( char *sourceFileName, char *fsmName,
131 ostream &out, bool wantComplete )
133 CodeGenData *codeGen = new JavaTabCodeGen(out);
135 codeGen->sourceFileName = sourceFileName;
136 codeGen->fsmName = fsmName;
137 codeGen->wantComplete = wantComplete;
142 /* Main, process args and call yyparse to start scanning input. */
143 int java_main(int argc, const char **argv)
145 ParamCheck pc("o:vHh?-:", argc, argv);
146 const char *xmlInputFileName = 0;
148 while ( pc.check() ) {
149 switch ( pc.state ) {
150 case ParamCheck::match:
151 switch ( pc.parameter ) {
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;
159 /* Ok, remember the output file name. */
160 outputFileName = pc.paramArg;
164 /* Version and help. */
168 case 'H': case 'h': case '?':
172 if ( strcmp(pc.paramArg, "help") == 0 ) {
176 else if ( strcmp(pc.paramArg, "version") == 0 ) {
181 java_error() << "--" << pc.paramArg <<
182 " is an invalid argument" << endl;
188 case ParamCheck::invalid:
189 java_error() << "-" << pc.parameter << " is an invalid argument" << endl;
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;
198 /* OK, Remember the filename. */
199 xmlInputFileName = pc.curArg;
205 /* Bail on above errors. */
206 if ( gblErrorCount > 0 )
209 /* Open the input file for reading. */
210 if ( xmlInputFileName != 0 ) {
211 /* Open the input file for reading. */
212 ifstream *inFile = new ifstream( xmlInputFileName );
214 if ( ! inFile->is_open() )
215 java_error() << "could not open " << xmlInputFileName << " for reading" << endl;
218 xmlInputFileName = strdup("<stdin>");
222 /* Bail on above errors. */
223 if ( gblErrorCount > 0 )
226 bool wantComplete = true;
227 bool outputActive = true;
229 /* Parse the input! */
230 xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete );
232 /* If writing to a file, delete the ostream, causing it to flush.
233 * Standard out is flushed automatically. */
234 if ( outputFileName != 0 ) {
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 );