blob: cfb3e44fe38c1227ab79d987ca30c00421c86c44 [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 Slee9cb7c612006-09-01 22:17:45 +000078%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +000079%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +000080%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +000081%token tok_double
Mark Slee31985722006-05-24 21:45:31 +000082
Mark Sleef5377b32006-10-10 01:42:59 +000083/**
84 * Complex type keywords
85 */
Mark Slee31985722006-05-24 21:45:31 +000086%token tok_map
87%token tok_list
88%token tok_set
89
Mark Sleef5377b32006-10-10 01:42:59 +000090/**
91 * Function modifiers
92 */
Mark Slee31985722006-05-24 21:45:31 +000093%token tok_async
94
Mark Sleef5377b32006-10-10 01:42:59 +000095/**
96 * Thrift language keywords
97 */
Mark Slee31985722006-05-24 21:45:31 +000098%token tok_typedef
99%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000100%token tok_xception
101%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000102%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000103%token tok_service
104%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000105%token tok_const
Mark Slee31985722006-05-24 21:45:31 +0000106
Mark Sleef5377b32006-10-10 01:42:59 +0000107/**
108 * Grammar nodes
109 */
110
Mark Slee31985722006-05-24 21:45:31 +0000111%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000112%type<ttype> ContainerType
113%type<ttype> MapType
114%type<ttype> SetType
115%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000116
Mark Sleef0712dc2006-10-25 19:03:57 +0000117%type<ttype> TypeDefinition
118
Mark Slee31985722006-05-24 21:45:31 +0000119%type<ttypedef> Typedef
120%type<ttype> DefinitionType
121
122%type<tfield> Field
123%type<ttype> FieldType
Mark Sleee8540632006-05-30 09:24:40 +0000124%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000125
126%type<tenum> Enum
127%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000128%type<tenumv> EnumDef
129
130%type<tconst> Const
131%type<tconstv> ConstValue
132%type<tconstv> ConstList
133%type<tconstv> ConstListContents
134%type<tconstv> ConstMap
135%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000136
137%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000138%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000139%type<tservice> Service
140
141%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000142%type<ttype> FunctionType
143%type<tservice> FunctionList
144
Mark Sleef5377b32006-10-10 01:42:59 +0000145%type<tstruct> ThrowsOptional
Mark Sleef0712dc2006-10-25 19:03:57 +0000146%type<tservice> ExtendsOptional
Mark Slee52f643d2006-08-09 00:03:43 +0000147%type<tbool> AsyncOptional
Mark Slee782abbb2007-01-19 00:17:02 +0000148%type<tbool> XsdAllOptional
Mark Slee4f8da1d2006-10-12 02:47:27 +0000149%type<id> CppTypeOptional
Mark Slee52f643d2006-08-09 00:03:43 +0000150
Mark Slee31985722006-05-24 21:45:31 +0000151%%
152
Mark Sleef5377b32006-10-10 01:42:59 +0000153/**
154 * Thrift Grammar Implementation.
155 *
156 * For the most part this source file works its way top down from what you
157 * might expect to find in a typical .thrift file, i.e. type definitions and
158 * namespaces up top followed by service definitions using those types.
159 */
Mark Slee31985722006-05-24 21:45:31 +0000160
161Program:
Mark Sleef0712dc2006-10-25 19:03:57 +0000162 HeaderList DefinitionList
163 {
164 pdebug("Program -> Headers DefinitionList");
165 }
166
167HeaderList:
168 HeaderList Header
169 {
170 pdebug("HeaderList -> HeaderList Header");
171 }
172|
173 {
174 pdebug("HeaderList -> ");
175 }
176
177Header:
178 Include
179 {
180 pdebug("Header -> Include");
181 }
182| tok_namespace tok_identifier
183 {
184 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
185 if (g_parse_mode == PROGRAM) {
186 g_program->set_cpp_namespace($2);
187 g_program->set_java_package($2);
188 }
189 }
190| tok_cpp_namespace tok_identifier
191 {
192 pdebug("Header -> tok_cpp_namespace tok_identifier");
193 if (g_parse_mode == PROGRAM) {
194 g_program->set_cpp_namespace($2);
195 }
196 }
197| tok_cpp_include tok_literal
198 {
199 pdebug("Header -> tok_cpp_include tok_literal");
200 if (g_parse_mode == PROGRAM) {
201 g_program->add_cpp_include($2);
202 }
203 }
Mark Sleee888b372007-01-12 01:06:24 +0000204| tok_php_namespace tok_identifier
205 {
206 pdebug("Header -> tok_php_namespace tok_identifier");
207 if (g_parse_mode == PROGRAM) {
208 g_program->set_php_namespace($2);
209 }
210 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000211| tok_java_package tok_identifier
212 {
213 pdebug("Header -> tok_java_package tok_identifier");
214 if (g_parse_mode == PROGRAM) {
215 g_program->set_java_package($2);
216 }
217 }
218
219Include:
220 tok_include tok_literal
221 {
222 pdebug("Include -> tok_include tok_literal");
223 if (g_parse_mode == INCLUDES) {
224 std::string path = include_file(std::string($2));
225 if (!path.empty()) {
226 g_program->add_include(path);
227 }
228 }
229 }
Mark Slee31985722006-05-24 21:45:31 +0000230
231DefinitionList:
232 DefinitionList Definition
233 {
234 pdebug("DefinitionList -> DefinitionList Definition");
235 }
236|
237 {
238 pdebug("DefinitionList -> ");
239 }
240
241Definition:
Mark Slee30152872006-11-28 01:24:07 +0000242 Const
243 {
244 pdebug("Definition -> Const");
245 if (g_parse_mode == PROGRAM) {
246 g_program->add_const($1);
247 }
248 }
249| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000250 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000251 pdebug("Definition -> TypeDefinition");
252 if (g_parse_mode == PROGRAM) {
253 g_scope->add_type($1->get_name(), $1);
254 if (g_parent_scope != NULL) {
255 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
256 }
257 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000258 }
Mark Slee31985722006-05-24 21:45:31 +0000259| Service
260 {
261 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000262 if (g_parse_mode == PROGRAM) {
263 g_scope->add_service($1->get_name(), $1);
264 if (g_parent_scope != NULL) {
265 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
266 }
267 g_program->add_service($1);
268 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000269 }
270
Mark Sleef0712dc2006-10-25 19:03:57 +0000271TypeDefinition:
272 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000273 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000274 pdebug("TypeDefinition -> Typedef");
275 if (g_parse_mode == PROGRAM) {
276 g_program->add_typedef($1);
277 }
278 }
279| Enum
280 {
281 pdebug("TypeDefinition -> Enum");
282 if (g_parse_mode == PROGRAM) {
283 g_program->add_enum($1);
284 }
285 }
286| Struct
287 {
288 pdebug("TypeDefinition -> Struct");
289 if (g_parse_mode == PROGRAM) {
290 g_program->add_struct($1);
291 }
292 }
293| Xception
294 {
295 pdebug("TypeDefinition -> Xception");
296 if (g_parse_mode == PROGRAM) {
297 g_program->add_xception($1);
298 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000299 }
Mark Slee31985722006-05-24 21:45:31 +0000300
301Typedef:
302 tok_typedef DefinitionType tok_identifier
303 {
304 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000305 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000306 $$ = td;
307 }
308
309Enum:
310 tok_enum tok_identifier '{' EnumDefList '}'
311 {
312 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
313 $$ = $4;
314 $$->set_name($2);
315 }
316
Mark Slee207cb462006-11-02 18:43:12 +0000317CommaOrSemicolonOptional:
318 ','
319 {}
320| ';'
321 {}
322|
323 {}
324
Mark Slee31985722006-05-24 21:45:31 +0000325EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000326 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000327 {
328 pdebug("EnumDefList -> EnumDefList EnumDef");
329 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000330 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000331 }
332|
333 {
334 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000335 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000336 }
337
338EnumDef:
Mark Slee207cb462006-11-02 18:43:12 +0000339 tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000340 {
Mark Slee30152872006-11-28 01:24:07 +0000341 pdebug("EnumDef -> tok_identifier = tok_int_constant");
Mark Slee31985722006-05-24 21:45:31 +0000342 if ($3 < 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000343 pwarning(1, "Negative value supplied for enum %s.\n", $1);
Mark Slee31985722006-05-24 21:45:31 +0000344 }
Mark Slee30152872006-11-28 01:24:07 +0000345 $$ = new t_enum_value($1, $3);
Mark Slee31985722006-05-24 21:45:31 +0000346 }
347|
Mark Slee04cc6052006-11-15 21:25:34 +0000348 tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000349 {
Mark Slee30152872006-11-28 01:24:07 +0000350 pdebug("EnumDef -> tok_identifier");
351 $$ = new t_enum_value($1);
352 }
353
354Const:
355 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
356 {
357 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000358 if (g_parse_mode == PROGRAM) {
359 $$ = new t_const($2, $3, $5);
360 validate_const_type($$);
361 } else {
362 $$ = NULL;
363 }
Mark Slee30152872006-11-28 01:24:07 +0000364 }
365
366ConstValue:
367 tok_int_constant
368 {
369 pdebug("ConstValue => tok_int_constant");
370 $$ = new t_const_value();
371 $$->set_integer($1);
372 }
373| tok_dub_constant
374 {
375 pdebug("ConstValue => tok_dub_constant");
376 $$ = new t_const_value();
377 $$->set_double($1);
378 }
379| tok_literal
380 {
381 pdebug("ConstValue => tok_literal");
382 $$ = new t_const_value();
383 $$->set_string($1);
384 }
Mark Slee67fc6342006-11-29 03:37:04 +0000385| tok_identifier
386 {
387 pdebug("ConstValue => tok_identifier");
388 $$ = new t_const_value();
389 $$->set_string($1);
390 }
Mark Slee30152872006-11-28 01:24:07 +0000391| ConstList
392 {
393 pdebug("ConstValue => ConstList");
394 $$ = $1;
395 }
396| ConstMap
397 {
398 pdebug("ConstValue => ConstMap");
399 $$ = $1;
400 }
401
402ConstList:
403 '[' ConstListContents ']'
404 {
405 pdebug("ConstList => [ ConstListContents ]");
406 $$ = $2;
407 }
408
409ConstListContents:
410 ConstListContents ConstValue CommaOrSemicolonOptional
411 {
412 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
413 $$ = $1;
414 $$->add_list($2);
415 }
416|
417 {
418 pdebug("ConstListContents =>");
419 $$ = new t_const_value();
420 $$->set_list();
421 }
422
423ConstMap:
424 '{' ConstMapContents '}'
425 {
426 pdebug("ConstMap => { ConstMapContents }");
427 $$ = $2;
428 }
429
430ConstMapContents:
431 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
432 {
433 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
434 $$ = $1;
435 $$->add_map($2, $4);
436 }
437|
438 {
439 pdebug("ConstMapContents =>");
440 $$ = new t_const_value();
441 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000442 }
443
444Struct:
Mark Slee782abbb2007-01-19 00:17:02 +0000445 tok_struct tok_identifier XsdAllOptional '{' FieldList '}'
Mark Slee31985722006-05-24 21:45:31 +0000446 {
447 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
Mark Slee782abbb2007-01-19 00:17:02 +0000448 $5->set_name($2);
449 $5->set_xsd_all($3);
450 $$ = $5;
Mark Slee9cb7c612006-09-01 22:17:45 +0000451 y_field_val = -1;
452 }
453
Mark Slee782abbb2007-01-19 00:17:02 +0000454XsdAllOptional:
455 tok_xsd_all
456 {
457 $$ = true;
458 }
459|
460 {
461 $$ = false;
462 }
463
Mark Slee9cb7c612006-09-01 22:17:45 +0000464Xception:
465 tok_xception tok_identifier '{' FieldList '}'
466 {
467 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
468 $4->set_name($2);
469 $4->set_xception(true);
470 $$ = $4;
471 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000472 }
473
474Service:
Mark Sleef0712dc2006-10-25 19:03:57 +0000475 tok_service tok_identifier ExtendsOptional '{' FunctionList '}'
Mark Slee31985722006-05-24 21:45:31 +0000476 {
477 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Mark Sleef0712dc2006-10-25 19:03:57 +0000478 $$ = $5;
Mark Slee31985722006-05-24 21:45:31 +0000479 $$->set_name($2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000480 $$->set_extends($3);
481 }
482
483ExtendsOptional:
484 tok_extends tok_identifier
485 {
486 pdebug("ExtendsOptional -> tok_extends tok_identifier");
487 $$ = NULL;
488 if (g_parse_mode == PROGRAM) {
489 $$ = g_scope->get_service($2);
490 if ($$ == NULL) {
491 yyerror("Service \"%s\" has not been defined.", $2);
492 exit(1);
493 }
494 }
495 }
496|
497 {
498 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000499 }
500
501FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000502 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000503 {
504 pdebug("FunctionList -> FunctionList Function");
505 $$ = $1;
506 $1->add_function($2);
507 }
508|
509 {
510 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000511 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000512 }
513
514Function:
Mark Slee207cb462006-11-02 18:43:12 +0000515 AsyncOptional FunctionType tok_identifier '(' FieldList ')' ThrowsOptional CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000516 {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000517 $5->set_name(std::string($3) + "_args");
Mark Slee4e755ca2006-09-12 00:46:08 +0000518 $$ = new t_function($2, $3, $5, $7, $1);
Mark Slee9cb7c612006-09-01 22:17:45 +0000519 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000520 }
521
Mark Slee52f643d2006-08-09 00:03:43 +0000522AsyncOptional:
523 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000524 {
Mark Slee52f643d2006-08-09 00:03:43 +0000525 $$ = true;
526 }
527|
528 {
529 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000530 }
531
Mark Slee9cb7c612006-09-01 22:17:45 +0000532ThrowsOptional:
533 tok_throws '(' FieldList ')'
534 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000535 pdebug("ThrowsOptional -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000536 $$ = $3;
537 }
538|
539 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000540 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000541 }
542
Mark Slee31985722006-05-24 21:45:31 +0000543FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000544 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000545 {
546 pdebug("FieldList -> FieldList , Field");
547 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000548 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000549 }
550|
551 {
552 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000553 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000554 }
555
556Field:
Mark Slee207cb462006-11-02 18:43:12 +0000557 tok_int_constant ':' FieldType tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000558 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000559 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
560 if ($1 <= 0) {
561 pwarning(1, "Nonpositive value (%d) not allowed as a field key for '%s'.\n", $1, $4);
562 $1 = y_field_val--;
Mark Slee31985722006-05-24 21:45:31 +0000563 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000564 $$ = new t_field($3, $4, $1);
Mark Slee31985722006-05-24 21:45:31 +0000565 }
Mark Slee2329a832006-11-09 00:23:30 +0000566| FieldType tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000567 {
Mark Sleee8540632006-05-30 09:24:40 +0000568 pdebug("Field -> FieldType tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000569 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 +0000570 $$ = new t_field($1, $2, y_field_val--);
Mark Slee31985722006-05-24 21:45:31 +0000571 }
Mark Slee2329a832006-11-09 00:23:30 +0000572| FieldType tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Sleef0712dc2006-10-25 19:03:57 +0000573 {
Mark Slee2329a832006-11-09 00:23:30 +0000574 pwarning(1, "Trailing = id notation is deprecated. Use 'Id: Type Name' notation instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000575 pdebug("Field -> FieldType tok_identifier = tok_int_constant");
576 if ($4 <= 0) {
577 pwarning(1, "Nonpositive value (%d) not allowed as a field key for '%s'.\n", $4, $2);
578 $4 = y_field_val--;
579 }
580 $$ = new t_field($1, $2, $4);
581 }
Mark Slee31985722006-05-24 21:45:31 +0000582
583DefinitionType:
584 BaseType
585 {
586 pdebug("DefinitionType -> BaseType");
587 $$ = $1;
588 }
Mark Sleee8540632006-05-30 09:24:40 +0000589| ContainerType
590 {
591 pdebug("DefinitionType -> ContainerType");
592 $$ = $1;
593 }
Mark Slee31985722006-05-24 21:45:31 +0000594
595FunctionType:
596 FieldType
597 {
Mark Sleee8540632006-05-30 09:24:40 +0000598 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000599 $$ = $1;
600 }
601| tok_void
602 {
Mark Sleee8540632006-05-30 09:24:40 +0000603 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000604 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000605 }
606
607FieldType:
608 tok_identifier
609 {
Mark Sleee8540632006-05-30 09:24:40 +0000610 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000611 if (g_parse_mode == INCLUDES) {
612 // Ignore identifiers in include mode
613 $$ = NULL;
614 } else {
615 // Lookup the identifier in the current scope
616 $$ = g_scope->get_type($1);
617 if ($$ == NULL) {
618 yyerror("Type \"%s\" has not been defined.", $1);
619 exit(1);
620 }
Mark Sleee8540632006-05-30 09:24:40 +0000621 }
Mark Slee31985722006-05-24 21:45:31 +0000622 }
623| BaseType
624 {
Mark Sleee8540632006-05-30 09:24:40 +0000625 pdebug("FieldType -> BaseType");
626 $$ = $1;
627 }
628| ContainerType
629 {
630 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000631 $$ = $1;
632 }
633
634BaseType:
635 tok_string
636 {
637 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000638 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000639 }
Mark Slee78f58e22006-09-02 04:17:07 +0000640| tok_bool
641 {
642 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000643 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000644 }
Mark Slee31985722006-05-24 21:45:31 +0000645| tok_byte
646 {
647 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000648 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000649 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000650| tok_i16
651 {
652 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000653 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000654 }
Mark Slee31985722006-05-24 21:45:31 +0000655| tok_i32
656 {
657 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000658 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000659 }
Mark Slee31985722006-05-24 21:45:31 +0000660| tok_i64
661 {
662 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000663 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000664 }
Mark Sleec98d0502006-09-06 02:42:25 +0000665| tok_double
666 {
667 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000668 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000669 }
Mark Slee31985722006-05-24 21:45:31 +0000670
Mark Sleee8540632006-05-30 09:24:40 +0000671ContainerType:
672 MapType
673 {
674 pdebug("ContainerType -> MapType");
675 $$ = $1;
676 }
677| SetType
678 {
679 pdebug("ContainerType -> SetType");
680 $$ = $1;
681 }
682| ListType
683 {
684 pdebug("ContainerType -> ListType");
685 $$ = $1;
686 }
687
688MapType:
Mark Slee4f8da1d2006-10-12 02:47:27 +0000689 tok_map CppTypeOptional '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000690 {
691 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000692 $$ = new t_map($4, $6);
693 if ($2 != NULL) {
694 ((t_container*)$$)->set_cpp_name(std::string($2));
695 }
Mark Sleee8540632006-05-30 09:24:40 +0000696 }
697
698SetType:
Mark Slee4f8da1d2006-10-12 02:47:27 +0000699 tok_set CppTypeOptional '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000700 {
701 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000702 $$ = new t_set($4);
703 if ($2 != NULL) {
704 ((t_container*)$$)->set_cpp_name(std::string($2));
705 }
Mark Sleee8540632006-05-30 09:24:40 +0000706 }
707
708ListType:
Mark Sleef0712dc2006-10-25 19:03:57 +0000709 tok_list '<' FieldType '>' CppTypeOptional
Mark Sleee8540632006-05-30 09:24:40 +0000710 {
711 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +0000712 $$ = new t_list($3);
713 if ($5 != NULL) {
714 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +0000715 }
716 }
717
718CppTypeOptional:
Mark Sleef0712dc2006-10-25 19:03:57 +0000719 '[' tok_cpp_type tok_literal ']'
Mark Slee4f8da1d2006-10-12 02:47:27 +0000720 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000721 $$ = $3;
Mark Slee4f8da1d2006-10-12 02:47:27 +0000722 }
723|
724 {
725 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +0000726 }
727
Mark Slee31985722006-05-24 21:45:31 +0000728%%