blob: ce11bf8661584e2918735278187ed5aac9b614f8 [file] [log] [blame]
Mark Slee31985722006-05-24 21:45:31 +00001%{
Mark Sleee9ce01c2007-05-16 02:29:53 +00002// Copyright (c) 2006- Facebook
3// Distributed under the Thrift Software License
4//
5// See accompanying file LICENSE or visit the Thrift site at:
6// http://developers.facebook.com/thrift/
Mark Slee31985722006-05-24 21:45:31 +00007
8/**
9 * Thrift parser.
10 *
11 * This parser is used on a thrift definition file.
12 *
13 * @author Mark Slee <mcslee@facebook.com>
14 */
15
16#include <stdio.h>
17#include "main.h"
18#include "globals.h"
19#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000020#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000021
Mark Sleef5377b32006-10-10 01:42:59 +000022/**
23 * This global variable is used for automatic numbering of field indices etc.
24 * when parsing the members of a struct. Field values are automatically
25 * assigned starting from -1 and working their way down.
26 */
Mark Slee9cb7c612006-09-01 22:17:45 +000027int y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +000028
29%}
30
Mark Sleef5377b32006-10-10 01:42:59 +000031/**
32 * This structure is used by the parser to hold the data types associated with
33 * various parse nodes.
34 */
Mark Slee31985722006-05-24 21:45:31 +000035%union {
Mark Slee30152872006-11-28 01:24:07 +000036 char* id;
37 int iconst;
38 double dconst;
39 bool tbool;
40 t_type* ttype;
Mark Slee6a47fed2007-02-07 02:40:59 +000041 t_base_type* tbase;
Mark Slee30152872006-11-28 01:24:07 +000042 t_typedef* ttypedef;
43 t_enum* tenum;
44 t_enum_value* tenumv;
45 t_const* tconst;
46 t_const_value* tconstv;
47 t_struct* tstruct;
48 t_service* tservice;
49 t_function* tfunction;
50 t_field* tfield;
ccheeverf53b5cf2007-02-05 20:33:11 +000051 char* tdoc;
Mark Slee31985722006-05-24 21:45:31 +000052}
53
Mark Sleef5377b32006-10-10 01:42:59 +000054/**
55 * Strings identifier
56 */
Mark Slee31985722006-05-24 21:45:31 +000057%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +000058%token<id> tok_literal
ccheeverf53b5cf2007-02-05 20:33:11 +000059%token<tdoc> tok_doctext
Mark Sleef5377b32006-10-10 01:42:59 +000060
61/**
Mark Slee30152872006-11-28 01:24:07 +000062 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +000063 */
Mark Slee31985722006-05-24 21:45:31 +000064%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +000065%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +000066
Mark Sleef5377b32006-10-10 01:42:59 +000067/**
Mark Sleef0712dc2006-10-25 19:03:57 +000068 * Header keywoards
Mark Sleef5377b32006-10-10 01:42:59 +000069 */
Mark Sleef0712dc2006-10-25 19:03:57 +000070%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +000071%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000072%token tok_cpp_namespace
73%token tok_cpp_include
74%token tok_cpp_type
Mark Sleee888b372007-01-12 01:06:24 +000075%token tok_php_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000076%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +000077%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +000078%token tok_xsd_optional
Mark Slee7df0e2a2007-02-06 21:03:18 +000079%token tok_xsd_nillable
Mark Slee0d9199e2007-01-31 02:08:30 +000080%token tok_xsd_namespace
Mark Slee21135c32007-02-05 21:52:08 +000081%token tok_xsd_attrs
Mark Slee58dfb4f2007-07-06 02:45:25 +000082%token tok_ruby_namespace
Mark Slee9cb7c612006-09-01 22:17:45 +000083
Mark Sleef5377b32006-10-10 01:42:59 +000084/**
85 * Base datatype keywords
86 */
87%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +000088%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +000089%token tok_byte
90%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +000091%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +000092%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +000093%token tok_senum
Mark Slee9cb7c612006-09-01 22:17:45 +000094%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +000095%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +000096%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +000097%token tok_double
Mark Slee31985722006-05-24 21:45:31 +000098
Mark Sleef5377b32006-10-10 01:42:59 +000099/**
100 * Complex type keywords
101 */
Mark Slee31985722006-05-24 21:45:31 +0000102%token tok_map
103%token tok_list
104%token tok_set
105
Mark Sleef5377b32006-10-10 01:42:59 +0000106/**
107 * Function modifiers
108 */
Mark Slee31985722006-05-24 21:45:31 +0000109%token tok_async
110
Mark Sleef5377b32006-10-10 01:42:59 +0000111/**
112 * Thrift language keywords
113 */
Mark Slee31985722006-05-24 21:45:31 +0000114%token tok_typedef
115%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000116%token tok_xception
117%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000118%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000119%token tok_service
120%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000121%token tok_const
Mark Slee31985722006-05-24 21:45:31 +0000122
Mark Sleef5377b32006-10-10 01:42:59 +0000123/**
124 * Grammar nodes
125 */
126
Mark Slee31985722006-05-24 21:45:31 +0000127%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000128%type<ttype> ContainerType
129%type<ttype> MapType
130%type<ttype> SetType
131%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000132
Mark Sleef0712dc2006-10-25 19:03:57 +0000133%type<ttype> TypeDefinition
134
Mark Slee31985722006-05-24 21:45:31 +0000135%type<ttypedef> Typedef
136%type<ttype> DefinitionType
137
138%type<tfield> Field
Mark Slee7ff32452007-02-01 05:26:18 +0000139%type<iconst> FieldIdentifier
Mark Slee31985722006-05-24 21:45:31 +0000140%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000141%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000142%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000143
144%type<tenum> Enum
145%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000146%type<tenumv> EnumDef
147
Mark Slee6a47fed2007-02-07 02:40:59 +0000148%type<ttypedef> Senum
149%type<tbase> SenumDefList
150%type<id> SenumDef
151
Mark Slee30152872006-11-28 01:24:07 +0000152%type<tconst> Const
153%type<tconstv> ConstValue
154%type<tconstv> ConstList
155%type<tconstv> ConstListContents
156%type<tconstv> ConstMap
157%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000158
159%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000160%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000161%type<tservice> Service
162
163%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000164%type<ttype> FunctionType
165%type<tservice> FunctionList
166
Mark Slee36bfa2e2007-01-19 20:09:51 +0000167%type<tstruct> Throws
168%type<tservice> Extends
169%type<tbool> Async
170%type<tbool> XsdAll
171%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000172%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000173%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000174%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000175
ccheeverf53b5cf2007-02-05 20:33:11 +0000176%type<tdoc> DocTextOptional
177
Mark Slee31985722006-05-24 21:45:31 +0000178%%
179
Mark Sleef5377b32006-10-10 01:42:59 +0000180/**
181 * Thrift Grammar Implementation.
182 *
183 * For the most part this source file works its way top down from what you
184 * might expect to find in a typical .thrift file, i.e. type definitions and
185 * namespaces up top followed by service definitions using those types.
186 */
Mark Slee31985722006-05-24 21:45:31 +0000187
188Program:
Mark Sleef0712dc2006-10-25 19:03:57 +0000189 HeaderList DefinitionList
190 {
191 pdebug("Program -> Headers DefinitionList");
192 }
193
194HeaderList:
195 HeaderList Header
196 {
197 pdebug("HeaderList -> HeaderList Header");
198 }
199|
200 {
201 pdebug("HeaderList -> ");
202 }
203
204Header:
205 Include
206 {
207 pdebug("Header -> Include");
208 }
209| tok_namespace tok_identifier
210 {
211 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
212 if (g_parse_mode == PROGRAM) {
213 g_program->set_cpp_namespace($2);
214 g_program->set_java_package($2);
215 }
216 }
217| tok_cpp_namespace tok_identifier
218 {
219 pdebug("Header -> tok_cpp_namespace tok_identifier");
220 if (g_parse_mode == PROGRAM) {
221 g_program->set_cpp_namespace($2);
222 }
223 }
224| tok_cpp_include tok_literal
225 {
226 pdebug("Header -> tok_cpp_include tok_literal");
227 if (g_parse_mode == PROGRAM) {
228 g_program->add_cpp_include($2);
229 }
230 }
Mark Sleee888b372007-01-12 01:06:24 +0000231| tok_php_namespace tok_identifier
232 {
233 pdebug("Header -> tok_php_namespace tok_identifier");
234 if (g_parse_mode == PROGRAM) {
235 g_program->set_php_namespace($2);
236 }
237 }
Mark Slee58dfb4f2007-07-06 02:45:25 +0000238| tok_ruby_namespace tok_identifier
239 {
240 pdebug("Header -> tok_ruby_namespace tok_identifier");
241 if (g_parse_mode == PROGRAM) {
242 g_program->set_ruby_namespace($2);
243 }
244 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000245| tok_java_package tok_identifier
246 {
247 pdebug("Header -> tok_java_package tok_identifier");
248 if (g_parse_mode == PROGRAM) {
249 g_program->set_java_package($2);
250 }
251 }
Mark Slee0d9199e2007-01-31 02:08:30 +0000252| tok_xsd_namespace tok_literal
253 {
254 pdebug("Header -> tok_xsd_namespace tok_literal");
255 if (g_parse_mode == PROGRAM) {
256 g_program->set_xsd_namespace($2);
257 }
258 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000259
260Include:
261 tok_include tok_literal
262 {
263 pdebug("Include -> tok_include tok_literal");
264 if (g_parse_mode == INCLUDES) {
265 std::string path = include_file(std::string($2));
266 if (!path.empty()) {
267 g_program->add_include(path);
268 }
269 }
270 }
Mark Slee31985722006-05-24 21:45:31 +0000271
272DefinitionList:
273 DefinitionList Definition
274 {
275 pdebug("DefinitionList -> DefinitionList Definition");
276 }
277|
278 {
279 pdebug("DefinitionList -> ");
280 }
281
282Definition:
Mark Slee30152872006-11-28 01:24:07 +0000283 Const
284 {
285 pdebug("Definition -> Const");
286 if (g_parse_mode == PROGRAM) {
287 g_program->add_const($1);
288 }
289 }
290| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000291 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000292 pdebug("Definition -> TypeDefinition");
293 if (g_parse_mode == PROGRAM) {
294 g_scope->add_type($1->get_name(), $1);
295 if (g_parent_scope != NULL) {
296 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
297 }
298 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000299 }
Mark Slee31985722006-05-24 21:45:31 +0000300| Service
301 {
302 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000303 if (g_parse_mode == PROGRAM) {
304 g_scope->add_service($1->get_name(), $1);
305 if (g_parent_scope != NULL) {
306 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
307 }
308 g_program->add_service($1);
309 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000310 }
311
Mark Sleef0712dc2006-10-25 19:03:57 +0000312TypeDefinition:
313 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000314 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000315 pdebug("TypeDefinition -> Typedef");
316 if (g_parse_mode == PROGRAM) {
317 g_program->add_typedef($1);
318 }
319 }
320| Enum
321 {
322 pdebug("TypeDefinition -> Enum");
323 if (g_parse_mode == PROGRAM) {
324 g_program->add_enum($1);
325 }
326 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000327| Senum
328 {
329 pdebug("TypeDefinition -> Senum");
330 if (g_parse_mode == PROGRAM) {
331 g_program->add_typedef($1);
332 }
333 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000334| Struct
335 {
336 pdebug("TypeDefinition -> Struct");
337 if (g_parse_mode == PROGRAM) {
338 g_program->add_struct($1);
339 }
340 }
341| Xception
342 {
343 pdebug("TypeDefinition -> Xception");
344 if (g_parse_mode == PROGRAM) {
345 g_program->add_xception($1);
346 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000347 }
Mark Slee31985722006-05-24 21:45:31 +0000348
349Typedef:
ccheeverf53b5cf2007-02-05 20:33:11 +0000350 DocTextOptional tok_typedef DefinitionType tok_identifier
Mark Slee31985722006-05-24 21:45:31 +0000351 {
352 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000353 t_typedef *td = new t_typedef(g_program, $3, $4);
Mark Slee31985722006-05-24 21:45:31 +0000354 $$ = td;
ccheeverf53b5cf2007-02-05 20:33:11 +0000355 if ($1 != NULL) {
356 td->set_doc($1);
357 }
Mark Slee31985722006-05-24 21:45:31 +0000358 }
359
ccheeverf53b5cf2007-02-05 20:33:11 +0000360DocTextOptional:
361 tok_doctext
362 {
363 pdebug("DocTextOptional -> tok_doctext");
364 $$ = $1;
365 }
366|
367 {
368 $$ = NULL;
369 }
370
Mark Slee6a47fed2007-02-07 02:40:59 +0000371CommaOrSemicolonOptional:
372 ','
373 {}
374| ';'
375 {}
376|
377 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000378
Mark Slee31985722006-05-24 21:45:31 +0000379Enum:
ccheeverf53b5cf2007-02-05 20:33:11 +0000380 DocTextOptional tok_enum tok_identifier '{' EnumDefList '}'
Mark Slee31985722006-05-24 21:45:31 +0000381 {
382 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
ccheeverf53b5cf2007-02-05 20:33:11 +0000383 $$ = $5;
384 $$->set_name($3);
385 if ($1 != NULL) {
386 $$->set_doc($1);
387 }
Mark Slee31985722006-05-24 21:45:31 +0000388 }
389
390EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000391 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000392 {
393 pdebug("EnumDefList -> EnumDefList EnumDef");
394 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000395 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000396 }
397|
398 {
399 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000400 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000401 }
402
403EnumDef:
ccheeverf53b5cf2007-02-05 20:33:11 +0000404 DocTextOptional tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000405 {
Mark Slee30152872006-11-28 01:24:07 +0000406 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000407 if ($4 < 0) {
408 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000409 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000410 $$ = new t_enum_value($2, $4);
411 if ($1 != NULL) {
412 $$->set_doc($1);
413 }
Mark Sleed0767c52007-07-27 22:14:41 +0000414 if (g_parse_mode == PROGRAM) {
415 g_scope->add_constant($2, new t_const(g_type_i32, $2, new t_const_value($4)));
416 if (g_parent_scope != NULL) {
417 g_parent_scope->add_constant(g_parent_prefix + $2, new t_const(g_type_i32, $2, new t_const_value($4)));
418 }
419 }
Mark Slee31985722006-05-24 21:45:31 +0000420 }
421|
ccheeverf53b5cf2007-02-05 20:33:11 +0000422 DocTextOptional tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000423 {
Mark Slee30152872006-11-28 01:24:07 +0000424 pdebug("EnumDef -> tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000425 $$ = new t_enum_value($2);
426 if ($1 != NULL) {
427 $$->set_doc($1);
428 }
Mark Slee30152872006-11-28 01:24:07 +0000429 }
430
Mark Slee6a47fed2007-02-07 02:40:59 +0000431Senum:
432 DocTextOptional tok_senum tok_identifier '{' SenumDefList '}'
433 {
434 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
435 $$ = new t_typedef(g_program, $5, $3);
436 if ($1 != NULL) {
437 $$->set_doc($1);
438 }
439 }
440
441SenumDefList:
442 SenumDefList SenumDef
443 {
444 pdebug("SenumDefList -> SenumDefList SenumDef");
445 $$ = $1;
446 $$->add_string_enum_val($2);
447 }
448|
449 {
450 pdebug("SenumDefList -> ");
451 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
452 $$->set_string_enum(true);
453 }
454
455SenumDef:
456 tok_literal CommaOrSemicolonOptional
457 {
458 pdebug("SenumDef -> tok_literal");
459 $$ = $1;
460 }
461
Mark Slee30152872006-11-28 01:24:07 +0000462Const:
463 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
464 {
465 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000466 if (g_parse_mode == PROGRAM) {
467 $$ = new t_const($2, $3, $5);
468 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000469
470 g_scope->add_constant($3, $$);
471 if (g_parent_scope != NULL) {
472 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
473 }
474
Mark Sleeaa7671d2006-11-29 03:19:31 +0000475 } else {
476 $$ = NULL;
477 }
Mark Slee30152872006-11-28 01:24:07 +0000478 }
479
480ConstValue:
481 tok_int_constant
482 {
483 pdebug("ConstValue => tok_int_constant");
484 $$ = new t_const_value();
485 $$->set_integer($1);
486 }
487| tok_dub_constant
488 {
489 pdebug("ConstValue => tok_dub_constant");
490 $$ = new t_const_value();
491 $$->set_double($1);
492 }
493| tok_literal
494 {
495 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000496 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000497 }
Mark Slee67fc6342006-11-29 03:37:04 +0000498| tok_identifier
499 {
500 pdebug("ConstValue => tok_identifier");
Mark Sleed0767c52007-07-27 22:14:41 +0000501 t_const* constant = g_scope->get_constant($1);
502 if (constant != NULL) {
503 $$ = constant->get_value();
504 } else {
505 if (g_parse_mode == PROGRAM) {
506 pwarning(1, "Constant strings should be quoted: %s\n", $1);
507 }
508 $$ = new t_const_value($1);
509 }
Mark Slee67fc6342006-11-29 03:37:04 +0000510 }
Mark Slee30152872006-11-28 01:24:07 +0000511| ConstList
512 {
513 pdebug("ConstValue => ConstList");
514 $$ = $1;
515 }
516| ConstMap
517 {
518 pdebug("ConstValue => ConstMap");
519 $$ = $1;
520 }
521
522ConstList:
523 '[' ConstListContents ']'
524 {
525 pdebug("ConstList => [ ConstListContents ]");
526 $$ = $2;
527 }
528
529ConstListContents:
530 ConstListContents ConstValue CommaOrSemicolonOptional
531 {
532 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
533 $$ = $1;
534 $$->add_list($2);
535 }
536|
537 {
538 pdebug("ConstListContents =>");
539 $$ = new t_const_value();
540 $$->set_list();
541 }
542
543ConstMap:
544 '{' ConstMapContents '}'
545 {
546 pdebug("ConstMap => { ConstMapContents }");
547 $$ = $2;
548 }
549
550ConstMapContents:
551 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
552 {
553 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
554 $$ = $1;
555 $$->add_map($2, $4);
556 }
557|
558 {
559 pdebug("ConstMapContents =>");
560 $$ = new t_const_value();
561 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000562 }
563
564Struct:
ccheeverf53b5cf2007-02-05 20:33:11 +0000565 DocTextOptional tok_struct tok_identifier XsdAll '{' FieldList '}'
Mark Slee31985722006-05-24 21:45:31 +0000566 {
567 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
ccheeverf53b5cf2007-02-05 20:33:11 +0000568 $6->set_name($3);
569 if ($1 != NULL) {
570 $6->set_doc($1);
571 }
572 $6->set_xsd_all($4);
573 $$ = $6;
Mark Slee9cb7c612006-09-01 22:17:45 +0000574 y_field_val = -1;
575 }
576
Mark Slee36bfa2e2007-01-19 20:09:51 +0000577XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000578 tok_xsd_all
579 {
580 $$ = true;
581 }
582|
583 {
584 $$ = false;
585 }
586
Mark Slee36bfa2e2007-01-19 20:09:51 +0000587XsdOptional:
588 tok_xsd_optional
589 {
590 $$ = true;
591 }
592|
593 {
594 $$ = false;
595 }
596
Mark Slee7df0e2a2007-02-06 21:03:18 +0000597XsdNillable:
598 tok_xsd_nillable
599 {
600 $$ = true;
601 }
602|
603 {
604 $$ = false;
605 }
606
Mark Slee21135c32007-02-05 21:52:08 +0000607XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000608 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000609 {
Mark Slee748d83f2007-02-07 01:20:08 +0000610 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000611 }
612|
613 {
614 $$ = NULL;
615 }
616
Mark Slee9cb7c612006-09-01 22:17:45 +0000617Xception:
618 tok_xception tok_identifier '{' FieldList '}'
619 {
620 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
621 $4->set_name($2);
622 $4->set_xception(true);
623 $$ = $4;
624 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000625 }
626
627Service:
ccheeverf53b5cf2007-02-05 20:33:11 +0000628 DocTextOptional tok_service tok_identifier Extends '{' FunctionList '}'
Mark Slee31985722006-05-24 21:45:31 +0000629 {
630 pdebug("Service -> tok_service tok_identifier { FunctionList }");
ccheeverf53b5cf2007-02-05 20:33:11 +0000631 $$ = $6;
632 $$->set_name($3);
633 $$->set_extends($4);
634 if ($1 != NULL) {
635 $$->set_doc($1);
636 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000637 }
638
Mark Slee36bfa2e2007-01-19 20:09:51 +0000639Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000640 tok_extends tok_identifier
641 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000642 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000643 $$ = NULL;
644 if (g_parse_mode == PROGRAM) {
645 $$ = g_scope->get_service($2);
646 if ($$ == NULL) {
647 yyerror("Service \"%s\" has not been defined.", $2);
648 exit(1);
649 }
650 }
651 }
652|
653 {
654 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000655 }
656
657FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000658 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000659 {
660 pdebug("FunctionList -> FunctionList Function");
661 $$ = $1;
662 $1->add_function($2);
663 }
664|
665 {
666 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000667 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000668 }
669
670Function:
ccheeverf53b5cf2007-02-05 20:33:11 +0000671 DocTextOptional Async FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000672 {
ccheeverf53b5cf2007-02-05 20:33:11 +0000673 $6->set_name(std::string($4) + "_args");
674 $$ = new t_function($3, $4, $6, $8, $2);
675 if ($1 != NULL) {
676 $$->set_doc($1);
677 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000678 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000679 }
680
Mark Slee36bfa2e2007-01-19 20:09:51 +0000681Async:
Mark Slee52f643d2006-08-09 00:03:43 +0000682 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000683 {
Mark Slee52f643d2006-08-09 00:03:43 +0000684 $$ = true;
685 }
686|
687 {
688 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000689 }
690
Mark Slee36bfa2e2007-01-19 20:09:51 +0000691Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000692 tok_throws '(' FieldList ')'
693 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000694 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000695 $$ = $3;
696 }
697|
698 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000699 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000700 }
701
Mark Slee31985722006-05-24 21:45:31 +0000702FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000703 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000704 {
705 pdebug("FieldList -> FieldList , Field");
706 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000707 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000708 }
709|
710 {
711 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000712 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000713 }
714
715Field:
Mark Slee7df0e2a2007-02-06 21:03:18 +0000716 DocTextOptional FieldIdentifier FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000717 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000718 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000719 if ($2 < 0) {
Mark Slee21135c32007-02-05 21:52:08 +0000720 pwarning(2, "No field key specified for %s, resulting protocol may have conflicts or not be backwards compatible!\n", $4);
Mark Slee31985722006-05-24 21:45:31 +0000721 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000722 $$ = new t_field($3, $4, $2);
723 if ($5 != NULL) {
724 validate_field_value($$, $5);
725 $$->set_value($5);
Mark Slee7ff32452007-02-01 05:26:18 +0000726 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000727 $$->set_xsd_optional($6);
Mark Slee7df0e2a2007-02-06 21:03:18 +0000728 $$->set_xsd_nillable($7);
ccheeverf53b5cf2007-02-05 20:33:11 +0000729 if ($1 != NULL) {
730 $$->set_doc($1);
731 }
Mark Slee7df0e2a2007-02-06 21:03:18 +0000732 if ($8 != NULL) {
Mark Slee748d83f2007-02-07 01:20:08 +0000733 $$->set_xsd_attrs($8);
Mark Slee21135c32007-02-05 21:52:08 +0000734 }
Mark Slee31985722006-05-24 21:45:31 +0000735 }
Mark Slee7ff32452007-02-01 05:26:18 +0000736
737FieldIdentifier:
738 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000739 {
Mark Slee7ff32452007-02-01 05:26:18 +0000740 if ($1 <= 0) {
741 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n", $1);
742 $1 = y_field_val--;
Mark Sleef0712dc2006-10-25 19:03:57 +0000743 }
Mark Slee7ff32452007-02-01 05:26:18 +0000744 $$ = $1;
745 }
746|
747 {
748 $$ = y_field_val--;
749 }
750
751FieldValue:
752 '=' ConstValue
753 {
754 if (g_parse_mode == PROGRAM) {
755 $$ = $2;
756 } else {
757 $$ = NULL;
758 }
759 }
760|
761 {
762 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000763 }
Mark Slee31985722006-05-24 21:45:31 +0000764
765DefinitionType:
766 BaseType
767 {
768 pdebug("DefinitionType -> BaseType");
769 $$ = $1;
770 }
Mark Sleee8540632006-05-30 09:24:40 +0000771| ContainerType
772 {
773 pdebug("DefinitionType -> ContainerType");
774 $$ = $1;
775 }
Mark Slee31985722006-05-24 21:45:31 +0000776
777FunctionType:
778 FieldType
779 {
Mark Sleee8540632006-05-30 09:24:40 +0000780 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000781 $$ = $1;
782 }
783| tok_void
784 {
Mark Sleee8540632006-05-30 09:24:40 +0000785 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000786 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000787 }
788
789FieldType:
790 tok_identifier
791 {
Mark Sleee8540632006-05-30 09:24:40 +0000792 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000793 if (g_parse_mode == INCLUDES) {
794 // Ignore identifiers in include mode
795 $$ = NULL;
796 } else {
797 // Lookup the identifier in the current scope
798 $$ = g_scope->get_type($1);
799 if ($$ == NULL) {
800 yyerror("Type \"%s\" has not been defined.", $1);
801 exit(1);
802 }
Mark Sleee8540632006-05-30 09:24:40 +0000803 }
Mark Slee31985722006-05-24 21:45:31 +0000804 }
805| BaseType
806 {
Mark Sleee8540632006-05-30 09:24:40 +0000807 pdebug("FieldType -> BaseType");
808 $$ = $1;
809 }
810| ContainerType
811 {
812 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000813 $$ = $1;
814 }
815
816BaseType:
817 tok_string
818 {
819 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000820 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000821 }
Mark Slee8d725a22007-04-13 01:57:12 +0000822| tok_binary
823 {
824 pdebug("BaseType -> tok_binary");
825 $$ = g_type_binary;
826 }
Mark Sleeb6200d82007-01-19 19:14:36 +0000827| tok_slist
828 {
829 pdebug("BaseType -> tok_slist");
830 $$ = g_type_slist;
831 }
Mark Slee78f58e22006-09-02 04:17:07 +0000832| tok_bool
833 {
834 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000835 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000836 }
Mark Slee31985722006-05-24 21:45:31 +0000837| tok_byte
838 {
839 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000840 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000841 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000842| tok_i16
843 {
844 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000845 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000846 }
Mark Slee31985722006-05-24 21:45:31 +0000847| tok_i32
848 {
849 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000850 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000851 }
Mark Slee31985722006-05-24 21:45:31 +0000852| tok_i64
853 {
854 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000855 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000856 }
Mark Sleec98d0502006-09-06 02:42:25 +0000857| tok_double
858 {
859 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000860 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000861 }
Mark Slee31985722006-05-24 21:45:31 +0000862
Mark Sleee8540632006-05-30 09:24:40 +0000863ContainerType:
864 MapType
865 {
866 pdebug("ContainerType -> MapType");
867 $$ = $1;
868 }
869| SetType
870 {
871 pdebug("ContainerType -> SetType");
872 $$ = $1;
873 }
874| ListType
875 {
876 pdebug("ContainerType -> ListType");
877 $$ = $1;
878 }
879
880MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000881 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000882 {
883 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000884 $$ = new t_map($4, $6);
885 if ($2 != NULL) {
886 ((t_container*)$$)->set_cpp_name(std::string($2));
887 }
Mark Sleee8540632006-05-30 09:24:40 +0000888 }
889
890SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000891 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000892 {
893 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000894 $$ = new t_set($4);
895 if ($2 != NULL) {
896 ((t_container*)$$)->set_cpp_name(std::string($2));
897 }
Mark Sleee8540632006-05-30 09:24:40 +0000898 }
899
900ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000901 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +0000902 {
903 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +0000904 $$ = new t_list($3);
905 if ($5 != NULL) {
906 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +0000907 }
908 }
909
Mark Slee36bfa2e2007-01-19 20:09:51 +0000910CppType:
Mark Sleeafc76542007-02-09 21:55:44 +0000911 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +0000912 {
Mark Sleeafc76542007-02-09 21:55:44 +0000913 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +0000914 }
915|
916 {
917 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +0000918 }
919
Mark Slee31985722006-05-24 21:45:31 +0000920%%