More work on the direct connection between frontend and backend. Now appears to
[external/ragel.git] / rlgen-java / main.cpp
1 /*
2  *  Copyright 2001-2007 Adrian Thurston <thurston@complang.org>
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 /*
55  * Callbacks invoked by the XML data parser.
56  */
57
58 /* Invoked by the parser when the root element is opened. */
59 ostream *javaOpenOutput( const char *inputFile )
60 {
61         if ( hostLang->lang != HostLang::Java ) {
62                 error() << "this code generator is for Java only" << endl;
63                 exit(1);
64         }
65
66         /* If the output format is code and no output file name is given, then
67          * make a default. */
68         if ( outputFileName == 0 ) {
69                 const char *ext = findFileExtension( inputFile );
70                 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
71                         outputFileName = fileNameFromStem( inputFile, ".h" );
72                 else
73                         outputFileName = fileNameFromStem( inputFile, ".java" );
74         }
75
76         /* Make sure we are not writing to the same file as the input file. */
77         if ( outputFileName != 0 && strcmp( inputFile, outputFileName  ) == 0 ) {
78                 error() << "output file \"" << outputFileName  << 
79                                 "\" is the same as the input file" << endl;
80         }
81
82         if ( outputFileName != 0 ) {
83                 /* Create the filter on the output and open it. */
84                 outFilter = new output_filter( outputFileName );
85                 outFilter->open( outputFileName, ios::out|ios::trunc );
86                 if ( !outFilter->is_open() ) {
87                         error() << "error opening " << outputFileName << " for writing" << endl;
88                         exit(1);
89                 }
90
91                 /* Open the output stream, attaching it to the filter. */
92                 outStream = new ostream( outFilter );
93         }
94         else {
95                 /* Writing out ot std out. */
96                 outStream = &cout;
97         }
98         return outStream;
99 }
100
101 /* Invoked by the parser when a ragel definition is opened. */
102 CodeGenData *javaMakeCodeGen( const char *sourceFileName, const char *fsmName, 
103                 ostream &out, bool wantComplete )
104 {
105         CodeGenData *codeGen = new JavaTabCodeGen(out);
106
107         codeGen->sourceFileName = sourceFileName;
108         codeGen->fsmName = fsmName;
109         codeGen->wantComplete = wantComplete;
110
111         return codeGen;
112 }