Removed arg passing from frontend to backend functions.
[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
54 /* Print a summary of the options. */
55 void java_usage()
56 {
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"
63         ;       
64 }
65
66 /* Print version information. */
67 void java_version()
68 {
69         cout << "Ragel Code Generator for Java" << endl <<
70                         "Version " VERSION << ", " PUBDATE << endl <<
71                         "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
72 }
73
74 ostream &java_error()
75 {
76         gblErrorCount += 1;
77         cerr << PROGNAME ": ";
78         return cerr;
79 }
80
81 /*
82  * Callbacks invoked by the XML data parser.
83  */
84
85 /* Invoked by the parser when the root element is opened. */
86 ostream *javaOpenOutput( char *inputFile )
87 {
88         if ( hostLang->lang != HostLang::Java ) {
89                 java_error() << "this code generator is for Java only" << endl;
90                 exit(1);
91         }
92
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" );
101         }
102
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;
107         }
108
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);
116                 }
117
118                 /* Open the output stream, attaching it to the filter. */
119                 outStream = new ostream( outFilter );
120         }
121         else {
122                 /* Writing out ot std out. */
123                 outStream = &cout;
124         }
125         return outStream;
126 }
127
128 /* Invoked by the parser when a ragel definition is opened. */
129 CodeGenData *javaMakeCodeGen( char *sourceFileName, char *fsmName, 
130                 ostream &out, bool wantComplete )
131 {
132         CodeGenData *codeGen = new JavaTabCodeGen(out);
133
134         codeGen->sourceFileName = sourceFileName;
135         codeGen->fsmName = fsmName;
136         codeGen->wantComplete = wantComplete;
137
138         return codeGen;
139 }
140
141 /* Main, process args and call yyparse to start scanning input. */
142 int java_main( const char *xmlInputFileName )
143 {
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;
149
150         /* Bail on above errors. */
151         if ( gblErrorCount > 0 )
152                 exit(1);
153
154         bool wantComplete = true;
155         bool outputActive = true;
156
157         /* Parse the input! */
158         xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete );
159
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;
165         }
166
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);
173         }
174         return 0;
175 }