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