Mark Slee | e9ce01c | 2007-05-16 02:29:53 +0000 | [diff] [blame] | 1 | // 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 Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 7 | /** |
| 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 Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 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/ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 14 | * |
| 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 Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 24 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 25 | // Careful: must include globals first for extern definitions |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 26 | #include "globals.h" |
| 27 | |
| 28 | #include "main.h" |
| 29 | #include "parse/t_program.h" |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 30 | #include "parse/t_scope.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 31 | #include "generate/t_cpp_generator.h" |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 32 | #include "generate/t_java_generator.h" |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 33 | #include "generate/t_php_generator.h" |
Mark Slee | fc89d39 | 2006-09-04 00:04:39 +0000 | [diff] [blame] | 34 | #include "generate/t_py_generator.h" |
Mark Slee | 6d7d595 | 2007-01-27 01:44:22 +0000 | [diff] [blame] | 35 | #include "generate/t_rb_generator.h" |
Mark Slee | 0e0ff7e | 2007-01-18 22:59:59 +0000 | [diff] [blame] | 36 | #include "generate/t_xsd_generator.h" |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 37 | #include "generate/t_perl_generator.h" |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 38 | #include "generate/t_ocaml_generator.h" |
Christopher Piro | 2f5afce | 2007-06-29 07:17:33 +0000 | [diff] [blame] | 39 | #include "generate/t_erl_generator.h" |
iproctor | ff8eb92 | 2007-07-25 19:06:13 +0000 | [diff] [blame] | 40 | #include "generate/t_hs_generator.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 41 | |
| 42 | using namespace std; |
| 43 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 44 | /** |
| 45 | * Global program tree |
| 46 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 47 | t_program* g_program; |
| 48 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 49 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 50 | * Global types |
| 51 | */ |
| 52 | |
| 53 | t_type* g_type_void; |
| 54 | t_type* g_type_string; |
Mark Slee | 8d725a2 | 2007-04-13 01:57:12 +0000 | [diff] [blame] | 55 | t_type* g_type_binary; |
Mark Slee | b6200d8 | 2007-01-19 19:14:36 +0000 | [diff] [blame] | 56 | t_type* g_type_slist; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 57 | t_type* g_type_bool; |
| 58 | t_type* g_type_byte; |
| 59 | t_type* g_type_i16; |
| 60 | t_type* g_type_i32; |
| 61 | t_type* g_type_i64; |
| 62 | t_type* g_type_double; |
| 63 | |
| 64 | /** |
| 65 | * Global scope |
| 66 | */ |
| 67 | t_scope* g_scope; |
| 68 | |
| 69 | /** |
| 70 | * Parent scope to also parse types |
| 71 | */ |
| 72 | t_scope* g_parent_scope; |
| 73 | |
| 74 | /** |
| 75 | * Prefix for putting types in parent scope |
| 76 | */ |
| 77 | string g_parent_prefix; |
| 78 | |
| 79 | /** |
| 80 | * Parsing pass |
| 81 | */ |
| 82 | PARSE_MODE g_parse_mode; |
| 83 | |
| 84 | /** |
| 85 | * Current directory of file being parsed |
| 86 | */ |
| 87 | string g_curdir; |
| 88 | |
| 89 | /** |
| 90 | * Current file being parsed |
| 91 | */ |
| 92 | string g_curpath; |
| 93 | |
| 94 | /** |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 95 | * Search path for inclusions |
| 96 | */ |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 97 | vector<string> g_incl_searchpath; |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 98 | |
| 99 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 100 | * Global debug state |
| 101 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 102 | int g_debug = 0; |
| 103 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 104 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 105 | * Warning level |
| 106 | */ |
| 107 | int g_warn = 1; |
| 108 | |
| 109 | /** |
| 110 | * Verbose output |
| 111 | */ |
| 112 | int g_verbose = 0; |
| 113 | |
| 114 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 115 | * Global time string |
| 116 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 117 | char* g_time_str; |
| 118 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 119 | /** |
David Reiss | cbd4bac | 2007-08-14 17:12:33 +0000 | [diff] [blame] | 120 | * The last parsed doctext comment. |
| 121 | */ |
| 122 | char* g_doctext; |
| 123 | |
| 124 | /** |
| 125 | * The location of the last parsed doctext comment. |
| 126 | */ |
| 127 | int g_doctext_lineno; |
| 128 | |
| 129 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 130 | * Flags to control code generation |
| 131 | */ |
| 132 | bool gen_cpp = false; |
| 133 | bool gen_java = false; |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 134 | bool gen_javabean = false; |
Mark Slee | 6d7d595 | 2007-01-27 01:44:22 +0000 | [diff] [blame] | 135 | bool gen_rb = false; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 136 | bool gen_py = false; |
Mark Slee | 0e0ff7e | 2007-01-18 22:59:59 +0000 | [diff] [blame] | 137 | bool gen_xsd = false; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 138 | bool gen_php = false; |
| 139 | bool gen_phpi = false; |
Mark Slee | 756b1d1 | 2007-07-06 00:30:21 +0000 | [diff] [blame] | 140 | bool gen_rest = false; |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 141 | bool gen_perl = false; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 142 | bool gen_ocaml = false; |
Christopher Piro | 2f5afce | 2007-06-29 07:17:33 +0000 | [diff] [blame] | 143 | bool gen_erl = false; |
iproctor | ff8eb92 | 2007-07-25 19:06:13 +0000 | [diff] [blame] | 144 | bool gen_hs = false; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 145 | bool gen_dense = false; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 146 | bool gen_recurse = false; |
| 147 | |
| 148 | /** |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 149 | * Report an error to the user. This is called yyerror for historical |
| 150 | * reasons (lex and yacc expect the error reporting routine to be called |
| 151 | * this). Call this function to report any errors to the user. |
| 152 | * yyerror takes printf style arguments. |
| 153 | * |
| 154 | * @param fmt C format string followed by additional arguments |
| 155 | */ |
| 156 | void yyerror(char* fmt, ...) { |
| 157 | va_list args; |
| 158 | fprintf(stderr, |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 159 | "[ERROR:%s:%d] (last token was '%s')\n", |
| 160 | g_curpath.c_str(), |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 161 | yylineno, |
| 162 | yytext); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 163 | |
| 164 | va_start(args, fmt); |
| 165 | vfprintf(stderr, fmt, args); |
| 166 | va_end(args); |
| 167 | |
| 168 | fprintf(stderr, "\n"); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Prints a debug message from the parser. |
| 173 | * |
| 174 | * @param fmt C format string followed by additional arguments |
| 175 | */ |
| 176 | void pdebug(char* fmt, ...) { |
| 177 | if (g_debug == 0) { |
| 178 | return; |
| 179 | } |
| 180 | va_list args; |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 181 | printf("[PARSE:%d] ", yylineno); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 182 | va_start(args, fmt); |
| 183 | vprintf(fmt, args); |
| 184 | va_end(args); |
| 185 | printf("\n"); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Prints a verbose output mode message |
| 190 | * |
| 191 | * @param fmt C format string followed by additional arguments |
| 192 | */ |
| 193 | void pverbose(char* fmt, ...) { |
| 194 | if (g_verbose == 0) { |
| 195 | return; |
| 196 | } |
| 197 | va_list args; |
| 198 | va_start(args, fmt); |
| 199 | vprintf(fmt, args); |
| 200 | va_end(args); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Prints a warning message |
| 205 | * |
| 206 | * @param fmt C format string followed by additional arguments |
| 207 | */ |
| 208 | void pwarning(int level, char* fmt, ...) { |
| 209 | if (g_warn < level) { |
| 210 | return; |
| 211 | } |
| 212 | va_list args; |
| 213 | printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 214 | va_start(args, fmt); |
| 215 | vprintf(fmt, args); |
| 216 | va_end(args); |
| 217 | printf("\n"); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Prints a failure message and exits |
| 222 | * |
| 223 | * @param fmt C format string followed by additional arguments |
| 224 | */ |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 225 | void failure(const char* fmt, ...) { |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 226 | va_list args; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 227 | fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 228 | va_start(args, fmt); |
| 229 | vfprintf(stderr, fmt, args); |
| 230 | va_end(args); |
| 231 | printf("\n"); |
| 232 | exit(1); |
| 233 | } |
| 234 | |
| 235 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 236 | * Converts a string filename into a thrift program name |
| 237 | */ |
| 238 | string program_name(string filename) { |
| 239 | string::size_type slash = filename.rfind("/"); |
| 240 | if (slash != string::npos) { |
| 241 | filename = filename.substr(slash+1); |
| 242 | } |
| 243 | string::size_type dot = filename.rfind("."); |
| 244 | if (dot != string::npos) { |
| 245 | filename = filename.substr(0, dot); |
| 246 | } |
| 247 | return filename; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Gets the directory path of a filename |
| 252 | */ |
| 253 | string directory_name(string filename) { |
| 254 | string::size_type slash = filename.rfind("/"); |
| 255 | // No slash, just use the current directory |
| 256 | if (slash == string::npos) { |
| 257 | return "."; |
| 258 | } |
| 259 | return filename.substr(0, slash); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Finds the appropriate file path for the given filename |
| 264 | */ |
| 265 | string include_file(string filename) { |
| 266 | // Absolute path? Just try that |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 267 | if (filename[0] == '/') { |
| 268 | // Realpath! |
| 269 | char rp[PATH_MAX]; |
| 270 | if (realpath(filename.c_str(), rp) == NULL) { |
| 271 | pwarning(0, "Cannot open include file %s\n", filename.c_str()); |
| 272 | return std::string(); |
| 273 | } |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 274 | |
| 275 | // Stat this file |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 276 | struct stat finfo; |
| 277 | if (stat(rp, &finfo) == 0) { |
| 278 | return rp; |
| 279 | } |
| 280 | } else { // relative path, start searching |
| 281 | // new search path with current dir global |
| 282 | vector<string> sp = g_incl_searchpath; |
| 283 | sp.insert(sp.begin(), g_curdir); |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 284 | |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 285 | // iterate through paths |
| 286 | vector<string>::iterator it; |
| 287 | for (it = sp.begin(); it != sp.end(); it++) { |
| 288 | string sfilename = *(it) + "/" + filename; |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 289 | |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 290 | // Realpath! |
| 291 | char rp[PATH_MAX]; |
| 292 | if (realpath(sfilename.c_str(), rp) == NULL) { |
| 293 | continue; |
| 294 | } |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 295 | |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 296 | // Stat this files |
| 297 | struct stat finfo; |
| 298 | if (stat(rp, &finfo) == 0) { |
| 299 | return rp; |
| 300 | } |
| 301 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 302 | } |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 303 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 304 | // Uh oh |
| 305 | pwarning(0, "Could not find include file %s\n", filename.c_str()); |
| 306 | return std::string(); |
| 307 | } |
| 308 | |
| 309 | /** |
David Reiss | cbd4bac | 2007-08-14 17:12:33 +0000 | [diff] [blame] | 310 | * Clears any previously stored doctext string. |
| 311 | * Also prints a warning if we are discarding information. |
| 312 | */ |
| 313 | void clear_doctext() { |
| 314 | if (g_doctext != NULL) { |
| 315 | pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno); |
| 316 | } |
| 317 | free(g_doctext); |
| 318 | g_doctext = NULL; |
| 319 | } |
| 320 | |
| 321 | /** |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 322 | * Cleans up text commonly found in doxygen-like comments |
| 323 | * |
| 324 | * Warning: if you mix tabs and spaces in a non-uniform way, |
| 325 | * you will get what you deserve. |
| 326 | */ |
| 327 | char* clean_up_doctext(char* doctext) { |
| 328 | // Convert to C++ string, and remove Windows's carriage returns. |
| 329 | string docstring = doctext; |
| 330 | docstring.erase( |
| 331 | remove(docstring.begin(), docstring.end(), '\r'), |
| 332 | docstring.end()); |
| 333 | |
| 334 | // Separate into lines. |
| 335 | vector<string> lines; |
| 336 | string::size_type pos = string::npos; |
| 337 | string::size_type last; |
| 338 | while (true) { |
| 339 | last = (pos == string::npos) ? 0 : pos+1; |
| 340 | pos = docstring.find('\n', last); |
| 341 | if (pos == string::npos) { |
| 342 | // First bit of cleaning. If the last line is only whitespace, drop it. |
| 343 | string::size_type nonwhite = docstring.find_first_not_of(" \t", last); |
| 344 | if (nonwhite != string::npos) { |
| 345 | lines.push_back(docstring.substr(last)); |
| 346 | } |
| 347 | break; |
| 348 | } |
| 349 | lines.push_back(docstring.substr(last, pos-last)); |
| 350 | } |
| 351 | |
| 352 | // A very profound docstring. |
| 353 | if (lines.empty()) { |
| 354 | return NULL; |
| 355 | } |
| 356 | |
| 357 | // Clear leading whitespace from the first line. |
| 358 | pos = lines.front().find_first_not_of(" \t"); |
| 359 | lines.front().erase(0, pos); |
| 360 | |
| 361 | // If every nonblank line after the first has the same number of spaces/tabs, |
| 362 | // then a star, remove them. |
| 363 | bool have_prefix = true; |
| 364 | bool found_prefix = false; |
| 365 | string::size_type prefix_len = 0; |
| 366 | vector<string>::iterator l_iter; |
| 367 | for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) { |
| 368 | if (l_iter->empty()) { |
| 369 | continue; |
| 370 | } |
| 371 | |
| 372 | pos = l_iter->find_first_not_of(" \t"); |
| 373 | if (!found_prefix) { |
| 374 | if (pos != string::npos) { |
| 375 | if (l_iter->at(pos) == '*') { |
| 376 | found_prefix = true; |
| 377 | prefix_len = pos; |
| 378 | } else { |
| 379 | have_prefix = false; |
| 380 | break; |
| 381 | } |
| 382 | } else { |
| 383 | // Whitespace-only line. Truncate it. |
| 384 | l_iter->clear(); |
| 385 | } |
| 386 | } else if (l_iter->size() > pos |
| 387 | && l_iter->at(pos) == '*' |
| 388 | && pos == prefix_len) { |
| 389 | // Business as usual. |
| 390 | } else if (pos == string::npos) { |
| 391 | // Whitespace-only line. Let's truncate it for them. |
| 392 | l_iter->clear(); |
| 393 | } else { |
| 394 | // The pattern has been broken. |
| 395 | have_prefix = false; |
| 396 | break; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | // If our prefix survived, delete it from every line. |
| 401 | if (have_prefix) { |
| 402 | // Get the star too. |
| 403 | prefix_len++; |
| 404 | for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) { |
| 405 | l_iter->erase(0, prefix_len); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | // Now delete the minimum amount of leading whitespace from each line. |
| 410 | prefix_len = string::npos; |
| 411 | for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) { |
| 412 | if (l_iter->empty()) { |
| 413 | continue; |
| 414 | } |
| 415 | pos = l_iter->find_first_not_of(" \t"); |
| 416 | if (pos != string::npos |
| 417 | && (prefix_len == string::npos || pos < prefix_len)) { |
| 418 | prefix_len = pos; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | // If our prefix survived, delete it from every line. |
| 423 | if (prefix_len != string::npos) { |
| 424 | for (l_iter = lines.begin()+1; l_iter != lines.end(); ++l_iter) { |
| 425 | l_iter->erase(0, prefix_len); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | // Remove trailing whitespace from every line. |
| 430 | for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) { |
| 431 | pos = l_iter->find_last_not_of(" \t"); |
| 432 | if (pos != string::npos && pos != l_iter->length()-1) { |
| 433 | l_iter->erase(pos+1); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | // If the first line is empty, remove it. |
| 438 | // Don't do this earlier because a lot of steps skip the first line. |
| 439 | if (lines.front().empty()) { |
| 440 | lines.erase(lines.begin()); |
| 441 | } |
| 442 | |
| 443 | // Now rejoin the lines and copy them back into doctext. |
| 444 | docstring.clear(); |
| 445 | for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) { |
| 446 | docstring += *l_iter; |
| 447 | docstring += '\n'; |
| 448 | } |
| 449 | |
| 450 | assert(docstring.length() <= strlen(doctext)); |
| 451 | strcpy(doctext, docstring.c_str()); |
| 452 | return doctext; |
| 453 | } |
| 454 | |
| 455 | /** Set to true to debug docstring parsing */ |
| 456 | static bool dump_docs = false; |
| 457 | |
| 458 | /** |
| 459 | * Dumps docstrings to stdout |
David Reiss | cdffe26 | 2007-08-14 17:12:31 +0000 | [diff] [blame] | 460 | * Only works for top-level definitions and the whole program doc |
| 461 | * (i.e., not enum constants, struct fields, or functions. |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 462 | */ |
| 463 | void dump_docstrings(t_program* program) { |
David Reiss | cdffe26 | 2007-08-14 17:12:31 +0000 | [diff] [blame] | 464 | string progdoc = program->get_doc(); |
David Reiss | c2532a9 | 2007-07-30 23:46:11 +0000 | [diff] [blame] | 465 | if (!progdoc.empty()) { |
| 466 | printf("Whole program doc:\n%s\n", progdoc.c_str()); |
| 467 | } |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 468 | const vector<t_typedef*>& typedefs = program->get_typedefs(); |
| 469 | vector<t_typedef*>::const_iterator t_iter; |
| 470 | for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) { |
| 471 | t_typedef* td = *t_iter; |
| 472 | if (td->has_doc()) { |
David Reiss | cdffe26 | 2007-08-14 17:12:31 +0000 | [diff] [blame] | 473 | printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str()); |
| 474 | } |
| 475 | } |
| 476 | const vector<t_enum*>& enums = program->get_enums(); |
| 477 | vector<t_enum*>::const_iterator e_iter; |
| 478 | for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) { |
| 479 | t_enum* en = *e_iter; |
| 480 | if (en->has_doc()) { |
| 481 | printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str()); |
| 482 | } |
| 483 | } |
| 484 | const vector<t_const*>& consts = program->get_consts(); |
| 485 | vector<t_const*>::const_iterator c_iter; |
| 486 | for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) { |
| 487 | t_const* co = *c_iter; |
| 488 | if (co->has_doc()) { |
| 489 | printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str()); |
| 490 | } |
| 491 | } |
| 492 | const vector<t_struct*>& structs = program->get_structs(); |
| 493 | vector<t_struct*>::const_iterator s_iter; |
| 494 | for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) { |
| 495 | t_struct* st = *s_iter; |
| 496 | if (st->has_doc()) { |
| 497 | printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str()); |
| 498 | } |
| 499 | } |
| 500 | const vector<t_struct*>& xceptions = program->get_xceptions(); |
| 501 | vector<t_struct*>::const_iterator x_iter; |
| 502 | for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) { |
| 503 | t_struct* xn = *x_iter; |
| 504 | if (xn->has_doc()) { |
| 505 | printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str()); |
| 506 | } |
| 507 | } |
| 508 | const vector<t_service*>& services = program->get_services(); |
| 509 | vector<t_service*>::const_iterator v_iter; |
| 510 | for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) { |
| 511 | t_service* sv = *v_iter; |
| 512 | if (sv->has_doc()) { |
| 513 | printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str()); |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | /** |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 519 | * Call generate_fingerprint for every structure. |
| 520 | */ |
| 521 | void generate_all_fingerprints(t_program* program) { |
| 522 | const vector<t_struct*>& structs = program->get_structs(); |
| 523 | vector<t_struct*>::const_iterator s_iter; |
| 524 | for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) { |
| 525 | t_struct* st = *s_iter; |
| 526 | st->generate_fingerprint(); |
| 527 | } |
| 528 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 529 | const vector<t_struct*>& xceptions = program->get_xceptions(); |
| 530 | vector<t_struct*>::const_iterator x_iter; |
| 531 | for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) { |
| 532 | t_struct* st = *x_iter; |
| 533 | st->generate_fingerprint(); |
| 534 | } |
| 535 | |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 536 | // If you want to generate fingerprints for implicit structures, start here. |
| 537 | /* |
| 538 | const vector<t_service*>& services = program->get_services(); |
| 539 | vector<t_service*>::const_iterator v_iter; |
| 540 | for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) { |
| 541 | t_service* sv = *v_iter; |
| 542 | } |
| 543 | */ |
| 544 | } |
| 545 | |
| 546 | /** |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 547 | * Diplays the usage message and then exits with an error code. |
| 548 | */ |
| 549 | void usage() { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 550 | fprintf(stderr, "Usage: thrift [options] file\n"); |
| 551 | fprintf(stderr, "Options:\n"); |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 552 | fprintf(stderr, " -cpp Generate C++ output files\n"); |
| 553 | fprintf(stderr, " -java Generate Java output files\n"); |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 554 | fprintf(stderr, " -javabean Generate Java bean-style output files\n"); |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 555 | fprintf(stderr, " -php Generate PHP output files\n"); |
| 556 | fprintf(stderr, " -phpi Generate PHP inlined files\n"); |
| 557 | fprintf(stderr, " -py Generate Python output files\n"); |
ccheever | f53b5cf | 2007-02-05 20:33:11 +0000 | [diff] [blame] | 558 | fprintf(stderr, " -rb Generate Ruby output files\n"); |
| 559 | fprintf(stderr, " -xsd Generate XSD output files\n"); |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 560 | fprintf(stderr, " -perl Generate Perl output files\n"); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 561 | fprintf(stderr, " -ocaml Generate OCaml output files\n"); |
Christopher Piro | 2f5afce | 2007-06-29 07:17:33 +0000 | [diff] [blame] | 562 | fprintf(stderr, " -erl Generate Erlang output files\n"); |
iproctor | ff8eb92 | 2007-07-25 19:06:13 +0000 | [diff] [blame] | 563 | fprintf(stderr, " -hs Generate Haskell output files\n"); |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 564 | fprintf(stderr, " -I dir Add a directory to the list of directories\n"); |
Mark Slee | 227ac2c | 2007-03-07 05:46:50 +0000 | [diff] [blame] | 565 | fprintf(stderr, " searched for include directives\n"); |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 566 | fprintf(stderr, " -dense Generate metadata for TDenseProtocol (C++)\n"); |
| 567 | fprintf(stderr, " -rest Generate PHP REST processors (with -php)\n"); |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 568 | fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n"); |
| 569 | fprintf(stderr, " -strict Strict compiler warnings on\n"); |
| 570 | fprintf(stderr, " -v[erbose] Verbose mode\n"); |
| 571 | fprintf(stderr, " -r[ecurse] Also generate included files\n"); |
| 572 | fprintf(stderr, " -debug Parse debug trace to stdout\n"); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 573 | exit(1); |
| 574 | } |
| 575 | |
| 576 | /** |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 577 | * You know, when I started working on Thrift I really thought it wasn't going |
| 578 | * to become a programming language because it was just a generator and it |
| 579 | * wouldn't need runtime type information and all that jazz. But then we |
| 580 | * decided to add constants, and all of a sudden that means runtime type |
| 581 | * validation and inference, except the "runtime" is the code generator |
| 582 | * runtime. Shit. I've been had. |
| 583 | */ |
| 584 | void validate_const_rec(std::string name, t_type* type, t_const_value* value) { |
| 585 | if (type->is_void()) { |
| 586 | throw "type error: cannot declare a void const: " + name; |
| 587 | } |
| 588 | |
| 589 | if (type->is_base_type()) { |
| 590 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); |
| 591 | switch (tbase) { |
| 592 | case t_base_type::TYPE_STRING: |
| 593 | if (value->get_type() != t_const_value::CV_STRING) { |
| 594 | throw "type error: const \"" + name + "\" was declared as string"; |
| 595 | } |
| 596 | break; |
| 597 | case t_base_type::TYPE_BOOL: |
| 598 | if (value->get_type() != t_const_value::CV_INTEGER) { |
| 599 | throw "type error: const \"" + name + "\" was declared as bool"; |
| 600 | } |
| 601 | break; |
| 602 | case t_base_type::TYPE_BYTE: |
| 603 | if (value->get_type() != t_const_value::CV_INTEGER) { |
| 604 | throw "type error: const \"" + name + "\" was declared as byte"; |
| 605 | } |
| 606 | break; |
| 607 | case t_base_type::TYPE_I16: |
| 608 | if (value->get_type() != t_const_value::CV_INTEGER) { |
| 609 | throw "type error: const \"" + name + "\" was declared as i16"; |
| 610 | } |
| 611 | break; |
| 612 | case t_base_type::TYPE_I32: |
| 613 | if (value->get_type() != t_const_value::CV_INTEGER) { |
| 614 | throw "type error: const \"" + name + "\" was declared as i32"; |
| 615 | } |
| 616 | break; |
| 617 | case t_base_type::TYPE_I64: |
| 618 | if (value->get_type() != t_const_value::CV_INTEGER) { |
| 619 | throw "type error: const \"" + name + "\" was declared as i64"; |
| 620 | } |
| 621 | break; |
| 622 | case t_base_type::TYPE_DOUBLE: |
| 623 | if (value->get_type() != t_const_value::CV_INTEGER && |
| 624 | value->get_type() != t_const_value::CV_DOUBLE) { |
| 625 | throw "type error: const \"" + name + "\" was declared as double"; |
| 626 | } |
| 627 | break; |
| 628 | default: |
David Reiss | dd7796f | 2007-08-28 21:09:06 +0000 | [diff] [blame] | 629 | throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name; |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 630 | } |
| 631 | } else if (type->is_enum()) { |
| 632 | if (value->get_type() != t_const_value::CV_INTEGER) { |
| 633 | throw "type error: const \"" + name + "\" was declared as enum"; |
| 634 | } |
| 635 | } else if (type->is_struct() || type->is_xception()) { |
| 636 | if (value->get_type() != t_const_value::CV_MAP) { |
| 637 | throw "type error: const \"" + name + "\" was declared as struct/xception"; |
| 638 | } |
| 639 | const vector<t_field*>& fields = ((t_struct*)type)->get_members(); |
| 640 | vector<t_field*>::const_iterator f_iter; |
| 641 | |
| 642 | const map<t_const_value*, t_const_value*>& val = value->get_map(); |
| 643 | map<t_const_value*, t_const_value*>::const_iterator v_iter; |
| 644 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 645 | if (v_iter->first->get_type() != t_const_value::CV_STRING) { |
| 646 | throw "type error: " + name + " struct key must be string"; |
| 647 | } |
| 648 | t_type* field_type = NULL; |
| 649 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 650 | if ((*f_iter)->get_name() == v_iter->first->get_string()) { |
| 651 | field_type = (*f_iter)->get_type(); |
| 652 | } |
| 653 | } |
| 654 | if (field_type == NULL) { |
| 655 | throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string(); |
| 656 | } |
| 657 | |
| 658 | validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second); |
| 659 | } |
| 660 | } else if (type->is_map()) { |
| 661 | t_type* k_type = ((t_map*)type)->get_key_type(); |
| 662 | t_type* v_type = ((t_map*)type)->get_val_type(); |
| 663 | const map<t_const_value*, t_const_value*>& val = value->get_map(); |
| 664 | map<t_const_value*, t_const_value*>::const_iterator v_iter; |
| 665 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 666 | validate_const_rec(name + "<key>", k_type, v_iter->first); |
| 667 | validate_const_rec(name + "<val>", v_type, v_iter->second); |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 668 | } |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 669 | } else if (type->is_list() || type->is_set()) { |
| 670 | t_type* e_type; |
| 671 | if (type->is_list()) { |
| 672 | e_type = ((t_list*)type)->get_elem_type(); |
| 673 | } else { |
| 674 | e_type = ((t_set*)type)->get_elem_type(); |
| 675 | } |
| 676 | const vector<t_const_value*>& val = value->get_list(); |
| 677 | vector<t_const_value*>::const_iterator v_iter; |
| 678 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 679 | validate_const_rec(name + "<elem>", e_type, *v_iter); |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * Check the type of the parsed const information against its declared type |
| 686 | */ |
| 687 | void validate_const_type(t_const* c) { |
| 688 | validate_const_rec(c->get_name(), c->get_type(), c->get_value()); |
| 689 | } |
| 690 | |
| 691 | /** |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 692 | * Check the type of a default value assigned to a field. |
| 693 | */ |
| 694 | void validate_field_value(t_field* field, t_const_value* cv) { |
| 695 | validate_const_rec(field->get_name(), field->get_type(), cv); |
| 696 | } |
| 697 | |
| 698 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 699 | * Parses a program |
| 700 | */ |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 701 | void parse(t_program* program, t_program* parent_program) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 702 | // Get scope file path |
| 703 | string path = program->get_path(); |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 704 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 705 | // Set current dir global, which is used in the include_file function |
| 706 | g_curdir = directory_name(path); |
| 707 | g_curpath = path; |
| 708 | |
| 709 | // Open the file |
| 710 | yyin = fopen(path.c_str(), "r"); |
| 711 | if (yyin == 0) { |
| 712 | failure("Could not open input file: \"%s\"", path.c_str()); |
| 713 | } |
| 714 | |
| 715 | // Create new scope and scan for includes |
| 716 | pverbose("Scanning %s for includes\n", path.c_str()); |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 717 | g_parse_mode = INCLUDES; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 718 | g_program = program; |
| 719 | g_scope = program->scope(); |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 720 | try { |
Mark Slee | 36bfa2e | 2007-01-19 20:09:51 +0000 | [diff] [blame] | 721 | yylineno = 1; |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 722 | if (yyparse() != 0) { |
| 723 | failure("Parser error during include pass."); |
| 724 | } |
| 725 | } catch (string x) { |
| 726 | failure(x.c_str()); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 727 | } |
| 728 | fclose(yyin); |
| 729 | |
| 730 | // Recursively parse all the include programs |
| 731 | vector<t_program*>& includes = program->get_includes(); |
| 732 | vector<t_program*>::iterator iter; |
| 733 | for (iter = includes.begin(); iter != includes.end(); ++iter) { |
| 734 | parse(*iter, program); |
| 735 | } |
| 736 | |
| 737 | // Parse the program the file |
| 738 | g_parse_mode = PROGRAM; |
| 739 | g_program = program; |
| 740 | g_scope = program->scope(); |
| 741 | g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL; |
| 742 | g_parent_prefix = program->get_name() + "."; |
| 743 | g_curpath = path; |
| 744 | yyin = fopen(path.c_str(), "r"); |
| 745 | if (yyin == 0) { |
| 746 | failure("Could not open input file: \"%s\"", path.c_str()); |
| 747 | } |
| 748 | pverbose("Parsing %s for types\n", path.c_str()); |
Mark Slee | 36bfa2e | 2007-01-19 20:09:51 +0000 | [diff] [blame] | 749 | yylineno = 1; |
David Reiss | 877237a | 2007-07-27 00:40:19 +0000 | [diff] [blame] | 750 | try { |
| 751 | if (yyparse() != 0) { |
| 752 | failure("Parser error during types pass."); |
| 753 | } |
| 754 | } catch (string x) { |
| 755 | failure(x.c_str()); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 756 | } |
| 757 | fclose(yyin); |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * Generate code |
| 762 | */ |
| 763 | void generate(t_program* program) { |
| 764 | // Oooohh, recursive code generation, hot!! |
| 765 | if (gen_recurse) { |
| 766 | const vector<t_program*>& includes = program->get_includes(); |
| 767 | for (size_t i = 0; i < includes.size(); ++i) { |
| 768 | generate(includes[i]); |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | // Generate code! |
| 773 | try { |
| 774 | pverbose("Program: %s\n", program->get_path().c_str()); |
| 775 | |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 776 | // Compute fingerprints. |
| 777 | generate_all_fingerprints(program); |
| 778 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 779 | if (gen_cpp) { |
| 780 | pverbose("Generating C++\n"); |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 781 | t_cpp_generator* cpp = new t_cpp_generator(program, gen_dense); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 782 | cpp->generate_program(); |
| 783 | delete cpp; |
| 784 | } |
| 785 | |
| 786 | if (gen_java) { |
| 787 | pverbose("Generating Java\n"); |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 788 | t_java_generator* java = new t_java_generator(program, false); |
| 789 | java->generate_program(); |
| 790 | delete java; |
| 791 | } |
| 792 | |
| 793 | if (gen_javabean) { |
| 794 | pverbose("Generating Java Beans\n"); |
| 795 | t_java_generator* java = new t_java_generator(program, true); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 796 | java->generate_program(); |
| 797 | delete java; |
| 798 | } |
| 799 | |
| 800 | if (gen_php) { |
| 801 | pverbose("Generating PHP\n"); |
Mark Slee | 756b1d1 | 2007-07-06 00:30:21 +0000 | [diff] [blame] | 802 | t_php_generator* php = new t_php_generator(program, false, gen_rest); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 803 | php->generate_program(); |
| 804 | delete php; |
| 805 | } |
| 806 | |
| 807 | if (gen_phpi) { |
| 808 | pverbose("Generating PHP-inline\n"); |
Mark Slee | 756b1d1 | 2007-07-06 00:30:21 +0000 | [diff] [blame] | 809 | t_php_generator* phpi = new t_php_generator(program, true, gen_rest); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 810 | phpi->generate_program(); |
| 811 | delete phpi; |
| 812 | } |
| 813 | |
| 814 | if (gen_py) { |
| 815 | pverbose("Generating Python\n"); |
| 816 | t_py_generator* py = new t_py_generator(program); |
| 817 | py->generate_program(); |
| 818 | delete py; |
| 819 | } |
Mark Slee | 0e0ff7e | 2007-01-18 22:59:59 +0000 | [diff] [blame] | 820 | |
Mark Slee | 6d7d595 | 2007-01-27 01:44:22 +0000 | [diff] [blame] | 821 | if (gen_rb) { |
| 822 | pverbose("Generating Ruby\n"); |
| 823 | t_rb_generator* rb = new t_rb_generator(program); |
| 824 | rb->generate_program(); |
| 825 | delete rb; |
| 826 | } |
| 827 | |
Mark Slee | 0e0ff7e | 2007-01-18 22:59:59 +0000 | [diff] [blame] | 828 | if (gen_xsd) { |
| 829 | pverbose("Generating XSD\n"); |
| 830 | t_xsd_generator* xsd = new t_xsd_generator(program); |
| 831 | xsd->generate_program(); |
| 832 | delete xsd; |
| 833 | } |
| 834 | |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 835 | if (gen_perl) { |
| 836 | pverbose("Generating PERL\n"); |
| 837 | t_perl_generator* perl = new t_perl_generator(program); |
| 838 | perl->generate_program(); |
| 839 | delete perl; |
| 840 | } |
| 841 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 842 | if (gen_ocaml) { |
| 843 | pverbose("Generating OCaml\n"); |
| 844 | t_ocaml_generator* ocaml = new t_ocaml_generator(program); |
| 845 | ocaml->generate_program(); |
| 846 | delete ocaml; |
| 847 | } |
| 848 | |
Christopher Piro | 2f5afce | 2007-06-29 07:17:33 +0000 | [diff] [blame] | 849 | if (gen_erl) { |
| 850 | pverbose("Generating Erlang\n"); |
| 851 | t_erl_generator* erl = new t_erl_generator(program); |
| 852 | erl->generate_program(); |
| 853 | delete erl; |
| 854 | } |
iproctor | ff8eb92 | 2007-07-25 19:06:13 +0000 | [diff] [blame] | 855 | if (gen_hs) { |
| 856 | pverbose("Generating Haskell\n"); |
| 857 | t_hs_generator* hs = new t_hs_generator(program); |
| 858 | hs->generate_program(); |
| 859 | delete hs; |
| 860 | } |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 861 | if (dump_docs) { |
| 862 | dump_docstrings(program); |
| 863 | } |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 864 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 865 | } catch (string s) { |
| 866 | printf("Error: %s\n", s.c_str()); |
| 867 | } catch (const char* exc) { |
| 868 | printf("Error: %s\n", exc); |
| 869 | } |
| 870 | |
| 871 | } |
| 872 | |
| 873 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 874 | * Parse it up.. then spit it back out, in pretty much every language. Alright |
| 875 | * not that many languages, but the cool ones that we care about. |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 876 | */ |
| 877 | int main(int argc, char** argv) { |
| 878 | int i; |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 879 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 880 | // Setup time string |
| 881 | time_t now = time(NULL); |
| 882 | g_time_str = ctime(&now); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 883 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 884 | // Check for necessary arguments, you gotta have at least a filename and |
| 885 | // an output language flag |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 886 | if (argc < 2) { |
| 887 | usage(); |
| 888 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 889 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 890 | // Hacky parameter handling... I didn't feel like using a library sorry! |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 891 | for (i = 1; i < argc-1; i++) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 892 | char* arg; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 893 | |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 894 | arg = strtok(argv[i], " "); |
| 895 | while (arg != NULL) { |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 896 | // Treat double dashes as single dashes |
Mark Slee | 52cb223 | 2006-11-10 22:32:07 +0000 | [diff] [blame] | 897 | if (arg[0] == '-' && arg[1] == '-') { |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 898 | ++arg; |
| 899 | } |
| 900 | |
| 901 | if (strcmp(arg, "-debug") == 0) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 902 | g_debug = 1; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 903 | } else if (strcmp(arg, "-nowarn") == 0) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 904 | g_warn = 0; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 905 | } else if (strcmp(arg, "-strict") == 0) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 906 | g_warn = 2; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 907 | } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0 ) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 908 | g_verbose = 1; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 909 | } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0 ) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 910 | gen_recurse = true; |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 911 | } else if (strcmp(arg, "-dense") == 0) { |
| 912 | gen_dense = true; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 913 | } else if (strcmp(arg, "-cpp") == 0) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 914 | gen_cpp = true; |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 915 | } else if (strcmp(arg, "-javabean") == 0) { |
| 916 | gen_javabean = true; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 917 | } else if (strcmp(arg, "-java") == 0) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 918 | gen_java = true; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 919 | } else if (strcmp(arg, "-php") == 0) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 920 | gen_php = true; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 921 | } else if (strcmp(arg, "-phpi") == 0) { |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 922 | gen_phpi = true; |
Mark Slee | 756b1d1 | 2007-07-06 00:30:21 +0000 | [diff] [blame] | 923 | } else if (strcmp(arg, "-rest") == 0) { |
| 924 | gen_rest = true; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 925 | } else if (strcmp(arg, "-py") == 0) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 926 | gen_py = true; |
Mark Slee | 6d7d595 | 2007-01-27 01:44:22 +0000 | [diff] [blame] | 927 | } else if (strcmp(arg, "-rb") == 0) { |
| 928 | gen_rb = true; |
Mark Slee | 0e0ff7e | 2007-01-18 22:59:59 +0000 | [diff] [blame] | 929 | } else if (strcmp(arg, "-xsd") == 0) { |
| 930 | gen_xsd = true; |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 931 | } else if (strcmp(arg, "-perl") == 0) { |
| 932 | gen_perl = true; |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 933 | } else if (strcmp(arg, "-ocaml") == 0) { |
| 934 | gen_ocaml = true; |
Christopher Piro | 2f5afce | 2007-06-29 07:17:33 +0000 | [diff] [blame] | 935 | } else if (strcmp(arg, "-erl") == 0) { |
| 936 | gen_erl = true; |
iproctor | ff8eb92 | 2007-07-25 19:06:13 +0000 | [diff] [blame] | 937 | } else if (strcmp(arg, "-hs") == 0) { |
| 938 | gen_hs = true; |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 939 | } else if (strcmp(arg, "-I") == 0) { |
| 940 | // An argument of "-I\ asdf" is invalid and has unknown results |
| 941 | arg = argv[++i]; |
| 942 | |
| 943 | if (arg == NULL) { |
| 944 | fprintf(stderr, "!!! Missing Include directory"); |
| 945 | usage(); |
| 946 | } |
| 947 | g_incl_searchpath.push_back(arg); |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 948 | } else { |
| 949 | fprintf(stderr, "!!! Unrecognized option: %s\n", arg); |
| 950 | usage(); |
| 951 | } |
| 952 | |
| 953 | // Tokenize more |
| 954 | arg = strtok(NULL, " "); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 955 | } |
| 956 | } |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 957 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 958 | // You gotta generate something! |
Mark Slee | 01a9f88 | 2007-08-31 00:55:28 +0000 | [diff] [blame] | 959 | if (!gen_cpp && !gen_java && !gen_javabean && !gen_php && !gen_phpi && !gen_py && !gen_rb && !gen_xsd && !gen_perl && !gen_ocaml && !gen_erl && !gen_hs) { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 960 | fprintf(stderr, "!!! No output language(s) specified\n\n"); |
| 961 | usage(); |
| 962 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 963 | |
| 964 | // Real-pathify it |
| 965 | char rp[PATH_MAX]; |
| 966 | if (realpath(argv[i], rp) == NULL) { |
| 967 | failure("Could not open input file: %s", argv[i]); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 968 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 969 | string input_file(rp); |
| 970 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 971 | // Instance of the global parse tree |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 972 | t_program* program = new t_program(input_file); |
| 973 | |
| 974 | // Initialize global types |
| 975 | g_type_void = new t_base_type("void", t_base_type::TYPE_VOID); |
| 976 | g_type_string = new t_base_type("string", t_base_type::TYPE_STRING); |
Mark Slee | 8d725a2 | 2007-04-13 01:57:12 +0000 | [diff] [blame] | 977 | g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING); |
| 978 | ((t_base_type*)g_type_binary)->set_binary(true); |
Mark Slee | b6200d8 | 2007-01-19 19:14:36 +0000 | [diff] [blame] | 979 | g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING); |
| 980 | ((t_base_type*)g_type_slist)->set_string_list(true); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 981 | g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL); |
| 982 | g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE); |
| 983 | g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16); |
| 984 | g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32); |
| 985 | g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64); |
| 986 | g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 987 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 988 | // Parse it! |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 989 | parse(program, NULL); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 990 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 991 | // Generate it! |
| 992 | generate(program); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 993 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 994 | // Clean up. Who am I kidding... this program probably orphans heap memory |
| 995 | // all over the place, but who cares because it is about to exit and it is |
| 996 | // all referenced and used by this wacky parse tree up until now anyways. |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 997 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 998 | delete program; |
| 999 | delete g_type_void; |
| 1000 | delete g_type_string; |
| 1001 | delete g_type_bool; |
| 1002 | delete g_type_byte; |
| 1003 | delete g_type_i16; |
| 1004 | delete g_type_i32; |
| 1005 | delete g_type_i64; |
| 1006 | delete g_type_double; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 1007 | |
| 1008 | // Finished |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 1009 | return 0; |
| 1010 | } |