blob: 5d7cfcc6708183494eef2c9cbe46475e4175db48 [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
Mark Slee7e9eea42007-09-10 21:00:23 +000090%token tok_cocoa_prefix
Mark Slee9cb7c612006-09-01 22:17:45 +000091
Mark Sleef5377b32006-10-10 01:42:59 +000092/**
93 * Base datatype keywords
94 */
95%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +000096%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +000097%token tok_byte
98%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +000099%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +0000100%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +0000101%token tok_senum
Mark Slee9cb7c612006-09-01 22:17:45 +0000102%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +0000103%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +0000104%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +0000105%token tok_double
Mark Slee31985722006-05-24 21:45:31 +0000106
Mark Sleef5377b32006-10-10 01:42:59 +0000107/**
108 * Complex type keywords
109 */
Mark Slee31985722006-05-24 21:45:31 +0000110%token tok_map
111%token tok_list
112%token tok_set
113
Mark Sleef5377b32006-10-10 01:42:59 +0000114/**
115 * Function modifiers
Mark Slee27ed6ec2007-08-16 01:26:31 +0000116 */
Mark Slee31985722006-05-24 21:45:31 +0000117%token tok_async
118
Mark Sleef5377b32006-10-10 01:42:59 +0000119/**
120 * Thrift language keywords
121 */
Mark Slee31985722006-05-24 21:45:31 +0000122%token tok_typedef
123%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000124%token tok_xception
125%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000126%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000127%token tok_service
128%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000129%token tok_const
David Reiss8320a922007-08-14 19:59:26 +0000130%token tok_required
131%token tok_optional
Mark Slee31985722006-05-24 21:45:31 +0000132
Mark Sleef5377b32006-10-10 01:42:59 +0000133/**
134 * Grammar nodes
135 */
136
Mark Slee31985722006-05-24 21:45:31 +0000137%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000138%type<ttype> ContainerType
139%type<ttype> MapType
140%type<ttype> SetType
141%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000142
David Reisscdffe262007-08-14 17:12:31 +0000143%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000144%type<ttype> TypeDefinition
145
Mark Slee31985722006-05-24 21:45:31 +0000146%type<ttypedef> Typedef
147%type<ttype> DefinitionType
148
149%type<tfield> Field
Mark Slee7ff32452007-02-01 05:26:18 +0000150%type<iconst> FieldIdentifier
David Reiss8320a922007-08-14 19:59:26 +0000151%type<ereq> FieldRequiredness
Mark Slee31985722006-05-24 21:45:31 +0000152%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000153%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000154%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000155
156%type<tenum> Enum
157%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000158%type<tenumv> EnumDef
159
Mark Slee6a47fed2007-02-07 02:40:59 +0000160%type<ttypedef> Senum
161%type<tbase> SenumDefList
162%type<id> SenumDef
163
Mark Slee30152872006-11-28 01:24:07 +0000164%type<tconst> Const
165%type<tconstv> ConstValue
166%type<tconstv> ConstList
167%type<tconstv> ConstListContents
168%type<tconstv> ConstMap
169%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000170
171%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000172%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000173%type<tservice> Service
174
175%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000176%type<ttype> FunctionType
177%type<tservice> FunctionList
178
Mark Slee36bfa2e2007-01-19 20:09:51 +0000179%type<tstruct> Throws
180%type<tservice> Extends
181%type<tbool> Async
182%type<tbool> XsdAll
183%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000184%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000185%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000186%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000187
David Reisscbd4bac2007-08-14 17:12:33 +0000188%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000189
Mark Slee31985722006-05-24 21:45:31 +0000190%%
191
Mark Sleef5377b32006-10-10 01:42:59 +0000192/**
193 * Thrift Grammar Implementation.
194 *
195 * For the most part this source file works its way top down from what you
196 * might expect to find in a typical .thrift file, i.e. type definitions and
197 * namespaces up top followed by service definitions using those types.
198 */
Mark Slee31985722006-05-24 21:45:31 +0000199
200Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000201 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000202 {
203 pdebug("Program -> Headers DefinitionList");
David Reisscbd4bac2007-08-14 17:12:33 +0000204 /*
205 TODO(dreiss): Decide whether full-program doctext is worth the trouble.
David Reissc2532a92007-07-30 23:46:11 +0000206 if ($1 != NULL) {
207 g_program->set_doc($1);
208 }
David Reisscbd4bac2007-08-14 17:12:33 +0000209 */
210 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000211 }
212
David Reisscbd4bac2007-08-14 17:12:33 +0000213CaptureDocText:
214 {
215 if (g_parse_mode == PROGRAM) {
Mark Sleebd588222007-11-21 08:43:35 +0000216 $$ = g_doctext;
David Reisscbd4bac2007-08-14 17:12:33 +0000217 g_doctext = NULL;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000218 } else {
David Reisscbd4bac2007-08-14 17:12:33 +0000219 $$ = NULL;
220 }
221 }
222
223/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
224DestroyDocText:
225 {
226 if (g_parse_mode == PROGRAM) {
227 clear_doctext();
228 }
229 }
230
231/* We have to DestroyDocText here, otherwise it catches the doctext
232 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000233HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000234 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000235 {
236 pdebug("HeaderList -> HeaderList Header");
237 }
238|
239 {
240 pdebug("HeaderList -> ");
241 }
242
243Header:
244 Include
245 {
246 pdebug("Header -> Include");
247 }
248| tok_namespace tok_identifier
249 {
250 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
251 if (g_parse_mode == PROGRAM) {
252 g_program->set_cpp_namespace($2);
253 g_program->set_java_package($2);
254 }
255 }
256| tok_cpp_namespace tok_identifier
257 {
258 pdebug("Header -> tok_cpp_namespace tok_identifier");
259 if (g_parse_mode == PROGRAM) {
260 g_program->set_cpp_namespace($2);
261 }
262 }
263| tok_cpp_include tok_literal
264 {
265 pdebug("Header -> tok_cpp_include tok_literal");
266 if (g_parse_mode == PROGRAM) {
267 g_program->add_cpp_include($2);
268 }
269 }
Mark Sleee888b372007-01-12 01:06:24 +0000270| tok_php_namespace tok_identifier
271 {
272 pdebug("Header -> tok_php_namespace tok_identifier");
273 if (g_parse_mode == PROGRAM) {
274 g_program->set_php_namespace($2);
275 }
276 }
David Reissc6fc3292007-08-30 00:58:43 +0000277| tok_py_module tok_identifier
278 {
279 pdebug("Header -> tok_py_module tok_identifier");
280 if (g_parse_mode == PROGRAM) {
281 g_program->set_py_module($2);
282 }
283 }
Mark Slee27ed6ec2007-08-16 01:26:31 +0000284| tok_perl_package tok_identifier
285 {
286 pdebug("Header -> tok_perl_namespace tok_identifier");
287 if (g_parse_mode == PROGRAM) {
288 g_program->set_perl_package($2);
289 }
290 }
Mark Slee58dfb4f2007-07-06 02:45:25 +0000291| tok_ruby_namespace tok_identifier
292 {
293 pdebug("Header -> tok_ruby_namespace tok_identifier");
294 if (g_parse_mode == PROGRAM) {
295 g_program->set_ruby_namespace($2);
296 }
297 }
Mark Sleebd588222007-11-21 08:43:35 +0000298| tok_smalltalk_category tok_st_identifier
299 {
300 pdebug("Header -> tok_smalltalk_category tok_st_identifier");
301 if (g_parse_mode == PROGRAM) {
302 g_program->set_smalltalk_category($2);
303 }
304 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000305| tok_java_package tok_identifier
306 {
307 pdebug("Header -> tok_java_package tok_identifier");
308 if (g_parse_mode == PROGRAM) {
309 g_program->set_java_package($2);
310 }
311 }
Mark Slee7e9eea42007-09-10 21:00:23 +0000312| tok_cocoa_prefix tok_identifier
313 {
314 pdebug("Header -> tok_cocoa_prefix tok_identifier");
315 if (g_parse_mode == PROGRAM) {
316 g_program->set_cocoa_prefix($2);
317 }
318 }
Mark Slee0d9199e2007-01-31 02:08:30 +0000319| tok_xsd_namespace tok_literal
320 {
321 pdebug("Header -> tok_xsd_namespace tok_literal");
322 if (g_parse_mode == PROGRAM) {
323 g_program->set_xsd_namespace($2);
324 }
325 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000326
327Include:
328 tok_include tok_literal
329 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000330 pdebug("Include -> tok_include tok_literal");
Mark Sleef0712dc2006-10-25 19:03:57 +0000331 if (g_parse_mode == INCLUDES) {
332 std::string path = include_file(std::string($2));
333 if (!path.empty()) {
334 g_program->add_include(path);
335 }
336 }
337 }
Mark Slee31985722006-05-24 21:45:31 +0000338
339DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000340 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000341 {
342 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000343 if ($2 != NULL && $3 != NULL) {
344 $3->set_doc($2);
345 }
Mark Slee31985722006-05-24 21:45:31 +0000346 }
347|
348 {
349 pdebug("DefinitionList -> ");
350 }
351
352Definition:
Mark Slee30152872006-11-28 01:24:07 +0000353 Const
354 {
355 pdebug("Definition -> Const");
356 if (g_parse_mode == PROGRAM) {
357 g_program->add_const($1);
Mark Sleebd588222007-11-21 08:43:35 +0000358 }
David Reisscdffe262007-08-14 17:12:31 +0000359 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000360 }
361| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000362 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000363 pdebug("Definition -> TypeDefinition");
364 if (g_parse_mode == PROGRAM) {
365 g_scope->add_type($1->get_name(), $1);
366 if (g_parent_scope != NULL) {
367 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
368 }
369 }
David Reisscdffe262007-08-14 17:12:31 +0000370 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000371 }
Mark Slee31985722006-05-24 21:45:31 +0000372| Service
373 {
374 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000375 if (g_parse_mode == PROGRAM) {
376 g_scope->add_service($1->get_name(), $1);
377 if (g_parent_scope != NULL) {
378 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
379 }
380 g_program->add_service($1);
381 }
David Reisscdffe262007-08-14 17:12:31 +0000382 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000383 }
384
Mark Sleef0712dc2006-10-25 19:03:57 +0000385TypeDefinition:
386 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000387 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000388 pdebug("TypeDefinition -> Typedef");
389 if (g_parse_mode == PROGRAM) {
390 g_program->add_typedef($1);
391 }
392 }
393| Enum
394 {
395 pdebug("TypeDefinition -> Enum");
396 if (g_parse_mode == PROGRAM) {
397 g_program->add_enum($1);
398 }
399 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000400| Senum
401 {
402 pdebug("TypeDefinition -> Senum");
403 if (g_parse_mode == PROGRAM) {
404 g_program->add_typedef($1);
405 }
406 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000407| Struct
408 {
409 pdebug("TypeDefinition -> Struct");
410 if (g_parse_mode == PROGRAM) {
411 g_program->add_struct($1);
412 }
413 }
414| Xception
Mark Slee27ed6ec2007-08-16 01:26:31 +0000415 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000416 pdebug("TypeDefinition -> Xception");
417 if (g_parse_mode == PROGRAM) {
418 g_program->add_xception($1);
419 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000420 }
Mark Slee31985722006-05-24 21:45:31 +0000421
422Typedef:
Mark Sleebd588222007-11-21 08:43:35 +0000423 tok_typedef DefinitionType tok_identifier
Mark Slee31985722006-05-24 21:45:31 +0000424 {
425 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
David Reisscdffe262007-08-14 17:12:31 +0000426 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000427 $$ = td;
428 }
429
Mark Slee6a47fed2007-02-07 02:40:59 +0000430CommaOrSemicolonOptional:
431 ','
432 {}
433| ';'
434 {}
435|
436 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000437
Mark Slee31985722006-05-24 21:45:31 +0000438Enum:
David Reisscdffe262007-08-14 17:12:31 +0000439 tok_enum tok_identifier '{' EnumDefList '}'
Mark Slee31985722006-05-24 21:45:31 +0000440 {
441 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000442 $$ = $4;
443 $$->set_name($2);
Mark Slee31985722006-05-24 21:45:31 +0000444 }
445
446EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000447 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000448 {
449 pdebug("EnumDefList -> EnumDefList EnumDef");
450 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000451 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000452 }
453|
454 {
455 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000456 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000457 }
458
459EnumDef:
David Reisscbd4bac2007-08-14 17:12:33 +0000460 CaptureDocText tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000461 {
Mark Slee30152872006-11-28 01:24:07 +0000462 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000463 if ($4 < 0) {
464 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000465 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000466 $$ = new t_enum_value($2, $4);
467 if ($1 != NULL) {
468 $$->set_doc($1);
469 }
Mark Sleed0767c52007-07-27 22:14:41 +0000470 if (g_parse_mode == PROGRAM) {
471 g_scope->add_constant($2, new t_const(g_type_i32, $2, new t_const_value($4)));
472 if (g_parent_scope != NULL) {
473 g_parent_scope->add_constant(g_parent_prefix + $2, new t_const(g_type_i32, $2, new t_const_value($4)));
474 }
475 }
Mark Slee31985722006-05-24 21:45:31 +0000476 }
477|
David Reisscbd4bac2007-08-14 17:12:33 +0000478 CaptureDocText tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000479 {
Mark Slee30152872006-11-28 01:24:07 +0000480 pdebug("EnumDef -> tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000481 $$ = new t_enum_value($2);
482 if ($1 != NULL) {
483 $$->set_doc($1);
484 }
Mark Slee30152872006-11-28 01:24:07 +0000485 }
486
Mark Slee6a47fed2007-02-07 02:40:59 +0000487Senum:
David Reisscdffe262007-08-14 17:12:31 +0000488 tok_senum tok_identifier '{' SenumDefList '}'
Mark Slee6a47fed2007-02-07 02:40:59 +0000489 {
490 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000491 $$ = new t_typedef(g_program, $4, $2);
Mark Slee6a47fed2007-02-07 02:40:59 +0000492 }
493
494SenumDefList:
495 SenumDefList SenumDef
496 {
497 pdebug("SenumDefList -> SenumDefList SenumDef");
498 $$ = $1;
499 $$->add_string_enum_val($2);
500 }
501|
502 {
503 pdebug("SenumDefList -> ");
504 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
505 $$->set_string_enum(true);
506 }
507
508SenumDef:
509 tok_literal CommaOrSemicolonOptional
510 {
511 pdebug("SenumDef -> tok_literal");
512 $$ = $1;
513 }
514
Mark Slee30152872006-11-28 01:24:07 +0000515Const:
516 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
517 {
518 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000519 if (g_parse_mode == PROGRAM) {
520 $$ = new t_const($2, $3, $5);
521 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000522
523 g_scope->add_constant($3, $$);
524 if (g_parent_scope != NULL) {
525 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
526 }
527
Mark Sleeaa7671d2006-11-29 03:19:31 +0000528 } else {
529 $$ = NULL;
530 }
Mark Slee30152872006-11-28 01:24:07 +0000531 }
532
533ConstValue:
534 tok_int_constant
535 {
536 pdebug("ConstValue => tok_int_constant");
537 $$ = new t_const_value();
538 $$->set_integer($1);
539 }
540| tok_dub_constant
541 {
542 pdebug("ConstValue => tok_dub_constant");
543 $$ = new t_const_value();
544 $$->set_double($1);
545 }
546| tok_literal
547 {
548 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000549 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000550 }
Mark Slee67fc6342006-11-29 03:37:04 +0000551| tok_identifier
552 {
553 pdebug("ConstValue => tok_identifier");
Mark Sleed0767c52007-07-27 22:14:41 +0000554 t_const* constant = g_scope->get_constant($1);
555 if (constant != NULL) {
556 $$ = constant->get_value();
557 } else {
558 if (g_parse_mode == PROGRAM) {
559 pwarning(1, "Constant strings should be quoted: %s\n", $1);
560 }
561 $$ = new t_const_value($1);
562 }
Mark Slee67fc6342006-11-29 03:37:04 +0000563 }
Mark Slee30152872006-11-28 01:24:07 +0000564| ConstList
565 {
566 pdebug("ConstValue => ConstList");
567 $$ = $1;
568 }
569| ConstMap
570 {
571 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000572 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000573 }
574
575ConstList:
576 '[' ConstListContents ']'
577 {
578 pdebug("ConstList => [ ConstListContents ]");
579 $$ = $2;
580 }
581
582ConstListContents:
583 ConstListContents ConstValue CommaOrSemicolonOptional
584 {
585 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
586 $$ = $1;
587 $$->add_list($2);
588 }
589|
590 {
591 pdebug("ConstListContents =>");
592 $$ = new t_const_value();
593 $$->set_list();
594 }
595
596ConstMap:
597 '{' ConstMapContents '}'
598 {
599 pdebug("ConstMap => { ConstMapContents }");
600 $$ = $2;
601 }
602
603ConstMapContents:
604 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
605 {
606 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
607 $$ = $1;
608 $$->add_map($2, $4);
609 }
610|
611 {
612 pdebug("ConstMapContents =>");
613 $$ = new t_const_value();
614 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000615 }
616
617Struct:
David Reisscdffe262007-08-14 17:12:31 +0000618 tok_struct tok_identifier XsdAll '{' FieldList '}'
Mark Slee31985722006-05-24 21:45:31 +0000619 {
620 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
David Reisscdffe262007-08-14 17:12:31 +0000621 $5->set_name($2);
622 $5->set_xsd_all($3);
623 $$ = $5;
Mark Slee9cb7c612006-09-01 22:17:45 +0000624 y_field_val = -1;
625 }
626
Mark Slee36bfa2e2007-01-19 20:09:51 +0000627XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000628 tok_xsd_all
629 {
630 $$ = true;
631 }
632|
633 {
634 $$ = false;
635 }
636
Mark Slee36bfa2e2007-01-19 20:09:51 +0000637XsdOptional:
638 tok_xsd_optional
639 {
640 $$ = true;
641 }
642|
643 {
644 $$ = false;
645 }
646
Mark Slee7df0e2a2007-02-06 21:03:18 +0000647XsdNillable:
648 tok_xsd_nillable
649 {
650 $$ = true;
651 }
652|
653 {
654 $$ = false;
655 }
656
Mark Slee21135c32007-02-05 21:52:08 +0000657XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000658 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000659 {
Mark Slee748d83f2007-02-07 01:20:08 +0000660 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000661 }
662|
663 {
664 $$ = NULL;
665 }
666
Mark Slee9cb7c612006-09-01 22:17:45 +0000667Xception:
668 tok_xception tok_identifier '{' FieldList '}'
669 {
670 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
671 $4->set_name($2);
672 $4->set_xception(true);
673 $$ = $4;
674 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000675 }
676
677Service:
Mark Slee78165722007-09-10 22:08:49 +0000678 tok_service tok_identifier Extends '{' FlagArgs FunctionList UnflagArgs '}'
Mark Slee31985722006-05-24 21:45:31 +0000679 {
680 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Mark Slee78165722007-09-10 22:08:49 +0000681 $$ = $6;
David Reisscdffe262007-08-14 17:12:31 +0000682 $$->set_name($2);
683 $$->set_extends($3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000684 }
685
Mark Slee78165722007-09-10 22:08:49 +0000686FlagArgs:
687 {
688 g_arglist = 1;
689 }
690
691UnflagArgs:
692 {
693 g_arglist = 0;
694 }
695
Mark Slee36bfa2e2007-01-19 20:09:51 +0000696Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000697 tok_extends tok_identifier
698 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000699 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000700 $$ = NULL;
701 if (g_parse_mode == PROGRAM) {
702 $$ = g_scope->get_service($2);
703 if ($$ == NULL) {
704 yyerror("Service \"%s\" has not been defined.", $2);
705 exit(1);
706 }
707 }
708 }
709|
710 {
711 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000712 }
713
714FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000715 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000716 {
717 pdebug("FunctionList -> FunctionList Function");
718 $$ = $1;
719 $1->add_function($2);
720 }
721|
722 {
723 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000724 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000725 }
726
727Function:
David Reisscbd4bac2007-08-14 17:12:33 +0000728 CaptureDocText Async FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000729 {
ccheeverf53b5cf2007-02-05 20:33:11 +0000730 $6->set_name(std::string($4) + "_args");
731 $$ = new t_function($3, $4, $6, $8, $2);
732 if ($1 != NULL) {
733 $$->set_doc($1);
734 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000735 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000736 }
737
Mark Slee36bfa2e2007-01-19 20:09:51 +0000738Async:
Mark Slee52f643d2006-08-09 00:03:43 +0000739 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000740 {
Mark Slee52f643d2006-08-09 00:03:43 +0000741 $$ = true;
742 }
743|
744 {
745 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000746 }
747
Mark Slee36bfa2e2007-01-19 20:09:51 +0000748Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000749 tok_throws '(' FieldList ')'
750 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000751 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000752 $$ = $3;
753 }
754|
755 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000756 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000757 }
758
Mark Slee31985722006-05-24 21:45:31 +0000759FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000760 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000761 {
762 pdebug("FieldList -> FieldList , Field");
763 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000764 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000765 }
766|
767 {
768 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000769 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000770 }
771
772Field:
David Reiss8320a922007-08-14 19:59:26 +0000773 CaptureDocText FieldIdentifier FieldRequiredness FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000774 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000775 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000776 if ($2 < 0) {
David Reiss8320a922007-08-14 19:59:26 +0000777 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 +0000778 }
David Reiss8320a922007-08-14 19:59:26 +0000779 $$ = new t_field($4, $5, $2);
780 $$->set_req($3);
781 if ($6 != NULL) {
782 validate_field_value($$, $6);
783 $$->set_value($6);
Mark Slee7ff32452007-02-01 05:26:18 +0000784 }
David Reiss8320a922007-08-14 19:59:26 +0000785 $$->set_xsd_optional($7);
786 $$->set_xsd_nillable($8);
ccheeverf53b5cf2007-02-05 20:33:11 +0000787 if ($1 != NULL) {
788 $$->set_doc($1);
789 }
David Reiss8320a922007-08-14 19:59:26 +0000790 if ($9 != NULL) {
791 $$->set_xsd_attrs($9);
Mark Slee21135c32007-02-05 21:52:08 +0000792 }
Mark Slee31985722006-05-24 21:45:31 +0000793 }
Mark Slee7ff32452007-02-01 05:26:18 +0000794
795FieldIdentifier:
796 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000797 {
Mark Slee7ff32452007-02-01 05:26:18 +0000798 if ($1 <= 0) {
799 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n", $1);
800 $1 = y_field_val--;
Mark Sleef0712dc2006-10-25 19:03:57 +0000801 }
Mark Slee7ff32452007-02-01 05:26:18 +0000802 $$ = $1;
803 }
804|
805 {
806 $$ = y_field_val--;
807 }
808
David Reiss8320a922007-08-14 19:59:26 +0000809FieldRequiredness:
810 tok_required
811 {
Mark Slee78165722007-09-10 22:08:49 +0000812 if (g_arglist) {
813 if (g_parse_mode == PROGRAM) {
814 pwarning(1, "required keyword is ignored in argument lists.\n");
815 }
816 $$ = t_field::OPT_IN_REQ_OUT;
817 } else {
818 $$ = t_field::REQUIRED;
819 }
David Reiss8320a922007-08-14 19:59:26 +0000820 }
821| tok_optional
822 {
Mark Slee78165722007-09-10 22:08:49 +0000823 if (g_arglist) {
824 if (g_parse_mode == PROGRAM) {
825 pwarning(1, "optional keyword is ignored in argument lists.\n");
826 }
827 $$ = t_field::OPT_IN_REQ_OUT;
828 } else {
829 $$ = t_field::OPTIONAL;
830 }
David Reiss8320a922007-08-14 19:59:26 +0000831 }
832|
833 {
834 $$ = t_field::OPT_IN_REQ_OUT;
835 }
836
Mark Slee7ff32452007-02-01 05:26:18 +0000837FieldValue:
838 '=' ConstValue
839 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000840 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +0000841 $$ = $2;
842 } else {
843 $$ = NULL;
844 }
845 }
846|
847 {
848 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000849 }
Mark Slee31985722006-05-24 21:45:31 +0000850
851DefinitionType:
852 BaseType
853 {
854 pdebug("DefinitionType -> BaseType");
855 $$ = $1;
856 }
Mark Sleee8540632006-05-30 09:24:40 +0000857| ContainerType
858 {
859 pdebug("DefinitionType -> ContainerType");
860 $$ = $1;
861 }
Mark Slee31985722006-05-24 21:45:31 +0000862
863FunctionType:
864 FieldType
865 {
Mark Sleee8540632006-05-30 09:24:40 +0000866 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000867 $$ = $1;
868 }
869| tok_void
870 {
Mark Sleee8540632006-05-30 09:24:40 +0000871 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000872 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000873 }
874
875FieldType:
876 tok_identifier
877 {
Mark Sleee8540632006-05-30 09:24:40 +0000878 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000879 if (g_parse_mode == INCLUDES) {
880 // Ignore identifiers in include mode
881 $$ = NULL;
882 } else {
883 // Lookup the identifier in the current scope
884 $$ = g_scope->get_type($1);
885 if ($$ == NULL) {
886 yyerror("Type \"%s\" has not been defined.", $1);
887 exit(1);
888 }
Mark Sleee8540632006-05-30 09:24:40 +0000889 }
Mark Slee31985722006-05-24 21:45:31 +0000890 }
891| BaseType
892 {
Mark Sleee8540632006-05-30 09:24:40 +0000893 pdebug("FieldType -> BaseType");
894 $$ = $1;
895 }
896| ContainerType
897 {
898 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000899 $$ = $1;
900 }
901
902BaseType:
903 tok_string
904 {
905 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000906 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000907 }
Mark Slee8d725a22007-04-13 01:57:12 +0000908| tok_binary
909 {
910 pdebug("BaseType -> tok_binary");
911 $$ = g_type_binary;
912 }
Mark Sleeb6200d82007-01-19 19:14:36 +0000913| tok_slist
914 {
915 pdebug("BaseType -> tok_slist");
916 $$ = g_type_slist;
917 }
Mark Slee78f58e22006-09-02 04:17:07 +0000918| tok_bool
919 {
920 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000921 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000922 }
Mark Slee31985722006-05-24 21:45:31 +0000923| tok_byte
924 {
925 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000926 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000927 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000928| tok_i16
929 {
930 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000931 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000932 }
Mark Slee31985722006-05-24 21:45:31 +0000933| tok_i32
934 {
935 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000936 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000937 }
Mark Slee31985722006-05-24 21:45:31 +0000938| tok_i64
939 {
940 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000941 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000942 }
Mark Sleec98d0502006-09-06 02:42:25 +0000943| tok_double
944 {
945 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000946 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000947 }
Mark Slee31985722006-05-24 21:45:31 +0000948
Mark Sleee8540632006-05-30 09:24:40 +0000949ContainerType:
950 MapType
951 {
952 pdebug("ContainerType -> MapType");
953 $$ = $1;
954 }
955| SetType
956 {
957 pdebug("ContainerType -> SetType");
958 $$ = $1;
959 }
960| ListType
961 {
962 pdebug("ContainerType -> ListType");
963 $$ = $1;
964 }
965
966MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000967 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000968 {
969 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000970 $$ = new t_map($4, $6);
971 if ($2 != NULL) {
972 ((t_container*)$$)->set_cpp_name(std::string($2));
973 }
Mark Sleee8540632006-05-30 09:24:40 +0000974 }
975
976SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000977 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000978 {
979 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000980 $$ = new t_set($4);
981 if ($2 != NULL) {
982 ((t_container*)$$)->set_cpp_name(std::string($2));
983 }
Mark Sleee8540632006-05-30 09:24:40 +0000984 }
985
986ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000987 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +0000988 {
989 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +0000990 $$ = new t_list($3);
991 if ($5 != NULL) {
992 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +0000993 }
994 }
995
Mark Slee36bfa2e2007-01-19 20:09:51 +0000996CppType:
Mark Sleeafc76542007-02-09 21:55:44 +0000997 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +0000998 {
Mark Sleeafc76542007-02-09 21:55:44 +0000999 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +00001000 }
1001|
1002 {
1003 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +00001004 }
1005
Mark Slee31985722006-05-24 21:45:31 +00001006%%