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" |
| 15 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 16 | int y_field_val = -1; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 17 | |
| 18 | %} |
| 19 | |
| 20 | %union { |
| 21 | char* id; |
| 22 | int iconst; |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 23 | bool tbool; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 24 | t_type* ttype; |
| 25 | t_typedef* ttypedef; |
| 26 | t_enum* tenum; |
| 27 | t_struct* tstruct; |
| 28 | t_service* tservice; |
| 29 | t_function* tfunction; |
| 30 | t_field* tfield; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 31 | t_constant* tconstant; |
| 32 | } |
| 33 | |
| 34 | /** Strings and constants */ |
| 35 | %token<id> tok_identifier |
| 36 | %token<iconst> tok_int_constant |
| 37 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 38 | /** Namespace */ |
| 39 | %token tok_namespace |
| 40 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 41 | /** Base datatypes */ |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 42 | %token tok_bool |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 43 | %token tok_byte |
| 44 | %token tok_string |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 45 | %token tok_i16 |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 46 | %token tok_i32 |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 47 | %token tok_i64 |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 48 | %token tok_double |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 49 | |
| 50 | /** Complex Types */ |
| 51 | %token tok_map |
| 52 | %token tok_list |
| 53 | %token tok_set |
| 54 | |
| 55 | /** Function types */ |
| 56 | %token tok_void |
| 57 | |
| 58 | /** Modifiers */ |
| 59 | %token tok_async |
| 60 | |
| 61 | /** Thrift actions */ |
| 62 | %token tok_typedef |
| 63 | %token tok_struct |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 64 | %token tok_xception |
| 65 | %token tok_throws |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 66 | %token tok_service |
| 67 | %token tok_enum |
| 68 | |
| 69 | /** Types */ |
| 70 | %type<ttype> BaseType |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 71 | %type<ttype> ContainerType |
| 72 | %type<ttype> MapType |
| 73 | %type<ttype> SetType |
| 74 | %type<ttype> ListType |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 75 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 76 | %type<id> Namespace |
| 77 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 78 | %type<ttypedef> Typedef |
| 79 | %type<ttype> DefinitionType |
| 80 | |
| 81 | %type<tfield> Field |
| 82 | %type<ttype> FieldType |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 83 | %type<tstruct> FieldList |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 84 | %type<tstruct> ThrowsOptional |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 85 | |
| 86 | %type<tenum> Enum |
| 87 | %type<tenum> EnumDefList |
| 88 | %type<tconstant> EnumDef |
| 89 | |
| 90 | %type<tstruct> Struct |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 91 | %type<tstruct> Xception |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 92 | |
| 93 | %type<tservice> Service |
| 94 | |
| 95 | %type<tfunction> Function |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 96 | %type<ttype> FunctionType |
| 97 | %type<tservice> FunctionList |
| 98 | |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 99 | %type<tbool> AsyncOptional |
| 100 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 101 | %% |
| 102 | |
| 103 | /** Thrift Grammar */ |
| 104 | |
| 105 | Program: |
| 106 | DefinitionList |
| 107 | { |
| 108 | pdebug("Program -> DefinitionList"); |
| 109 | } |
| 110 | |
| 111 | DefinitionList: |
| 112 | DefinitionList Definition |
| 113 | { |
| 114 | pdebug("DefinitionList -> DefinitionList Definition"); |
| 115 | } |
| 116 | | |
| 117 | { |
| 118 | pdebug("DefinitionList -> "); |
| 119 | } |
| 120 | |
| 121 | Definition: |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 122 | Namespace |
| 123 | { |
| 124 | pdebug("Definition -> Namespace"); |
| 125 | g_program->set_namespace($1); |
| 126 | } |
| 127 | | Typedef |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 128 | { |
| 129 | pdebug("Definition -> Typedef"); |
| 130 | g_program->add_typedef($1); |
| 131 | } |
| 132 | | Enum |
| 133 | { |
| 134 | pdebug("Definition -> Enum"); |
| 135 | g_program->add_enum($1); |
| 136 | } |
| 137 | | Struct |
| 138 | { |
| 139 | pdebug("Definition -> Struct"); |
| 140 | g_program->add_struct($1); |
| 141 | } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 142 | | Xception |
| 143 | { |
| 144 | pdebug("Definition -> Xception"); |
| 145 | g_program->add_xception($1); |
| 146 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 147 | | Service |
| 148 | { |
| 149 | pdebug("Definition -> Service"); |
| 150 | g_program->add_service($1); |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | Namespace: |
| 154 | tok_namespace tok_identifier |
| 155 | { |
| 156 | pdebug("Namespace -> tok_namespace tok_identifier"); |
| 157 | $$ = $2; |
| 158 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 159 | |
| 160 | Typedef: |
| 161 | tok_typedef DefinitionType tok_identifier |
| 162 | { |
| 163 | pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier"); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 164 | t_typedef *td = new t_typedef($2, $3); |
| 165 | $$ = td; |
| 166 | } |
| 167 | |
| 168 | Enum: |
| 169 | tok_enum tok_identifier '{' EnumDefList '}' |
| 170 | { |
| 171 | pdebug("Enum -> tok_enum tok_identifier { EnumDefList }"); |
| 172 | $$ = $4; |
| 173 | $$->set_name($2); |
| 174 | } |
| 175 | |
| 176 | EnumDefList: |
| 177 | EnumDefList ',' EnumDef |
| 178 | { |
| 179 | pdebug("EnumDefList -> EnumDefList EnumDef"); |
| 180 | $$ = $1; |
| 181 | $$->append($3); |
| 182 | } |
| 183 | | EnumDef |
| 184 | { |
| 185 | pdebug("EnumDefList -> EnumDef"); |
| 186 | $$ = new t_enum; |
| 187 | $$->append($1); |
| 188 | } |
| 189 | | |
| 190 | { |
| 191 | pdebug("EnumDefList -> "); |
| 192 | $$ = new t_enum; |
| 193 | } |
| 194 | |
| 195 | EnumDef: |
| 196 | tok_identifier '=' tok_int_constant |
| 197 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 198 | pdebug("EnumDef => tok_identifier = tok_int_constant"); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 199 | if ($3 < 0) { |
| 200 | printf("WARNING (%d): Negative value supplied for enum %s.\n", yylineno, $1); |
| 201 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 202 | $$ = new t_constant($1, $3); |
| 203 | } |
| 204 | | |
| 205 | tok_identifier |
| 206 | { |
| 207 | pdebug("EnumDef => tok_identifier"); |
| 208 | $$ = new t_constant($1); |
| 209 | } |
| 210 | |
| 211 | Struct: |
| 212 | tok_struct tok_identifier '{' FieldList '}' |
| 213 | { |
| 214 | pdebug("Struct -> tok_struct tok_identifier { FieldList }"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 215 | $4->set_name($2); |
| 216 | $$ = $4; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 217 | y_field_val = -1; |
| 218 | } |
| 219 | |
| 220 | Xception: |
| 221 | tok_xception tok_identifier '{' FieldList '}' |
| 222 | { |
| 223 | pdebug("Xception -> tok_xception tok_identifier { FieldList }"); |
| 224 | $4->set_name($2); |
| 225 | $4->set_xception(true); |
| 226 | $$ = $4; |
| 227 | y_field_val = -1; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | Service: |
| 231 | tok_service tok_identifier '{' FunctionList '}' |
| 232 | { |
| 233 | pdebug("Service -> tok_service tok_identifier { FunctionList }"); |
| 234 | $$ = $4; |
| 235 | $$->set_name($2); |
| 236 | } |
| 237 | |
| 238 | FunctionList: |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 239 | FunctionList Function CommaOptional |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 240 | { |
| 241 | pdebug("FunctionList -> FunctionList Function"); |
| 242 | $$ = $1; |
| 243 | $1->add_function($2); |
| 244 | } |
| 245 | | |
| 246 | { |
| 247 | pdebug("FunctionList -> "); |
| 248 | $$ = new t_service; |
| 249 | } |
| 250 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 251 | CommaOptional: |
| 252 | ',' |
| 253 | {} |
| 254 | | |
| 255 | {} |
| 256 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 257 | Function: |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 258 | FunctionType AsyncOptional tok_identifier '(' FieldList ')' ThrowsOptional |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 259 | { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 260 | $5->set_name(std::string($3) + "_args"); |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 261 | $$ = new t_function($1, $3, $5, $7, $2); |
| 262 | y_field_val = -1; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 265 | AsyncOptional: |
| 266 | tok_async |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 267 | { |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 268 | $$ = true; |
| 269 | } |
| 270 | | |
| 271 | { |
| 272 | $$ = false; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 275 | ThrowsOptional: |
| 276 | tok_throws '(' FieldList ')' |
| 277 | { |
| 278 | $$ = $3; |
| 279 | } |
| 280 | | |
| 281 | { |
| 282 | $$ = new t_struct; |
| 283 | } |
| 284 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 285 | FieldList: |
| 286 | FieldList ',' Field |
| 287 | { |
| 288 | pdebug("FieldList -> FieldList , Field"); |
| 289 | $$ = $1; |
| 290 | $$->append($3); |
| 291 | } |
| 292 | | Field |
| 293 | { |
| 294 | pdebug("FieldList -> Field"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 295 | $$ = new t_struct; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 296 | $$->append($1); |
| 297 | } |
| 298 | | |
| 299 | { |
| 300 | pdebug("FieldList -> "); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 301 | $$ = new t_struct; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | Field: |
| 305 | FieldType tok_identifier '=' tok_int_constant |
| 306 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 307 | pdebug("Field -> FieldType tok_identifier = tok_int_constant"); |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 308 | if ($4 <= 0) { |
| 309 | printf("WARNING (%d): Nonpositive value (%d) not allowed as a field key for '%s'.\n", yylineno, $4, $2); |
| 310 | $4 = y_field_val--; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 311 | } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 312 | $$ = new t_field($1, $2, $4); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 313 | } |
| 314 | | FieldType tok_identifier |
| 315 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 316 | pdebug("Field -> FieldType tok_identifier"); |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 317 | printf("WARNING (%d): No field key specified for '%s', resulting protocol may have conflicts or not be backwards compatible!\n", yylineno, $2); |
| 318 | $$ = new t_field($1, $2, y_field_val--); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | DefinitionType: |
| 322 | BaseType |
| 323 | { |
| 324 | pdebug("DefinitionType -> BaseType"); |
| 325 | $$ = $1; |
| 326 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 327 | | ContainerType |
| 328 | { |
| 329 | pdebug("DefinitionType -> ContainerType"); |
| 330 | $$ = $1; |
| 331 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 332 | |
| 333 | FunctionType: |
| 334 | FieldType |
| 335 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 336 | pdebug("FunctionType -> FieldType"); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 337 | $$ = $1; |
| 338 | } |
| 339 | | tok_void |
| 340 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 341 | pdebug("FunctionType -> tok_void"); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 342 | $$ = g_program->get_void_type(); |
| 343 | } |
| 344 | |
| 345 | FieldType: |
| 346 | tok_identifier |
| 347 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 348 | pdebug("FieldType -> tok_identifier"); |
| 349 | $$ = g_program->get_custom_type($1); |
| 350 | if ($$ == NULL) { |
| 351 | yyerror("Type \"%s\" has not been defined.", $1); |
| 352 | exit(1); |
| 353 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 354 | } |
| 355 | | BaseType |
| 356 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 357 | pdebug("FieldType -> BaseType"); |
| 358 | $$ = $1; |
| 359 | } |
| 360 | | ContainerType |
| 361 | { |
| 362 | pdebug("FieldType -> ContainerType"); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 363 | $$ = $1; |
| 364 | } |
| 365 | |
| 366 | BaseType: |
| 367 | tok_string |
| 368 | { |
| 369 | pdebug("BaseType -> tok_string"); |
| 370 | $$ = g_program->get_string_type(); |
| 371 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 372 | | tok_bool |
| 373 | { |
| 374 | pdebug("BaseType -> tok_bool"); |
| 375 | $$ = g_program->get_bool_type(); |
| 376 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 377 | | tok_byte |
| 378 | { |
| 379 | pdebug("BaseType -> tok_byte"); |
| 380 | $$ = g_program->get_byte_type(); |
| 381 | } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 382 | | tok_i16 |
| 383 | { |
| 384 | pdebug("BaseType -> tok_i16"); |
| 385 | $$ = g_program->get_i16_type(); |
| 386 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 387 | | tok_i32 |
| 388 | { |
| 389 | pdebug("BaseType -> tok_i32"); |
| 390 | $$ = g_program->get_i32_type(); |
| 391 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 392 | | tok_i64 |
| 393 | { |
| 394 | pdebug("BaseType -> tok_i64"); |
| 395 | $$ = g_program->get_i64_type(); |
| 396 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 397 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 398 | ContainerType: |
| 399 | MapType |
| 400 | { |
| 401 | pdebug("ContainerType -> MapType"); |
| 402 | $$ = $1; |
| 403 | } |
| 404 | | SetType |
| 405 | { |
| 406 | pdebug("ContainerType -> SetType"); |
| 407 | $$ = $1; |
| 408 | } |
| 409 | | ListType |
| 410 | { |
| 411 | pdebug("ContainerType -> ListType"); |
| 412 | $$ = $1; |
| 413 | } |
| 414 | |
| 415 | MapType: |
| 416 | tok_map '<' FieldType ',' FieldType '>' |
| 417 | { |
| 418 | pdebug("MapType -> tok_map <FieldType, FieldType>"); |
| 419 | $$ = new t_map($3, $5); |
| 420 | } |
| 421 | |
| 422 | SetType: |
| 423 | tok_set '<' FieldType '>' |
| 424 | { |
| 425 | pdebug("SetType -> tok_set<FieldType>"); |
| 426 | $$ = new t_set($3); |
| 427 | } |
| 428 | |
| 429 | ListType: |
| 430 | tok_list '<' FieldType '>' |
| 431 | { |
| 432 | pdebug("ListType -> tok_list<FieldType>"); |
| 433 | $$ = new t_list($3); |
| 434 | } |
| 435 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 436 | %% |