blob: a96bfc4077cc11aa93d5fb9914eb48a7f8fad5bd [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;
David Reiss8320a922007-08-14 19:59:26 +000053 t_field::e_req ereq;
Mark Slee31985722006-05-24 21:45:31 +000054}
55
Mark Sleef5377b32006-10-10 01:42:59 +000056/**
57 * Strings identifier
58 */
Mark Slee31985722006-05-24 21:45:31 +000059%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +000060%token<id> tok_literal
David Reisscdffe262007-08-14 17:12:31 +000061%token<dtext> tok_doctext
Mark Sleef5377b32006-10-10 01:42:59 +000062
63/**
Mark Slee30152872006-11-28 01:24:07 +000064 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +000065 */
Mark Slee31985722006-05-24 21:45:31 +000066%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +000067%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +000068
Mark Sleef5377b32006-10-10 01:42:59 +000069/**
Mark Sleef0712dc2006-10-25 19:03:57 +000070 * Header keywoards
Mark Sleef5377b32006-10-10 01:42:59 +000071 */
Mark Sleef0712dc2006-10-25 19:03:57 +000072%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +000073%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000074%token tok_cpp_namespace
75%token tok_cpp_include
76%token tok_cpp_type
Mark Sleee888b372007-01-12 01:06:24 +000077%token tok_php_namespace
David Reissc6fc3292007-08-30 00:58:43 +000078%token tok_py_module
Mark Slee27ed6ec2007-08-16 01:26:31 +000079%token tok_perl_package
Mark Sleef0712dc2006-10-25 19:03:57 +000080%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +000081%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +000082%token tok_xsd_optional
Mark Slee7df0e2a2007-02-06 21:03:18 +000083%token tok_xsd_nillable
Mark Slee0d9199e2007-01-31 02:08:30 +000084%token tok_xsd_namespace
Mark Slee21135c32007-02-05 21:52:08 +000085%token tok_xsd_attrs
Mark Slee58dfb4f2007-07-06 02:45:25 +000086%token tok_ruby_namespace
Mark Slee7e9eea42007-09-10 21:00:23 +000087%token tok_cocoa_prefix
Mark Slee9cb7c612006-09-01 22:17:45 +000088
Mark Sleef5377b32006-10-10 01:42:59 +000089/**
90 * Base datatype keywords
91 */
92%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +000093%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +000094%token tok_byte
95%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +000096%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +000097%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +000098%token tok_senum
Mark Slee9cb7c612006-09-01 22:17:45 +000099%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +0000100%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +0000101%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +0000102%token tok_double
Mark Slee31985722006-05-24 21:45:31 +0000103
Mark Sleef5377b32006-10-10 01:42:59 +0000104/**
105 * Complex type keywords
106 */
Mark Slee31985722006-05-24 21:45:31 +0000107%token tok_map
108%token tok_list
109%token tok_set
110
Mark Sleef5377b32006-10-10 01:42:59 +0000111/**
112 * Function modifiers
Mark Slee27ed6ec2007-08-16 01:26:31 +0000113 */
Mark Slee31985722006-05-24 21:45:31 +0000114%token tok_async
115
Mark Sleef5377b32006-10-10 01:42:59 +0000116/**
117 * Thrift language keywords
118 */
Mark Slee31985722006-05-24 21:45:31 +0000119%token tok_typedef
120%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000121%token tok_xception
122%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000123%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000124%token tok_service
125%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000126%token tok_const
David Reiss8320a922007-08-14 19:59:26 +0000127%token tok_required
128%token tok_optional
Mark Slee31985722006-05-24 21:45:31 +0000129
Mark Sleef5377b32006-10-10 01:42:59 +0000130/**
131 * Grammar nodes
132 */
133
Mark Slee31985722006-05-24 21:45:31 +0000134%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000135%type<ttype> ContainerType
136%type<ttype> MapType
137%type<ttype> SetType
138%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000139
David Reisscdffe262007-08-14 17:12:31 +0000140%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000141%type<ttype> TypeDefinition
142
Mark Slee31985722006-05-24 21:45:31 +0000143%type<ttypedef> Typedef
144%type<ttype> DefinitionType
145
146%type<tfield> Field
Mark Slee7ff32452007-02-01 05:26:18 +0000147%type<iconst> FieldIdentifier
David Reiss8320a922007-08-14 19:59:26 +0000148%type<ereq> FieldRequiredness
Mark Slee31985722006-05-24 21:45:31 +0000149%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000150%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000151%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000152
153%type<tenum> Enum
154%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000155%type<tenumv> EnumDef
156
Mark Slee6a47fed2007-02-07 02:40:59 +0000157%type<ttypedef> Senum
158%type<tbase> SenumDefList
159%type<id> SenumDef
160
Mark Slee30152872006-11-28 01:24:07 +0000161%type<tconst> Const
162%type<tconstv> ConstValue
163%type<tconstv> ConstList
164%type<tconstv> ConstListContents
165%type<tconstv> ConstMap
166%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000167
168%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000169%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000170%type<tservice> Service
171
172%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000173%type<ttype> FunctionType
174%type<tservice> FunctionList
175
Mark Slee36bfa2e2007-01-19 20:09:51 +0000176%type<tstruct> Throws
177%type<tservice> Extends
178%type<tbool> Async
179%type<tbool> XsdAll
180%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000181%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000182%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000183%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000184
David Reisscbd4bac2007-08-14 17:12:33 +0000185%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000186
Mark Slee31985722006-05-24 21:45:31 +0000187%%
188
Mark Sleef5377b32006-10-10 01:42:59 +0000189/**
190 * Thrift Grammar Implementation.
191 *
192 * For the most part this source file works its way top down from what you
193 * might expect to find in a typical .thrift file, i.e. type definitions and
194 * namespaces up top followed by service definitions using those types.
195 */
Mark Slee31985722006-05-24 21:45:31 +0000196
197Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000198 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000199 {
200 pdebug("Program -> Headers DefinitionList");
David Reisscbd4bac2007-08-14 17:12:33 +0000201 /*
202 TODO(dreiss): Decide whether full-program doctext is worth the trouble.
David Reissc2532a92007-07-30 23:46:11 +0000203 if ($1 != NULL) {
204 g_program->set_doc($1);
205 }
David Reisscbd4bac2007-08-14 17:12:33 +0000206 */
207 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000208 }
209
David Reisscbd4bac2007-08-14 17:12:33 +0000210CaptureDocText:
211 {
212 if (g_parse_mode == PROGRAM) {
213 $$ = g_doctext;
214 g_doctext = NULL;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000215 } else {
David Reisscbd4bac2007-08-14 17:12:33 +0000216 $$ = NULL;
217 }
218 }
219
220/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
221DestroyDocText:
222 {
223 if (g_parse_mode == PROGRAM) {
224 clear_doctext();
225 }
226 }
227
228/* We have to DestroyDocText here, otherwise it catches the doctext
229 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000230HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000231 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000232 {
233 pdebug("HeaderList -> HeaderList Header");
234 }
235|
236 {
237 pdebug("HeaderList -> ");
238 }
239
240Header:
241 Include
242 {
243 pdebug("Header -> Include");
244 }
245| tok_namespace tok_identifier
246 {
247 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
248 if (g_parse_mode == PROGRAM) {
249 g_program->set_cpp_namespace($2);
250 g_program->set_java_package($2);
251 }
252 }
253| tok_cpp_namespace tok_identifier
254 {
255 pdebug("Header -> tok_cpp_namespace tok_identifier");
256 if (g_parse_mode == PROGRAM) {
257 g_program->set_cpp_namespace($2);
258 }
259 }
260| tok_cpp_include tok_literal
261 {
262 pdebug("Header -> tok_cpp_include tok_literal");
263 if (g_parse_mode == PROGRAM) {
264 g_program->add_cpp_include($2);
265 }
266 }
Mark Sleee888b372007-01-12 01:06:24 +0000267| tok_php_namespace tok_identifier
268 {
269 pdebug("Header -> tok_php_namespace tok_identifier");
270 if (g_parse_mode == PROGRAM) {
271 g_program->set_php_namespace($2);
272 }
273 }
David Reissc6fc3292007-08-30 00:58:43 +0000274| tok_py_module tok_identifier
275 {
276 pdebug("Header -> tok_py_module tok_identifier");
277 if (g_parse_mode == PROGRAM) {
278 g_program->set_py_module($2);
279 }
280 }
Mark Slee27ed6ec2007-08-16 01:26:31 +0000281| tok_perl_package tok_identifier
282 {
283 pdebug("Header -> tok_perl_namespace tok_identifier");
284 if (g_parse_mode == PROGRAM) {
285 g_program->set_perl_package($2);
286 }
287 }
Mark Slee58dfb4f2007-07-06 02:45:25 +0000288| tok_ruby_namespace tok_identifier
289 {
290 pdebug("Header -> tok_ruby_namespace tok_identifier");
291 if (g_parse_mode == PROGRAM) {
292 g_program->set_ruby_namespace($2);
293 }
294 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000295| tok_java_package tok_identifier
296 {
297 pdebug("Header -> tok_java_package tok_identifier");
298 if (g_parse_mode == PROGRAM) {
299 g_program->set_java_package($2);
300 }
301 }
Mark Slee7e9eea42007-09-10 21:00:23 +0000302| tok_cocoa_prefix tok_identifier
303 {
304 pdebug("Header -> tok_cocoa_prefix tok_identifier");
305 if (g_parse_mode == PROGRAM) {
306 g_program->set_cocoa_prefix($2);
307 }
308 }
Mark Slee0d9199e2007-01-31 02:08:30 +0000309| tok_xsd_namespace tok_literal
310 {
311 pdebug("Header -> tok_xsd_namespace tok_literal");
312 if (g_parse_mode == PROGRAM) {
313 g_program->set_xsd_namespace($2);
314 }
315 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000316
317Include:
318 tok_include tok_literal
319 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000320 pdebug("Include -> tok_include tok_literal");
Mark Sleef0712dc2006-10-25 19:03:57 +0000321 if (g_parse_mode == INCLUDES) {
322 std::string path = include_file(std::string($2));
323 if (!path.empty()) {
324 g_program->add_include(path);
325 }
326 }
327 }
Mark Slee31985722006-05-24 21:45:31 +0000328
329DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000330 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000331 {
332 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000333 if ($2 != NULL && $3 != NULL) {
334 $3->set_doc($2);
335 }
Mark Slee31985722006-05-24 21:45:31 +0000336 }
337|
338 {
339 pdebug("DefinitionList -> ");
340 }
341
342Definition:
Mark Slee30152872006-11-28 01:24:07 +0000343 Const
344 {
345 pdebug("Definition -> Const");
346 if (g_parse_mode == PROGRAM) {
347 g_program->add_const($1);
348 }
David Reisscdffe262007-08-14 17:12:31 +0000349 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000350 }
351| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000352 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000353 pdebug("Definition -> TypeDefinition");
354 if (g_parse_mode == PROGRAM) {
355 g_scope->add_type($1->get_name(), $1);
356 if (g_parent_scope != NULL) {
357 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
358 }
359 }
David Reisscdffe262007-08-14 17:12:31 +0000360 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000361 }
Mark Slee31985722006-05-24 21:45:31 +0000362| Service
363 {
364 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000365 if (g_parse_mode == PROGRAM) {
366 g_scope->add_service($1->get_name(), $1);
367 if (g_parent_scope != NULL) {
368 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
369 }
370 g_program->add_service($1);
371 }
David Reisscdffe262007-08-14 17:12:31 +0000372 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000373 }
374
Mark Sleef0712dc2006-10-25 19:03:57 +0000375TypeDefinition:
376 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000377 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000378 pdebug("TypeDefinition -> Typedef");
379 if (g_parse_mode == PROGRAM) {
380 g_program->add_typedef($1);
381 }
382 }
383| Enum
384 {
385 pdebug("TypeDefinition -> Enum");
386 if (g_parse_mode == PROGRAM) {
387 g_program->add_enum($1);
388 }
389 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000390| Senum
391 {
392 pdebug("TypeDefinition -> Senum");
393 if (g_parse_mode == PROGRAM) {
394 g_program->add_typedef($1);
395 }
396 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000397| Struct
398 {
399 pdebug("TypeDefinition -> Struct");
400 if (g_parse_mode == PROGRAM) {
401 g_program->add_struct($1);
402 }
403 }
404| Xception
Mark Slee27ed6ec2007-08-16 01:26:31 +0000405 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000406 pdebug("TypeDefinition -> Xception");
407 if (g_parse_mode == PROGRAM) {
408 g_program->add_xception($1);
409 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000410 }
Mark Slee31985722006-05-24 21:45:31 +0000411
412Typedef:
David Reisscdffe262007-08-14 17:12:31 +0000413 tok_typedef DefinitionType tok_identifier
Mark Slee31985722006-05-24 21:45:31 +0000414 {
415 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
David Reisscdffe262007-08-14 17:12:31 +0000416 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000417 $$ = td;
418 }
419
Mark Slee6a47fed2007-02-07 02:40:59 +0000420CommaOrSemicolonOptional:
421 ','
422 {}
423| ';'
424 {}
425|
426 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000427
Mark Slee31985722006-05-24 21:45:31 +0000428Enum:
David Reisscdffe262007-08-14 17:12:31 +0000429 tok_enum tok_identifier '{' EnumDefList '}'
Mark Slee31985722006-05-24 21:45:31 +0000430 {
431 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000432 $$ = $4;
433 $$->set_name($2);
Mark Slee31985722006-05-24 21:45:31 +0000434 }
435
436EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000437 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000438 {
439 pdebug("EnumDefList -> EnumDefList EnumDef");
440 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000441 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000442 }
443|
444 {
445 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000446 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000447 }
448
449EnumDef:
David Reisscbd4bac2007-08-14 17:12:33 +0000450 CaptureDocText tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000451 {
Mark Slee30152872006-11-28 01:24:07 +0000452 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000453 if ($4 < 0) {
454 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000455 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000456 $$ = new t_enum_value($2, $4);
457 if ($1 != NULL) {
458 $$->set_doc($1);
459 }
Mark Sleed0767c52007-07-27 22:14:41 +0000460 if (g_parse_mode == PROGRAM) {
461 g_scope->add_constant($2, new t_const(g_type_i32, $2, new t_const_value($4)));
462 if (g_parent_scope != NULL) {
463 g_parent_scope->add_constant(g_parent_prefix + $2, new t_const(g_type_i32, $2, new t_const_value($4)));
464 }
465 }
Mark Slee31985722006-05-24 21:45:31 +0000466 }
467|
David Reisscbd4bac2007-08-14 17:12:33 +0000468 CaptureDocText tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000469 {
Mark Slee30152872006-11-28 01:24:07 +0000470 pdebug("EnumDef -> tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000471 $$ = new t_enum_value($2);
472 if ($1 != NULL) {
473 $$->set_doc($1);
474 }
Mark Slee30152872006-11-28 01:24:07 +0000475 }
476
Mark Slee6a47fed2007-02-07 02:40:59 +0000477Senum:
David Reisscdffe262007-08-14 17:12:31 +0000478 tok_senum tok_identifier '{' SenumDefList '}'
Mark Slee6a47fed2007-02-07 02:40:59 +0000479 {
480 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000481 $$ = new t_typedef(g_program, $4, $2);
Mark Slee6a47fed2007-02-07 02:40:59 +0000482 }
483
484SenumDefList:
485 SenumDefList SenumDef
486 {
487 pdebug("SenumDefList -> SenumDefList SenumDef");
488 $$ = $1;
489 $$->add_string_enum_val($2);
490 }
491|
492 {
493 pdebug("SenumDefList -> ");
494 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
495 $$->set_string_enum(true);
496 }
497
498SenumDef:
499 tok_literal CommaOrSemicolonOptional
500 {
501 pdebug("SenumDef -> tok_literal");
502 $$ = $1;
503 }
504
Mark Slee30152872006-11-28 01:24:07 +0000505Const:
506 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
507 {
508 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000509 if (g_parse_mode == PROGRAM) {
510 $$ = new t_const($2, $3, $5);
511 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000512
513 g_scope->add_constant($3, $$);
514 if (g_parent_scope != NULL) {
515 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
516 }
517
Mark Sleeaa7671d2006-11-29 03:19:31 +0000518 } else {
519 $$ = NULL;
520 }
Mark Slee30152872006-11-28 01:24:07 +0000521 }
522
523ConstValue:
524 tok_int_constant
525 {
526 pdebug("ConstValue => tok_int_constant");
527 $$ = new t_const_value();
528 $$->set_integer($1);
529 }
530| tok_dub_constant
531 {
532 pdebug("ConstValue => tok_dub_constant");
533 $$ = new t_const_value();
534 $$->set_double($1);
535 }
536| tok_literal
537 {
538 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000539 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000540 }
Mark Slee67fc6342006-11-29 03:37:04 +0000541| tok_identifier
542 {
543 pdebug("ConstValue => tok_identifier");
Mark Sleed0767c52007-07-27 22:14:41 +0000544 t_const* constant = g_scope->get_constant($1);
545 if (constant != NULL) {
546 $$ = constant->get_value();
547 } else {
548 if (g_parse_mode == PROGRAM) {
549 pwarning(1, "Constant strings should be quoted: %s\n", $1);
550 }
551 $$ = new t_const_value($1);
552 }
Mark Slee67fc6342006-11-29 03:37:04 +0000553 }
Mark Slee30152872006-11-28 01:24:07 +0000554| ConstList
555 {
556 pdebug("ConstValue => ConstList");
557 $$ = $1;
558 }
559| ConstMap
560 {
561 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000562 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000563 }
564
565ConstList:
566 '[' ConstListContents ']'
567 {
568 pdebug("ConstList => [ ConstListContents ]");
569 $$ = $2;
570 }
571
572ConstListContents:
573 ConstListContents ConstValue CommaOrSemicolonOptional
574 {
575 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
576 $$ = $1;
577 $$->add_list($2);
578 }
579|
580 {
581 pdebug("ConstListContents =>");
582 $$ = new t_const_value();
583 $$->set_list();
584 }
585
586ConstMap:
587 '{' ConstMapContents '}'
588 {
589 pdebug("ConstMap => { ConstMapContents }");
590 $$ = $2;
591 }
592
593ConstMapContents:
594 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
595 {
596 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
597 $$ = $1;
598 $$->add_map($2, $4);
599 }
600|
601 {
602 pdebug("ConstMapContents =>");
603 $$ = new t_const_value();
604 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000605 }
606
607Struct:
David Reisscdffe262007-08-14 17:12:31 +0000608 tok_struct tok_identifier XsdAll '{' FieldList '}'
Mark Slee31985722006-05-24 21:45:31 +0000609 {
610 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
David Reisscdffe262007-08-14 17:12:31 +0000611 $5->set_name($2);
612 $5->set_xsd_all($3);
613 $$ = $5;
Mark Slee9cb7c612006-09-01 22:17:45 +0000614 y_field_val = -1;
615 }
616
Mark Slee36bfa2e2007-01-19 20:09:51 +0000617XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000618 tok_xsd_all
619 {
620 $$ = true;
621 }
622|
623 {
624 $$ = false;
625 }
626
Mark Slee36bfa2e2007-01-19 20:09:51 +0000627XsdOptional:
628 tok_xsd_optional
629 {
630 $$ = true;
631 }
632|
633 {
634 $$ = false;
635 }
636
Mark Slee7df0e2a2007-02-06 21:03:18 +0000637XsdNillable:
638 tok_xsd_nillable
639 {
640 $$ = true;
641 }
642|
643 {
644 $$ = false;
645 }
646
Mark Slee21135c32007-02-05 21:52:08 +0000647XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000648 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000649 {
Mark Slee748d83f2007-02-07 01:20:08 +0000650 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000651 }
652|
653 {
654 $$ = NULL;
655 }
656
Mark Slee9cb7c612006-09-01 22:17:45 +0000657Xception:
658 tok_xception tok_identifier '{' FieldList '}'
659 {
660 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
661 $4->set_name($2);
662 $4->set_xception(true);
663 $$ = $4;
664 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000665 }
666
667Service:
David Reisscdffe262007-08-14 17:12:31 +0000668 tok_service tok_identifier Extends '{' FunctionList '}'
Mark Slee31985722006-05-24 21:45:31 +0000669 {
670 pdebug("Service -> tok_service tok_identifier { FunctionList }");
David Reisscdffe262007-08-14 17:12:31 +0000671 $$ = $5;
672 $$->set_name($2);
673 $$->set_extends($3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000674 }
675
Mark Slee36bfa2e2007-01-19 20:09:51 +0000676Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000677 tok_extends tok_identifier
678 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000679 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000680 $$ = NULL;
681 if (g_parse_mode == PROGRAM) {
682 $$ = g_scope->get_service($2);
683 if ($$ == NULL) {
684 yyerror("Service \"%s\" has not been defined.", $2);
685 exit(1);
686 }
687 }
688 }
689|
690 {
691 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000692 }
693
694FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000695 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000696 {
697 pdebug("FunctionList -> FunctionList Function");
698 $$ = $1;
699 $1->add_function($2);
700 }
701|
702 {
703 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000704 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000705 }
706
707Function:
David Reisscbd4bac2007-08-14 17:12:33 +0000708 CaptureDocText Async FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000709 {
ccheeverf53b5cf2007-02-05 20:33:11 +0000710 $6->set_name(std::string($4) + "_args");
711 $$ = new t_function($3, $4, $6, $8, $2);
712 if ($1 != NULL) {
713 $$->set_doc($1);
714 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000715 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000716 }
717
Mark Slee36bfa2e2007-01-19 20:09:51 +0000718Async:
Mark Slee52f643d2006-08-09 00:03:43 +0000719 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000720 {
Mark Slee52f643d2006-08-09 00:03:43 +0000721 $$ = true;
722 }
723|
724 {
725 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000726 }
727
Mark Slee36bfa2e2007-01-19 20:09:51 +0000728Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000729 tok_throws '(' FieldList ')'
730 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000731 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000732 $$ = $3;
733 }
734|
735 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000736 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000737 }
738
Mark Slee31985722006-05-24 21:45:31 +0000739FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000740 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000741 {
742 pdebug("FieldList -> FieldList , Field");
743 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000744 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000745 }
746|
747 {
748 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000749 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000750 }
751
752Field:
David Reiss8320a922007-08-14 19:59:26 +0000753 CaptureDocText FieldIdentifier FieldRequiredness FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000754 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000755 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000756 if ($2 < 0) {
David Reiss8320a922007-08-14 19:59:26 +0000757 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 +0000758 }
David Reiss8320a922007-08-14 19:59:26 +0000759 $$ = new t_field($4, $5, $2);
760 $$->set_req($3);
761 if ($6 != NULL) {
762 validate_field_value($$, $6);
763 $$->set_value($6);
Mark Slee7ff32452007-02-01 05:26:18 +0000764 }
David Reiss8320a922007-08-14 19:59:26 +0000765 $$->set_xsd_optional($7);
766 $$->set_xsd_nillable($8);
ccheeverf53b5cf2007-02-05 20:33:11 +0000767 if ($1 != NULL) {
768 $$->set_doc($1);
769 }
David Reiss8320a922007-08-14 19:59:26 +0000770 if ($9 != NULL) {
771 $$->set_xsd_attrs($9);
Mark Slee21135c32007-02-05 21:52:08 +0000772 }
Mark Slee31985722006-05-24 21:45:31 +0000773 }
Mark Slee7ff32452007-02-01 05:26:18 +0000774
775FieldIdentifier:
776 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000777 {
Mark Slee7ff32452007-02-01 05:26:18 +0000778 if ($1 <= 0) {
779 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n", $1);
780 $1 = y_field_val--;
Mark Sleef0712dc2006-10-25 19:03:57 +0000781 }
Mark Slee7ff32452007-02-01 05:26:18 +0000782 $$ = $1;
783 }
784|
785 {
786 $$ = y_field_val--;
787 }
788
David Reiss8320a922007-08-14 19:59:26 +0000789FieldRequiredness:
790 tok_required
791 {
792 $$ = t_field::REQUIRED;
793 }
794| tok_optional
795 {
796 $$ = t_field::OPTIONAL;
797 }
798|
799 {
800 $$ = t_field::OPT_IN_REQ_OUT;
801 }
802
Mark Slee7ff32452007-02-01 05:26:18 +0000803FieldValue:
804 '=' ConstValue
805 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000806 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +0000807 $$ = $2;
808 } else {
809 $$ = NULL;
810 }
811 }
812|
813 {
814 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000815 }
Mark Slee31985722006-05-24 21:45:31 +0000816
817DefinitionType:
818 BaseType
819 {
820 pdebug("DefinitionType -> BaseType");
821 $$ = $1;
822 }
Mark Sleee8540632006-05-30 09:24:40 +0000823| ContainerType
824 {
825 pdebug("DefinitionType -> ContainerType");
826 $$ = $1;
827 }
Mark Slee31985722006-05-24 21:45:31 +0000828
829FunctionType:
830 FieldType
831 {
Mark Sleee8540632006-05-30 09:24:40 +0000832 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000833 $$ = $1;
834 }
835| tok_void
836 {
Mark Sleee8540632006-05-30 09:24:40 +0000837 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000838 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000839 }
840
841FieldType:
842 tok_identifier
843 {
Mark Sleee8540632006-05-30 09:24:40 +0000844 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000845 if (g_parse_mode == INCLUDES) {
846 // Ignore identifiers in include mode
847 $$ = NULL;
848 } else {
849 // Lookup the identifier in the current scope
850 $$ = g_scope->get_type($1);
851 if ($$ == NULL) {
852 yyerror("Type \"%s\" has not been defined.", $1);
853 exit(1);
854 }
Mark Sleee8540632006-05-30 09:24:40 +0000855 }
Mark Slee31985722006-05-24 21:45:31 +0000856 }
857| BaseType
858 {
Mark Sleee8540632006-05-30 09:24:40 +0000859 pdebug("FieldType -> BaseType");
860 $$ = $1;
861 }
862| ContainerType
863 {
864 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000865 $$ = $1;
866 }
867
868BaseType:
869 tok_string
870 {
871 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000872 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000873 }
Mark Slee8d725a22007-04-13 01:57:12 +0000874| tok_binary
875 {
876 pdebug("BaseType -> tok_binary");
877 $$ = g_type_binary;
878 }
Mark Sleeb6200d82007-01-19 19:14:36 +0000879| tok_slist
880 {
881 pdebug("BaseType -> tok_slist");
882 $$ = g_type_slist;
883 }
Mark Slee78f58e22006-09-02 04:17:07 +0000884| tok_bool
885 {
886 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000887 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000888 }
Mark Slee31985722006-05-24 21:45:31 +0000889| tok_byte
890 {
891 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000892 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000893 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000894| tok_i16
895 {
896 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000897 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000898 }
Mark Slee31985722006-05-24 21:45:31 +0000899| tok_i32
900 {
901 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000902 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000903 }
Mark Slee31985722006-05-24 21:45:31 +0000904| tok_i64
905 {
906 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000907 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000908 }
Mark Sleec98d0502006-09-06 02:42:25 +0000909| tok_double
910 {
911 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000912 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000913 }
Mark Slee31985722006-05-24 21:45:31 +0000914
Mark Sleee8540632006-05-30 09:24:40 +0000915ContainerType:
916 MapType
917 {
918 pdebug("ContainerType -> MapType");
919 $$ = $1;
920 }
921| SetType
922 {
923 pdebug("ContainerType -> SetType");
924 $$ = $1;
925 }
926| ListType
927 {
928 pdebug("ContainerType -> ListType");
929 $$ = $1;
930 }
931
932MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000933 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000934 {
935 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000936 $$ = new t_map($4, $6);
937 if ($2 != NULL) {
938 ((t_container*)$$)->set_cpp_name(std::string($2));
939 }
Mark Sleee8540632006-05-30 09:24:40 +0000940 }
941
942SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000943 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000944 {
945 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000946 $$ = new t_set($4);
947 if ($2 != NULL) {
948 ((t_container*)$$)->set_cpp_name(std::string($2));
949 }
Mark Sleee8540632006-05-30 09:24:40 +0000950 }
951
952ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000953 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +0000954 {
955 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +0000956 $$ = new t_list($3);
957 if ($5 != NULL) {
958 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +0000959 }
960 }
961
Mark Slee36bfa2e2007-01-19 20:09:51 +0000962CppType:
Mark Sleeafc76542007-02-09 21:55:44 +0000963 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +0000964 {
Mark Sleeafc76542007-02-09 21:55:44 +0000965 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +0000966 }
967|
968 {
969 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +0000970 }
971
Mark Slee31985722006-05-24 21:45:31 +0000972%%