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>
26 // Careful: must include globals first for extern definitions
30 #include "parse/t_program.h"
31 #include "parse/t_scope.h"
32 #include "generate/t_cpp_generator.h"
33 #include "generate/t_java_generator.h"
34 #include "generate/t_php_generator.h"
35 #include "generate/t_py_generator.h"
36 #include "generate/t_rb_generator.h"
37 #include "generate/t_xsd_generator.h"
38 #include "generate/t_perl_generator.h"
39 #include "generate/t_erl_generator.h"
40 #include "generate/t_ocaml_generator.h"
41 #include "generate/t_hs_generator.h"
42 #include "generate/t_cocoa_generator.h"
43 #include "generate/t_st_generator.h"
57 t_type
* g_type_string
;
58 t_type
* g_type_binary
;
65 t_type
* g_type_double
;
73 * Parent scope to also parse types
75 t_scope
* g_parent_scope
;
78 * Prefix for putting types in parent scope
80 string g_parent_prefix
;
85 PARSE_MODE g_parse_mode
;
88 * Current directory of file being parsed
93 * Current file being parsed
98 * Search path for inclusions
100 vector
<string
> g_incl_searchpath
;
123 * The last parsed doctext comment.
128 * The location of the last parsed doctext comment.
130 int g_doctext_lineno
;
133 * Flags to control code generation
135 bool gen_cpp
= false;
136 bool gen_java
= false;
137 bool gen_javabean
= false;
140 bool gen_xsd
= false;
141 bool gen_php
= false;
142 bool gen_phpi
= false;
143 bool gen_phps
= true;
144 bool gen_phpa
= false;
145 bool gen_rest
= false;
146 bool gen_perl
= false;
147 bool gen_erl
= false;
148 bool gen_ocaml
= false;
150 bool gen_cocoa
= false;
152 bool gen_dense
= false;
153 bool gen_recurse
= false;
156 * Report an error to the user. This is called yyerror for historical
157 * reasons (lex and yacc expect the error reporting routine to be called
158 * this). Call this function to report any errors to the user.
159 * yyerror takes printf style arguments.
161 * @param fmt C format string followed by additional arguments
163 void yyerror(char* fmt
, ...) {
166 "[ERROR:%s:%d] (last token was '%s')\n",
172 vfprintf(stderr
, fmt
, args
);
175 fprintf(stderr
, "\n");
179 * Prints a debug message from the parser.
181 * @param fmt C format string followed by additional arguments
183 void pdebug(char* fmt
, ...) {
188 printf("[PARSE:%d] ", yylineno
);
196 * Prints a verbose output mode message
198 * @param fmt C format string followed by additional arguments
200 void pverbose(char* fmt
, ...) {
201 if (g_verbose
== 0) {
211 * Prints a warning message
213 * @param fmt C format string followed by additional arguments
215 void pwarning(int level
, char* fmt
, ...) {
216 if (g_warn
< level
) {
220 printf("[WARNING:%s:%d] ", g_curpath
.c_str(), yylineno
);
228 * Prints a failure message and exits
230 * @param fmt C format string followed by additional arguments
232 void failure(const char* fmt
, ...) {
234 fprintf(stderr
, "[FAILURE:%s:%d] ", g_curpath
.c_str(), yylineno
);
236 vfprintf(stderr
, fmt
, args
);
243 * Converts a string filename into a thrift program name
245 string
program_name(string filename
) {
246 string::size_type slash
= filename
.rfind("/");
247 if (slash
!= string::npos
) {
248 filename
= filename
.substr(slash
+1);
250 string::size_type dot
= filename
.rfind(".");
251 if (dot
!= string::npos
) {
252 filename
= filename
.substr(0, dot
);
258 * Gets the directory path of a filename
260 string
directory_name(string filename
) {
261 string::size_type slash
= filename
.rfind("/");
262 // No slash, just use the current directory
263 if (slash
== string::npos
) {
266 return filename
.substr(0, slash
);
270 * Finds the appropriate file path for the given filename
272 string
include_file(string filename
) {
273 // Absolute path? Just try that
274 if (filename
[0] == '/') {
277 if (realpath(filename
.c_str(), rp
) == NULL
) {
278 pwarning(0, "Cannot open include file %s\n", filename
.c_str());
279 return std::string();
284 if (stat(rp
, &finfo
) == 0) {
287 } else { // relative path, start searching
288 // new search path with current dir global
289 vector
<string
> sp
= g_incl_searchpath
;
290 sp
.insert(sp
.begin(), g_curdir
);
292 // iterate through paths
293 vector
<string
>::iterator it
;
294 for (it
= sp
.begin(); it
!= sp
.end(); it
++) {
295 string sfilename
= *(it
) + "/" + filename
;
299 if (realpath(sfilename
.c_str(), rp
) == NULL
) {
305 if (stat(rp
, &finfo
) == 0) {
312 pwarning(0, "Could not find include file %s\n", filename
.c_str());
313 return std::string();
317 * Clears any previously stored doctext string.
318 * Also prints a warning if we are discarding information.
320 void clear_doctext() {
321 if (g_doctext
!= NULL
) {
322 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno
);
329 * Cleans up text commonly found in doxygen-like comments
331 * Warning: if you mix tabs and spaces in a non-uniform way,
332 * you will get what you deserve.
334 char* clean_up_doctext(char* doctext
) {
335 // Convert to C++ string, and remove Windows's carriage returns.
336 string docstring
= doctext
;
338 remove(docstring
.begin(), docstring
.end(), '\r'),
341 // Separate into lines.
342 vector
<string
> lines
;
343 string::size_type pos
= string::npos
;
344 string::size_type last
;
346 last
= (pos
== string::npos
) ? 0 : pos
+1;
347 pos
= docstring
.find('\n', last
);
348 if (pos
== string::npos
) {
349 // First bit of cleaning. If the last line is only whitespace, drop it.
350 string::size_type nonwhite
= docstring
.find_first_not_of(" \t", last
);
351 if (nonwhite
!= string::npos
) {
352 lines
.push_back(docstring
.substr(last
));
356 lines
.push_back(docstring
.substr(last
, pos
-last
));
359 // A very profound docstring.
364 // Clear leading whitespace from the first line.
365 pos
= lines
.front().find_first_not_of(" \t");
366 lines
.front().erase(0, pos
);
368 // If every nonblank line after the first has the same number of spaces/tabs,
369 // then a star, remove them.
370 bool have_prefix
= true;
371 bool found_prefix
= false;
372 string::size_type prefix_len
= 0;
373 vector
<string
>::iterator l_iter
;
374 for (l_iter
= lines
.begin()+1; l_iter
!= lines
.end(); ++l_iter
) {
375 if (l_iter
->empty()) {
379 pos
= l_iter
->find_first_not_of(" \t");
381 if (pos
!= string::npos
) {
382 if (l_iter
->at(pos
) == '*') {
390 // Whitespace-only line. Truncate it.
393 } else if (l_iter
->size() > pos
394 && l_iter
->at(pos
) == '*'
395 && pos
== prefix_len
) {
396 // Business as usual.
397 } else if (pos
== string::npos
) {
398 // Whitespace-only line. Let's truncate it for them.
401 // The pattern has been broken.
407 // If our prefix survived, delete it from every line.
411 for (l_iter
= lines
.begin()+1; l_iter
!= lines
.end(); ++l_iter
) {
412 l_iter
->erase(0, prefix_len
);
416 // Now delete the minimum amount of leading whitespace from each line.
417 prefix_len
= string::npos
;
418 for (l_iter
= lines
.begin()+1; l_iter
!= lines
.end(); ++l_iter
) {
419 if (l_iter
->empty()) {
422 pos
= l_iter
->find_first_not_of(" \t");
423 if (pos
!= string::npos
424 && (prefix_len
== string::npos
|| pos
< prefix_len
)) {
429 // If our prefix survived, delete it from every line.
430 if (prefix_len
!= string::npos
) {
431 for (l_iter
= lines
.begin()+1; l_iter
!= lines
.end(); ++l_iter
) {
432 l_iter
->erase(0, prefix_len
);
436 // Remove trailing whitespace from every line.
437 for (l_iter
= lines
.begin(); l_iter
!= lines
.end(); ++l_iter
) {
438 pos
= l_iter
->find_last_not_of(" \t");
439 if (pos
!= string::npos
&& pos
!= l_iter
->length()-1) {
440 l_iter
->erase(pos
+1);
444 // If the first line is empty, remove it.
445 // Don't do this earlier because a lot of steps skip the first line.
446 if (lines
.front().empty()) {
447 lines
.erase(lines
.begin());
450 // Now rejoin the lines and copy them back into doctext.
452 for (l_iter
= lines
.begin(); l_iter
!= lines
.end(); ++l_iter
) {
453 docstring
+= *l_iter
;
457 assert(docstring
.length() <= strlen(doctext
));
458 strcpy(doctext
, docstring
.c_str());
462 /** Set to true to debug docstring parsing */
463 static bool dump_docs
= false;
466 * Dumps docstrings to stdout
467 * Only works for top-level definitions and the whole program doc
468 * (i.e., not enum constants, struct fields, or functions.
470 void dump_docstrings(t_program
* program
) {
471 string progdoc
= program
->get_doc();
472 if (!progdoc
.empty()) {
473 printf("Whole program doc:\n%s\n", progdoc
.c_str());
475 const vector
<t_typedef
*>& typedefs
= program
->get_typedefs();
476 vector
<t_typedef
*>::const_iterator t_iter
;
477 for (t_iter
= typedefs
.begin(); t_iter
!= typedefs
.end(); ++t_iter
) {
478 t_typedef
* td
= *t_iter
;
480 printf("typedef %s:\n%s\n", td
->get_name().c_str(), td
->get_doc().c_str());
483 const vector
<t_enum
*>& enums
= program
->get_enums();
484 vector
<t_enum
*>::const_iterator e_iter
;
485 for (e_iter
= enums
.begin(); e_iter
!= enums
.end(); ++e_iter
) {
486 t_enum
* en
= *e_iter
;
488 printf("enum %s:\n%s\n", en
->get_name().c_str(), en
->get_doc().c_str());
491 const vector
<t_const
*>& consts
= program
->get_consts();
492 vector
<t_const
*>::const_iterator c_iter
;
493 for (c_iter
= consts
.begin(); c_iter
!= consts
.end(); ++c_iter
) {
494 t_const
* co
= *c_iter
;
496 printf("const %s:\n%s\n", co
->get_name().c_str(), co
->get_doc().c_str());
499 const vector
<t_struct
*>& structs
= program
->get_structs();
500 vector
<t_struct
*>::const_iterator s_iter
;
501 for (s_iter
= structs
.begin(); s_iter
!= structs
.end(); ++s_iter
) {
502 t_struct
* st
= *s_iter
;
504 printf("struct %s:\n%s\n", st
->get_name().c_str(), st
->get_doc().c_str());
507 const vector
<t_struct
*>& xceptions
= program
->get_xceptions();
508 vector
<t_struct
*>::const_iterator x_iter
;
509 for (x_iter
= xceptions
.begin(); x_iter
!= xceptions
.end(); ++x_iter
) {
510 t_struct
* xn
= *x_iter
;
512 printf("xception %s:\n%s\n", xn
->get_name().c_str(), xn
->get_doc().c_str());
515 const vector
<t_service
*>& services
= program
->get_services();
516 vector
<t_service
*>::const_iterator v_iter
;
517 for (v_iter
= services
.begin(); v_iter
!= services
.end(); ++v_iter
) {
518 t_service
* sv
= *v_iter
;
520 printf("service %s:\n%s\n", sv
->get_name().c_str(), sv
->get_doc().c_str());
526 * Call generate_fingerprint for every structure.
528 void generate_all_fingerprints(t_program
* program
) {
529 const vector
<t_struct
*>& structs
= program
->get_structs();
530 vector
<t_struct
*>::const_iterator s_iter
;
531 for (s_iter
= structs
.begin(); s_iter
!= structs
.end(); ++s_iter
) {
532 t_struct
* st
= *s_iter
;
533 st
->generate_fingerprint();
536 const vector
<t_struct
*>& xceptions
= program
->get_xceptions();
537 vector
<t_struct
*>::const_iterator x_iter
;
538 for (x_iter
= xceptions
.begin(); x_iter
!= xceptions
.end(); ++x_iter
) {
539 t_struct
* st
= *x_iter
;
540 st
->generate_fingerprint();
543 g_type_void
->generate_fingerprint();
545 // If you want to generate fingerprints for implicit structures, start here.
547 const vector<t_service*>& services = program->get_services();
548 vector<t_service*>::const_iterator v_iter;
549 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
550 t_service* sv = *v_iter;
556 * Diplays the usage message and then exits with an error code.
559 fprintf(stderr
, "Usage: thrift [options] file\n");
560 fprintf(stderr
, "Options:\n");
561 fprintf(stderr
, " -cpp Generate C++ output files\n");
562 fprintf(stderr
, " -java Generate Java output files\n");
563 fprintf(stderr
, " -javabean Generate Java bean-style output files\n");
564 fprintf(stderr
, " -php Generate PHP output files\n");
565 fprintf(stderr
, " -phpi Generate PHP inlined files\n");
566 fprintf(stderr
, " -phps Generate PHP server stubs (with -php)\n");
567 fprintf(stderr
, " -phpl Generate PHP-lite (with -php)\n");
568 fprintf(stderr
, " -phpa Generate PHP with autoload (with -php)\n");
569 fprintf(stderr
, " -py Generate Python output files\n");
570 fprintf(stderr
, " -rb Generate Ruby output files\n");
571 fprintf(stderr
, " -xsd Generate XSD output files\n");
572 fprintf(stderr
, " -perl Generate Perl output files\n");
573 fprintf(stderr
, " -erl Generate Erlang output files\n");
574 fprintf(stderr
, " -ocaml Generate OCaml output files\n");
575 fprintf(stderr
, " -hs Generate Haskell output files\n");
576 fprintf(stderr
, " -cocoa Generate Cocoa/Objective-C output files\n");
577 fprintf(stderr
, " -st Generate Squeak/Smalltalk output files\n");
578 fprintf(stderr
, " -o dir Set the output directory for gen-* packages\n");
579 fprintf(stderr
, " (default: current directory)\n");
580 fprintf(stderr
, " -I dir Add a directory to the list of directories\n");
581 fprintf(stderr
, " searched for include directives\n");
582 fprintf(stderr
, " -dense Generate metadata for TDenseProtocol (C++)\n");
583 fprintf(stderr
, " -rest Generate PHP REST processors (with -php)\n");
584 fprintf(stderr
, " -nowarn Suppress all compiler warnings (BAD!)\n");
585 fprintf(stderr
, " -strict Strict compiler warnings on\n");
586 fprintf(stderr
, " -v[erbose] Verbose mode\n");
587 fprintf(stderr
, " -r[ecurse] Also generate included files\n");
588 fprintf(stderr
, " -debug Parse debug trace to stdout\n");
593 * You know, when I started working on Thrift I really thought it wasn't going
594 * to become a programming language because it was just a generator and it
595 * wouldn't need runtime type information and all that jazz. But then we
596 * decided to add constants, and all of a sudden that means runtime type
597 * validation and inference, except the "runtime" is the code generator
598 * runtime. Shit. I've been had.
600 void validate_const_rec(std::string name
, t_type
* type
, t_const_value
* value
) {
601 if (type
->is_void()) {
602 throw "type error: cannot declare a void const: " + name
;
605 if (type
->is_base_type()) {
606 t_base_type::t_base tbase
= ((t_base_type
*)type
)->get_base();
608 case t_base_type::TYPE_STRING
:
609 if (value
->get_type() != t_const_value::CV_STRING
) {
610 throw "type error: const \"" + name
+ "\" was declared as string";
613 case t_base_type::TYPE_BOOL
:
614 if (value
->get_type() != t_const_value::CV_INTEGER
) {
615 throw "type error: const \"" + name
+ "\" was declared as bool";
618 case t_base_type::TYPE_BYTE
:
619 if (value
->get_type() != t_const_value::CV_INTEGER
) {
620 throw "type error: const \"" + name
+ "\" was declared as byte";
623 case t_base_type::TYPE_I16
:
624 if (value
->get_type() != t_const_value::CV_INTEGER
) {
625 throw "type error: const \"" + name
+ "\" was declared as i16";
628 case t_base_type::TYPE_I32
:
629 if (value
->get_type() != t_const_value::CV_INTEGER
) {
630 throw "type error: const \"" + name
+ "\" was declared as i32";
633 case t_base_type::TYPE_I64
:
634 if (value
->get_type() != t_const_value::CV_INTEGER
) {
635 throw "type error: const \"" + name
+ "\" was declared as i64";
638 case t_base_type::TYPE_DOUBLE
:
639 if (value
->get_type() != t_const_value::CV_INTEGER
&&
640 value
->get_type() != t_const_value::CV_DOUBLE
) {
641 throw "type error: const \"" + name
+ "\" was declared as double";
645 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase
) + name
;
647 } else if (type
->is_enum()) {
648 if (value
->get_type() != t_const_value::CV_INTEGER
) {
649 throw "type error: const \"" + name
+ "\" was declared as enum";
651 } else if (type
->is_struct() || type
->is_xception()) {
652 if (value
->get_type() != t_const_value::CV_MAP
) {
653 throw "type error: const \"" + name
+ "\" was declared as struct/xception";
655 const vector
<t_field
*>& fields
= ((t_struct
*)type
)->get_members();
656 vector
<t_field
*>::const_iterator f_iter
;
658 const map
<t_const_value
*, t_const_value
*>& val
= value
->get_map();
659 map
<t_const_value
*, t_const_value
*>::const_iterator v_iter
;
660 for (v_iter
= val
.begin(); v_iter
!= val
.end(); ++v_iter
) {
661 if (v_iter
->first
->get_type() != t_const_value::CV_STRING
) {
662 throw "type error: " + name
+ " struct key must be string";
664 t_type
* field_type
= NULL
;
665 for (f_iter
= fields
.begin(); f_iter
!= fields
.end(); ++f_iter
) {
666 if ((*f_iter
)->get_name() == v_iter
->first
->get_string()) {
667 field_type
= (*f_iter
)->get_type();
670 if (field_type
== NULL
) {
671 throw "type error: " + type
->get_name() + " has no field " + v_iter
->first
->get_string();
674 validate_const_rec(name
+ "." + v_iter
->first
->get_string(), field_type
, v_iter
->second
);
676 } else if (type
->is_map()) {
677 t_type
* k_type
= ((t_map
*)type
)->get_key_type();
678 t_type
* v_type
= ((t_map
*)type
)->get_val_type();
679 const map
<t_const_value
*, t_const_value
*>& val
= value
->get_map();
680 map
<t_const_value
*, t_const_value
*>::const_iterator v_iter
;
681 for (v_iter
= val
.begin(); v_iter
!= val
.end(); ++v_iter
) {
682 validate_const_rec(name
+ "<key>", k_type
, v_iter
->first
);
683 validate_const_rec(name
+ "<val>", v_type
, v_iter
->second
);
685 } else if (type
->is_list() || type
->is_set()) {
687 if (type
->is_list()) {
688 e_type
= ((t_list
*)type
)->get_elem_type();
690 e_type
= ((t_set
*)type
)->get_elem_type();
692 const vector
<t_const_value
*>& val
= value
->get_list();
693 vector
<t_const_value
*>::const_iterator v_iter
;
694 for (v_iter
= val
.begin(); v_iter
!= val
.end(); ++v_iter
) {
695 validate_const_rec(name
+ "<elem>", e_type
, *v_iter
);
701 * Check the type of the parsed const information against its declared type
703 void validate_const_type(t_const
* c
) {
704 validate_const_rec(c
->get_name(), c
->get_type(), c
->get_value());
708 * Check the type of a default value assigned to a field.
710 void validate_field_value(t_field
* field
, t_const_value
* cv
) {
711 validate_const_rec(field
->get_name(), field
->get_type(), cv
);
717 void parse(t_program
* program
, t_program
* parent_program
) {
718 // Get scope file path
719 string path
= program
->get_path();
721 // Set current dir global, which is used in the include_file function
722 g_curdir
= directory_name(path
);
726 yyin
= fopen(path
.c_str(), "r");
728 failure("Could not open input file: \"%s\"", path
.c_str());
731 // Create new scope and scan for includes
732 pverbose("Scanning %s for includes\n", path
.c_str());
733 g_parse_mode
= INCLUDES
;
735 g_scope
= program
->scope();
738 if (yyparse() != 0) {
739 failure("Parser error during include pass.");
746 // Recursively parse all the include programs
747 vector
<t_program
*>& includes
= program
->get_includes();
748 vector
<t_program
*>::iterator iter
;
749 for (iter
= includes
.begin(); iter
!= includes
.end(); ++iter
) {
750 parse(*iter
, program
);
753 // Parse the program the file
754 g_parse_mode
= PROGRAM
;
756 g_scope
= program
->scope();
757 g_parent_scope
= (parent_program
!= NULL
) ? parent_program
->scope() : NULL
;
758 g_parent_prefix
= program
->get_name() + ".";
760 yyin
= fopen(path
.c_str(), "r");
762 failure("Could not open input file: \"%s\"", path
.c_str());
764 pverbose("Parsing %s for types\n", path
.c_str());
767 if (yyparse() != 0) {
768 failure("Parser error during types pass.");
779 void generate(t_program
* program
) {
780 // Oooohh, recursive code generation, hot!!
782 const vector
<t_program
*>& includes
= program
->get_includes();
783 for (size_t i
= 0; i
< includes
.size(); ++i
) {
784 // Propogate output path from parent to child programs
785 includes
[i
]->set_out_path(program
->get_out_path());
787 generate(includes
[i
]);
793 pverbose("Program: %s\n", program
->get_path().c_str());
795 // Compute fingerprints.
796 generate_all_fingerprints(program
);
799 pverbose("Generating C++\n");
800 t_cpp_generator
* cpp
= new t_cpp_generator(program
, gen_dense
);
801 cpp
->generate_program();
806 pverbose("Generating Java\n");
807 t_java_generator
* java
= new t_java_generator(program
, false);
808 java
->generate_program();
813 pverbose("Generating Java Beans\n");
814 t_java_generator
* java
= new t_java_generator(program
, true);
815 java
->generate_program();
820 pverbose("Generating PHP\n");
821 t_php_generator
* php
= new t_php_generator(program
, false, gen_rest
, gen_phps
, gen_phpa
);
822 php
->generate_program();
827 pverbose("Generating PHP-inline\n");
828 t_php_generator
* phpi
= new t_php_generator(program
, true, gen_rest
);
829 phpi
->generate_program();
834 pverbose("Generating Python\n");
835 t_py_generator
* py
= new t_py_generator(program
);
836 py
->generate_program();
841 pverbose("Generating Ruby\n");
842 t_rb_generator
* rb
= new t_rb_generator(program
);
843 rb
->generate_program();
848 pverbose("Generating XSD\n");
849 t_xsd_generator
* xsd
= new t_xsd_generator(program
);
850 xsd
->generate_program();
855 pverbose("Generating PERL\n");
856 t_perl_generator
* perl
= new t_perl_generator(program
);
857 perl
->generate_program();
862 pverbose("Generating Erlang\n");
863 t_erl_generator
* erl
= new t_erl_generator(program
);
864 erl
->generate_program();
869 pverbose("Generating OCaml\n");
870 t_ocaml_generator
* ocaml
= new t_ocaml_generator(program
);
871 ocaml
->generate_program();
876 pverbose("Generating Haskell\n");
877 t_hs_generator
* hs
= new t_hs_generator(program
);
878 hs
->generate_program();
883 pverbose("Generating Cocoa/Objective-C\n");
884 t_cocoa_generator
* cocoa
= new t_cocoa_generator(program
);
885 cocoa
->generate_program();
890 pverbose("Generating Smalltalk/Squeak\n");
891 t_st_generator
* st
= new t_st_generator(program
);
892 st
->generate_program();
897 dump_docstrings(program
);
900 printf("Error: %s\n", s
.c_str());
901 } catch (const char* exc
) {
902 printf("Error: %s\n", exc
);
908 * Parse it up.. then spit it back out, in pretty much every language. Alright
909 * not that many languages, but the cool ones that we care about.
911 int main(int argc
, char** argv
) {
913 std::string out_path
;
916 time_t now
= time(NULL
);
917 g_time_str
= ctime(&now
);
919 // Check for necessary arguments, you gotta have at least a filename and
920 // an output language flag
925 // Hacky parameter handling... I didn't feel like using a library sorry!
926 for (i
= 1; i
< argc
-1; i
++) {
929 arg
= strtok(argv
[i
], " ");
930 while (arg
!= NULL
) {
931 // Treat double dashes as single dashes
932 if (arg
[0] == '-' && arg
[1] == '-') {
936 if (strcmp(arg
, "-debug") == 0) {
938 } else if (strcmp(arg
, "-nowarn") == 0) {
940 } else if (strcmp(arg
, "-strict") == 0) {
942 } else if (strcmp(arg
, "-v") == 0 || strcmp(arg
, "-verbose") == 0 ) {
944 } else if (strcmp(arg
, "-r") == 0 || strcmp(arg
, "-recurse") == 0 ) {
946 } else if (strcmp(arg
, "-dense") == 0) {
948 } else if (strcmp(arg
, "-cpp") == 0) {
950 } else if (strcmp(arg
, "-javabean") == 0) {
952 } else if (strcmp(arg
, "-java") == 0) {
954 } else if (strcmp(arg
, "-php") == 0) {
956 } else if (strcmp(arg
, "-phpi") == 0) {
958 } else if (strcmp(arg
, "-phps") == 0) {
963 } else if (strcmp(arg
, "-phpl") == 0) {
968 } else if (strcmp(arg
, "-phpa") == 0) {
974 } else if (strcmp(arg
, "-rest") == 0) {
976 } else if (strcmp(arg
, "-py") == 0) {
978 } else if (strcmp(arg
, "-rb") == 0) {
980 } else if (strcmp(arg
, "-xsd") == 0) {
982 } else if (strcmp(arg
, "-perl") == 0) {
984 } else if (strcmp(arg
, "-erl") == 0) {
986 } else if (strcmp(arg
, "-ocaml") == 0) {
988 } else if (strcmp(arg
, "-hs") == 0) {
990 } else if (strcmp(arg
, "-cocoa") == 0) {
992 } else if (strcmp(arg
, "-st") == 0) {
994 } else if (strcmp(arg
, "-I") == 0) {
995 // An argument of "-I\ asdf" is invalid and has unknown results
999 fprintf(stderr
, "!!! Missing Include directory");
1002 g_incl_searchpath
.push_back(arg
);
1003 } else if (strcmp(arg
, "-o") == 0) {
1006 fprintf(stderr
, "-o: missing output directory");
1011 if (stat(out_path
.c_str(), &sb
) < 0) {
1012 fprintf(stderr
, "Output directory %s is unusable: %s\n", out_path
.c_str(), strerror(errno
));
1015 if (! S_ISDIR(sb
.st_mode
)) {
1016 fprintf(stderr
, "Output directory %s exists but is not a directory\n", out_path
.c_str());
1020 fprintf(stderr
, "!!! Unrecognized option: %s\n", arg
);
1025 arg
= strtok(NULL
, " ");
1029 // You gotta generate something!
1030 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
) {
1031 fprintf(stderr
, "!!! No output language(s) specified\n\n");
1037 if (realpath(argv
[i
], rp
) == NULL
) {
1038 failure("Could not open input file: %s", argv
[i
]);
1040 string
input_file(rp
);
1042 // Instance of the global parse tree
1043 t_program
* program
= new t_program(input_file
);
1044 if (out_path
.size()) {
1045 program
->set_out_path(out_path
);
1048 // Initialize global types
1049 g_type_void
= new t_base_type("void", t_base_type::TYPE_VOID
);
1050 g_type_string
= new t_base_type("string", t_base_type::TYPE_STRING
);
1051 g_type_binary
= new t_base_type("string", t_base_type::TYPE_STRING
);
1052 ((t_base_type
*)g_type_binary
)->set_binary(true);
1053 g_type_slist
= new t_base_type("string", t_base_type::TYPE_STRING
);
1054 ((t_base_type
*)g_type_slist
)->set_string_list(true);
1055 g_type_bool
= new t_base_type("bool", t_base_type::TYPE_BOOL
);
1056 g_type_byte
= new t_base_type("byte", t_base_type::TYPE_BYTE
);
1057 g_type_i16
= new t_base_type("i16", t_base_type::TYPE_I16
);
1058 g_type_i32
= new t_base_type("i32", t_base_type::TYPE_I32
);
1059 g_type_i64
= new t_base_type("i64", t_base_type::TYPE_I64
);
1060 g_type_double
= new t_base_type("double", t_base_type::TYPE_DOUBLE
);
1063 parse(program
, NULL
);
1068 // Clean up. Who am I kidding... this program probably orphans heap memory
1069 // all over the place, but who cares because it is about to exit and it is
1070 // all referenced and used by this wacky parse tree up until now anyways.
1074 delete g_type_string
;
1080 delete g_type_double
;