blob: fe56dad971a2a3387dd187bad2495857f3b95211 [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 Slee9cb7c612006-09-01 22:17:45 +000070
Mark Sleef5377b32006-10-10 01:42:59 +000071/**
72 * Base datatype keywords
73 */
74%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +000075%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +000076%token tok_byte
77%token tok_string
Mark Sleeb6200d82007-01-19 19:14:36 +000078%token tok_slist
Mark Slee9cb7c612006-09-01 22:17:45 +000079%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +000080%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +000081%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +000082%token tok_double
Mark Slee31985722006-05-24 21:45:31 +000083
Mark Sleef5377b32006-10-10 01:42:59 +000084/**
85 * Complex type keywords
86 */
Mark Slee31985722006-05-24 21:45:31 +000087%token tok_map
88%token tok_list
89%token tok_set
90
Mark Sleef5377b32006-10-10 01:42:59 +000091/**
92 * Function modifiers
93 */
Mark Slee31985722006-05-24 21:45:31 +000094%token tok_async
95
Mark Sleef5377b32006-10-10 01:42:59 +000096/**
97 * Thrift language keywords
98 */
Mark Slee31985722006-05-24 21:45:31 +000099%token tok_typedef
100%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000101%token tok_xception
102%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000103%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000104%token tok_service
105%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000106%token tok_const
Mark Slee31985722006-05-24 21:45:31 +0000107
Mark Sleef5377b32006-10-10 01:42:59 +0000108/**
109 * Grammar nodes
110 */
111
Mark Slee31985722006-05-24 21:45:31 +0000112%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000113%type<ttype> ContainerType
114%type<ttype> MapType
115%type<ttype> SetType
116%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000117
Mark Sleef0712dc2006-10-25 19:03:57 +0000118%type<ttype> TypeDefinition
119
Mark Slee31985722006-05-24 21:45:31 +0000120%type<ttypedef> Typedef
121%type<ttype> DefinitionType
122
123%type<tfield> Field
124%type<ttype> FieldType
Mark Sleee8540632006-05-30 09:24:40 +0000125%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000126
127%type<tenum> Enum
128%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000129%type<tenumv> EnumDef
130
131%type<tconst> Const
132%type<tconstv> ConstValue
133%type<tconstv> ConstList
134%type<tconstv> ConstListContents
135%type<tconstv> ConstMap
136%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000137
138%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000139%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000140%type<tservice> Service
141
142%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000143%type<ttype> FunctionType
144%type<tservice> FunctionList
145
Mark Sleef5377b32006-10-10 01:42:59 +0000146%type<tstruct> ThrowsOptional
Mark Sleef0712dc2006-10-25 19:03:57 +0000147%type<tservice> ExtendsOptional
Mark Slee52f643d2006-08-09 00:03:43 +0000148%type<tbool> AsyncOptional
Mark Slee782abbb2007-01-19 00:17:02 +0000149%type<tbool> XsdAllOptional
Mark Slee4f8da1d2006-10-12 02:47:27 +0000150%type<id> CppTypeOptional
Mark Slee52f643d2006-08-09 00:03:43 +0000151
Mark Slee31985722006-05-24 21:45:31 +0000152%%
153
Mark Sleef5377b32006-10-10 01:42:59 +0000154/**
155 * Thrift Grammar Implementation.
156 *
157 * For the most part this source file works its way top down from what you
158 * might expect to find in a typical .thrift file, i.e. type definitions and
159 * namespaces up top followed by service definitions using those types.
160 */
Mark Slee31985722006-05-24 21:45:31 +0000161
162Program:
Mark Sleef0712dc2006-10-25 19:03:57 +0000163 HeaderList DefinitionList
164 {
165 pdebug("Program -> Headers DefinitionList");
166 }
167
168HeaderList:
169 HeaderList Header
170 {
171 pdebug("HeaderList -> HeaderList Header");
172 }
173|
174 {
175 pdebug("HeaderList -> ");
176 }
177
178Header:
179 Include
180 {
181 pdebug("Header -> Include");
182 }
183| tok_namespace tok_identifier
184 {
185 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
186 if (g_parse_mode == PROGRAM) {
187 g_program->set_cpp_namespace($2);
188 g_program->set_java_package($2);
189 }
190 }
191| tok_cpp_namespace tok_identifier
192 {
193 pdebug("Header -> tok_cpp_namespace tok_identifier");
194 if (g_parse_mode == PROGRAM) {
195 g_program->set_cpp_namespace($2);
196 }
197 }
198| tok_cpp_include tok_literal
199 {
200 pdebug("Header -> tok_cpp_include tok_literal");
201 if (g_parse_mode == PROGRAM) {
202 g_program->add_cpp_include($2);
203 }
204 }
Mark Sleee888b372007-01-12 01:06:24 +0000205| tok_php_namespace tok_identifier
206 {
207 pdebug("Header -> tok_php_namespace tok_identifier");
208 if (g_parse_mode == PROGRAM) {
209 g_program->set_php_namespace($2);
210 }
211 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000212| tok_java_package tok_identifier
213 {
214 pdebug("Header -> tok_java_package tok_identifier");
215 if (g_parse_mode == PROGRAM) {
216 g_program->set_java_package($2);
217 }
218 }
219
220Include:
221 tok_include tok_literal
222 {
223 pdebug("Include -> tok_include tok_literal");
224 if (g_parse_mode == INCLUDES) {
225 std::string path = include_file(std::string($2));
226 if (!path.empty()) {
227 g_program->add_include(path);
228 }
229 }
230 }
Mark Slee31985722006-05-24 21:45:31 +0000231
232DefinitionList:
233 DefinitionList Definition
234 {
235 pdebug("DefinitionList -> DefinitionList Definition");
236 }
237|
238 {
239 pdebug("DefinitionList -> ");
240 }
241
242Definition:
Mark Slee30152872006-11-28 01:24:07 +0000243 Const
244 {
245 pdebug("Definition -> Const");
246 if (g_parse_mode == PROGRAM) {
247 g_program->add_const($1);
248 }
249 }
250| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000251 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000252 pdebug("Definition -> TypeDefinition");
253 if (g_parse_mode == PROGRAM) {
254 g_scope->add_type($1->get_name(), $1);
255 if (g_parent_scope != NULL) {
256 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
257 }
258 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000259 }
Mark Slee31985722006-05-24 21:45:31 +0000260| Service
261 {
262 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000263 if (g_parse_mode == PROGRAM) {
264 g_scope->add_service($1->get_name(), $1);
265 if (g_parent_scope != NULL) {
266 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
267 }
268 g_program->add_service($1);
269 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000270 }
271
Mark Sleef0712dc2006-10-25 19:03:57 +0000272TypeDefinition:
273 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000274 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000275 pdebug("TypeDefinition -> Typedef");
276 if (g_parse_mode == PROGRAM) {
277 g_program->add_typedef($1);
278 }
279 }
280| Enum
281 {
282 pdebug("TypeDefinition -> Enum");
283 if (g_parse_mode == PROGRAM) {
284 g_program->add_enum($1);
285 }
286 }
287| Struct
288 {
289 pdebug("TypeDefinition -> Struct");
290 if (g_parse_mode == PROGRAM) {
291 g_program->add_struct($1);
292 }
293 }
294| Xception
295 {
296 pdebug("TypeDefinition -> Xception");
297 if (g_parse_mode == PROGRAM) {
298 g_program->add_xception($1);
299 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000300 }
Mark Slee31985722006-05-24 21:45:31 +0000301
302Typedef:
303 tok_typedef DefinitionType tok_identifier
304 {
305 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000306 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000307 $$ = td;
308 }
309
310Enum:
311 tok_enum tok_identifier '{' EnumDefList '}'
312 {
313 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
314 $$ = $4;
315 $$->set_name($2);
316 }
317
Mark Slee207cb462006-11-02 18:43:12 +0000318CommaOrSemicolonOptional:
319 ','
320 {}
321| ';'
322 {}
323|
324 {}
325
Mark Slee31985722006-05-24 21:45:31 +0000326EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000327 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000328 {
329 pdebug("EnumDefList -> EnumDefList EnumDef");
330 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000331 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000332 }
333|
334 {
335 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000336 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000337 }
338
339EnumDef:
Mark Slee207cb462006-11-02 18:43:12 +0000340 tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000341 {
Mark Slee30152872006-11-28 01:24:07 +0000342 pdebug("EnumDef -> tok_identifier = tok_int_constant");
Mark Slee31985722006-05-24 21:45:31 +0000343 if ($3 < 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000344 pwarning(1, "Negative value supplied for enum %s.\n", $1);
Mark Slee31985722006-05-24 21:45:31 +0000345 }
Mark Slee30152872006-11-28 01:24:07 +0000346 $$ = new t_enum_value($1, $3);
Mark Slee31985722006-05-24 21:45:31 +0000347 }
348|
Mark Slee04cc6052006-11-15 21:25:34 +0000349 tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000350 {
Mark Slee30152872006-11-28 01:24:07 +0000351 pdebug("EnumDef -> tok_identifier");
352 $$ = new t_enum_value($1);
353 }
354
355Const:
356 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
357 {
358 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000359 if (g_parse_mode == PROGRAM) {
360 $$ = new t_const($2, $3, $5);
361 validate_const_type($$);
362 } else {
363 $$ = NULL;
364 }
Mark Slee30152872006-11-28 01:24:07 +0000365 }
366
367ConstValue:
368 tok_int_constant
369 {
370 pdebug("ConstValue => tok_int_constant");
371 $$ = new t_const_value();
372 $$->set_integer($1);
373 }
374| tok_dub_constant
375 {
376 pdebug("ConstValue => tok_dub_constant");
377 $$ = new t_const_value();
378 $$->set_double($1);
379 }
380| tok_literal
381 {
382 pdebug("ConstValue => tok_literal");
383 $$ = new t_const_value();
384 $$->set_string($1);
385 }
Mark Slee67fc6342006-11-29 03:37:04 +0000386| tok_identifier
387 {
388 pdebug("ConstValue => tok_identifier");
389 $$ = new t_const_value();
390 $$->set_string($1);
391 }
Mark Slee30152872006-11-28 01:24:07 +0000392| ConstList
393 {
394 pdebug("ConstValue => ConstList");
395 $$ = $1;
396 }
397| ConstMap
398 {
399 pdebug("ConstValue => ConstMap");
400 $$ = $1;
401 }
402
403ConstList:
404 '[' ConstListContents ']'
405 {
406 pdebug("ConstList => [ ConstListContents ]");
407 $$ = $2;
408 }
409
410ConstListContents:
411 ConstListContents ConstValue CommaOrSemicolonOptional
412 {
413 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
414 $$ = $1;
415 $$->add_list($2);
416 }
417|
418 {
419 pdebug("ConstListContents =>");
420 $$ = new t_const_value();
421 $$->set_list();
422 }
423
424ConstMap:
425 '{' ConstMapContents '}'
426 {
427 pdebug("ConstMap => { ConstMapContents }");
428 $$ = $2;
429 }
430
431ConstMapContents:
432 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
433 {
434 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
435 $$ = $1;
436 $$->add_map($2, $4);
437 }
438|
439 {
440 pdebug("ConstMapContents =>");
441 $$ = new t_const_value();
442 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000443 }
444
445Struct:
Mark Slee782abbb2007-01-19 00:17:02 +0000446 tok_struct tok_identifier XsdAllOptional '{' FieldList '}'
Mark Slee31985722006-05-24 21:45:31 +0000447 {
448 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
Mark Slee782abbb2007-01-19 00:17:02 +0000449 $5->set_name($2);
450 $5->set_xsd_all($3);
451 $$ = $5;
Mark Slee9cb7c612006-09-01 22:17:45 +0000452 y_field_val = -1;
453 }
454
Mark Slee782abbb2007-01-19 00:17:02 +0000455XsdAllOptional:
456 tok_xsd_all
457 {
458 $$ = true;
459 }
460|
461 {
462 $$ = false;
463 }
464
Mark Slee9cb7c612006-09-01 22:17:45 +0000465Xception:
466 tok_xception tok_identifier '{' FieldList '}'
467 {
468 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
469 $4->set_name($2);
470 $4->set_xception(true);
471 $$ = $4;
472 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000473 }
474
475Service:
Mark Sleef0712dc2006-10-25 19:03:57 +0000476 tok_service tok_identifier ExtendsOptional '{' FunctionList '}'
Mark Slee31985722006-05-24 21:45:31 +0000477 {
478 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Mark Sleef0712dc2006-10-25 19:03:57 +0000479 $$ = $5;
Mark Slee31985722006-05-24 21:45:31 +0000480 $$->set_name($2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000481 $$->set_extends($3);
482 }
483
484ExtendsOptional:
485 tok_extends tok_identifier
486 {
487 pdebug("ExtendsOptional -> tok_extends tok_identifier");
488 $$ = NULL;
489 if (g_parse_mode == PROGRAM) {
490 $$ = g_scope->get_service($2);
491 if ($$ == NULL) {
492 yyerror("Service \"%s\" has not been defined.", $2);
493 exit(1);
494 }
495 }
496 }
497|
498 {
499 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000500 }
501
502FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000503 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000504 {
505 pdebug("FunctionList -> FunctionList Function");
506 $$ = $1;
507 $1->add_function($2);
508 }
509|
510 {
511 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000512 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000513 }
514
515Function:
Mark Slee207cb462006-11-02 18:43:12 +0000516 AsyncOptional FunctionType tok_identifier '(' FieldList ')' ThrowsOptional CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000517 {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000518 $5->set_name(std::string($3) + "_args");
Mark Slee4e755ca2006-09-12 00:46:08 +0000519 $$ = new t_function($2, $3, $5, $7, $1);
Mark Slee9cb7c612006-09-01 22:17:45 +0000520 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000521 }
522
Mark Slee52f643d2006-08-09 00:03:43 +0000523AsyncOptional:
524 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000525 {
Mark Slee52f643d2006-08-09 00:03:43 +0000526 $$ = true;
527 }
528|
529 {
530 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000531 }
532
Mark Slee9cb7c612006-09-01 22:17:45 +0000533ThrowsOptional:
534 tok_throws '(' FieldList ')'
535 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000536 pdebug("ThrowsOptional -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000537 $$ = $3;
538 }
539|
540 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000541 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000542 }
543
Mark Slee31985722006-05-24 21:45:31 +0000544FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000545 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000546 {
547 pdebug("FieldList -> FieldList , Field");
548 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000549 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000550 }
551|
552 {
553 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000554 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000555 }
556
557Field:
Mark Slee207cb462006-11-02 18:43:12 +0000558 tok_int_constant ':' FieldType tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000559 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000560 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
561 if ($1 <= 0) {
562 pwarning(1, "Nonpositive value (%d) not allowed as a field key for '%s'.\n", $1, $4);
563 $1 = y_field_val--;
Mark Slee31985722006-05-24 21:45:31 +0000564 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000565 $$ = new t_field($3, $4, $1);
Mark Slee31985722006-05-24 21:45:31 +0000566 }
Mark Slee2329a832006-11-09 00:23:30 +0000567| FieldType tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000568 {
Mark Sleee8540632006-05-30 09:24:40 +0000569 pdebug("Field -> FieldType tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000570 pwarning(2, "No field key specified for '%s', resulting protocol may have conflicts or not be backwards compatible!\n", $2);
Mark Slee9cb7c612006-09-01 22:17:45 +0000571 $$ = new t_field($1, $2, y_field_val--);
Mark Slee31985722006-05-24 21:45:31 +0000572 }
Mark Slee2329a832006-11-09 00:23:30 +0000573| FieldType tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Sleef0712dc2006-10-25 19:03:57 +0000574 {
Mark Slee2329a832006-11-09 00:23:30 +0000575 pwarning(1, "Trailing = id notation is deprecated. Use 'Id: Type Name' notation instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000576 pdebug("Field -> FieldType tok_identifier = tok_int_constant");
577 if ($4 <= 0) {
578 pwarning(1, "Nonpositive value (%d) not allowed as a field key for '%s'.\n", $4, $2);
579 $4 = y_field_val--;
580 }
581 $$ = new t_field($1, $2, $4);
582 }
Mark Slee31985722006-05-24 21:45:31 +0000583
584DefinitionType:
585 BaseType
586 {
587 pdebug("DefinitionType -> BaseType");
588 $$ = $1;
589 }
Mark Sleee8540632006-05-30 09:24:40 +0000590| ContainerType
591 {
592 pdebug("DefinitionType -> ContainerType");
593 $$ = $1;
594 }
Mark Slee31985722006-05-24 21:45:31 +0000595
596FunctionType:
597 FieldType
598 {
Mark Sleee8540632006-05-30 09:24:40 +0000599 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000600 $$ = $1;
601 }
602| tok_void
603 {
Mark Sleee8540632006-05-30 09:24:40 +0000604 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000605 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000606 }
607
608FieldType:
609 tok_identifier
610 {
Mark Sleee8540632006-05-30 09:24:40 +0000611 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000612 if (g_parse_mode == INCLUDES) {
613 // Ignore identifiers in include mode
614 $$ = NULL;
615 } else {
616 // Lookup the identifier in the current scope
617 $$ = g_scope->get_type($1);
618 if ($$ == NULL) {
619 yyerror("Type \"%s\" has not been defined.", $1);
620 exit(1);
621 }
Mark Sleee8540632006-05-30 09:24:40 +0000622 }
Mark Slee31985722006-05-24 21:45:31 +0000623 }
624| BaseType
625 {
Mark Sleee8540632006-05-30 09:24:40 +0000626 pdebug("FieldType -> BaseType");
627 $$ = $1;
628 }
629| ContainerType
630 {
631 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000632 $$ = $1;
633 }
634
635BaseType:
636 tok_string
637 {
638 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000639 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000640 }
Mark Sleeb6200d82007-01-19 19:14:36 +0000641| tok_slist
642 {
643 pdebug("BaseType -> tok_slist");
644 $$ = g_type_slist;
645 }
Mark Slee78f58e22006-09-02 04:17:07 +0000646| tok_bool
647 {
648 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000649 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000650 }
Mark Slee31985722006-05-24 21:45:31 +0000651| tok_byte
652 {
653 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000654 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000655 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000656| tok_i16
657 {
658 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000659 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000660 }
Mark Slee31985722006-05-24 21:45:31 +0000661| tok_i32
662 {
663 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000664 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000665 }
Mark Slee31985722006-05-24 21:45:31 +0000666| tok_i64
667 {
668 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000669 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000670 }
Mark Sleec98d0502006-09-06 02:42:25 +0000671| tok_double
672 {
673 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000674 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000675 }
Mark Slee31985722006-05-24 21:45:31 +0000676
Mark Sleee8540632006-05-30 09:24:40 +0000677ContainerType:
678 MapType
679 {
680 pdebug("ContainerType -> MapType");
681 $$ = $1;
682 }
683| SetType
684 {
685 pdebug("ContainerType -> SetType");
686 $$ = $1;
687 }
688| ListType
689 {
690 pdebug("ContainerType -> ListType");
691 $$ = $1;
692 }
693
694MapType:
Mark Slee4f8da1d2006-10-12 02:47:27 +0000695 tok_map CppTypeOptional '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000696 {
697 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000698 $$ = new t_map($4, $6);
699 if ($2 != NULL) {
700 ((t_container*)$$)->set_cpp_name(std::string($2));
701 }
Mark Sleee8540632006-05-30 09:24:40 +0000702 }
703
704SetType:
Mark Slee4f8da1d2006-10-12 02:47:27 +0000705 tok_set CppTypeOptional '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000706 {
707 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000708 $$ = new t_set($4);
709 if ($2 != NULL) {
710 ((t_container*)$$)->set_cpp_name(std::string($2));
711 }
Mark Sleee8540632006-05-30 09:24:40 +0000712 }
713
714ListType:
Mark Sleef0712dc2006-10-25 19:03:57 +0000715 tok_list '<' FieldType '>' CppTypeOptional
Mark Sleee8540632006-05-30 09:24:40 +0000716 {
717 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +0000718 $$ = new t_list($3);
719 if ($5 != NULL) {
720 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +0000721 }
722 }
723
724CppTypeOptional:
Mark Sleef0712dc2006-10-25 19:03:57 +0000725 '[' tok_cpp_type tok_literal ']'
Mark Slee4f8da1d2006-10-12 02:47:27 +0000726 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000727 $$ = $3;
Mark Slee4f8da1d2006-10-12 02:47:27 +0000728 }
729|
730 {
731 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +0000732 }
733
Mark Slee31985722006-05-24 21:45:31 +0000734%%