blob: 9dee5350b14aa5243d34d4ab1eea1ea423c5d71b [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
67%token tok_java_package
Mark Slee9cb7c612006-09-01 22:17:45 +000068
Mark Sleef5377b32006-10-10 01:42:59 +000069/**
70 * Base datatype keywords
71 */
72%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +000073%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +000074%token tok_byte
75%token tok_string
Mark Slee9cb7c612006-09-01 22:17:45 +000076%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +000077%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +000078%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +000079%token tok_double
Mark Slee31985722006-05-24 21:45:31 +000080
Mark Sleef5377b32006-10-10 01:42:59 +000081/**
82 * Complex type keywords
83 */
Mark Slee31985722006-05-24 21:45:31 +000084%token tok_map
85%token tok_list
86%token tok_set
87
Mark Sleef5377b32006-10-10 01:42:59 +000088/**
89 * Function modifiers
90 */
Mark Slee31985722006-05-24 21:45:31 +000091%token tok_async
92
Mark Sleef5377b32006-10-10 01:42:59 +000093/**
94 * Thrift language keywords
95 */
Mark Slee31985722006-05-24 21:45:31 +000096%token tok_typedef
97%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +000098%token tok_xception
99%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000100%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000101%token tok_service
102%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000103%token tok_const
Mark Slee31985722006-05-24 21:45:31 +0000104
Mark Sleef5377b32006-10-10 01:42:59 +0000105/**
106 * Grammar nodes
107 */
108
Mark Slee31985722006-05-24 21:45:31 +0000109%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000110%type<ttype> ContainerType
111%type<ttype> MapType
112%type<ttype> SetType
113%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000114
Mark Sleef0712dc2006-10-25 19:03:57 +0000115%type<ttype> TypeDefinition
116
Mark Slee31985722006-05-24 21:45:31 +0000117%type<ttypedef> Typedef
118%type<ttype> DefinitionType
119
120%type<tfield> Field
121%type<ttype> FieldType
Mark Sleee8540632006-05-30 09:24:40 +0000122%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000123
124%type<tenum> Enum
125%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000126%type<tenumv> EnumDef
127
128%type<tconst> Const
129%type<tconstv> ConstValue
130%type<tconstv> ConstList
131%type<tconstv> ConstListContents
132%type<tconstv> ConstMap
133%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000134
135%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000136%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000137%type<tservice> Service
138
139%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000140%type<ttype> FunctionType
141%type<tservice> FunctionList
142
Mark Sleef5377b32006-10-10 01:42:59 +0000143%type<tstruct> ThrowsOptional
Mark Sleef0712dc2006-10-25 19:03:57 +0000144%type<tservice> ExtendsOptional
Mark Slee52f643d2006-08-09 00:03:43 +0000145%type<tbool> AsyncOptional
Mark Slee4f8da1d2006-10-12 02:47:27 +0000146%type<id> CppTypeOptional
Mark Slee52f643d2006-08-09 00:03:43 +0000147
Mark Slee31985722006-05-24 21:45:31 +0000148%%
149
Mark Sleef5377b32006-10-10 01:42:59 +0000150/**
151 * Thrift Grammar Implementation.
152 *
153 * For the most part this source file works its way top down from what you
154 * might expect to find in a typical .thrift file, i.e. type definitions and
155 * namespaces up top followed by service definitions using those types.
156 */
Mark Slee31985722006-05-24 21:45:31 +0000157
158Program:
Mark Sleef0712dc2006-10-25 19:03:57 +0000159 HeaderList DefinitionList
160 {
161 pdebug("Program -> Headers DefinitionList");
162 }
163
164HeaderList:
165 HeaderList Header
166 {
167 pdebug("HeaderList -> HeaderList Header");
168 }
169|
170 {
171 pdebug("HeaderList -> ");
172 }
173
174Header:
175 Include
176 {
177 pdebug("Header -> Include");
178 }
179| tok_namespace tok_identifier
180 {
181 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
182 if (g_parse_mode == PROGRAM) {
183 g_program->set_cpp_namespace($2);
184 g_program->set_java_package($2);
185 }
186 }
187| tok_cpp_namespace tok_identifier
188 {
189 pdebug("Header -> tok_cpp_namespace tok_identifier");
190 if (g_parse_mode == PROGRAM) {
191 g_program->set_cpp_namespace($2);
192 }
193 }
194| tok_cpp_include tok_literal
195 {
196 pdebug("Header -> tok_cpp_include tok_literal");
197 if (g_parse_mode == PROGRAM) {
198 g_program->add_cpp_include($2);
199 }
200 }
201| tok_java_package tok_identifier
202 {
203 pdebug("Header -> tok_java_package tok_identifier");
204 if (g_parse_mode == PROGRAM) {
205 g_program->set_java_package($2);
206 }
207 }
208
209Include:
210 tok_include tok_literal
211 {
212 pdebug("Include -> tok_include tok_literal");
213 if (g_parse_mode == INCLUDES) {
214 std::string path = include_file(std::string($2));
215 if (!path.empty()) {
216 g_program->add_include(path);
217 }
218 }
219 }
Mark Slee31985722006-05-24 21:45:31 +0000220
221DefinitionList:
222 DefinitionList Definition
223 {
224 pdebug("DefinitionList -> DefinitionList Definition");
225 }
226|
227 {
228 pdebug("DefinitionList -> ");
229 }
230
231Definition:
Mark Slee30152872006-11-28 01:24:07 +0000232 Const
233 {
234 pdebug("Definition -> Const");
235 if (g_parse_mode == PROGRAM) {
236 g_program->add_const($1);
237 }
238 }
239| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000240 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000241 pdebug("Definition -> TypeDefinition");
242 if (g_parse_mode == PROGRAM) {
243 g_scope->add_type($1->get_name(), $1);
244 if (g_parent_scope != NULL) {
245 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
246 }
247 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000248 }
Mark Slee31985722006-05-24 21:45:31 +0000249| Service
250 {
251 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000252 if (g_parse_mode == PROGRAM) {
253 g_scope->add_service($1->get_name(), $1);
254 if (g_parent_scope != NULL) {
255 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
256 }
257 g_program->add_service($1);
258 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000259 }
260
Mark Sleef0712dc2006-10-25 19:03:57 +0000261TypeDefinition:
262 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000263 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000264 pdebug("TypeDefinition -> Typedef");
265 if (g_parse_mode == PROGRAM) {
266 g_program->add_typedef($1);
267 }
268 }
269| Enum
270 {
271 pdebug("TypeDefinition -> Enum");
272 if (g_parse_mode == PROGRAM) {
273 g_program->add_enum($1);
274 }
275 }
276| Struct
277 {
278 pdebug("TypeDefinition -> Struct");
279 if (g_parse_mode == PROGRAM) {
280 g_program->add_struct($1);
281 }
282 }
283| Xception
284 {
285 pdebug("TypeDefinition -> Xception");
286 if (g_parse_mode == PROGRAM) {
287 g_program->add_xception($1);
288 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000289 }
Mark Slee31985722006-05-24 21:45:31 +0000290
291Typedef:
292 tok_typedef DefinitionType tok_identifier
293 {
294 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000295 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000296 $$ = td;
297 }
298
299Enum:
300 tok_enum tok_identifier '{' EnumDefList '}'
301 {
302 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
303 $$ = $4;
304 $$->set_name($2);
305 }
306
Mark Slee207cb462006-11-02 18:43:12 +0000307CommaOrSemicolonOptional:
308 ','
309 {}
310| ';'
311 {}
312|
313 {}
314
Mark Slee31985722006-05-24 21:45:31 +0000315EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000316 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000317 {
318 pdebug("EnumDefList -> EnumDefList EnumDef");
319 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000320 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000321 }
322|
323 {
324 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000325 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000326 }
327
328EnumDef:
Mark Slee207cb462006-11-02 18:43:12 +0000329 tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000330 {
Mark Slee30152872006-11-28 01:24:07 +0000331 pdebug("EnumDef -> tok_identifier = tok_int_constant");
Mark Slee31985722006-05-24 21:45:31 +0000332 if ($3 < 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000333 pwarning(1, "Negative value supplied for enum %s.\n", $1);
Mark Slee31985722006-05-24 21:45:31 +0000334 }
Mark Slee30152872006-11-28 01:24:07 +0000335 $$ = new t_enum_value($1, $3);
Mark Slee31985722006-05-24 21:45:31 +0000336 }
337|
Mark Slee04cc6052006-11-15 21:25:34 +0000338 tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000339 {
Mark Slee30152872006-11-28 01:24:07 +0000340 pdebug("EnumDef -> tok_identifier");
341 $$ = new t_enum_value($1);
342 }
343
344Const:
345 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
346 {
347 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000348 if (g_parse_mode == PROGRAM) {
349 $$ = new t_const($2, $3, $5);
350 validate_const_type($$);
351 } else {
352 $$ = NULL;
353 }
Mark Slee30152872006-11-28 01:24:07 +0000354 }
355
356ConstValue:
357 tok_int_constant
358 {
359 pdebug("ConstValue => tok_int_constant");
360 $$ = new t_const_value();
361 $$->set_integer($1);
362 }
363| tok_dub_constant
364 {
365 pdebug("ConstValue => tok_dub_constant");
366 $$ = new t_const_value();
367 $$->set_double($1);
368 }
369| tok_literal
370 {
371 pdebug("ConstValue => tok_literal");
372 $$ = new t_const_value();
373 $$->set_string($1);
374 }
Mark Slee67fc6342006-11-29 03:37:04 +0000375| tok_identifier
376 {
377 pdebug("ConstValue => tok_identifier");
378 $$ = new t_const_value();
379 $$->set_string($1);
380 }
Mark Slee30152872006-11-28 01:24:07 +0000381| ConstList
382 {
383 pdebug("ConstValue => ConstList");
384 $$ = $1;
385 }
386| ConstMap
387 {
388 pdebug("ConstValue => ConstMap");
389 $$ = $1;
390 }
391
392ConstList:
393 '[' ConstListContents ']'
394 {
395 pdebug("ConstList => [ ConstListContents ]");
396 $$ = $2;
397 }
398
399ConstListContents:
400 ConstListContents ConstValue CommaOrSemicolonOptional
401 {
402 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
403 $$ = $1;
404 $$->add_list($2);
405 }
406|
407 {
408 pdebug("ConstListContents =>");
409 $$ = new t_const_value();
410 $$->set_list();
411 }
412
413ConstMap:
414 '{' ConstMapContents '}'
415 {
416 pdebug("ConstMap => { ConstMapContents }");
417 $$ = $2;
418 }
419
420ConstMapContents:
421 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
422 {
423 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
424 $$ = $1;
425 $$->add_map($2, $4);
426 }
427|
428 {
429 pdebug("ConstMapContents =>");
430 $$ = new t_const_value();
431 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000432 }
433
434Struct:
435 tok_struct tok_identifier '{' FieldList '}'
436 {
437 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
Mark Sleee8540632006-05-30 09:24:40 +0000438 $4->set_name($2);
439 $$ = $4;
Mark Slee9cb7c612006-09-01 22:17:45 +0000440 y_field_val = -1;
441 }
442
443Xception:
444 tok_xception tok_identifier '{' FieldList '}'
445 {
446 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
447 $4->set_name($2);
448 $4->set_xception(true);
449 $$ = $4;
450 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000451 }
452
453Service:
Mark Sleef0712dc2006-10-25 19:03:57 +0000454 tok_service tok_identifier ExtendsOptional '{' FunctionList '}'
Mark Slee31985722006-05-24 21:45:31 +0000455 {
456 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Mark Sleef0712dc2006-10-25 19:03:57 +0000457 $$ = $5;
Mark Slee31985722006-05-24 21:45:31 +0000458 $$->set_name($2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000459 $$->set_extends($3);
460 }
461
462ExtendsOptional:
463 tok_extends tok_identifier
464 {
465 pdebug("ExtendsOptional -> tok_extends tok_identifier");
466 $$ = NULL;
467 if (g_parse_mode == PROGRAM) {
468 $$ = g_scope->get_service($2);
469 if ($$ == NULL) {
470 yyerror("Service \"%s\" has not been defined.", $2);
471 exit(1);
472 }
473 }
474 }
475|
476 {
477 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000478 }
479
480FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000481 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000482 {
483 pdebug("FunctionList -> FunctionList Function");
484 $$ = $1;
485 $1->add_function($2);
486 }
487|
488 {
489 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000490 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000491 }
492
493Function:
Mark Slee207cb462006-11-02 18:43:12 +0000494 AsyncOptional FunctionType tok_identifier '(' FieldList ')' ThrowsOptional CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000495 {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000496 $5->set_name(std::string($3) + "_args");
Mark Slee4e755ca2006-09-12 00:46:08 +0000497 $$ = new t_function($2, $3, $5, $7, $1);
Mark Slee9cb7c612006-09-01 22:17:45 +0000498 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000499 }
500
Mark Slee52f643d2006-08-09 00:03:43 +0000501AsyncOptional:
502 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000503 {
Mark Slee52f643d2006-08-09 00:03:43 +0000504 $$ = true;
505 }
506|
507 {
508 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000509 }
510
Mark Slee9cb7c612006-09-01 22:17:45 +0000511ThrowsOptional:
512 tok_throws '(' FieldList ')'
513 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000514 pdebug("ThrowsOptional -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000515 $$ = $3;
516 }
517|
518 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000519 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000520 }
521
Mark Slee31985722006-05-24 21:45:31 +0000522FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000523 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000524 {
525 pdebug("FieldList -> FieldList , Field");
526 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000527 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000528 }
529|
530 {
531 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000532 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000533 }
534
535Field:
Mark Slee207cb462006-11-02 18:43:12 +0000536 tok_int_constant ':' FieldType tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000537 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000538 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
539 if ($1 <= 0) {
540 pwarning(1, "Nonpositive value (%d) not allowed as a field key for '%s'.\n", $1, $4);
541 $1 = y_field_val--;
Mark Slee31985722006-05-24 21:45:31 +0000542 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000543 $$ = new t_field($3, $4, $1);
Mark Slee31985722006-05-24 21:45:31 +0000544 }
Mark Slee2329a832006-11-09 00:23:30 +0000545| FieldType tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000546 {
Mark Sleee8540632006-05-30 09:24:40 +0000547 pdebug("Field -> FieldType tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000548 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 +0000549 $$ = new t_field($1, $2, y_field_val--);
Mark Slee31985722006-05-24 21:45:31 +0000550 }
Mark Slee2329a832006-11-09 00:23:30 +0000551| FieldType tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Sleef0712dc2006-10-25 19:03:57 +0000552 {
Mark Slee2329a832006-11-09 00:23:30 +0000553 pwarning(1, "Trailing = id notation is deprecated. Use 'Id: Type Name' notation instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000554 pdebug("Field -> FieldType tok_identifier = tok_int_constant");
555 if ($4 <= 0) {
556 pwarning(1, "Nonpositive value (%d) not allowed as a field key for '%s'.\n", $4, $2);
557 $4 = y_field_val--;
558 }
559 $$ = new t_field($1, $2, $4);
560 }
Mark Slee31985722006-05-24 21:45:31 +0000561
562DefinitionType:
563 BaseType
564 {
565 pdebug("DefinitionType -> BaseType");
566 $$ = $1;
567 }
Mark Sleee8540632006-05-30 09:24:40 +0000568| ContainerType
569 {
570 pdebug("DefinitionType -> ContainerType");
571 $$ = $1;
572 }
Mark Slee31985722006-05-24 21:45:31 +0000573
574FunctionType:
575 FieldType
576 {
Mark Sleee8540632006-05-30 09:24:40 +0000577 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000578 $$ = $1;
579 }
580| tok_void
581 {
Mark Sleee8540632006-05-30 09:24:40 +0000582 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000583 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000584 }
585
586FieldType:
587 tok_identifier
588 {
Mark Sleee8540632006-05-30 09:24:40 +0000589 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000590 if (g_parse_mode == INCLUDES) {
591 // Ignore identifiers in include mode
592 $$ = NULL;
593 } else {
594 // Lookup the identifier in the current scope
595 $$ = g_scope->get_type($1);
596 if ($$ == NULL) {
597 yyerror("Type \"%s\" has not been defined.", $1);
598 exit(1);
599 }
Mark Sleee8540632006-05-30 09:24:40 +0000600 }
Mark Slee31985722006-05-24 21:45:31 +0000601 }
602| BaseType
603 {
Mark Sleee8540632006-05-30 09:24:40 +0000604 pdebug("FieldType -> BaseType");
605 $$ = $1;
606 }
607| ContainerType
608 {
609 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000610 $$ = $1;
611 }
612
613BaseType:
614 tok_string
615 {
616 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000617 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000618 }
Mark Slee78f58e22006-09-02 04:17:07 +0000619| tok_bool
620 {
621 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000622 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000623 }
Mark Slee31985722006-05-24 21:45:31 +0000624| tok_byte
625 {
626 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000627 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000628 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000629| tok_i16
630 {
631 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000632 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000633 }
Mark Slee31985722006-05-24 21:45:31 +0000634| tok_i32
635 {
636 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000637 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000638 }
Mark Slee31985722006-05-24 21:45:31 +0000639| tok_i64
640 {
641 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000642 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000643 }
Mark Sleec98d0502006-09-06 02:42:25 +0000644| tok_double
645 {
646 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000647 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000648 }
Mark Slee31985722006-05-24 21:45:31 +0000649
Mark Sleee8540632006-05-30 09:24:40 +0000650ContainerType:
651 MapType
652 {
653 pdebug("ContainerType -> MapType");
654 $$ = $1;
655 }
656| SetType
657 {
658 pdebug("ContainerType -> SetType");
659 $$ = $1;
660 }
661| ListType
662 {
663 pdebug("ContainerType -> ListType");
664 $$ = $1;
665 }
666
667MapType:
Mark Slee4f8da1d2006-10-12 02:47:27 +0000668 tok_map CppTypeOptional '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000669 {
670 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000671 $$ = new t_map($4, $6);
672 if ($2 != NULL) {
673 ((t_container*)$$)->set_cpp_name(std::string($2));
674 }
Mark Sleee8540632006-05-30 09:24:40 +0000675 }
676
677SetType:
Mark Slee4f8da1d2006-10-12 02:47:27 +0000678 tok_set CppTypeOptional '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000679 {
680 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000681 $$ = new t_set($4);
682 if ($2 != NULL) {
683 ((t_container*)$$)->set_cpp_name(std::string($2));
684 }
Mark Sleee8540632006-05-30 09:24:40 +0000685 }
686
687ListType:
Mark Sleef0712dc2006-10-25 19:03:57 +0000688 tok_list '<' FieldType '>' CppTypeOptional
Mark Sleee8540632006-05-30 09:24:40 +0000689 {
690 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +0000691 $$ = new t_list($3);
692 if ($5 != NULL) {
693 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +0000694 }
695 }
696
697CppTypeOptional:
Mark Sleef0712dc2006-10-25 19:03:57 +0000698 '[' tok_cpp_type tok_literal ']'
Mark Slee4f8da1d2006-10-12 02:47:27 +0000699 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000700 $$ = $3;
Mark Slee4f8da1d2006-10-12 02:47:27 +0000701 }
702|
703 {
704 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +0000705 }
706
Mark Slee31985722006-05-24 21:45:31 +0000707%%