blob: c62c925b97f0d0058176a412718129d793171d88 [file] [log] [blame]
Mark Sleee9ce01c2007-05-16 02:29:53 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Mark Slee31985722006-05-24 21:45:31 +00007/**
8 * thrift - a lightweight cross-language rpc/serialization tool
9 *
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
Mark Sleef5377b32006-10-10 01:42:59 +000012 * code for each language lives in a file by the language name under the
13 * generate/ folder, and all parse structures live in parse/
Mark Slee31985722006-05-24 21:45:31 +000014 *
15 * @author Mark Slee <mcslee@facebook.com>
16 */
17
18#include <stdlib.h>
19#include <stdio.h>
20#include <stdarg.h>
21#include <string>
Mark Sleef0712dc2006-10-25 19:03:57 +000022#include <sys/types.h>
23#include <sys/stat.h>
Mark Slee31985722006-05-24 21:45:31 +000024
Mark Sleef0712dc2006-10-25 19:03:57 +000025// Careful: must include globals first for extern definitions
Mark Slee31985722006-05-24 21:45:31 +000026#include "globals.h"
27
28#include "main.h"
29#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000030#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000031#include "generate/t_cpp_generator.h"
Mark Sleeb15a68b2006-06-07 06:46:24 +000032#include "generate/t_java_generator.h"
Mark Slee6e536442006-06-30 18:28:50 +000033#include "generate/t_php_generator.h"
Mark Sleefc89d392006-09-04 00:04:39 +000034#include "generate/t_py_generator.h"
Mark Slee6d7d5952007-01-27 01:44:22 +000035#include "generate/t_rb_generator.h"
Mark Slee0e0ff7e2007-01-18 22:59:59 +000036#include "generate/t_xsd_generator.h"
Mark Slee2c44d202007-05-16 02:18:07 +000037#include "generate/t_perl_generator.h"
Mark Slee31985722006-05-24 21:45:31 +000038
39using namespace std;
40
Mark Sleef5377b32006-10-10 01:42:59 +000041/**
42 * Global program tree
43 */
Mark Slee31985722006-05-24 21:45:31 +000044t_program* g_program;
45
Mark Sleef5377b32006-10-10 01:42:59 +000046/**
Mark Sleef0712dc2006-10-25 19:03:57 +000047 * Global types
48 */
49
50t_type* g_type_void;
51t_type* g_type_string;
Mark Slee8d725a22007-04-13 01:57:12 +000052t_type* g_type_binary;
Mark Sleeb6200d82007-01-19 19:14:36 +000053t_type* g_type_slist;
Mark Sleef0712dc2006-10-25 19:03:57 +000054t_type* g_type_bool;
55t_type* g_type_byte;
56t_type* g_type_i16;
57t_type* g_type_i32;
58t_type* g_type_i64;
59t_type* g_type_double;
60
61/**
62 * Global scope
63 */
64t_scope* g_scope;
65
66/**
67 * Parent scope to also parse types
68 */
69t_scope* g_parent_scope;
70
71/**
72 * Prefix for putting types in parent scope
73 */
74string g_parent_prefix;
75
76/**
77 * Parsing pass
78 */
79PARSE_MODE g_parse_mode;
80
81/**
82 * Current directory of file being parsed
83 */
84string g_curdir;
85
86/**
87 * Current file being parsed
88 */
89string g_curpath;
90
91/**
Martin Kraemer32c66e12006-11-09 00:06:36 +000092 * Search path for inclusions
93 */
Mark Slee2329a832006-11-09 00:23:30 +000094vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +000095
96/**
Mark Sleef5377b32006-10-10 01:42:59 +000097 * Global debug state
98 */
Mark Slee31985722006-05-24 21:45:31 +000099int g_debug = 0;
100
Mark Sleef5377b32006-10-10 01:42:59 +0000101/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000102 * Warning level
103 */
104int g_warn = 1;
105
106/**
107 * Verbose output
108 */
109int g_verbose = 0;
110
111/**
Mark Sleef5377b32006-10-10 01:42:59 +0000112 * Global time string
113 */
Mark Slee31985722006-05-24 21:45:31 +0000114char* g_time_str;
115
Mark Slee31985722006-05-24 21:45:31 +0000116/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000117 * Flags to control code generation
118 */
119bool gen_cpp = false;
120bool gen_java = false;
Mark Slee6d7d5952007-01-27 01:44:22 +0000121bool gen_rb = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000122bool gen_py = false;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000123bool gen_xsd = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000124bool gen_php = false;
125bool gen_phpi = false;
Mark Slee2c44d202007-05-16 02:18:07 +0000126bool gen_perl = false;
Mark Sleef0712dc2006-10-25 19:03:57 +0000127bool gen_recurse = false;
128
129/**
Mark Slee31985722006-05-24 21:45:31 +0000130 * Report an error to the user. This is called yyerror for historical
131 * reasons (lex and yacc expect the error reporting routine to be called
132 * this). Call this function to report any errors to the user.
133 * yyerror takes printf style arguments.
134 *
135 * @param fmt C format string followed by additional arguments
136 */
137void yyerror(char* fmt, ...) {
138 va_list args;
139 fprintf(stderr,
Mark Sleef0712dc2006-10-25 19:03:57 +0000140 "[ERROR:%s:%d] (last token was '%s')\n",
141 g_curpath.c_str(),
Mark Slee31985722006-05-24 21:45:31 +0000142 yylineno,
143 yytext);
Mark Slee31985722006-05-24 21:45:31 +0000144
145 va_start(args, fmt);
146 vfprintf(stderr, fmt, args);
147 va_end(args);
148
149 fprintf(stderr, "\n");
150}
151
152/**
153 * Prints a debug message from the parser.
154 *
155 * @param fmt C format string followed by additional arguments
156 */
157void pdebug(char* fmt, ...) {
158 if (g_debug == 0) {
159 return;
160 }
161 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000162 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000163 va_start(args, fmt);
164 vprintf(fmt, args);
165 va_end(args);
166 printf("\n");
167}
168
169/**
170 * Prints a verbose output mode message
171 *
172 * @param fmt C format string followed by additional arguments
173 */
174void pverbose(char* fmt, ...) {
175 if (g_verbose == 0) {
176 return;
177 }
178 va_list args;
179 va_start(args, fmt);
180 vprintf(fmt, args);
181 va_end(args);
182}
183
184/**
185 * Prints a warning message
186 *
187 * @param fmt C format string followed by additional arguments
188 */
189void pwarning(int level, char* fmt, ...) {
190 if (g_warn < level) {
191 return;
192 }
193 va_list args;
194 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000195 va_start(args, fmt);
196 vprintf(fmt, args);
197 va_end(args);
198 printf("\n");
199}
200
201/**
202 * Prints a failure message and exits
203 *
204 * @param fmt C format string followed by additional arguments
205 */
Mark Slee30152872006-11-28 01:24:07 +0000206void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000207 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000208 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000209 va_start(args, fmt);
210 vfprintf(stderr, fmt, args);
211 va_end(args);
212 printf("\n");
213 exit(1);
214}
215
216/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000217 * Converts a string filename into a thrift program name
218 */
219string program_name(string filename) {
220 string::size_type slash = filename.rfind("/");
221 if (slash != string::npos) {
222 filename = filename.substr(slash+1);
223 }
224 string::size_type dot = filename.rfind(".");
225 if (dot != string::npos) {
226 filename = filename.substr(0, dot);
227 }
228 return filename;
229}
230
231/**
232 * Gets the directory path of a filename
233 */
234string directory_name(string filename) {
235 string::size_type slash = filename.rfind("/");
236 // No slash, just use the current directory
237 if (slash == string::npos) {
238 return ".";
239 }
240 return filename.substr(0, slash);
241}
242
243/**
244 * Finds the appropriate file path for the given filename
245 */
246string include_file(string filename) {
247 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000248 if (filename[0] == '/') {
249 // Realpath!
250 char rp[PATH_MAX];
251 if (realpath(filename.c_str(), rp) == NULL) {
252 pwarning(0, "Cannot open include file %s\n", filename.c_str());
253 return std::string();
254 }
Mark Slee2c44d202007-05-16 02:18:07 +0000255
256 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000257 struct stat finfo;
258 if (stat(rp, &finfo) == 0) {
259 return rp;
260 }
261 } else { // relative path, start searching
262 // new search path with current dir global
263 vector<string> sp = g_incl_searchpath;
264 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000265
Martin Kraemer32c66e12006-11-09 00:06:36 +0000266 // iterate through paths
267 vector<string>::iterator it;
268 for (it = sp.begin(); it != sp.end(); it++) {
269 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000270
Martin Kraemer32c66e12006-11-09 00:06:36 +0000271 // Realpath!
272 char rp[PATH_MAX];
273 if (realpath(sfilename.c_str(), rp) == NULL) {
274 continue;
275 }
Mark Slee2c44d202007-05-16 02:18:07 +0000276
Martin Kraemer32c66e12006-11-09 00:06:36 +0000277 // Stat this files
278 struct stat finfo;
279 if (stat(rp, &finfo) == 0) {
280 return rp;
281 }
282 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000283 }
Mark Slee2c44d202007-05-16 02:18:07 +0000284
Mark Sleef0712dc2006-10-25 19:03:57 +0000285 // Uh oh
286 pwarning(0, "Could not find include file %s\n", filename.c_str());
287 return std::string();
288}
289
290/**
Mark Slee31985722006-05-24 21:45:31 +0000291 * Diplays the usage message and then exits with an error code.
292 */
293void usage() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000294 fprintf(stderr, "Usage: thrift [options] file\n");
295 fprintf(stderr, "Options:\n");
Mark Slee2329a832006-11-09 00:23:30 +0000296 fprintf(stderr, " -cpp Generate C++ output files\n");
297 fprintf(stderr, " -java Generate Java output files\n");
298 fprintf(stderr, " -php Generate PHP output files\n");
299 fprintf(stderr, " -phpi Generate PHP inlined files\n");
300 fprintf(stderr, " -py Generate Python output files\n");
ccheeverf53b5cf2007-02-05 20:33:11 +0000301 fprintf(stderr, " -rb Generate Ruby output files\n");
302 fprintf(stderr, " -xsd Generate XSD output files\n");
Mark Slee2c44d202007-05-16 02:18:07 +0000303 fprintf(stderr, " -perl Generate Perl output files\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000304 fprintf(stderr, " -I dir Add a directory to the list of directories \n");
305 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000306 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
307 fprintf(stderr, " -strict Strict compiler warnings on\n");
308 fprintf(stderr, " -v[erbose] Verbose mode\n");
309 fprintf(stderr, " -r[ecurse] Also generate included files\n");
310 fprintf(stderr, " -debug Parse debug trace to stdout\n");
Mark Slee31985722006-05-24 21:45:31 +0000311 exit(1);
312}
313
314/**
Mark Slee30152872006-11-28 01:24:07 +0000315 * You know, when I started working on Thrift I really thought it wasn't going
316 * to become a programming language because it was just a generator and it
317 * wouldn't need runtime type information and all that jazz. But then we
318 * decided to add constants, and all of a sudden that means runtime type
319 * validation and inference, except the "runtime" is the code generator
320 * runtime. Shit. I've been had.
321 */
322void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
323 if (type->is_void()) {
324 throw "type error: cannot declare a void const: " + name;
325 }
326
327 if (type->is_base_type()) {
328 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
329 switch (tbase) {
330 case t_base_type::TYPE_STRING:
331 if (value->get_type() != t_const_value::CV_STRING) {
332 throw "type error: const \"" + name + "\" was declared as string";
333 }
334 break;
335 case t_base_type::TYPE_BOOL:
336 if (value->get_type() != t_const_value::CV_INTEGER) {
337 throw "type error: const \"" + name + "\" was declared as bool";
338 }
339 break;
340 case t_base_type::TYPE_BYTE:
341 if (value->get_type() != t_const_value::CV_INTEGER) {
342 throw "type error: const \"" + name + "\" was declared as byte";
343 }
344 break;
345 case t_base_type::TYPE_I16:
346 if (value->get_type() != t_const_value::CV_INTEGER) {
347 throw "type error: const \"" + name + "\" was declared as i16";
348 }
349 break;
350 case t_base_type::TYPE_I32:
351 if (value->get_type() != t_const_value::CV_INTEGER) {
352 throw "type error: const \"" + name + "\" was declared as i32";
353 }
354 break;
355 case t_base_type::TYPE_I64:
356 if (value->get_type() != t_const_value::CV_INTEGER) {
357 throw "type error: const \"" + name + "\" was declared as i64";
358 }
359 break;
360 case t_base_type::TYPE_DOUBLE:
361 if (value->get_type() != t_const_value::CV_INTEGER &&
362 value->get_type() != t_const_value::CV_DOUBLE) {
363 throw "type error: const \"" + name + "\" was declared as double";
364 }
365 break;
366 default:
367 throw "compiler error: no const of base type " + tbase + name;
368 }
369 } else if (type->is_enum()) {
370 if (value->get_type() != t_const_value::CV_INTEGER) {
371 throw "type error: const \"" + name + "\" was declared as enum";
372 }
373 } else if (type->is_struct() || type->is_xception()) {
374 if (value->get_type() != t_const_value::CV_MAP) {
375 throw "type error: const \"" + name + "\" was declared as struct/xception";
376 }
377 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
378 vector<t_field*>::const_iterator f_iter;
379
380 const map<t_const_value*, t_const_value*>& val = value->get_map();
381 map<t_const_value*, t_const_value*>::const_iterator v_iter;
382 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
383 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
384 throw "type error: " + name + " struct key must be string";
385 }
386 t_type* field_type = NULL;
387 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
388 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
389 field_type = (*f_iter)->get_type();
390 }
391 }
392 if (field_type == NULL) {
393 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
394 }
395
396 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
397 }
398 } else if (type->is_map()) {
399 t_type* k_type = ((t_map*)type)->get_key_type();
400 t_type* v_type = ((t_map*)type)->get_val_type();
401 const map<t_const_value*, t_const_value*>& val = value->get_map();
402 map<t_const_value*, t_const_value*>::const_iterator v_iter;
403 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
404 validate_const_rec(name + "<key>", k_type, v_iter->first);
405 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000406 }
Mark Slee30152872006-11-28 01:24:07 +0000407 } else if (type->is_list() || type->is_set()) {
408 t_type* e_type;
409 if (type->is_list()) {
410 e_type = ((t_list*)type)->get_elem_type();
411 } else {
412 e_type = ((t_set*)type)->get_elem_type();
413 }
414 const vector<t_const_value*>& val = value->get_list();
415 vector<t_const_value*>::const_iterator v_iter;
416 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
417 validate_const_rec(name + "<elem>", e_type, *v_iter);
418 }
419 }
420}
421
422/**
423 * Check the type of the parsed const information against its declared type
424 */
425void validate_const_type(t_const* c) {
426 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
427}
428
429/**
Mark Slee7ff32452007-02-01 05:26:18 +0000430 * Check the type of a default value assigned to a field.
431 */
432void validate_field_value(t_field* field, t_const_value* cv) {
433 validate_const_rec(field->get_name(), field->get_type(), cv);
434}
435
436/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000437 * Parses a program
438 */
Mark Slee2c44d202007-05-16 02:18:07 +0000439void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000440 // Get scope file path
441 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000442
Mark Sleef0712dc2006-10-25 19:03:57 +0000443 // Set current dir global, which is used in the include_file function
444 g_curdir = directory_name(path);
445 g_curpath = path;
446
447 // Open the file
448 yyin = fopen(path.c_str(), "r");
449 if (yyin == 0) {
450 failure("Could not open input file: \"%s\"", path.c_str());
451 }
452
453 // Create new scope and scan for includes
454 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000455 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000456 g_program = program;
457 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000458 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000459 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000460 if (yyparse() != 0) {
461 failure("Parser error during include pass.");
462 }
463 } catch (string x) {
464 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000465 }
466 fclose(yyin);
467
468 // Recursively parse all the include programs
469 vector<t_program*>& includes = program->get_includes();
470 vector<t_program*>::iterator iter;
471 for (iter = includes.begin(); iter != includes.end(); ++iter) {
472 parse(*iter, program);
473 }
474
475 // Parse the program the file
476 g_parse_mode = PROGRAM;
477 g_program = program;
478 g_scope = program->scope();
479 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
480 g_parent_prefix = program->get_name() + ".";
481 g_curpath = path;
482 yyin = fopen(path.c_str(), "r");
483 if (yyin == 0) {
484 failure("Could not open input file: \"%s\"", path.c_str());
485 }
486 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000487 yylineno = 1;
Mark Sleef0712dc2006-10-25 19:03:57 +0000488 if (yyparse() != 0) {
489 failure("Parser error during types pass.");
490 }
491 fclose(yyin);
492}
493
494/**
495 * Generate code
496 */
497void generate(t_program* program) {
498 // Oooohh, recursive code generation, hot!!
499 if (gen_recurse) {
500 const vector<t_program*>& includes = program->get_includes();
501 for (size_t i = 0; i < includes.size(); ++i) {
502 generate(includes[i]);
503 }
504 }
505
506 // Generate code!
507 try {
508 pverbose("Program: %s\n", program->get_path().c_str());
509
510 if (gen_cpp) {
511 pverbose("Generating C++\n");
512 t_cpp_generator* cpp = new t_cpp_generator(program);
513 cpp->generate_program();
514 delete cpp;
515 }
516
517 if (gen_java) {
518 pverbose("Generating Java\n");
519 t_java_generator* java = new t_java_generator(program);
520 java->generate_program();
521 delete java;
522 }
523
524 if (gen_php) {
525 pverbose("Generating PHP\n");
526 t_php_generator* php = new t_php_generator(program, false);
527 php->generate_program();
528 delete php;
529 }
530
531 if (gen_phpi) {
532 pverbose("Generating PHP-inline\n");
533 t_php_generator* phpi = new t_php_generator(program, true);
534 phpi->generate_program();
535 delete phpi;
536 }
537
538 if (gen_py) {
539 pverbose("Generating Python\n");
540 t_py_generator* py = new t_py_generator(program);
541 py->generate_program();
542 delete py;
543 }
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000544
Mark Slee6d7d5952007-01-27 01:44:22 +0000545 if (gen_rb) {
546 pverbose("Generating Ruby\n");
547 t_rb_generator* rb = new t_rb_generator(program);
548 rb->generate_program();
549 delete rb;
550 }
551
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000552 if (gen_xsd) {
553 pverbose("Generating XSD\n");
554 t_xsd_generator* xsd = new t_xsd_generator(program);
555 xsd->generate_program();
556 delete xsd;
557 }
558
Mark Slee2c44d202007-05-16 02:18:07 +0000559 if (gen_perl) {
560 pverbose("Generating PERL\n");
561 t_perl_generator* perl = new t_perl_generator(program);
562 perl->generate_program();
563 delete perl;
564 }
565
Mark Sleef0712dc2006-10-25 19:03:57 +0000566 } catch (string s) {
567 printf("Error: %s\n", s.c_str());
568 } catch (const char* exc) {
569 printf("Error: %s\n", exc);
570 }
571
572}
573
574/**
Mark Sleef5377b32006-10-10 01:42:59 +0000575 * Parse it up.. then spit it back out, in pretty much every language. Alright
576 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +0000577 */
578int main(int argc, char** argv) {
579 int i;
Mark Sleef5377b32006-10-10 01:42:59 +0000580
Mark Sleeb15a68b2006-06-07 06:46:24 +0000581 // Setup time string
582 time_t now = time(NULL);
583 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +0000584
Mark Sleef0712dc2006-10-25 19:03:57 +0000585 // Check for necessary arguments, you gotta have at least a filename and
586 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +0000587 if (argc < 2) {
588 usage();
589 }
Mark Slee31985722006-05-24 21:45:31 +0000590
Mark Sleef5377b32006-10-10 01:42:59 +0000591 // Hacky parameter handling... I didn't feel like using a library sorry!
Mark Slee31985722006-05-24 21:45:31 +0000592 for (i = 1; i < argc-1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +0000593 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +0000594
Mark Sleefdbee812006-09-27 18:50:48 +0000595 arg = strtok(argv[i], " ");
596 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +0000597 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +0000598 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +0000599 ++arg;
600 }
601
602 if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000603 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000604 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000605 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +0000606 } else if (strcmp(arg, "-strict") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000607 g_warn = 2;
Mark Slee2329a832006-11-09 00:23:30 +0000608 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000609 g_verbose = 1;
Mark Slee2329a832006-11-09 00:23:30 +0000610 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000611 gen_recurse = true;
Mark Slee2329a832006-11-09 00:23:30 +0000612 } else if (strcmp(arg, "-cpp") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000613 gen_cpp = true;
Mark Slee2329a832006-11-09 00:23:30 +0000614 } else if (strcmp(arg, "-java") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000615 gen_java = true;
Mark Slee2329a832006-11-09 00:23:30 +0000616 } else if (strcmp(arg, "-php") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000617 gen_php = true;
Mark Slee2329a832006-11-09 00:23:30 +0000618 } else if (strcmp(arg, "-phpi") == 0) {
Mark Sleef5377b32006-10-10 01:42:59 +0000619 gen_phpi = true;
Mark Slee2329a832006-11-09 00:23:30 +0000620 } else if (strcmp(arg, "-py") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +0000621 gen_py = true;
Mark Slee6d7d5952007-01-27 01:44:22 +0000622 } else if (strcmp(arg, "-rb") == 0) {
623 gen_rb = true;
Mark Slee0e0ff7e2007-01-18 22:59:59 +0000624 } else if (strcmp(arg, "-xsd") == 0) {
625 gen_xsd = true;
Mark Slee2c44d202007-05-16 02:18:07 +0000626 } else if (strcmp(arg, "-perl") == 0) {
627 gen_perl = true;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000628 } else if (strcmp(arg, "-I") == 0) {
629 // An argument of "-I\ asdf" is invalid and has unknown results
630 arg = argv[++i];
631
632 if (arg == NULL) {
633 fprintf(stderr, "!!! Missing Include directory");
634 usage();
635 }
636 g_incl_searchpath.push_back(arg);
Mark Sleefdbee812006-09-27 18:50:48 +0000637 } else {
638 fprintf(stderr, "!!! Unrecognized option: %s\n", arg);
639 usage();
640 }
641
642 // Tokenize more
643 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +0000644 }
645 }
Mark Slee2c44d202007-05-16 02:18:07 +0000646
Mark Sleef0712dc2006-10-25 19:03:57 +0000647 // You gotta generate something!
Mark Slee2c44d202007-05-16 02:18:07 +0000648 if (!gen_cpp && !gen_java && !gen_php && !gen_phpi && !gen_py && !gen_rb && !gen_xsd && !gen_perl) {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000649 fprintf(stderr, "!!! No output language(s) specified\n\n");
650 usage();
651 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000652
653 // Real-pathify it
654 char rp[PATH_MAX];
655 if (realpath(argv[i], rp) == NULL) {
656 failure("Could not open input file: %s", argv[i]);
Mark Slee31985722006-05-24 21:45:31 +0000657 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000658 string input_file(rp);
659
Mark Sleef5377b32006-10-10 01:42:59 +0000660 // Instance of the global parse tree
Mark Sleef0712dc2006-10-25 19:03:57 +0000661 t_program* program = new t_program(input_file);
662
663 // Initialize global types
664 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
665 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +0000666 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
667 ((t_base_type*)g_type_binary)->set_binary(true);
Mark Sleeb6200d82007-01-19 19:14:36 +0000668 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
669 ((t_base_type*)g_type_slist)->set_string_list(true);
Mark Sleef0712dc2006-10-25 19:03:57 +0000670 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
671 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
672 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
673 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
674 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
675 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +0000676
Mark Sleef5377b32006-10-10 01:42:59 +0000677 // Parse it!
Mark Sleef0712dc2006-10-25 19:03:57 +0000678 parse(program, NULL);
Mark Slee31985722006-05-24 21:45:31 +0000679
Mark Sleef0712dc2006-10-25 19:03:57 +0000680 // Generate it!
681 generate(program);
Mark Sleeb15a68b2006-06-07 06:46:24 +0000682
Mark Sleef0712dc2006-10-25 19:03:57 +0000683 // Clean up. Who am I kidding... this program probably orphans heap memory
684 // all over the place, but who cares because it is about to exit and it is
685 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +0000686
Mark Sleef0712dc2006-10-25 19:03:57 +0000687 delete program;
688 delete g_type_void;
689 delete g_type_string;
690 delete g_type_bool;
691 delete g_type_byte;
692 delete g_type_i16;
693 delete g_type_i32;
694 delete g_type_i64;
695 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +0000696
697 // Finished
Mark Slee31985722006-05-24 21:45:31 +0000698 return 0;
699}