Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 1 | %{ |
| 2 | |
| 3 | /** |
| 4 | * Thrift parser. |
| 5 | * |
| 6 | * This parser is used on a thrift definition file. |
| 7 | * |
| 8 | * @author Mark Slee <mcslee@facebook.com> |
| 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include "main.h" |
| 13 | #include "globals.h" |
| 14 | #include "parse/t_program.h" |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 15 | #include "parse/t_scope.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 16 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 17 | /** |
| 18 | * This global variable is used for automatic numbering of field indices etc. |
| 19 | * when parsing the members of a struct. Field values are automatically |
| 20 | * assigned starting from -1 and working their way down. |
| 21 | */ |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 22 | int y_field_val = -1; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 23 | |
| 24 | %} |
| 25 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 26 | /** |
| 27 | * This structure is used by the parser to hold the data types associated with |
| 28 | * various parse nodes. |
| 29 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 30 | %union { |
| 31 | char* id; |
| 32 | int iconst; |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 33 | bool tbool; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 34 | t_type* ttype; |
| 35 | t_typedef* ttypedef; |
| 36 | t_enum* tenum; |
| 37 | t_struct* tstruct; |
| 38 | t_service* tservice; |
| 39 | t_function* tfunction; |
| 40 | t_field* tfield; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 41 | t_constant* tconstant; |
| 42 | } |
| 43 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 44 | /** |
| 45 | * Strings identifier |
| 46 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 47 | %token<id> tok_identifier |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 48 | %token<id> tok_literal |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Integer constant value |
| 52 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 53 | %token<iconst> tok_int_constant |
| 54 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 55 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 56 | * Header keywoards |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 57 | */ |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 58 | %token tok_include |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 59 | %token tok_namespace |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 60 | %token tok_cpp_namespace |
| 61 | %token tok_cpp_include |
| 62 | %token tok_cpp_type |
| 63 | %token tok_java_package |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 64 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 65 | /** |
| 66 | * Base datatype keywords |
| 67 | */ |
| 68 | %token tok_void |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 69 | %token tok_bool |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 70 | %token tok_byte |
| 71 | %token tok_string |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 72 | %token tok_i16 |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 73 | %token tok_i32 |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 74 | %token tok_i64 |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 75 | %token tok_double |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 76 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 77 | /** |
| 78 | * Complex type keywords |
| 79 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 80 | %token tok_map |
| 81 | %token tok_list |
| 82 | %token tok_set |
| 83 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 84 | /** |
| 85 | * Function modifiers |
| 86 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 87 | %token tok_async |
| 88 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 89 | /** |
| 90 | * Thrift language keywords |
| 91 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 92 | %token tok_typedef |
| 93 | %token tok_struct |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 94 | %token tok_xception |
| 95 | %token tok_throws |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 96 | %token tok_extends |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 97 | %token tok_service |
| 98 | %token tok_enum |
| 99 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 100 | /** |
| 101 | * Grammar nodes |
| 102 | */ |
| 103 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 104 | %type<ttype> BaseType |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 105 | %type<ttype> ContainerType |
| 106 | %type<ttype> MapType |
| 107 | %type<ttype> SetType |
| 108 | %type<ttype> ListType |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 109 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 110 | %type<ttype> TypeDefinition |
| 111 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 112 | %type<ttypedef> Typedef |
| 113 | %type<ttype> DefinitionType |
| 114 | |
| 115 | %type<tfield> Field |
| 116 | %type<ttype> FieldType |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 117 | %type<tstruct> FieldList |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 118 | |
| 119 | %type<tenum> Enum |
| 120 | %type<tenum> EnumDefList |
| 121 | %type<tconstant> EnumDef |
| 122 | |
| 123 | %type<tstruct> Struct |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 124 | %type<tstruct> Xception |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 125 | %type<tservice> Service |
| 126 | |
| 127 | %type<tfunction> Function |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 128 | %type<ttype> FunctionType |
| 129 | %type<tservice> FunctionList |
| 130 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 131 | %type<tstruct> ThrowsOptional |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 132 | %type<tservice> ExtendsOptional |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 133 | %type<tbool> AsyncOptional |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 134 | %type<id> CppTypeOptional |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 135 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 136 | %% |
| 137 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 138 | /** |
| 139 | * Thrift Grammar Implementation. |
| 140 | * |
| 141 | * For the most part this source file works its way top down from what you |
| 142 | * might expect to find in a typical .thrift file, i.e. type definitions and |
| 143 | * namespaces up top followed by service definitions using those types. |
| 144 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 145 | |
| 146 | Program: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 147 | HeaderList DefinitionList |
| 148 | { |
| 149 | pdebug("Program -> Headers DefinitionList"); |
| 150 | } |
| 151 | |
| 152 | HeaderList: |
| 153 | HeaderList Header |
| 154 | { |
| 155 | pdebug("HeaderList -> HeaderList Header"); |
| 156 | } |
| 157 | | |
| 158 | { |
| 159 | pdebug("HeaderList -> "); |
| 160 | } |
| 161 | |
| 162 | Header: |
| 163 | Include |
| 164 | { |
| 165 | pdebug("Header -> Include"); |
| 166 | } |
| 167 | | tok_namespace tok_identifier |
| 168 | { |
| 169 | pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead"); |
| 170 | if (g_parse_mode == PROGRAM) { |
| 171 | g_program->set_cpp_namespace($2); |
| 172 | g_program->set_java_package($2); |
| 173 | } |
| 174 | } |
| 175 | | tok_cpp_namespace tok_identifier |
| 176 | { |
| 177 | pdebug("Header -> tok_cpp_namespace tok_identifier"); |
| 178 | if (g_parse_mode == PROGRAM) { |
| 179 | g_program->set_cpp_namespace($2); |
| 180 | } |
| 181 | } |
| 182 | | tok_cpp_include tok_literal |
| 183 | { |
| 184 | pdebug("Header -> tok_cpp_include tok_literal"); |
| 185 | if (g_parse_mode == PROGRAM) { |
| 186 | g_program->add_cpp_include($2); |
| 187 | } |
| 188 | } |
| 189 | | tok_java_package tok_identifier |
| 190 | { |
| 191 | pdebug("Header -> tok_java_package tok_identifier"); |
| 192 | if (g_parse_mode == PROGRAM) { |
| 193 | g_program->set_java_package($2); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | Include: |
| 198 | tok_include tok_literal |
| 199 | { |
| 200 | pdebug("Include -> tok_include tok_literal"); |
| 201 | if (g_parse_mode == INCLUDES) { |
| 202 | std::string path = include_file(std::string($2)); |
| 203 | if (!path.empty()) { |
| 204 | g_program->add_include(path); |
| 205 | } |
| 206 | } |
| 207 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 208 | |
| 209 | DefinitionList: |
| 210 | DefinitionList Definition |
| 211 | { |
| 212 | pdebug("DefinitionList -> DefinitionList Definition"); |
| 213 | } |
| 214 | | |
| 215 | { |
| 216 | pdebug("DefinitionList -> "); |
| 217 | } |
| 218 | |
| 219 | Definition: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 220 | TypeDefinition |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 221 | { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 222 | pdebug("Definition -> TypeDefinition"); |
| 223 | if (g_parse_mode == PROGRAM) { |
| 224 | g_scope->add_type($1->get_name(), $1); |
| 225 | if (g_parent_scope != NULL) { |
| 226 | g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1); |
| 227 | } |
| 228 | } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 229 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 230 | | Service |
| 231 | { |
| 232 | pdebug("Definition -> Service"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 233 | if (g_parse_mode == PROGRAM) { |
| 234 | g_scope->add_service($1->get_name(), $1); |
| 235 | if (g_parent_scope != NULL) { |
| 236 | g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1); |
| 237 | } |
| 238 | g_program->add_service($1); |
| 239 | } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 242 | TypeDefinition: |
| 243 | Typedef |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 244 | { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 245 | pdebug("TypeDefinition -> Typedef"); |
| 246 | if (g_parse_mode == PROGRAM) { |
| 247 | g_program->add_typedef($1); |
| 248 | } |
| 249 | } |
| 250 | | Enum |
| 251 | { |
| 252 | pdebug("TypeDefinition -> Enum"); |
| 253 | if (g_parse_mode == PROGRAM) { |
| 254 | g_program->add_enum($1); |
| 255 | } |
| 256 | } |
| 257 | | Struct |
| 258 | { |
| 259 | pdebug("TypeDefinition -> Struct"); |
| 260 | if (g_parse_mode == PROGRAM) { |
| 261 | g_program->add_struct($1); |
| 262 | } |
| 263 | } |
| 264 | | Xception |
| 265 | { |
| 266 | pdebug("TypeDefinition -> Xception"); |
| 267 | if (g_parse_mode == PROGRAM) { |
| 268 | g_program->add_xception($1); |
| 269 | } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 270 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 271 | |
| 272 | Typedef: |
| 273 | tok_typedef DefinitionType tok_identifier |
| 274 | { |
| 275 | pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 276 | t_typedef *td = new t_typedef(g_program, $2, $3); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 277 | $$ = td; |
| 278 | } |
| 279 | |
| 280 | Enum: |
| 281 | tok_enum tok_identifier '{' EnumDefList '}' |
| 282 | { |
| 283 | pdebug("Enum -> tok_enum tok_identifier { EnumDefList }"); |
| 284 | $$ = $4; |
| 285 | $$->set_name($2); |
| 286 | } |
| 287 | |
| 288 | EnumDefList: |
| 289 | EnumDefList ',' EnumDef |
| 290 | { |
| 291 | pdebug("EnumDefList -> EnumDefList EnumDef"); |
| 292 | $$ = $1; |
| 293 | $$->append($3); |
| 294 | } |
| 295 | | EnumDef |
| 296 | { |
| 297 | pdebug("EnumDefList -> EnumDef"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 298 | $$ = new t_enum(g_program); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 299 | $$->append($1); |
| 300 | } |
| 301 | | |
| 302 | { |
| 303 | pdebug("EnumDefList -> "); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 304 | $$ = new t_enum(g_program); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | EnumDef: |
| 308 | tok_identifier '=' tok_int_constant |
| 309 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 310 | pdebug("EnumDef => tok_identifier = tok_int_constant"); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 311 | if ($3 < 0) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 312 | pwarning(1, "Negative value supplied for enum %s.\n", $1); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 313 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 314 | $$ = new t_constant($1, $3); |
| 315 | } |
| 316 | | |
| 317 | tok_identifier |
| 318 | { |
| 319 | pdebug("EnumDef => tok_identifier"); |
| 320 | $$ = new t_constant($1); |
| 321 | } |
| 322 | |
| 323 | Struct: |
| 324 | tok_struct tok_identifier '{' FieldList '}' |
| 325 | { |
| 326 | pdebug("Struct -> tok_struct tok_identifier { FieldList }"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 327 | $4->set_name($2); |
| 328 | $$ = $4; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 329 | y_field_val = -1; |
| 330 | } |
| 331 | |
| 332 | Xception: |
| 333 | tok_xception tok_identifier '{' FieldList '}' |
| 334 | { |
| 335 | pdebug("Xception -> tok_xception tok_identifier { FieldList }"); |
| 336 | $4->set_name($2); |
| 337 | $4->set_xception(true); |
| 338 | $$ = $4; |
| 339 | y_field_val = -1; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | Service: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 343 | tok_service tok_identifier ExtendsOptional '{' FunctionList '}' |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 344 | { |
| 345 | pdebug("Service -> tok_service tok_identifier { FunctionList }"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 346 | $$ = $5; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 347 | $$->set_name($2); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 348 | $$->set_extends($3); |
| 349 | } |
| 350 | |
| 351 | ExtendsOptional: |
| 352 | tok_extends tok_identifier |
| 353 | { |
| 354 | pdebug("ExtendsOptional -> tok_extends tok_identifier"); |
| 355 | $$ = NULL; |
| 356 | if (g_parse_mode == PROGRAM) { |
| 357 | $$ = g_scope->get_service($2); |
| 358 | if ($$ == NULL) { |
| 359 | yyerror("Service \"%s\" has not been defined.", $2); |
| 360 | exit(1); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | | |
| 365 | { |
| 366 | $$ = NULL; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | FunctionList: |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 370 | FunctionList Function CommaOptional |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 371 | { |
| 372 | pdebug("FunctionList -> FunctionList Function"); |
| 373 | $$ = $1; |
| 374 | $1->add_function($2); |
| 375 | } |
| 376 | | |
| 377 | { |
| 378 | pdebug("FunctionList -> "); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 379 | $$ = new t_service(g_program); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 382 | CommaOptional: |
| 383 | ',' |
| 384 | {} |
| 385 | | |
| 386 | {} |
| 387 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 388 | Function: |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 389 | AsyncOptional FunctionType tok_identifier '(' FieldList ')' ThrowsOptional |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 390 | { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 391 | $5->set_name(std::string($3) + "_args"); |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 392 | $$ = new t_function($2, $3, $5, $7, $1); |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 393 | y_field_val = -1; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 396 | AsyncOptional: |
| 397 | tok_async |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 398 | { |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 399 | $$ = true; |
| 400 | } |
| 401 | | |
| 402 | { |
| 403 | $$ = false; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 406 | ThrowsOptional: |
| 407 | tok_throws '(' FieldList ')' |
| 408 | { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 409 | pdebug("ThrowsOptional -> tok_throws ( FieldList )"); |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 410 | $$ = $3; |
| 411 | } |
| 412 | | |
| 413 | { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 414 | $$ = new t_struct(g_program); |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 417 | FieldList: |
| 418 | FieldList ',' Field |
| 419 | { |
| 420 | pdebug("FieldList -> FieldList , Field"); |
| 421 | $$ = $1; |
| 422 | $$->append($3); |
| 423 | } |
| 424 | | Field |
| 425 | { |
| 426 | pdebug("FieldList -> Field"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 427 | $$ = new t_struct(g_program); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 428 | $$->append($1); |
| 429 | } |
| 430 | | |
| 431 | { |
| 432 | pdebug("FieldList -> "); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 433 | $$ = new t_struct(g_program); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | Field: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 437 | tok_int_constant ':' FieldType tok_identifier |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 438 | { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 439 | pdebug("tok_int_constant : Field -> FieldType tok_identifier"); |
| 440 | if ($1 <= 0) { |
| 441 | pwarning(1, "Nonpositive value (%d) not allowed as a field key for '%s'.\n", $1, $4); |
| 442 | $1 = y_field_val--; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 443 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 444 | $$ = new t_field($3, $4, $1); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 445 | } |
| 446 | | FieldType tok_identifier |
| 447 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 448 | pdebug("Field -> FieldType tok_identifier"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 449 | pwarning(2, "No field key specified for '%s', resulting protocol may have conflicts or not be backwards compatible!\n", $2); |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 450 | $$ = new t_field($1, $2, y_field_val--); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 451 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 452 | | FieldType tok_identifier '=' tok_int_constant |
| 453 | { |
| 454 | pwarning(1, "Trailing = id notation is deprecated. Use 'Id: Type Name' notatio instead"); |
| 455 | pdebug("Field -> FieldType tok_identifier = tok_int_constant"); |
| 456 | if ($4 <= 0) { |
| 457 | pwarning(1, "Nonpositive value (%d) not allowed as a field key for '%s'.\n", $4, $2); |
| 458 | $4 = y_field_val--; |
| 459 | } |
| 460 | $$ = new t_field($1, $2, $4); |
| 461 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 462 | |
| 463 | DefinitionType: |
| 464 | BaseType |
| 465 | { |
| 466 | pdebug("DefinitionType -> BaseType"); |
| 467 | $$ = $1; |
| 468 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 469 | | ContainerType |
| 470 | { |
| 471 | pdebug("DefinitionType -> ContainerType"); |
| 472 | $$ = $1; |
| 473 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 474 | |
| 475 | FunctionType: |
| 476 | FieldType |
| 477 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 478 | pdebug("FunctionType -> FieldType"); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 479 | $$ = $1; |
| 480 | } |
| 481 | | tok_void |
| 482 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 483 | pdebug("FunctionType -> tok_void"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 484 | $$ = g_type_void; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | FieldType: |
| 488 | tok_identifier |
| 489 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 490 | pdebug("FieldType -> tok_identifier"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 491 | if (g_parse_mode == INCLUDES) { |
| 492 | // Ignore identifiers in include mode |
| 493 | $$ = NULL; |
| 494 | } else { |
| 495 | // Lookup the identifier in the current scope |
| 496 | $$ = g_scope->get_type($1); |
| 497 | if ($$ == NULL) { |
| 498 | yyerror("Type \"%s\" has not been defined.", $1); |
| 499 | exit(1); |
| 500 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 501 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 502 | } |
| 503 | | BaseType |
| 504 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 505 | pdebug("FieldType -> BaseType"); |
| 506 | $$ = $1; |
| 507 | } |
| 508 | | ContainerType |
| 509 | { |
| 510 | pdebug("FieldType -> ContainerType"); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 511 | $$ = $1; |
| 512 | } |
| 513 | |
| 514 | BaseType: |
| 515 | tok_string |
| 516 | { |
| 517 | pdebug("BaseType -> tok_string"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 518 | $$ = g_type_string; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 519 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 520 | | tok_bool |
| 521 | { |
| 522 | pdebug("BaseType -> tok_bool"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 523 | $$ = g_type_bool; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 524 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 525 | | tok_byte |
| 526 | { |
| 527 | pdebug("BaseType -> tok_byte"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 528 | $$ = g_type_byte; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 529 | } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 530 | | tok_i16 |
| 531 | { |
| 532 | pdebug("BaseType -> tok_i16"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 533 | $$ = g_type_i16; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 534 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 535 | | tok_i32 |
| 536 | { |
| 537 | pdebug("BaseType -> tok_i32"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 538 | $$ = g_type_i32; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 539 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 540 | | tok_i64 |
| 541 | { |
| 542 | pdebug("BaseType -> tok_i64"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 543 | $$ = g_type_i64; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 544 | } |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 545 | | tok_double |
| 546 | { |
| 547 | pdebug("BaseType -> tok_double"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 548 | $$ = g_type_double; |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 549 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 550 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 551 | ContainerType: |
| 552 | MapType |
| 553 | { |
| 554 | pdebug("ContainerType -> MapType"); |
| 555 | $$ = $1; |
| 556 | } |
| 557 | | SetType |
| 558 | { |
| 559 | pdebug("ContainerType -> SetType"); |
| 560 | $$ = $1; |
| 561 | } |
| 562 | | ListType |
| 563 | { |
| 564 | pdebug("ContainerType -> ListType"); |
| 565 | $$ = $1; |
| 566 | } |
| 567 | |
| 568 | MapType: |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 569 | tok_map CppTypeOptional '<' FieldType ',' FieldType '>' |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 570 | { |
| 571 | pdebug("MapType -> tok_map <FieldType, FieldType>"); |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 572 | $$ = new t_map($4, $6); |
| 573 | if ($2 != NULL) { |
| 574 | ((t_container*)$$)->set_cpp_name(std::string($2)); |
| 575 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | SetType: |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 579 | tok_set CppTypeOptional '<' FieldType '>' |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 580 | { |
| 581 | pdebug("SetType -> tok_set<FieldType>"); |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 582 | $$ = new t_set($4); |
| 583 | if ($2 != NULL) { |
| 584 | ((t_container*)$$)->set_cpp_name(std::string($2)); |
| 585 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | ListType: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 589 | tok_list '<' FieldType '>' CppTypeOptional |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 590 | { |
| 591 | pdebug("ListType -> tok_list<FieldType>"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 592 | $$ = new t_list($3); |
| 593 | if ($5 != NULL) { |
| 594 | ((t_container*)$$)->set_cpp_name(std::string($5)); |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
| 598 | CppTypeOptional: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 599 | '[' tok_cpp_type tok_literal ']' |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 600 | { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame^] | 601 | $$ = $3; |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 602 | } |
| 603 | | |
| 604 | { |
| 605 | $$ = NULL; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 608 | %% |