blob: 38ca516238a0aec7737d1a75aecf93addddf4c3b [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
Mark Slee27ed6ec2007-08-16 01:26:31 +000078%token tok_perl_package
Mark Sleef0712dc2006-10-25 19:03:57 +000079%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +000080%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +000081%token tok_xsd_optional
Mark Slee7df0e2a2007-02-06 21:03:18 +000082%token tok_xsd_nillable
Mark Slee0d9199e2007-01-31 02:08:30 +000083%token tok_xsd_namespace
Mark Slee21135c32007-02-05 21:52:08 +000084%token tok_xsd_attrs
Mark Slee58dfb4f2007-07-06 02:45:25 +000085%token tok_ruby_namespace
Mark Slee9cb7c612006-09-01 22:17:45 +000086
Mark Sleef5377b32006-10-10 01:42:59 +000087/**
88 * Base datatype keywords
89 */
90%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +000091%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +000092%token tok_byte
93%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +000094%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +000095%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +000096%token tok_senum
Mark Slee9cb7c612006-09-01 22:17:45 +000097%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +000098%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +000099%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +0000100%token tok_double
Mark Slee31985722006-05-24 21:45:31 +0000101
Mark Sleef5377b32006-10-10 01:42:59 +0000102/**
103 * Complex type keywords
104 */
Mark Slee31985722006-05-24 21:45:31 +0000105%token tok_map
106%token tok_list
107%token tok_set
108
Mark Sleef5377b32006-10-10 01:42:59 +0000109/**
110 * Function modifiers
Mark Slee27ed6ec2007-08-16 01:26:31 +0000111 */
Mark Slee31985722006-05-24 21:45:31 +0000112%token tok_async
113
Mark Sleef5377b32006-10-10 01:42:59 +0000114/**
115 * Thrift language keywords
116 */
Mark Slee31985722006-05-24 21:45:31 +0000117%token tok_typedef
118%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000119%token tok_xception
120%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000121%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000122%token tok_service
123%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000124%token tok_const
David Reiss8320a922007-08-14 19:59:26 +0000125%token tok_required
126%token tok_optional
Mark Slee31985722006-05-24 21:45:31 +0000127
Mark Sleef5377b32006-10-10 01:42:59 +0000128/**
129 * Grammar nodes
130 */
131
Mark Slee31985722006-05-24 21:45:31 +0000132%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000133%type<ttype> ContainerType
134%type<ttype> MapType
135%type<ttype> SetType
136%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000137
David Reisscdffe262007-08-14 17:12:31 +0000138%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000139%type<ttype> TypeDefinition
140
Mark Slee31985722006-05-24 21:45:31 +0000141%type<ttypedef> Typedef
142%type<ttype> DefinitionType
143
144%type<tfield> Field
Mark Slee7ff32452007-02-01 05:26:18 +0000145%type<iconst> FieldIdentifier
David Reiss8320a922007-08-14 19:59:26 +0000146%type<ereq> FieldRequiredness
Mark Slee31985722006-05-24 21:45:31 +0000147%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000148%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000149%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000150
151%type<tenum> Enum
152%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000153%type<tenumv> EnumDef
154
Mark Slee6a47fed2007-02-07 02:40:59 +0000155%type<ttypedef> Senum
156%type<tbase> SenumDefList
157%type<id> SenumDef
158
Mark Slee30152872006-11-28 01:24:07 +0000159%type<tconst> Const
160%type<tconstv> ConstValue
161%type<tconstv> ConstList
162%type<tconstv> ConstListContents
163%type<tconstv> ConstMap
164%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000165
166%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000167%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000168%type<tservice> Service
169
170%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000171%type<ttype> FunctionType
172%type<tservice> FunctionList
173
Mark Slee36bfa2e2007-01-19 20:09:51 +0000174%type<tstruct> Throws
175%type<tservice> Extends
176%type<tbool> Async
177%type<tbool> XsdAll
178%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000179%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000180%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000181%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000182
David Reisscbd4bac2007-08-14 17:12:33 +0000183%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000184
Mark Slee31985722006-05-24 21:45:31 +0000185%%
186
Mark Sleef5377b32006-10-10 01:42:59 +0000187/**
188 * Thrift Grammar Implementation.
189 *
190 * For the most part this source file works its way top down from what you
191 * might expect to find in a typical .thrift file, i.e. type definitions and
192 * namespaces up top followed by service definitions using those types.
193 */
Mark Slee31985722006-05-24 21:45:31 +0000194
195Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000196 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000197 {
198 pdebug("Program -> Headers DefinitionList");
David Reisscbd4bac2007-08-14 17:12:33 +0000199 /*
200 TODO(dreiss): Decide whether full-program doctext is worth the trouble.
David Reissc2532a92007-07-30 23:46:11 +0000201 if ($1 != NULL) {
202 g_program->set_doc($1);
203 }
David Reisscbd4bac2007-08-14 17:12:33 +0000204 */
205 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000206 }
207
David Reisscbd4bac2007-08-14 17:12:33 +0000208CaptureDocText:
209 {
210 if (g_parse_mode == PROGRAM) {
211 $$ = g_doctext;
212 g_doctext = NULL;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000213 } else {
David Reisscbd4bac2007-08-14 17:12:33 +0000214 $$ = NULL;
215 }
216 }
217
218/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
219DestroyDocText:
220 {
221 if (g_parse_mode == PROGRAM) {
222 clear_doctext();
223 }
224 }
225
226/* We have to DestroyDocText here, otherwise it catches the doctext
227 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000228HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000229 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000230 {
231 pdebug("HeaderList -> HeaderList Header");
232 }
233|
234 {
235 pdebug("HeaderList -> ");
236 }
237
238Header:
239 Include
240 {
241 pdebug("Header -> Include");
242 }
243| tok_namespace tok_identifier
244 {
245 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
246 if (g_parse_mode == PROGRAM) {
247 g_program->set_cpp_namespace($2);
248 g_program->set_java_package($2);
249 }
250 }
251| tok_cpp_namespace tok_identifier
252 {
253 pdebug("Header -> tok_cpp_namespace tok_identifier");
254 if (g_parse_mode == PROGRAM) {
255 g_program->set_cpp_namespace($2);
256 }
257 }
258| tok_cpp_include tok_literal
259 {
260 pdebug("Header -> tok_cpp_include tok_literal");
261 if (g_parse_mode == PROGRAM) {
262 g_program->add_cpp_include($2);
263 }
264 }
Mark Sleee888b372007-01-12 01:06:24 +0000265| tok_php_namespace tok_identifier
266 {
267 pdebug("Header -> tok_php_namespace tok_identifier");
268 if (g_parse_mode == PROGRAM) {
269 g_program->set_php_namespace($2);
270 }
271 }
Mark Slee27ed6ec2007-08-16 01:26:31 +0000272| tok_perl_package tok_identifier
273 {
274 pdebug("Header -> tok_perl_namespace tok_identifier");
275 if (g_parse_mode == PROGRAM) {
276 g_program->set_perl_package($2);
277 }
278 }
Mark Slee58dfb4f2007-07-06 02:45:25 +0000279| tok_ruby_namespace tok_identifier
280 {
281 pdebug("Header -> tok_ruby_namespace tok_identifier");
282 if (g_parse_mode == PROGRAM) {
283 g_program->set_ruby_namespace($2);
284 }
285 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000286| tok_java_package tok_identifier
287 {
288 pdebug("Header -> tok_java_package tok_identifier");
289 if (g_parse_mode == PROGRAM) {
290 g_program->set_java_package($2);
291 }
292 }
Mark Slee0d9199e2007-01-31 02:08:30 +0000293| tok_xsd_namespace tok_literal
294 {
295 pdebug("Header -> tok_xsd_namespace tok_literal");
296 if (g_parse_mode == PROGRAM) {
297 g_program->set_xsd_namespace($2);
298 }
299 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000300
301Include:
302 tok_include tok_literal
303 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000304 pdebug("Include -> tok_include tok_literal");
Mark Sleef0712dc2006-10-25 19:03:57 +0000305 if (g_parse_mode == INCLUDES) {
306 std::string path = include_file(std::string($2));
307 if (!path.empty()) {
308 g_program->add_include(path);
309 }
310 }
311 }
Mark Slee31985722006-05-24 21:45:31 +0000312
313DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000314 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000315 {
316 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000317 if ($2 != NULL && $3 != NULL) {
318 $3->set_doc($2);
319 }
Mark Slee31985722006-05-24 21:45:31 +0000320 }
321|
322 {
323 pdebug("DefinitionList -> ");
324 }
325
326Definition:
Mark Slee30152872006-11-28 01:24:07 +0000327 Const
328 {
329 pdebug("Definition -> Const");
330 if (g_parse_mode == PROGRAM) {
331 g_program->add_const($1);
332 }
David Reisscdffe262007-08-14 17:12:31 +0000333 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000334 }
335| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000336 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000337 pdebug("Definition -> TypeDefinition");
338 if (g_parse_mode == PROGRAM) {
339 g_scope->add_type($1->get_name(), $1);
340 if (g_parent_scope != NULL) {
341 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
342 }
343 }
David Reisscdffe262007-08-14 17:12:31 +0000344 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000345 }
Mark Slee31985722006-05-24 21:45:31 +0000346| Service
347 {
348 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000349 if (g_parse_mode == PROGRAM) {
350 g_scope->add_service($1->get_name(), $1);
351 if (g_parent_scope != NULL) {
352 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
353 }
354 g_program->add_service($1);
355 }
David Reisscdffe262007-08-14 17:12:31 +0000356 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000357 }
358
Mark Sleef0712dc2006-10-25 19:03:57 +0000359TypeDefinition:
360 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000361 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000362 pdebug("TypeDefinition -> Typedef");
363 if (g_parse_mode == PROGRAM) {
364 g_program->add_typedef($1);
365 }
366 }
367| Enum
368 {
369 pdebug("TypeDefinition -> Enum");
370 if (g_parse_mode == PROGRAM) {
371 g_program->add_enum($1);
372 }
373 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000374| Senum
375 {
376 pdebug("TypeDefinition -> Senum");
377 if (g_parse_mode == PROGRAM) {
378 g_program->add_typedef($1);
379 }
380 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000381| Struct
382 {
383 pdebug("TypeDefinition -> Struct");
384 if (g_parse_mode == PROGRAM) {
385 g_program->add_struct($1);
386 }
387 }
388| Xception
Mark Slee27ed6ec2007-08-16 01:26:31 +0000389 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000390 pdebug("TypeDefinition -> Xception");
391 if (g_parse_mode == PROGRAM) {
392 g_program->add_xception($1);
393 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000394 }
Mark Slee31985722006-05-24 21:45:31 +0000395
396Typedef:
David Reisscdffe262007-08-14 17:12:31 +0000397 tok_typedef DefinitionType tok_identifier
Mark Slee31985722006-05-24 21:45:31 +0000398 {
399 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
David Reisscdffe262007-08-14 17:12:31 +0000400 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000401 $$ = td;
402 }
403
Mark Slee6a47fed2007-02-07 02:40:59 +0000404CommaOrSemicolonOptional:
405 ','
406 {}
407| ';'
408 {}
409|
410 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000411
Mark Slee31985722006-05-24 21:45:31 +0000412Enum:
David Reisscdffe262007-08-14 17:12:31 +0000413 tok_enum tok_identifier '{' EnumDefList '}'
Mark Slee31985722006-05-24 21:45:31 +0000414 {
415 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000416 $$ = $4;
417 $$->set_name($2);
Mark Slee31985722006-05-24 21:45:31 +0000418 }
419
420EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000421 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000422 {
423 pdebug("EnumDefList -> EnumDefList EnumDef");
424 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000425 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000426 }
427|
428 {
429 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000430 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000431 }
432
433EnumDef:
David Reisscbd4bac2007-08-14 17:12:33 +0000434 CaptureDocText tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000435 {
Mark Slee30152872006-11-28 01:24:07 +0000436 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000437 if ($4 < 0) {
438 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000439 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000440 $$ = new t_enum_value($2, $4);
441 if ($1 != NULL) {
442 $$->set_doc($1);
443 }
Mark Sleed0767c52007-07-27 22:14:41 +0000444 if (g_parse_mode == PROGRAM) {
445 g_scope->add_constant($2, new t_const(g_type_i32, $2, new t_const_value($4)));
446 if (g_parent_scope != NULL) {
447 g_parent_scope->add_constant(g_parent_prefix + $2, new t_const(g_type_i32, $2, new t_const_value($4)));
448 }
449 }
Mark Slee31985722006-05-24 21:45:31 +0000450 }
451|
David Reisscbd4bac2007-08-14 17:12:33 +0000452 CaptureDocText tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000453 {
Mark Slee30152872006-11-28 01:24:07 +0000454 pdebug("EnumDef -> tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000455 $$ = new t_enum_value($2);
456 if ($1 != NULL) {
457 $$->set_doc($1);
458 }
Mark Slee30152872006-11-28 01:24:07 +0000459 }
460
Mark Slee6a47fed2007-02-07 02:40:59 +0000461Senum:
David Reisscdffe262007-08-14 17:12:31 +0000462 tok_senum tok_identifier '{' SenumDefList '}'
Mark Slee6a47fed2007-02-07 02:40:59 +0000463 {
464 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000465 $$ = new t_typedef(g_program, $4, $2);
Mark Slee6a47fed2007-02-07 02:40:59 +0000466 }
467
468SenumDefList:
469 SenumDefList SenumDef
470 {
471 pdebug("SenumDefList -> SenumDefList SenumDef");
472 $$ = $1;
473 $$->add_string_enum_val($2);
474 }
475|
476 {
477 pdebug("SenumDefList -> ");
478 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
479 $$->set_string_enum(true);
480 }
481
482SenumDef:
483 tok_literal CommaOrSemicolonOptional
484 {
485 pdebug("SenumDef -> tok_literal");
486 $$ = $1;
487 }
488
Mark Slee30152872006-11-28 01:24:07 +0000489Const:
490 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
491 {
492 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000493 if (g_parse_mode == PROGRAM) {
494 $$ = new t_const($2, $3, $5);
495 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000496
497 g_scope->add_constant($3, $$);
498 if (g_parent_scope != NULL) {
499 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
500 }
501
Mark Sleeaa7671d2006-11-29 03:19:31 +0000502 } else {
503 $$ = NULL;
504 }
Mark Slee30152872006-11-28 01:24:07 +0000505 }
506
507ConstValue:
508 tok_int_constant
509 {
510 pdebug("ConstValue => tok_int_constant");
511 $$ = new t_const_value();
512 $$->set_integer($1);
513 }
514| tok_dub_constant
515 {
516 pdebug("ConstValue => tok_dub_constant");
517 $$ = new t_const_value();
518 $$->set_double($1);
519 }
520| tok_literal
521 {
522 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000523 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000524 }
Mark Slee67fc6342006-11-29 03:37:04 +0000525| tok_identifier
526 {
527 pdebug("ConstValue => tok_identifier");
Mark Sleed0767c52007-07-27 22:14:41 +0000528 t_const* constant = g_scope->get_constant($1);
529 if (constant != NULL) {
530 $$ = constant->get_value();
531 } else {
532 if (g_parse_mode == PROGRAM) {
533 pwarning(1, "Constant strings should be quoted: %s\n", $1);
534 }
535 $$ = new t_const_value($1);
536 }
Mark Slee67fc6342006-11-29 03:37:04 +0000537 }
Mark Slee30152872006-11-28 01:24:07 +0000538| ConstList
539 {
540 pdebug("ConstValue => ConstList");
541 $$ = $1;
542 }
543| ConstMap
544 {
545 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000546 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000547 }
548
549ConstList:
550 '[' ConstListContents ']'
551 {
552 pdebug("ConstList => [ ConstListContents ]");
553 $$ = $2;
554 }
555
556ConstListContents:
557 ConstListContents ConstValue CommaOrSemicolonOptional
558 {
559 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
560 $$ = $1;
561 $$->add_list($2);
562 }
563|
564 {
565 pdebug("ConstListContents =>");
566 $$ = new t_const_value();
567 $$->set_list();
568 }
569
570ConstMap:
571 '{' ConstMapContents '}'
572 {
573 pdebug("ConstMap => { ConstMapContents }");
574 $$ = $2;
575 }
576
577ConstMapContents:
578 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
579 {
580 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
581 $$ = $1;
582 $$->add_map($2, $4);
583 }
584|
585 {
586 pdebug("ConstMapContents =>");
587 $$ = new t_const_value();
588 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000589 }
590
591Struct:
David Reisscdffe262007-08-14 17:12:31 +0000592 tok_struct tok_identifier XsdAll '{' FieldList '}'
Mark Slee31985722006-05-24 21:45:31 +0000593 {
594 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
David Reisscdffe262007-08-14 17:12:31 +0000595 $5->set_name($2);
596 $5->set_xsd_all($3);
597 $$ = $5;
Mark Slee9cb7c612006-09-01 22:17:45 +0000598 y_field_val = -1;
599 }
600
Mark Slee36bfa2e2007-01-19 20:09:51 +0000601XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000602 tok_xsd_all
603 {
604 $$ = true;
605 }
606|
607 {
608 $$ = false;
609 }
610
Mark Slee36bfa2e2007-01-19 20:09:51 +0000611XsdOptional:
612 tok_xsd_optional
613 {
614 $$ = true;
615 }
616|
617 {
618 $$ = false;
619 }
620
Mark Slee7df0e2a2007-02-06 21:03:18 +0000621XsdNillable:
622 tok_xsd_nillable
623 {
624 $$ = true;
625 }
626|
627 {
628 $$ = false;
629 }
630
Mark Slee21135c32007-02-05 21:52:08 +0000631XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000632 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000633 {
Mark Slee748d83f2007-02-07 01:20:08 +0000634 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000635 }
636|
637 {
638 $$ = NULL;
639 }
640
Mark Slee9cb7c612006-09-01 22:17:45 +0000641Xception:
642 tok_xception tok_identifier '{' FieldList '}'
643 {
644 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
645 $4->set_name($2);
646 $4->set_xception(true);
647 $$ = $4;
648 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000649 }
650
651Service:
David Reisscdffe262007-08-14 17:12:31 +0000652 tok_service tok_identifier Extends '{' FunctionList '}'
Mark Slee31985722006-05-24 21:45:31 +0000653 {
654 pdebug("Service -> tok_service tok_identifier { FunctionList }");
David Reisscdffe262007-08-14 17:12:31 +0000655 $$ = $5;
656 $$->set_name($2);
657 $$->set_extends($3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000658 }
659
Mark Slee36bfa2e2007-01-19 20:09:51 +0000660Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000661 tok_extends tok_identifier
662 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000663 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000664 $$ = NULL;
665 if (g_parse_mode == PROGRAM) {
666 $$ = g_scope->get_service($2);
667 if ($$ == NULL) {
668 yyerror("Service \"%s\" has not been defined.", $2);
669 exit(1);
670 }
671 }
672 }
673|
674 {
675 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000676 }
677
678FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000679 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000680 {
681 pdebug("FunctionList -> FunctionList Function");
682 $$ = $1;
683 $1->add_function($2);
684 }
685|
686 {
687 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000688 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000689 }
690
691Function:
David Reisscbd4bac2007-08-14 17:12:33 +0000692 CaptureDocText Async FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000693 {
ccheeverf53b5cf2007-02-05 20:33:11 +0000694 $6->set_name(std::string($4) + "_args");
695 $$ = new t_function($3, $4, $6, $8, $2);
696 if ($1 != NULL) {
697 $$->set_doc($1);
698 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000699 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000700 }
701
Mark Slee36bfa2e2007-01-19 20:09:51 +0000702Async:
Mark Slee52f643d2006-08-09 00:03:43 +0000703 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000704 {
Mark Slee52f643d2006-08-09 00:03:43 +0000705 $$ = true;
706 }
707|
708 {
709 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000710 }
711
Mark Slee36bfa2e2007-01-19 20:09:51 +0000712Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000713 tok_throws '(' FieldList ')'
714 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000715 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000716 $$ = $3;
717 }
718|
719 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000720 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000721 }
722
Mark Slee31985722006-05-24 21:45:31 +0000723FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000724 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000725 {
726 pdebug("FieldList -> FieldList , Field");
727 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000728 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000729 }
730|
731 {
732 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000733 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000734 }
735
736Field:
David Reiss8320a922007-08-14 19:59:26 +0000737 CaptureDocText FieldIdentifier FieldRequiredness FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000738 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000739 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000740 if ($2 < 0) {
David Reiss8320a922007-08-14 19:59:26 +0000741 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 +0000742 }
David Reiss8320a922007-08-14 19:59:26 +0000743 $$ = new t_field($4, $5, $2);
744 $$->set_req($3);
745 if ($6 != NULL) {
746 validate_field_value($$, $6);
747 $$->set_value($6);
Mark Slee7ff32452007-02-01 05:26:18 +0000748 }
David Reiss8320a922007-08-14 19:59:26 +0000749 $$->set_xsd_optional($7);
750 $$->set_xsd_nillable($8);
ccheeverf53b5cf2007-02-05 20:33:11 +0000751 if ($1 != NULL) {
752 $$->set_doc($1);
753 }
David Reiss8320a922007-08-14 19:59:26 +0000754 if ($9 != NULL) {
755 $$->set_xsd_attrs($9);
Mark Slee21135c32007-02-05 21:52:08 +0000756 }
Mark Slee31985722006-05-24 21:45:31 +0000757 }
Mark Slee7ff32452007-02-01 05:26:18 +0000758
759FieldIdentifier:
760 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000761 {
Mark Slee7ff32452007-02-01 05:26:18 +0000762 if ($1 <= 0) {
763 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n", $1);
764 $1 = y_field_val--;
Mark Sleef0712dc2006-10-25 19:03:57 +0000765 }
Mark Slee7ff32452007-02-01 05:26:18 +0000766 $$ = $1;
767 }
768|
769 {
770 $$ = y_field_val--;
771 }
772
David Reiss8320a922007-08-14 19:59:26 +0000773FieldRequiredness:
774 tok_required
775 {
776 $$ = t_field::REQUIRED;
777 }
778| tok_optional
779 {
780 $$ = t_field::OPTIONAL;
781 }
782|
783 {
784 $$ = t_field::OPT_IN_REQ_OUT;
785 }
786
Mark Slee7ff32452007-02-01 05:26:18 +0000787FieldValue:
788 '=' ConstValue
789 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000790 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +0000791 $$ = $2;
792 } else {
793 $$ = NULL;
794 }
795 }
796|
797 {
798 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000799 }
Mark Slee31985722006-05-24 21:45:31 +0000800
801DefinitionType:
802 BaseType
803 {
804 pdebug("DefinitionType -> BaseType");
805 $$ = $1;
806 }
Mark Sleee8540632006-05-30 09:24:40 +0000807| ContainerType
808 {
809 pdebug("DefinitionType -> ContainerType");
810 $$ = $1;
811 }
Mark Slee31985722006-05-24 21:45:31 +0000812
813FunctionType:
814 FieldType
815 {
Mark Sleee8540632006-05-30 09:24:40 +0000816 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000817 $$ = $1;
818 }
819| tok_void
820 {
Mark Sleee8540632006-05-30 09:24:40 +0000821 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000822 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000823 }
824
825FieldType:
826 tok_identifier
827 {
Mark Sleee8540632006-05-30 09:24:40 +0000828 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000829 if (g_parse_mode == INCLUDES) {
830 // Ignore identifiers in include mode
831 $$ = NULL;
832 } else {
833 // Lookup the identifier in the current scope
834 $$ = g_scope->get_type($1);
835 if ($$ == NULL) {
836 yyerror("Type \"%s\" has not been defined.", $1);
837 exit(1);
838 }
Mark Sleee8540632006-05-30 09:24:40 +0000839 }
Mark Slee31985722006-05-24 21:45:31 +0000840 }
841| BaseType
842 {
Mark Sleee8540632006-05-30 09:24:40 +0000843 pdebug("FieldType -> BaseType");
844 $$ = $1;
845 }
846| ContainerType
847 {
848 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000849 $$ = $1;
850 }
851
852BaseType:
853 tok_string
854 {
855 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000856 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000857 }
Mark Slee8d725a22007-04-13 01:57:12 +0000858| tok_binary
859 {
860 pdebug("BaseType -> tok_binary");
861 $$ = g_type_binary;
862 }
Mark Sleeb6200d82007-01-19 19:14:36 +0000863| tok_slist
864 {
865 pdebug("BaseType -> tok_slist");
866 $$ = g_type_slist;
867 }
Mark Slee78f58e22006-09-02 04:17:07 +0000868| tok_bool
869 {
870 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000871 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000872 }
Mark Slee31985722006-05-24 21:45:31 +0000873| tok_byte
874 {
875 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000876 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000877 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000878| tok_i16
879 {
880 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000881 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000882 }
Mark Slee31985722006-05-24 21:45:31 +0000883| tok_i32
884 {
885 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000886 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000887 }
Mark Slee31985722006-05-24 21:45:31 +0000888| tok_i64
889 {
890 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000891 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000892 }
Mark Sleec98d0502006-09-06 02:42:25 +0000893| tok_double
894 {
895 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000896 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000897 }
Mark Slee31985722006-05-24 21:45:31 +0000898
Mark Sleee8540632006-05-30 09:24:40 +0000899ContainerType:
900 MapType
901 {
902 pdebug("ContainerType -> MapType");
903 $$ = $1;
904 }
905| SetType
906 {
907 pdebug("ContainerType -> SetType");
908 $$ = $1;
909 }
910| ListType
911 {
912 pdebug("ContainerType -> ListType");
913 $$ = $1;
914 }
915
916MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000917 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000918 {
919 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000920 $$ = new t_map($4, $6);
921 if ($2 != NULL) {
922 ((t_container*)$$)->set_cpp_name(std::string($2));
923 }
Mark Sleee8540632006-05-30 09:24:40 +0000924 }
925
926SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000927 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000928 {
929 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000930 $$ = new t_set($4);
931 if ($2 != NULL) {
932 ((t_container*)$$)->set_cpp_name(std::string($2));
933 }
Mark Sleee8540632006-05-30 09:24:40 +0000934 }
935
936ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000937 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +0000938 {
939 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +0000940 $$ = new t_list($3);
941 if ($5 != NULL) {
942 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +0000943 }
944 }
945
Mark Slee36bfa2e2007-01-19 20:09:51 +0000946CppType:
Mark Sleeafc76542007-02-09 21:55:44 +0000947 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +0000948 {
Mark Sleeafc76542007-02-09 21:55:44 +0000949 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +0000950 }
951|
952 {
953 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +0000954 }
955
Mark Slee31985722006-05-24 21:45:31 +0000956%%