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