blob: e46d0ef7dcaf6d852a6a1954b62709253d810f41 [file] [log] [blame]
Mark Slee31985722006-05-24 21:45:31 +00001%{
Mark Sleee9ce01c2007-05-16 02:29:53 +00002// Copyright (c) 2006- Facebook
3// Distributed under the Thrift Software License
4//
5// See accompanying file LICENSE or visit the Thrift site at:
6// http://developers.facebook.com/thrift/
Mark Slee31985722006-05-24 21:45:31 +00007
8/**
9 * Thrift parser.
10 *
11 * This parser is used on a thrift definition file.
12 *
13 * @author Mark Slee <mcslee@facebook.com>
14 */
15
16#include <stdio.h>
17#include "main.h"
18#include "globals.h"
19#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000020#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000021
Mark Sleef5377b32006-10-10 01:42:59 +000022/**
23 * This global variable is used for automatic numbering of field indices etc.
24 * when parsing the members of a struct. Field values are automatically
25 * assigned starting from -1 and working their way down.
26 */
Mark Slee9cb7c612006-09-01 22:17:45 +000027int y_field_val = -1;
Mark Slee78165722007-09-10 22:08:49 +000028int g_arglist = 0;
Mark Slee31985722006-05-24 21:45:31 +000029
30%}
31
Mark Sleef5377b32006-10-10 01:42:59 +000032/**
33 * This structure is used by the parser to hold the data types associated with
34 * various parse nodes.
35 */
Mark Slee31985722006-05-24 21:45:31 +000036%union {
Mark Slee30152872006-11-28 01:24:07 +000037 char* id;
38 int iconst;
39 double dconst;
40 bool tbool;
David Reisscdffe262007-08-14 17:12:31 +000041 t_doc* tdoc;
Mark Slee30152872006-11-28 01:24:07 +000042 t_type* ttype;
Mark Slee6a47fed2007-02-07 02:40:59 +000043 t_base_type* tbase;
Mark Slee30152872006-11-28 01:24:07 +000044 t_typedef* ttypedef;
45 t_enum* tenum;
46 t_enum_value* tenumv;
47 t_const* tconst;
48 t_const_value* tconstv;
49 t_struct* tstruct;
50 t_service* tservice;
51 t_function* tfunction;
52 t_field* tfield;
David Reisscdffe262007-08-14 17:12:31 +000053 char* dtext;
David Reiss8320a922007-08-14 19:59:26 +000054 t_field::e_req ereq;
Mark Slee31985722006-05-24 21:45:31 +000055}
56
Mark Sleef5377b32006-10-10 01:42:59 +000057/**
58 * Strings identifier
59 */
Mark Slee31985722006-05-24 21:45:31 +000060%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +000061%token<id> tok_literal
David Reisscdffe262007-08-14 17:12:31 +000062%token<dtext> tok_doctext
Mark Sleebd588222007-11-21 08:43:35 +000063%token<id> tok_st_identifier
Mark Sleef5377b32006-10-10 01:42:59 +000064
65/**
Mark Slee30152872006-11-28 01:24:07 +000066 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +000067 */
Mark Slee31985722006-05-24 21:45:31 +000068%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +000069%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +000070
Mark Sleef5377b32006-10-10 01:42:59 +000071/**
Mark Sleef0712dc2006-10-25 19:03:57 +000072 * Header keywoards
Mark Sleef5377b32006-10-10 01:42:59 +000073 */
Mark Sleef0712dc2006-10-25 19:03:57 +000074%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +000075%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000076%token tok_cpp_namespace
77%token tok_cpp_include
78%token tok_cpp_type
Mark Sleee888b372007-01-12 01:06:24 +000079%token tok_php_namespace
David Reissc6fc3292007-08-30 00:58:43 +000080%token tok_py_module
Mark Slee27ed6ec2007-08-16 01:26:31 +000081%token tok_perl_package
Mark Sleef0712dc2006-10-25 19:03:57 +000082%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +000083%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +000084%token tok_xsd_optional
Mark Slee7df0e2a2007-02-06 21:03:18 +000085%token tok_xsd_nillable
Mark Slee0d9199e2007-01-31 02:08:30 +000086%token tok_xsd_namespace
Mark Slee21135c32007-02-05 21:52:08 +000087%token tok_xsd_attrs
Mark Slee58dfb4f2007-07-06 02:45:25 +000088%token tok_ruby_namespace
Mark Sleebd588222007-11-21 08:43:35 +000089%token tok_smalltalk_category
David Reiss15457c92007-12-14 07:03:03 +000090%token tok_smalltalk_prefix
Mark Slee7e9eea42007-09-10 21:00:23 +000091%token tok_cocoa_prefix
Mark Slee9cb7c612006-09-01 22:17:45 +000092
Mark Sleef5377b32006-10-10 01:42:59 +000093/**
94 * Base datatype keywords
95 */
96%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +000097%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +000098%token tok_byte
99%token tok_string
Mark Slee8d725a22007-04-13 01:57:12 +0000100%token tok_binary
Mark Sleeb6200d82007-01-19 19:14:36 +0000101%token tok_slist
Mark Slee6a47fed2007-02-07 02:40:59 +0000102%token tok_senum
Mark Slee9cb7c612006-09-01 22:17:45 +0000103%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +0000104%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +0000105%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +0000106%token tok_double
Mark Slee31985722006-05-24 21:45:31 +0000107
Mark Sleef5377b32006-10-10 01:42:59 +0000108/**
109 * Complex type keywords
110 */
Mark Slee31985722006-05-24 21:45:31 +0000111%token tok_map
112%token tok_list
113%token tok_set
114
Mark Sleef5377b32006-10-10 01:42:59 +0000115/**
116 * Function modifiers
Mark Slee27ed6ec2007-08-16 01:26:31 +0000117 */
Mark Slee31985722006-05-24 21:45:31 +0000118%token tok_async
119
Mark Sleef5377b32006-10-10 01:42:59 +0000120/**
121 * Thrift language keywords
122 */
Mark Slee31985722006-05-24 21:45:31 +0000123%token tok_typedef
124%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000125%token tok_xception
126%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000127%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000128%token tok_service
129%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000130%token tok_const
David Reiss8320a922007-08-14 19:59:26 +0000131%token tok_required
132%token tok_optional
Mark Slee31985722006-05-24 21:45:31 +0000133
Mark Sleef5377b32006-10-10 01:42:59 +0000134/**
135 * Grammar nodes
136 */
137
Mark Slee31985722006-05-24 21:45:31 +0000138%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000139%type<ttype> ContainerType
140%type<ttype> MapType
141%type<ttype> SetType
142%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000143
David Reisscdffe262007-08-14 17:12:31 +0000144%type<tdoc> Definition
Mark Sleef0712dc2006-10-25 19:03:57 +0000145%type<ttype> TypeDefinition
146
Mark Slee31985722006-05-24 21:45:31 +0000147%type<ttypedef> Typedef
148%type<ttype> DefinitionType
149
150%type<tfield> Field
Mark Slee7ff32452007-02-01 05:26:18 +0000151%type<iconst> FieldIdentifier
David Reiss8320a922007-08-14 19:59:26 +0000152%type<ereq> FieldRequiredness
Mark Slee31985722006-05-24 21:45:31 +0000153%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000154%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000155%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000156
157%type<tenum> Enum
158%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000159%type<tenumv> EnumDef
160
Mark Slee6a47fed2007-02-07 02:40:59 +0000161%type<ttypedef> Senum
162%type<tbase> SenumDefList
163%type<id> SenumDef
164
Mark Slee30152872006-11-28 01:24:07 +0000165%type<tconst> Const
166%type<tconstv> ConstValue
167%type<tconstv> ConstList
168%type<tconstv> ConstListContents
169%type<tconstv> ConstMap
170%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000171
172%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000173%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000174%type<tservice> Service
175
176%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000177%type<ttype> FunctionType
178%type<tservice> FunctionList
179
Mark Slee36bfa2e2007-01-19 20:09:51 +0000180%type<tstruct> Throws
181%type<tservice> Extends
182%type<tbool> Async
183%type<tbool> XsdAll
184%type<tbool> XsdOptional
Mark Slee7df0e2a2007-02-06 21:03:18 +0000185%type<tbool> XsdNillable
Mark Slee748d83f2007-02-07 01:20:08 +0000186%type<tstruct> XsdAttributes
Mark Slee36bfa2e2007-01-19 20:09:51 +0000187%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000188
David Reisscbd4bac2007-08-14 17:12:33 +0000189%type<dtext> CaptureDocText
ccheeverf53b5cf2007-02-05 20:33:11 +0000190
Mark Slee31985722006-05-24 21:45:31 +0000191%%
192
Mark Sleef5377b32006-10-10 01:42:59 +0000193/**
194 * Thrift Grammar Implementation.
195 *
196 * For the most part this source file works its way top down from what you
197 * might expect to find in a typical .thrift file, i.e. type definitions and
198 * namespaces up top followed by service definitions using those types.
199 */
Mark Slee31985722006-05-24 21:45:31 +0000200
201Program:
David Reisscbd4bac2007-08-14 17:12:33 +0000202 HeaderList DefinitionList
Mark Sleef0712dc2006-10-25 19:03:57 +0000203 {
204 pdebug("Program -> Headers DefinitionList");
David Reisscbd4bac2007-08-14 17:12:33 +0000205 /*
206 TODO(dreiss): Decide whether full-program doctext is worth the trouble.
David Reissc2532a92007-07-30 23:46:11 +0000207 if ($1 != NULL) {
208 g_program->set_doc($1);
209 }
David Reisscbd4bac2007-08-14 17:12:33 +0000210 */
211 clear_doctext();
Mark Sleef0712dc2006-10-25 19:03:57 +0000212 }
213
David Reisscbd4bac2007-08-14 17:12:33 +0000214CaptureDocText:
215 {
216 if (g_parse_mode == PROGRAM) {
Mark Sleebd588222007-11-21 08:43:35 +0000217 $$ = g_doctext;
David Reisscbd4bac2007-08-14 17:12:33 +0000218 g_doctext = NULL;
Mark Slee27ed6ec2007-08-16 01:26:31 +0000219 } else {
David Reisscbd4bac2007-08-14 17:12:33 +0000220 $$ = NULL;
221 }
222 }
223
224/* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
225DestroyDocText:
226 {
227 if (g_parse_mode == PROGRAM) {
228 clear_doctext();
229 }
230 }
231
232/* We have to DestroyDocText here, otherwise it catches the doctext
233 on the first real element. */
Mark Sleef0712dc2006-10-25 19:03:57 +0000234HeaderList:
David Reisscbd4bac2007-08-14 17:12:33 +0000235 HeaderList DestroyDocText Header
Mark Sleef0712dc2006-10-25 19:03:57 +0000236 {
237 pdebug("HeaderList -> HeaderList Header");
238 }
239|
240 {
241 pdebug("HeaderList -> ");
242 }
243
244Header:
245 Include
246 {
247 pdebug("Header -> Include");
248 }
249| tok_namespace tok_identifier
250 {
251 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
252 if (g_parse_mode == PROGRAM) {
253 g_program->set_cpp_namespace($2);
254 g_program->set_java_package($2);
255 }
256 }
257| tok_cpp_namespace tok_identifier
258 {
259 pdebug("Header -> tok_cpp_namespace tok_identifier");
260 if (g_parse_mode == PROGRAM) {
261 g_program->set_cpp_namespace($2);
262 }
263 }
264| tok_cpp_include tok_literal
265 {
266 pdebug("Header -> tok_cpp_include tok_literal");
267 if (g_parse_mode == PROGRAM) {
268 g_program->add_cpp_include($2);
269 }
270 }
Mark Sleee888b372007-01-12 01:06:24 +0000271| tok_php_namespace tok_identifier
272 {
273 pdebug("Header -> tok_php_namespace tok_identifier");
274 if (g_parse_mode == PROGRAM) {
275 g_program->set_php_namespace($2);
276 }
277 }
David Reissc6fc3292007-08-30 00:58:43 +0000278| tok_py_module tok_identifier
279 {
280 pdebug("Header -> tok_py_module tok_identifier");
281 if (g_parse_mode == PROGRAM) {
282 g_program->set_py_module($2);
283 }
284 }
Mark Slee27ed6ec2007-08-16 01:26:31 +0000285| tok_perl_package tok_identifier
286 {
287 pdebug("Header -> tok_perl_namespace tok_identifier");
288 if (g_parse_mode == PROGRAM) {
289 g_program->set_perl_package($2);
290 }
291 }
Mark Slee58dfb4f2007-07-06 02:45:25 +0000292| tok_ruby_namespace tok_identifier
293 {
294 pdebug("Header -> tok_ruby_namespace tok_identifier");
295 if (g_parse_mode == PROGRAM) {
296 g_program->set_ruby_namespace($2);
297 }
298 }
Mark Sleebd588222007-11-21 08:43:35 +0000299| tok_smalltalk_category tok_st_identifier
300 {
301 pdebug("Header -> tok_smalltalk_category tok_st_identifier");
302 if (g_parse_mode == PROGRAM) {
303 g_program->set_smalltalk_category($2);
304 }
305 }
David Reiss15457c92007-12-14 07:03:03 +0000306| tok_smalltalk_prefix tok_identifier
307 {
308 pdebug("Header -> tok_smalltalk_prefix tok_identifier");
309 if (g_parse_mode == PROGRAM) {
310 g_program->set_smalltalk_prefix($2);
311 }
312 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000313| tok_java_package tok_identifier
314 {
315 pdebug("Header -> tok_java_package tok_identifier");
316 if (g_parse_mode == PROGRAM) {
317 g_program->set_java_package($2);
318 }
319 }
Mark Slee7e9eea42007-09-10 21:00:23 +0000320| tok_cocoa_prefix tok_identifier
321 {
322 pdebug("Header -> tok_cocoa_prefix tok_identifier");
323 if (g_parse_mode == PROGRAM) {
324 g_program->set_cocoa_prefix($2);
325 }
326 }
Mark Slee0d9199e2007-01-31 02:08:30 +0000327| tok_xsd_namespace tok_literal
328 {
329 pdebug("Header -> tok_xsd_namespace tok_literal");
330 if (g_parse_mode == PROGRAM) {
331 g_program->set_xsd_namespace($2);
332 }
333 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000334
335Include:
336 tok_include tok_literal
337 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000338 pdebug("Include -> tok_include tok_literal");
Mark Sleef0712dc2006-10-25 19:03:57 +0000339 if (g_parse_mode == INCLUDES) {
340 std::string path = include_file(std::string($2));
341 if (!path.empty()) {
342 g_program->add_include(path);
343 }
344 }
345 }
Mark Slee31985722006-05-24 21:45:31 +0000346
347DefinitionList:
David Reisscbd4bac2007-08-14 17:12:33 +0000348 DefinitionList CaptureDocText Definition
Mark Slee31985722006-05-24 21:45:31 +0000349 {
350 pdebug("DefinitionList -> DefinitionList Definition");
David Reisscdffe262007-08-14 17:12:31 +0000351 if ($2 != NULL && $3 != NULL) {
352 $3->set_doc($2);
353 }
Mark Slee31985722006-05-24 21:45:31 +0000354 }
355|
356 {
357 pdebug("DefinitionList -> ");
358 }
359
360Definition:
Mark Slee30152872006-11-28 01:24:07 +0000361 Const
362 {
363 pdebug("Definition -> Const");
364 if (g_parse_mode == PROGRAM) {
365 g_program->add_const($1);
Mark Sleebd588222007-11-21 08:43:35 +0000366 }
David Reisscdffe262007-08-14 17:12:31 +0000367 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000368 }
369| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000370 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000371 pdebug("Definition -> TypeDefinition");
372 if (g_parse_mode == PROGRAM) {
373 g_scope->add_type($1->get_name(), $1);
374 if (g_parent_scope != NULL) {
375 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
376 }
377 }
David Reisscdffe262007-08-14 17:12:31 +0000378 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000379 }
Mark Slee31985722006-05-24 21:45:31 +0000380| Service
381 {
382 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000383 if (g_parse_mode == PROGRAM) {
384 g_scope->add_service($1->get_name(), $1);
385 if (g_parent_scope != NULL) {
386 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
387 }
388 g_program->add_service($1);
389 }
David Reisscdffe262007-08-14 17:12:31 +0000390 $$ = $1;
Mark Slee9cb7c612006-09-01 22:17:45 +0000391 }
392
Mark Sleef0712dc2006-10-25 19:03:57 +0000393TypeDefinition:
394 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000395 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000396 pdebug("TypeDefinition -> Typedef");
397 if (g_parse_mode == PROGRAM) {
398 g_program->add_typedef($1);
399 }
400 }
401| Enum
402 {
403 pdebug("TypeDefinition -> Enum");
404 if (g_parse_mode == PROGRAM) {
405 g_program->add_enum($1);
406 }
407 }
Mark Slee6a47fed2007-02-07 02:40:59 +0000408| Senum
409 {
410 pdebug("TypeDefinition -> Senum");
411 if (g_parse_mode == PROGRAM) {
412 g_program->add_typedef($1);
413 }
414 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000415| Struct
416 {
417 pdebug("TypeDefinition -> Struct");
418 if (g_parse_mode == PROGRAM) {
419 g_program->add_struct($1);
420 }
421 }
422| Xception
Mark Slee27ed6ec2007-08-16 01:26:31 +0000423 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000424 pdebug("TypeDefinition -> Xception");
425 if (g_parse_mode == PROGRAM) {
426 g_program->add_xception($1);
427 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000428 }
Mark Slee31985722006-05-24 21:45:31 +0000429
430Typedef:
Mark Sleebd588222007-11-21 08:43:35 +0000431 tok_typedef DefinitionType tok_identifier
Mark Slee31985722006-05-24 21:45:31 +0000432 {
433 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
David Reisscdffe262007-08-14 17:12:31 +0000434 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000435 $$ = td;
436 }
437
Mark Slee6a47fed2007-02-07 02:40:59 +0000438CommaOrSemicolonOptional:
439 ','
440 {}
441| ';'
442 {}
443|
444 {}
ccheeverf53b5cf2007-02-05 20:33:11 +0000445
Mark Slee31985722006-05-24 21:45:31 +0000446Enum:
David Reisscdffe262007-08-14 17:12:31 +0000447 tok_enum tok_identifier '{' EnumDefList '}'
Mark Slee31985722006-05-24 21:45:31 +0000448 {
449 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000450 $$ = $4;
451 $$->set_name($2);
Mark Slee31985722006-05-24 21:45:31 +0000452 }
453
454EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000455 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000456 {
457 pdebug("EnumDefList -> EnumDefList EnumDef");
458 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000459 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000460 }
461|
462 {
463 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000464 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000465 }
466
467EnumDef:
David Reisscbd4bac2007-08-14 17:12:33 +0000468 CaptureDocText tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000469 {
Mark Slee30152872006-11-28 01:24:07 +0000470 pdebug("EnumDef -> tok_identifier = tok_int_constant");
ccheeverf53b5cf2007-02-05 20:33:11 +0000471 if ($4 < 0) {
472 pwarning(1, "Negative value supplied for enum %s.\n", $2);
Mark Slee31985722006-05-24 21:45:31 +0000473 }
ccheeverf53b5cf2007-02-05 20:33:11 +0000474 $$ = new t_enum_value($2, $4);
475 if ($1 != NULL) {
476 $$->set_doc($1);
477 }
Mark Sleed0767c52007-07-27 22:14:41 +0000478 if (g_parse_mode == PROGRAM) {
479 g_scope->add_constant($2, new t_const(g_type_i32, $2, new t_const_value($4)));
480 if (g_parent_scope != NULL) {
481 g_parent_scope->add_constant(g_parent_prefix + $2, new t_const(g_type_i32, $2, new t_const_value($4)));
482 }
483 }
Mark Slee31985722006-05-24 21:45:31 +0000484 }
485|
David Reisscbd4bac2007-08-14 17:12:33 +0000486 CaptureDocText tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000487 {
Mark Slee30152872006-11-28 01:24:07 +0000488 pdebug("EnumDef -> tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000489 $$ = new t_enum_value($2);
490 if ($1 != NULL) {
491 $$->set_doc($1);
492 }
Mark Slee30152872006-11-28 01:24:07 +0000493 }
494
Mark Slee6a47fed2007-02-07 02:40:59 +0000495Senum:
David Reisscdffe262007-08-14 17:12:31 +0000496 tok_senum tok_identifier '{' SenumDefList '}'
Mark Slee6a47fed2007-02-07 02:40:59 +0000497 {
498 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
David Reisscdffe262007-08-14 17:12:31 +0000499 $$ = new t_typedef(g_program, $4, $2);
Mark Slee6a47fed2007-02-07 02:40:59 +0000500 }
501
502SenumDefList:
503 SenumDefList SenumDef
504 {
505 pdebug("SenumDefList -> SenumDefList SenumDef");
506 $$ = $1;
507 $$->add_string_enum_val($2);
508 }
509|
510 {
511 pdebug("SenumDefList -> ");
512 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
513 $$->set_string_enum(true);
514 }
515
516SenumDef:
517 tok_literal CommaOrSemicolonOptional
518 {
519 pdebug("SenumDef -> tok_literal");
520 $$ = $1;
521 }
522
Mark Slee30152872006-11-28 01:24:07 +0000523Const:
524 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
525 {
526 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000527 if (g_parse_mode == PROGRAM) {
528 $$ = new t_const($2, $3, $5);
529 validate_const_type($$);
Mark Sleed0767c52007-07-27 22:14:41 +0000530
531 g_scope->add_constant($3, $$);
532 if (g_parent_scope != NULL) {
533 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
534 }
535
Mark Sleeaa7671d2006-11-29 03:19:31 +0000536 } else {
537 $$ = NULL;
538 }
Mark Slee30152872006-11-28 01:24:07 +0000539 }
540
541ConstValue:
542 tok_int_constant
543 {
544 pdebug("ConstValue => tok_int_constant");
545 $$ = new t_const_value();
546 $$->set_integer($1);
547 }
548| tok_dub_constant
549 {
550 pdebug("ConstValue => tok_dub_constant");
551 $$ = new t_const_value();
552 $$->set_double($1);
553 }
554| tok_literal
555 {
556 pdebug("ConstValue => tok_literal");
Mark Sleed0767c52007-07-27 22:14:41 +0000557 $$ = new t_const_value($1);
Mark Slee30152872006-11-28 01:24:07 +0000558 }
Mark Slee67fc6342006-11-29 03:37:04 +0000559| tok_identifier
560 {
561 pdebug("ConstValue => tok_identifier");
Mark Sleed0767c52007-07-27 22:14:41 +0000562 t_const* constant = g_scope->get_constant($1);
563 if (constant != NULL) {
564 $$ = constant->get_value();
565 } else {
566 if (g_parse_mode == PROGRAM) {
567 pwarning(1, "Constant strings should be quoted: %s\n", $1);
568 }
569 $$ = new t_const_value($1);
570 }
Mark Slee67fc6342006-11-29 03:37:04 +0000571 }
Mark Slee30152872006-11-28 01:24:07 +0000572| ConstList
573 {
574 pdebug("ConstValue => ConstList");
575 $$ = $1;
576 }
577| ConstMap
578 {
579 pdebug("ConstValue => ConstMap");
Mark Slee27ed6ec2007-08-16 01:26:31 +0000580 $$ = $1;
Mark Slee30152872006-11-28 01:24:07 +0000581 }
582
583ConstList:
584 '[' ConstListContents ']'
585 {
586 pdebug("ConstList => [ ConstListContents ]");
587 $$ = $2;
588 }
589
590ConstListContents:
591 ConstListContents ConstValue CommaOrSemicolonOptional
592 {
593 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
594 $$ = $1;
595 $$->add_list($2);
596 }
597|
598 {
599 pdebug("ConstListContents =>");
600 $$ = new t_const_value();
601 $$->set_list();
602 }
603
604ConstMap:
605 '{' ConstMapContents '}'
606 {
607 pdebug("ConstMap => { ConstMapContents }");
608 $$ = $2;
609 }
610
611ConstMapContents:
612 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
613 {
614 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
615 $$ = $1;
616 $$->add_map($2, $4);
617 }
618|
619 {
620 pdebug("ConstMapContents =>");
621 $$ = new t_const_value();
622 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000623 }
624
625Struct:
David Reisscdffe262007-08-14 17:12:31 +0000626 tok_struct tok_identifier XsdAll '{' FieldList '}'
Mark Slee31985722006-05-24 21:45:31 +0000627 {
628 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
David Reisscdffe262007-08-14 17:12:31 +0000629 $5->set_name($2);
630 $5->set_xsd_all($3);
631 $$ = $5;
Mark Slee9cb7c612006-09-01 22:17:45 +0000632 y_field_val = -1;
633 }
634
Mark Slee36bfa2e2007-01-19 20:09:51 +0000635XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000636 tok_xsd_all
637 {
638 $$ = true;
639 }
640|
641 {
642 $$ = false;
643 }
644
Mark Slee36bfa2e2007-01-19 20:09:51 +0000645XsdOptional:
646 tok_xsd_optional
647 {
648 $$ = true;
649 }
650|
651 {
652 $$ = false;
653 }
654
Mark Slee7df0e2a2007-02-06 21:03:18 +0000655XsdNillable:
656 tok_xsd_nillable
657 {
658 $$ = true;
659 }
660|
661 {
662 $$ = false;
663 }
664
Mark Slee21135c32007-02-05 21:52:08 +0000665XsdAttributes:
Mark Slee748d83f2007-02-07 01:20:08 +0000666 tok_xsd_attrs '{' FieldList '}'
Mark Slee21135c32007-02-05 21:52:08 +0000667 {
Mark Slee748d83f2007-02-07 01:20:08 +0000668 $$ = $3;
Mark Slee21135c32007-02-05 21:52:08 +0000669 }
670|
671 {
672 $$ = NULL;
673 }
674
Mark Slee9cb7c612006-09-01 22:17:45 +0000675Xception:
676 tok_xception tok_identifier '{' FieldList '}'
677 {
678 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
679 $4->set_name($2);
680 $4->set_xception(true);
681 $$ = $4;
682 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000683 }
684
685Service:
Mark Slee78165722007-09-10 22:08:49 +0000686 tok_service tok_identifier Extends '{' FlagArgs FunctionList UnflagArgs '}'
Mark Slee31985722006-05-24 21:45:31 +0000687 {
688 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Mark Slee78165722007-09-10 22:08:49 +0000689 $$ = $6;
David Reisscdffe262007-08-14 17:12:31 +0000690 $$->set_name($2);
691 $$->set_extends($3);
Mark Sleef0712dc2006-10-25 19:03:57 +0000692 }
693
Mark Slee78165722007-09-10 22:08:49 +0000694FlagArgs:
695 {
696 g_arglist = 1;
697 }
698
699UnflagArgs:
700 {
701 g_arglist = 0;
702 }
703
Mark Slee36bfa2e2007-01-19 20:09:51 +0000704Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000705 tok_extends tok_identifier
706 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000707 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000708 $$ = NULL;
709 if (g_parse_mode == PROGRAM) {
710 $$ = g_scope->get_service($2);
711 if ($$ == NULL) {
712 yyerror("Service \"%s\" has not been defined.", $2);
713 exit(1);
714 }
715 }
716 }
717|
718 {
719 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000720 }
721
722FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000723 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000724 {
725 pdebug("FunctionList -> FunctionList Function");
726 $$ = $1;
727 $1->add_function($2);
728 }
729|
730 {
731 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000732 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000733 }
734
735Function:
David Reisscbd4bac2007-08-14 17:12:33 +0000736 CaptureDocText Async FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000737 {
ccheeverf53b5cf2007-02-05 20:33:11 +0000738 $6->set_name(std::string($4) + "_args");
739 $$ = new t_function($3, $4, $6, $8, $2);
740 if ($1 != NULL) {
741 $$->set_doc($1);
742 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000743 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000744 }
745
Mark Slee36bfa2e2007-01-19 20:09:51 +0000746Async:
Mark Slee52f643d2006-08-09 00:03:43 +0000747 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000748 {
Mark Slee52f643d2006-08-09 00:03:43 +0000749 $$ = true;
750 }
751|
752 {
753 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000754 }
755
Mark Slee36bfa2e2007-01-19 20:09:51 +0000756Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000757 tok_throws '(' FieldList ')'
758 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000759 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000760 $$ = $3;
761 }
762|
763 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000764 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000765 }
766
Mark Slee31985722006-05-24 21:45:31 +0000767FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000768 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000769 {
770 pdebug("FieldList -> FieldList , Field");
771 $$ = $1;
Mark Slee6f9ac3f2007-11-28 22:28:13 +0000772 if (!($$->validate_field($2))) {
773 yyerror("Field identifier %d for \"%s\" has already been used", $2->get_key(), $2->get_name().c_str());
774 exit(1);
775 }
Mark Slee207cb462006-11-02 18:43:12 +0000776 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000777 }
778|
779 {
780 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000781 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000782 }
783
784Field:
David Reiss8320a922007-08-14 19:59:26 +0000785 CaptureDocText FieldIdentifier FieldRequiredness FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000786 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000787 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
ccheeverf53b5cf2007-02-05 20:33:11 +0000788 if ($2 < 0) {
David Reiss8320a922007-08-14 19:59:26 +0000789 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 +0000790 }
David Reiss8320a922007-08-14 19:59:26 +0000791 $$ = new t_field($4, $5, $2);
792 $$->set_req($3);
793 if ($6 != NULL) {
794 validate_field_value($$, $6);
795 $$->set_value($6);
Mark Slee7ff32452007-02-01 05:26:18 +0000796 }
David Reiss8320a922007-08-14 19:59:26 +0000797 $$->set_xsd_optional($7);
798 $$->set_xsd_nillable($8);
ccheeverf53b5cf2007-02-05 20:33:11 +0000799 if ($1 != NULL) {
800 $$->set_doc($1);
801 }
David Reiss8320a922007-08-14 19:59:26 +0000802 if ($9 != NULL) {
803 $$->set_xsd_attrs($9);
Mark Slee21135c32007-02-05 21:52:08 +0000804 }
Mark Slee31985722006-05-24 21:45:31 +0000805 }
Mark Slee7ff32452007-02-01 05:26:18 +0000806
807FieldIdentifier:
808 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000809 {
Mark Slee7ff32452007-02-01 05:26:18 +0000810 if ($1 <= 0) {
811 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n", $1);
812 $1 = y_field_val--;
Mark Sleef0712dc2006-10-25 19:03:57 +0000813 }
Mark Slee7ff32452007-02-01 05:26:18 +0000814 $$ = $1;
815 }
816|
817 {
818 $$ = y_field_val--;
819 }
820
David Reiss8320a922007-08-14 19:59:26 +0000821FieldRequiredness:
822 tok_required
823 {
Mark Slee78165722007-09-10 22:08:49 +0000824 if (g_arglist) {
825 if (g_parse_mode == PROGRAM) {
826 pwarning(1, "required keyword is ignored in argument lists.\n");
827 }
828 $$ = t_field::OPT_IN_REQ_OUT;
829 } else {
830 $$ = t_field::REQUIRED;
831 }
David Reiss8320a922007-08-14 19:59:26 +0000832 }
833| tok_optional
834 {
Mark Slee78165722007-09-10 22:08:49 +0000835 if (g_arglist) {
836 if (g_parse_mode == PROGRAM) {
837 pwarning(1, "optional keyword is ignored in argument lists.\n");
838 }
839 $$ = t_field::OPT_IN_REQ_OUT;
840 } else {
841 $$ = t_field::OPTIONAL;
842 }
David Reiss8320a922007-08-14 19:59:26 +0000843 }
844|
845 {
846 $$ = t_field::OPT_IN_REQ_OUT;
847 }
848
Mark Slee7ff32452007-02-01 05:26:18 +0000849FieldValue:
850 '=' ConstValue
851 {
Mark Slee27ed6ec2007-08-16 01:26:31 +0000852 if (g_parse_mode == PROGRAM) {
Mark Slee7ff32452007-02-01 05:26:18 +0000853 $$ = $2;
854 } else {
855 $$ = NULL;
856 }
857 }
858|
859 {
860 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000861 }
Mark Slee31985722006-05-24 21:45:31 +0000862
863DefinitionType:
864 BaseType
865 {
866 pdebug("DefinitionType -> BaseType");
867 $$ = $1;
868 }
Mark Sleee8540632006-05-30 09:24:40 +0000869| ContainerType
870 {
871 pdebug("DefinitionType -> ContainerType");
872 $$ = $1;
873 }
Mark Slee31985722006-05-24 21:45:31 +0000874
875FunctionType:
876 FieldType
877 {
Mark Sleee8540632006-05-30 09:24:40 +0000878 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000879 $$ = $1;
880 }
881| tok_void
882 {
Mark Sleee8540632006-05-30 09:24:40 +0000883 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000884 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000885 }
886
887FieldType:
888 tok_identifier
889 {
Mark Sleee8540632006-05-30 09:24:40 +0000890 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000891 if (g_parse_mode == INCLUDES) {
892 // Ignore identifiers in include mode
893 $$ = NULL;
894 } else {
895 // Lookup the identifier in the current scope
896 $$ = g_scope->get_type($1);
897 if ($$ == NULL) {
898 yyerror("Type \"%s\" has not been defined.", $1);
899 exit(1);
900 }
Mark Sleee8540632006-05-30 09:24:40 +0000901 }
Mark Slee31985722006-05-24 21:45:31 +0000902 }
903| BaseType
904 {
Mark Sleee8540632006-05-30 09:24:40 +0000905 pdebug("FieldType -> BaseType");
906 $$ = $1;
907 }
908| ContainerType
909 {
910 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000911 $$ = $1;
912 }
913
914BaseType:
915 tok_string
916 {
917 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000918 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000919 }
Mark Slee8d725a22007-04-13 01:57:12 +0000920| tok_binary
921 {
922 pdebug("BaseType -> tok_binary");
923 $$ = g_type_binary;
924 }
Mark Sleeb6200d82007-01-19 19:14:36 +0000925| tok_slist
926 {
927 pdebug("BaseType -> tok_slist");
928 $$ = g_type_slist;
929 }
Mark Slee78f58e22006-09-02 04:17:07 +0000930| tok_bool
931 {
932 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000933 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000934 }
Mark Slee31985722006-05-24 21:45:31 +0000935| tok_byte
936 {
937 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000938 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000939 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000940| tok_i16
941 {
942 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000943 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000944 }
Mark Slee31985722006-05-24 21:45:31 +0000945| tok_i32
946 {
947 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000948 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000949 }
Mark Slee31985722006-05-24 21:45:31 +0000950| tok_i64
951 {
952 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000953 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000954 }
Mark Sleec98d0502006-09-06 02:42:25 +0000955| tok_double
956 {
957 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000958 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000959 }
Mark Slee31985722006-05-24 21:45:31 +0000960
Mark Sleee8540632006-05-30 09:24:40 +0000961ContainerType:
962 MapType
963 {
964 pdebug("ContainerType -> MapType");
965 $$ = $1;
966 }
967| SetType
968 {
969 pdebug("ContainerType -> SetType");
970 $$ = $1;
971 }
972| ListType
973 {
974 pdebug("ContainerType -> ListType");
975 $$ = $1;
976 }
977
978MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000979 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000980 {
981 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000982 $$ = new t_map($4, $6);
983 if ($2 != NULL) {
984 ((t_container*)$$)->set_cpp_name(std::string($2));
985 }
Mark Sleee8540632006-05-30 09:24:40 +0000986 }
987
988SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000989 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000990 {
991 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000992 $$ = new t_set($4);
993 if ($2 != NULL) {
994 ((t_container*)$$)->set_cpp_name(std::string($2));
995 }
Mark Sleee8540632006-05-30 09:24:40 +0000996 }
997
998ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000999 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +00001000 {
1001 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +00001002 $$ = new t_list($3);
1003 if ($5 != NULL) {
1004 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +00001005 }
1006 }
1007
Mark Slee36bfa2e2007-01-19 20:09:51 +00001008CppType:
Mark Sleeafc76542007-02-09 21:55:44 +00001009 tok_cpp_type tok_literal
Mark Slee4f8da1d2006-10-12 02:47:27 +00001010 {
Mark Sleeafc76542007-02-09 21:55:44 +00001011 $$ = $2;
Mark Slee4f8da1d2006-10-12 02:47:27 +00001012 }
1013|
1014 {
1015 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +00001016 }
1017
Mark Slee31985722006-05-24 21:45:31 +00001018%%