blob: 23b9fa0690979313f78c25299a7464c4c0362f5f [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 Slee9cb7c612006-09-01 22:17:45 +000082
Mark Sleef5377b32006-10-10 01:42:59 +000083/**
84 * Base datatype keywords
85 */
86%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +000087%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +000088%token tok_byte
89%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +000090%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +000091%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +000092%token tok_senum
Mark Slee9cb7c612006-09-01 22:17:45 +000093%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +000094%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +000095%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +000096%token tok_double
Mark Slee31985722006-05-24 21:45:31 +000097
Mark Sleef5377b32006-10-10 01:42:59 +000098/**
99 * Complex type keywords
100 */
Mark Slee31985722006-05-24 21:45:31 +0000101%token tok_map
102%token tok_list
103%token tok_set
104
Mark Sleef5377b32006-10-10 01:42:59 +0000105/**
106 * Function modifiers
107 */
Mark Slee31985722006-05-24 21:45:31 +0000108%token tok_async
109
Mark Sleef5377b32006-10-10 01:42:59 +0000110/**
111 * Thrift language keywords
112 */
Mark Slee31985722006-05-24 21:45:31 +0000113%token tok_typedef
114%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000115%token tok_xception
116%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000117%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000118%token tok_service
119%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000120%token tok_const
Mark Slee31985722006-05-24 21:45:31 +0000121
Mark Sleef5377b32006-10-10 01:42:59 +0000122/**
123 * Grammar nodes
124 */
125
Mark Slee31985722006-05-24 21:45:31 +0000126%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000127%type<ttype> ContainerType
128%type<ttype> MapType
129%type<ttype> SetType
130%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000131
Mark Sleef0712dc2006-10-25 19:03:57 +0000132%type<ttype> TypeDefinition
133
Mark Slee31985722006-05-24 21:45:31 +0000134%type<ttypedef> Typedef
135%type<ttype> DefinitionType
136
137%type<tfield> Field
Mark Slee7ff32452007-02-01 05:26:18 +0000138%type<iconst> FieldIdentifier
Mark Slee31985722006-05-24 21:45:31 +0000139%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000140%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000141%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000142
143%type<tenum> Enum
144%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000145%type<tenumv> EnumDef
146
Mark Slee6a47fed2007-02-07 02:40:59 +0000147%type<ttypedef> Senum
148%type<tbase> SenumDefList
149%type<id> SenumDef
150
Mark Slee30152872006-11-28 01:24:07 +0000151%type<tconst> Const
152%type<tconstv> ConstValue
153%type<tconstv> ConstList
154%type<tconstv> ConstListContents
155%type<tconstv> ConstMap
156%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000157
158%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000159%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000160%type<tservice> Service
161
162%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000163%type<ttype> FunctionType
164%type<tservice> FunctionList
165
Mark Slee36bfa2e2007-01-19 20:09:51 +0000166%type<tstruct> Throws
167%type<tservice> Extends
168%type<tbool> Async
169%type<tbool> XsdAll
170%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000171%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000172%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000173%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000174
ccheeverf53b5cf2007-02-05 20:33:11 +0000175%type<tdoc> DocTextOptional
176
Mark Slee31985722006-05-24 21:45:31 +0000177%%
178
Mark Sleef5377b32006-10-10 01:42:59 +0000179/**
180 * Thrift Grammar Implementation.
181 *
182 * For the most part this source file works its way top down from what you
183 * might expect to find in a typical .thrift file, i.e. type definitions and
184 * namespaces up top followed by service definitions using those types.
185 */
Mark Slee31985722006-05-24 21:45:31 +0000186
187Program:
Mark Sleef0712dc2006-10-25 19:03:57 +0000188 HeaderList DefinitionList
189 {
190 pdebug("Program -> Headers DefinitionList");
191 }
192
193HeaderList:
194 HeaderList Header
195 {
196 pdebug("HeaderList -> HeaderList Header");
197 }
198|
199 {
200 pdebug("HeaderList -> ");
201 }
202
203Header:
204 Include
205 {
206 pdebug("Header -> Include");
207 }
208| tok_namespace tok_identifier
209 {
210 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
211 if (g_parse_mode == PROGRAM) {
212 g_program->set_cpp_namespace($2);
213 g_program->set_java_package($2);
214 }
215 }
216| tok_cpp_namespace tok_identifier
217 {
218 pdebug("Header -> tok_cpp_namespace tok_identifier");
219 if (g_parse_mode == PROGRAM) {
220 g_program->set_cpp_namespace($2);
221 }
222 }
223| tok_cpp_include tok_literal
224 {
225 pdebug("Header -> tok_cpp_include tok_literal");
226 if (g_parse_mode == PROGRAM) {
227 g_program->add_cpp_include($2);
228 }
229 }
Mark Sleee888b372007-01-12 01:06:24 +0000230| tok_php_namespace tok_identifier
231 {
232 pdebug("Header -> tok_php_namespace tok_identifier");
233 if (g_parse_mode == PROGRAM) {
234 g_program->set_php_namespace($2);
235 }
236 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000237| tok_java_package tok_identifier
238 {
239 pdebug("Header -> tok_java_package tok_identifier");
240 if (g_parse_mode == PROGRAM) {
241 g_program->set_java_package($2);
242 }
243 }
Mark Slee0d9199e2007-01-31 02:08:30 +0000244| tok_xsd_namespace tok_literal
245 {
246 pdebug("Header -> tok_xsd_namespace tok_literal");
247 if (g_parse_mode == PROGRAM) {
248 g_program->set_xsd_namespace($2);
249 }
250 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000251
252Include:
253 tok_include tok_literal
254 {
255 pdebug("Include -> tok_include tok_literal");
256 if (g_parse_mode == INCLUDES) {
257 std::string path = include_file(std::string($2));
258 if (!path.empty()) {
259 g_program->add_include(path);
260 }
261 }
262 }
Mark Slee31985722006-05-24 21:45:31 +0000263
264DefinitionList:
265 DefinitionList Definition
266 {
267 pdebug("DefinitionList -> DefinitionList Definition");
268 }
269|
270 {
271 pdebug("DefinitionList -> ");
272 }
273
274Definition:
Mark Slee30152872006-11-28 01:24:07 +0000275 Const
276 {
277 pdebug("Definition -> Const");
278 if (g_parse_mode == PROGRAM) {
279 g_program->add_const($1);
280 }
281 }
282| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000283 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000284 pdebug("Definition -> TypeDefinition");
285 if (g_parse_mode == PROGRAM) {
286 g_scope->add_type($1->get_name(), $1);
287 if (g_parent_scope != NULL) {
288 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
289 }
290 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000291 }
Mark Slee31985722006-05-24 21:45:31 +0000292| Service
293 {
294 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000295 if (g_parse_mode == PROGRAM) {
296 g_scope->add_service($1->get_name(), $1);
297 if (g_parent_scope != NULL) {
298 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
299 }
300 g_program->add_service($1);
301 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000302 }
303
Mark Sleef0712dc2006-10-25 19:03:57 +0000304TypeDefinition:
305 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000306 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000307 pdebug("TypeDefinition -> Typedef");
308 if (g_parse_mode == PROGRAM) {
309 g_program->add_typedef($1);
310 }
311 }
312| Enum
313 {
314 pdebug("TypeDefinition -> Enum");
315 if (g_parse_mode == PROGRAM) {
316 g_program->add_enum($1);
317 }
318 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000319| Senum
320 {
321 pdebug("TypeDefinition -> Senum");
322 if (g_parse_mode == PROGRAM) {
323 g_program->add_typedef($1);
324 }
325 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000326| Struct
327 {
328 pdebug("TypeDefinition -> Struct");
329 if (g_parse_mode == PROGRAM) {
330 g_program->add_struct($1);
331 }
332 }
333| Xception
334 {
335 pdebug("TypeDefinition -> Xception");
336 if (g_parse_mode == PROGRAM) {
337 g_program->add_xception($1);
338 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000339 }
Mark Slee31985722006-05-24 21:45:31 +0000340
341Typedef:
ccheeverf53b5cf2007-02-05 20:33:11 +0000342 DocTextOptional tok_typedef DefinitionType tok_identifier
Mark Slee31985722006-05-24 21:45:31 +0000343 {
344 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000345 t_typedef *td = new t_typedef(g_program, $3, $4);
Mark Slee31985722006-05-24 21:45:31 +0000346 $$ = td;
ccheeverf53b5cf2007-02-05 20:33:11 +0000347 if ($1 != NULL) {
348 td->set_doc($1);
349 }
Mark Slee31985722006-05-24 21:45:31 +0000350 }
351
ccheeverf53b5cf2007-02-05 20:33:11 +0000352DocTextOptional:
353 tok_doctext
354 {
355 pdebug("DocTextOptional -> tok_doctext");
356 $$ = $1;
357 }
358|
359 {
360 $$ = NULL;
361 }
362
Mark Slee6a47fed2007-02-07 02:40:59 +0000363CommaOrSemicolonOptional:
364 ','
365 {}
366| ';'
367 {}
368|
369 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000370
Mark Slee31985722006-05-24 21:45:31 +0000371Enum:
ccheeverf53b5cf2007-02-05 20:33:11 +0000372 DocTextOptional tok_enum tok_identifier '{' EnumDefList '}'
Mark Slee31985722006-05-24 21:45:31 +0000373 {
374 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
ccheeverf53b5cf2007-02-05 20:33:11 +0000375 $$ = $5;
376 $$->set_name($3);
377 if ($1 != NULL) {
378 $$->set_doc($1);
379 }
Mark Slee31985722006-05-24 21:45:31 +0000380 }
381
382EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000383 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000384 {
385 pdebug("EnumDefList -> EnumDefList EnumDef");
386 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000387 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000388 }
389|
390 {
391 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000392 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000393 }
394
395EnumDef:
ccheeverf53b5cf2007-02-05 20:33:11 +0000396 DocTextOptional tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000397 {
Mark Slee30152872006-11-28 01:24:07 +0000398 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000399 if ($4 < 0) {
400 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000401 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000402 $$ = new t_enum_value($2, $4);
403 if ($1 != NULL) {
404 $$->set_doc($1);
405 }
Mark Slee31985722006-05-24 21:45:31 +0000406 }
407|
ccheeverf53b5cf2007-02-05 20:33:11 +0000408 DocTextOptional tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000409 {
Mark Slee30152872006-11-28 01:24:07 +0000410 pdebug("EnumDef -> tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000411 $$ = new t_enum_value($2);
412 if ($1 != NULL) {
413 $$->set_doc($1);
414 }
Mark Slee30152872006-11-28 01:24:07 +0000415 }
416
Mark Slee6a47fed2007-02-07 02:40:59 +0000417Senum:
418 DocTextOptional tok_senum tok_identifier '{' SenumDefList '}'
419 {
420 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
421 $$ = new t_typedef(g_program, $5, $3);
422 if ($1 != NULL) {
423 $$->set_doc($1);
424 }
425 }
426
427SenumDefList:
428 SenumDefList SenumDef
429 {
430 pdebug("SenumDefList -> SenumDefList SenumDef");
431 $$ = $1;
432 $$->add_string_enum_val($2);
433 }
434|
435 {
436 pdebug("SenumDefList -> ");
437 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
438 $$->set_string_enum(true);
439 }
440
441SenumDef:
442 tok_literal CommaOrSemicolonOptional
443 {
444 pdebug("SenumDef -> tok_literal");
445 $$ = $1;
446 }
447
Mark Slee30152872006-11-28 01:24:07 +0000448Const:
449 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
450 {
451 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000452 if (g_parse_mode == PROGRAM) {
453 $$ = new t_const($2, $3, $5);
454 validate_const_type($$);
455 } else {
456 $$ = NULL;
457 }
Mark Slee30152872006-11-28 01:24:07 +0000458 }
459
460ConstValue:
461 tok_int_constant
462 {
463 pdebug("ConstValue => tok_int_constant");
464 $$ = new t_const_value();
465 $$->set_integer($1);
466 }
467| tok_dub_constant
468 {
469 pdebug("ConstValue => tok_dub_constant");
470 $$ = new t_const_value();
471 $$->set_double($1);
472 }
473| tok_literal
474 {
475 pdebug("ConstValue => tok_literal");
476 $$ = new t_const_value();
477 $$->set_string($1);
478 }
Mark Slee67fc6342006-11-29 03:37:04 +0000479| tok_identifier
480 {
481 pdebug("ConstValue => tok_identifier");
482 $$ = new t_const_value();
483 $$->set_string($1);
484 }
Mark Slee30152872006-11-28 01:24:07 +0000485| ConstList
486 {
487 pdebug("ConstValue => ConstList");
488 $$ = $1;
489 }
490| ConstMap
491 {
492 pdebug("ConstValue => ConstMap");
493 $$ = $1;
494 }
495
496ConstList:
497 '[' ConstListContents ']'
498 {
499 pdebug("ConstList => [ ConstListContents ]");
500 $$ = $2;
501 }
502
503ConstListContents:
504 ConstListContents ConstValue CommaOrSemicolonOptional
505 {
506 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
507 $$ = $1;
508 $$->add_list($2);
509 }
510|
511 {
512 pdebug("ConstListContents =>");
513 $$ = new t_const_value();
514 $$->set_list();
515 }
516
517ConstMap:
518 '{' ConstMapContents '}'
519 {
520 pdebug("ConstMap => { ConstMapContents }");
521 $$ = $2;
522 }
523
524ConstMapContents:
525 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
526 {
527 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
528 $$ = $1;
529 $$->add_map($2, $4);
530 }
531|
532 {
533 pdebug("ConstMapContents =>");
534 $$ = new t_const_value();
535 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000536 }
537
538Struct:
ccheeverf53b5cf2007-02-05 20:33:11 +0000539 DocTextOptional tok_struct tok_identifier XsdAll '{' FieldList '}'
Mark Slee31985722006-05-24 21:45:31 +0000540 {
541 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
ccheeverf53b5cf2007-02-05 20:33:11 +0000542 $6->set_name($3);
543 if ($1 != NULL) {
544 $6->set_doc($1);
545 }
546 $6->set_xsd_all($4);
547 $$ = $6;
Mark Slee9cb7c612006-09-01 22:17:45 +0000548 y_field_val = -1;
549 }
550
Mark Slee36bfa2e2007-01-19 20:09:51 +0000551XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000552 tok_xsd_all
553 {
554 $$ = true;
555 }
556|
557 {
558 $$ = false;
559 }
560
Mark Slee36bfa2e2007-01-19 20:09:51 +0000561XsdOptional:
562 tok_xsd_optional
563 {
564 $$ = true;
565 }
566|
567 {
568 $$ = false;
569 }
570
Mark Slee7df0e2a2007-02-06 21:03:18 +0000571XsdNillable:
572 tok_xsd_nillable
573 {
574 $$ = true;
575 }
576|
577 {
578 $$ = false;
579 }
580
Mark Slee21135c32007-02-05 21:52:08 +0000581XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000582 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000583 {
Mark Slee748d83f2007-02-07 01:20:08 +0000584 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000585 }
586|
587 {
588 $$ = NULL;
589 }
590
Mark Slee9cb7c612006-09-01 22:17:45 +0000591Xception:
592 tok_xception tok_identifier '{' FieldList '}'
593 {
594 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
595 $4->set_name($2);
596 $4->set_xception(true);
ccheeverf53b5cf2007-02-05 20:33:11 +0000597/*
598 if ($4 != NULL) {
599 $5->set_doc($4);
600 }
601*/
Mark Slee9cb7c612006-09-01 22:17:45 +0000602 $$ = $4;
603 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000604 }
605
606Service:
ccheeverf53b5cf2007-02-05 20:33:11 +0000607 DocTextOptional tok_service tok_identifier Extends '{' FunctionList '}'
Mark Slee31985722006-05-24 21:45:31 +0000608 {
609 pdebug("Service -> tok_service tok_identifier { FunctionList }");
ccheeverf53b5cf2007-02-05 20:33:11 +0000610 $$ = $6;
611 $$->set_name($3);
612 $$->set_extends($4);
613 if ($1 != NULL) {
614 $$->set_doc($1);
615 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000616 }
617
Mark Slee36bfa2e2007-01-19 20:09:51 +0000618Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000619 tok_extends tok_identifier
620 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000621 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000622 $$ = NULL;
623 if (g_parse_mode == PROGRAM) {
624 $$ = g_scope->get_service($2);
625 if ($$ == NULL) {
626 yyerror("Service \"%s\" has not been defined.", $2);
627 exit(1);
628 }
629 }
630 }
631|
632 {
633 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000634 }
635
636FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000637 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000638 {
639 pdebug("FunctionList -> FunctionList Function");
640 $$ = $1;
641 $1->add_function($2);
642 }
643|
644 {
645 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000646 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000647 }
648
649Function:
ccheeverf53b5cf2007-02-05 20:33:11 +0000650 DocTextOptional Async FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000651 {
ccheeverf53b5cf2007-02-05 20:33:11 +0000652 $6->set_name(std::string($4) + "_args");
653 $$ = new t_function($3, $4, $6, $8, $2);
654 if ($1 != NULL) {
655 $$->set_doc($1);
656 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000657 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000658 }
659
Mark Slee36bfa2e2007-01-19 20:09:51 +0000660Async:
Mark Slee52f643d2006-08-09 00:03:43 +0000661 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000662 {
Mark Slee52f643d2006-08-09 00:03:43 +0000663 $$ = true;
664 }
665|
666 {
667 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000668 }
669
Mark Slee36bfa2e2007-01-19 20:09:51 +0000670Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000671 tok_throws '(' FieldList ')'
672 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000673 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000674 $$ = $3;
675 }
676|
677 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000678 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000679 }
680
Mark Slee31985722006-05-24 21:45:31 +0000681FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000682 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000683 {
684 pdebug("FieldList -> FieldList , Field");
685 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000686 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000687 }
688|
689 {
690 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000691 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000692 }
693
694Field:
Mark Slee7df0e2a2007-02-06 21:03:18 +0000695 DocTextOptional FieldIdentifier FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000696 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000697 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000698 if ($2 < 0) {
Mark Slee21135c32007-02-05 21:52:08 +0000699 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 +0000700 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000701 $$ = new t_field($3, $4, $2);
702 if ($5 != NULL) {
703 validate_field_value($$, $5);
704 $$->set_value($5);
Mark Slee7ff32452007-02-01 05:26:18 +0000705 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000706 $$->set_xsd_optional($6);
Mark Slee7df0e2a2007-02-06 21:03:18 +0000707 $$->set_xsd_nillable($7);
ccheeverf53b5cf2007-02-05 20:33:11 +0000708 if ($1 != NULL) {
709 $$->set_doc($1);
710 }
Mark Slee7df0e2a2007-02-06 21:03:18 +0000711 if ($8 != NULL) {
Mark Slee748d83f2007-02-07 01:20:08 +0000712 $$->set_xsd_attrs($8);
Mark Slee21135c32007-02-05 21:52:08 +0000713 }
Mark Slee31985722006-05-24 21:45:31 +0000714 }
Mark Slee7ff32452007-02-01 05:26:18 +0000715
716FieldIdentifier:
717 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000718 {
Mark Slee7ff32452007-02-01 05:26:18 +0000719 if ($1 <= 0) {
720 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n", $1);
721 $1 = y_field_val--;
Mark Sleef0712dc2006-10-25 19:03:57 +0000722 }
Mark Slee7ff32452007-02-01 05:26:18 +0000723 $$ = $1;
724 }
725|
726 {
727 $$ = y_field_val--;
728 }
729
730FieldValue:
731 '=' ConstValue
732 {
733 if (g_parse_mode == PROGRAM) {
734 $$ = $2;
735 } else {
736 $$ = NULL;
737 }
738 }
739|
740 {
741 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000742 }
Mark Slee31985722006-05-24 21:45:31 +0000743
744DefinitionType:
745 BaseType
746 {
747 pdebug("DefinitionType -> BaseType");
748 $$ = $1;
749 }
Mark Sleee8540632006-05-30 09:24:40 +0000750| ContainerType
751 {
752 pdebug("DefinitionType -> ContainerType");
753 $$ = $1;
754 }
Mark Slee31985722006-05-24 21:45:31 +0000755
756FunctionType:
757 FieldType
758 {
Mark Sleee8540632006-05-30 09:24:40 +0000759 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000760 $$ = $1;
761 }
762| tok_void
763 {
Mark Sleee8540632006-05-30 09:24:40 +0000764 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000765 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000766 }
767
768FieldType:
769 tok_identifier
770 {
Mark Sleee8540632006-05-30 09:24:40 +0000771 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000772 if (g_parse_mode == INCLUDES) {
773 // Ignore identifiers in include mode
774 $$ = NULL;
775 } else {
776 // Lookup the identifier in the current scope
777 $$ = g_scope->get_type($1);
778 if ($$ == NULL) {
779 yyerror("Type \"%s\" has not been defined.", $1);
780 exit(1);
781 }
Mark Sleee8540632006-05-30 09:24:40 +0000782 }
Mark Slee31985722006-05-24 21:45:31 +0000783 }
784| BaseType
785 {
Mark Sleee8540632006-05-30 09:24:40 +0000786 pdebug("FieldType -> BaseType");
787 $$ = $1;
788 }
789| ContainerType
790 {
791 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000792 $$ = $1;
793 }
794
795BaseType:
796 tok_string
797 {
798 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000799 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000800 }
Mark Slee8d725a22007-04-13 01:57:12 +0000801| tok_binary
802 {
803 pdebug("BaseType -> tok_binary");
804 $$ = g_type_binary;
805 }
Mark Sleeb6200d82007-01-19 19:14:36 +0000806| tok_slist
807 {
808 pdebug("BaseType -> tok_slist");
809 $$ = g_type_slist;
810 }
Mark Slee78f58e22006-09-02 04:17:07 +0000811| tok_bool
812 {
813 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000814 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000815 }
Mark Slee31985722006-05-24 21:45:31 +0000816| tok_byte
817 {
818 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000819 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000820 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000821| tok_i16
822 {
823 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000824 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000825 }
Mark Slee31985722006-05-24 21:45:31 +0000826| tok_i32
827 {
828 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000829 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000830 }
Mark Slee31985722006-05-24 21:45:31 +0000831| tok_i64
832 {
833 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000834 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000835 }
Mark Sleec98d0502006-09-06 02:42:25 +0000836| tok_double
837 {
838 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000839 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000840 }
Mark Slee31985722006-05-24 21:45:31 +0000841
Mark Sleee8540632006-05-30 09:24:40 +0000842ContainerType:
843 MapType
844 {
845 pdebug("ContainerType -> MapType");
846 $$ = $1;
847 }
848| SetType
849 {
850 pdebug("ContainerType -> SetType");
851 $$ = $1;
852 }
853| ListType
854 {
855 pdebug("ContainerType -> ListType");
856 $$ = $1;
857 }
858
859MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000860 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000861 {
862 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000863 $$ = new t_map($4, $6);
864 if ($2 != NULL) {
865 ((t_container*)$$)->set_cpp_name(std::string($2));
866 }
Mark Sleee8540632006-05-30 09:24:40 +0000867 }
868
869SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000870 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000871 {
872 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000873 $$ = new t_set($4);
874 if ($2 != NULL) {
875 ((t_container*)$$)->set_cpp_name(std::string($2));
876 }
Mark Sleee8540632006-05-30 09:24:40 +0000877 }
878
879ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000880 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +0000881 {
882 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +0000883 $$ = new t_list($3);
884 if ($5 != NULL) {
885 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +0000886 }
887 }
888
Mark Slee36bfa2e2007-01-19 20:09:51 +0000889CppType:
Mark Sleeafc76542007-02-09 21:55:44 +0000890 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +0000891 {
Mark Sleeafc76542007-02-09 21:55:44 +0000892 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +0000893 }
894|
895 {
896 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +0000897 }
898
Mark Slee31985722006-05-24 21:45:31 +0000899%%