blob: 9c38996242dff640d530c640a3e2e32647101387 [file] [log] [blame]
Mark Slee31985722006-05-24 21:45:31 +00001%{
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 Sleef0712dc2006-10-25 19:03:57 +000015#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000016
Mark Sleef5377b32006-10-10 01:42:59 +000017/**
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 Slee9cb7c612006-09-01 22:17:45 +000022int y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +000023
24%}
25
Mark Sleef5377b32006-10-10 01:42:59 +000026/**
27 * This structure is used by the parser to hold the data types associated with
28 * various parse nodes.
29 */
Mark Slee31985722006-05-24 21:45:31 +000030%union {
Mark Slee30152872006-11-28 01:24:07 +000031 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;
ccheeverf53b5cf2007-02-05 20:33:11 +000045 char* tdoc;
Mark Slee31985722006-05-24 21:45:31 +000046}
47
Mark Sleef5377b32006-10-10 01:42:59 +000048/**
49 * Strings identifier
50 */
Mark Slee31985722006-05-24 21:45:31 +000051%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +000052%token<id> tok_literal
ccheeverf53b5cf2007-02-05 20:33:11 +000053%token<tdoc> tok_doctext
Mark Sleef5377b32006-10-10 01:42:59 +000054
55/**
Mark Slee30152872006-11-28 01:24:07 +000056 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +000057 */
Mark Slee31985722006-05-24 21:45:31 +000058%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +000059%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +000060
Mark Sleef5377b32006-10-10 01:42:59 +000061/**
Mark Sleef0712dc2006-10-25 19:03:57 +000062 * Header keywoards
Mark Sleef5377b32006-10-10 01:42:59 +000063 */
Mark Sleef0712dc2006-10-25 19:03:57 +000064%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +000065%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000066%token tok_cpp_namespace
67%token tok_cpp_include
68%token tok_cpp_type
Mark Sleee888b372007-01-12 01:06:24 +000069%token tok_php_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000070%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +000071%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +000072%token tok_xsd_optional
Mark Slee0d9199e2007-01-31 02:08:30 +000073%token tok_xsd_namespace
Mark Slee9cb7c612006-09-01 22:17:45 +000074
Mark Sleef5377b32006-10-10 01:42:59 +000075/**
76 * Base datatype keywords
77 */
78%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +000079%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +000080%token tok_byte
81%token tok_string
Mark Sleeb6200d82007-01-19 19:14:36 +000082%token tok_slist
Mark Slee9cb7c612006-09-01 22:17:45 +000083%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +000084%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +000085%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +000086%token tok_double
Mark Slee31985722006-05-24 21:45:31 +000087
Mark Sleef5377b32006-10-10 01:42:59 +000088/**
89 * Complex type keywords
90 */
Mark Slee31985722006-05-24 21:45:31 +000091%token tok_map
92%token tok_list
93%token tok_set
94
Mark Sleef5377b32006-10-10 01:42:59 +000095/**
96 * Function modifiers
97 */
Mark Slee31985722006-05-24 21:45:31 +000098%token tok_async
99
Mark Sleef5377b32006-10-10 01:42:59 +0000100/**
101 * Thrift language keywords
102 */
Mark Slee31985722006-05-24 21:45:31 +0000103%token tok_typedef
104%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000105%token tok_xception
106%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000107%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000108%token tok_service
109%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000110%token tok_const
Mark Slee31985722006-05-24 21:45:31 +0000111
Mark Sleef5377b32006-10-10 01:42:59 +0000112/**
113 * Grammar nodes
114 */
115
Mark Slee31985722006-05-24 21:45:31 +0000116%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000117%type<ttype> ContainerType
118%type<ttype> MapType
119%type<ttype> SetType
120%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000121
Mark Sleef0712dc2006-10-25 19:03:57 +0000122%type<ttype> TypeDefinition
123
Mark Slee31985722006-05-24 21:45:31 +0000124%type<ttypedef> Typedef
125%type<ttype> DefinitionType
126
127%type<tfield> Field
Mark Slee7ff32452007-02-01 05:26:18 +0000128%type<iconst> FieldIdentifier
Mark Slee31985722006-05-24 21:45:31 +0000129%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000130%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000131%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000132
133%type<tenum> Enum
134%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000135%type<tenumv> EnumDef
136
137%type<tconst> Const
138%type<tconstv> ConstValue
139%type<tconstv> ConstList
140%type<tconstv> ConstListContents
141%type<tconstv> ConstMap
142%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000143
144%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000145%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000146%type<tservice> Service
147
148%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000149%type<ttype> FunctionType
150%type<tservice> FunctionList
151
Mark Slee36bfa2e2007-01-19 20:09:51 +0000152%type<tstruct> Throws
153%type<tservice> Extends
154%type<tbool> Async
155%type<tbool> XsdAll
156%type<tbool> XsdOptional
157%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000158
ccheeverf53b5cf2007-02-05 20:33:11 +0000159%type<tdoc> DocTextOptional
160
Mark Slee31985722006-05-24 21:45:31 +0000161%%
162
Mark Sleef5377b32006-10-10 01:42:59 +0000163/**
164 * Thrift Grammar Implementation.
165 *
166 * For the most part this source file works its way top down from what you
167 * might expect to find in a typical .thrift file, i.e. type definitions and
168 * namespaces up top followed by service definitions using those types.
169 */
Mark Slee31985722006-05-24 21:45:31 +0000170
171Program:
Mark Sleef0712dc2006-10-25 19:03:57 +0000172 HeaderList DefinitionList
173 {
174 pdebug("Program -> Headers DefinitionList");
175 }
176
177HeaderList:
178 HeaderList Header
179 {
180 pdebug("HeaderList -> HeaderList Header");
181 }
182|
183 {
184 pdebug("HeaderList -> ");
185 }
186
187Header:
188 Include
189 {
190 pdebug("Header -> Include");
191 }
192| tok_namespace tok_identifier
193 {
194 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
195 if (g_parse_mode == PROGRAM) {
196 g_program->set_cpp_namespace($2);
197 g_program->set_java_package($2);
198 }
199 }
200| tok_cpp_namespace tok_identifier
201 {
202 pdebug("Header -> tok_cpp_namespace tok_identifier");
203 if (g_parse_mode == PROGRAM) {
204 g_program->set_cpp_namespace($2);
205 }
206 }
207| tok_cpp_include tok_literal
208 {
209 pdebug("Header -> tok_cpp_include tok_literal");
210 if (g_parse_mode == PROGRAM) {
211 g_program->add_cpp_include($2);
212 }
213 }
Mark Sleee888b372007-01-12 01:06:24 +0000214| tok_php_namespace tok_identifier
215 {
216 pdebug("Header -> tok_php_namespace tok_identifier");
217 if (g_parse_mode == PROGRAM) {
218 g_program->set_php_namespace($2);
219 }
220 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000221| tok_java_package tok_identifier
222 {
223 pdebug("Header -> tok_java_package tok_identifier");
224 if (g_parse_mode == PROGRAM) {
225 g_program->set_java_package($2);
226 }
227 }
Mark Slee0d9199e2007-01-31 02:08:30 +0000228| tok_xsd_namespace tok_literal
229 {
230 pdebug("Header -> tok_xsd_namespace tok_literal");
231 if (g_parse_mode == PROGRAM) {
232 g_program->set_xsd_namespace($2);
233 }
234 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000235
236Include:
237 tok_include tok_literal
238 {
239 pdebug("Include -> tok_include tok_literal");
240 if (g_parse_mode == INCLUDES) {
241 std::string path = include_file(std::string($2));
242 if (!path.empty()) {
243 g_program->add_include(path);
244 }
245 }
246 }
Mark Slee31985722006-05-24 21:45:31 +0000247
248DefinitionList:
249 DefinitionList Definition
250 {
251 pdebug("DefinitionList -> DefinitionList Definition");
252 }
253|
254 {
255 pdebug("DefinitionList -> ");
256 }
257
258Definition:
Mark Slee30152872006-11-28 01:24:07 +0000259 Const
260 {
261 pdebug("Definition -> Const");
262 if (g_parse_mode == PROGRAM) {
263 g_program->add_const($1);
264 }
265 }
266| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000267 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000268 pdebug("Definition -> TypeDefinition");
269 if (g_parse_mode == PROGRAM) {
270 g_scope->add_type($1->get_name(), $1);
271 if (g_parent_scope != NULL) {
272 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
273 }
274 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000275 }
Mark Slee31985722006-05-24 21:45:31 +0000276| Service
277 {
278 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000279 if (g_parse_mode == PROGRAM) {
280 g_scope->add_service($1->get_name(), $1);
281 if (g_parent_scope != NULL) {
282 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
283 }
284 g_program->add_service($1);
285 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000286 }
287
Mark Sleef0712dc2006-10-25 19:03:57 +0000288TypeDefinition:
289 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000290 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000291 pdebug("TypeDefinition -> Typedef");
292 if (g_parse_mode == PROGRAM) {
293 g_program->add_typedef($1);
294 }
295 }
296| Enum
297 {
298 pdebug("TypeDefinition -> Enum");
299 if (g_parse_mode == PROGRAM) {
300 g_program->add_enum($1);
301 }
302 }
303| Struct
304 {
305 pdebug("TypeDefinition -> Struct");
306 if (g_parse_mode == PROGRAM) {
307 g_program->add_struct($1);
308 }
309 }
310| Xception
311 {
312 pdebug("TypeDefinition -> Xception");
313 if (g_parse_mode == PROGRAM) {
314 g_program->add_xception($1);
315 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000316 }
Mark Slee31985722006-05-24 21:45:31 +0000317
318Typedef:
ccheeverf53b5cf2007-02-05 20:33:11 +0000319 DocTextOptional tok_typedef DefinitionType tok_identifier
Mark Slee31985722006-05-24 21:45:31 +0000320 {
321 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000322 t_typedef *td = new t_typedef(g_program, $3, $4);
Mark Slee31985722006-05-24 21:45:31 +0000323 $$ = td;
ccheeverf53b5cf2007-02-05 20:33:11 +0000324 if ($1 != NULL) {
325 td->set_doc($1);
326 }
Mark Slee31985722006-05-24 21:45:31 +0000327 }
328
ccheeverf53b5cf2007-02-05 20:33:11 +0000329DocTextOptional:
330 tok_doctext
331 {
332 pdebug("DocTextOptional -> tok_doctext");
333 $$ = $1;
334 }
335|
336 {
337 $$ = NULL;
338 }
339
340
Mark Slee31985722006-05-24 21:45:31 +0000341Enum:
ccheeverf53b5cf2007-02-05 20:33:11 +0000342 DocTextOptional tok_enum tok_identifier '{' EnumDefList '}'
Mark Slee31985722006-05-24 21:45:31 +0000343 {
344 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
ccheeverf53b5cf2007-02-05 20:33:11 +0000345 $$ = $5;
346 $$->set_name($3);
347 if ($1 != NULL) {
348 $$->set_doc($1);
349 }
Mark Slee31985722006-05-24 21:45:31 +0000350 }
351
Mark Slee207cb462006-11-02 18:43:12 +0000352CommaOrSemicolonOptional:
353 ','
354 {}
355| ';'
356 {}
357|
358 {}
359
Mark Slee31985722006-05-24 21:45:31 +0000360EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000361 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000362 {
363 pdebug("EnumDefList -> EnumDefList EnumDef");
364 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000365 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000366 }
367|
368 {
369 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000370 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000371 }
372
373EnumDef:
ccheeverf53b5cf2007-02-05 20:33:11 +0000374 DocTextOptional tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000375 {
Mark Slee30152872006-11-28 01:24:07 +0000376 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000377 if ($4 < 0) {
378 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000379 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000380 $$ = new t_enum_value($2, $4);
381 if ($1 != NULL) {
382 $$->set_doc($1);
383 }
Mark Slee31985722006-05-24 21:45:31 +0000384 }
385|
ccheeverf53b5cf2007-02-05 20:33:11 +0000386 DocTextOptional tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000387 {
Mark Slee30152872006-11-28 01:24:07 +0000388 pdebug("EnumDef -> tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000389 $$ = new t_enum_value($2);
390 if ($1 != NULL) {
391 $$->set_doc($1);
392 }
Mark Slee30152872006-11-28 01:24:07 +0000393 }
394
395Const:
396 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
397 {
398 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000399 if (g_parse_mode == PROGRAM) {
400 $$ = new t_const($2, $3, $5);
401 validate_const_type($$);
402 } else {
403 $$ = NULL;
404 }
Mark Slee30152872006-11-28 01:24:07 +0000405 }
406
407ConstValue:
408 tok_int_constant
409 {
410 pdebug("ConstValue => tok_int_constant");
411 $$ = new t_const_value();
412 $$->set_integer($1);
413 }
414| tok_dub_constant
415 {
416 pdebug("ConstValue => tok_dub_constant");
417 $$ = new t_const_value();
418 $$->set_double($1);
419 }
420| tok_literal
421 {
422 pdebug("ConstValue => tok_literal");
423 $$ = new t_const_value();
424 $$->set_string($1);
425 }
Mark Slee67fc6342006-11-29 03:37:04 +0000426| tok_identifier
427 {
428 pdebug("ConstValue => tok_identifier");
429 $$ = new t_const_value();
430 $$->set_string($1);
431 }
Mark Slee30152872006-11-28 01:24:07 +0000432| ConstList
433 {
434 pdebug("ConstValue => ConstList");
435 $$ = $1;
436 }
437| ConstMap
438 {
439 pdebug("ConstValue => ConstMap");
440 $$ = $1;
441 }
442
443ConstList:
444 '[' ConstListContents ']'
445 {
446 pdebug("ConstList => [ ConstListContents ]");
447 $$ = $2;
448 }
449
450ConstListContents:
451 ConstListContents ConstValue CommaOrSemicolonOptional
452 {
453 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
454 $$ = $1;
455 $$->add_list($2);
456 }
457|
458 {
459 pdebug("ConstListContents =>");
460 $$ = new t_const_value();
461 $$->set_list();
462 }
463
464ConstMap:
465 '{' ConstMapContents '}'
466 {
467 pdebug("ConstMap => { ConstMapContents }");
468 $$ = $2;
469 }
470
471ConstMapContents:
472 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
473 {
474 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
475 $$ = $1;
476 $$->add_map($2, $4);
477 }
478|
479 {
480 pdebug("ConstMapContents =>");
481 $$ = new t_const_value();
482 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000483 }
484
485Struct:
ccheeverf53b5cf2007-02-05 20:33:11 +0000486 DocTextOptional tok_struct tok_identifier XsdAll '{' FieldList '}'
Mark Slee31985722006-05-24 21:45:31 +0000487 {
488 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
ccheeverf53b5cf2007-02-05 20:33:11 +0000489 $6->set_name($3);
490 if ($1 != NULL) {
491 $6->set_doc($1);
492 }
493 $6->set_xsd_all($4);
494 $$ = $6;
Mark Slee9cb7c612006-09-01 22:17:45 +0000495 y_field_val = -1;
496 }
497
Mark Slee36bfa2e2007-01-19 20:09:51 +0000498XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000499 tok_xsd_all
500 {
501 $$ = true;
502 }
503|
504 {
505 $$ = false;
506 }
507
Mark Slee36bfa2e2007-01-19 20:09:51 +0000508XsdOptional:
509 tok_xsd_optional
510 {
511 $$ = true;
512 }
513|
514 {
515 $$ = false;
516 }
517
Mark Slee9cb7c612006-09-01 22:17:45 +0000518Xception:
519 tok_xception tok_identifier '{' FieldList '}'
520 {
521 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
522 $4->set_name($2);
523 $4->set_xception(true);
ccheeverf53b5cf2007-02-05 20:33:11 +0000524/*
525 if ($4 != NULL) {
526 $5->set_doc($4);
527 }
528*/
Mark Slee9cb7c612006-09-01 22:17:45 +0000529 $$ = $4;
530 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000531 }
532
533Service:
ccheeverf53b5cf2007-02-05 20:33:11 +0000534 DocTextOptional tok_service tok_identifier Extends '{' FunctionList '}'
Mark Slee31985722006-05-24 21:45:31 +0000535 {
536 pdebug("Service -> tok_service tok_identifier { FunctionList }");
ccheeverf53b5cf2007-02-05 20:33:11 +0000537 $$ = $6;
538 $$->set_name($3);
539 $$->set_extends($4);
540 if ($1 != NULL) {
541 $$->set_doc($1);
542 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000543 }
544
Mark Slee36bfa2e2007-01-19 20:09:51 +0000545Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000546 tok_extends tok_identifier
547 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000548 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000549 $$ = NULL;
550 if (g_parse_mode == PROGRAM) {
551 $$ = g_scope->get_service($2);
552 if ($$ == NULL) {
553 yyerror("Service \"%s\" has not been defined.", $2);
554 exit(1);
555 }
556 }
557 }
558|
559 {
560 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000561 }
562
563FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000564 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000565 {
566 pdebug("FunctionList -> FunctionList Function");
567 $$ = $1;
568 $1->add_function($2);
569 }
570|
571 {
572 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000573 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000574 }
575
576Function:
ccheeverf53b5cf2007-02-05 20:33:11 +0000577 DocTextOptional Async FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000578 {
ccheeverf53b5cf2007-02-05 20:33:11 +0000579 $6->set_name(std::string($4) + "_args");
580 $$ = new t_function($3, $4, $6, $8, $2);
581 if ($1 != NULL) {
582 $$->set_doc($1);
583 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000584 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000585 }
586
Mark Slee36bfa2e2007-01-19 20:09:51 +0000587Async:
Mark Slee52f643d2006-08-09 00:03:43 +0000588 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000589 {
Mark Slee52f643d2006-08-09 00:03:43 +0000590 $$ = true;
591 }
592|
593 {
594 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000595 }
596
Mark Slee36bfa2e2007-01-19 20:09:51 +0000597Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000598 tok_throws '(' FieldList ')'
599 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000600 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000601 $$ = $3;
602 }
603|
604 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000605 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000606 }
607
Mark Slee31985722006-05-24 21:45:31 +0000608FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000609 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000610 {
611 pdebug("FieldList -> FieldList , Field");
612 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000613 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000614 }
615|
616 {
617 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000618 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000619 }
620
621Field:
ccheeverf53b5cf2007-02-05 20:33:11 +0000622 DocTextOptional FieldIdentifier FieldType tok_identifier FieldValue XsdOptional CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000623 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000624 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000625 if ($2 < 0) {
Mark Slee7ff32452007-02-01 05:26:18 +0000626 pwarning(2, "No field key specified for %s, resulting protocol may have conflicts or not be backwards compatible!\n", $3);
Mark Slee31985722006-05-24 21:45:31 +0000627 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000628 $$ = new t_field($3, $4, $2);
629 if ($5 != NULL) {
630 validate_field_value($$, $5);
631 $$->set_value($5);
Mark Slee7ff32452007-02-01 05:26:18 +0000632 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000633 $$->set_xsd_optional($6);
634 if ($1 != NULL) {
635 $$->set_doc($1);
636 }
Mark Slee31985722006-05-24 21:45:31 +0000637 }
Mark Slee7ff32452007-02-01 05:26:18 +0000638
639FieldIdentifier:
640 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000641 {
Mark Slee7ff32452007-02-01 05:26:18 +0000642 if ($1 <= 0) {
643 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n", $1);
644 $1 = y_field_val--;
Mark Sleef0712dc2006-10-25 19:03:57 +0000645 }
Mark Slee7ff32452007-02-01 05:26:18 +0000646 $$ = $1;
647 }
648|
649 {
650 $$ = y_field_val--;
651 }
652
653FieldValue:
654 '=' ConstValue
655 {
656 if (g_parse_mode == PROGRAM) {
657 $$ = $2;
658 } else {
659 $$ = NULL;
660 }
661 }
662|
663 {
664 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000665 }
Mark Slee31985722006-05-24 21:45:31 +0000666
667DefinitionType:
668 BaseType
669 {
670 pdebug("DefinitionType -> BaseType");
671 $$ = $1;
672 }
Mark Sleee8540632006-05-30 09:24:40 +0000673| ContainerType
674 {
675 pdebug("DefinitionType -> ContainerType");
676 $$ = $1;
677 }
Mark Slee31985722006-05-24 21:45:31 +0000678
679FunctionType:
680 FieldType
681 {
Mark Sleee8540632006-05-30 09:24:40 +0000682 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000683 $$ = $1;
684 }
685| tok_void
686 {
Mark Sleee8540632006-05-30 09:24:40 +0000687 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000688 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000689 }
690
691FieldType:
692 tok_identifier
693 {
Mark Sleee8540632006-05-30 09:24:40 +0000694 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000695 if (g_parse_mode == INCLUDES) {
696 // Ignore identifiers in include mode
697 $$ = NULL;
698 } else {
699 // Lookup the identifier in the current scope
700 $$ = g_scope->get_type($1);
701 if ($$ == NULL) {
702 yyerror("Type \"%s\" has not been defined.", $1);
703 exit(1);
704 }
Mark Sleee8540632006-05-30 09:24:40 +0000705 }
Mark Slee31985722006-05-24 21:45:31 +0000706 }
707| BaseType
708 {
Mark Sleee8540632006-05-30 09:24:40 +0000709 pdebug("FieldType -> BaseType");
710 $$ = $1;
711 }
712| ContainerType
713 {
714 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000715 $$ = $1;
716 }
717
718BaseType:
719 tok_string
720 {
721 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000722 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000723 }
Mark Sleeb6200d82007-01-19 19:14:36 +0000724| tok_slist
725 {
726 pdebug("BaseType -> tok_slist");
727 $$ = g_type_slist;
728 }
Mark Slee78f58e22006-09-02 04:17:07 +0000729| tok_bool
730 {
731 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000732 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000733 }
Mark Slee31985722006-05-24 21:45:31 +0000734| tok_byte
735 {
736 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000737 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000738 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000739| tok_i16
740 {
741 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000742 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000743 }
Mark Slee31985722006-05-24 21:45:31 +0000744| tok_i32
745 {
746 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000747 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000748 }
Mark Slee31985722006-05-24 21:45:31 +0000749| tok_i64
750 {
751 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000752 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000753 }
Mark Sleec98d0502006-09-06 02:42:25 +0000754| tok_double
755 {
756 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000757 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000758 }
Mark Slee31985722006-05-24 21:45:31 +0000759
Mark Sleee8540632006-05-30 09:24:40 +0000760ContainerType:
761 MapType
762 {
763 pdebug("ContainerType -> MapType");
764 $$ = $1;
765 }
766| SetType
767 {
768 pdebug("ContainerType -> SetType");
769 $$ = $1;
770 }
771| ListType
772 {
773 pdebug("ContainerType -> ListType");
774 $$ = $1;
775 }
776
777MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000778 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000779 {
780 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000781 $$ = new t_map($4, $6);
782 if ($2 != NULL) {
783 ((t_container*)$$)->set_cpp_name(std::string($2));
784 }
Mark Sleee8540632006-05-30 09:24:40 +0000785 }
786
787SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000788 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000789 {
790 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000791 $$ = new t_set($4);
792 if ($2 != NULL) {
793 ((t_container*)$$)->set_cpp_name(std::string($2));
794 }
Mark Sleee8540632006-05-30 09:24:40 +0000795 }
796
797ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000798 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +0000799 {
800 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +0000801 $$ = new t_list($3);
802 if ($5 != NULL) {
803 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +0000804 }
805 }
806
Mark Slee36bfa2e2007-01-19 20:09:51 +0000807CppType:
Mark Sleef0712dc2006-10-25 19:03:57 +0000808 '[' tok_cpp_type tok_literal ']'
Mark Slee4f8da1d2006-10-12 02:47:27 +0000809 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000810 $$ = $3;
Mark Slee4f8da1d2006-10-12 02:47:27 +0000811 }
812|
813 {
814 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +0000815 }
816
Mark Slee31985722006-05-24 21:45:31 +0000817%%