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