blob: 019fb9dda35a922d25d24442d2878ac86a43907b [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;
Mark Slee6a47fed2007-02-07 02:40:59 +000036 t_base_type* tbase;
Mark Slee30152872006-11-28 01:24:07 +000037 t_typedef* ttypedef;
38 t_enum* tenum;
39 t_enum_value* tenumv;
40 t_const* tconst;
41 t_const_value* tconstv;
42 t_struct* tstruct;
43 t_service* tservice;
44 t_function* tfunction;
45 t_field* tfield;
ccheeverf53b5cf2007-02-05 20:33:11 +000046 char* tdoc;
Mark Slee31985722006-05-24 21:45:31 +000047}
48
Mark Sleef5377b32006-10-10 01:42:59 +000049/**
50 * Strings identifier
51 */
Mark Slee31985722006-05-24 21:45:31 +000052%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +000053%token<id> tok_literal
ccheeverf53b5cf2007-02-05 20:33:11 +000054%token<tdoc> tok_doctext
Mark Sleef5377b32006-10-10 01:42:59 +000055
56/**
Mark Slee30152872006-11-28 01:24:07 +000057 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +000058 */
Mark Slee31985722006-05-24 21:45:31 +000059%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +000060%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +000061
Mark Sleef5377b32006-10-10 01:42:59 +000062/**
Mark Sleef0712dc2006-10-25 19:03:57 +000063 * Header keywoards
Mark Sleef5377b32006-10-10 01:42:59 +000064 */
Mark Sleef0712dc2006-10-25 19:03:57 +000065%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +000066%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000067%token tok_cpp_namespace
68%token tok_cpp_include
69%token tok_cpp_type
Mark Sleee888b372007-01-12 01:06:24 +000070%token tok_php_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000071%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +000072%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +000073%token tok_xsd_optional
Mark Slee7df0e2a2007-02-06 21:03:18 +000074%token tok_xsd_nillable
Mark Slee0d9199e2007-01-31 02:08:30 +000075%token tok_xsd_namespace
Mark Slee21135c32007-02-05 21:52:08 +000076%token tok_xsd_attrs
Mark Slee9cb7c612006-09-01 22:17:45 +000077
Mark Sleef5377b32006-10-10 01:42:59 +000078/**
79 * Base datatype keywords
80 */
81%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +000082%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +000083%token tok_byte
84%token tok_string
Mark Sleeb6200d82007-01-19 19:14:36 +000085%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +000086%token tok_senum
Mark Slee9cb7c612006-09-01 22:17:45 +000087%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +000088%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +000089%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +000090%token tok_double
Mark Slee31985722006-05-24 21:45:31 +000091
Mark Sleef5377b32006-10-10 01:42:59 +000092/**
93 * Complex type keywords
94 */
Mark Slee31985722006-05-24 21:45:31 +000095%token tok_map
96%token tok_list
97%token tok_set
98
Mark Sleef5377b32006-10-10 01:42:59 +000099/**
100 * Function modifiers
101 */
Mark Slee31985722006-05-24 21:45:31 +0000102%token tok_async
103
Mark Sleef5377b32006-10-10 01:42:59 +0000104/**
105 * Thrift language keywords
106 */
Mark Slee31985722006-05-24 21:45:31 +0000107%token tok_typedef
108%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000109%token tok_xception
110%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000111%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000112%token tok_service
113%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000114%token tok_const
Mark Slee31985722006-05-24 21:45:31 +0000115
Mark Sleef5377b32006-10-10 01:42:59 +0000116/**
117 * Grammar nodes
118 */
119
Mark Slee31985722006-05-24 21:45:31 +0000120%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000121%type<ttype> ContainerType
122%type<ttype> MapType
123%type<ttype> SetType
124%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000125
Mark Sleef0712dc2006-10-25 19:03:57 +0000126%type<ttype> TypeDefinition
127
Mark Slee31985722006-05-24 21:45:31 +0000128%type<ttypedef> Typedef
129%type<ttype> DefinitionType
130
131%type<tfield> Field
Mark Slee7ff32452007-02-01 05:26:18 +0000132%type<iconst> FieldIdentifier
Mark Slee31985722006-05-24 21:45:31 +0000133%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000134%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000135%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000136
137%type<tenum> Enum
138%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000139%type<tenumv> EnumDef
140
Mark Slee6a47fed2007-02-07 02:40:59 +0000141%type<ttypedef> Senum
142%type<tbase> SenumDefList
143%type<id> SenumDef
144
Mark Slee30152872006-11-28 01:24:07 +0000145%type<tconst> Const
146%type<tconstv> ConstValue
147%type<tconstv> ConstList
148%type<tconstv> ConstListContents
149%type<tconstv> ConstMap
150%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000151
152%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000153%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000154%type<tservice> Service
155
156%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000157%type<ttype> FunctionType
158%type<tservice> FunctionList
159
Mark Slee36bfa2e2007-01-19 20:09:51 +0000160%type<tstruct> Throws
161%type<tservice> Extends
162%type<tbool> Async
163%type<tbool> XsdAll
164%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000165%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000166%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000167%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000168
ccheeverf53b5cf2007-02-05 20:33:11 +0000169%type<tdoc> DocTextOptional
170
Mark Slee31985722006-05-24 21:45:31 +0000171%%
172
Mark Sleef5377b32006-10-10 01:42:59 +0000173/**
174 * Thrift Grammar Implementation.
175 *
176 * For the most part this source file works its way top down from what you
177 * might expect to find in a typical .thrift file, i.e. type definitions and
178 * namespaces up top followed by service definitions using those types.
179 */
Mark Slee31985722006-05-24 21:45:31 +0000180
181Program:
Mark Sleef0712dc2006-10-25 19:03:57 +0000182 HeaderList DefinitionList
183 {
184 pdebug("Program -> Headers DefinitionList");
185 }
186
187HeaderList:
188 HeaderList Header
189 {
190 pdebug("HeaderList -> HeaderList Header");
191 }
192|
193 {
194 pdebug("HeaderList -> ");
195 }
196
197Header:
198 Include
199 {
200 pdebug("Header -> Include");
201 }
202| tok_namespace tok_identifier
203 {
204 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
205 if (g_parse_mode == PROGRAM) {
206 g_program->set_cpp_namespace($2);
207 g_program->set_java_package($2);
208 }
209 }
210| tok_cpp_namespace tok_identifier
211 {
212 pdebug("Header -> tok_cpp_namespace tok_identifier");
213 if (g_parse_mode == PROGRAM) {
214 g_program->set_cpp_namespace($2);
215 }
216 }
217| tok_cpp_include tok_literal
218 {
219 pdebug("Header -> tok_cpp_include tok_literal");
220 if (g_parse_mode == PROGRAM) {
221 g_program->add_cpp_include($2);
222 }
223 }
Mark Sleee888b372007-01-12 01:06:24 +0000224| tok_php_namespace tok_identifier
225 {
226 pdebug("Header -> tok_php_namespace tok_identifier");
227 if (g_parse_mode == PROGRAM) {
228 g_program->set_php_namespace($2);
229 }
230 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000231| tok_java_package tok_identifier
232 {
233 pdebug("Header -> tok_java_package tok_identifier");
234 if (g_parse_mode == PROGRAM) {
235 g_program->set_java_package($2);
236 }
237 }
Mark Slee0d9199e2007-01-31 02:08:30 +0000238| tok_xsd_namespace tok_literal
239 {
240 pdebug("Header -> tok_xsd_namespace tok_literal");
241 if (g_parse_mode == PROGRAM) {
242 g_program->set_xsd_namespace($2);
243 }
244 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000245
246Include:
247 tok_include tok_literal
248 {
249 pdebug("Include -> tok_include tok_literal");
250 if (g_parse_mode == INCLUDES) {
251 std::string path = include_file(std::string($2));
252 if (!path.empty()) {
253 g_program->add_include(path);
254 }
255 }
256 }
Mark Slee31985722006-05-24 21:45:31 +0000257
258DefinitionList:
259 DefinitionList Definition
260 {
261 pdebug("DefinitionList -> DefinitionList Definition");
262 }
263|
264 {
265 pdebug("DefinitionList -> ");
266 }
267
268Definition:
Mark Slee30152872006-11-28 01:24:07 +0000269 Const
270 {
271 pdebug("Definition -> Const");
272 if (g_parse_mode == PROGRAM) {
273 g_program->add_const($1);
274 }
275 }
276| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000277 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000278 pdebug("Definition -> TypeDefinition");
279 if (g_parse_mode == PROGRAM) {
280 g_scope->add_type($1->get_name(), $1);
281 if (g_parent_scope != NULL) {
282 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
283 }
284 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000285 }
Mark Slee31985722006-05-24 21:45:31 +0000286| Service
287 {
288 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000289 if (g_parse_mode == PROGRAM) {
290 g_scope->add_service($1->get_name(), $1);
291 if (g_parent_scope != NULL) {
292 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
293 }
294 g_program->add_service($1);
295 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000296 }
297
Mark Sleef0712dc2006-10-25 19:03:57 +0000298TypeDefinition:
299 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000300 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000301 pdebug("TypeDefinition -> Typedef");
302 if (g_parse_mode == PROGRAM) {
303 g_program->add_typedef($1);
304 }
305 }
306| Enum
307 {
308 pdebug("TypeDefinition -> Enum");
309 if (g_parse_mode == PROGRAM) {
310 g_program->add_enum($1);
311 }
312 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000313| Senum
314 {
315 pdebug("TypeDefinition -> Senum");
316 if (g_parse_mode == PROGRAM) {
317 g_program->add_typedef($1);
318 }
319 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000320| Struct
321 {
322 pdebug("TypeDefinition -> Struct");
323 if (g_parse_mode == PROGRAM) {
324 g_program->add_struct($1);
325 }
326 }
327| Xception
328 {
329 pdebug("TypeDefinition -> Xception");
330 if (g_parse_mode == PROGRAM) {
331 g_program->add_xception($1);
332 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000333 }
Mark Slee31985722006-05-24 21:45:31 +0000334
335Typedef:
ccheeverf53b5cf2007-02-05 20:33:11 +0000336 DocTextOptional tok_typedef DefinitionType tok_identifier
Mark Slee31985722006-05-24 21:45:31 +0000337 {
338 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000339 t_typedef *td = new t_typedef(g_program, $3, $4);
Mark Slee31985722006-05-24 21:45:31 +0000340 $$ = td;
ccheeverf53b5cf2007-02-05 20:33:11 +0000341 if ($1 != NULL) {
342 td->set_doc($1);
343 }
Mark Slee31985722006-05-24 21:45:31 +0000344 }
345
ccheeverf53b5cf2007-02-05 20:33:11 +0000346DocTextOptional:
347 tok_doctext
348 {
349 pdebug("DocTextOptional -> tok_doctext");
350 $$ = $1;
351 }
352|
353 {
354 $$ = NULL;
355 }
356
Mark Slee6a47fed2007-02-07 02:40:59 +0000357CommaOrSemicolonOptional:
358 ','
359 {}
360| ';'
361 {}
362|
363 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000364
Mark Slee31985722006-05-24 21:45:31 +0000365Enum:
ccheeverf53b5cf2007-02-05 20:33:11 +0000366 DocTextOptional tok_enum tok_identifier '{' EnumDefList '}'
Mark Slee31985722006-05-24 21:45:31 +0000367 {
368 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
ccheeverf53b5cf2007-02-05 20:33:11 +0000369 $$ = $5;
370 $$->set_name($3);
371 if ($1 != NULL) {
372 $$->set_doc($1);
373 }
Mark Slee31985722006-05-24 21:45:31 +0000374 }
375
376EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000377 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000378 {
379 pdebug("EnumDefList -> EnumDefList EnumDef");
380 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000381 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000382 }
383|
384 {
385 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000386 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000387 }
388
389EnumDef:
ccheeverf53b5cf2007-02-05 20:33:11 +0000390 DocTextOptional tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000391 {
Mark Slee30152872006-11-28 01:24:07 +0000392 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000393 if ($4 < 0) {
394 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000395 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000396 $$ = new t_enum_value($2, $4);
397 if ($1 != NULL) {
398 $$->set_doc($1);
399 }
Mark Slee31985722006-05-24 21:45:31 +0000400 }
401|
ccheeverf53b5cf2007-02-05 20:33:11 +0000402 DocTextOptional tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000403 {
Mark Slee30152872006-11-28 01:24:07 +0000404 pdebug("EnumDef -> tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000405 $$ = new t_enum_value($2);
406 if ($1 != NULL) {
407 $$->set_doc($1);
408 }
Mark Slee30152872006-11-28 01:24:07 +0000409 }
410
Mark Slee6a47fed2007-02-07 02:40:59 +0000411Senum:
412 DocTextOptional tok_senum tok_identifier '{' SenumDefList '}'
413 {
414 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
415 $$ = new t_typedef(g_program, $5, $3);
416 if ($1 != NULL) {
417 $$->set_doc($1);
418 }
419 }
420
421SenumDefList:
422 SenumDefList SenumDef
423 {
424 pdebug("SenumDefList -> SenumDefList SenumDef");
425 $$ = $1;
426 $$->add_string_enum_val($2);
427 }
428|
429 {
430 pdebug("SenumDefList -> ");
431 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
432 $$->set_string_enum(true);
433 }
434
435SenumDef:
436 tok_literal CommaOrSemicolonOptional
437 {
438 pdebug("SenumDef -> tok_literal");
439 $$ = $1;
440 }
441
Mark Slee30152872006-11-28 01:24:07 +0000442Const:
443 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
444 {
445 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000446 if (g_parse_mode == PROGRAM) {
447 $$ = new t_const($2, $3, $5);
448 validate_const_type($$);
449 } else {
450 $$ = NULL;
451 }
Mark Slee30152872006-11-28 01:24:07 +0000452 }
453
454ConstValue:
455 tok_int_constant
456 {
457 pdebug("ConstValue => tok_int_constant");
458 $$ = new t_const_value();
459 $$->set_integer($1);
460 }
461| tok_dub_constant
462 {
463 pdebug("ConstValue => tok_dub_constant");
464 $$ = new t_const_value();
465 $$->set_double($1);
466 }
467| tok_literal
468 {
469 pdebug("ConstValue => tok_literal");
470 $$ = new t_const_value();
471 $$->set_string($1);
472 }
Mark Slee67fc6342006-11-29 03:37:04 +0000473| tok_identifier
474 {
475 pdebug("ConstValue => tok_identifier");
476 $$ = new t_const_value();
477 $$->set_string($1);
478 }
Mark Slee30152872006-11-28 01:24:07 +0000479| ConstList
480 {
481 pdebug("ConstValue => ConstList");
482 $$ = $1;
483 }
484| ConstMap
485 {
486 pdebug("ConstValue => ConstMap");
487 $$ = $1;
488 }
489
490ConstList:
491 '[' ConstListContents ']'
492 {
493 pdebug("ConstList => [ ConstListContents ]");
494 $$ = $2;
495 }
496
497ConstListContents:
498 ConstListContents ConstValue CommaOrSemicolonOptional
499 {
500 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
501 $$ = $1;
502 $$->add_list($2);
503 }
504|
505 {
506 pdebug("ConstListContents =>");
507 $$ = new t_const_value();
508 $$->set_list();
509 }
510
511ConstMap:
512 '{' ConstMapContents '}'
513 {
514 pdebug("ConstMap => { ConstMapContents }");
515 $$ = $2;
516 }
517
518ConstMapContents:
519 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
520 {
521 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
522 $$ = $1;
523 $$->add_map($2, $4);
524 }
525|
526 {
527 pdebug("ConstMapContents =>");
528 $$ = new t_const_value();
529 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000530 }
531
532Struct:
ccheeverf53b5cf2007-02-05 20:33:11 +0000533 DocTextOptional tok_struct tok_identifier XsdAll '{' FieldList '}'
Mark Slee31985722006-05-24 21:45:31 +0000534 {
535 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
ccheeverf53b5cf2007-02-05 20:33:11 +0000536 $6->set_name($3);
537 if ($1 != NULL) {
538 $6->set_doc($1);
539 }
540 $6->set_xsd_all($4);
541 $$ = $6;
Mark Slee9cb7c612006-09-01 22:17:45 +0000542 y_field_val = -1;
543 }
544
Mark Slee36bfa2e2007-01-19 20:09:51 +0000545XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000546 tok_xsd_all
547 {
548 $$ = true;
549 }
550|
551 {
552 $$ = false;
553 }
554
Mark Slee36bfa2e2007-01-19 20:09:51 +0000555XsdOptional:
556 tok_xsd_optional
557 {
558 $$ = true;
559 }
560|
561 {
562 $$ = false;
563 }
564
Mark Slee7df0e2a2007-02-06 21:03:18 +0000565XsdNillable:
566 tok_xsd_nillable
567 {
568 $$ = true;
569 }
570|
571 {
572 $$ = false;
573 }
574
Mark Slee21135c32007-02-05 21:52:08 +0000575XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000576 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000577 {
Mark Slee748d83f2007-02-07 01:20:08 +0000578 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000579 }
580|
581 {
582 $$ = NULL;
583 }
584
Mark Slee9cb7c612006-09-01 22:17:45 +0000585Xception:
586 tok_xception tok_identifier '{' FieldList '}'
587 {
588 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
589 $4->set_name($2);
590 $4->set_xception(true);
ccheeverf53b5cf2007-02-05 20:33:11 +0000591/*
592 if ($4 != NULL) {
593 $5->set_doc($4);
594 }
595*/
Mark Slee9cb7c612006-09-01 22:17:45 +0000596 $$ = $4;
597 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000598 }
599
600Service:
ccheeverf53b5cf2007-02-05 20:33:11 +0000601 DocTextOptional tok_service tok_identifier Extends '{' FunctionList '}'
Mark Slee31985722006-05-24 21:45:31 +0000602 {
603 pdebug("Service -> tok_service tok_identifier { FunctionList }");
ccheeverf53b5cf2007-02-05 20:33:11 +0000604 $$ = $6;
605 $$->set_name($3);
606 $$->set_extends($4);
607 if ($1 != NULL) {
608 $$->set_doc($1);
609 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000610 }
611
Mark Slee36bfa2e2007-01-19 20:09:51 +0000612Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000613 tok_extends tok_identifier
614 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000615 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000616 $$ = NULL;
617 if (g_parse_mode == PROGRAM) {
618 $$ = g_scope->get_service($2);
619 if ($$ == NULL) {
620 yyerror("Service \"%s\" has not been defined.", $2);
621 exit(1);
622 }
623 }
624 }
625|
626 {
627 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000628 }
629
630FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000631 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000632 {
633 pdebug("FunctionList -> FunctionList Function");
634 $$ = $1;
635 $1->add_function($2);
636 }
637|
638 {
639 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000640 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000641 }
642
643Function:
ccheeverf53b5cf2007-02-05 20:33:11 +0000644 DocTextOptional Async FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000645 {
ccheeverf53b5cf2007-02-05 20:33:11 +0000646 $6->set_name(std::string($4) + "_args");
647 $$ = new t_function($3, $4, $6, $8, $2);
648 if ($1 != NULL) {
649 $$->set_doc($1);
650 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000651 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000652 }
653
Mark Slee36bfa2e2007-01-19 20:09:51 +0000654Async:
Mark Slee52f643d2006-08-09 00:03:43 +0000655 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000656 {
Mark Slee52f643d2006-08-09 00:03:43 +0000657 $$ = true;
658 }
659|
660 {
661 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000662 }
663
Mark Slee36bfa2e2007-01-19 20:09:51 +0000664Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000665 tok_throws '(' FieldList ')'
666 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000667 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000668 $$ = $3;
669 }
670|
671 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000672 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000673 }
674
Mark Slee31985722006-05-24 21:45:31 +0000675FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000676 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000677 {
678 pdebug("FieldList -> FieldList , Field");
679 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000680 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000681 }
682|
683 {
684 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000685 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000686 }
687
688Field:
Mark Slee7df0e2a2007-02-06 21:03:18 +0000689 DocTextOptional FieldIdentifier FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000690 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000691 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000692 if ($2 < 0) {
Mark Slee21135c32007-02-05 21:52:08 +0000693 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 +0000694 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000695 $$ = new t_field($3, $4, $2);
696 if ($5 != NULL) {
697 validate_field_value($$, $5);
698 $$->set_value($5);
Mark Slee7ff32452007-02-01 05:26:18 +0000699 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000700 $$->set_xsd_optional($6);
Mark Slee7df0e2a2007-02-06 21:03:18 +0000701 $$->set_xsd_nillable($7);
ccheeverf53b5cf2007-02-05 20:33:11 +0000702 if ($1 != NULL) {
703 $$->set_doc($1);
704 }
Mark Slee7df0e2a2007-02-06 21:03:18 +0000705 if ($8 != NULL) {
Mark Slee748d83f2007-02-07 01:20:08 +0000706 $$->set_xsd_attrs($8);
Mark Slee21135c32007-02-05 21:52:08 +0000707 }
Mark Slee31985722006-05-24 21:45:31 +0000708 }
Mark Slee7ff32452007-02-01 05:26:18 +0000709
710FieldIdentifier:
711 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000712 {
Mark Slee7ff32452007-02-01 05:26:18 +0000713 if ($1 <= 0) {
714 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n", $1);
715 $1 = y_field_val--;
Mark Sleef0712dc2006-10-25 19:03:57 +0000716 }
Mark Slee7ff32452007-02-01 05:26:18 +0000717 $$ = $1;
718 }
719|
720 {
721 $$ = y_field_val--;
722 }
723
724FieldValue:
725 '=' ConstValue
726 {
727 if (g_parse_mode == PROGRAM) {
728 $$ = $2;
729 } else {
730 $$ = NULL;
731 }
732 }
733|
734 {
735 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000736 }
Mark Slee31985722006-05-24 21:45:31 +0000737
738DefinitionType:
739 BaseType
740 {
741 pdebug("DefinitionType -> BaseType");
742 $$ = $1;
743 }
Mark Sleee8540632006-05-30 09:24:40 +0000744| ContainerType
745 {
746 pdebug("DefinitionType -> ContainerType");
747 $$ = $1;
748 }
Mark Slee31985722006-05-24 21:45:31 +0000749
750FunctionType:
751 FieldType
752 {
Mark Sleee8540632006-05-30 09:24:40 +0000753 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000754 $$ = $1;
755 }
756| tok_void
757 {
Mark Sleee8540632006-05-30 09:24:40 +0000758 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000759 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000760 }
761
762FieldType:
763 tok_identifier
764 {
Mark Sleee8540632006-05-30 09:24:40 +0000765 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000766 if (g_parse_mode == INCLUDES) {
767 // Ignore identifiers in include mode
768 $$ = NULL;
769 } else {
770 // Lookup the identifier in the current scope
771 $$ = g_scope->get_type($1);
772 if ($$ == NULL) {
773 yyerror("Type \"%s\" has not been defined.", $1);
774 exit(1);
775 }
Mark Sleee8540632006-05-30 09:24:40 +0000776 }
Mark Slee31985722006-05-24 21:45:31 +0000777 }
778| BaseType
779 {
Mark Sleee8540632006-05-30 09:24:40 +0000780 pdebug("FieldType -> BaseType");
781 $$ = $1;
782 }
783| ContainerType
784 {
785 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000786 $$ = $1;
787 }
788
789BaseType:
790 tok_string
791 {
792 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000793 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000794 }
Mark Sleeb6200d82007-01-19 19:14:36 +0000795| tok_slist
796 {
797 pdebug("BaseType -> tok_slist");
798 $$ = g_type_slist;
799 }
Mark Slee78f58e22006-09-02 04:17:07 +0000800| tok_bool
801 {
802 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000803 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000804 }
Mark Slee31985722006-05-24 21:45:31 +0000805| tok_byte
806 {
807 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000808 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000809 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000810| tok_i16
811 {
812 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000813 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000814 }
Mark Slee31985722006-05-24 21:45:31 +0000815| tok_i32
816 {
817 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000818 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000819 }
Mark Slee31985722006-05-24 21:45:31 +0000820| tok_i64
821 {
822 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000823 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000824 }
Mark Sleec98d0502006-09-06 02:42:25 +0000825| tok_double
826 {
827 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000828 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000829 }
Mark Slee31985722006-05-24 21:45:31 +0000830
Mark Sleee8540632006-05-30 09:24:40 +0000831ContainerType:
832 MapType
833 {
834 pdebug("ContainerType -> MapType");
835 $$ = $1;
836 }
837| SetType
838 {
839 pdebug("ContainerType -> SetType");
840 $$ = $1;
841 }
842| ListType
843 {
844 pdebug("ContainerType -> ListType");
845 $$ = $1;
846 }
847
848MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000849 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000850 {
851 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000852 $$ = new t_map($4, $6);
853 if ($2 != NULL) {
854 ((t_container*)$$)->set_cpp_name(std::string($2));
855 }
Mark Sleee8540632006-05-30 09:24:40 +0000856 }
857
858SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000859 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000860 {
861 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000862 $$ = new t_set($4);
863 if ($2 != NULL) {
864 ((t_container*)$$)->set_cpp_name(std::string($2));
865 }
Mark Sleee8540632006-05-30 09:24:40 +0000866 }
867
868ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000869 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +0000870 {
871 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +0000872 $$ = new t_list($3);
873 if ($5 != NULL) {
874 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +0000875 }
876 }
877
Mark Slee36bfa2e2007-01-19 20:09:51 +0000878CppType:
Mark Sleeafc76542007-02-09 21:55:44 +0000879 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +0000880 {
Mark Sleeafc76542007-02-09 21:55:44 +0000881 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +0000882 }
883|
884 {
885 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +0000886 }
887
Mark Slee31985722006-05-24 21:45:31 +0000888%%