2 * Copyright 2001-2007 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
36 /* Code generators. */
37 #include "tabcodegen.h"
38 #include "ftabcodegen.h"
39 #include "flatcodegen.h"
40 #include "fflatcodegen.h"
41 #include "gotocodegen.h"
42 #include "fgotocodegen.h"
43 #include "ipgotocodegen.h"
44 #include "splitcodegen.h"
55 /* Target language and output style. */
56 CodeStyleEnum codeStyle
= GenTables
;
59 istream
*inStream
= 0;
60 ostream
*outStream
= 0;
61 output_filter
*outFilter
= 0;
62 const char *outputFileName
= 0;
64 /* Graphviz dot file generation. */
65 bool graphvizDone
= false;
67 int numSplitPartitions
= 0;
68 bool noLineDirectives
= false;
69 bool printPrintables
= false;
71 /* Print a summary of the options. */
75 "usage: " PROGNAME
" [options] file\n"
77 " -h, -H, -?, --help Print this usage and exit\n"
78 " -v, --version Print version information and exit\n"
79 " -o <file> Write output to <file>\n"
80 "code generation options:\n"
81 " -L Inhibit writing of #line directives\n"
82 "generated code style:\n"
83 " -T0 Table driven FSM (default)\n"
84 " -T1 Faster table driven FSM\n"
85 " -F0 Flat table driven FSM\n"
86 " -F1 Faster flat table-driven FSM\n"
87 " -G0 Goto-driven FSM\n"
88 " -G1 Faster goto-driven FSM\n"
89 " -G2 Really fast goto-driven FSM\n"
90 " -P<N> N-Way Split really fast goto-driven FSM\n"
94 /* Print version information. */
97 cout
<< "Ragel Code Generator for C, C++, Objective-C and D" << endl
<<
98 "Version " VERSION
<< ", " PUBDATE
<< endl
<<
99 "Copyright (c) 2001-2007 by Adrian Thurston" << endl
;
105 cerr
<< PROGNAME
": ";
110 * Callbacks invoked by the XML data parser.
113 /* Invoked by the parser when the root element is opened. */
114 ostream
*cdOpenOutput( char *inputFile
)
116 if ( hostLang
->lang
!= HostLang::C
&& hostLang
->lang
!= HostLang::D
) {
117 cd_error() << "this code generator is for C and D only" << endl
;
121 /* If the output format is code and no output file name is given, then
123 if ( outputFileName
== 0 ) {
124 char *ext
= findFileExtension( inputFile
);
125 if ( ext
!= 0 && strcmp( ext
, ".rh" ) == 0 )
126 outputFileName
= fileNameFromStem( inputFile
, ".h" );
128 const char *defExtension
= 0;
129 switch ( hostLang
->lang
) {
130 case HostLang::C
: defExtension
= ".c"; break;
131 case HostLang::D
: defExtension
= ".d"; break;
134 outputFileName
= fileNameFromStem( inputFile
, defExtension
);
138 /* Make sure we are not writing to the same file as the input file. */
139 if ( outputFileName
!= 0 && strcmp( inputFile
, outputFileName
) == 0 ) {
140 cd_error() << "output file \"" << outputFileName
<<
141 "\" is the same as the input file" << endl
;
144 if ( outputFileName
!= 0 ) {
145 /* Create the filter on the output and open it. */
146 outFilter
= new output_filter( outputFileName
);
147 outFilter
->open( outputFileName
, ios::out
|ios::trunc
);
148 if ( !outFilter
->is_open() ) {
149 cd_error() << "error opening " << outputFileName
<< " for writing" << endl
;
153 /* Open the output stream, attaching it to the filter. */
154 outStream
= new ostream( outFilter
);
157 /* Writing out ot std out. */
163 /* Invoked by the parser when a ragel definition is opened. */
164 CodeGenData
*cdMakeCodeGen( char *sourceFileName
, char *fsmName
,
165 ostream
&out
, bool wantComplete
)
167 CodeGenData
*codeGen
= 0;
168 switch ( hostLang
->lang
) {
170 switch ( codeStyle
) {
172 codeGen
= new CTabCodeGen(out
);
175 codeGen
= new CFTabCodeGen(out
);
178 codeGen
= new CFlatCodeGen(out
);
181 codeGen
= new CFFlatCodeGen(out
);
184 codeGen
= new CGotoCodeGen(out
);
187 codeGen
= new CFGotoCodeGen(out
);
190 codeGen
= new CIpGotoCodeGen(out
);
193 codeGen
= new CSplitCodeGen(out
);
199 switch ( codeStyle
) {
201 codeGen
= new DTabCodeGen(out
);
204 codeGen
= new DFTabCodeGen(out
);
207 codeGen
= new DFlatCodeGen(out
);
210 codeGen
= new DFFlatCodeGen(out
);
213 codeGen
= new DGotoCodeGen(out
);
216 codeGen
= new DFGotoCodeGen(out
);
219 codeGen
= new DIpGotoCodeGen(out
);
222 codeGen
= new DSplitCodeGen(out
);
230 codeGen
->sourceFileName
= sourceFileName
;
231 codeGen
->fsmName
= fsmName
;
232 codeGen
->wantComplete
= wantComplete
;
237 /* Main, process args and call yyparse to start scanning input. */
238 int cd_main(int argc
, const char **argv
)
240 ParamCheck
pc("-:Hh?vLo:T:F:G:P:", argc
, argv
);
241 const char *xmlInputFileName
= 0;
243 while ( pc
.check() ) {
244 switch ( pc
.state
) {
245 case ParamCheck::match
:
246 switch ( pc
.parameter
) {
249 if ( *pc
.paramArg
== 0 )
250 cd_error() << "a zero length output file name was given" << endl
;
251 else if ( outputFileName
!= 0 )
252 cd_error() << "more than one output file name was given" << endl
;
254 /* Ok, remember the output file name. */
255 outputFileName
= pc
.paramArg
;
260 noLineDirectives
= true;
265 if ( pc
.paramArg
[0] == '0' )
266 codeStyle
= GenTables
;
267 else if ( pc
.paramArg
[0] == '1' )
268 codeStyle
= GenFTables
;
270 cd_error() << "-T" << pc
.paramArg
[0] <<
271 " is an invalid argument" << endl
;
276 if ( pc
.paramArg
[0] == '0' )
278 else if ( pc
.paramArg
[0] == '1' )
279 codeStyle
= GenFFlat
;
281 cd_error() << "-F" << pc
.paramArg
[0] <<
282 " is an invalid argument" << endl
;
287 if ( pc
.paramArg
[0] == '0' )
289 else if ( pc
.paramArg
[0] == '1' )
290 codeStyle
= GenFGoto
;
291 else if ( pc
.paramArg
[0] == '2' )
292 codeStyle
= GenIpGoto
;
294 cd_error() << "-G" << pc
.paramArg
[0] <<
295 " is an invalid argument" << endl
;
300 codeStyle
= GenSplit
;
301 numSplitPartitions
= atoi( pc
.paramArg
);
304 /* Version and help. */
308 case 'H': case 'h': case '?':
312 if ( strcmp(pc
.paramArg
, "help") == 0 ) {
316 else if ( strcmp(pc
.paramArg
, "version") == 0 ) {
321 cd_error() << "--" << pc
.paramArg
<<
322 " is an invalid argument" << endl
;
328 case ParamCheck::invalid
:
329 cd_error() << "-" << pc
.parameter
<< " is an invalid argument" << endl
;
332 case ParamCheck::noparam
:
333 if ( *pc
.curArg
== 0 )
334 cd_error() << "a zero length input file name was given" << endl
;
335 else if ( xmlInputFileName
!= 0 )
336 cd_error() << "more than one input file name was given" << endl
;
338 /* OK, Remember the filename. */
339 xmlInputFileName
= pc
.curArg
;
345 /* Bail on above errors. */
346 if ( gblErrorCount
> 0 )
349 /* Open the input file for reading. */
350 if ( xmlInputFileName
!= 0 ) {
351 /* Open the input file for reading. */
352 ifstream
*inFile
= new ifstream( xmlInputFileName
);
354 if ( ! inFile
->is_open() )
355 cd_error() << "could not open " << xmlInputFileName
<< " for reading" << endl
;
358 xmlInputFileName
= strdup("<stdin>");
362 /* Bail on above errors. */
363 if ( gblErrorCount
> 0 )
366 bool wantComplete
= true;
367 bool outputActive
= true;
369 /* Parse the input! */
370 xml_parse( *inStream
, xmlInputFileName
, outputActive
, wantComplete
);
372 /* If writing to a file, delete the ostream, causing it to flush.
373 * Standard out is flushed automatically. */
374 if ( outputFileName
!= 0 ) {
379 /* Finished, final check for errors.. */
380 if ( gblErrorCount
> 0 ) {
381 /* If we opened an output file, remove it. */
382 if ( outputFileName
!= 0 )
383 unlink( outputFileName
);