blob: 48552e628b5fa2bb2f443a04ce3c8040c4372cd5 [file] [log] [blame]
Mark Slee31985722006-05-24 21:45:31 +00001/**
2 * thrift - a lightweight cross-language rpc/serialization tool
3 *
4 * This file contains the main compiler engine for Thrift, which invokes the
5 * scanner/parser to build the thrift object tree. The interface generation
Mark Sleef5377b32006-10-10 01:42:59 +00006 * code for each language lives in a file by the language name under the
7 * generate/ folder, and all parse structures live in parse/
Mark Slee31985722006-05-24 21:45:31 +00008 *
9 * @author Mark Slee <mcslee@facebook.com>
10 */
11
12#include <stdlib.h>
13#include <stdio.h>
14#include <stdarg.h>
15#include <string>
Mark Sleef0712dc2006-10-25 19:03:57 +000016#include <sys/types.h>
17#include <sys/stat.h>
Mark Slee31985722006-05-24 21:45:31 +000018
Mark Sleef0712dc2006-10-25 19:03:57 +000019// Careful: must include globals first for extern definitions
Mark Slee31985722006-05-24 21:45:31 +000020#include "globals.h"
21
22#include "main.h"
23#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000024#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000025#include "generate/t_cpp_generator.h"
Mark Sleeb15a68b2006-06-07 06:46:24 +000026#include "generate/t_java_generator.h"
Mark Slee6e536442006-06-30 18:28:50 +000027#include "generate/t_php_generator.h"
Mark Sleefc89d392006-09-04 00:04:39 +000028#include "generate/t_py_generator.h"
Mark Slee0e0ff7e2007-01-18 22:59:59 +000029#include "generate/t_xsd_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000030
31using namespace std;
32
Mark Sleef5377b32006-10-10 01:42:59 +000033/**
34 * Global program tree
35 */
Mark Slee31985722006-05-24 21:45:31 +000036t_program* g_program;
37
Mark Sleef5377b32006-10-10 01:42:59 +000038/**
Mark Sleef0712dc2006-10-25 19:03:57 +000039 * Global types
40 */
41
42t_type* g_type_void;
43t_type* g_type_string;
Mark Sleeb6200d82007-01-19 19:14:36 +000044t_type* g_type_slist;
Mark Sleef0712dc2006-10-25 19:03:57 +000045t_type* g_type_bool;
46t_type* g_type_byte;
47t_type* g_type_i16;
48t_type* g_type_i32;
49t_type* g_type_i64;
50t_type* g_type_double;
51
52/**
53 * Global scope
54 */
55t_scope* g_scope;
56
57/**
58 * Parent scope to also parse types
59 */
60t_scope* g_parent_scope;
61
62/**
63 * Prefix for putting types in parent scope
64 */
65string g_parent_prefix;
66
67/**
68 * Parsing pass
69 */
70PARSE_MODE g_parse_mode;
71
72/**
73 * Current directory of file being parsed
74 */
75string g_curdir;
76
77/**
78 * Current file being parsed
79 */
80string g_curpath;
81
82/**
Martin Kraemer32c66e12006-11-09 00:06:36 +000083 * Search path for inclusions
84 */
Mark Slee2329a832006-11-09 00:23:30 +000085vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +000086
87/**
Mark Sleef5377b32006-10-10 01:42:59 +000088 * Global debug state
89 */
Mark Slee31985722006-05-24 21:45:31 +000090int g_debug = 0;
91
Mark Sleef5377b32006-10-10 01:42:59 +000092/**
Mark Sleef0712dc2006-10-25 19:03:57 +000093 * Warning level
94 */
95int g_warn = 1;
96
97/**
98 * Verbose output
99 */
100int g_verbose = 0;
101
102/**
Mark Sleef5377b32006-10-10 01:42:59 +0000103 * Global time string
104 */
Mark Slee31985722006-05-24 21:45:31 +0000105char* g_time_str;
106
Mark Slee31985722006-05-24 21:45:31 +0000107/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000108 * Flags to control code generation
109 */
110bool gen_cpp = false;
111bool gen_java = false;
112bool gen_py = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000113bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000114bool gen_php = false;
115bool gen_phpi = false;
116bool gen_recurse = false;
117
118/**
Mark Slee31985722006-05-24 21:45:31 +0000119 * Report an error to the user. This is called yyerror for historical
120 * reasons (lex and yacc expect the error reporting routine to be called
121 * this). Call this function to report any errors to the user.
122 * yyerror takes printf style arguments.
123 *
124 * @param fmt C format string followed by additional arguments
125 */
126void yyerror(char* fmt, ...) {
127 va_list args;
128 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000129 "[ERROR:%s:%d] (last token was '%s')\n",
130 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000131 yylineno,
132 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000133
134 va_start(args, fmt);
135 vfprintf(stderr, fmt, args);
136 va_end(args);
137
138 fprintf(stderr, "\n");
139}
140
141/**
142 * Prints a debug message from the parser.
143 *
144 * @param fmt C format string followed by additional arguments
145 */
146void pdebug(char* fmt, ...) {
147 if (g_debug == 0) {
148 return;
149 }
150 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000151 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000152 va_start(args, fmt);
153 vprintf(fmt, args);
154 va_end(args);
155 printf("\n");
156}
157
158/**
159 * Prints a verbose output mode message
160 *
161 * @param fmt C format string followed by additional arguments
162 */
163void pverbose(char* fmt, ...) {
164 if (g_verbose == 0) {
165 return;
166 }
167 va_list args;
168 va_start(args, fmt);
169 vprintf(fmt, args);
170 va_end(args);
171}
172
173/**
174 * Prints a warning message
175 *
176 * @param fmt C format string followed by additional arguments
177 */
178void pwarning(int level, char* fmt, ...) {
179 if (g_warn < level) {
180 return;
181 }
182 va_list args;
183 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000184 va_start(args, fmt);
185 vprintf(fmt, args);
186 va_end(args);
187 printf("\n");
188}
189
190/**
191 * Prints a failure message and exits
192 *
193 * @param fmt C format string followed by additional arguments
194 */
Mark Slee30152872006-11-28 01:24:07 +0000195void failure(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000196 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000197 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000198 va_start(args, fmt);
199 vfprintf(stderr, fmt, args);
200 va_end(args);
201 printf("\n");
202 exit(1);
203}
204
205/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000206 * Converts a string filename into a thrift program name
207 */
208string program_name(string filename) {
209 string::size_type slash = filename.rfind("/");
210 if (slash != string::npos) {
211 filename = filename.substr(slash+1);
212 }
213 string::size_type dot = filename.rfind(".");
214 if (dot != string::npos) {
215 filename = filename.substr(0, dot);
216 }
217 return filename;
218}
219
220/**
221 * Gets the directory path of a filename
222 */
223string directory_name(string filename) {
224 string::size_type slash = filename.rfind("/");
225 // No slash, just use the current directory
226 if (slash == string::npos) {
227 return ".";
228 }
229 return filename.substr(0, slash);
230}
231
232/**
233 * Finds the appropriate file path for the given filename
234 */
235string include_file(string filename) {
236 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000237 if (filename[0] == '/') {
238 // Realpath!
239 char rp[PATH_MAX];
240 if (realpath(filename.c_str(), rp) == NULL) {
241 pwarning(0, "Cannot open include file %s\n", filename.c_str());
242 return std::string();
243 }
244
245 // Stat this files
246 struct stat finfo;
247 if (stat(rp, &finfo) == 0) {
248 return rp;
249 }
250 } else { // relative path, start searching
251 // new search path with current dir global
252 vector<string> sp = g_incl_searchpath;
253 sp.insert(sp.begin(), g_curdir);
254
255 // iterate through paths
256 vector<string>::iterator it;
257 for (it = sp.begin(); it != sp.end(); it++) {
258 string sfilename = *(it) + "/" + filename;
259
260 // Realpath!
261 char rp[PATH_MAX];
262 if (realpath(sfilename.c_str(), rp) == NULL) {
263 continue;
264 }
265
266 // Stat this files
267 struct stat finfo;
268 if (stat(rp, &finfo) == 0) {
269 return rp;
270 }
271 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000272 }
273
Mark Sleef0712dc2006-10-25 19:03:57 +0000274 // Uh oh
275 pwarning(0, "Could not find include file %s\n", filename.c_str());
276 return std::string();
277}
278
279/**
Mark Slee31985722006-05-24 21:45:31 +0000280 * Diplays the usage message and then exits with an error code.
281 */
282void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000283 fprintf(stderr, "Usage: thrift [options] file\n");
284 fprintf(stderr, "Options:\n");
Mark Slee2329a832006-11-09 00:23:30 +0000285 fprintf(stderr, " -cpp Generate C++ output files\n");
286 fprintf(stderr, " -java Generate Java output files\n");
287 fprintf(stderr, " -php Generate PHP output files\n");
288 fprintf(stderr, " -phpi Generate PHP inlined files\n");
289 fprintf(stderr, " -py Generate Python output files\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +0000290 fprintf(stderr, " -I dir Add a directory to the list of directories \n");
291 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000292 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
293 fprintf(stderr, " -strict Strict compiler warnings on\n");
294 fprintf(stderr, " -v[erbose] Verbose mode\n");
295 fprintf(stderr, " -r[ecurse] Also generate included files\n");
296 fprintf(stderr, " -debug Parse debug trace to stdout\n");
Mark Slee31985722006-05-24 21:45:31 +0000297 exit(1);
298}
299
300/**
Mark Slee30152872006-11-28 01:24:07 +0000301 * You know, when I started working on Thrift I really thought it wasn't going
302 * to become a programming language because it was just a generator and it
303 * wouldn't need runtime type information and all that jazz. But then we
304 * decided to add constants, and all of a sudden that means runtime type
305 * validation and inference, except the "runtime" is the code generator
306 * runtime. Shit. I've been had.
307 */
308void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
309 if (type->is_void()) {
310 throw "type error: cannot declare a void const: " + name;
311 }
312
313 if (type->is_base_type()) {
314 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
315 switch (tbase) {
316 case t_base_type::TYPE_STRING:
317 if (value->get_type() != t_const_value::CV_STRING) {
318 throw "type error: const \"" + name + "\" was declared as string";
319 }
320 break;
321 case t_base_type::TYPE_BOOL:
322 if (value->get_type() != t_const_value::CV_INTEGER) {
323 throw "type error: const \"" + name + "\" was declared as bool";
324 }
325 break;
326 case t_base_type::TYPE_BYTE:
327 if (value->get_type() != t_const_value::CV_INTEGER) {
328 throw "type error: const \"" + name + "\" was declared as byte";
329 }
330 break;
331 case t_base_type::TYPE_I16:
332 if (value->get_type() != t_const_value::CV_INTEGER) {
333 throw "type error: const \"" + name + "\" was declared as i16";
334 }
335 break;
336 case t_base_type::TYPE_I32:
337 if (value->get_type() != t_const_value::CV_INTEGER) {
338 throw "type error: const \"" + name + "\" was declared as i32";
339 }
340 break;
341 case t_base_type::TYPE_I64:
342 if (value->get_type() != t_const_value::CV_INTEGER) {
343 throw "type error: const \"" + name + "\" was declared as i64";
344 }
345 break;
346 case t_base_type::TYPE_DOUBLE:
347 if (value->get_type() != t_const_value::CV_INTEGER &&
348 value->get_type() != t_const_value::CV_DOUBLE) {
349 throw "type error: const \"" + name + "\" was declared as double";
350 }
351 break;
352 default:
353 throw "compiler error: no const of base type " + tbase + name;
354 }
355 } else if (type->is_enum()) {
356 if (value->get_type() != t_const_value::CV_INTEGER) {
357 throw "type error: const \"" + name + "\" was declared as enum";
358 }
359 } else if (type->is_struct() || type->is_xception()) {
360 if (value->get_type() != t_const_value::CV_MAP) {
361 throw "type error: const \"" + name + "\" was declared as struct/xception";
362 }
363 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
364 vector<t_field*>::const_iterator f_iter;
365
366 const map<t_const_value*, t_const_value*>& val = value->get_map();
367 map<t_const_value*, t_const_value*>::const_iterator v_iter;
368 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
369 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
370 throw "type error: " + name + " struct key must be string";
371 }
372 t_type* field_type = NULL;
373 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
374 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
375 field_type = (*f_iter)->get_type();
376 }
377 }
378 if (field_type == NULL) {
379 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
380 }
381
382 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
383 }
384 } else if (type->is_map()) {
385 t_type* k_type = ((t_map*)type)->get_key_type();
386 t_type* v_type = ((t_map*)type)->get_val_type();
387 const map<t_const_value*, t_const_value*>& val = value->get_map();
388 map<t_const_value*, t_const_value*>::const_iterator v_iter;
389 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
390 validate_const_rec(name + "<key>", k_type, v_iter->first);
391 validate_const_rec(name + "<val>", v_type, v_iter->second);
392 }
393 } else if (type->is_list() || type->is_set()) {
394 t_type* e_type;
395 if (type->is_list()) {
396 e_type = ((t_list*)type)->get_elem_type();
397 } else {
398 e_type = ((t_set*)type)->get_elem_type();
399 }
400 const vector<t_const_value*>& val = value->get_list();
401 vector<t_const_value*>::const_iterator v_iter;
402 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
403 validate_const_rec(name + "<elem>", e_type, *v_iter);
404 }
405 }
406}
407
408/**
409 * Check the type of the parsed const information against its declared type
410 */
411void validate_const_type(t_const* c) {
412 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
413}
414
415/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000416 * Parses a program
417 */
418void parse(t_program* program, t_program* parent_program) {
419 // Get scope file path
420 string path = program->get_path();
421
422 // Set current dir global, which is used in the include_file function
423 g_curdir = directory_name(path);
424 g_curpath = path;
425
426 // Open the file
427 yyin = fopen(path.c_str(), "r");
428 if (yyin == 0) {
429 failure("Could not open input file: \"%s\"", path.c_str());
430 }
431
432 // Create new scope and scan for includes
433 pverbose("Scanning %s for includes\n", path.c_str());
434 g_parse_mode = INCLUDES;
435 g_program = program;
436 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000437 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000438 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000439 if (yyparse() != 0) {
440 failure("Parser error during include pass.");
441 }
442 } catch (string x) {
443 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000444 }
445 fclose(yyin);
446
447 // Recursively parse all the include programs
448 vector<t_program*>& includes = program->get_includes();
449 vector<t_program*>::iterator iter;
450 for (iter = includes.begin(); iter != includes.end(); ++iter) {
451 parse(*iter, program);
452 }
453
454 // Parse the program the file
455 g_parse_mode = PROGRAM;
456 g_program = program;
457 g_scope = program->scope();
458 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
459 g_parent_prefix = program->get_name() + ".";
460 g_curpath = path;
461 yyin = fopen(path.c_str(), "r");
462 if (yyin == 0) {
463 failure("Could not open input file: \"%s\"", path.c_str());
464 }
465 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000466 yylineno = 1;
Mark Sleef0712dc2006-10-25 19:03:57 +0000467 if (yyparse() != 0) {
468 failure("Parser error during types pass.");
469 }
470 fclose(yyin);
471}
472
473/**
474 * Generate code
475 */
476void generate(t_program* program) {
477 // Oooohh, recursive code generation, hot!!
478 if (gen_recurse) {
479 const vector<t_program*>& includes = program->get_includes();
480 for (size_t i = 0; i < includes.size(); ++i) {
481 generate(includes[i]);
482 }
483 }
484
485 // Generate code!
486 try {
487 pverbose("Program: %s\n", program->get_path().c_str());
488
489 if (gen_cpp) {
490 pverbose("Generating C++\n");
491 t_cpp_generator* cpp = new t_cpp_generator(program);
492 cpp->generate_program();
493 delete cpp;
494 }
495
496 if (gen_java) {
497 pverbose("Generating Java\n");
498 t_java_generator* java = new t_java_generator(program);
499 java->generate_program();
500 delete java;
501 }
502
503 if (gen_php) {
504 pverbose("Generating PHP\n");
505 t_php_generator* php = new t_php_generator(program, false);
506 php->generate_program();
507 delete php;
508 }
509
510 if (gen_phpi) {
511 pverbose("Generating PHP-inline\n");
512 t_php_generator* phpi = new t_php_generator(program, true);
513 phpi->generate_program();
514 delete phpi;
515 }
516
517 if (gen_py) {
518 pverbose("Generating Python\n");
519 t_py_generator* py = new t_py_generator(program);
520 py->generate_program();
521 delete py;
522 }
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000523
524 if (gen_xsd) {
525 pverbose("Generating XSD\n");
526 t_xsd_generator* xsd = new t_xsd_generator(program);
527 xsd->generate_program();
528 delete xsd;
529 }
530
Mark Sleef0712dc2006-10-25 19:03:57 +0000531 } catch (string s) {
532 printf("Error: %s\n", s.c_str());
533 } catch (const char* exc) {
534 printf("Error: %s\n", exc);
535 }
536
537}
538
539/**
Mark Sleef5377b32006-10-10 01:42:59 +0000540 * Parse it up.. then spit it back out, in pretty much every language. Alright
541 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000542 */
543int main(int argc, char** argv) {
544 int i;
Mark Sleef5377b32006-10-10 01:42:59 +0000545
Mark Sleeb15a68b2006-06-07 06:46:24 +0000546 // Setup time string
547 time_t now = time(NULL);
548 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000549
Mark Sleef0712dc2006-10-25 19:03:57 +0000550 // Check for necessary arguments, you gotta have at least a filename and
551 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000552 if (argc < 2) {
553 usage();
554 }
Mark Slee31985722006-05-24 21:45:31 +0000555
Mark Sleef5377b32006-10-10 01:42:59 +0000556 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000557 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000558 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000559
Mark Sleefdbee812006-09-27 18:50:48 +0000560 arg = strtok(argv[i], " ");
561 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000562 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000563 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000564 ++arg;
565 }
566
567 if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000568 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000569 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000570 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +0000571 } else if (strcmp(arg, "-strict") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000572 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +0000573 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000574 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000575 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000576 gen_recurse = true;
Mark Slee2329a832006-11-09 00:23:30 +0000577 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000578 gen_cpp = true;
Mark Slee2329a832006-11-09 00:23:30 +0000579 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000580 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +0000581 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000582 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +0000583 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +0000584 gen_phpi = true;
Mark Slee2329a832006-11-09 00:23:30 +0000585 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000586 gen_py = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000587 } else if (strcmp(arg, "-xsd") == 0) {
588 gen_xsd = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000589 } else if (strcmp(arg, "-I") == 0) {
590 // An argument of "-I\ asdf" is invalid and has unknown results
591 arg = argv[++i];
592
593 if (arg == NULL) {
594 fprintf(stderr, "!!! Missing Include directory");
595 usage();
596 }
597 g_incl_searchpath.push_back(arg);
Mark Sleefdbee812006-09-27 18:50:48 +0000598 } else {
599 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
600 usage();
601 }
602
603 // Tokenize more
604 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +0000605 }
606 }
607
Mark Sleef0712dc2006-10-25 19:03:57 +0000608 // You gotta generate something!
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000609 if (!gen_cpp && !gen_java && !gen_php && !gen_phpi && !gen_py && !gen_xsd) {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000610 fprintf(stderr, "!!! No output language(s) specified\n\n");
611 usage();
612 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000613
614 // Real-pathify it
615 char rp[PATH_MAX];
616 if (realpath(argv[i], rp) == NULL) {
617 failure("Could not open input file: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +0000618 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000619 string input_file(rp);
620
Mark Sleef5377b32006-10-10 01:42:59 +0000621 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +0000622 t_program* program = new t_program(input_file);
623
624 // Initialize global types
625 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
626 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Sleeb6200d82007-01-19 19:14:36 +0000627 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
628 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +0000629 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
630 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
631 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
632 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
633 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
634 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +0000635
Mark Sleef5377b32006-10-10 01:42:59 +0000636 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +0000637 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +0000638
Mark Sleef0712dc2006-10-25 19:03:57 +0000639 // Generate it!
640 generate(program);
Mark Sleeb15a68b2006-06-07 06:46:24 +0000641
Mark Sleef0712dc2006-10-25 19:03:57 +0000642 // Clean up. Who am I kidding... this program probably orphans heap memory
643 // all over the place, but who cares because it is about to exit and it is
644 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +0000645
Mark Sleef0712dc2006-10-25 19:03:57 +0000646 delete program;
647 delete g_type_void;
648 delete g_type_string;
649 delete g_type_bool;
650 delete g_type_byte;
651 delete g_type_i16;
652 delete g_type_i32;
653 delete g_type_i64;
654 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +0000655
656 // Finished
Mark Slee31985722006-05-24 21:45:31 +0000657 return 0;
658}