blob: 5be19b36ba08b28b2f2a8bebc7aa9d00ddd1b29b [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 Slee78165722007-09-10 22:08:49 +000028int g_arglist = 0;
Mark Slee31985722006-05-24 21:45:31 +000029
30%}
31
Mark Sleef5377b32006-10-10 01:42:59 +000032/**
33 * This structure is used by the parser to hold the data types associated with
34 * various parse nodes.
35 */
Mark Slee31985722006-05-24 21:45:31 +000036%union {
Mark Slee30152872006-11-28 01:24:07 +000037 char* id;
38 int iconst;
39 double dconst;
40 bool tbool;
David Reisscdffe262007-08-14 17:12:31 +000041 t_doc* tdoc;
Mark Slee30152872006-11-28 01:24:07 +000042 t_type* ttype;
Mark Slee6a47fed2007-02-07 02:40:59 +000043 t_base_type* tbase;
Mark Slee30152872006-11-28 01:24:07 +000044 t_typedef* ttypedef;
45 t_enum* tenum;
46 t_enum_value* tenumv;
47 t_const* tconst;
48 t_const_value* tconstv;
49 t_struct* tstruct;
50 t_service* tservice;
51 t_function* tfunction;
52 t_field* tfield;
David Reisscdffe262007-08-14 17:12:31 +000053 char* dtext;
David Reiss8320a922007-08-14 19:59:26 +000054 t_field::e_req ereq;
Mark Slee31985722006-05-24 21:45:31 +000055}
56
Mark Sleef5377b32006-10-10 01:42:59 +000057/**
58 * Strings identifier
59 */
Mark Slee31985722006-05-24 21:45:31 +000060%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +000061%token<id> tok_literal
David Reisscdffe262007-08-14 17:12:31 +000062%token<dtext> tok_doctext
Mark Sleebd588222007-11-21 08:43:35 +000063%token<id> tok_st_identifier
Mark Sleef5377b32006-10-10 01:42:59 +000064
65/**
Mark Slee30152872006-11-28 01:24:07 +000066 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +000067 */
Mark Slee31985722006-05-24 21:45:31 +000068%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +000069%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +000070
Mark Sleef5377b32006-10-10 01:42:59 +000071/**
Mark Sleef0712dc2006-10-25 19:03:57 +000072 * Header keywoards
Mark Sleef5377b32006-10-10 01:42:59 +000073 */
Mark Sleef0712dc2006-10-25 19:03:57 +000074%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +000075%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000076%token tok_cpp_namespace
77%token tok_cpp_include
78%token tok_cpp_type
Mark Sleee888b372007-01-12 01:06:24 +000079%token tok_php_namespace
David Reissc6fc3292007-08-30 00:58:43 +000080%token tok_py_module
Mark Slee27ed6ec2007-08-16 01:26:31 +000081%token tok_perl_package
Mark Sleef0712dc2006-10-25 19:03:57 +000082%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +000083%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +000084%token tok_xsd_optional
Mark Slee7df0e2a2007-02-06 21:03:18 +000085%token tok_xsd_nillable
Mark Slee0d9199e2007-01-31 02:08:30 +000086%token tok_xsd_namespace
Mark Slee21135c32007-02-05 21:52:08 +000087%token tok_xsd_attrs
Mark Slee58dfb4f2007-07-06 02:45:25 +000088%token tok_ruby_namespace
Mark Sleebd588222007-11-21 08:43:35 +000089%token tok_smalltalk_category
David Reiss15457c92007-12-14 07:03:03 +000090%token tok_smalltalk_prefix
Mark Slee7e9eea42007-09-10 21:00:23 +000091%token tok_cocoa_prefix
David Reiss7f42bcf2008-01-11 20:59:12 +000092%token tok_csharp_namespace
Mark Slee9cb7c612006-09-01 22:17:45 +000093
Mark Sleef5377b32006-10-10 01:42:59 +000094/**
95 * Base datatype keywords
96 */
97%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +000098%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +000099%token tok_byte
100%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +0000101%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +0000102%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +0000103%token tok_senum
Mark Slee9cb7c612006-09-01 22:17:45 +0000104%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +0000105%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +0000106%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +0000107%token tok_double
Mark Slee31985722006-05-24 21:45:31 +0000108
Mark Sleef5377b32006-10-10 01:42:59 +0000109/**
110 * Complex type keywords
111 */
Mark Slee31985722006-05-24 21:45:31 +0000112%token tok_map
113%token tok_list
114%token tok_set
115
Mark Sleef5377b32006-10-10 01:42:59 +0000116/**
117 * Function modifiers
Mark Slee27ed6ec2007-08-16 01:26:31 +0000118 */
Mark Slee31985722006-05-24 21:45:31 +0000119%token tok_async
120
Mark Sleef5377b32006-10-10 01:42:59 +0000121/**
122 * Thrift language keywords
123 */
Mark Slee31985722006-05-24 21:45:31 +0000124%token tok_typedef
125%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000126%token tok_xception
127%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000128%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000129%token tok_service
130%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000131%token tok_const
David Reiss8320a922007-08-14 19:59:26 +0000132%token tok_required
133%token tok_optional
Mark Slee31985722006-05-24 21:45:31 +0000134
Mark Sleef5377b32006-10-10 01:42:59 +0000135/**
136 * Grammar nodes
137 */
138
Mark Slee31985722006-05-24 21:45:31 +0000139%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000140%type<ttype> ContainerType
141%type<ttype> MapType
142%type<ttype> SetType
143%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000144
David Reisscdffe262007-08-14 17:12:31 +0000145%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000146%type<ttype> TypeDefinition
147
Mark Slee31985722006-05-24 21:45:31 +0000148%type<ttypedef> Typedef
149%type<ttype> DefinitionType
150
151%type<tfield> Field
Mark Slee7ff32452007-02-01 05:26:18 +0000152%type<iconst> FieldIdentifier
David Reiss8320a922007-08-14 19:59:26 +0000153%type<ereq> FieldRequiredness
Mark Slee31985722006-05-24 21:45:31 +0000154%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000155%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000156%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000157
158%type<tenum> Enum
159%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000160%type<tenumv> EnumDef
161
Mark Slee6a47fed2007-02-07 02:40:59 +0000162%type<ttypedef> Senum
163%type<tbase> SenumDefList
164%type<id> SenumDef
165
Mark Slee30152872006-11-28 01:24:07 +0000166%type<tconst> Const
167%type<tconstv> ConstValue
168%type<tconstv> ConstList
169%type<tconstv> ConstListContents
170%type<tconstv> ConstMap
171%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000172
173%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000174%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000175%type<tservice> Service
176
177%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000178%type<ttype> FunctionType
179%type<tservice> FunctionList
180
Mark Slee36bfa2e2007-01-19 20:09:51 +0000181%type<tstruct> Throws
182%type<tservice> Extends
183%type<tbool> Async
184%type<tbool> XsdAll
185%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000186%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000187%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000188%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000189
David Reisscbd4bac2007-08-14 17:12:33 +0000190%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000191
Mark Slee31985722006-05-24 21:45:31 +0000192%%
193
Mark Sleef5377b32006-10-10 01:42:59 +0000194/**
195 * Thrift Grammar Implementation.
196 *
197 * For the most part this source file works its way top down from what you
198 * might expect to find in a typical .thrift file, i.e. type definitions and
199 * namespaces up top followed by service definitions using those types.
200 */
Mark Slee31985722006-05-24 21:45:31 +0000201
202Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000203 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000204 {
205 pdebug("Program -> Headers DefinitionList");
David Reisscbd4bac2007-08-14 17:12:33 +0000206 /*
207 TODO(dreiss): Decide whether full-program doctext is worth the trouble.
David Reissc2532a92007-07-30 23:46:11 +0000208 if ($1 != NULL) {
209 g_program->set_doc($1);
210 }
David Reisscbd4bac2007-08-14 17:12:33 +0000211 */
212 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000213 }
214
David Reisscbd4bac2007-08-14 17:12:33 +0000215CaptureDocText:
216 {
217 if (g_parse_mode == PROGRAM) {
Mark Sleebd588222007-11-21 08:43:35 +0000218 $$ = g_doctext;
David Reisscbd4bac2007-08-14 17:12:33 +0000219 g_doctext = NULL;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000220 } else {
David Reisscbd4bac2007-08-14 17:12:33 +0000221 $$ = NULL;
222 }
223 }
224
225/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
226DestroyDocText:
227 {
228 if (g_parse_mode == PROGRAM) {
229 clear_doctext();
230 }
231 }
232
233/* We have to DestroyDocText here, otherwise it catches the doctext
234 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000235HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000236 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000237 {
238 pdebug("HeaderList -> HeaderList Header");
239 }
240|
241 {
242 pdebug("HeaderList -> ");
243 }
244
245Header:
246 Include
247 {
248 pdebug("Header -> Include");
249 }
250| tok_namespace tok_identifier
251 {
252 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
253 if (g_parse_mode == PROGRAM) {
254 g_program->set_cpp_namespace($2);
255 g_program->set_java_package($2);
256 }
257 }
258| tok_cpp_namespace tok_identifier
259 {
260 pdebug("Header -> tok_cpp_namespace tok_identifier");
261 if (g_parse_mode == PROGRAM) {
262 g_program->set_cpp_namespace($2);
263 }
264 }
265| tok_cpp_include tok_literal
266 {
267 pdebug("Header -> tok_cpp_include tok_literal");
268 if (g_parse_mode == PROGRAM) {
269 g_program->add_cpp_include($2);
270 }
271 }
Mark Sleee888b372007-01-12 01:06:24 +0000272| tok_php_namespace tok_identifier
273 {
274 pdebug("Header -> tok_php_namespace tok_identifier");
275 if (g_parse_mode == PROGRAM) {
276 g_program->set_php_namespace($2);
277 }
278 }
David Reissc6fc3292007-08-30 00:58:43 +0000279| tok_py_module tok_identifier
280 {
281 pdebug("Header -> tok_py_module tok_identifier");
282 if (g_parse_mode == PROGRAM) {
283 g_program->set_py_module($2);
284 }
285 }
Mark Slee27ed6ec2007-08-16 01:26:31 +0000286| tok_perl_package tok_identifier
287 {
288 pdebug("Header -> tok_perl_namespace tok_identifier");
289 if (g_parse_mode == PROGRAM) {
290 g_program->set_perl_package($2);
291 }
292 }
Mark Slee58dfb4f2007-07-06 02:45:25 +0000293| tok_ruby_namespace tok_identifier
294 {
295 pdebug("Header -> tok_ruby_namespace tok_identifier");
296 if (g_parse_mode == PROGRAM) {
297 g_program->set_ruby_namespace($2);
298 }
299 }
Mark Sleebd588222007-11-21 08:43:35 +0000300| tok_smalltalk_category tok_st_identifier
301 {
302 pdebug("Header -> tok_smalltalk_category tok_st_identifier");
303 if (g_parse_mode == PROGRAM) {
304 g_program->set_smalltalk_category($2);
305 }
306 }
David Reiss15457c92007-12-14 07:03:03 +0000307| tok_smalltalk_prefix tok_identifier
308 {
309 pdebug("Header -> tok_smalltalk_prefix tok_identifier");
310 if (g_parse_mode == PROGRAM) {
311 g_program->set_smalltalk_prefix($2);
312 }
313 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000314| tok_java_package tok_identifier
315 {
316 pdebug("Header -> tok_java_package tok_identifier");
317 if (g_parse_mode == PROGRAM) {
318 g_program->set_java_package($2);
319 }
320 }
Mark Slee7e9eea42007-09-10 21:00:23 +0000321| tok_cocoa_prefix tok_identifier
322 {
323 pdebug("Header -> tok_cocoa_prefix tok_identifier");
324 if (g_parse_mode == PROGRAM) {
325 g_program->set_cocoa_prefix($2);
326 }
327 }
Mark Slee0d9199e2007-01-31 02:08:30 +0000328| tok_xsd_namespace tok_literal
329 {
330 pdebug("Header -> tok_xsd_namespace tok_literal");
331 if (g_parse_mode == PROGRAM) {
332 g_program->set_xsd_namespace($2);
333 }
334 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000335| tok_csharp_namespace tok_identifier
336 {
337 pdebug("Header -> tok_csharp_package tok_identifier");
338 if (g_parse_mode == PROGRAM) {
339 g_program->set_csharp_namespace($2);
340 }
341 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000342
343Include:
344 tok_include tok_literal
345 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000346 pdebug("Include -> tok_include tok_literal");
Mark Sleef0712dc2006-10-25 19:03:57 +0000347 if (g_parse_mode == INCLUDES) {
348 std::string path = include_file(std::string($2));
349 if (!path.empty()) {
350 g_program->add_include(path);
351 }
352 }
353 }
Mark Slee31985722006-05-24 21:45:31 +0000354
355DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000356 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000357 {
358 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000359 if ($2 != NULL && $3 != NULL) {
360 $3->set_doc($2);
361 }
Mark Slee31985722006-05-24 21:45:31 +0000362 }
363|
364 {
365 pdebug("DefinitionList -> ");
366 }
367
368Definition:
Mark Slee30152872006-11-28 01:24:07 +0000369 Const
370 {
371 pdebug("Definition -> Const");
372 if (g_parse_mode == PROGRAM) {
373 g_program->add_const($1);
Mark Sleebd588222007-11-21 08:43:35 +0000374 }
David Reisscdffe262007-08-14 17:12:31 +0000375 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000376 }
377| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000378 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000379 pdebug("Definition -> TypeDefinition");
380 if (g_parse_mode == PROGRAM) {
381 g_scope->add_type($1->get_name(), $1);
382 if (g_parent_scope != NULL) {
383 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
384 }
385 }
David Reisscdffe262007-08-14 17:12:31 +0000386 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000387 }
Mark Slee31985722006-05-24 21:45:31 +0000388| Service
389 {
390 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000391 if (g_parse_mode == PROGRAM) {
392 g_scope->add_service($1->get_name(), $1);
393 if (g_parent_scope != NULL) {
394 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
395 }
396 g_program->add_service($1);
397 }
David Reisscdffe262007-08-14 17:12:31 +0000398 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000399 }
400
Mark Sleef0712dc2006-10-25 19:03:57 +0000401TypeDefinition:
402 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000403 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000404 pdebug("TypeDefinition -> Typedef");
405 if (g_parse_mode == PROGRAM) {
406 g_program->add_typedef($1);
407 }
408 }
409| Enum
410 {
411 pdebug("TypeDefinition -> Enum");
412 if (g_parse_mode == PROGRAM) {
413 g_program->add_enum($1);
414 }
415 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000416| Senum
417 {
418 pdebug("TypeDefinition -> Senum");
419 if (g_parse_mode == PROGRAM) {
420 g_program->add_typedef($1);
421 }
422 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000423| Struct
424 {
425 pdebug("TypeDefinition -> Struct");
426 if (g_parse_mode == PROGRAM) {
427 g_program->add_struct($1);
428 }
429 }
430| Xception
Mark Slee27ed6ec2007-08-16 01:26:31 +0000431 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000432 pdebug("TypeDefinition -> Xception");
433 if (g_parse_mode == PROGRAM) {
434 g_program->add_xception($1);
435 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000436 }
Mark Slee31985722006-05-24 21:45:31 +0000437
438Typedef:
Mark Sleebd588222007-11-21 08:43:35 +0000439 tok_typedef DefinitionType tok_identifier
Mark Slee31985722006-05-24 21:45:31 +0000440 {
441 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
David Reisscdffe262007-08-14 17:12:31 +0000442 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000443 $$ = td;
444 }
445
Mark Slee6a47fed2007-02-07 02:40:59 +0000446CommaOrSemicolonOptional:
447 ','
448 {}
449| ';'
450 {}
451|
452 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000453
Mark Slee31985722006-05-24 21:45:31 +0000454Enum:
David Reisscdffe262007-08-14 17:12:31 +0000455 tok_enum tok_identifier '{' EnumDefList '}'
Mark Slee31985722006-05-24 21:45:31 +0000456 {
457 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000458 $$ = $4;
459 $$->set_name($2);
Mark Slee31985722006-05-24 21:45:31 +0000460 }
461
462EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000463 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000464 {
465 pdebug("EnumDefList -> EnumDefList EnumDef");
466 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000467 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000468 }
469|
470 {
471 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000472 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000473 }
474
475EnumDef:
David Reisscbd4bac2007-08-14 17:12:33 +0000476 CaptureDocText tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000477 {
Mark Slee30152872006-11-28 01:24:07 +0000478 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000479 if ($4 < 0) {
480 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000481 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000482 $$ = new t_enum_value($2, $4);
483 if ($1 != NULL) {
484 $$->set_doc($1);
485 }
Mark Sleed0767c52007-07-27 22:14:41 +0000486 if (g_parse_mode == PROGRAM) {
487 g_scope->add_constant($2, new t_const(g_type_i32, $2, new t_const_value($4)));
488 if (g_parent_scope != NULL) {
489 g_parent_scope->add_constant(g_parent_prefix + $2, new t_const(g_type_i32, $2, new t_const_value($4)));
490 }
491 }
Mark Slee31985722006-05-24 21:45:31 +0000492 }
493|
David Reisscbd4bac2007-08-14 17:12:33 +0000494 CaptureDocText tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000495 {
Mark Slee30152872006-11-28 01:24:07 +0000496 pdebug("EnumDef -> tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000497 $$ = new t_enum_value($2);
498 if ($1 != NULL) {
499 $$->set_doc($1);
500 }
Mark Slee30152872006-11-28 01:24:07 +0000501 }
502
Mark Slee6a47fed2007-02-07 02:40:59 +0000503Senum:
David Reisscdffe262007-08-14 17:12:31 +0000504 tok_senum tok_identifier '{' SenumDefList '}'
Mark Slee6a47fed2007-02-07 02:40:59 +0000505 {
506 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000507 $$ = new t_typedef(g_program, $4, $2);
Mark Slee6a47fed2007-02-07 02:40:59 +0000508 }
509
510SenumDefList:
511 SenumDefList SenumDef
512 {
513 pdebug("SenumDefList -> SenumDefList SenumDef");
514 $$ = $1;
515 $$->add_string_enum_val($2);
516 }
517|
518 {
519 pdebug("SenumDefList -> ");
520 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
521 $$->set_string_enum(true);
522 }
523
524SenumDef:
525 tok_literal CommaOrSemicolonOptional
526 {
527 pdebug("SenumDef -> tok_literal");
528 $$ = $1;
529 }
530
Mark Slee30152872006-11-28 01:24:07 +0000531Const:
532 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
533 {
534 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000535 if (g_parse_mode == PROGRAM) {
536 $$ = new t_const($2, $3, $5);
537 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000538
539 g_scope->add_constant($3, $$);
540 if (g_parent_scope != NULL) {
541 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
542 }
543
Mark Sleeaa7671d2006-11-29 03:19:31 +0000544 } else {
545 $$ = NULL;
546 }
Mark Slee30152872006-11-28 01:24:07 +0000547 }
548
549ConstValue:
550 tok_int_constant
551 {
552 pdebug("ConstValue => tok_int_constant");
553 $$ = new t_const_value();
554 $$->set_integer($1);
555 }
556| tok_dub_constant
557 {
558 pdebug("ConstValue => tok_dub_constant");
559 $$ = new t_const_value();
560 $$->set_double($1);
561 }
562| tok_literal
563 {
564 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000565 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000566 }
Mark Slee67fc6342006-11-29 03:37:04 +0000567| tok_identifier
568 {
569 pdebug("ConstValue => tok_identifier");
Mark Sleed0767c52007-07-27 22:14:41 +0000570 t_const* constant = g_scope->get_constant($1);
571 if (constant != NULL) {
572 $$ = constant->get_value();
573 } else {
574 if (g_parse_mode == PROGRAM) {
575 pwarning(1, "Constant strings should be quoted: %s\n", $1);
576 }
577 $$ = new t_const_value($1);
578 }
Mark Slee67fc6342006-11-29 03:37:04 +0000579 }
Mark Slee30152872006-11-28 01:24:07 +0000580| ConstList
581 {
582 pdebug("ConstValue => ConstList");
583 $$ = $1;
584 }
585| ConstMap
586 {
587 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000588 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000589 }
590
591ConstList:
592 '[' ConstListContents ']'
593 {
594 pdebug("ConstList => [ ConstListContents ]");
595 $$ = $2;
596 }
597
598ConstListContents:
599 ConstListContents ConstValue CommaOrSemicolonOptional
600 {
601 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
602 $$ = $1;
603 $$->add_list($2);
604 }
605|
606 {
607 pdebug("ConstListContents =>");
608 $$ = new t_const_value();
609 $$->set_list();
610 }
611
612ConstMap:
613 '{' ConstMapContents '}'
614 {
615 pdebug("ConstMap => { ConstMapContents }");
616 $$ = $2;
617 }
618
619ConstMapContents:
620 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
621 {
622 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
623 $$ = $1;
624 $$->add_map($2, $4);
625 }
626|
627 {
628 pdebug("ConstMapContents =>");
629 $$ = new t_const_value();
630 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000631 }
632
633Struct:
David Reisscdffe262007-08-14 17:12:31 +0000634 tok_struct tok_identifier XsdAll '{' FieldList '}'
Mark Slee31985722006-05-24 21:45:31 +0000635 {
636 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
David Reisscdffe262007-08-14 17:12:31 +0000637 $5->set_name($2);
638 $5->set_xsd_all($3);
639 $$ = $5;
Mark Slee9cb7c612006-09-01 22:17:45 +0000640 y_field_val = -1;
641 }
642
Mark Slee36bfa2e2007-01-19 20:09:51 +0000643XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000644 tok_xsd_all
645 {
646 $$ = true;
647 }
648|
649 {
650 $$ = false;
651 }
652
Mark Slee36bfa2e2007-01-19 20:09:51 +0000653XsdOptional:
654 tok_xsd_optional
655 {
656 $$ = true;
657 }
658|
659 {
660 $$ = false;
661 }
662
Mark Slee7df0e2a2007-02-06 21:03:18 +0000663XsdNillable:
664 tok_xsd_nillable
665 {
666 $$ = true;
667 }
668|
669 {
670 $$ = false;
671 }
672
Mark Slee21135c32007-02-05 21:52:08 +0000673XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000674 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000675 {
Mark Slee748d83f2007-02-07 01:20:08 +0000676 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000677 }
678|
679 {
680 $$ = NULL;
681 }
682
Mark Slee9cb7c612006-09-01 22:17:45 +0000683Xception:
684 tok_xception tok_identifier '{' FieldList '}'
685 {
686 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
687 $4->set_name($2);
688 $4->set_xception(true);
689 $$ = $4;
690 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000691 }
692
693Service:
Mark Slee78165722007-09-10 22:08:49 +0000694 tok_service tok_identifier Extends '{' FlagArgs FunctionList UnflagArgs '}'
Mark Slee31985722006-05-24 21:45:31 +0000695 {
696 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Mark Slee78165722007-09-10 22:08:49 +0000697 $$ = $6;
David Reisscdffe262007-08-14 17:12:31 +0000698 $$->set_name($2);
699 $$->set_extends($3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000700 }
701
Mark Slee78165722007-09-10 22:08:49 +0000702FlagArgs:
703 {
704 g_arglist = 1;
705 }
706
707UnflagArgs:
708 {
709 g_arglist = 0;
710 }
711
Mark Slee36bfa2e2007-01-19 20:09:51 +0000712Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000713 tok_extends tok_identifier
714 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000715 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000716 $$ = NULL;
717 if (g_parse_mode == PROGRAM) {
718 $$ = g_scope->get_service($2);
719 if ($$ == NULL) {
720 yyerror("Service \"%s\" has not been defined.", $2);
721 exit(1);
722 }
723 }
724 }
725|
726 {
727 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000728 }
729
730FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000731 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000732 {
733 pdebug("FunctionList -> FunctionList Function");
734 $$ = $1;
735 $1->add_function($2);
736 }
737|
738 {
739 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000740 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000741 }
742
743Function:
David Reisscbd4bac2007-08-14 17:12:33 +0000744 CaptureDocText Async FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000745 {
ccheeverf53b5cf2007-02-05 20:33:11 +0000746 $6->set_name(std::string($4) + "_args");
747 $$ = new t_function($3, $4, $6, $8, $2);
748 if ($1 != NULL) {
749 $$->set_doc($1);
750 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000751 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000752 }
753
Mark Slee36bfa2e2007-01-19 20:09:51 +0000754Async:
Mark Slee52f643d2006-08-09 00:03:43 +0000755 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000756 {
Mark Slee52f643d2006-08-09 00:03:43 +0000757 $$ = true;
758 }
759|
760 {
761 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000762 }
763
Mark Slee36bfa2e2007-01-19 20:09:51 +0000764Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000765 tok_throws '(' FieldList ')'
766 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000767 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000768 $$ = $3;
769 }
770|
771 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000772 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000773 }
774
Mark Slee31985722006-05-24 21:45:31 +0000775FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000776 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000777 {
778 pdebug("FieldList -> FieldList , Field");
779 $$ = $1;
Mark Slee6f9ac3f2007-11-28 22:28:13 +0000780 if (!($$->validate_field($2))) {
781 yyerror("Field identifier %d for \"%s\" has already been used", $2->get_key(), $2->get_name().c_str());
782 exit(1);
783 }
Mark Slee207cb462006-11-02 18:43:12 +0000784 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000785 }
786|
787 {
788 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000789 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000790 }
791
792Field:
David Reiss8320a922007-08-14 19:59:26 +0000793 CaptureDocText FieldIdentifier FieldRequiredness FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000794 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000795 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000796 if ($2 < 0) {
David Reiss8320a922007-08-14 19:59:26 +0000797 pwarning(2, "No field key specified for %s, resulting protocol may have conflicts or not be backwards compatible!\n", $5);
Mark Slee31985722006-05-24 21:45:31 +0000798 }
David Reiss8320a922007-08-14 19:59:26 +0000799 $$ = new t_field($4, $5, $2);
800 $$->set_req($3);
801 if ($6 != NULL) {
802 validate_field_value($$, $6);
803 $$->set_value($6);
Mark Slee7ff32452007-02-01 05:26:18 +0000804 }
David Reiss8320a922007-08-14 19:59:26 +0000805 $$->set_xsd_optional($7);
806 $$->set_xsd_nillable($8);
ccheeverf53b5cf2007-02-05 20:33:11 +0000807 if ($1 != NULL) {
808 $$->set_doc($1);
809 }
David Reiss8320a922007-08-14 19:59:26 +0000810 if ($9 != NULL) {
811 $$->set_xsd_attrs($9);
Mark Slee21135c32007-02-05 21:52:08 +0000812 }
Mark Slee31985722006-05-24 21:45:31 +0000813 }
Mark Slee7ff32452007-02-01 05:26:18 +0000814
815FieldIdentifier:
816 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000817 {
Mark Slee7ff32452007-02-01 05:26:18 +0000818 if ($1 <= 0) {
819 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n", $1);
820 $1 = y_field_val--;
Mark Sleef0712dc2006-10-25 19:03:57 +0000821 }
Mark Slee7ff32452007-02-01 05:26:18 +0000822 $$ = $1;
823 }
824|
825 {
826 $$ = y_field_val--;
827 }
828
David Reiss8320a922007-08-14 19:59:26 +0000829FieldRequiredness:
830 tok_required
831 {
Mark Slee78165722007-09-10 22:08:49 +0000832 if (g_arglist) {
833 if (g_parse_mode == PROGRAM) {
834 pwarning(1, "required keyword is ignored in argument lists.\n");
835 }
David Reiss204420f2008-01-11 20:59:03 +0000836 $$ = t_field::T_OPT_IN_REQ_OUT;
Mark Slee78165722007-09-10 22:08:49 +0000837 } else {
David Reiss204420f2008-01-11 20:59:03 +0000838 $$ = t_field::T_REQUIRED;
Mark Slee78165722007-09-10 22:08:49 +0000839 }
David Reiss8320a922007-08-14 19:59:26 +0000840 }
841| tok_optional
842 {
Mark Slee78165722007-09-10 22:08:49 +0000843 if (g_arglist) {
844 if (g_parse_mode == PROGRAM) {
845 pwarning(1, "optional keyword is ignored in argument lists.\n");
846 }
David Reiss204420f2008-01-11 20:59:03 +0000847 $$ = t_field::T_OPT_IN_REQ_OUT;
Mark Slee78165722007-09-10 22:08:49 +0000848 } else {
David Reiss204420f2008-01-11 20:59:03 +0000849 $$ = t_field::T_OPTIONAL;
Mark Slee78165722007-09-10 22:08:49 +0000850 }
David Reiss8320a922007-08-14 19:59:26 +0000851 }
852|
853 {
David Reiss204420f2008-01-11 20:59:03 +0000854 $$ = t_field::T_OPT_IN_REQ_OUT;
David Reiss8320a922007-08-14 19:59:26 +0000855 }
856
Mark Slee7ff32452007-02-01 05:26:18 +0000857FieldValue:
858 '=' ConstValue
859 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000860 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +0000861 $$ = $2;
862 } else {
863 $$ = NULL;
864 }
865 }
866|
867 {
868 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000869 }
Mark Slee31985722006-05-24 21:45:31 +0000870
871DefinitionType:
872 BaseType
873 {
874 pdebug("DefinitionType -> BaseType");
875 $$ = $1;
876 }
Mark Sleee8540632006-05-30 09:24:40 +0000877| ContainerType
878 {
879 pdebug("DefinitionType -> ContainerType");
880 $$ = $1;
881 }
Mark Slee31985722006-05-24 21:45:31 +0000882
883FunctionType:
884 FieldType
885 {
Mark Sleee8540632006-05-30 09:24:40 +0000886 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000887 $$ = $1;
888 }
889| tok_void
890 {
Mark Sleee8540632006-05-30 09:24:40 +0000891 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000892 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000893 }
894
895FieldType:
896 tok_identifier
897 {
Mark Sleee8540632006-05-30 09:24:40 +0000898 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000899 if (g_parse_mode == INCLUDES) {
900 // Ignore identifiers in include mode
901 $$ = NULL;
902 } else {
903 // Lookup the identifier in the current scope
904 $$ = g_scope->get_type($1);
905 if ($$ == NULL) {
906 yyerror("Type \"%s\" has not been defined.", $1);
907 exit(1);
908 }
Mark Sleee8540632006-05-30 09:24:40 +0000909 }
Mark Slee31985722006-05-24 21:45:31 +0000910 }
911| BaseType
912 {
Mark Sleee8540632006-05-30 09:24:40 +0000913 pdebug("FieldType -> BaseType");
914 $$ = $1;
915 }
916| ContainerType
917 {
918 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000919 $$ = $1;
920 }
921
922BaseType:
923 tok_string
924 {
925 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000926 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000927 }
Mark Slee8d725a22007-04-13 01:57:12 +0000928| tok_binary
929 {
930 pdebug("BaseType -> tok_binary");
931 $$ = g_type_binary;
932 }
Mark Sleeb6200d82007-01-19 19:14:36 +0000933| tok_slist
934 {
935 pdebug("BaseType -> tok_slist");
936 $$ = g_type_slist;
937 }
Mark Slee78f58e22006-09-02 04:17:07 +0000938| tok_bool
939 {
940 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000941 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000942 }
Mark Slee31985722006-05-24 21:45:31 +0000943| tok_byte
944 {
945 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000946 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000947 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000948| tok_i16
949 {
950 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000951 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000952 }
Mark Slee31985722006-05-24 21:45:31 +0000953| tok_i32
954 {
955 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000956 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000957 }
Mark Slee31985722006-05-24 21:45:31 +0000958| tok_i64
959 {
960 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000961 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000962 }
Mark Sleec98d0502006-09-06 02:42:25 +0000963| tok_double
964 {
965 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000966 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000967 }
Mark Slee31985722006-05-24 21:45:31 +0000968
Mark Sleee8540632006-05-30 09:24:40 +0000969ContainerType:
970 MapType
971 {
972 pdebug("ContainerType -> MapType");
973 $$ = $1;
974 }
975| SetType
976 {
977 pdebug("ContainerType -> SetType");
978 $$ = $1;
979 }
980| ListType
981 {
982 pdebug("ContainerType -> ListType");
983 $$ = $1;
984 }
985
986MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000987 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000988 {
989 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000990 $$ = new t_map($4, $6);
991 if ($2 != NULL) {
992 ((t_container*)$$)->set_cpp_name(std::string($2));
993 }
Mark Sleee8540632006-05-30 09:24:40 +0000994 }
995
996SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000997 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000998 {
999 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +00001000 $$ = new t_set($4);
1001 if ($2 != NULL) {
1002 ((t_container*)$$)->set_cpp_name(std::string($2));
1003 }
Mark Sleee8540632006-05-30 09:24:40 +00001004 }
1005
1006ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +00001007 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +00001008 {
1009 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +00001010 $$ = new t_list($3);
1011 if ($5 != NULL) {
1012 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +00001013 }
1014 }
1015
Mark Slee36bfa2e2007-01-19 20:09:51 +00001016CppType:
Mark Sleeafc76542007-02-09 21:55:44 +00001017 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +00001018 {
Mark Sleeafc76542007-02-09 21:55:44 +00001019 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +00001020 }
1021|
1022 {
1023 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +00001024 }
1025
Mark Slee31985722006-05-24 21:45:31 +00001026%%