Removed arg passing from frontend to backend functions.
[ragel.git] / rlgen-java / main.cpp
blob06398c24ad942e461bb509a87df9c1667f3abbad
1 /*
2 * Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
3 */
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
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <iostream>
26 #include <fstream>
27 #include <unistd.h>
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"
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;
46 /* Io globals. */
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. */
55 void java_usage()
57 cout <<
58 "usage: " PROGNAME " [options] file\n"
59 "general:\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. */
67 void java_version()
69 cout << "Ragel Code Generator for Java" << endl <<
70 "Version " VERSION << ", " PUBDATE << endl <<
71 "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
74 ostream &java_error()
76 gblErrorCount += 1;
77 cerr << PROGNAME ": ";
78 return cerr;
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;
90 exit(1);
93 /* If the output format is code and no output file name is given, then
94 * make a default. */
95 if ( outputFileName == 0 ) {
96 char *ext = findFileExtension( inputFile );
97 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
98 outputFileName = fileNameFromStem( inputFile, ".h" );
99 else
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;
115 exit(1);
118 /* Open the output stream, attaching it to the filter. */
119 outStream = new ostream( outFilter );
121 else {
122 /* Writing out ot std out. */
123 outStream = &cout;
125 return outStream;
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;
138 return codeGen;
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 );
146 inStream = inFile;
147 if ( ! inFile->is_open() )
148 java_error() << "could not open " << xmlInputFileName << " for reading" << endl;
150 /* Bail on above errors. */
151 if ( gblErrorCount > 0 )
152 exit(1);
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 ) {
163 delete outStream;
164 delete outFilter;
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 );
172 exit(1);
174 return 0;