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
35 /* Code generators. */
36 #include "javacodegen.h"
50 /* Target language and output style. */
51 OutputFormat outputFormat = OutCode;
52 CodeStyleEnum codeStyle = GenTables;
55 istream *inStream = 0;
56 ostream *outStream = 0;
57 output_filter *outFilter = 0;
58 char *outputFileName = 0;
60 /* Graphviz dot file generation. */
61 bool graphvizDone = false;
63 int numSplitPartitions = 0;
64 bool printPrintables = false;
66 /* Print a summary of the options. */
70 "usage: rlcodegen-java [options] file\n"
72 " -h, -H, -?, --help Print this usage and exit\n"
73 " -v, --version Print version information and exit\n"
74 " -o <file> Write output to <file>\n"
76 " -V Generate a Graphviz dotfile instead of code\n"
77 " -p Print printable characters in Graphviz output\n"
78 "generated code style:\n"
79 " -T0 Table driven FSM (default)\n"
80 " -T1 Faster table driven FSM\n"
81 " -F0 Flat table driven FSM\n"
82 " -F1 Faster flat table-driven FSM\n"
83 " -G0 Goto-driven FSM\n"
84 " -G1 Faster goto-driven FSM\n"
85 " -G2 Really fast goto-driven FSM\n"
86 " -P<N> N-Way Split really fast goto-driven FSM\n"
90 /* Print version information. */
93 cout << "Ragel Code Generator version " VERSION << " " PUBDATE << endl <<
94 "Copyright (c) 2001-2006 by Adrian Thurston" << endl;
97 /* Scans a string looking for the file extension. If there is a file
98 * extension then pointer returned points to inside the string
99 * passed in. Otherwise returns null. */
100 char *findFileExtension( char *stemFile )
102 char *ppos = stemFile + strlen(stemFile) - 1;
104 /* Scan backwards from the end looking for the first dot.
105 * If we encounter a '/' before the first dot, then stop the scan. */
107 /* If we found a dot or got to the beginning of the string then
109 if ( ppos == stemFile || *ppos == '.' )
112 /* If we hit a / then there is no extension. Done. */
113 if ( *ppos == '/' ) {
120 /* If we got to the front of the string then bail we
121 * did not find an extension */
122 if ( ppos == stemFile )
128 /* Make a file name from a stem. Removes the old filename suffix and
129 * replaces it with a new one. Returns a newed up string. */
130 char *fileNameFromStem( char *stemFile, char *suffix )
132 int len = strlen( stemFile );
135 /* Get the extension. */
136 char *ppos = findFileExtension( stemFile );
138 /* If an extension was found, then shorten what we think the len is. */
140 len = ppos - stemFile;
142 /* Make the return string from the stem and the suffix. */
143 char *retVal = new char[ len + strlen( suffix ) + 1 ];
144 strncpy( retVal, stemFile, len );
145 strcpy( retVal + len, suffix );
150 /* Total error count. */
151 int gblErrorCount = 0;
156 cerr << PROGNAME ": ";
160 /* Counts newlines before sending sync. */
161 int output_filter::sync( )
164 return std::filebuf::sync();
167 /* Counts newlines before sending data out to file. */
168 std::streamsize output_filter::xsputn( const char *s, std::streamsize n )
170 for ( int i = 0; i < n; i++ ) {
174 return std::filebuf::xsputn( s, n );
177 void escapeLineDirectivePath( std::ostream &out, char *path )
179 for ( char *pc = path; *pc != 0; pc++ ) {
188 * Callbacks invoked by the XML data parser.
191 /* Invoked by the parser when the root element is opened. */
192 ostream *openOutput( char *inputFile, char *language )
194 if ( strcmp( language, "C" ) == 0 ) {
195 hostLangType = CCode;
196 hostLang = &hostLangC;
198 else if ( strcmp( language, "D" ) == 0 ) {
199 hostLangType = DCode;
200 hostLang = &hostLangD;
202 else if ( strcmp( language, "Java" ) == 0 ) {
203 hostLangType = JavaCode;
204 hostLang = &hostLangJava;
207 /* Eventually more types will be supported. */
208 if ( hostLangType == JavaCode && codeStyle != GenTables ) {
209 error() << "java: only the table code style -T0 is "
210 "currently supported" << endl;
213 /* If the output format is code and no output file name is given, then
215 if ( outputFormat == OutCode && outputFileName == 0 ) {
216 char *ext = findFileExtension( inputFile );
217 if ( ext != 0 && strcmp( ext, ".rh" ) == 0 )
218 outputFileName = fileNameFromStem( inputFile, ".h" );
220 char *defExtension = 0;
221 switch ( hostLangType ) {
222 case CCode: defExtension = ".c"; break;
223 case DCode: defExtension = ".d"; break;
224 case JavaCode: defExtension = ".java"; break;
226 outputFileName = fileNameFromStem( inputFile, defExtension );
230 /* Make sure we are not writing to the same file as the input file. */
231 if ( outputFileName != 0 && strcmp( inputFile, outputFileName ) == 0 ) {
232 error() << "output file \"" << outputFileName <<
233 "\" is the same as the input file" << endl;
236 if ( outputFileName != 0 ) {
237 /* Create the filter on the output and open it. */
238 outFilter = new output_filter( outputFileName );
239 outFilter->open( outputFileName, ios::out|ios::trunc );
240 if ( !outFilter->is_open() ) {
241 error() << "error opening " << outputFileName << " for writing" << endl;
245 /* Open the output stream, attaching it to the filter. */
246 outStream = new ostream( outFilter );
249 /* Writing out ot std out. */
255 /* Invoked by the parser when a ragel definition is opened. */
256 CodeGenData *makeCodeGen( char *sourceFileName, char *fsmName,
257 ostream &out, bool wantComplete )
259 CodeGenData *codeGen = new JavaTabCodeGen(out);
261 codeGen->sourceFileName = sourceFileName;
262 codeGen->fsmName = fsmName;
263 codeGen->wantComplete = wantComplete;
268 /* Main, process args and call yyparse to start scanning input. */
269 int main(int argc, char **argv)
271 ParamCheck pc("o:VpT:F:G:vHh?-:P:", argc, argv);
272 char *xmlInputFileName = 0;
274 while ( pc.check() ) {
275 switch ( pc.state ) {
276 case ParamCheck::match:
277 switch ( pc.parameter ) {
280 if ( *pc.parameterArg == 0 )
281 error() << "a zero length output file name was given" << endl;
282 else if ( outputFileName != 0 )
283 error() << "more than one output file name was given" << endl;
285 /* Ok, remember the output file name. */
286 outputFileName = pc.parameterArg;
290 /* Output formats. */
292 outputFormat = OutGraphvizDot;
296 printPrintables = true;
301 if ( pc.parameterArg[0] == '0' )
302 codeStyle = GenTables;
303 else if ( pc.parameterArg[0] == '1' )
304 codeStyle = GenFTables;
306 error() << "-T" << pc.parameterArg[0] <<
307 " is an invalid argument" << endl;
312 if ( pc.parameterArg[0] == '0' )
314 else if ( pc.parameterArg[0] == '1' )
315 codeStyle = GenFFlat;
317 error() << "-F" << pc.parameterArg[0] <<
318 " is an invalid argument" << endl;
323 if ( pc.parameterArg[0] == '0' )
325 else if ( pc.parameterArg[0] == '1' )
326 codeStyle = GenFGoto;
327 else if ( pc.parameterArg[0] == '2' )
328 codeStyle = GenIpGoto;
330 error() << "-G" << pc.parameterArg[0] <<
331 " is an invalid argument" << endl;
336 codeStyle = GenSplit;
337 numSplitPartitions = atoi( pc.parameterArg );
340 /* Version and help. */
344 case 'H': case 'h': case '?':
348 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
352 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
357 error() << "--" << pc.parameterArg <<
358 " is an invalid argument" << endl;
364 case ParamCheck::invalid:
365 error() << "-" << pc.parameter << " is an invalid argument" << endl;
368 case ParamCheck::noparam:
369 if ( *pc.curArg == 0 )
370 error() << "a zero length input file name was given" << endl;
371 else if ( xmlInputFileName != 0 )
372 error() << "more than one input file name was given" << endl;
374 /* OK, Remember the filename. */
375 xmlInputFileName = pc.curArg;
381 /* Bail on above errors. */
382 if ( gblErrorCount > 0 )
385 /* Open the input file for reading. */
386 if ( xmlInputFileName != 0 ) {
387 /* Open the input file for reading. */
388 ifstream *inFile = new ifstream( xmlInputFileName );
390 if ( ! inFile->is_open() )
391 error() << "could not open " << xmlInputFileName << " for reading" << endl;
394 xmlInputFileName = "<stdin>";
398 /* Bail on above errors. */
399 if ( gblErrorCount > 0 )
402 bool wantComplete = outputFormat != OutGraphvizDot;
403 bool outputActive = outputFormat == OutCode;
405 /* Parse the input! */
406 xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete );
408 /* If writing to a file, delete the ostream, causing it to flush.
409 * Standard out is flushed automatically. */
410 if ( outputFileName != 0 ) {
415 /* Finished, final check for errors.. */
416 if ( gblErrorCount > 0 ) {
417 /* If we opened an output file, remove it. */
418 if ( outputFileName != 0 )
419 unlink( outputFileName );