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"); |
Mark Slee | aa7671d | 2006-11-29 03:19:31 +0000 | [diff] [blame^] | 348 | if (g_parse_mode == PROGRAM) { |
| 349 | $$ = new t_const($2, $3, $5); |
| 350 | validate_const_type($$); |
| 351 | } else { |
| 352 | $$ = NULL; |
| 353 | } |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | ConstValue: |
| 357 | tok_int_constant |
| 358 | { |
| 359 | pdebug("ConstValue => tok_int_constant"); |
| 360 | $$ = new t_const_value(); |
| 361 | $$->set_integer($1); |
| 362 | } |
| 363 | | tok_dub_constant |
| 364 | { |
| 365 | pdebug("ConstValue => tok_dub_constant"); |
| 366 | $$ = new t_const_value(); |
| 367 | $$->set_double($1); |
| 368 | } |
| 369 | | tok_literal |
| 370 | { |
| 371 | pdebug("ConstValue => tok_literal"); |
| 372 | $$ = new t_const_value(); |
| 373 | $$->set_string($1); |
| 374 | } |
| 375 | | ConstList |
| 376 | { |
| 377 | pdebug("ConstValue => ConstList"); |
| 378 | $$ = $1; |
| 379 | } |
| 380 | | ConstMap |
| 381 | { |
| 382 | pdebug("ConstValue => ConstMap"); |
| 383 | $$ = $1; |
| 384 | } |
| 385 | |
| 386 | ConstList: |
| 387 | '[' ConstListContents ']' |
| 388 | { |
| 389 | pdebug("ConstList => [ ConstListContents ]"); |
| 390 | $$ = $2; |
| 391 | } |
| 392 | |
| 393 | ConstListContents: |
| 394 | ConstListContents ConstValue CommaOrSemicolonOptional |
| 395 | { |
| 396 | pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional"); |
| 397 | $$ = $1; |
| 398 | $$->add_list($2); |
| 399 | } |
| 400 | | |
| 401 | { |
| 402 | pdebug("ConstListContents =>"); |
| 403 | $$ = new t_const_value(); |
| 404 | $$->set_list(); |
| 405 | } |
| 406 | |
| 407 | ConstMap: |
| 408 | '{' ConstMapContents '}' |
| 409 | { |
| 410 | pdebug("ConstMap => { ConstMapContents }"); |
| 411 | $$ = $2; |
| 412 | } |
| 413 | |
| 414 | ConstMapContents: |
| 415 | ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional |
| 416 | { |
| 417 | pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional"); |
| 418 | $$ = $1; |
| 419 | $$->add_map($2, $4); |
| 420 | } |
| 421 | | |
| 422 | { |
| 423 | pdebug("ConstMapContents =>"); |
| 424 | $$ = new t_const_value(); |
| 425 | $$->set_map(); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | Struct: |
| 429 | tok_struct tok_identifier '{' FieldList '}' |
| 430 | { |
| 431 | pdebug("Struct -> tok_struct tok_identifier { FieldList }"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 432 | $4->set_name($2); |
| 433 | $$ = $4; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 434 | y_field_val = -1; |
| 435 | } |
| 436 | |
| 437 | Xception: |
| 438 | tok_xception tok_identifier '{' FieldList '}' |
| 439 | { |
| 440 | pdebug("Xception -> tok_xception tok_identifier { FieldList }"); |
| 441 | $4->set_name($2); |
| 442 | $4->set_xception(true); |
| 443 | $$ = $4; |
| 444 | y_field_val = -1; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | Service: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 448 | tok_service tok_identifier ExtendsOptional '{' FunctionList '}' |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 449 | { |
| 450 | pdebug("Service -> tok_service tok_identifier { FunctionList }"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 451 | $$ = $5; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 452 | $$->set_name($2); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 453 | $$->set_extends($3); |
| 454 | } |
| 455 | |
| 456 | ExtendsOptional: |
| 457 | tok_extends tok_identifier |
| 458 | { |
| 459 | pdebug("ExtendsOptional -> tok_extends tok_identifier"); |
| 460 | $$ = NULL; |
| 461 | if (g_parse_mode == PROGRAM) { |
| 462 | $$ = g_scope->get_service($2); |
| 463 | if ($$ == NULL) { |
| 464 | yyerror("Service \"%s\" has not been defined.", $2); |
| 465 | exit(1); |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | | |
| 470 | { |
| 471 | $$ = NULL; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | FunctionList: |
Mark Slee | 207cb46 | 2006-11-02 18:43:12 +0000 | [diff] [blame] | 475 | FunctionList Function |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 476 | { |
| 477 | pdebug("FunctionList -> FunctionList Function"); |
| 478 | $$ = $1; |
| 479 | $1->add_function($2); |
| 480 | } |
| 481 | | |
| 482 | { |
| 483 | pdebug("FunctionList -> "); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 484 | $$ = new t_service(g_program); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | Function: |
Mark Slee | 207cb46 | 2006-11-02 18:43:12 +0000 | [diff] [blame] | 488 | AsyncOptional FunctionType tok_identifier '(' FieldList ')' ThrowsOptional CommaOrSemicolonOptional |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 489 | { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 490 | $5->set_name(std::string($3) + "_args"); |
Mark Slee | 4e755ca | 2006-09-12 00:46:08 +0000 | [diff] [blame] | 491 | $$ = new t_function($2, $3, $5, $7, $1); |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 492 | y_field_val = -1; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 495 | AsyncOptional: |
| 496 | tok_async |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 497 | { |
Mark Slee | 52f643d | 2006-08-09 00:03:43 +0000 | [diff] [blame] | 498 | $$ = true; |
| 499 | } |
| 500 | | |
| 501 | { |
| 502 | $$ = false; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 505 | ThrowsOptional: |
| 506 | tok_throws '(' FieldList ')' |
| 507 | { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 508 | pdebug("ThrowsOptional -> tok_throws ( FieldList )"); |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 509 | $$ = $3; |
| 510 | } |
| 511 | | |
| 512 | { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 513 | $$ = new t_struct(g_program); |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 516 | FieldList: |
Mark Slee | 207cb46 | 2006-11-02 18:43:12 +0000 | [diff] [blame] | 517 | FieldList Field |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 518 | { |
| 519 | pdebug("FieldList -> FieldList , Field"); |
| 520 | $$ = $1; |
Mark Slee | 207cb46 | 2006-11-02 18:43:12 +0000 | [diff] [blame] | 521 | $$->append($2); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 522 | } |
| 523 | | |
| 524 | { |
| 525 | pdebug("FieldList -> "); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 526 | $$ = new t_struct(g_program); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | Field: |
Mark Slee | 207cb46 | 2006-11-02 18:43:12 +0000 | [diff] [blame] | 530 | tok_int_constant ':' FieldType tok_identifier CommaOrSemicolonOptional |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 531 | { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 532 | pdebug("tok_int_constant : Field -> FieldType tok_identifier"); |
| 533 | if ($1 <= 0) { |
| 534 | pwarning(1, "Nonpositive value (%d) not allowed as a field key for '%s'.\n", $1, $4); |
| 535 | $1 = y_field_val--; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 536 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 537 | $$ = new t_field($3, $4, $1); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 538 | } |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 539 | | FieldType tok_identifier CommaOrSemicolonOptional |
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 | pdebug("Field -> FieldType tok_identifier"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 542 | 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] | 543 | $$ = new t_field($1, $2, y_field_val--); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 544 | } |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 545 | | FieldType tok_identifier '=' tok_int_constant CommaOrSemicolonOptional |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 546 | { |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 547 | 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] | 548 | pdebug("Field -> FieldType tok_identifier = tok_int_constant"); |
| 549 | if ($4 <= 0) { |
| 550 | pwarning(1, "Nonpositive value (%d) not allowed as a field key for '%s'.\n", $4, $2); |
| 551 | $4 = y_field_val--; |
| 552 | } |
| 553 | $$ = new t_field($1, $2, $4); |
| 554 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 555 | |
| 556 | DefinitionType: |
| 557 | BaseType |
| 558 | { |
| 559 | pdebug("DefinitionType -> BaseType"); |
| 560 | $$ = $1; |
| 561 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 562 | | ContainerType |
| 563 | { |
| 564 | pdebug("DefinitionType -> ContainerType"); |
| 565 | $$ = $1; |
| 566 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 567 | |
| 568 | FunctionType: |
| 569 | FieldType |
| 570 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 571 | pdebug("FunctionType -> FieldType"); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 572 | $$ = $1; |
| 573 | } |
| 574 | | tok_void |
| 575 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 576 | pdebug("FunctionType -> tok_void"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 577 | $$ = g_type_void; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | FieldType: |
| 581 | tok_identifier |
| 582 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 583 | pdebug("FieldType -> tok_identifier"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 584 | if (g_parse_mode == INCLUDES) { |
| 585 | // Ignore identifiers in include mode |
| 586 | $$ = NULL; |
| 587 | } else { |
| 588 | // Lookup the identifier in the current scope |
| 589 | $$ = g_scope->get_type($1); |
| 590 | if ($$ == NULL) { |
| 591 | yyerror("Type \"%s\" has not been defined.", $1); |
| 592 | exit(1); |
| 593 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 594 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 595 | } |
| 596 | | BaseType |
| 597 | { |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 598 | pdebug("FieldType -> BaseType"); |
| 599 | $$ = $1; |
| 600 | } |
| 601 | | ContainerType |
| 602 | { |
| 603 | pdebug("FieldType -> ContainerType"); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 604 | $$ = $1; |
| 605 | } |
| 606 | |
| 607 | BaseType: |
| 608 | tok_string |
| 609 | { |
| 610 | pdebug("BaseType -> tok_string"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 611 | $$ = g_type_string; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 612 | } |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 613 | | tok_bool |
| 614 | { |
| 615 | pdebug("BaseType -> tok_bool"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 616 | $$ = g_type_bool; |
Mark Slee | 78f58e2 | 2006-09-02 04:17:07 +0000 | [diff] [blame] | 617 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 618 | | tok_byte |
| 619 | { |
| 620 | pdebug("BaseType -> tok_byte"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 621 | $$ = g_type_byte; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 622 | } |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 623 | | tok_i16 |
| 624 | { |
| 625 | pdebug("BaseType -> tok_i16"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 626 | $$ = g_type_i16; |
Mark Slee | 9cb7c61 | 2006-09-01 22:17:45 +0000 | [diff] [blame] | 627 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 628 | | tok_i32 |
| 629 | { |
| 630 | pdebug("BaseType -> tok_i32"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 631 | $$ = g_type_i32; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 632 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 633 | | tok_i64 |
| 634 | { |
| 635 | pdebug("BaseType -> tok_i64"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 636 | $$ = g_type_i64; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 637 | } |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 638 | | tok_double |
| 639 | { |
| 640 | pdebug("BaseType -> tok_double"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 641 | $$ = g_type_double; |
Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 642 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 643 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 644 | ContainerType: |
| 645 | MapType |
| 646 | { |
| 647 | pdebug("ContainerType -> MapType"); |
| 648 | $$ = $1; |
| 649 | } |
| 650 | | SetType |
| 651 | { |
| 652 | pdebug("ContainerType -> SetType"); |
| 653 | $$ = $1; |
| 654 | } |
| 655 | | ListType |
| 656 | { |
| 657 | pdebug("ContainerType -> ListType"); |
| 658 | $$ = $1; |
| 659 | } |
| 660 | |
| 661 | MapType: |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 662 | tok_map CppTypeOptional '<' FieldType ',' FieldType '>' |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 663 | { |
| 664 | pdebug("MapType -> tok_map <FieldType, FieldType>"); |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 665 | $$ = new t_map($4, $6); |
| 666 | if ($2 != NULL) { |
| 667 | ((t_container*)$$)->set_cpp_name(std::string($2)); |
| 668 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | SetType: |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 672 | tok_set CppTypeOptional '<' FieldType '>' |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 673 | { |
| 674 | pdebug("SetType -> tok_set<FieldType>"); |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 675 | $$ = new t_set($4); |
| 676 | if ($2 != NULL) { |
| 677 | ((t_container*)$$)->set_cpp_name(std::string($2)); |
| 678 | } |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | ListType: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 682 | tok_list '<' FieldType '>' CppTypeOptional |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 683 | { |
| 684 | pdebug("ListType -> tok_list<FieldType>"); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 685 | $$ = new t_list($3); |
| 686 | if ($5 != NULL) { |
| 687 | ((t_container*)$$)->set_cpp_name(std::string($5)); |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
| 691 | CppTypeOptional: |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 692 | '[' tok_cpp_type tok_literal ']' |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 693 | { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 694 | $$ = $3; |
Mark Slee | 4f8da1d | 2006-10-12 02:47:27 +0000 | [diff] [blame] | 695 | } |
| 696 | | |
| 697 | { |
| 698 | $$ = NULL; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 701 | %% |