blob: 8dbe6214d287f3ef04c00404505d058a1b5458b4 [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 Slee9cb7c612006-09-01 22:17:45 +000069
Mark Sleef5377b32006-10-10 01:42:59 +000070/**
71 * Base datatype keywords
72 */
73%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +000074%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +000075%token tok_byte
76%token tok_string
Mark Slee9cb7c612006-09-01 22:17:45 +000077%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +000078%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +000079%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +000080%token tok_double
Mark Slee31985722006-05-24 21:45:31 +000081
Mark Sleef5377b32006-10-10 01:42:59 +000082/**
83 * Complex type keywords
84 */
Mark Slee31985722006-05-24 21:45:31 +000085%token tok_map
86%token tok_list
87%token tok_set
88
Mark Sleef5377b32006-10-10 01:42:59 +000089/**
90 * Function modifiers
91 */
Mark Slee31985722006-05-24 21:45:31 +000092%token tok_async
93
Mark Sleef5377b32006-10-10 01:42:59 +000094/**
95 * Thrift language keywords
96 */
Mark Slee31985722006-05-24 21:45:31 +000097%token tok_typedef
98%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +000099%token tok_xception
100%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000101%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000102%token tok_service
103%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000104%token tok_const
Mark Slee31985722006-05-24 21:45:31 +0000105
Mark Sleef5377b32006-10-10 01:42:59 +0000106/**
107 * Grammar nodes
108 */
109
Mark Slee31985722006-05-24 21:45:31 +0000110%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000111%type<ttype> ContainerType
112%type<ttype> MapType
113%type<ttype> SetType
114%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000115
Mark Sleef0712dc2006-10-25 19:03:57 +0000116%type<ttype> TypeDefinition
117
Mark Slee31985722006-05-24 21:45:31 +0000118%type<ttypedef> Typedef
119%type<ttype> DefinitionType
120
121%type<tfield> Field
122%type<ttype> FieldType
Mark Sleee8540632006-05-30 09:24:40 +0000123%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000124
125%type<tenum> Enum
126%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000127%type<tenumv> EnumDef
128
129%type<tconst> Const
130%type<tconstv> ConstValue
131%type<tconstv> ConstList
132%type<tconstv> ConstListContents
133%type<tconstv> ConstMap
134%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000135
136%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000137%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000138%type<tservice> Service
139
140%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000141%type<ttype> FunctionType
142%type<tservice> FunctionList
143
Mark Sleef5377b32006-10-10 01:42:59 +0000144%type<tstruct> ThrowsOptional
Mark Sleef0712dc2006-10-25 19:03:57 +0000145%type<tservice> ExtendsOptional
Mark Slee52f643d2006-08-09 00:03:43 +0000146%type<tbool> AsyncOptional
Mark Slee4f8da1d2006-10-12 02:47:27 +0000147%type<id> CppTypeOptional
Mark Slee52f643d2006-08-09 00:03:43 +0000148
Mark Slee31985722006-05-24 21:45:31 +0000149%%
150
Mark Sleef5377b32006-10-10 01:42:59 +0000151/**
152 * Thrift Grammar Implementation.
153 *
154 * For the most part this source file works its way top down from what you
155 * might expect to find in a typical .thrift file, i.e. type definitions and
156 * namespaces up top followed by service definitions using those types.
157 */
Mark Slee31985722006-05-24 21:45:31 +0000158
159Program:
Mark Sleef0712dc2006-10-25 19:03:57 +0000160 HeaderList DefinitionList
161 {
162 pdebug("Program -> Headers DefinitionList");
163 }
164
165HeaderList:
166 HeaderList Header
167 {
168 pdebug("HeaderList -> HeaderList Header");
169 }
170|
171 {
172 pdebug("HeaderList -> ");
173 }
174
175Header:
176 Include
177 {
178 pdebug("Header -> Include");
179 }
180| tok_namespace tok_identifier
181 {
182 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
183 if (g_parse_mode == PROGRAM) {
184 g_program->set_cpp_namespace($2);
185 g_program->set_java_package($2);
186 }
187 }
188| tok_cpp_namespace tok_identifier
189 {
190 pdebug("Header -> tok_cpp_namespace tok_identifier");
191 if (g_parse_mode == PROGRAM) {
192 g_program->set_cpp_namespace($2);
193 }
194 }
195| tok_cpp_include tok_literal
196 {
197 pdebug("Header -> tok_cpp_include tok_literal");
198 if (g_parse_mode == PROGRAM) {
199 g_program->add_cpp_include($2);
200 }
201 }
Mark Sleee888b372007-01-12 01:06:24 +0000202| tok_php_namespace tok_identifier
203 {
204 pdebug("Header -> tok_php_namespace tok_identifier");
205 if (g_parse_mode == PROGRAM) {
206 g_program->set_php_namespace($2);
207 }
208 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000209| tok_java_package tok_identifier
210 {
211 pdebug("Header -> tok_java_package tok_identifier");
212 if (g_parse_mode == PROGRAM) {
213 g_program->set_java_package($2);
214 }
215 }
216
217Include:
218 tok_include tok_literal
219 {
220 pdebug("Include -> tok_include tok_literal");
221 if (g_parse_mode == INCLUDES) {
222 std::string path = include_file(std::string($2));
223 if (!path.empty()) {
224 g_program->add_include(path);
225 }
226 }
227 }
Mark Slee31985722006-05-24 21:45:31 +0000228
229DefinitionList:
230 DefinitionList Definition
231 {
232 pdebug("DefinitionList -> DefinitionList Definition");
233 }
234|
235 {
236 pdebug("DefinitionList -> ");
237 }
238
239Definition:
Mark Slee30152872006-11-28 01:24:07 +0000240 Const
241 {
242 pdebug("Definition -> Const");
243 if (g_parse_mode == PROGRAM) {
244 g_program->add_const($1);
245 }
246 }
247| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000248 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000249 pdebug("Definition -> TypeDefinition");
250 if (g_parse_mode == PROGRAM) {
251 g_scope->add_type($1->get_name(), $1);
252 if (g_parent_scope != NULL) {
253 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
254 }
255 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000256 }
Mark Slee31985722006-05-24 21:45:31 +0000257| Service
258 {
259 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000260 if (g_parse_mode == PROGRAM) {
261 g_scope->add_service($1->get_name(), $1);
262 if (g_parent_scope != NULL) {
263 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
264 }
265 g_program->add_service($1);
266 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000267 }
268
Mark Sleef0712dc2006-10-25 19:03:57 +0000269TypeDefinition:
270 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000271 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000272 pdebug("TypeDefinition -> Typedef");
273 if (g_parse_mode == PROGRAM) {
274 g_program->add_typedef($1);
275 }
276 }
277| Enum
278 {
279 pdebug("TypeDefinition -> Enum");
280 if (g_parse_mode == PROGRAM) {
281 g_program->add_enum($1);
282 }
283 }
284| Struct
285 {
286 pdebug("TypeDefinition -> Struct");
287 if (g_parse_mode == PROGRAM) {
288 g_program->add_struct($1);
289 }
290 }
291| Xception
292 {
293 pdebug("TypeDefinition -> Xception");
294 if (g_parse_mode == PROGRAM) {
295 g_program->add_xception($1);
296 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000297 }
Mark Slee31985722006-05-24 21:45:31 +0000298
299Typedef:
300 tok_typedef DefinitionType tok_identifier
301 {
302 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000303 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000304 $$ = td;
305 }
306
307Enum:
308 tok_enum tok_identifier '{' EnumDefList '}'
309 {
310 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
311 $$ = $4;
312 $$->set_name($2);
313 }
314
Mark Slee207cb462006-11-02 18:43:12 +0000315CommaOrSemicolonOptional:
316 ','
317 {}
318| ';'
319 {}
320|
321 {}
322
Mark Slee31985722006-05-24 21:45:31 +0000323EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000324 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000325 {
326 pdebug("EnumDefList -> EnumDefList EnumDef");
327 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000328 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000329 }
330|
331 {
332 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000333 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000334 }
335
336EnumDef:
Mark Slee207cb462006-11-02 18:43:12 +0000337 tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000338 {
Mark Slee30152872006-11-28 01:24:07 +0000339 pdebug("EnumDef -> tok_identifier = tok_int_constant");
Mark Slee31985722006-05-24 21:45:31 +0000340 if ($3 < 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000341 pwarning(1, "Negative value supplied for enum %s.\n", $1);
Mark Slee31985722006-05-24 21:45:31 +0000342 }
Mark Slee30152872006-11-28 01:24:07 +0000343 $$ = new t_enum_value($1, $3);
Mark Slee31985722006-05-24 21:45:31 +0000344 }
345|
Mark Slee04cc6052006-11-15 21:25:34 +0000346 tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000347 {
Mark Slee30152872006-11-28 01:24:07 +0000348 pdebug("EnumDef -> tok_identifier");
349 $$ = new t_enum_value($1);
350 }
351
352Const:
353 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
354 {
355 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000356 if (g_parse_mode == PROGRAM) {
357 $$ = new t_const($2, $3, $5);
358 validate_const_type($$);
359 } else {
360 $$ = NULL;
361 }
Mark Slee30152872006-11-28 01:24:07 +0000362 }
363
364ConstValue:
365 tok_int_constant
366 {
367 pdebug("ConstValue => tok_int_constant");
368 $$ = new t_const_value();
369 $$->set_integer($1);
370 }
371| tok_dub_constant
372 {
373 pdebug("ConstValue => tok_dub_constant");
374 $$ = new t_const_value();
375 $$->set_double($1);
376 }
377| tok_literal
378 {
379 pdebug("ConstValue => tok_literal");
380 $$ = new t_const_value();
381 $$->set_string($1);
382 }
Mark Slee67fc6342006-11-29 03:37:04 +0000383| tok_identifier
384 {
385 pdebug("ConstValue => tok_identifier");
386 $$ = new t_const_value();
387 $$->set_string($1);
388 }
Mark Slee30152872006-11-28 01:24:07 +0000389| ConstList
390 {
391 pdebug("ConstValue => ConstList");
392 $$ = $1;
393 }
394| ConstMap
395 {
396 pdebug("ConstValue => ConstMap");
397 $$ = $1;
398 }
399
400ConstList:
401 '[' ConstListContents ']'
402 {
403 pdebug("ConstList => [ ConstListContents ]");
404 $$ = $2;
405 }
406
407ConstListContents:
408 ConstListContents ConstValue CommaOrSemicolonOptional
409 {
410 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
411 $$ = $1;
412 $$->add_list($2);
413 }
414|
415 {
416 pdebug("ConstListContents =>");
417 $$ = new t_const_value();
418 $$->set_list();
419 }
420
421ConstMap:
422 '{' ConstMapContents '}'
423 {
424 pdebug("ConstMap => { ConstMapContents }");
425 $$ = $2;
426 }
427
428ConstMapContents:
429 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
430 {
431 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
432 $$ = $1;
433 $$->add_map($2, $4);
434 }
435|
436 {
437 pdebug("ConstMapContents =>");
438 $$ = new t_const_value();
439 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000440 }
441
442Struct:
443 tok_struct tok_identifier '{' FieldList '}'
444 {
445 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
Mark Sleee8540632006-05-30 09:24:40 +0000446 $4->set_name($2);
447 $$ = $4;
Mark Slee9cb7c612006-09-01 22:17:45 +0000448 y_field_val = -1;
449 }
450
451Xception:
452 tok_xception tok_identifier '{' FieldList '}'
453 {
454 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
455 $4->set_name($2);
456 $4->set_xception(true);
457 $$ = $4;
458 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000459 }
460
461Service:
Mark Sleef0712dc2006-10-25 19:03:57 +0000462 tok_service tok_identifier ExtendsOptional '{' FunctionList '}'
Mark Slee31985722006-05-24 21:45:31 +0000463 {
464 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Mark Sleef0712dc2006-10-25 19:03:57 +0000465 $$ = $5;
Mark Slee31985722006-05-24 21:45:31 +0000466 $$->set_name($2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000467 $$->set_extends($3);
468 }
469
470ExtendsOptional:
471 tok_extends tok_identifier
472 {
473 pdebug("ExtendsOptional -> tok_extends tok_identifier");
474 $$ = NULL;
475 if (g_parse_mode == PROGRAM) {
476 $$ = g_scope->get_service($2);
477 if ($$ == NULL) {
478 yyerror("Service \"%s\" has not been defined.", $2);
479 exit(1);
480 }
481 }
482 }
483|
484 {
485 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000486 }
487
488FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000489 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000490 {
491 pdebug("FunctionList -> FunctionList Function");
492 $$ = $1;
493 $1->add_function($2);
494 }
495|
496 {
497 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000498 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000499 }
500
501Function:
Mark Slee207cb462006-11-02 18:43:12 +0000502 AsyncOptional FunctionType tok_identifier '(' FieldList ')' ThrowsOptional CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000503 {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000504 $5->set_name(std::string($3) + "_args");
Mark Slee4e755ca2006-09-12 00:46:08 +0000505 $$ = new t_function($2, $3, $5, $7, $1);
Mark Slee9cb7c612006-09-01 22:17:45 +0000506 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000507 }
508
Mark Slee52f643d2006-08-09 00:03:43 +0000509AsyncOptional:
510 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000511 {
Mark Slee52f643d2006-08-09 00:03:43 +0000512 $$ = true;
513 }
514|
515 {
516 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000517 }
518
Mark Slee9cb7c612006-09-01 22:17:45 +0000519ThrowsOptional:
520 tok_throws '(' FieldList ')'
521 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000522 pdebug("ThrowsOptional -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000523 $$ = $3;
524 }
525|
526 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000527 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000528 }
529
Mark Slee31985722006-05-24 21:45:31 +0000530FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000531 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000532 {
533 pdebug("FieldList -> FieldList , Field");
534 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000535 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000536 }
537|
538 {
539 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000540 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000541 }
542
543Field:
Mark Slee207cb462006-11-02 18:43:12 +0000544 tok_int_constant ':' FieldType tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000545 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000546 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
547 if ($1 <= 0) {
548 pwarning(1, "Nonpositive value (%d) not allowed as a field key for '%s'.\n", $1, $4);
549 $1 = y_field_val--;
Mark Slee31985722006-05-24 21:45:31 +0000550 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000551 $$ = new t_field($3, $4, $1);
Mark Slee31985722006-05-24 21:45:31 +0000552 }
Mark Slee2329a832006-11-09 00:23:30 +0000553| FieldType tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000554 {
Mark Sleee8540632006-05-30 09:24:40 +0000555 pdebug("Field -> FieldType tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000556 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 +0000557 $$ = new t_field($1, $2, y_field_val--);
Mark Slee31985722006-05-24 21:45:31 +0000558 }
Mark Slee2329a832006-11-09 00:23:30 +0000559| FieldType tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Sleef0712dc2006-10-25 19:03:57 +0000560 {
Mark Slee2329a832006-11-09 00:23:30 +0000561 pwarning(1, "Trailing = id notation is deprecated. Use 'Id: Type Name' notation instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000562 pdebug("Field -> FieldType tok_identifier = tok_int_constant");
563 if ($4 <= 0) {
564 pwarning(1, "Nonpositive value (%d) not allowed as a field key for '%s'.\n", $4, $2);
565 $4 = y_field_val--;
566 }
567 $$ = new t_field($1, $2, $4);
568 }
Mark Slee31985722006-05-24 21:45:31 +0000569
570DefinitionType:
571 BaseType
572 {
573 pdebug("DefinitionType -> BaseType");
574 $$ = $1;
575 }
Mark Sleee8540632006-05-30 09:24:40 +0000576| ContainerType
577 {
578 pdebug("DefinitionType -> ContainerType");
579 $$ = $1;
580 }
Mark Slee31985722006-05-24 21:45:31 +0000581
582FunctionType:
583 FieldType
584 {
Mark Sleee8540632006-05-30 09:24:40 +0000585 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000586 $$ = $1;
587 }
588| tok_void
589 {
Mark Sleee8540632006-05-30 09:24:40 +0000590 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000591 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000592 }
593
594FieldType:
595 tok_identifier
596 {
Mark Sleee8540632006-05-30 09:24:40 +0000597 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000598 if (g_parse_mode == INCLUDES) {
599 // Ignore identifiers in include mode
600 $$ = NULL;
601 } else {
602 // Lookup the identifier in the current scope
603 $$ = g_scope->get_type($1);
604 if ($$ == NULL) {
605 yyerror("Type \"%s\" has not been defined.", $1);
606 exit(1);
607 }
Mark Sleee8540632006-05-30 09:24:40 +0000608 }
Mark Slee31985722006-05-24 21:45:31 +0000609 }
610| BaseType
611 {
Mark Sleee8540632006-05-30 09:24:40 +0000612 pdebug("FieldType -> BaseType");
613 $$ = $1;
614 }
615| ContainerType
616 {
617 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000618 $$ = $1;
619 }
620
621BaseType:
622 tok_string
623 {
624 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000625 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000626 }
Mark Slee78f58e22006-09-02 04:17:07 +0000627| tok_bool
628 {
629 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000630 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000631 }
Mark Slee31985722006-05-24 21:45:31 +0000632| tok_byte
633 {
634 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000635 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000636 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000637| tok_i16
638 {
639 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000640 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000641 }
Mark Slee31985722006-05-24 21:45:31 +0000642| tok_i32
643 {
644 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000645 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000646 }
Mark Slee31985722006-05-24 21:45:31 +0000647| tok_i64
648 {
649 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000650 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000651 }
Mark Sleec98d0502006-09-06 02:42:25 +0000652| tok_double
653 {
654 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000655 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000656 }
Mark Slee31985722006-05-24 21:45:31 +0000657
Mark Sleee8540632006-05-30 09:24:40 +0000658ContainerType:
659 MapType
660 {
661 pdebug("ContainerType -> MapType");
662 $$ = $1;
663 }
664| SetType
665 {
666 pdebug("ContainerType -> SetType");
667 $$ = $1;
668 }
669| ListType
670 {
671 pdebug("ContainerType -> ListType");
672 $$ = $1;
673 }
674
675MapType:
Mark Slee4f8da1d2006-10-12 02:47:27 +0000676 tok_map CppTypeOptional '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000677 {
678 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000679 $$ = new t_map($4, $6);
680 if ($2 != NULL) {
681 ((t_container*)$$)->set_cpp_name(std::string($2));
682 }
Mark Sleee8540632006-05-30 09:24:40 +0000683 }
684
685SetType:
Mark Slee4f8da1d2006-10-12 02:47:27 +0000686 tok_set CppTypeOptional '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000687 {
688 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000689 $$ = new t_set($4);
690 if ($2 != NULL) {
691 ((t_container*)$$)->set_cpp_name(std::string($2));
692 }
Mark Sleee8540632006-05-30 09:24:40 +0000693 }
694
695ListType:
Mark Sleef0712dc2006-10-25 19:03:57 +0000696 tok_list '<' FieldType '>' CppTypeOptional
Mark Sleee8540632006-05-30 09:24:40 +0000697 {
698 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +0000699 $$ = new t_list($3);
700 if ($5 != NULL) {
701 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +0000702 }
703 }
704
705CppTypeOptional:
Mark Sleef0712dc2006-10-25 19:03:57 +0000706 '[' tok_cpp_type tok_literal ']'
Mark Slee4f8da1d2006-10-12 02:47:27 +0000707 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000708 $$ = $3;
Mark Slee4f8da1d2006-10-12 02:47:27 +0000709 }
710|
711 {
712 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +0000713 }
714
Mark Slee31985722006-05-24 21:45:31 +0000715%%