blob: a6492fe9fb8cab27a58915b9f027a323c84260c0 [file] [log] [blame]
Mark Slee31985722006-05-24 21:45:31 +00001%{
2
3/**
4 * Thrift parser.
5 *
6 * This parser is used on a thrift definition file.
7 *
8 * @author Mark Slee <mcslee@facebook.com>
9 */
10
11#include <stdio.h>
12#include "main.h"
13#include "globals.h"
14#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000015#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000016
Mark Sleef5377b32006-10-10 01:42:59 +000017/**
18 * This global variable is used for automatic numbering of field indices etc.
19 * when parsing the members of a struct. Field values are automatically
20 * assigned starting from -1 and working their way down.
21 */
Mark Slee9cb7c612006-09-01 22:17:45 +000022int y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +000023
24%}
25
Mark Sleef5377b32006-10-10 01:42:59 +000026/**
27 * This structure is used by the parser to hold the data types associated with
28 * various parse nodes.
29 */
Mark Slee31985722006-05-24 21:45:31 +000030%union {
Mark Slee30152872006-11-28 01:24:07 +000031 char* id;
32 int iconst;
33 double dconst;
34 bool tbool;
35 t_type* ttype;
36 t_typedef* ttypedef;
37 t_enum* tenum;
38 t_enum_value* tenumv;
39 t_const* tconst;
40 t_const_value* tconstv;
41 t_struct* tstruct;
42 t_service* tservice;
43 t_function* tfunction;
44 t_field* tfield;
Mark Slee31985722006-05-24 21:45:31 +000045}
46
Mark Sleef5377b32006-10-10 01:42:59 +000047/**
48 * Strings identifier
49 */
Mark Slee31985722006-05-24 21:45:31 +000050%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +000051%token<id> tok_literal
Mark Sleef5377b32006-10-10 01:42:59 +000052
53/**
Mark Slee30152872006-11-28 01:24:07 +000054 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +000055 */
Mark Slee31985722006-05-24 21:45:31 +000056%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +000057%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +000058
Mark Sleef5377b32006-10-10 01:42:59 +000059/**
Mark Sleef0712dc2006-10-25 19:03:57 +000060 * Header keywoards
Mark Sleef5377b32006-10-10 01:42:59 +000061 */
Mark Sleef0712dc2006-10-25 19:03:57 +000062%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +000063%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000064%token tok_cpp_namespace
65%token tok_cpp_include
66%token tok_cpp_type
Mark Sleee888b372007-01-12 01:06:24 +000067%token tok_php_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000068%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +000069%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +000070%token tok_xsd_optional
Mark Slee0d9199e2007-01-31 02:08:30 +000071%token tok_xsd_namespace
Mark Slee9cb7c612006-09-01 22:17:45 +000072
Mark Sleef5377b32006-10-10 01:42:59 +000073/**
74 * Base datatype keywords
75 */
76%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +000077%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +000078%token tok_byte
79%token tok_string
Mark Sleeb6200d82007-01-19 19:14:36 +000080%token tok_slist
Mark Slee9cb7c612006-09-01 22:17:45 +000081%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +000082%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +000083%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +000084%token tok_double
Mark Slee31985722006-05-24 21:45:31 +000085
Mark Sleef5377b32006-10-10 01:42:59 +000086/**
87 * Complex type keywords
88 */
Mark Slee31985722006-05-24 21:45:31 +000089%token tok_map
90%token tok_list
91%token tok_set
92
Mark Sleef5377b32006-10-10 01:42:59 +000093/**
94 * Function modifiers
95 */
Mark Slee31985722006-05-24 21:45:31 +000096%token tok_async
97
Mark Sleef5377b32006-10-10 01:42:59 +000098/**
99 * Thrift language keywords
100 */
Mark Slee31985722006-05-24 21:45:31 +0000101%token tok_typedef
102%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000103%token tok_xception
104%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000105%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000106%token tok_service
107%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000108%token tok_const
Mark Slee31985722006-05-24 21:45:31 +0000109
Mark Sleef5377b32006-10-10 01:42:59 +0000110/**
111 * Grammar nodes
112 */
113
Mark Slee31985722006-05-24 21:45:31 +0000114%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000115%type<ttype> ContainerType
116%type<ttype> MapType
117%type<ttype> SetType
118%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000119
Mark Sleef0712dc2006-10-25 19:03:57 +0000120%type<ttype> TypeDefinition
121
Mark Slee31985722006-05-24 21:45:31 +0000122%type<ttypedef> Typedef
123%type<ttype> DefinitionType
124
125%type<tfield> Field
Mark Slee7ff32452007-02-01 05:26:18 +0000126%type<iconst> FieldIdentifier
Mark Slee31985722006-05-24 21:45:31 +0000127%type<ttype> FieldType
Mark Slee7ff32452007-02-01 05:26:18 +0000128%type<tconstv> FieldValue
Mark Sleee8540632006-05-30 09:24:40 +0000129%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000130
131%type<tenum> Enum
132%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000133%type<tenumv> EnumDef
134
135%type<tconst> Const
136%type<tconstv> ConstValue
137%type<tconstv> ConstList
138%type<tconstv> ConstListContents
139%type<tconstv> ConstMap
140%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000141
142%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000143%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000144%type<tservice> Service
145
146%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000147%type<ttype> FunctionType
148%type<tservice> FunctionList
149
Mark Slee36bfa2e2007-01-19 20:09:51 +0000150%type<tstruct> Throws
151%type<tservice> Extends
152%type<tbool> Async
153%type<tbool> XsdAll
154%type<tbool> XsdOptional
155%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000156
Mark Slee31985722006-05-24 21:45:31 +0000157%%
158
Mark Sleef5377b32006-10-10 01:42:59 +0000159/**
160 * Thrift Grammar Implementation.
161 *
162 * For the most part this source file works its way top down from what you
163 * might expect to find in a typical .thrift file, i.e. type definitions and
164 * namespaces up top followed by service definitions using those types.
165 */
Mark Slee31985722006-05-24 21:45:31 +0000166
167Program:
Mark Sleef0712dc2006-10-25 19:03:57 +0000168 HeaderList DefinitionList
169 {
170 pdebug("Program -> Headers DefinitionList");
171 }
172
173HeaderList:
174 HeaderList Header
175 {
176 pdebug("HeaderList -> HeaderList Header");
177 }
178|
179 {
180 pdebug("HeaderList -> ");
181 }
182
183Header:
184 Include
185 {
186 pdebug("Header -> Include");
187 }
188| tok_namespace tok_identifier
189 {
190 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
191 if (g_parse_mode == PROGRAM) {
192 g_program->set_cpp_namespace($2);
193 g_program->set_java_package($2);
194 }
195 }
196| tok_cpp_namespace tok_identifier
197 {
198 pdebug("Header -> tok_cpp_namespace tok_identifier");
199 if (g_parse_mode == PROGRAM) {
200 g_program->set_cpp_namespace($2);
201 }
202 }
203| tok_cpp_include tok_literal
204 {
205 pdebug("Header -> tok_cpp_include tok_literal");
206 if (g_parse_mode == PROGRAM) {
207 g_program->add_cpp_include($2);
208 }
209 }
Mark Sleee888b372007-01-12 01:06:24 +0000210| tok_php_namespace tok_identifier
211 {
212 pdebug("Header -> tok_php_namespace tok_identifier");
213 if (g_parse_mode == PROGRAM) {
214 g_program->set_php_namespace($2);
215 }
216 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000217| tok_java_package tok_identifier
218 {
219 pdebug("Header -> tok_java_package tok_identifier");
220 if (g_parse_mode == PROGRAM) {
221 g_program->set_java_package($2);
222 }
223 }
Mark Slee0d9199e2007-01-31 02:08:30 +0000224| tok_xsd_namespace tok_literal
225 {
226 pdebug("Header -> tok_xsd_namespace tok_literal");
227 if (g_parse_mode == PROGRAM) {
228 g_program->set_xsd_namespace($2);
229 }
230 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000231
232Include:
233 tok_include tok_literal
234 {
235 pdebug("Include -> tok_include tok_literal");
236 if (g_parse_mode == INCLUDES) {
237 std::string path = include_file(std::string($2));
238 if (!path.empty()) {
239 g_program->add_include(path);
240 }
241 }
242 }
Mark Slee31985722006-05-24 21:45:31 +0000243
244DefinitionList:
245 DefinitionList Definition
246 {
247 pdebug("DefinitionList -> DefinitionList Definition");
248 }
249|
250 {
251 pdebug("DefinitionList -> ");
252 }
253
254Definition:
Mark Slee30152872006-11-28 01:24:07 +0000255 Const
256 {
257 pdebug("Definition -> Const");
258 if (g_parse_mode == PROGRAM) {
259 g_program->add_const($1);
260 }
261 }
262| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000263 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000264 pdebug("Definition -> TypeDefinition");
265 if (g_parse_mode == PROGRAM) {
266 g_scope->add_type($1->get_name(), $1);
267 if (g_parent_scope != NULL) {
268 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
269 }
270 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000271 }
Mark Slee31985722006-05-24 21:45:31 +0000272| Service
273 {
274 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000275 if (g_parse_mode == PROGRAM) {
276 g_scope->add_service($1->get_name(), $1);
277 if (g_parent_scope != NULL) {
278 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
279 }
280 g_program->add_service($1);
281 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000282 }
283
Mark Sleef0712dc2006-10-25 19:03:57 +0000284TypeDefinition:
285 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000286 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000287 pdebug("TypeDefinition -> Typedef");
288 if (g_parse_mode == PROGRAM) {
289 g_program->add_typedef($1);
290 }
291 }
292| Enum
293 {
294 pdebug("TypeDefinition -> Enum");
295 if (g_parse_mode == PROGRAM) {
296 g_program->add_enum($1);
297 }
298 }
299| Struct
300 {
301 pdebug("TypeDefinition -> Struct");
302 if (g_parse_mode == PROGRAM) {
303 g_program->add_struct($1);
304 }
305 }
306| Xception
307 {
308 pdebug("TypeDefinition -> Xception");
309 if (g_parse_mode == PROGRAM) {
310 g_program->add_xception($1);
311 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000312 }
Mark Slee31985722006-05-24 21:45:31 +0000313
314Typedef:
315 tok_typedef DefinitionType tok_identifier
316 {
317 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000318 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000319 $$ = td;
320 }
321
322Enum:
323 tok_enum tok_identifier '{' EnumDefList '}'
324 {
325 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
326 $$ = $4;
327 $$->set_name($2);
328 }
329
Mark Slee207cb462006-11-02 18:43:12 +0000330CommaOrSemicolonOptional:
331 ','
332 {}
333| ';'
334 {}
335|
336 {}
337
Mark Slee31985722006-05-24 21:45:31 +0000338EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000339 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000340 {
341 pdebug("EnumDefList -> EnumDefList EnumDef");
342 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000343 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000344 }
345|
346 {
347 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000348 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000349 }
350
351EnumDef:
Mark Slee207cb462006-11-02 18:43:12 +0000352 tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000353 {
Mark Slee30152872006-11-28 01:24:07 +0000354 pdebug("EnumDef -> tok_identifier = tok_int_constant");
Mark Slee31985722006-05-24 21:45:31 +0000355 if ($3 < 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000356 pwarning(1, "Negative value supplied for enum %s.\n", $1);
Mark Slee31985722006-05-24 21:45:31 +0000357 }
Mark Slee30152872006-11-28 01:24:07 +0000358 $$ = new t_enum_value($1, $3);
Mark Slee31985722006-05-24 21:45:31 +0000359 }
360|
Mark Slee04cc6052006-11-15 21:25:34 +0000361 tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000362 {
Mark Slee30152872006-11-28 01:24:07 +0000363 pdebug("EnumDef -> tok_identifier");
364 $$ = new t_enum_value($1);
365 }
366
367Const:
368 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
369 {
370 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000371 if (g_parse_mode == PROGRAM) {
372 $$ = new t_const($2, $3, $5);
373 validate_const_type($$);
374 } else {
375 $$ = NULL;
376 }
Mark Slee30152872006-11-28 01:24:07 +0000377 }
378
379ConstValue:
380 tok_int_constant
381 {
382 pdebug("ConstValue => tok_int_constant");
383 $$ = new t_const_value();
384 $$->set_integer($1);
385 }
386| tok_dub_constant
387 {
388 pdebug("ConstValue => tok_dub_constant");
389 $$ = new t_const_value();
390 $$->set_double($1);
391 }
392| tok_literal
393 {
394 pdebug("ConstValue => tok_literal");
395 $$ = new t_const_value();
396 $$->set_string($1);
397 }
Mark Slee67fc6342006-11-29 03:37:04 +0000398| tok_identifier
399 {
400 pdebug("ConstValue => tok_identifier");
401 $$ = new t_const_value();
402 $$->set_string($1);
403 }
Mark Slee30152872006-11-28 01:24:07 +0000404| ConstList
405 {
406 pdebug("ConstValue => ConstList");
407 $$ = $1;
408 }
409| ConstMap
410 {
411 pdebug("ConstValue => ConstMap");
412 $$ = $1;
413 }
414
415ConstList:
416 '[' ConstListContents ']'
417 {
418 pdebug("ConstList => [ ConstListContents ]");
419 $$ = $2;
420 }
421
422ConstListContents:
423 ConstListContents ConstValue CommaOrSemicolonOptional
424 {
425 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
426 $$ = $1;
427 $$->add_list($2);
428 }
429|
430 {
431 pdebug("ConstListContents =>");
432 $$ = new t_const_value();
433 $$->set_list();
434 }
435
436ConstMap:
437 '{' ConstMapContents '}'
438 {
439 pdebug("ConstMap => { ConstMapContents }");
440 $$ = $2;
441 }
442
443ConstMapContents:
444 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
445 {
446 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
447 $$ = $1;
448 $$->add_map($2, $4);
449 }
450|
451 {
452 pdebug("ConstMapContents =>");
453 $$ = new t_const_value();
454 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000455 }
456
457Struct:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000458 tok_struct tok_identifier XsdAll '{' FieldList '}'
Mark Slee31985722006-05-24 21:45:31 +0000459 {
460 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
Mark Slee782abbb2007-01-19 00:17:02 +0000461 $5->set_name($2);
462 $5->set_xsd_all($3);
463 $$ = $5;
Mark Slee9cb7c612006-09-01 22:17:45 +0000464 y_field_val = -1;
465 }
466
Mark Slee36bfa2e2007-01-19 20:09:51 +0000467XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000468 tok_xsd_all
469 {
470 $$ = true;
471 }
472|
473 {
474 $$ = false;
475 }
476
Mark Slee36bfa2e2007-01-19 20:09:51 +0000477XsdOptional:
478 tok_xsd_optional
479 {
480 $$ = true;
481 }
482|
483 {
484 $$ = false;
485 }
486
Mark Slee9cb7c612006-09-01 22:17:45 +0000487Xception:
488 tok_xception tok_identifier '{' FieldList '}'
489 {
490 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
491 $4->set_name($2);
492 $4->set_xception(true);
493 $$ = $4;
494 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000495 }
496
497Service:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000498 tok_service tok_identifier Extends '{' FunctionList '}'
Mark Slee31985722006-05-24 21:45:31 +0000499 {
500 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Mark Sleef0712dc2006-10-25 19:03:57 +0000501 $$ = $5;
Mark Slee31985722006-05-24 21:45:31 +0000502 $$->set_name($2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000503 $$->set_extends($3);
504 }
505
Mark Slee36bfa2e2007-01-19 20:09:51 +0000506Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000507 tok_extends tok_identifier
508 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000509 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000510 $$ = NULL;
511 if (g_parse_mode == PROGRAM) {
512 $$ = g_scope->get_service($2);
513 if ($$ == NULL) {
514 yyerror("Service \"%s\" has not been defined.", $2);
515 exit(1);
516 }
517 }
518 }
519|
520 {
521 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000522 }
523
524FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000525 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000526 {
527 pdebug("FunctionList -> FunctionList Function");
528 $$ = $1;
529 $1->add_function($2);
530 }
531|
532 {
533 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000534 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000535 }
536
537Function:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000538 Async FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000539 {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000540 $5->set_name(std::string($3) + "_args");
Mark Slee4e755ca2006-09-12 00:46:08 +0000541 $$ = new t_function($2, $3, $5, $7, $1);
Mark Slee9cb7c612006-09-01 22:17:45 +0000542 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000543 }
544
Mark Slee36bfa2e2007-01-19 20:09:51 +0000545Async:
Mark Slee52f643d2006-08-09 00:03:43 +0000546 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000547 {
Mark Slee52f643d2006-08-09 00:03:43 +0000548 $$ = true;
549 }
550|
551 {
552 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000553 }
554
Mark Slee36bfa2e2007-01-19 20:09:51 +0000555Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000556 tok_throws '(' FieldList ')'
557 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000558 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000559 $$ = $3;
560 }
561|
562 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000563 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000564 }
565
Mark Slee31985722006-05-24 21:45:31 +0000566FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000567 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000568 {
569 pdebug("FieldList -> FieldList , Field");
570 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000571 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000572 }
573|
574 {
575 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000576 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000577 }
578
579Field:
Mark Slee7ff32452007-02-01 05:26:18 +0000580 FieldIdentifier FieldType tok_identifier FieldValue XsdOptional CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000581 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000582 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
Mark Slee7ff32452007-02-01 05:26:18 +0000583 if ($1 < 0) {
584 pwarning(2, "No field key specified for %s, resulting protocol may have conflicts or not be backwards compatible!\n", $3);
Mark Slee31985722006-05-24 21:45:31 +0000585 }
Mark Slee7ff32452007-02-01 05:26:18 +0000586 $$ = new t_field($2, $3, $1);
587 if ($4 != NULL) {
588 validate_field_value($$, $4);
589 $$->set_value($4);
590 }
Mark Slee36bfa2e2007-01-19 20:09:51 +0000591 $$->set_xsd_optional($5);
Mark Slee31985722006-05-24 21:45:31 +0000592 }
Mark Slee7ff32452007-02-01 05:26:18 +0000593
594FieldIdentifier:
595 tok_int_constant ':'
Mark Slee31985722006-05-24 21:45:31 +0000596 {
Mark Slee7ff32452007-02-01 05:26:18 +0000597 if ($1 <= 0) {
598 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n", $1);
599 $1 = y_field_val--;
Mark Sleef0712dc2006-10-25 19:03:57 +0000600 }
Mark Slee7ff32452007-02-01 05:26:18 +0000601 $$ = $1;
602 }
603|
604 {
605 $$ = y_field_val--;
606 }
607
608FieldValue:
609 '=' ConstValue
610 {
611 if (g_parse_mode == PROGRAM) {
612 $$ = $2;
613 } else {
614 $$ = NULL;
615 }
616 }
617|
618 {
619 $$ = NULL;
Mark Sleef0712dc2006-10-25 19:03:57 +0000620 }
Mark Slee31985722006-05-24 21:45:31 +0000621
622DefinitionType:
623 BaseType
624 {
625 pdebug("DefinitionType -> BaseType");
626 $$ = $1;
627 }
Mark Sleee8540632006-05-30 09:24:40 +0000628| ContainerType
629 {
630 pdebug("DefinitionType -> ContainerType");
631 $$ = $1;
632 }
Mark Slee31985722006-05-24 21:45:31 +0000633
634FunctionType:
635 FieldType
636 {
Mark Sleee8540632006-05-30 09:24:40 +0000637 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000638 $$ = $1;
639 }
640| tok_void
641 {
Mark Sleee8540632006-05-30 09:24:40 +0000642 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000643 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000644 }
645
646FieldType:
647 tok_identifier
648 {
Mark Sleee8540632006-05-30 09:24:40 +0000649 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000650 if (g_parse_mode == INCLUDES) {
651 // Ignore identifiers in include mode
652 $$ = NULL;
653 } else {
654 // Lookup the identifier in the current scope
655 $$ = g_scope->get_type($1);
656 if ($$ == NULL) {
657 yyerror("Type \"%s\" has not been defined.", $1);
658 exit(1);
659 }
Mark Sleee8540632006-05-30 09:24:40 +0000660 }
Mark Slee31985722006-05-24 21:45:31 +0000661 }
662| BaseType
663 {
Mark Sleee8540632006-05-30 09:24:40 +0000664 pdebug("FieldType -> BaseType");
665 $$ = $1;
666 }
667| ContainerType
668 {
669 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000670 $$ = $1;
671 }
672
673BaseType:
674 tok_string
675 {
676 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000677 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000678 }
Mark Sleeb6200d82007-01-19 19:14:36 +0000679| tok_slist
680 {
681 pdebug("BaseType -> tok_slist");
682 $$ = g_type_slist;
683 }
Mark Slee78f58e22006-09-02 04:17:07 +0000684| tok_bool
685 {
686 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000687 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000688 }
Mark Slee31985722006-05-24 21:45:31 +0000689| tok_byte
690 {
691 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000692 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000693 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000694| tok_i16
695 {
696 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000697 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000698 }
Mark Slee31985722006-05-24 21:45:31 +0000699| tok_i32
700 {
701 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000702 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000703 }
Mark Slee31985722006-05-24 21:45:31 +0000704| tok_i64
705 {
706 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000707 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000708 }
Mark Sleec98d0502006-09-06 02:42:25 +0000709| tok_double
710 {
711 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000712 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000713 }
Mark Slee31985722006-05-24 21:45:31 +0000714
Mark Sleee8540632006-05-30 09:24:40 +0000715ContainerType:
716 MapType
717 {
718 pdebug("ContainerType -> MapType");
719 $$ = $1;
720 }
721| SetType
722 {
723 pdebug("ContainerType -> SetType");
724 $$ = $1;
725 }
726| ListType
727 {
728 pdebug("ContainerType -> ListType");
729 $$ = $1;
730 }
731
732MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000733 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000734 {
735 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000736 $$ = new t_map($4, $6);
737 if ($2 != NULL) {
738 ((t_container*)$$)->set_cpp_name(std::string($2));
739 }
Mark Sleee8540632006-05-30 09:24:40 +0000740 }
741
742SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000743 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000744 {
745 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000746 $$ = new t_set($4);
747 if ($2 != NULL) {
748 ((t_container*)$$)->set_cpp_name(std::string($2));
749 }
Mark Sleee8540632006-05-30 09:24:40 +0000750 }
751
752ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000753 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +0000754 {
755 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +0000756 $$ = new t_list($3);
757 if ($5 != NULL) {
758 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +0000759 }
760 }
761
Mark Slee36bfa2e2007-01-19 20:09:51 +0000762CppType:
Mark Sleef0712dc2006-10-25 19:03:57 +0000763 '[' tok_cpp_type tok_literal ']'
Mark Slee4f8da1d2006-10-12 02:47:27 +0000764 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000765 $$ = $3;
Mark Slee4f8da1d2006-10-12 02:47:27 +0000766 }
767|
768 {
769 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +0000770 }
771
Mark Slee31985722006-05-24 21:45:31 +0000772%%