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