2 * Copyright 2001-2005 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 istream *inStream = 0;
48 ostream *outStream = 0;
49 output_filter *outFilter = 0;
50 char *outputFileName = 0;
52 /* Graphviz dot file generation. */
53 bool graphvizDone = false;
55 int numSplitPartitions = 0;
56 bool printPrintables = false;
58 /* Print a summary of the options. */
62 "usage: " PROGNAME " [options] file\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"
70 /* Print version information. */
73 cout << "Ragel Code Generator for Java" << endl <<
74 "Version " VERSION << ", " PUBDATE << endl <<
75 "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
78 /* Scans a string looking for the file extension. If there is a file
79 * extension then pointer returned points to inside the string
80 * passed in. Otherwise returns null. */
81 char *findFileExtension( char *stemFile )
83 char *ppos = stemFile + strlen(stemFile) - 1;
85 /* Scan backwards from the end looking for the first dot.
86 * If we encounter a '/' before the first dot, then stop the scan. */
88 /* If we found a dot or got to the beginning of the string then
90 if ( ppos == stemFile || *ppos == '.' )
93 /* If we hit a / then there is no extension. Done. */
101 /* If we got to the front of the string then bail we
102 * did not find an extension */
103 if ( ppos == stemFile )
109 /* Make a file name from a stem. Removes the old filename suffix and
110 * replaces it with a new one. Returns a newed up string. */
111 char *fileNameFromStem( char *stemFile, char *suffix )
113 int len = strlen( stemFile );
116 /* Get the extension. */
117 char *ppos = findFileExtension( stemFile );
119 /* If an extension was found, then shorten what we think the len is. */
121 len = ppos - stemFile;
123 /* Make the return string from the stem and the suffix. */
124 char *retVal = new char[ len + strlen( suffix ) + 1 ];
125 strncpy( retVal, stemFile, len );
126 strcpy( retVal + len, suffix );
131 /* Total error count. */
132 int gblErrorCount = 0;
137 cerr << PROGNAME ": ";
141 /* Counts newlines before sending sync. */
142 int output_filter::sync( )
145 return std::filebuf::sync();
148 /* Counts newlines before sending data out to file. */
149 std::streamsize output_filter::xsputn( const char *s, std::streamsize n )
151 for ( int i = 0; i < n; i++ ) {
155 return std::filebuf::xsputn( s, n );
158 void escapeLineDirectivePath( std::ostream &out, char *path )
160 for ( char *pc = path; *pc != 0; pc++ ) {
169 * Callbacks invoked by the XML data parser.
172 /* Invoked by the parser when the root element is opened. */
173 ostream *openOutput( char *inputFile, char *language )
175 if ( strcmp( language, "Java" ) == 0 ) {
176 hostLangType = JavaCode;
177 hostLang = &hostLangJava;
180 error() << "this code genreator is for Java only" << endl;
183 /* If the output format is code and no output file name is given, then
185 if ( outputFileName == 0 ) {
186 char *ext = findFileExtension( inputFile );
187 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
188 outputFileName = fileNameFromStem( inputFile, ".h" );
190 outputFileName = fileNameFromStem( inputFile, ".java" );
193 /* Make sure we are not writing to the same file as the input file. */
194 if ( outputFileName != 0 && strcmp( inputFile, outputFileName ) == 0 ) {
195 error() << "output file \"" << outputFileName <<
196 "\" is the same as the input file" << endl;
199 if ( outputFileName != 0 ) {
200 /* Create the filter on the output and open it. */
201 outFilter = new output_filter( outputFileName );
202 outFilter->open( outputFileName, ios::out|ios::trunc );
203 if ( !outFilter->is_open() ) {
204 error() << "error opening " << outputFileName << " for writing" << endl;
208 /* Open the output stream, attaching it to the filter. */
209 outStream = new ostream( outFilter );
212 /* Writing out ot std out. */
218 /* Invoked by the parser when a ragel definition is opened. */
219 CodeGenData *makeCodeGen( char *sourceFileName, char *fsmName,
220 ostream &out, bool wantComplete )
222 CodeGenData *codeGen = new JavaTabCodeGen(out);
224 codeGen->sourceFileName = sourceFileName;
225 codeGen->fsmName = fsmName;
226 codeGen->wantComplete = wantComplete;
231 /* Main, process args and call yyparse to start scanning input. */
232 int main(int argc, char **argv)
234 ParamCheck pc("o:VpT:F:G:vHh?-:P:", argc, argv);
235 char *xmlInputFileName = 0;
237 while ( pc.check() ) {
238 switch ( pc.state ) {
239 case ParamCheck::match:
240 switch ( pc.parameter ) {
243 if ( *pc.parameterArg == 0 )
244 error() << "a zero length output file name was given" << endl;
245 else if ( outputFileName != 0 )
246 error() << "more than one output file name was given" << endl;
248 /* Ok, remember the output file name. */
249 outputFileName = pc.parameterArg;
253 /* Version and help. */
257 case 'H': case 'h': case '?':
261 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
265 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
270 error() << "--" << pc.parameterArg <<
271 " is an invalid argument" << endl;
277 case ParamCheck::invalid:
278 error() << "-" << pc.parameter << " is an invalid argument" << endl;
281 case ParamCheck::noparam:
282 if ( *pc.curArg == 0 )
283 error() << "a zero length input file name was given" << endl;
284 else if ( xmlInputFileName != 0 )
285 error() << "more than one input file name was given" << endl;
287 /* OK, Remember the filename. */
288 xmlInputFileName = pc.curArg;
294 /* Bail on above errors. */
295 if ( gblErrorCount > 0 )
298 /* Open the input file for reading. */
299 if ( xmlInputFileName != 0 ) {
300 /* Open the input file for reading. */
301 ifstream *inFile = new ifstream( xmlInputFileName );
303 if ( ! inFile->is_open() )
304 error() << "could not open " << xmlInputFileName << " for reading" << endl;
307 xmlInputFileName = "<stdin>";
311 /* Bail on above errors. */
312 if ( gblErrorCount > 0 )
315 bool wantComplete = true;
316 bool outputActive = true;
318 /* Parse the input! */
319 xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete );
321 /* If writing to a file, delete the ostream, causing it to flush.
322 * Standard out is flushed automatically. */
323 if ( outputFileName != 0 ) {
328 /* Finished, final check for errors.. */
329 if ( gblErrorCount > 0 ) {
330 /* If we opened an output file, remove it. */
331 if ( outputFileName != 0 )
332 unlink( outputFileName );