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 | |
Mark Slee | 207cb46 | 2006-11-02 18:43:12 +0000 | [diff] [blame] | 288 | CommaOrSemicolonOptional: |
| 289 | ',' |
| 290 | {} |
| 291 | | ';' |
| 292 | {} |
| 293 | | |
| 294 | {} |
| 295 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 296 | EnumDefList: |
Mark Slee | 207cb46 | 2006-11-02 18:43:12 +0000 | [diff] [blame] | 297 | EnumDefList EnumDef |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 298 | { |
| 299 | pdebug("EnumDefList -> EnumDefList EnumDef"); |
| 300 | $$ = $1; |
Mark Slee | 207cb46 | 2006-11-02 18:43:12 +0000 | [diff] [blame] | 301 | $$->append($2); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 302 | } |
| 303 | | |
| 304 | { |
| 305 | pdebug("EnumDefList -> "); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 306 | $$ = new t_enum(g_program); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | EnumDef: |
Mark Slee | 207cb46 | 2006-11-02 18:43:12 +0000 | [diff] [blame] | 310 | tok_identifier '=' tok_int_constant CommaOrSemicolonOptional |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 311 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 312 | pdebug("EnumDef => tok_identifier = tok_int_constant"); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 313 | if ($3 < 0) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 314 | pwarning(1, "Negative value supplied for enum %s.\n", $1); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 315 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 316 | $$ = new t_constant($1, $3); |
| 317 | } |
| 318 | | |
Mark Slee | 04cc605 | 2006-11-15 21:25:34 +0000 | [diff] [blame^] | 319 | tok_identifier CommaOrSemicolonOptional |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 320 | { |
| 321 | pdebug("EnumDef => tok_identifier"); |
| 322 | $$ = new t_constant($1); |
| 323 | } |
| 324 | |
| 325 | Struct: |
| 326 | tok_struct tok_identifier '{' FieldList '}' |
| 327 | { |
| 328 | pdebug("Struct -> tok_struct tok_identifier { FieldList }"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 329 | $4->set_name($2); |
| 330 | $$ = $4; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 331 | y_field_val = -1; |
| 332 | } |
| 333 | |
| 334 | Xception: |
| 335 | tok_xception tok_identifier '{' FieldList '}' |
| 336 | { |
| 337 | pdebug("Xception -> tok_xception tok_identifier { FieldList }"); |
| 338 | $4->set_name($2); |
| 339 | $4->set_xception(true); |
| 340 | $$ = $4; |
| 341 | y_field_val = -1; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | Service: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 345 | tok_service tok_identifier ExtendsOptional '{' FunctionList '}' |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 346 | { |
| 347 | pdebug("Service -> tok_service tok_identifier { FunctionList }"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 348 | $$ = $5; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 349 | $$->set_name($2); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 350 | $$->set_extends($3); |
| 351 | } |
| 352 | |
| 353 | ExtendsOptional: |
| 354 | tok_extends tok_identifier |
| 355 | { |
| 356 | pdebug("ExtendsOptional -> tok_extends tok_identifier"); |
| 357 | $$ = NULL; |
| 358 | if (g_parse_mode == PROGRAM) { |
| 359 | $$ = g_scope->get_service($2); |
| 360 | if ($$ == NULL) { |
| 361 | yyerror("Service \"%s\" has not been defined.", $2); |
| 362 | exit(1); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | | |
| 367 | { |
| 368 | $$ = NULL; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | FunctionList: |
Mark Slee | 207cb46 | 2006-11-02 18:43:12 +0000 | [diff] [blame] | 372 | FunctionList Function |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 373 | { |
| 374 | pdebug("FunctionList -> FunctionList Function"); |
| 375 | $$ = $1; |
| 376 | $1->add_function($2); |
| 377 | } |
| 378 | | |
| 379 | { |
| 380 | pdebug("FunctionList -> "); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 381 | $$ = new t_service(g_program); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | Function: |
Mark Slee | 207cb46 | 2006-11-02 18:43:12 +0000 | [diff] [blame] | 385 | AsyncOptional FunctionType tok_identifier '(' FieldList ')' ThrowsOptional CommaOrSemicolonOptional |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 386 | { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 387 | $5->set_name(std::string($3) + "_args"); |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 388 | $$ = new t_function($2, $3, $5, $7, $1); |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 389 | y_field_val = -1; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 392 | AsyncOptional: |
| 393 | tok_async |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 394 | { |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 395 | $$ = true; |
| 396 | } |
| 397 | | |
| 398 | { |
| 399 | $$ = false; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 402 | ThrowsOptional: |
| 403 | tok_throws '(' FieldList ')' |
| 404 | { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 405 | pdebug("ThrowsOptional -> tok_throws ( FieldList )"); |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 406 | $$ = $3; |
| 407 | } |
| 408 | | |
| 409 | { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 410 | $$ = new t_struct(g_program); |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 413 | FieldList: |
Mark Slee | 207cb46 | 2006-11-02 18:43:12 +0000 | [diff] [blame] | 414 | FieldList Field |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 415 | { |
| 416 | pdebug("FieldList -> FieldList , Field"); |
| 417 | $$ = $1; |
Mark Slee | 207cb46 | 2006-11-02 18:43:12 +0000 | [diff] [blame] | 418 | $$->append($2); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 419 | } |
| 420 | | |
| 421 | { |
| 422 | pdebug("FieldList -> "); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 423 | $$ = new t_struct(g_program); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | Field: |
Mark Slee | 207cb46 | 2006-11-02 18:43:12 +0000 | [diff] [blame] | 427 | tok_int_constant ':' FieldType tok_identifier CommaOrSemicolonOptional |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 428 | { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 429 | pdebug("tok_int_constant : Field -> FieldType tok_identifier"); |
| 430 | if ($1 <= 0) { |
| 431 | pwarning(1, "Nonpositive value (%d) not allowed as a field key for '%s'.\n", $1, $4); |
| 432 | $1 = y_field_val--; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 433 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 434 | $$ = new t_field($3, $4, $1); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 435 | } |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 436 | | FieldType tok_identifier CommaOrSemicolonOptional |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 437 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 438 | pdebug("Field -> FieldType tok_identifier"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 439 | 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] | 440 | $$ = new t_field($1, $2, y_field_val--); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 441 | } |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 442 | | FieldType tok_identifier '=' tok_int_constant CommaOrSemicolonOptional |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 443 | { |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 444 | pwarning(1, "Trailing = id notation is deprecated. Use 'Id: Type Name' notation instead"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 445 | pdebug("Field -> FieldType tok_identifier = tok_int_constant"); |
| 446 | if ($4 <= 0) { |
| 447 | pwarning(1, "Nonpositive value (%d) not allowed as a field key for '%s'.\n", $4, $2); |
| 448 | $4 = y_field_val--; |
| 449 | } |
| 450 | $$ = new t_field($1, $2, $4); |
| 451 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 452 | |
| 453 | DefinitionType: |
| 454 | BaseType |
| 455 | { |
| 456 | pdebug("DefinitionType -> BaseType"); |
| 457 | $$ = $1; |
| 458 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 459 | | ContainerType |
| 460 | { |
| 461 | pdebug("DefinitionType -> ContainerType"); |
| 462 | $$ = $1; |
| 463 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 464 | |
| 465 | FunctionType: |
| 466 | FieldType |
| 467 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 468 | pdebug("FunctionType -> FieldType"); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 469 | $$ = $1; |
| 470 | } |
| 471 | | tok_void |
| 472 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 473 | pdebug("FunctionType -> tok_void"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 474 | $$ = g_type_void; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | FieldType: |
| 478 | tok_identifier |
| 479 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 480 | pdebug("FieldType -> tok_identifier"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 481 | if (g_parse_mode == INCLUDES) { |
| 482 | // Ignore identifiers in include mode |
| 483 | $$ = NULL; |
| 484 | } else { |
| 485 | // Lookup the identifier in the current scope |
| 486 | $$ = g_scope->get_type($1); |
| 487 | if ($$ == NULL) { |
| 488 | yyerror("Type \"%s\" has not been defined.", $1); |
| 489 | exit(1); |
| 490 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 491 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 492 | } |
| 493 | | BaseType |
| 494 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 495 | pdebug("FieldType -> BaseType"); |
| 496 | $$ = $1; |
| 497 | } |
| 498 | | ContainerType |
| 499 | { |
| 500 | pdebug("FieldType -> ContainerType"); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 501 | $$ = $1; |
| 502 | } |
| 503 | |
| 504 | BaseType: |
| 505 | tok_string |
| 506 | { |
| 507 | pdebug("BaseType -> tok_string"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 508 | $$ = g_type_string; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 509 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 510 | | tok_bool |
| 511 | { |
| 512 | pdebug("BaseType -> tok_bool"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 513 | $$ = g_type_bool; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 514 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 515 | | tok_byte |
| 516 | { |
| 517 | pdebug("BaseType -> tok_byte"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 518 | $$ = g_type_byte; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 519 | } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 520 | | tok_i16 |
| 521 | { |
| 522 | pdebug("BaseType -> tok_i16"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 523 | $$ = g_type_i16; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 524 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 525 | | tok_i32 |
| 526 | { |
| 527 | pdebug("BaseType -> tok_i32"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 528 | $$ = g_type_i32; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 529 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 530 | | tok_i64 |
| 531 | { |
| 532 | pdebug("BaseType -> tok_i64"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 533 | $$ = g_type_i64; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 534 | } |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 535 | | tok_double |
| 536 | { |
| 537 | pdebug("BaseType -> tok_double"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 538 | $$ = g_type_double; |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 539 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 540 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 541 | ContainerType: |
| 542 | MapType |
| 543 | { |
| 544 | pdebug("ContainerType -> MapType"); |
| 545 | $$ = $1; |
| 546 | } |
| 547 | | SetType |
| 548 | { |
| 549 | pdebug("ContainerType -> SetType"); |
| 550 | $$ = $1; |
| 551 | } |
| 552 | | ListType |
| 553 | { |
| 554 | pdebug("ContainerType -> ListType"); |
| 555 | $$ = $1; |
| 556 | } |
| 557 | |
| 558 | MapType: |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 559 | tok_map CppTypeOptional '<' FieldType ',' FieldType '>' |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 560 | { |
| 561 | pdebug("MapType -> tok_map <FieldType, FieldType>"); |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 562 | $$ = new t_map($4, $6); |
| 563 | if ($2 != NULL) { |
| 564 | ((t_container*)$$)->set_cpp_name(std::string($2)); |
| 565 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | SetType: |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 569 | tok_set CppTypeOptional '<' FieldType '>' |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 570 | { |
| 571 | pdebug("SetType -> tok_set<FieldType>"); |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 572 | $$ = new t_set($4); |
| 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 | ListType: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 579 | tok_list '<' FieldType '>' CppTypeOptional |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 580 | { |
| 581 | pdebug("ListType -> tok_list<FieldType>"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 582 | $$ = new t_list($3); |
| 583 | if ($5 != NULL) { |
| 584 | ((t_container*)$$)->set_cpp_name(std::string($5)); |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
| 588 | CppTypeOptional: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 589 | '[' tok_cpp_type tok_literal ']' |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 590 | { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 591 | $$ = $3; |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 592 | } |
| 593 | | |
| 594 | { |
| 595 | $$ = NULL; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 598 | %% |