blob: fe64e8539e17230cdd8785a15ca80527ffb8a41f [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 Slee31985722006-05-24 21:45:31 +0000414 }
415|
ccheeverf53b5cf2007-02-05 20:33:11 +0000416 DocTextOptional tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000417 {
Mark Slee30152872006-11-28 01:24:07 +0000418 pdebug("EnumDef -> tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000419 $$ = new t_enum_value($2);
420 if ($1 != NULL) {
421 $$->set_doc($1);
422 }
Mark Slee30152872006-11-28 01:24:07 +0000423 }
424
Mark Slee6a47fed2007-02-07 02:40:59 +0000425Senum:
426 DocTextOptional tok_senum tok_identifier '{' SenumDefList '}'
427 {
428 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
429 $$ = new t_typedef(g_program, $5, $3);
430 if ($1 != NULL) {
431 $$->set_doc($1);
432 }
433 }
434
435SenumDefList:
436 SenumDefList SenumDef
437 {
438 pdebug("SenumDefList -> SenumDefList SenumDef");
439 $$ = $1;
440 $$->add_string_enum_val($2);
441 }
442|
443 {
444 pdebug("SenumDefList -> ");
445 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
446 $$->set_string_enum(true);
447 }
448
449SenumDef:
450 tok_literal CommaOrSemicolonOptional
451 {
452 pdebug("SenumDef -> tok_literal");
453 $$ = $1;
454 }
455
Mark Slee30152872006-11-28 01:24:07 +0000456Const:
457 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
458 {
459 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000460 if (g_parse_mode == PROGRAM) {
461 $$ = new t_const($2, $3, $5);
462 validate_const_type($$);
463 } else {
464 $$ = NULL;
465 }
Mark Slee30152872006-11-28 01:24:07 +0000466 }
467
468ConstValue:
469 tok_int_constant
470 {
471 pdebug("ConstValue => tok_int_constant");
472 $$ = new t_const_value();
473 $$->set_integer($1);
474 }
475| tok_dub_constant
476 {
477 pdebug("ConstValue => tok_dub_constant");
478 $$ = new t_const_value();
479 $$->set_double($1);
480 }
481| tok_literal
482 {
483 pdebug("ConstValue => tok_literal");
484 $$ = new t_const_value();
485 $$->set_string($1);
486 }
Mark Slee67fc6342006-11-29 03:37:04 +0000487| tok_identifier
488 {
489 pdebug("ConstValue => tok_identifier");
490 $$ = new t_const_value();
491 $$->set_string($1);
492 }
Mark Slee30152872006-11-28 01:24:07 +0000493| ConstList
494 {
495 pdebug("ConstValue => ConstList");
496 $$ = $1;
497 }
498| ConstMap
499 {
500 pdebug("ConstValue => ConstMap");
501 $$ = $1;
502 }
503
504ConstList:
505 '[' ConstListContents ']'
506 {
507 pdebug("ConstList => [ ConstListContents ]");
508 $$ = $2;
509 }
510
511ConstListContents:
512 ConstListContents ConstValue CommaOrSemicolonOptional
513 {
514 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
515 $$ = $1;
516 $$->add_list($2);
517 }
518|
519 {
520 pdebug("ConstListContents =>");
521 $$ = new t_const_value();
522 $$->set_list();
523 }
524
525ConstMap:
526 '{' ConstMapContents '}'
527 {
528 pdebug("ConstMap => { ConstMapContents }");
529 $$ = $2;
530 }
531
532ConstMapContents:
533 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
534 {
535 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
536 $$ = $1;
537 $$->add_map($2, $4);
538 }
539|
540 {
541 pdebug("ConstMapContents =>");
542 $$ = new t_const_value();
543 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000544 }
545
546Struct:
ccheeverf53b5cf2007-02-05 20:33:11 +0000547 DocTextOptional tok_struct tok_identifier XsdAll '{' FieldList '}'
Mark Slee31985722006-05-24 21:45:31 +0000548 {
549 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
ccheeverf53b5cf2007-02-05 20:33:11 +0000550 $6->set_name($3);
551 if ($1 != NULL) {
552 $6->set_doc($1);
553 }
554 $6->set_xsd_all($4);
555 $$ = $6;
Mark Slee9cb7c612006-09-01 22:17:45 +0000556 y_field_val = -1;
557 }
558
Mark Slee36bfa2e2007-01-19 20:09:51 +0000559XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000560 tok_xsd_all
561 {
562 $$ = true;
563 }
564|
565 {
566 $$ = false;
567 }
568
Mark Slee36bfa2e2007-01-19 20:09:51 +0000569XsdOptional:
570 tok_xsd_optional
571 {
572 $$ = true;
573 }
574|
575 {
576 $$ = false;
577 }
578
Mark Slee7df0e2a2007-02-06 21:03:18 +0000579XsdNillable:
580 tok_xsd_nillable
581 {
582 $$ = true;
583 }
584|
585 {
586 $$ = false;
587 }
588
Mark Slee21135c32007-02-05 21:52:08 +0000589XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000590 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000591 {
Mark Slee748d83f2007-02-07 01:20:08 +0000592 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000593 }
594|
595 {
596 $$ = NULL;
597 }
598
Mark Slee9cb7c612006-09-01 22:17:45 +0000599Xception:
600 tok_xception tok_identifier '{' FieldList '}'
601 {
602 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
603 $4->set_name($2);
604 $4->set_xception(true);
ccheeverf53b5cf2007-02-05 20:33:11 +0000605/*
606 if ($4 != NULL) {
607 $5->set_doc($4);
608 }
609*/
Mark Slee9cb7c612006-09-01 22:17:45 +0000610 $$ = $4;
611 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000612 }
613
614Service:
ccheeverf53b5cf2007-02-05 20:33:11 +0000615 DocTextOptional tok_service tok_identifier Extends '{' FunctionList '}'
Mark Slee31985722006-05-24 21:45:31 +0000616 {
617 pdebug("Service -> tok_service tok_identifier { FunctionList }");
ccheeverf53b5cf2007-02-05 20:33:11 +0000618 $$ = $6;
619 $$->set_name($3);
620 $$->set_extends($4);
621 if ($1 != NULL) {
622 $$->set_doc($1);
623 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000624 }
625
Mark Slee36bfa2e2007-01-19 20:09:51 +0000626Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000627 tok_extends tok_identifier
628 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000629 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000630 $$ = NULL;
631 if (g_parse_mode == PROGRAM) {
632 $$ = g_scope->get_service($2);
633 if ($$ == NULL) {
634 yyerror("Service \"%s\" has not been defined.", $2);
635 exit(1);
636 }
637 }
638 }
639|
640 {
641 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000642 }
643
644FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000645 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000646 {
647 pdebug("FunctionList -> FunctionList Function");
648 $$ = $1;
649 $1->add_function($2);
650 }
651|
652 {
653 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000654 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000655 }
656
657Function:
ccheeverf53b5cf2007-02-05 20:33:11 +0000658 DocTextOptional Async FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000659 {
ccheeverf53b5cf2007-02-05 20:33:11 +0000660 $6->set_name(std::string($4) + "_args");
661 $$ = new t_function($3, $4, $6, $8, $2);
662 if ($1 != NULL) {
663 $$->set_doc($1);
664 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000665 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000666 }
667
Mark Slee36bfa2e2007-01-19 20:09:51 +0000668Async:
Mark Slee52f643d2006-08-09 00:03:43 +0000669 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000670 {
Mark Slee52f643d2006-08-09 00:03:43 +0000671 $$ = true;
672 }
673|
674 {
675 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000676 }
677
Mark Slee36bfa2e2007-01-19 20:09:51 +0000678Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000679 tok_throws '(' FieldList ')'
680 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000681 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000682 $$ = $3;
683 }
684|
685 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000686 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000687 }
688
Mark Slee31985722006-05-24 21:45:31 +0000689FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000690 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000691 {
692 pdebug("FieldList -> FieldList , Field");
693 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000694 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000695 }
696|
697 {
698 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000699 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000700 }
701
702Field:
Mark Slee7df0e2a2007-02-06 21:03:18 +0000703 DocTextOptional FieldIdentifier FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000704 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000705 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000706 if ($2 < 0) {
Mark Slee21135c32007-02-05 21:52:08 +0000707 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 +0000708 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000709 $$ = new t_field($3, $4, $2);
710 if ($5 != NULL) {
711 validate_field_value($$, $5);
712 $$->set_value($5);
Mark Slee7ff32452007-02-01 05:26:18 +0000713 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000714 $$->set_xsd_optional($6);
Mark Slee7df0e2a2007-02-06 21:03:18 +0000715 $$->set_xsd_nillable($7);
ccheeverf53b5cf2007-02-05 20:33:11 +0000716 if ($1 != NULL) {
717 $$->set_doc($1);
718 }
Mark Slee7df0e2a2007-02-06 21:03:18 +0000719 if ($8 != NULL) {
Mark Slee748d83f2007-02-07 01:20:08 +0000720 $$->set_xsd_attrs($8);
Mark Slee21135c32007-02-05 21:52:08 +0000721 }
Mark Slee31985722006-05-24 21:45:31 +0000722 }
Mark Slee7ff32452007-02-01 05:26:18 +0000723
724FieldIdentifier:
725 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000726 {
Mark Slee7ff32452007-02-01 05:26:18 +0000727 if ($1 <= 0) {
728 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n", $1);
729 $1 = y_field_val--;
Mark Sleef0712dc2006-10-25 19:03:57 +0000730 }
Mark Slee7ff32452007-02-01 05:26:18 +0000731 $$ = $1;
732 }
733|
734 {
735 $$ = y_field_val--;
736 }
737
738FieldValue:
739 '=' ConstValue
740 {
741 if (g_parse_mode == PROGRAM) {
742 $$ = $2;
743 } else {
744 $$ = NULL;
745 }
746 }
747|
748 {
749 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000750 }
Mark Slee31985722006-05-24 21:45:31 +0000751
752DefinitionType:
753 BaseType
754 {
755 pdebug("DefinitionType -> BaseType");
756 $$ = $1;
757 }
Mark Sleee8540632006-05-30 09:24:40 +0000758| ContainerType
759 {
760 pdebug("DefinitionType -> ContainerType");
761 $$ = $1;
762 }
Mark Slee31985722006-05-24 21:45:31 +0000763
764FunctionType:
765 FieldType
766 {
Mark Sleee8540632006-05-30 09:24:40 +0000767 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000768 $$ = $1;
769 }
770| tok_void
771 {
Mark Sleee8540632006-05-30 09:24:40 +0000772 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000773 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000774 }
775
776FieldType:
777 tok_identifier
778 {
Mark Sleee8540632006-05-30 09:24:40 +0000779 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000780 if (g_parse_mode == INCLUDES) {
781 // Ignore identifiers in include mode
782 $$ = NULL;
783 } else {
784 // Lookup the identifier in the current scope
785 $$ = g_scope->get_type($1);
786 if ($$ == NULL) {
787 yyerror("Type \"%s\" has not been defined.", $1);
788 exit(1);
789 }
Mark Sleee8540632006-05-30 09:24:40 +0000790 }
Mark Slee31985722006-05-24 21:45:31 +0000791 }
792| BaseType
793 {
Mark Sleee8540632006-05-30 09:24:40 +0000794 pdebug("FieldType -> BaseType");
795 $$ = $1;
796 }
797| ContainerType
798 {
799 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000800 $$ = $1;
801 }
802
803BaseType:
804 tok_string
805 {
806 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000807 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000808 }
Mark Slee8d725a22007-04-13 01:57:12 +0000809| tok_binary
810 {
811 pdebug("BaseType -> tok_binary");
812 $$ = g_type_binary;
813 }
Mark Sleeb6200d82007-01-19 19:14:36 +0000814| tok_slist
815 {
816 pdebug("BaseType -> tok_slist");
817 $$ = g_type_slist;
818 }
Mark Slee78f58e22006-09-02 04:17:07 +0000819| tok_bool
820 {
821 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000822 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000823 }
Mark Slee31985722006-05-24 21:45:31 +0000824| tok_byte
825 {
826 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000827 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000828 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000829| tok_i16
830 {
831 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000832 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000833 }
Mark Slee31985722006-05-24 21:45:31 +0000834| tok_i32
835 {
836 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000837 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000838 }
Mark Slee31985722006-05-24 21:45:31 +0000839| tok_i64
840 {
841 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000842 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000843 }
Mark Sleec98d0502006-09-06 02:42:25 +0000844| tok_double
845 {
846 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000847 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000848 }
Mark Slee31985722006-05-24 21:45:31 +0000849
Mark Sleee8540632006-05-30 09:24:40 +0000850ContainerType:
851 MapType
852 {
853 pdebug("ContainerType -> MapType");
854 $$ = $1;
855 }
856| SetType
857 {
858 pdebug("ContainerType -> SetType");
859 $$ = $1;
860 }
861| ListType
862 {
863 pdebug("ContainerType -> ListType");
864 $$ = $1;
865 }
866
867MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000868 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000869 {
870 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000871 $$ = new t_map($4, $6);
872 if ($2 != NULL) {
873 ((t_container*)$$)->set_cpp_name(std::string($2));
874 }
Mark Sleee8540632006-05-30 09:24:40 +0000875 }
876
877SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000878 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000879 {
880 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000881 $$ = new t_set($4);
882 if ($2 != NULL) {
883 ((t_container*)$$)->set_cpp_name(std::string($2));
884 }
Mark Sleee8540632006-05-30 09:24:40 +0000885 }
886
887ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000888 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +0000889 {
890 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +0000891 $$ = new t_list($3);
892 if ($5 != NULL) {
893 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +0000894 }
895 }
896
Mark Slee36bfa2e2007-01-19 20:09:51 +0000897CppType:
Mark Sleeafc76542007-02-09 21:55:44 +0000898 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +0000899 {
Mark Sleeafc76542007-02-09 21:55:44 +0000900 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +0000901 }
902|
903 {
904 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +0000905 }
906
Mark Slee31985722006-05-24 21:45:31 +0000907%%