blob: c8be3f033aead758a1356a350f727d0f0ba3ebda [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;
Mark Slee31985722006-05-24 21:45:31 +000053}
54
Mark Sleef5377b32006-10-10 01:42:59 +000055/**
56 * Strings identifier
57 */
Mark Slee31985722006-05-24 21:45:31 +000058%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +000059%token<id> tok_literal
David Reisscdffe262007-08-14 17:12:31 +000060%token<dtext> tok_doctext
Mark Sleef5377b32006-10-10 01:42:59 +000061
62/**
Mark Slee30152872006-11-28 01:24:07 +000063 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +000064 */
Mark Slee31985722006-05-24 21:45:31 +000065%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +000066%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +000067
Mark Sleef5377b32006-10-10 01:42:59 +000068/**
Mark Sleef0712dc2006-10-25 19:03:57 +000069 * Header keywoards
Mark Sleef5377b32006-10-10 01:42:59 +000070 */
Mark Sleef0712dc2006-10-25 19:03:57 +000071%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +000072%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000073%token tok_cpp_namespace
74%token tok_cpp_include
75%token tok_cpp_type
Mark Sleee888b372007-01-12 01:06:24 +000076%token tok_php_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000077%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +000078%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +000079%token tok_xsd_optional
Mark Slee7df0e2a2007-02-06 21:03:18 +000080%token tok_xsd_nillable
Mark Slee0d9199e2007-01-31 02:08:30 +000081%token tok_xsd_namespace
Mark Slee21135c32007-02-05 21:52:08 +000082%token tok_xsd_attrs
Mark Slee58dfb4f2007-07-06 02:45:25 +000083%token tok_ruby_namespace
Mark Slee9cb7c612006-09-01 22:17:45 +000084
Mark Sleef5377b32006-10-10 01:42:59 +000085/**
86 * Base datatype keywords
87 */
88%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +000089%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +000090%token tok_byte
91%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +000092%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +000093%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +000094%token tok_senum
Mark Slee9cb7c612006-09-01 22:17:45 +000095%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +000096%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +000097%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +000098%token tok_double
Mark Slee31985722006-05-24 21:45:31 +000099
Mark Sleef5377b32006-10-10 01:42:59 +0000100/**
101 * Complex type keywords
102 */
Mark Slee31985722006-05-24 21:45:31 +0000103%token tok_map
104%token tok_list
105%token tok_set
106
Mark Sleef5377b32006-10-10 01:42:59 +0000107/**
108 * Function modifiers
109 */
Mark Slee31985722006-05-24 21:45:31 +0000110%token tok_async
111
Mark Sleef5377b32006-10-10 01:42:59 +0000112/**
113 * Thrift language keywords
114 */
Mark Slee31985722006-05-24 21:45:31 +0000115%token tok_typedef
116%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000117%token tok_xception
118%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000119%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000120%token tok_service
121%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000122%token tok_const
Mark Slee31985722006-05-24 21:45:31 +0000123
Mark Sleef5377b32006-10-10 01:42:59 +0000124/**
125 * Grammar nodes
126 */
127
Mark Slee31985722006-05-24 21:45:31 +0000128%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000129%type<ttype> ContainerType
130%type<ttype> MapType
131%type<ttype> SetType
132%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000133
David Reisscdffe262007-08-14 17:12:31 +0000134%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000135%type<ttype> TypeDefinition
136
Mark Slee31985722006-05-24 21:45:31 +0000137%type<ttypedef> Typedef
138%type<ttype> DefinitionType
139
140%type<tfield> Field
Mark Slee7ff32452007-02-01 05:26:18 +0000141%type<iconst> FieldIdentifier
Mark Slee31985722006-05-24 21:45:31 +0000142%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000143%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000144%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000145
146%type<tenum> Enum
147%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000148%type<tenumv> EnumDef
149
Mark Slee6a47fed2007-02-07 02:40:59 +0000150%type<ttypedef> Senum
151%type<tbase> SenumDefList
152%type<id> SenumDef
153
Mark Slee30152872006-11-28 01:24:07 +0000154%type<tconst> Const
155%type<tconstv> ConstValue
156%type<tconstv> ConstList
157%type<tconstv> ConstListContents
158%type<tconstv> ConstMap
159%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000160
161%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000162%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000163%type<tservice> Service
164
165%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000166%type<ttype> FunctionType
167%type<tservice> FunctionList
168
Mark Slee36bfa2e2007-01-19 20:09:51 +0000169%type<tstruct> Throws
170%type<tservice> Extends
171%type<tbool> Async
172%type<tbool> XsdAll
173%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000174%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000175%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000176%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000177
David Reisscbd4bac2007-08-14 17:12:33 +0000178%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000179
Mark Slee31985722006-05-24 21:45:31 +0000180%%
181
Mark Sleef5377b32006-10-10 01:42:59 +0000182/**
183 * Thrift Grammar Implementation.
184 *
185 * For the most part this source file works its way top down from what you
186 * might expect to find in a typical .thrift file, i.e. type definitions and
187 * namespaces up top followed by service definitions using those types.
188 */
Mark Slee31985722006-05-24 21:45:31 +0000189
190Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000191 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000192 {
193 pdebug("Program -> Headers DefinitionList");
David Reisscbd4bac2007-08-14 17:12:33 +0000194 /*
195 TODO(dreiss): Decide whether full-program doctext is worth the trouble.
David Reissc2532a92007-07-30 23:46:11 +0000196 if ($1 != NULL) {
197 g_program->set_doc($1);
198 }
David Reisscbd4bac2007-08-14 17:12:33 +0000199 */
200 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000201 }
202
David Reisscbd4bac2007-08-14 17:12:33 +0000203CaptureDocText:
204 {
205 if (g_parse_mode == PROGRAM) {
206 $$ = g_doctext;
207 g_doctext = NULL;
208 }
209 else {
210 $$ = NULL;
211 }
212 }
213
214/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
215DestroyDocText:
216 {
217 if (g_parse_mode == PROGRAM) {
218 clear_doctext();
219 }
220 }
221
222/* We have to DestroyDocText here, otherwise it catches the doctext
223 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000224HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000225 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000226 {
227 pdebug("HeaderList -> HeaderList Header");
228 }
229|
230 {
231 pdebug("HeaderList -> ");
232 }
233
234Header:
235 Include
236 {
237 pdebug("Header -> Include");
238 }
239| tok_namespace tok_identifier
240 {
241 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
242 if (g_parse_mode == PROGRAM) {
243 g_program->set_cpp_namespace($2);
244 g_program->set_java_package($2);
245 }
246 }
247| tok_cpp_namespace tok_identifier
248 {
249 pdebug("Header -> tok_cpp_namespace tok_identifier");
250 if (g_parse_mode == PROGRAM) {
251 g_program->set_cpp_namespace($2);
252 }
253 }
254| tok_cpp_include tok_literal
255 {
256 pdebug("Header -> tok_cpp_include tok_literal");
257 if (g_parse_mode == PROGRAM) {
258 g_program->add_cpp_include($2);
259 }
260 }
Mark Sleee888b372007-01-12 01:06:24 +0000261| tok_php_namespace tok_identifier
262 {
263 pdebug("Header -> tok_php_namespace tok_identifier");
264 if (g_parse_mode == PROGRAM) {
265 g_program->set_php_namespace($2);
266 }
267 }
Mark Slee58dfb4f2007-07-06 02:45:25 +0000268| tok_ruby_namespace tok_identifier
269 {
270 pdebug("Header -> tok_ruby_namespace tok_identifier");
271 if (g_parse_mode == PROGRAM) {
272 g_program->set_ruby_namespace($2);
273 }
274 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000275| tok_java_package tok_identifier
276 {
277 pdebug("Header -> tok_java_package tok_identifier");
278 if (g_parse_mode == PROGRAM) {
279 g_program->set_java_package($2);
280 }
281 }
Mark Slee0d9199e2007-01-31 02:08:30 +0000282| tok_xsd_namespace tok_literal
283 {
284 pdebug("Header -> tok_xsd_namespace tok_literal");
285 if (g_parse_mode == PROGRAM) {
286 g_program->set_xsd_namespace($2);
287 }
288 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000289
290Include:
291 tok_include tok_literal
292 {
293 pdebug("Include -> tok_include tok_literal");
294 if (g_parse_mode == INCLUDES) {
295 std::string path = include_file(std::string($2));
296 if (!path.empty()) {
297 g_program->add_include(path);
298 }
299 }
300 }
Mark Slee31985722006-05-24 21:45:31 +0000301
302DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000303 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000304 {
305 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000306 if ($2 != NULL && $3 != NULL) {
307 $3->set_doc($2);
308 }
Mark Slee31985722006-05-24 21:45:31 +0000309 }
310|
311 {
312 pdebug("DefinitionList -> ");
313 }
314
315Definition:
Mark Slee30152872006-11-28 01:24:07 +0000316 Const
317 {
318 pdebug("Definition -> Const");
319 if (g_parse_mode == PROGRAM) {
320 g_program->add_const($1);
321 }
David Reisscdffe262007-08-14 17:12:31 +0000322 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000323 }
324| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000325 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000326 pdebug("Definition -> TypeDefinition");
327 if (g_parse_mode == PROGRAM) {
328 g_scope->add_type($1->get_name(), $1);
329 if (g_parent_scope != NULL) {
330 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
331 }
332 }
David Reisscdffe262007-08-14 17:12:31 +0000333 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000334 }
Mark Slee31985722006-05-24 21:45:31 +0000335| Service
336 {
337 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000338 if (g_parse_mode == PROGRAM) {
339 g_scope->add_service($1->get_name(), $1);
340 if (g_parent_scope != NULL) {
341 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
342 }
343 g_program->add_service($1);
344 }
David Reisscdffe262007-08-14 17:12:31 +0000345 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000346 }
347
Mark Sleef0712dc2006-10-25 19:03:57 +0000348TypeDefinition:
349 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000350 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000351 pdebug("TypeDefinition -> Typedef");
352 if (g_parse_mode == PROGRAM) {
353 g_program->add_typedef($1);
354 }
355 }
356| Enum
357 {
358 pdebug("TypeDefinition -> Enum");
359 if (g_parse_mode == PROGRAM) {
360 g_program->add_enum($1);
361 }
362 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000363| Senum
364 {
365 pdebug("TypeDefinition -> Senum");
366 if (g_parse_mode == PROGRAM) {
367 g_program->add_typedef($1);
368 }
369 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000370| Struct
371 {
372 pdebug("TypeDefinition -> Struct");
373 if (g_parse_mode == PROGRAM) {
374 g_program->add_struct($1);
375 }
376 }
377| Xception
378 {
379 pdebug("TypeDefinition -> Xception");
380 if (g_parse_mode == PROGRAM) {
381 g_program->add_xception($1);
382 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000383 }
Mark Slee31985722006-05-24 21:45:31 +0000384
385Typedef:
David Reisscdffe262007-08-14 17:12:31 +0000386 tok_typedef DefinitionType tok_identifier
Mark Slee31985722006-05-24 21:45:31 +0000387 {
388 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
David Reisscdffe262007-08-14 17:12:31 +0000389 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000390 $$ = td;
391 }
392
Mark Slee6a47fed2007-02-07 02:40:59 +0000393CommaOrSemicolonOptional:
394 ','
395 {}
396| ';'
397 {}
398|
399 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000400
Mark Slee31985722006-05-24 21:45:31 +0000401Enum:
David Reisscdffe262007-08-14 17:12:31 +0000402 tok_enum tok_identifier '{' EnumDefList '}'
Mark Slee31985722006-05-24 21:45:31 +0000403 {
404 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000405 $$ = $4;
406 $$->set_name($2);
Mark Slee31985722006-05-24 21:45:31 +0000407 }
408
409EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000410 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000411 {
412 pdebug("EnumDefList -> EnumDefList EnumDef");
413 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000414 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000415 }
416|
417 {
418 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000419 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000420 }
421
422EnumDef:
David Reisscbd4bac2007-08-14 17:12:33 +0000423 CaptureDocText tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000424 {
Mark Slee30152872006-11-28 01:24:07 +0000425 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000426 if ($4 < 0) {
427 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000428 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000429 $$ = new t_enum_value($2, $4);
430 if ($1 != NULL) {
431 $$->set_doc($1);
432 }
Mark Sleed0767c52007-07-27 22:14:41 +0000433 if (g_parse_mode == PROGRAM) {
434 g_scope->add_constant($2, new t_const(g_type_i32, $2, new t_const_value($4)));
435 if (g_parent_scope != NULL) {
436 g_parent_scope->add_constant(g_parent_prefix + $2, new t_const(g_type_i32, $2, new t_const_value($4)));
437 }
438 }
Mark Slee31985722006-05-24 21:45:31 +0000439 }
440|
David Reisscbd4bac2007-08-14 17:12:33 +0000441 CaptureDocText tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000442 {
Mark Slee30152872006-11-28 01:24:07 +0000443 pdebug("EnumDef -> tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000444 $$ = new t_enum_value($2);
445 if ($1 != NULL) {
446 $$->set_doc($1);
447 }
Mark Slee30152872006-11-28 01:24:07 +0000448 }
449
Mark Slee6a47fed2007-02-07 02:40:59 +0000450Senum:
David Reisscdffe262007-08-14 17:12:31 +0000451 tok_senum tok_identifier '{' SenumDefList '}'
Mark Slee6a47fed2007-02-07 02:40:59 +0000452 {
453 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000454 $$ = new t_typedef(g_program, $4, $2);
Mark Slee6a47fed2007-02-07 02:40:59 +0000455 }
456
457SenumDefList:
458 SenumDefList SenumDef
459 {
460 pdebug("SenumDefList -> SenumDefList SenumDef");
461 $$ = $1;
462 $$->add_string_enum_val($2);
463 }
464|
465 {
466 pdebug("SenumDefList -> ");
467 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
468 $$->set_string_enum(true);
469 }
470
471SenumDef:
472 tok_literal CommaOrSemicolonOptional
473 {
474 pdebug("SenumDef -> tok_literal");
475 $$ = $1;
476 }
477
Mark Slee30152872006-11-28 01:24:07 +0000478Const:
479 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
480 {
481 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000482 if (g_parse_mode == PROGRAM) {
483 $$ = new t_const($2, $3, $5);
484 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000485
486 g_scope->add_constant($3, $$);
487 if (g_parent_scope != NULL) {
488 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
489 }
490
Mark Sleeaa7671d2006-11-29 03:19:31 +0000491 } else {
492 $$ = NULL;
493 }
Mark Slee30152872006-11-28 01:24:07 +0000494 }
495
496ConstValue:
497 tok_int_constant
498 {
499 pdebug("ConstValue => tok_int_constant");
500 $$ = new t_const_value();
501 $$->set_integer($1);
502 }
503| tok_dub_constant
504 {
505 pdebug("ConstValue => tok_dub_constant");
506 $$ = new t_const_value();
507 $$->set_double($1);
508 }
509| tok_literal
510 {
511 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000512 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000513 }
Mark Slee67fc6342006-11-29 03:37:04 +0000514| tok_identifier
515 {
516 pdebug("ConstValue => tok_identifier");
Mark Sleed0767c52007-07-27 22:14:41 +0000517 t_const* constant = g_scope->get_constant($1);
518 if (constant != NULL) {
519 $$ = constant->get_value();
520 } else {
521 if (g_parse_mode == PROGRAM) {
522 pwarning(1, "Constant strings should be quoted: %s\n", $1);
523 }
524 $$ = new t_const_value($1);
525 }
Mark Slee67fc6342006-11-29 03:37:04 +0000526 }
Mark Slee30152872006-11-28 01:24:07 +0000527| ConstList
528 {
529 pdebug("ConstValue => ConstList");
530 $$ = $1;
531 }
532| ConstMap
533 {
534 pdebug("ConstValue => ConstMap");
535 $$ = $1;
536 }
537
538ConstList:
539 '[' ConstListContents ']'
540 {
541 pdebug("ConstList => [ ConstListContents ]");
542 $$ = $2;
543 }
544
545ConstListContents:
546 ConstListContents ConstValue CommaOrSemicolonOptional
547 {
548 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
549 $$ = $1;
550 $$->add_list($2);
551 }
552|
553 {
554 pdebug("ConstListContents =>");
555 $$ = new t_const_value();
556 $$->set_list();
557 }
558
559ConstMap:
560 '{' ConstMapContents '}'
561 {
562 pdebug("ConstMap => { ConstMapContents }");
563 $$ = $2;
564 }
565
566ConstMapContents:
567 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
568 {
569 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
570 $$ = $1;
571 $$->add_map($2, $4);
572 }
573|
574 {
575 pdebug("ConstMapContents =>");
576 $$ = new t_const_value();
577 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000578 }
579
580Struct:
David Reisscdffe262007-08-14 17:12:31 +0000581 tok_struct tok_identifier XsdAll '{' FieldList '}'
Mark Slee31985722006-05-24 21:45:31 +0000582 {
583 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
David Reisscdffe262007-08-14 17:12:31 +0000584 $5->set_name($2);
585 $5->set_xsd_all($3);
586 $$ = $5;
Mark Slee9cb7c612006-09-01 22:17:45 +0000587 y_field_val = -1;
588 }
589
Mark Slee36bfa2e2007-01-19 20:09:51 +0000590XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000591 tok_xsd_all
592 {
593 $$ = true;
594 }
595|
596 {
597 $$ = false;
598 }
599
Mark Slee36bfa2e2007-01-19 20:09:51 +0000600XsdOptional:
601 tok_xsd_optional
602 {
603 $$ = true;
604 }
605|
606 {
607 $$ = false;
608 }
609
Mark Slee7df0e2a2007-02-06 21:03:18 +0000610XsdNillable:
611 tok_xsd_nillable
612 {
613 $$ = true;
614 }
615|
616 {
617 $$ = false;
618 }
619
Mark Slee21135c32007-02-05 21:52:08 +0000620XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000621 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000622 {
Mark Slee748d83f2007-02-07 01:20:08 +0000623 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000624 }
625|
626 {
627 $$ = NULL;
628 }
629
Mark Slee9cb7c612006-09-01 22:17:45 +0000630Xception:
631 tok_xception tok_identifier '{' FieldList '}'
632 {
633 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
634 $4->set_name($2);
635 $4->set_xception(true);
636 $$ = $4;
637 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000638 }
639
640Service:
David Reisscdffe262007-08-14 17:12:31 +0000641 tok_service tok_identifier Extends '{' FunctionList '}'
Mark Slee31985722006-05-24 21:45:31 +0000642 {
643 pdebug("Service -> tok_service tok_identifier { FunctionList }");
David Reisscdffe262007-08-14 17:12:31 +0000644 $$ = $5;
645 $$->set_name($2);
646 $$->set_extends($3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000647 }
648
Mark Slee36bfa2e2007-01-19 20:09:51 +0000649Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000650 tok_extends tok_identifier
651 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000652 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000653 $$ = NULL;
654 if (g_parse_mode == PROGRAM) {
655 $$ = g_scope->get_service($2);
656 if ($$ == NULL) {
657 yyerror("Service \"%s\" has not been defined.", $2);
658 exit(1);
659 }
660 }
661 }
662|
663 {
664 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000665 }
666
667FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000668 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000669 {
670 pdebug("FunctionList -> FunctionList Function");
671 $$ = $1;
672 $1->add_function($2);
673 }
674|
675 {
676 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000677 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000678 }
679
680Function:
David Reisscbd4bac2007-08-14 17:12:33 +0000681 CaptureDocText Async FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000682 {
ccheeverf53b5cf2007-02-05 20:33:11 +0000683 $6->set_name(std::string($4) + "_args");
684 $$ = new t_function($3, $4, $6, $8, $2);
685 if ($1 != NULL) {
686 $$->set_doc($1);
687 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000688 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000689 }
690
Mark Slee36bfa2e2007-01-19 20:09:51 +0000691Async:
Mark Slee52f643d2006-08-09 00:03:43 +0000692 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000693 {
Mark Slee52f643d2006-08-09 00:03:43 +0000694 $$ = true;
695 }
696|
697 {
698 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000699 }
700
Mark Slee36bfa2e2007-01-19 20:09:51 +0000701Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000702 tok_throws '(' FieldList ')'
703 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000704 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000705 $$ = $3;
706 }
707|
708 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000709 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000710 }
711
Mark Slee31985722006-05-24 21:45:31 +0000712FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000713 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000714 {
715 pdebug("FieldList -> FieldList , Field");
716 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000717 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000718 }
719|
720 {
721 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000722 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000723 }
724
725Field:
David Reisscbd4bac2007-08-14 17:12:33 +0000726 CaptureDocText FieldIdentifier FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000727 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000728 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000729 if ($2 < 0) {
Mark Slee21135c32007-02-05 21:52:08 +0000730 pwarning(2, "No field key specified for %s, resulting protocol may have conflicts or not be backwards compatible!\n", $4);
Mark Slee31985722006-05-24 21:45:31 +0000731 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000732 $$ = new t_field($3, $4, $2);
733 if ($5 != NULL) {
734 validate_field_value($$, $5);
735 $$->set_value($5);
Mark Slee7ff32452007-02-01 05:26:18 +0000736 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000737 $$->set_xsd_optional($6);
Mark Slee7df0e2a2007-02-06 21:03:18 +0000738 $$->set_xsd_nillable($7);
ccheeverf53b5cf2007-02-05 20:33:11 +0000739 if ($1 != NULL) {
740 $$->set_doc($1);
741 }
Mark Slee7df0e2a2007-02-06 21:03:18 +0000742 if ($8 != NULL) {
Mark Slee748d83f2007-02-07 01:20:08 +0000743 $$->set_xsd_attrs($8);
Mark Slee21135c32007-02-05 21:52:08 +0000744 }
Mark Slee31985722006-05-24 21:45:31 +0000745 }
Mark Slee7ff32452007-02-01 05:26:18 +0000746
747FieldIdentifier:
748 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000749 {
Mark Slee7ff32452007-02-01 05:26:18 +0000750 if ($1 <= 0) {
751 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n", $1);
752 $1 = y_field_val--;
Mark Sleef0712dc2006-10-25 19:03:57 +0000753 }
Mark Slee7ff32452007-02-01 05:26:18 +0000754 $$ = $1;
755 }
756|
757 {
758 $$ = y_field_val--;
759 }
760
761FieldValue:
762 '=' ConstValue
763 {
764 if (g_parse_mode == PROGRAM) {
765 $$ = $2;
766 } else {
767 $$ = NULL;
768 }
769 }
770|
771 {
772 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000773 }
Mark Slee31985722006-05-24 21:45:31 +0000774
775DefinitionType:
776 BaseType
777 {
778 pdebug("DefinitionType -> BaseType");
779 $$ = $1;
780 }
Mark Sleee8540632006-05-30 09:24:40 +0000781| ContainerType
782 {
783 pdebug("DefinitionType -> ContainerType");
784 $$ = $1;
785 }
Mark Slee31985722006-05-24 21:45:31 +0000786
787FunctionType:
788 FieldType
789 {
Mark Sleee8540632006-05-30 09:24:40 +0000790 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000791 $$ = $1;
792 }
793| tok_void
794 {
Mark Sleee8540632006-05-30 09:24:40 +0000795 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000796 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000797 }
798
799FieldType:
800 tok_identifier
801 {
Mark Sleee8540632006-05-30 09:24:40 +0000802 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000803 if (g_parse_mode == INCLUDES) {
804 // Ignore identifiers in include mode
805 $$ = NULL;
806 } else {
807 // Lookup the identifier in the current scope
808 $$ = g_scope->get_type($1);
809 if ($$ == NULL) {
810 yyerror("Type \"%s\" has not been defined.", $1);
811 exit(1);
812 }
Mark Sleee8540632006-05-30 09:24:40 +0000813 }
Mark Slee31985722006-05-24 21:45:31 +0000814 }
815| BaseType
816 {
Mark Sleee8540632006-05-30 09:24:40 +0000817 pdebug("FieldType -> BaseType");
818 $$ = $1;
819 }
820| ContainerType
821 {
822 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000823 $$ = $1;
824 }
825
826BaseType:
827 tok_string
828 {
829 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000830 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000831 }
Mark Slee8d725a22007-04-13 01:57:12 +0000832| tok_binary
833 {
834 pdebug("BaseType -> tok_binary");
835 $$ = g_type_binary;
836 }
Mark Sleeb6200d82007-01-19 19:14:36 +0000837| tok_slist
838 {
839 pdebug("BaseType -> tok_slist");
840 $$ = g_type_slist;
841 }
Mark Slee78f58e22006-09-02 04:17:07 +0000842| tok_bool
843 {
844 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000845 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000846 }
Mark Slee31985722006-05-24 21:45:31 +0000847| tok_byte
848 {
849 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000850 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000851 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000852| tok_i16
853 {
854 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000855 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000856 }
Mark Slee31985722006-05-24 21:45:31 +0000857| tok_i32
858 {
859 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000860 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000861 }
Mark Slee31985722006-05-24 21:45:31 +0000862| tok_i64
863 {
864 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000865 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000866 }
Mark Sleec98d0502006-09-06 02:42:25 +0000867| tok_double
868 {
869 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000870 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000871 }
Mark Slee31985722006-05-24 21:45:31 +0000872
Mark Sleee8540632006-05-30 09:24:40 +0000873ContainerType:
874 MapType
875 {
876 pdebug("ContainerType -> MapType");
877 $$ = $1;
878 }
879| SetType
880 {
881 pdebug("ContainerType -> SetType");
882 $$ = $1;
883 }
884| ListType
885 {
886 pdebug("ContainerType -> ListType");
887 $$ = $1;
888 }
889
890MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000891 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000892 {
893 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000894 $$ = new t_map($4, $6);
895 if ($2 != NULL) {
896 ((t_container*)$$)->set_cpp_name(std::string($2));
897 }
Mark Sleee8540632006-05-30 09:24:40 +0000898 }
899
900SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000901 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000902 {
903 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000904 $$ = new t_set($4);
905 if ($2 != NULL) {
906 ((t_container*)$$)->set_cpp_name(std::string($2));
907 }
Mark Sleee8540632006-05-30 09:24:40 +0000908 }
909
910ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000911 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +0000912 {
913 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +0000914 $$ = new t_list($3);
915 if ($5 != NULL) {
916 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +0000917 }
918 }
919
Mark Slee36bfa2e2007-01-19 20:09:51 +0000920CppType:
Mark Sleeafc76542007-02-09 21:55:44 +0000921 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +0000922 {
Mark Sleeafc76542007-02-09 21:55:44 +0000923 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +0000924 }
925|
926 {
927 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +0000928 }
929
Mark Slee31985722006-05-24 21:45:31 +0000930%%