blob: cd727c10d38fd4906b3826fc80347e4f03266112 [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 Sleef5377b32006-10-10 01:42:59 +000063
64/**
Mark Slee30152872006-11-28 01:24:07 +000065 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +000066 */
Mark Slee31985722006-05-24 21:45:31 +000067%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +000068%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +000069
Mark Sleef5377b32006-10-10 01:42:59 +000070/**
Mark Sleef0712dc2006-10-25 19:03:57 +000071 * Header keywoards
Mark Sleef5377b32006-10-10 01:42:59 +000072 */
Mark Sleef0712dc2006-10-25 19:03:57 +000073%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +000074%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000075%token tok_cpp_namespace
76%token tok_cpp_include
77%token tok_cpp_type
Mark Sleee888b372007-01-12 01:06:24 +000078%token tok_php_namespace
David Reissc6fc3292007-08-30 00:58:43 +000079%token tok_py_module
Mark Slee27ed6ec2007-08-16 01:26:31 +000080%token tok_perl_package
Mark Sleef0712dc2006-10-25 19:03:57 +000081%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +000082%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +000083%token tok_xsd_optional
Mark Slee7df0e2a2007-02-06 21:03:18 +000084%token tok_xsd_nillable
Mark Slee0d9199e2007-01-31 02:08:30 +000085%token tok_xsd_namespace
Mark Slee21135c32007-02-05 21:52:08 +000086%token tok_xsd_attrs
Mark Slee58dfb4f2007-07-06 02:45:25 +000087%token tok_ruby_namespace
Mark Slee7e9eea42007-09-10 21:00:23 +000088%token tok_cocoa_prefix
Mark Slee9cb7c612006-09-01 22:17:45 +000089
Mark Sleef5377b32006-10-10 01:42:59 +000090/**
91 * Base datatype keywords
92 */
93%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +000094%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +000095%token tok_byte
96%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +000097%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +000098%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +000099%token tok_senum
Mark Slee9cb7c612006-09-01 22:17:45 +0000100%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +0000101%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +0000102%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +0000103%token tok_double
Mark Slee31985722006-05-24 21:45:31 +0000104
Mark Sleef5377b32006-10-10 01:42:59 +0000105/**
106 * Complex type keywords
107 */
Mark Slee31985722006-05-24 21:45:31 +0000108%token tok_map
109%token tok_list
110%token tok_set
111
Mark Sleef5377b32006-10-10 01:42:59 +0000112/**
113 * Function modifiers
Mark Slee27ed6ec2007-08-16 01:26:31 +0000114 */
Mark Slee31985722006-05-24 21:45:31 +0000115%token tok_async
116
Mark Sleef5377b32006-10-10 01:42:59 +0000117/**
118 * Thrift language keywords
119 */
Mark Slee31985722006-05-24 21:45:31 +0000120%token tok_typedef
121%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000122%token tok_xception
123%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000124%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000125%token tok_service
126%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000127%token tok_const
David Reiss8320a922007-08-14 19:59:26 +0000128%token tok_required
129%token tok_optional
Mark Slee31985722006-05-24 21:45:31 +0000130
Mark Sleef5377b32006-10-10 01:42:59 +0000131/**
132 * Grammar nodes
133 */
134
Mark Slee31985722006-05-24 21:45:31 +0000135%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000136%type<ttype> ContainerType
137%type<ttype> MapType
138%type<ttype> SetType
139%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000140
David Reisscdffe262007-08-14 17:12:31 +0000141%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000142%type<ttype> TypeDefinition
143
Mark Slee31985722006-05-24 21:45:31 +0000144%type<ttypedef> Typedef
145%type<ttype> DefinitionType
146
147%type<tfield> Field
Mark Slee7ff32452007-02-01 05:26:18 +0000148%type<iconst> FieldIdentifier
David Reiss8320a922007-08-14 19:59:26 +0000149%type<ereq> FieldRequiredness
Mark Slee31985722006-05-24 21:45:31 +0000150%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000151%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000152%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000153
154%type<tenum> Enum
155%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000156%type<tenumv> EnumDef
157
Mark Slee6a47fed2007-02-07 02:40:59 +0000158%type<ttypedef> Senum
159%type<tbase> SenumDefList
160%type<id> SenumDef
161
Mark Slee30152872006-11-28 01:24:07 +0000162%type<tconst> Const
163%type<tconstv> ConstValue
164%type<tconstv> ConstList
165%type<tconstv> ConstListContents
166%type<tconstv> ConstMap
167%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000168
169%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000170%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000171%type<tservice> Service
172
173%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000174%type<ttype> FunctionType
175%type<tservice> FunctionList
176
Mark Slee36bfa2e2007-01-19 20:09:51 +0000177%type<tstruct> Throws
178%type<tservice> Extends
179%type<tbool> Async
180%type<tbool> XsdAll
181%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000182%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000183%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000184%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000185
David Reisscbd4bac2007-08-14 17:12:33 +0000186%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000187
Mark Slee31985722006-05-24 21:45:31 +0000188%%
189
Mark Sleef5377b32006-10-10 01:42:59 +0000190/**
191 * Thrift Grammar Implementation.
192 *
193 * For the most part this source file works its way top down from what you
194 * might expect to find in a typical .thrift file, i.e. type definitions and
195 * namespaces up top followed by service definitions using those types.
196 */
Mark Slee31985722006-05-24 21:45:31 +0000197
198Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000199 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000200 {
201 pdebug("Program -> Headers DefinitionList");
David Reisscbd4bac2007-08-14 17:12:33 +0000202 /*
203 TODO(dreiss): Decide whether full-program doctext is worth the trouble.
David Reissc2532a92007-07-30 23:46:11 +0000204 if ($1 != NULL) {
205 g_program->set_doc($1);
206 }
David Reisscbd4bac2007-08-14 17:12:33 +0000207 */
208 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000209 }
210
David Reisscbd4bac2007-08-14 17:12:33 +0000211CaptureDocText:
212 {
213 if (g_parse_mode == PROGRAM) {
214 $$ = g_doctext;
215 g_doctext = NULL;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000216 } else {
David Reisscbd4bac2007-08-14 17:12:33 +0000217 $$ = NULL;
218 }
219 }
220
221/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
222DestroyDocText:
223 {
224 if (g_parse_mode == PROGRAM) {
225 clear_doctext();
226 }
227 }
228
229/* We have to DestroyDocText here, otherwise it catches the doctext
230 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000231HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000232 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000233 {
234 pdebug("HeaderList -> HeaderList Header");
235 }
236|
237 {
238 pdebug("HeaderList -> ");
239 }
240
241Header:
242 Include
243 {
244 pdebug("Header -> Include");
245 }
246| tok_namespace tok_identifier
247 {
248 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
249 if (g_parse_mode == PROGRAM) {
250 g_program->set_cpp_namespace($2);
251 g_program->set_java_package($2);
252 }
253 }
254| tok_cpp_namespace tok_identifier
255 {
256 pdebug("Header -> tok_cpp_namespace tok_identifier");
257 if (g_parse_mode == PROGRAM) {
258 g_program->set_cpp_namespace($2);
259 }
260 }
261| tok_cpp_include tok_literal
262 {
263 pdebug("Header -> tok_cpp_include tok_literal");
264 if (g_parse_mode == PROGRAM) {
265 g_program->add_cpp_include($2);
266 }
267 }
Mark Sleee888b372007-01-12 01:06:24 +0000268| tok_php_namespace tok_identifier
269 {
270 pdebug("Header -> tok_php_namespace tok_identifier");
271 if (g_parse_mode == PROGRAM) {
272 g_program->set_php_namespace($2);
273 }
274 }
David Reissc6fc3292007-08-30 00:58:43 +0000275| tok_py_module tok_identifier
276 {
277 pdebug("Header -> tok_py_module tok_identifier");
278 if (g_parse_mode == PROGRAM) {
279 g_program->set_py_module($2);
280 }
281 }
Mark Slee27ed6ec2007-08-16 01:26:31 +0000282| tok_perl_package tok_identifier
283 {
284 pdebug("Header -> tok_perl_namespace tok_identifier");
285 if (g_parse_mode == PROGRAM) {
286 g_program->set_perl_package($2);
287 }
288 }
Mark Slee58dfb4f2007-07-06 02:45:25 +0000289| tok_ruby_namespace tok_identifier
290 {
291 pdebug("Header -> tok_ruby_namespace tok_identifier");
292 if (g_parse_mode == PROGRAM) {
293 g_program->set_ruby_namespace($2);
294 }
295 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000296| tok_java_package tok_identifier
297 {
298 pdebug("Header -> tok_java_package tok_identifier");
299 if (g_parse_mode == PROGRAM) {
300 g_program->set_java_package($2);
301 }
302 }
Mark Slee7e9eea42007-09-10 21:00:23 +0000303| tok_cocoa_prefix tok_identifier
304 {
305 pdebug("Header -> tok_cocoa_prefix tok_identifier");
306 if (g_parse_mode == PROGRAM) {
307 g_program->set_cocoa_prefix($2);
308 }
309 }
Mark Slee0d9199e2007-01-31 02:08:30 +0000310| tok_xsd_namespace tok_literal
311 {
312 pdebug("Header -> tok_xsd_namespace tok_literal");
313 if (g_parse_mode == PROGRAM) {
314 g_program->set_xsd_namespace($2);
315 }
316 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000317
318Include:
319 tok_include tok_literal
320 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000321 pdebug("Include -> tok_include tok_literal");
Mark Sleef0712dc2006-10-25 19:03:57 +0000322 if (g_parse_mode == INCLUDES) {
323 std::string path = include_file(std::string($2));
324 if (!path.empty()) {
325 g_program->add_include(path);
326 }
327 }
328 }
Mark Slee31985722006-05-24 21:45:31 +0000329
330DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000331 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000332 {
333 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000334 if ($2 != NULL && $3 != NULL) {
335 $3->set_doc($2);
336 }
Mark Slee31985722006-05-24 21:45:31 +0000337 }
338|
339 {
340 pdebug("DefinitionList -> ");
341 }
342
343Definition:
Mark Slee30152872006-11-28 01:24:07 +0000344 Const
345 {
346 pdebug("Definition -> Const");
347 if (g_parse_mode == PROGRAM) {
348 g_program->add_const($1);
349 }
David Reisscdffe262007-08-14 17:12:31 +0000350 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000351 }
352| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000353 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000354 pdebug("Definition -> TypeDefinition");
355 if (g_parse_mode == PROGRAM) {
356 g_scope->add_type($1->get_name(), $1);
357 if (g_parent_scope != NULL) {
358 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
359 }
360 }
David Reisscdffe262007-08-14 17:12:31 +0000361 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000362 }
Mark Slee31985722006-05-24 21:45:31 +0000363| Service
364 {
365 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000366 if (g_parse_mode == PROGRAM) {
367 g_scope->add_service($1->get_name(), $1);
368 if (g_parent_scope != NULL) {
369 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
370 }
371 g_program->add_service($1);
372 }
David Reisscdffe262007-08-14 17:12:31 +0000373 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000374 }
375
Mark Sleef0712dc2006-10-25 19:03:57 +0000376TypeDefinition:
377 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000378 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000379 pdebug("TypeDefinition -> Typedef");
380 if (g_parse_mode == PROGRAM) {
381 g_program->add_typedef($1);
382 }
383 }
384| Enum
385 {
386 pdebug("TypeDefinition -> Enum");
387 if (g_parse_mode == PROGRAM) {
388 g_program->add_enum($1);
389 }
390 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000391| Senum
392 {
393 pdebug("TypeDefinition -> Senum");
394 if (g_parse_mode == PROGRAM) {
395 g_program->add_typedef($1);
396 }
397 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000398| Struct
399 {
400 pdebug("TypeDefinition -> Struct");
401 if (g_parse_mode == PROGRAM) {
402 g_program->add_struct($1);
403 }
404 }
405| Xception
Mark Slee27ed6ec2007-08-16 01:26:31 +0000406 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000407 pdebug("TypeDefinition -> Xception");
408 if (g_parse_mode == PROGRAM) {
409 g_program->add_xception($1);
410 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000411 }
Mark Slee31985722006-05-24 21:45:31 +0000412
413Typedef:
David Reisscdffe262007-08-14 17:12:31 +0000414 tok_typedef DefinitionType tok_identifier
Mark Slee31985722006-05-24 21:45:31 +0000415 {
416 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
David Reisscdffe262007-08-14 17:12:31 +0000417 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000418 $$ = td;
419 }
420
Mark Slee6a47fed2007-02-07 02:40:59 +0000421CommaOrSemicolonOptional:
422 ','
423 {}
424| ';'
425 {}
426|
427 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000428
Mark Slee31985722006-05-24 21:45:31 +0000429Enum:
David Reisscdffe262007-08-14 17:12:31 +0000430 tok_enum tok_identifier '{' EnumDefList '}'
Mark Slee31985722006-05-24 21:45:31 +0000431 {
432 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000433 $$ = $4;
434 $$->set_name($2);
Mark Slee31985722006-05-24 21:45:31 +0000435 }
436
437EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000438 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000439 {
440 pdebug("EnumDefList -> EnumDefList EnumDef");
441 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000442 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000443 }
444|
445 {
446 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000447 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000448 }
449
450EnumDef:
David Reisscbd4bac2007-08-14 17:12:33 +0000451 CaptureDocText tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000452 {
Mark Slee30152872006-11-28 01:24:07 +0000453 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000454 if ($4 < 0) {
455 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000456 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000457 $$ = new t_enum_value($2, $4);
458 if ($1 != NULL) {
459 $$->set_doc($1);
460 }
Mark Sleed0767c52007-07-27 22:14:41 +0000461 if (g_parse_mode == PROGRAM) {
462 g_scope->add_constant($2, new t_const(g_type_i32, $2, new t_const_value($4)));
463 if (g_parent_scope != NULL) {
464 g_parent_scope->add_constant(g_parent_prefix + $2, new t_const(g_type_i32, $2, new t_const_value($4)));
465 }
466 }
Mark Slee31985722006-05-24 21:45:31 +0000467 }
468|
David Reisscbd4bac2007-08-14 17:12:33 +0000469 CaptureDocText tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000470 {
Mark Slee30152872006-11-28 01:24:07 +0000471 pdebug("EnumDef -> tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000472 $$ = new t_enum_value($2);
473 if ($1 != NULL) {
474 $$->set_doc($1);
475 }
Mark Slee30152872006-11-28 01:24:07 +0000476 }
477
Mark Slee6a47fed2007-02-07 02:40:59 +0000478Senum:
David Reisscdffe262007-08-14 17:12:31 +0000479 tok_senum tok_identifier '{' SenumDefList '}'
Mark Slee6a47fed2007-02-07 02:40:59 +0000480 {
481 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000482 $$ = new t_typedef(g_program, $4, $2);
Mark Slee6a47fed2007-02-07 02:40:59 +0000483 }
484
485SenumDefList:
486 SenumDefList SenumDef
487 {
488 pdebug("SenumDefList -> SenumDefList SenumDef");
489 $$ = $1;
490 $$->add_string_enum_val($2);
491 }
492|
493 {
494 pdebug("SenumDefList -> ");
495 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
496 $$->set_string_enum(true);
497 }
498
499SenumDef:
500 tok_literal CommaOrSemicolonOptional
501 {
502 pdebug("SenumDef -> tok_literal");
503 $$ = $1;
504 }
505
Mark Slee30152872006-11-28 01:24:07 +0000506Const:
507 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
508 {
509 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000510 if (g_parse_mode == PROGRAM) {
511 $$ = new t_const($2, $3, $5);
512 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000513
514 g_scope->add_constant($3, $$);
515 if (g_parent_scope != NULL) {
516 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
517 }
518
Mark Sleeaa7671d2006-11-29 03:19:31 +0000519 } else {
520 $$ = NULL;
521 }
Mark Slee30152872006-11-28 01:24:07 +0000522 }
523
524ConstValue:
525 tok_int_constant
526 {
527 pdebug("ConstValue => tok_int_constant");
528 $$ = new t_const_value();
529 $$->set_integer($1);
530 }
531| tok_dub_constant
532 {
533 pdebug("ConstValue => tok_dub_constant");
534 $$ = new t_const_value();
535 $$->set_double($1);
536 }
537| tok_literal
538 {
539 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000540 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000541 }
Mark Slee67fc6342006-11-29 03:37:04 +0000542| tok_identifier
543 {
544 pdebug("ConstValue => tok_identifier");
Mark Sleed0767c52007-07-27 22:14:41 +0000545 t_const* constant = g_scope->get_constant($1);
546 if (constant != NULL) {
547 $$ = constant->get_value();
548 } else {
549 if (g_parse_mode == PROGRAM) {
550 pwarning(1, "Constant strings should be quoted: %s\n", $1);
551 }
552 $$ = new t_const_value($1);
553 }
Mark Slee67fc6342006-11-29 03:37:04 +0000554 }
Mark Slee30152872006-11-28 01:24:07 +0000555| ConstList
556 {
557 pdebug("ConstValue => ConstList");
558 $$ = $1;
559 }
560| ConstMap
561 {
562 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000563 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000564 }
565
566ConstList:
567 '[' ConstListContents ']'
568 {
569 pdebug("ConstList => [ ConstListContents ]");
570 $$ = $2;
571 }
572
573ConstListContents:
574 ConstListContents ConstValue CommaOrSemicolonOptional
575 {
576 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
577 $$ = $1;
578 $$->add_list($2);
579 }
580|
581 {
582 pdebug("ConstListContents =>");
583 $$ = new t_const_value();
584 $$->set_list();
585 }
586
587ConstMap:
588 '{' ConstMapContents '}'
589 {
590 pdebug("ConstMap => { ConstMapContents }");
591 $$ = $2;
592 }
593
594ConstMapContents:
595 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
596 {
597 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
598 $$ = $1;
599 $$->add_map($2, $4);
600 }
601|
602 {
603 pdebug("ConstMapContents =>");
604 $$ = new t_const_value();
605 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000606 }
607
608Struct:
David Reisscdffe262007-08-14 17:12:31 +0000609 tok_struct tok_identifier XsdAll '{' FieldList '}'
Mark Slee31985722006-05-24 21:45:31 +0000610 {
611 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
David Reisscdffe262007-08-14 17:12:31 +0000612 $5->set_name($2);
613 $5->set_xsd_all($3);
614 $$ = $5;
Mark Slee9cb7c612006-09-01 22:17:45 +0000615 y_field_val = -1;
616 }
617
Mark Slee36bfa2e2007-01-19 20:09:51 +0000618XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000619 tok_xsd_all
620 {
621 $$ = true;
622 }
623|
624 {
625 $$ = false;
626 }
627
Mark Slee36bfa2e2007-01-19 20:09:51 +0000628XsdOptional:
629 tok_xsd_optional
630 {
631 $$ = true;
632 }
633|
634 {
635 $$ = false;
636 }
637
Mark Slee7df0e2a2007-02-06 21:03:18 +0000638XsdNillable:
639 tok_xsd_nillable
640 {
641 $$ = true;
642 }
643|
644 {
645 $$ = false;
646 }
647
Mark Slee21135c32007-02-05 21:52:08 +0000648XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000649 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000650 {
Mark Slee748d83f2007-02-07 01:20:08 +0000651 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000652 }
653|
654 {
655 $$ = NULL;
656 }
657
Mark Slee9cb7c612006-09-01 22:17:45 +0000658Xception:
659 tok_xception tok_identifier '{' FieldList '}'
660 {
661 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
662 $4->set_name($2);
663 $4->set_xception(true);
664 $$ = $4;
665 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000666 }
667
668Service:
Mark Slee78165722007-09-10 22:08:49 +0000669 tok_service tok_identifier Extends '{' FlagArgs FunctionList UnflagArgs '}'
Mark Slee31985722006-05-24 21:45:31 +0000670 {
671 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Mark Slee78165722007-09-10 22:08:49 +0000672 $$ = $6;
David Reisscdffe262007-08-14 17:12:31 +0000673 $$->set_name($2);
674 $$->set_extends($3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000675 }
676
Mark Slee78165722007-09-10 22:08:49 +0000677FlagArgs:
678 {
679 g_arglist = 1;
680 }
681
682UnflagArgs:
683 {
684 g_arglist = 0;
685 }
686
Mark Slee36bfa2e2007-01-19 20:09:51 +0000687Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000688 tok_extends tok_identifier
689 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000690 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000691 $$ = NULL;
692 if (g_parse_mode == PROGRAM) {
693 $$ = g_scope->get_service($2);
694 if ($$ == NULL) {
695 yyerror("Service \"%s\" has not been defined.", $2);
696 exit(1);
697 }
698 }
699 }
700|
701 {
702 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000703 }
704
705FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000706 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000707 {
708 pdebug("FunctionList -> FunctionList Function");
709 $$ = $1;
710 $1->add_function($2);
711 }
712|
713 {
714 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000715 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000716 }
717
718Function:
David Reisscbd4bac2007-08-14 17:12:33 +0000719 CaptureDocText Async FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000720 {
ccheeverf53b5cf2007-02-05 20:33:11 +0000721 $6->set_name(std::string($4) + "_args");
722 $$ = new t_function($3, $4, $6, $8, $2);
723 if ($1 != NULL) {
724 $$->set_doc($1);
725 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000726 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000727 }
728
Mark Slee36bfa2e2007-01-19 20:09:51 +0000729Async:
Mark Slee52f643d2006-08-09 00:03:43 +0000730 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000731 {
Mark Slee52f643d2006-08-09 00:03:43 +0000732 $$ = true;
733 }
734|
735 {
736 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000737 }
738
Mark Slee36bfa2e2007-01-19 20:09:51 +0000739Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000740 tok_throws '(' FieldList ')'
741 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000742 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000743 $$ = $3;
744 }
745|
746 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000747 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000748 }
749
Mark Slee31985722006-05-24 21:45:31 +0000750FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000751 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000752 {
753 pdebug("FieldList -> FieldList , Field");
754 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000755 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000756 }
757|
758 {
759 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000760 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000761 }
762
763Field:
David Reiss8320a922007-08-14 19:59:26 +0000764 CaptureDocText FieldIdentifier FieldRequiredness FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000765 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000766 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000767 if ($2 < 0) {
David Reiss8320a922007-08-14 19:59:26 +0000768 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 +0000769 }
David Reiss8320a922007-08-14 19:59:26 +0000770 $$ = new t_field($4, $5, $2);
771 $$->set_req($3);
772 if ($6 != NULL) {
773 validate_field_value($$, $6);
774 $$->set_value($6);
Mark Slee7ff32452007-02-01 05:26:18 +0000775 }
David Reiss8320a922007-08-14 19:59:26 +0000776 $$->set_xsd_optional($7);
777 $$->set_xsd_nillable($8);
ccheeverf53b5cf2007-02-05 20:33:11 +0000778 if ($1 != NULL) {
779 $$->set_doc($1);
780 }
David Reiss8320a922007-08-14 19:59:26 +0000781 if ($9 != NULL) {
782 $$->set_xsd_attrs($9);
Mark Slee21135c32007-02-05 21:52:08 +0000783 }
Mark Slee31985722006-05-24 21:45:31 +0000784 }
Mark Slee7ff32452007-02-01 05:26:18 +0000785
786FieldIdentifier:
787 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000788 {
Mark Slee7ff32452007-02-01 05:26:18 +0000789 if ($1 <= 0) {
790 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n", $1);
791 $1 = y_field_val--;
Mark Sleef0712dc2006-10-25 19:03:57 +0000792 }
Mark Slee7ff32452007-02-01 05:26:18 +0000793 $$ = $1;
794 }
795|
796 {
797 $$ = y_field_val--;
798 }
799
David Reiss8320a922007-08-14 19:59:26 +0000800FieldRequiredness:
801 tok_required
802 {
Mark Slee78165722007-09-10 22:08:49 +0000803 if (g_arglist) {
804 if (g_parse_mode == PROGRAM) {
805 pwarning(1, "required keyword is ignored in argument lists.\n");
806 }
807 $$ = t_field::OPT_IN_REQ_OUT;
808 } else {
809 $$ = t_field::REQUIRED;
810 }
David Reiss8320a922007-08-14 19:59:26 +0000811 }
812| tok_optional
813 {
Mark Slee78165722007-09-10 22:08:49 +0000814 if (g_arglist) {
815 if (g_parse_mode == PROGRAM) {
816 pwarning(1, "optional keyword is ignored in argument lists.\n");
817 }
818 $$ = t_field::OPT_IN_REQ_OUT;
819 } else {
820 $$ = t_field::OPTIONAL;
821 }
David Reiss8320a922007-08-14 19:59:26 +0000822 }
823|
824 {
825 $$ = t_field::OPT_IN_REQ_OUT;
826 }
827
Mark Slee7ff32452007-02-01 05:26:18 +0000828FieldValue:
829 '=' ConstValue
830 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000831 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +0000832 $$ = $2;
833 } else {
834 $$ = NULL;
835 }
836 }
837|
838 {
839 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000840 }
Mark Slee31985722006-05-24 21:45:31 +0000841
842DefinitionType:
843 BaseType
844 {
845 pdebug("DefinitionType -> BaseType");
846 $$ = $1;
847 }
Mark Sleee8540632006-05-30 09:24:40 +0000848| ContainerType
849 {
850 pdebug("DefinitionType -> ContainerType");
851 $$ = $1;
852 }
Mark Slee31985722006-05-24 21:45:31 +0000853
854FunctionType:
855 FieldType
856 {
Mark Sleee8540632006-05-30 09:24:40 +0000857 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000858 $$ = $1;
859 }
860| tok_void
861 {
Mark Sleee8540632006-05-30 09:24:40 +0000862 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000863 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000864 }
865
866FieldType:
867 tok_identifier
868 {
Mark Sleee8540632006-05-30 09:24:40 +0000869 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000870 if (g_parse_mode == INCLUDES) {
871 // Ignore identifiers in include mode
872 $$ = NULL;
873 } else {
874 // Lookup the identifier in the current scope
875 $$ = g_scope->get_type($1);
876 if ($$ == NULL) {
877 yyerror("Type \"%s\" has not been defined.", $1);
878 exit(1);
879 }
Mark Sleee8540632006-05-30 09:24:40 +0000880 }
Mark Slee31985722006-05-24 21:45:31 +0000881 }
882| BaseType
883 {
Mark Sleee8540632006-05-30 09:24:40 +0000884 pdebug("FieldType -> BaseType");
885 $$ = $1;
886 }
887| ContainerType
888 {
889 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000890 $$ = $1;
891 }
892
893BaseType:
894 tok_string
895 {
896 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000897 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000898 }
Mark Slee8d725a22007-04-13 01:57:12 +0000899| tok_binary
900 {
901 pdebug("BaseType -> tok_binary");
902 $$ = g_type_binary;
903 }
Mark Sleeb6200d82007-01-19 19:14:36 +0000904| tok_slist
905 {
906 pdebug("BaseType -> tok_slist");
907 $$ = g_type_slist;
908 }
Mark Slee78f58e22006-09-02 04:17:07 +0000909| tok_bool
910 {
911 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000912 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000913 }
Mark Slee31985722006-05-24 21:45:31 +0000914| tok_byte
915 {
916 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000917 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000918 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000919| tok_i16
920 {
921 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000922 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000923 }
Mark Slee31985722006-05-24 21:45:31 +0000924| tok_i32
925 {
926 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000927 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000928 }
Mark Slee31985722006-05-24 21:45:31 +0000929| tok_i64
930 {
931 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000932 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000933 }
Mark Sleec98d0502006-09-06 02:42:25 +0000934| tok_double
935 {
936 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000937 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000938 }
Mark Slee31985722006-05-24 21:45:31 +0000939
Mark Sleee8540632006-05-30 09:24:40 +0000940ContainerType:
941 MapType
942 {
943 pdebug("ContainerType -> MapType");
944 $$ = $1;
945 }
946| SetType
947 {
948 pdebug("ContainerType -> SetType");
949 $$ = $1;
950 }
951| ListType
952 {
953 pdebug("ContainerType -> ListType");
954 $$ = $1;
955 }
956
957MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000958 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000959 {
960 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000961 $$ = new t_map($4, $6);
962 if ($2 != NULL) {
963 ((t_container*)$$)->set_cpp_name(std::string($2));
964 }
Mark Sleee8540632006-05-30 09:24:40 +0000965 }
966
967SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000968 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000969 {
970 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000971 $$ = new t_set($4);
972 if ($2 != NULL) {
973 ((t_container*)$$)->set_cpp_name(std::string($2));
974 }
Mark Sleee8540632006-05-30 09:24:40 +0000975 }
976
977ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000978 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +0000979 {
980 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +0000981 $$ = new t_list($3);
982 if ($5 != NULL) {
983 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +0000984 }
985 }
986
Mark Slee36bfa2e2007-01-19 20:09:51 +0000987CppType:
Mark Sleeafc76542007-02-09 21:55:44 +0000988 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +0000989 {
Mark Sleeafc76542007-02-09 21:55:44 +0000990 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +0000991 }
992|
993 {
994 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +0000995 }
996
Mark Slee31985722006-05-24 21:45:31 +0000997%%