1 // Copyright (c) 2006- Facebook
2 // Distributed under the Thrift Software License
4 // See accompanying file LICENSE or visit the Thrift site at:
5 // http://developers.facebook.com/thrift/
8 * thrift - a lightweight cross-language rpc/serialization tool
10 * This file contains the main compiler engine for Thrift, which invokes the
11 * scanner/parser to build the thrift object tree. The interface generation
12 * code for each language lives in a file by the language name under the
13 * generate/ folder, and all parse structures live in parse/
15 * @author Mark Slee <mcslee@facebook.com>
22 #include <sys/types.h>
27 # include <windows.h> /* for GetFullPathName */
31 // Careful: must include globals first for extern definitions
35 #include "parse/t_program.h"
36 #include "parse/t_scope.h"
37 #include "generate/t_cpp_generator.h"
38 #include "generate/t_java_generator.h"
39 #include "generate/t_php_generator.h"
40 #include "generate/t_py_generator.h"
41 #include "generate/t_rb_generator.h"
42 #include "generate/t_xsd_generator.h"
43 #include "generate/t_perl_generator.h"
44 #include "generate/t_erl_generator.h"
45 #include "generate/t_ocaml_generator.h"
46 #include "generate/t_hs_generator.h"
47 #include "generate/t_cocoa_generator.h"
48 #include "generate/t_st_generator.h"
49 #include "generate/t_csharp_generator.h"
63 t_type
* g_type_string
;
64 t_type
* g_type_binary
;
71 t_type
* g_type_double
;
79 * Parent scope to also parse types
81 t_scope
* g_parent_scope
;
84 * Prefix for putting types in parent scope
86 string g_parent_prefix
;
91 PARSE_MODE g_parse_mode
;
94 * Current directory of file being parsed
99 * Current file being parsed
104 * Search path for inclusions
106 vector
<string
> g_incl_searchpath
;
129 * The last parsed doctext comment.
134 * The location of the last parsed doctext comment.
136 int g_doctext_lineno
;
139 * Flags to control code generation
141 bool gen_cpp
= false;
142 bool gen_dense
= false;
143 bool gen_java
= false;
144 bool gen_javabean
= false;
147 bool gen_py_newstyle
= false;
148 bool gen_xsd
= false;
149 bool gen_php
= false;
150 bool gen_phpi
= false;
151 bool gen_phps
= true;
152 bool gen_phpa
= false;
153 bool gen_phpo
= false;
154 bool gen_rest
= false;
155 bool gen_perl
= false;
156 bool gen_erl
= false;
157 bool gen_ocaml
= false;
159 bool gen_cocoa
= false;
160 bool gen_csharp
= false;
162 bool gen_recurse
= false;
165 * MinGW doesn't have realpath, so use fallback implementation in that case,
166 * otherwise this just calls through to realpath
168 char *saferealpath(const char *path
, char *resolved_path
) {
172 DWORD len
= GetFullPathName(path
, MAX_PATH
, buf
, &basename
);
173 if (len
== 0 || len
> MAX_PATH
- 1){
174 strcpy(resolved_path
, path
);
176 CharLowerBuff(buf
, len
);
177 strcpy(resolved_path
, buf
);
179 return resolved_path
;
181 return realpath(path
, resolved_path
);
187 * Report an error to the user. This is called yyerror for historical
188 * reasons (lex and yacc expect the error reporting routine to be called
189 * this). Call this function to report any errors to the user.
190 * yyerror takes printf style arguments.
192 * @param fmt C format string followed by additional arguments
194 void yyerror(char* fmt
, ...) {
197 "[ERROR:%s:%d] (last token was '%s')\n",
203 vfprintf(stderr
, fmt
, args
);
206 fprintf(stderr
, "\n");
210 * Prints a debug message from the parser.
212 * @param fmt C format string followed by additional arguments
214 void pdebug(char* fmt
, ...) {
219 printf("[PARSE:%d] ", yylineno
);
227 * Prints a verbose output mode message
229 * @param fmt C format string followed by additional arguments
231 void pverbose(char* fmt
, ...) {
232 if (g_verbose
== 0) {
242 * Prints a warning message
244 * @param fmt C format string followed by additional arguments
246 void pwarning(int level
, char* fmt
, ...) {
247 if (g_warn
< level
) {
251 printf("[WARNING:%s:%d] ", g_curpath
.c_str(), yylineno
);
259 * Prints a failure message and exits
261 * @param fmt C format string followed by additional arguments
263 void failure(const char* fmt
, ...) {
265 fprintf(stderr
, "[FAILURE:%s:%d] ", g_curpath
.c_str(), yylineno
);
267 vfprintf(stderr
, fmt
, args
);
274 * Converts a string filename into a thrift program name
276 string
program_name(string filename
) {
277 string::size_type slash
= filename
.rfind("/");
278 if (slash
!= string::npos
) {
279 filename
= filename
.substr(slash
+1);
281 string::size_type dot
= filename
.rfind(".");
282 if (dot
!= string::npos
) {
283 filename
= filename
.substr(0, dot
);
289 * Gets the directory path of a filename
291 string
directory_name(string filename
) {
292 string::size_type slash
= filename
.rfind("/");
293 // No slash, just use the current directory
294 if (slash
== string::npos
) {
297 return filename
.substr(0, slash
);
301 * Finds the appropriate file path for the given filename
303 string
include_file(string filename
) {
304 // Absolute path? Just try that
305 if (filename
[0] == '/') {
308 if (saferealpath(filename
.c_str(), rp
) == NULL
) {
309 pwarning(0, "Cannot open include file %s\n", filename
.c_str());
310 return std::string();
315 if (stat(rp
, &finfo
) == 0) {
318 } else { // relative path, start searching
319 // new search path with current dir global
320 vector
<string
> sp
= g_incl_searchpath
;
321 sp
.insert(sp
.begin(), g_curdir
);
323 // iterate through paths
324 vector
<string
>::iterator it
;
325 for (it
= sp
.begin(); it
!= sp
.end(); it
++) {
326 string sfilename
= *(it
) + "/" + filename
;
330 if (saferealpath(sfilename
.c_str(), rp
) == NULL
) {
336 if (stat(rp
, &finfo
) == 0) {
343 pwarning(0, "Could not find include file %s\n", filename
.c_str());
344 return std::string();
348 * Clears any previously stored doctext string.
349 * Also prints a warning if we are discarding information.
351 void clear_doctext() {
352 if (g_doctext
!= NULL
) {
353 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno
);
360 * Cleans up text commonly found in doxygen-like comments
362 * Warning: if you mix tabs and spaces in a non-uniform way,
363 * you will get what you deserve.
365 char* clean_up_doctext(char* doctext
) {
366 // Convert to C++ string, and remove Windows's carriage returns.
367 string docstring
= doctext
;
369 remove(docstring
.begin(), docstring
.end(), '\r'),
372 // Separate into lines.
373 vector
<string
> lines
;
374 string::size_type pos
= string::npos
;
375 string::size_type last
;
377 last
= (pos
== string::npos
) ? 0 : pos
+1;
378 pos
= docstring
.find('\n', last
);
379 if (pos
== string::npos
) {
380 // First bit of cleaning. If the last line is only whitespace, drop it.
381 string::size_type nonwhite
= docstring
.find_first_not_of(" \t", last
);
382 if (nonwhite
!= string::npos
) {
383 lines
.push_back(docstring
.substr(last
));
387 lines
.push_back(docstring
.substr(last
, pos
-last
));
390 // A very profound docstring.
395 // Clear leading whitespace from the first line.
396 pos
= lines
.front().find_first_not_of(" \t");
397 lines
.front().erase(0, pos
);
399 // If every nonblank line after the first has the same number of spaces/tabs,
400 // then a star, remove them.
401 bool have_prefix
= true;
402 bool found_prefix
= false;
403 string::size_type prefix_len
= 0;
404 vector
<string
>::iterator l_iter
;
405 for (l_iter
= lines
.begin()+1; l_iter
!= lines
.end(); ++l_iter
) {
406 if (l_iter
->empty()) {
410 pos
= l_iter
->find_first_not_of(" \t");
412 if (pos
!= string::npos
) {
413 if (l_iter
->at(pos
) == '*') {
421 // Whitespace-only line. Truncate it.
424 } else if (l_iter
->size() > pos
425 && l_iter
->at(pos
) == '*'
426 && pos
== prefix_len
) {
427 // Business as usual.
428 } else if (pos
== string::npos
) {
429 // Whitespace-only line. Let's truncate it for them.
432 // The pattern has been broken.
438 // If our prefix survived, delete it from every line.
442 for (l_iter
= lines
.begin()+1; l_iter
!= lines
.end(); ++l_iter
) {
443 l_iter
->erase(0, prefix_len
);
447 // Now delete the minimum amount of leading whitespace from each line.
448 prefix_len
= string::npos
;
449 for (l_iter
= lines
.begin()+1; l_iter
!= lines
.end(); ++l_iter
) {
450 if (l_iter
->empty()) {
453 pos
= l_iter
->find_first_not_of(" \t");
454 if (pos
!= string::npos
455 && (prefix_len
== string::npos
|| pos
< prefix_len
)) {
460 // If our prefix survived, delete it from every line.
461 if (prefix_len
!= string::npos
) {
462 for (l_iter
= lines
.begin()+1; l_iter
!= lines
.end(); ++l_iter
) {
463 l_iter
->erase(0, prefix_len
);
467 // Remove trailing whitespace from every line.
468 for (l_iter
= lines
.begin(); l_iter
!= lines
.end(); ++l_iter
) {
469 pos
= l_iter
->find_last_not_of(" \t");
470 if (pos
!= string::npos
&& pos
!= l_iter
->length()-1) {
471 l_iter
->erase(pos
+1);
475 // If the first line is empty, remove it.
476 // Don't do this earlier because a lot of steps skip the first line.
477 if (lines
.front().empty()) {
478 lines
.erase(lines
.begin());
481 // Now rejoin the lines and copy them back into doctext.
483 for (l_iter
= lines
.begin(); l_iter
!= lines
.end(); ++l_iter
) {
484 docstring
+= *l_iter
;
488 assert(docstring
.length() <= strlen(doctext
));
489 strcpy(doctext
, docstring
.c_str());
493 /** Set to true to debug docstring parsing */
494 static bool dump_docs
= false;
497 * Dumps docstrings to stdout
498 * Only works for top-level definitions and the whole program doc
499 * (i.e., not enum constants, struct fields, or functions.
501 void dump_docstrings(t_program
* program
) {
502 string progdoc
= program
->get_doc();
503 if (!progdoc
.empty()) {
504 printf("Whole program doc:\n%s\n", progdoc
.c_str());
506 const vector
<t_typedef
*>& typedefs
= program
->get_typedefs();
507 vector
<t_typedef
*>::const_iterator t_iter
;
508 for (t_iter
= typedefs
.begin(); t_iter
!= typedefs
.end(); ++t_iter
) {
509 t_typedef
* td
= *t_iter
;
511 printf("typedef %s:\n%s\n", td
->get_name().c_str(), td
->get_doc().c_str());
514 const vector
<t_enum
*>& enums
= program
->get_enums();
515 vector
<t_enum
*>::const_iterator e_iter
;
516 for (e_iter
= enums
.begin(); e_iter
!= enums
.end(); ++e_iter
) {
517 t_enum
* en
= *e_iter
;
519 printf("enum %s:\n%s\n", en
->get_name().c_str(), en
->get_doc().c_str());
522 const vector
<t_const
*>& consts
= program
->get_consts();
523 vector
<t_const
*>::const_iterator c_iter
;
524 for (c_iter
= consts
.begin(); c_iter
!= consts
.end(); ++c_iter
) {
525 t_const
* co
= *c_iter
;
527 printf("const %s:\n%s\n", co
->get_name().c_str(), co
->get_doc().c_str());
530 const vector
<t_struct
*>& structs
= program
->get_structs();
531 vector
<t_struct
*>::const_iterator s_iter
;
532 for (s_iter
= structs
.begin(); s_iter
!= structs
.end(); ++s_iter
) {
533 t_struct
* st
= *s_iter
;
535 printf("struct %s:\n%s\n", st
->get_name().c_str(), st
->get_doc().c_str());
538 const vector
<t_struct
*>& xceptions
= program
->get_xceptions();
539 vector
<t_struct
*>::const_iterator x_iter
;
540 for (x_iter
= xceptions
.begin(); x_iter
!= xceptions
.end(); ++x_iter
) {
541 t_struct
* xn
= *x_iter
;
543 printf("xception %s:\n%s\n", xn
->get_name().c_str(), xn
->get_doc().c_str());
546 const vector
<t_service
*>& services
= program
->get_services();
547 vector
<t_service
*>::const_iterator v_iter
;
548 for (v_iter
= services
.begin(); v_iter
!= services
.end(); ++v_iter
) {
549 t_service
* sv
= *v_iter
;
551 printf("service %s:\n%s\n", sv
->get_name().c_str(), sv
->get_doc().c_str());
557 * Call generate_fingerprint for every structure.
559 void generate_all_fingerprints(t_program
* program
) {
560 const vector
<t_struct
*>& structs
= program
->get_structs();
561 vector
<t_struct
*>::const_iterator s_iter
;
562 for (s_iter
= structs
.begin(); s_iter
!= structs
.end(); ++s_iter
) {
563 t_struct
* st
= *s_iter
;
564 st
->generate_fingerprint();
567 const vector
<t_struct
*>& xceptions
= program
->get_xceptions();
568 vector
<t_struct
*>::const_iterator x_iter
;
569 for (x_iter
= xceptions
.begin(); x_iter
!= xceptions
.end(); ++x_iter
) {
570 t_struct
* st
= *x_iter
;
571 st
->generate_fingerprint();
574 g_type_void
->generate_fingerprint();
576 // If you want to generate fingerprints for implicit structures, start here.
578 const vector<t_service*>& services = program->get_services();
579 vector<t_service*>::const_iterator v_iter;
580 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
581 t_service* sv = *v_iter;
587 * Diplays the usage message and then exits with an error code.
590 fprintf(stderr
, "Usage: thrift [options] file\n");
591 fprintf(stderr
, "Options:\n");
592 fprintf(stderr
, " -cpp Generate C++ output files\n");
593 fprintf(stderr
, " -java Generate Java output files\n");
594 fprintf(stderr
, " -javabean Generate Java bean-style output files\n");
595 fprintf(stderr
, " -php Generate PHP output files\n");
596 fprintf(stderr
, " -phpi Generate PHP inlined files\n");
597 fprintf(stderr
, " -phps Generate PHP server stubs (with -php)\n");
598 fprintf(stderr
, " -phpl Generate PHP-lite (with -php)\n");
599 fprintf(stderr
, " -phpa Generate PHP with autoload (with -php)\n");
600 fprintf(stderr
, " -phpo Generate PHP with object oriented subclasses (with -php)\n");
601 fprintf(stderr
, " -py Generate Python output files\n");
602 fprintf(stderr
, " -pyns Generate Python new-style classes (with -py)\n");
603 fprintf(stderr
, " -rb Generate Ruby output files\n");
604 fprintf(stderr
, " -xsd Generate XSD output files\n");
605 fprintf(stderr
, " -perl Generate Perl output files\n");
606 fprintf(stderr
, " -erl Generate Erlang output files\n");
607 fprintf(stderr
, " -ocaml Generate OCaml output files\n");
608 fprintf(stderr
, " -hs Generate Haskell output files\n");
609 fprintf(stderr
, " -cocoa Generate Cocoa/Objective-C output files\n");
610 fprintf(stderr
, " -csharp Generate C# output files\n");
611 fprintf(stderr
, " -st Generate Squeak/Smalltalk output files\n");
612 fprintf(stderr
, " -o dir Set the output directory for gen-* packages\n");
613 fprintf(stderr
, " (default: current directory)\n");
614 fprintf(stderr
, " -I dir Add a directory to the list of directories\n");
615 fprintf(stderr
, " searched for include directives\n");
616 fprintf(stderr
, " -dense Generate metadata for TDenseProtocol (C++)\n");
617 fprintf(stderr
, " -rest Generate PHP REST processors (with -php)\n");
618 fprintf(stderr
, " -nowarn Suppress all compiler warnings (BAD!)\n");
619 fprintf(stderr
, " -strict Strict compiler warnings on\n");
620 fprintf(stderr
, " -v[erbose] Verbose mode\n");
621 fprintf(stderr
, " -r[ecurse] Also generate included files\n");
622 fprintf(stderr
, " -debug Parse debug trace to stdout\n");
627 * You know, when I started working on Thrift I really thought it wasn't going
628 * to become a programming language because it was just a generator and it
629 * wouldn't need runtime type information and all that jazz. But then we
630 * decided to add constants, and all of a sudden that means runtime type
631 * validation and inference, except the "runtime" is the code generator
632 * runtime. Shit. I've been had.
634 void validate_const_rec(std::string name
, t_type
* type
, t_const_value
* value
) {
635 if (type
->is_void()) {
636 throw "type error: cannot declare a void const: " + name
;
639 if (type
->is_base_type()) {
640 t_base_type::t_base tbase
= ((t_base_type
*)type
)->get_base();
642 case t_base_type::TYPE_STRING
:
643 if (value
->get_type() != t_const_value::CV_STRING
) {
644 throw "type error: const \"" + name
+ "\" was declared as string";
647 case t_base_type::TYPE_BOOL
:
648 if (value
->get_type() != t_const_value::CV_INTEGER
) {
649 throw "type error: const \"" + name
+ "\" was declared as bool";
652 case t_base_type::TYPE_BYTE
:
653 if (value
->get_type() != t_const_value::CV_INTEGER
) {
654 throw "type error: const \"" + name
+ "\" was declared as byte";
657 case t_base_type::TYPE_I16
:
658 if (value
->get_type() != t_const_value::CV_INTEGER
) {
659 throw "type error: const \"" + name
+ "\" was declared as i16";
662 case t_base_type::TYPE_I32
:
663 if (value
->get_type() != t_const_value::CV_INTEGER
) {
664 throw "type error: const \"" + name
+ "\" was declared as i32";
667 case t_base_type::TYPE_I64
:
668 if (value
->get_type() != t_const_value::CV_INTEGER
) {
669 throw "type error: const \"" + name
+ "\" was declared as i64";
672 case t_base_type::TYPE_DOUBLE
:
673 if (value
->get_type() != t_const_value::CV_INTEGER
&&
674 value
->get_type() != t_const_value::CV_DOUBLE
) {
675 throw "type error: const \"" + name
+ "\" was declared as double";
679 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase
) + name
;
681 } else if (type
->is_enum()) {
682 if (value
->get_type() != t_const_value::CV_INTEGER
) {
683 throw "type error: const \"" + name
+ "\" was declared as enum";
685 } else if (type
->is_struct() || type
->is_xception()) {
686 if (value
->get_type() != t_const_value::CV_MAP
) {
687 throw "type error: const \"" + name
+ "\" was declared as struct/xception";
689 const vector
<t_field
*>& fields
= ((t_struct
*)type
)->get_members();
690 vector
<t_field
*>::const_iterator f_iter
;
692 const map
<t_const_value
*, t_const_value
*>& val
= value
->get_map();
693 map
<t_const_value
*, t_const_value
*>::const_iterator v_iter
;
694 for (v_iter
= val
.begin(); v_iter
!= val
.end(); ++v_iter
) {
695 if (v_iter
->first
->get_type() != t_const_value::CV_STRING
) {
696 throw "type error: " + name
+ " struct key must be string";
698 t_type
* field_type
= NULL
;
699 for (f_iter
= fields
.begin(); f_iter
!= fields
.end(); ++f_iter
) {
700 if ((*f_iter
)->get_name() == v_iter
->first
->get_string()) {
701 field_type
= (*f_iter
)->get_type();
704 if (field_type
== NULL
) {
705 throw "type error: " + type
->get_name() + " has no field " + v_iter
->first
->get_string();
708 validate_const_rec(name
+ "." + v_iter
->first
->get_string(), field_type
, v_iter
->second
);
710 } else if (type
->is_map()) {
711 t_type
* k_type
= ((t_map
*)type
)->get_key_type();
712 t_type
* v_type
= ((t_map
*)type
)->get_val_type();
713 const map
<t_const_value
*, t_const_value
*>& val
= value
->get_map();
714 map
<t_const_value
*, t_const_value
*>::const_iterator v_iter
;
715 for (v_iter
= val
.begin(); v_iter
!= val
.end(); ++v_iter
) {
716 validate_const_rec(name
+ "<key>", k_type
, v_iter
->first
);
717 validate_const_rec(name
+ "<val>", v_type
, v_iter
->second
);
719 } else if (type
->is_list() || type
->is_set()) {
721 if (type
->is_list()) {
722 e_type
= ((t_list
*)type
)->get_elem_type();
724 e_type
= ((t_set
*)type
)->get_elem_type();
726 const vector
<t_const_value
*>& val
= value
->get_list();
727 vector
<t_const_value
*>::const_iterator v_iter
;
728 for (v_iter
= val
.begin(); v_iter
!= val
.end(); ++v_iter
) {
729 validate_const_rec(name
+ "<elem>", e_type
, *v_iter
);
735 * Check the type of the parsed const information against its declared type
737 void validate_const_type(t_const
* c
) {
738 validate_const_rec(c
->get_name(), c
->get_type(), c
->get_value());
742 * Check the type of a default value assigned to a field.
744 void validate_field_value(t_field
* field
, t_const_value
* cv
) {
745 validate_const_rec(field
->get_name(), field
->get_type(), cv
);
751 void parse(t_program
* program
, t_program
* parent_program
) {
752 // Get scope file path
753 string path
= program
->get_path();
755 // Set current dir global, which is used in the include_file function
756 g_curdir
= directory_name(path
);
760 yyin
= fopen(path
.c_str(), "r");
762 failure("Could not open input file: \"%s\"", path
.c_str());
765 // Create new scope and scan for includes
766 pverbose("Scanning %s for includes\n", path
.c_str());
767 g_parse_mode
= INCLUDES
;
769 g_scope
= program
->scope();
772 if (yyparse() != 0) {
773 failure("Parser error during include pass.");
780 // Recursively parse all the include programs
781 vector
<t_program
*>& includes
= program
->get_includes();
782 vector
<t_program
*>::iterator iter
;
783 for (iter
= includes
.begin(); iter
!= includes
.end(); ++iter
) {
784 parse(*iter
, program
);
787 // Parse the program file
788 g_parse_mode
= PROGRAM
;
790 g_scope
= program
->scope();
791 g_parent_scope
= (parent_program
!= NULL
) ? parent_program
->scope() : NULL
;
792 g_parent_prefix
= program
->get_name() + ".";
794 yyin
= fopen(path
.c_str(), "r");
796 failure("Could not open input file: \"%s\"", path
.c_str());
798 pverbose("Parsing %s for types\n", path
.c_str());
801 if (yyparse() != 0) {
802 failure("Parser error during types pass.");
813 void generate(t_program
* program
) {
814 // Oooohh, recursive code generation, hot!!
816 const vector
<t_program
*>& includes
= program
->get_includes();
817 for (size_t i
= 0; i
< includes
.size(); ++i
) {
818 // Propogate output path from parent to child programs
819 includes
[i
]->set_out_path(program
->get_out_path());
821 generate(includes
[i
]);
827 pverbose("Program: %s\n", program
->get_path().c_str());
829 // Compute fingerprints.
830 generate_all_fingerprints(program
);
833 pverbose("Generating C++\n");
834 t_cpp_generator
* cpp
= new t_cpp_generator(program
, gen_dense
);
835 cpp
->generate_program();
840 pverbose("Generating Java\n");
841 t_java_generator
* java
= new t_java_generator(program
, false);
842 java
->generate_program();
847 pverbose("Generating Java Beans\n");
848 t_java_generator
* java
= new t_java_generator(program
, true);
849 java
->generate_program();
854 pverbose("Generating PHP\n");
855 t_php_generator
* php
= new t_php_generator(program
, false, gen_rest
, gen_phps
, gen_phpa
, gen_phpo
);
856 php
->generate_program();
861 pverbose("Generating PHP-inline\n");
862 t_php_generator
* phpi
= new t_php_generator(program
, true, gen_rest
);
863 phpi
->generate_program();
868 pverbose("Generating Python\n");
869 t_py_generator
* py
= new t_py_generator(program
, gen_py_newstyle
);
870 py
->generate_program();
875 pverbose("Generating Ruby\n");
876 t_rb_generator
* rb
= new t_rb_generator(program
);
877 rb
->generate_program();
882 pverbose("Generating XSD\n");
883 t_xsd_generator
* xsd
= new t_xsd_generator(program
);
884 xsd
->generate_program();
889 pverbose("Generating PERL\n");
890 t_perl_generator
* perl
= new t_perl_generator(program
);
891 perl
->generate_program();
896 pverbose("Generating Erlang\n");
897 t_erl_generator
* erl
= new t_erl_generator(program
);
898 erl
->generate_program();
903 pverbose("Generating OCaml\n");
904 t_ocaml_generator
* ocaml
= new t_ocaml_generator(program
);
905 ocaml
->generate_program();
910 pverbose("Generating Haskell\n");
911 t_hs_generator
* hs
= new t_hs_generator(program
);
912 hs
->generate_program();
917 pverbose("Generating Cocoa/Objective-C\n");
918 t_cocoa_generator
* cocoa
= new t_cocoa_generator(program
);
919 cocoa
->generate_program();
924 pverbose("Generating Smalltalk/Squeak\n");
925 t_st_generator
* st
= new t_st_generator(program
);
926 st
->generate_program();
931 pverbose("Generating C#\n");
932 t_csharp_generator
* csharp
= new t_csharp_generator(program
);
933 csharp
->generate_program();
938 dump_docstrings(program
);
941 printf("Error: %s\n", s
.c_str());
942 } catch (const char* exc
) {
943 printf("Error: %s\n", exc
);
949 * Parse it up.. then spit it back out, in pretty much every language. Alright
950 * not that many languages, but the cool ones that we care about.
952 int main(int argc
, char** argv
) {
954 std::string out_path
;
957 time_t now
= time(NULL
);
958 g_time_str
= ctime(&now
);
960 // Check for necessary arguments, you gotta have at least a filename and
961 // an output language flag
966 // Hacky parameter handling... I didn't feel like using a library sorry!
967 for (i
= 1; i
< argc
-1; i
++) {
970 arg
= strtok(argv
[i
], " ");
971 while (arg
!= NULL
) {
972 // Treat double dashes as single dashes
973 if (arg
[0] == '-' && arg
[1] == '-') {
977 if (strcmp(arg
, "-debug") == 0) {
979 } else if (strcmp(arg
, "-nowarn") == 0) {
981 } else if (strcmp(arg
, "-strict") == 0) {
983 } else if (strcmp(arg
, "-v") == 0 || strcmp(arg
, "-verbose") == 0 ) {
985 } else if (strcmp(arg
, "-r") == 0 || strcmp(arg
, "-recurse") == 0 ) {
987 } else if (strcmp(arg
, "-dense") == 0) {
989 } else if (strcmp(arg
, "-cpp") == 0) {
991 } else if (strcmp(arg
, "-javabean") == 0) {
993 } else if (strcmp(arg
, "-java") == 0) {
995 } else if (strcmp(arg
, "-php") == 0) {
997 } else if (strcmp(arg
, "-phpi") == 0) {
999 } else if (strcmp(arg
, "-phps") == 0) {
1002 } else if (strcmp(arg
, "-phpl") == 0) {
1005 } else if (strcmp(arg
, "-phpa") == 0) {
1009 } else if (strcmp(arg
, "-phpo") == 0) {
1012 } else if (strcmp(arg
, "-rest") == 0) {
1014 } else if (strcmp(arg
, "-py") == 0) {
1016 } else if (strcmp(arg
, "-pyns") == 0) {
1018 gen_py_newstyle
= true;
1019 } else if (strcmp(arg
, "-rb") == 0) {
1021 } else if (strcmp(arg
, "-xsd") == 0) {
1023 } else if (strcmp(arg
, "-perl") == 0) {
1025 } else if (strcmp(arg
, "-erl") == 0) {
1027 } else if (strcmp(arg
, "-ocaml") == 0) {
1029 } else if (strcmp(arg
, "-hs") == 0) {
1031 } else if (strcmp(arg
, "-cocoa") == 0) {
1033 } else if (strcmp(arg
, "-st") == 0) {
1035 } else if (strcmp(arg
, "-csharp") == 0) {
1037 } else if (strcmp(arg
, "-I") == 0) {
1038 // An argument of "-I\ asdf" is invalid and has unknown results
1042 fprintf(stderr
, "!!! Missing Include directory");
1045 g_incl_searchpath
.push_back(arg
);
1046 } else if (strcmp(arg
, "-o") == 0) {
1049 fprintf(stderr
, "-o: missing output directory");
1055 //strip out trailing \ on Windows
1056 int last
= out_path
.length()-1;
1057 if (out_path
[last
] == '\\')
1059 out_path
.erase(last
);
1064 if (stat(out_path
.c_str(), &sb
) < 0) {
1065 fprintf(stderr
, "Output directory %s is unusable: %s\n", out_path
.c_str(), strerror(errno
));
1068 if (! S_ISDIR(sb
.st_mode
)) {
1069 fprintf(stderr
, "Output directory %s exists but is not a directory\n", out_path
.c_str());
1073 fprintf(stderr
, "!!! Unrecognized option: %s\n", arg
);
1078 arg
= strtok(NULL
, " ");
1082 // You gotta generate something!
1083 if (!gen_cpp
&& !gen_java
&& !gen_javabean
&& !gen_php
&& !gen_phpi
&& !gen_py
&& !gen_rb
&& !gen_xsd
&& !gen_perl
&& !gen_erl
&& !gen_ocaml
&& !gen_hs
&& !gen_cocoa
&& !gen_st
&& !gen_csharp
) {
1084 fprintf(stderr
, "!!! No output language(s) specified\n\n");
1090 if (saferealpath(argv
[i
], rp
) == NULL
) {
1091 failure("Could not open input file with realpath: %s", argv
[i
]);
1093 string
input_file(rp
);
1095 // Instance of the global parse tree
1096 t_program
* program
= new t_program(input_file
);
1097 if (out_path
.size()) {
1098 program
->set_out_path(out_path
);
1101 // Initialize global types
1102 g_type_void
= new t_base_type("void", t_base_type::TYPE_VOID
);
1103 g_type_string
= new t_base_type("string", t_base_type::TYPE_STRING
);
1104 g_type_binary
= new t_base_type("string", t_base_type::TYPE_STRING
);
1105 ((t_base_type
*)g_type_binary
)->set_binary(true);
1106 g_type_slist
= new t_base_type("string", t_base_type::TYPE_STRING
);
1107 ((t_base_type
*)g_type_slist
)->set_string_list(true);
1108 g_type_bool
= new t_base_type("bool", t_base_type::TYPE_BOOL
);
1109 g_type_byte
= new t_base_type("byte", t_base_type::TYPE_BYTE
);
1110 g_type_i16
= new t_base_type("i16", t_base_type::TYPE_I16
);
1111 g_type_i32
= new t_base_type("i32", t_base_type::TYPE_I32
);
1112 g_type_i64
= new t_base_type("i64", t_base_type::TYPE_I64
);
1113 g_type_double
= new t_base_type("double", t_base_type::TYPE_DOUBLE
);
1116 parse(program
, NULL
);
1121 // Clean up. Who am I kidding... this program probably orphans heap memory
1122 // all over the place, but who cares because it is about to exit and it is
1123 // all referenced and used by this wacky parse tree up until now anyways.
1127 delete g_type_string
;
1133 delete g_type_double
;