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
;
54 /* Print a summary of the options. */
58 "usage: " PROGNAME
" [options] file\n"
60 " -h, -H, -?, --help Print this usage and exit\n"
61 " -v, --version Print version information and exit\n"
62 " -o <file> Write output to <file>\n"
66 /* Print version information. */
69 cout
<< "Ragel Code Generator for Java" << endl
<<
70 "Version " VERSION
<< ", " PUBDATE
<< endl
<<
71 "Copyright (c) 2001-2007 by Adrian Thurston" << endl
;
77 cerr
<< PROGNAME
": ";
82 * Callbacks invoked by the XML data parser.
85 /* Invoked by the parser when the root element is opened. */
86 ostream
*javaOpenOutput( char *inputFile
)
88 if ( hostLang
->lang
!= HostLang::Java
) {
89 java_error() << "this code generator is for Java only" << endl
;
93 /* If the output format is code and no output file name is given, then
95 if ( outputFileName
== 0 ) {
96 char *ext
= findFileExtension( inputFile
);
97 if ( ext
!= 0 && strcmp( ext
, ".rh" ) == 0 )
98 outputFileName
= fileNameFromStem( inputFile
, ".h" );
100 outputFileName
= fileNameFromStem( inputFile
, ".java" );
103 /* Make sure we are not writing to the same file as the input file. */
104 if ( outputFileName
!= 0 && strcmp( inputFile
, outputFileName
) == 0 ) {
105 java_error() << "output file \"" << outputFileName
<<
106 "\" is the same as the input file" << endl
;
109 if ( outputFileName
!= 0 ) {
110 /* Create the filter on the output and open it. */
111 outFilter
= new output_filter( outputFileName
);
112 outFilter
->open( outputFileName
, ios::out
|ios::trunc
);
113 if ( !outFilter
->is_open() ) {
114 java_error() << "error opening " << outputFileName
<< " for writing" << endl
;
118 /* Open the output stream, attaching it to the filter. */
119 outStream
= new ostream( outFilter
);
122 /* Writing out ot std out. */
128 /* Invoked by the parser when a ragel definition is opened. */
129 CodeGenData
*javaMakeCodeGen( char *sourceFileName
, char *fsmName
,
130 ostream
&out
, bool wantComplete
)
132 CodeGenData
*codeGen
= new JavaTabCodeGen(out
);
134 codeGen
->sourceFileName
= sourceFileName
;
135 codeGen
->fsmName
= fsmName
;
136 codeGen
->wantComplete
= wantComplete
;
141 /* Main, process args and call yyparse to start scanning input. */
142 int java_main( const char *xmlInputFileName
)
144 /* Open the input file for reading. */
145 ifstream
*inFile
= new ifstream( xmlInputFileName
);
147 if ( ! inFile
->is_open() )
148 java_error() << "could not open " << xmlInputFileName
<< " for reading" << endl
;
150 /* Bail on above errors. */
151 if ( gblErrorCount
> 0 )
154 bool wantComplete
= true;
155 bool outputActive
= true;
157 /* Parse the input! */
158 xml_parse( *inStream
, xmlInputFileName
, outputActive
, wantComplete
);
160 /* If writing to a file, delete the ostream, causing it to flush.
161 * Standard out is flushed automatically. */
162 if ( outputFileName
!= 0 ) {
167 /* Finished, final check for errors.. */
168 if ( gblErrorCount
> 0 ) {
169 /* If we opened an output file, remove it. */
170 if ( outputFileName
!= 0 )
171 unlink( outputFileName
);