blob: 5f7e3b9950a205c67229d7958e2caa3c941bfbed [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;
David Reisscdffe262007-08-14 17:12:31 +000040 t_doc* tdoc;
Mark Slee30152872006-11-28 01:24:07 +000041 t_type* ttype;
Mark Slee6a47fed2007-02-07 02:40:59 +000042 t_base_type* tbase;
Mark Slee30152872006-11-28 01:24:07 +000043 t_typedef* ttypedef;
44 t_enum* tenum;
45 t_enum_value* tenumv;
46 t_const* tconst;
47 t_const_value* tconstv;
48 t_struct* tstruct;
49 t_service* tservice;
50 t_function* tfunction;
51 t_field* tfield;
David Reisscdffe262007-08-14 17:12:31 +000052 char* dtext;
Mark Slee31985722006-05-24 21:45:31 +000053}
54
Mark Sleef5377b32006-10-10 01:42:59 +000055/**
56 * Strings identifier
57 */
Mark Slee31985722006-05-24 21:45:31 +000058%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +000059%token<id> tok_literal
David Reisscdffe262007-08-14 17:12:31 +000060%token<dtext> tok_doctext
Mark Sleef5377b32006-10-10 01:42:59 +000061
62/**
Mark Slee30152872006-11-28 01:24:07 +000063 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +000064 */
Mark Slee31985722006-05-24 21:45:31 +000065%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +000066%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +000067
Mark Sleef5377b32006-10-10 01:42:59 +000068/**
Mark Sleef0712dc2006-10-25 19:03:57 +000069 * Header keywoards
Mark Sleef5377b32006-10-10 01:42:59 +000070 */
Mark Sleef0712dc2006-10-25 19:03:57 +000071%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +000072%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000073%token tok_cpp_namespace
74%token tok_cpp_include
75%token tok_cpp_type
Mark Sleee888b372007-01-12 01:06:24 +000076%token tok_php_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000077%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +000078%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +000079%token tok_xsd_optional
Mark Slee7df0e2a2007-02-06 21:03:18 +000080%token tok_xsd_nillable
Mark Slee0d9199e2007-01-31 02:08:30 +000081%token tok_xsd_namespace
Mark Slee21135c32007-02-05 21:52:08 +000082%token tok_xsd_attrs
Mark Slee58dfb4f2007-07-06 02:45:25 +000083%token tok_ruby_namespace
Mark Slee9cb7c612006-09-01 22:17:45 +000084
Mark Sleef5377b32006-10-10 01:42:59 +000085/**
86 * Base datatype keywords
87 */
88%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +000089%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +000090%token tok_byte
91%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +000092%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +000093%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +000094%token tok_senum
Mark Slee9cb7c612006-09-01 22:17:45 +000095%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +000096%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +000097%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +000098%token tok_double
Mark Slee31985722006-05-24 21:45:31 +000099
Mark Sleef5377b32006-10-10 01:42:59 +0000100/**
101 * Complex type keywords
102 */
Mark Slee31985722006-05-24 21:45:31 +0000103%token tok_map
104%token tok_list
105%token tok_set
106
Mark Sleef5377b32006-10-10 01:42:59 +0000107/**
108 * Function modifiers
109 */
Mark Slee31985722006-05-24 21:45:31 +0000110%token tok_async
111
Mark Sleef5377b32006-10-10 01:42:59 +0000112/**
113 * Thrift language keywords
114 */
Mark Slee31985722006-05-24 21:45:31 +0000115%token tok_typedef
116%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000117%token tok_xception
118%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000119%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000120%token tok_service
121%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000122%token tok_const
Mark Slee31985722006-05-24 21:45:31 +0000123
Mark Sleef5377b32006-10-10 01:42:59 +0000124/**
125 * Grammar nodes
126 */
127
Mark Slee31985722006-05-24 21:45:31 +0000128%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000129%type<ttype> ContainerType
130%type<ttype> MapType
131%type<ttype> SetType
132%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000133
David Reisscdffe262007-08-14 17:12:31 +0000134%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000135%type<ttype> TypeDefinition
136
Mark Slee31985722006-05-24 21:45:31 +0000137%type<ttypedef> Typedef
138%type<ttype> DefinitionType
139
140%type<tfield> Field
Mark Slee7ff32452007-02-01 05:26:18 +0000141%type<iconst> FieldIdentifier
Mark Slee31985722006-05-24 21:45:31 +0000142%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000143%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000144%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000145
146%type<tenum> Enum
147%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000148%type<tenumv> EnumDef
149
Mark Slee6a47fed2007-02-07 02:40:59 +0000150%type<ttypedef> Senum
151%type<tbase> SenumDefList
152%type<id> SenumDef
153
Mark Slee30152872006-11-28 01:24:07 +0000154%type<tconst> Const
155%type<tconstv> ConstValue
156%type<tconstv> ConstList
157%type<tconstv> ConstListContents
158%type<tconstv> ConstMap
159%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000160
161%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000162%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000163%type<tservice> Service
164
165%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000166%type<ttype> FunctionType
167%type<tservice> FunctionList
168
Mark Slee36bfa2e2007-01-19 20:09:51 +0000169%type<tstruct> Throws
170%type<tservice> Extends
171%type<tbool> Async
172%type<tbool> XsdAll
173%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000174%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000175%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000176%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000177
David Reisscdffe262007-08-14 17:12:31 +0000178%type<dtext> DocTextOptional
ccheeverf53b5cf2007-02-05 20:33:11 +0000179
Mark Slee31985722006-05-24 21:45:31 +0000180%%
181
Mark Sleef5377b32006-10-10 01:42:59 +0000182/**
183 * Thrift Grammar Implementation.
184 *
185 * For the most part this source file works its way top down from what you
186 * might expect to find in a typical .thrift file, i.e. type definitions and
187 * namespaces up top followed by service definitions using those types.
188 */
Mark Slee31985722006-05-24 21:45:31 +0000189
190Program:
David Reissc2532a92007-07-30 23:46:11 +0000191 DocTextOptional HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000192 {
193 pdebug("Program -> Headers DefinitionList");
David Reissc2532a92007-07-30 23:46:11 +0000194 if ($1 != NULL) {
195 g_program->set_doc($1);
196 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000197 }
198
199HeaderList:
200 HeaderList Header
201 {
202 pdebug("HeaderList -> HeaderList Header");
203 }
204|
205 {
206 pdebug("HeaderList -> ");
207 }
208
209Header:
210 Include
211 {
212 pdebug("Header -> Include");
213 }
214| tok_namespace tok_identifier
215 {
216 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
217 if (g_parse_mode == PROGRAM) {
218 g_program->set_cpp_namespace($2);
219 g_program->set_java_package($2);
220 }
221 }
222| tok_cpp_namespace tok_identifier
223 {
224 pdebug("Header -> tok_cpp_namespace tok_identifier");
225 if (g_parse_mode == PROGRAM) {
226 g_program->set_cpp_namespace($2);
227 }
228 }
229| tok_cpp_include tok_literal
230 {
231 pdebug("Header -> tok_cpp_include tok_literal");
232 if (g_parse_mode == PROGRAM) {
233 g_program->add_cpp_include($2);
234 }
235 }
Mark Sleee888b372007-01-12 01:06:24 +0000236| tok_php_namespace tok_identifier
237 {
238 pdebug("Header -> tok_php_namespace tok_identifier");
239 if (g_parse_mode == PROGRAM) {
240 g_program->set_php_namespace($2);
241 }
242 }
Mark Slee58dfb4f2007-07-06 02:45:25 +0000243| tok_ruby_namespace tok_identifier
244 {
245 pdebug("Header -> tok_ruby_namespace tok_identifier");
246 if (g_parse_mode == PROGRAM) {
247 g_program->set_ruby_namespace($2);
248 }
249 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000250| tok_java_package tok_identifier
251 {
252 pdebug("Header -> tok_java_package tok_identifier");
253 if (g_parse_mode == PROGRAM) {
254 g_program->set_java_package($2);
255 }
256 }
Mark Slee0d9199e2007-01-31 02:08:30 +0000257| tok_xsd_namespace tok_literal
258 {
259 pdebug("Header -> tok_xsd_namespace tok_literal");
260 if (g_parse_mode == PROGRAM) {
261 g_program->set_xsd_namespace($2);
262 }
263 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000264
265Include:
266 tok_include tok_literal
267 {
268 pdebug("Include -> tok_include tok_literal");
269 if (g_parse_mode == INCLUDES) {
270 std::string path = include_file(std::string($2));
271 if (!path.empty()) {
272 g_program->add_include(path);
273 }
274 }
275 }
Mark Slee31985722006-05-24 21:45:31 +0000276
277DefinitionList:
David Reisscdffe262007-08-14 17:12:31 +0000278 DefinitionList DocTextOptional Definition
Mark Slee31985722006-05-24 21:45:31 +0000279 {
280 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000281 if ($2 != NULL && $3 != NULL) {
282 $3->set_doc($2);
283 }
Mark Slee31985722006-05-24 21:45:31 +0000284 }
285|
286 {
287 pdebug("DefinitionList -> ");
288 }
289
290Definition:
Mark Slee30152872006-11-28 01:24:07 +0000291 Const
292 {
293 pdebug("Definition -> Const");
294 if (g_parse_mode == PROGRAM) {
295 g_program->add_const($1);
296 }
David Reisscdffe262007-08-14 17:12:31 +0000297 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000298 }
299| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000300 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000301 pdebug("Definition -> TypeDefinition");
302 if (g_parse_mode == PROGRAM) {
303 g_scope->add_type($1->get_name(), $1);
304 if (g_parent_scope != NULL) {
305 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
306 }
307 }
David Reisscdffe262007-08-14 17:12:31 +0000308 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000309 }
Mark Slee31985722006-05-24 21:45:31 +0000310| Service
311 {
312 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000313 if (g_parse_mode == PROGRAM) {
314 g_scope->add_service($1->get_name(), $1);
315 if (g_parent_scope != NULL) {
316 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
317 }
318 g_program->add_service($1);
319 }
David Reisscdffe262007-08-14 17:12:31 +0000320 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000321 }
322
Mark Sleef0712dc2006-10-25 19:03:57 +0000323TypeDefinition:
324 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000325 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000326 pdebug("TypeDefinition -> Typedef");
327 if (g_parse_mode == PROGRAM) {
328 g_program->add_typedef($1);
329 }
330 }
331| Enum
332 {
333 pdebug("TypeDefinition -> Enum");
334 if (g_parse_mode == PROGRAM) {
335 g_program->add_enum($1);
336 }
337 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000338| Senum
339 {
340 pdebug("TypeDefinition -> Senum");
341 if (g_parse_mode == PROGRAM) {
342 g_program->add_typedef($1);
343 }
344 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000345| Struct
346 {
347 pdebug("TypeDefinition -> Struct");
348 if (g_parse_mode == PROGRAM) {
349 g_program->add_struct($1);
350 }
351 }
352| Xception
353 {
354 pdebug("TypeDefinition -> Xception");
355 if (g_parse_mode == PROGRAM) {
356 g_program->add_xception($1);
357 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000358 }
Mark Slee31985722006-05-24 21:45:31 +0000359
360Typedef:
David Reisscdffe262007-08-14 17:12:31 +0000361 tok_typedef DefinitionType tok_identifier
Mark Slee31985722006-05-24 21:45:31 +0000362 {
363 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
David Reisscdffe262007-08-14 17:12:31 +0000364 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000365 $$ = td;
366 }
367
ccheeverf53b5cf2007-02-05 20:33:11 +0000368DocTextOptional:
369 tok_doctext
370 {
371 pdebug("DocTextOptional -> tok_doctext");
David Reiss1ac05802007-07-30 22:00:27 +0000372 $$ = clean_up_doctext($1);
ccheeverf53b5cf2007-02-05 20:33:11 +0000373 }
374|
375 {
376 $$ = NULL;
377 }
378
Mark Slee6a47fed2007-02-07 02:40:59 +0000379CommaOrSemicolonOptional:
380 ','
381 {}
382| ';'
383 {}
384|
385 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000386
Mark Slee31985722006-05-24 21:45:31 +0000387Enum:
David Reisscdffe262007-08-14 17:12:31 +0000388 tok_enum tok_identifier '{' EnumDefList '}'
Mark Slee31985722006-05-24 21:45:31 +0000389 {
390 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000391 $$ = $4;
392 $$->set_name($2);
Mark Slee31985722006-05-24 21:45:31 +0000393 }
394
395EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000396 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000397 {
398 pdebug("EnumDefList -> EnumDefList EnumDef");
399 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000400 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000401 }
402|
403 {
404 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000405 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000406 }
407
408EnumDef:
ccheeverf53b5cf2007-02-05 20:33:11 +0000409 DocTextOptional tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000410 {
Mark Slee30152872006-11-28 01:24:07 +0000411 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000412 if ($4 < 0) {
413 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000414 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000415 $$ = new t_enum_value($2, $4);
416 if ($1 != NULL) {
417 $$->set_doc($1);
418 }
Mark Sleed0767c52007-07-27 22:14:41 +0000419 if (g_parse_mode == PROGRAM) {
420 g_scope->add_constant($2, new t_const(g_type_i32, $2, new t_const_value($4)));
421 if (g_parent_scope != NULL) {
422 g_parent_scope->add_constant(g_parent_prefix + $2, new t_const(g_type_i32, $2, new t_const_value($4)));
423 }
424 }
Mark Slee31985722006-05-24 21:45:31 +0000425 }
426|
ccheeverf53b5cf2007-02-05 20:33:11 +0000427 DocTextOptional tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000428 {
Mark Slee30152872006-11-28 01:24:07 +0000429 pdebug("EnumDef -> tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000430 $$ = new t_enum_value($2);
431 if ($1 != NULL) {
432 $$->set_doc($1);
433 }
Mark Slee30152872006-11-28 01:24:07 +0000434 }
435
Mark Slee6a47fed2007-02-07 02:40:59 +0000436Senum:
David Reisscdffe262007-08-14 17:12:31 +0000437 tok_senum tok_identifier '{' SenumDefList '}'
Mark Slee6a47fed2007-02-07 02:40:59 +0000438 {
439 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000440 $$ = new t_typedef(g_program, $4, $2);
Mark Slee6a47fed2007-02-07 02:40:59 +0000441 }
442
443SenumDefList:
444 SenumDefList SenumDef
445 {
446 pdebug("SenumDefList -> SenumDefList SenumDef");
447 $$ = $1;
448 $$->add_string_enum_val($2);
449 }
450|
451 {
452 pdebug("SenumDefList -> ");
453 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
454 $$->set_string_enum(true);
455 }
456
457SenumDef:
458 tok_literal CommaOrSemicolonOptional
459 {
460 pdebug("SenumDef -> tok_literal");
461 $$ = $1;
462 }
463
Mark Slee30152872006-11-28 01:24:07 +0000464Const:
465 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
466 {
467 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000468 if (g_parse_mode == PROGRAM) {
469 $$ = new t_const($2, $3, $5);
470 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000471
472 g_scope->add_constant($3, $$);
473 if (g_parent_scope != NULL) {
474 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
475 }
476
Mark Sleeaa7671d2006-11-29 03:19:31 +0000477 } else {
478 $$ = NULL;
479 }
Mark Slee30152872006-11-28 01:24:07 +0000480 }
481
482ConstValue:
483 tok_int_constant
484 {
485 pdebug("ConstValue => tok_int_constant");
486 $$ = new t_const_value();
487 $$->set_integer($1);
488 }
489| tok_dub_constant
490 {
491 pdebug("ConstValue => tok_dub_constant");
492 $$ = new t_const_value();
493 $$->set_double($1);
494 }
495| tok_literal
496 {
497 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000498 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000499 }
Mark Slee67fc6342006-11-29 03:37:04 +0000500| tok_identifier
501 {
502 pdebug("ConstValue => tok_identifier");
Mark Sleed0767c52007-07-27 22:14:41 +0000503 t_const* constant = g_scope->get_constant($1);
504 if (constant != NULL) {
505 $$ = constant->get_value();
506 } else {
507 if (g_parse_mode == PROGRAM) {
508 pwarning(1, "Constant strings should be quoted: %s\n", $1);
509 }
510 $$ = new t_const_value($1);
511 }
Mark Slee67fc6342006-11-29 03:37:04 +0000512 }
Mark Slee30152872006-11-28 01:24:07 +0000513| ConstList
514 {
515 pdebug("ConstValue => ConstList");
516 $$ = $1;
517 }
518| ConstMap
519 {
520 pdebug("ConstValue => ConstMap");
521 $$ = $1;
522 }
523
524ConstList:
525 '[' ConstListContents ']'
526 {
527 pdebug("ConstList => [ ConstListContents ]");
528 $$ = $2;
529 }
530
531ConstListContents:
532 ConstListContents ConstValue CommaOrSemicolonOptional
533 {
534 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
535 $$ = $1;
536 $$->add_list($2);
537 }
538|
539 {
540 pdebug("ConstListContents =>");
541 $$ = new t_const_value();
542 $$->set_list();
543 }
544
545ConstMap:
546 '{' ConstMapContents '}'
547 {
548 pdebug("ConstMap => { ConstMapContents }");
549 $$ = $2;
550 }
551
552ConstMapContents:
553 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
554 {
555 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
556 $$ = $1;
557 $$->add_map($2, $4);
558 }
559|
560 {
561 pdebug("ConstMapContents =>");
562 $$ = new t_const_value();
563 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000564 }
565
566Struct:
David Reisscdffe262007-08-14 17:12:31 +0000567 tok_struct tok_identifier XsdAll '{' FieldList '}'
Mark Slee31985722006-05-24 21:45:31 +0000568 {
569 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
David Reisscdffe262007-08-14 17:12:31 +0000570 $5->set_name($2);
571 $5->set_xsd_all($3);
572 $$ = $5;
Mark Slee9cb7c612006-09-01 22:17:45 +0000573 y_field_val = -1;
574 }
575
Mark Slee36bfa2e2007-01-19 20:09:51 +0000576XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000577 tok_xsd_all
578 {
579 $$ = true;
580 }
581|
582 {
583 $$ = false;
584 }
585
Mark Slee36bfa2e2007-01-19 20:09:51 +0000586XsdOptional:
587 tok_xsd_optional
588 {
589 $$ = true;
590 }
591|
592 {
593 $$ = false;
594 }
595
Mark Slee7df0e2a2007-02-06 21:03:18 +0000596XsdNillable:
597 tok_xsd_nillable
598 {
599 $$ = true;
600 }
601|
602 {
603 $$ = false;
604 }
605
Mark Slee21135c32007-02-05 21:52:08 +0000606XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000607 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000608 {
Mark Slee748d83f2007-02-07 01:20:08 +0000609 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000610 }
611|
612 {
613 $$ = NULL;
614 }
615
Mark Slee9cb7c612006-09-01 22:17:45 +0000616Xception:
617 tok_xception tok_identifier '{' FieldList '}'
618 {
619 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
620 $4->set_name($2);
621 $4->set_xception(true);
622 $$ = $4;
623 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000624 }
625
626Service:
David Reisscdffe262007-08-14 17:12:31 +0000627 tok_service tok_identifier Extends '{' FunctionList '}'
Mark Slee31985722006-05-24 21:45:31 +0000628 {
629 pdebug("Service -> tok_service tok_identifier { FunctionList }");
David Reisscdffe262007-08-14 17:12:31 +0000630 $$ = $5;
631 $$->set_name($2);
632 $$->set_extends($3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000633 }
634
Mark Slee36bfa2e2007-01-19 20:09:51 +0000635Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000636 tok_extends tok_identifier
637 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000638 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000639 $$ = NULL;
640 if (g_parse_mode == PROGRAM) {
641 $$ = g_scope->get_service($2);
642 if ($$ == NULL) {
643 yyerror("Service \"%s\" has not been defined.", $2);
644 exit(1);
645 }
646 }
647 }
648|
649 {
650 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000651 }
652
653FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000654 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000655 {
656 pdebug("FunctionList -> FunctionList Function");
657 $$ = $1;
658 $1->add_function($2);
659 }
660|
661 {
662 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000663 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000664 }
665
666Function:
ccheeverf53b5cf2007-02-05 20:33:11 +0000667 DocTextOptional Async FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000668 {
ccheeverf53b5cf2007-02-05 20:33:11 +0000669 $6->set_name(std::string($4) + "_args");
670 $$ = new t_function($3, $4, $6, $8, $2);
671 if ($1 != NULL) {
672 $$->set_doc($1);
673 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000674 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000675 }
676
Mark Slee36bfa2e2007-01-19 20:09:51 +0000677Async:
Mark Slee52f643d2006-08-09 00:03:43 +0000678 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000679 {
Mark Slee52f643d2006-08-09 00:03:43 +0000680 $$ = true;
681 }
682|
683 {
684 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000685 }
686
Mark Slee36bfa2e2007-01-19 20:09:51 +0000687Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000688 tok_throws '(' FieldList ')'
689 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000690 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000691 $$ = $3;
692 }
693|
694 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000695 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000696 }
697
Mark Slee31985722006-05-24 21:45:31 +0000698FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000699 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000700 {
701 pdebug("FieldList -> FieldList , Field");
702 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000703 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000704 }
705|
706 {
707 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000708 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000709 }
710
711Field:
Mark Slee7df0e2a2007-02-06 21:03:18 +0000712 DocTextOptional FieldIdentifier FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000713 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000714 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000715 if ($2 < 0) {
Mark Slee21135c32007-02-05 21:52:08 +0000716 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 +0000717 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000718 $$ = new t_field($3, $4, $2);
719 if ($5 != NULL) {
720 validate_field_value($$, $5);
721 $$->set_value($5);
Mark Slee7ff32452007-02-01 05:26:18 +0000722 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000723 $$->set_xsd_optional($6);
Mark Slee7df0e2a2007-02-06 21:03:18 +0000724 $$->set_xsd_nillable($7);
ccheeverf53b5cf2007-02-05 20:33:11 +0000725 if ($1 != NULL) {
726 $$->set_doc($1);
727 }
Mark Slee7df0e2a2007-02-06 21:03:18 +0000728 if ($8 != NULL) {
Mark Slee748d83f2007-02-07 01:20:08 +0000729 $$->set_xsd_attrs($8);
Mark Slee21135c32007-02-05 21:52:08 +0000730 }
Mark Slee31985722006-05-24 21:45:31 +0000731 }
Mark Slee7ff32452007-02-01 05:26:18 +0000732
733FieldIdentifier:
734 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000735 {
Mark Slee7ff32452007-02-01 05:26:18 +0000736 if ($1 <= 0) {
737 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n", $1);
738 $1 = y_field_val--;
Mark Sleef0712dc2006-10-25 19:03:57 +0000739 }
Mark Slee7ff32452007-02-01 05:26:18 +0000740 $$ = $1;
741 }
742|
743 {
744 $$ = y_field_val--;
745 }
746
747FieldValue:
748 '=' ConstValue
749 {
750 if (g_parse_mode == PROGRAM) {
751 $$ = $2;
752 } else {
753 $$ = NULL;
754 }
755 }
756|
757 {
758 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000759 }
Mark Slee31985722006-05-24 21:45:31 +0000760
761DefinitionType:
762 BaseType
763 {
764 pdebug("DefinitionType -> BaseType");
765 $$ = $1;
766 }
Mark Sleee8540632006-05-30 09:24:40 +0000767| ContainerType
768 {
769 pdebug("DefinitionType -> ContainerType");
770 $$ = $1;
771 }
Mark Slee31985722006-05-24 21:45:31 +0000772
773FunctionType:
774 FieldType
775 {
Mark Sleee8540632006-05-30 09:24:40 +0000776 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000777 $$ = $1;
778 }
779| tok_void
780 {
Mark Sleee8540632006-05-30 09:24:40 +0000781 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000782 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000783 }
784
785FieldType:
786 tok_identifier
787 {
Mark Sleee8540632006-05-30 09:24:40 +0000788 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000789 if (g_parse_mode == INCLUDES) {
790 // Ignore identifiers in include mode
791 $$ = NULL;
792 } else {
793 // Lookup the identifier in the current scope
794 $$ = g_scope->get_type($1);
795 if ($$ == NULL) {
796 yyerror("Type \"%s\" has not been defined.", $1);
797 exit(1);
798 }
Mark Sleee8540632006-05-30 09:24:40 +0000799 }
Mark Slee31985722006-05-24 21:45:31 +0000800 }
801| BaseType
802 {
Mark Sleee8540632006-05-30 09:24:40 +0000803 pdebug("FieldType -> BaseType");
804 $$ = $1;
805 }
806| ContainerType
807 {
808 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000809 $$ = $1;
810 }
811
812BaseType:
813 tok_string
814 {
815 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000816 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000817 }
Mark Slee8d725a22007-04-13 01:57:12 +0000818| tok_binary
819 {
820 pdebug("BaseType -> tok_binary");
821 $$ = g_type_binary;
822 }
Mark Sleeb6200d82007-01-19 19:14:36 +0000823| tok_slist
824 {
825 pdebug("BaseType -> tok_slist");
826 $$ = g_type_slist;
827 }
Mark Slee78f58e22006-09-02 04:17:07 +0000828| tok_bool
829 {
830 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000831 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000832 }
Mark Slee31985722006-05-24 21:45:31 +0000833| tok_byte
834 {
835 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000836 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000837 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000838| tok_i16
839 {
840 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000841 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000842 }
Mark Slee31985722006-05-24 21:45:31 +0000843| tok_i32
844 {
845 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000846 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000847 }
Mark Slee31985722006-05-24 21:45:31 +0000848| tok_i64
849 {
850 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000851 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000852 }
Mark Sleec98d0502006-09-06 02:42:25 +0000853| tok_double
854 {
855 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000856 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000857 }
Mark Slee31985722006-05-24 21:45:31 +0000858
Mark Sleee8540632006-05-30 09:24:40 +0000859ContainerType:
860 MapType
861 {
862 pdebug("ContainerType -> MapType");
863 $$ = $1;
864 }
865| SetType
866 {
867 pdebug("ContainerType -> SetType");
868 $$ = $1;
869 }
870| ListType
871 {
872 pdebug("ContainerType -> ListType");
873 $$ = $1;
874 }
875
876MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000877 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000878 {
879 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000880 $$ = new t_map($4, $6);
881 if ($2 != NULL) {
882 ((t_container*)$$)->set_cpp_name(std::string($2));
883 }
Mark Sleee8540632006-05-30 09:24:40 +0000884 }
885
886SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000887 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000888 {
889 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000890 $$ = new t_set($4);
891 if ($2 != NULL) {
892 ((t_container*)$$)->set_cpp_name(std::string($2));
893 }
Mark Sleee8540632006-05-30 09:24:40 +0000894 }
895
896ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000897 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +0000898 {
899 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +0000900 $$ = new t_list($3);
901 if ($5 != NULL) {
902 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +0000903 }
904 }
905
Mark Slee36bfa2e2007-01-19 20:09:51 +0000906CppType:
Mark Sleeafc76542007-02-09 21:55:44 +0000907 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +0000908 {
Mark Sleeafc76542007-02-09 21:55:44 +0000909 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +0000910 }
911|
912 {
913 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +0000914 }
915
Mark Slee31985722006-05-24 21:45:31 +0000916%%