blob: d8f343550732760bd407e80e89fb1c0276ff8893 [file] [log] [blame]
Mark Slee31985722006-05-24 21:45:31 +00001%{
2
3/**
4 * Thrift parser.
5 *
6 * This parser is used on a thrift definition file.
7 *
8 * @author Mark Slee <mcslee@facebook.com>
9 */
10
11#include <stdio.h>
12#include "main.h"
13#include "globals.h"
14#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000015#include "parse/t_scope.h"
Mark Slee31985722006-05-24 21:45:31 +000016
Mark Sleef5377b32006-10-10 01:42:59 +000017/**
18 * This global variable is used for automatic numbering of field indices etc.
19 * when parsing the members of a struct. Field values are automatically
20 * assigned starting from -1 and working their way down.
21 */
Mark Slee9cb7c612006-09-01 22:17:45 +000022int y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +000023
24%}
25
Mark Sleef5377b32006-10-10 01:42:59 +000026/**
27 * This structure is used by the parser to hold the data types associated with
28 * various parse nodes.
29 */
Mark Slee31985722006-05-24 21:45:31 +000030%union {
Mark Slee30152872006-11-28 01:24:07 +000031 char* id;
32 int iconst;
33 double dconst;
34 bool tbool;
35 t_type* ttype;
36 t_typedef* ttypedef;
37 t_enum* tenum;
38 t_enum_value* tenumv;
39 t_const* tconst;
40 t_const_value* tconstv;
41 t_struct* tstruct;
42 t_service* tservice;
43 t_function* tfunction;
44 t_field* tfield;
Mark Slee31985722006-05-24 21:45:31 +000045}
46
Mark Sleef5377b32006-10-10 01:42:59 +000047/**
48 * Strings identifier
49 */
Mark Slee31985722006-05-24 21:45:31 +000050%token<id> tok_identifier
Mark Sleef0712dc2006-10-25 19:03:57 +000051%token<id> tok_literal
Mark Sleef5377b32006-10-10 01:42:59 +000052
53/**
Mark Slee30152872006-11-28 01:24:07 +000054 * Constant values
Mark Sleef5377b32006-10-10 01:42:59 +000055 */
Mark Slee31985722006-05-24 21:45:31 +000056%token<iconst> tok_int_constant
Mark Slee30152872006-11-28 01:24:07 +000057%token<dconst> tok_dub_constant
Mark Slee31985722006-05-24 21:45:31 +000058
Mark Sleef5377b32006-10-10 01:42:59 +000059/**
Mark Sleef0712dc2006-10-25 19:03:57 +000060 * Header keywoards
Mark Sleef5377b32006-10-10 01:42:59 +000061 */
Mark Sleef0712dc2006-10-25 19:03:57 +000062%token tok_include
Mark Slee9cb7c612006-09-01 22:17:45 +000063%token tok_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000064%token tok_cpp_namespace
65%token tok_cpp_include
66%token tok_cpp_type
Mark Sleee888b372007-01-12 01:06:24 +000067%token tok_php_namespace
Mark Sleef0712dc2006-10-25 19:03:57 +000068%token tok_java_package
Mark Slee782abbb2007-01-19 00:17:02 +000069%token tok_xsd_all
Mark Slee36bfa2e2007-01-19 20:09:51 +000070%token tok_xsd_optional
Mark Slee9cb7c612006-09-01 22:17:45 +000071
Mark Sleef5377b32006-10-10 01:42:59 +000072/**
73 * Base datatype keywords
74 */
75%token tok_void
Mark Slee78f58e22006-09-02 04:17:07 +000076%token tok_bool
Mark Slee31985722006-05-24 21:45:31 +000077%token tok_byte
78%token tok_string
Mark Sleeb6200d82007-01-19 19:14:36 +000079%token tok_slist
Mark Slee9cb7c612006-09-01 22:17:45 +000080%token tok_i16
Mark Slee31985722006-05-24 21:45:31 +000081%token tok_i32
Mark Slee31985722006-05-24 21:45:31 +000082%token tok_i64
Mark Slee9cb7c612006-09-01 22:17:45 +000083%token tok_double
Mark Slee31985722006-05-24 21:45:31 +000084
Mark Sleef5377b32006-10-10 01:42:59 +000085/**
86 * Complex type keywords
87 */
Mark Slee31985722006-05-24 21:45:31 +000088%token tok_map
89%token tok_list
90%token tok_set
91
Mark Sleef5377b32006-10-10 01:42:59 +000092/**
93 * Function modifiers
94 */
Mark Slee31985722006-05-24 21:45:31 +000095%token tok_async
96
Mark Sleef5377b32006-10-10 01:42:59 +000097/**
98 * Thrift language keywords
99 */
Mark Slee31985722006-05-24 21:45:31 +0000100%token tok_typedef
101%token tok_struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000102%token tok_xception
103%token tok_throws
Mark Sleef0712dc2006-10-25 19:03:57 +0000104%token tok_extends
Mark Slee31985722006-05-24 21:45:31 +0000105%token tok_service
106%token tok_enum
Mark Slee30152872006-11-28 01:24:07 +0000107%token tok_const
Mark Slee31985722006-05-24 21:45:31 +0000108
Mark Sleef5377b32006-10-10 01:42:59 +0000109/**
110 * Grammar nodes
111 */
112
Mark Slee31985722006-05-24 21:45:31 +0000113%type<ttype> BaseType
Mark Sleee8540632006-05-30 09:24:40 +0000114%type<ttype> ContainerType
115%type<ttype> MapType
116%type<ttype> SetType
117%type<ttype> ListType
Mark Slee31985722006-05-24 21:45:31 +0000118
Mark Sleef0712dc2006-10-25 19:03:57 +0000119%type<ttype> TypeDefinition
120
Mark Slee31985722006-05-24 21:45:31 +0000121%type<ttypedef> Typedef
122%type<ttype> DefinitionType
123
124%type<tfield> Field
125%type<ttype> FieldType
Mark Sleee8540632006-05-30 09:24:40 +0000126%type<tstruct> FieldList
Mark Slee31985722006-05-24 21:45:31 +0000127
128%type<tenum> Enum
129%type<tenum> EnumDefList
Mark Slee30152872006-11-28 01:24:07 +0000130%type<tenumv> EnumDef
131
132%type<tconst> Const
133%type<tconstv> ConstValue
134%type<tconstv> ConstList
135%type<tconstv> ConstListContents
136%type<tconstv> ConstMap
137%type<tconstv> ConstMapContents
Mark Slee31985722006-05-24 21:45:31 +0000138
139%type<tstruct> Struct
Mark Slee9cb7c612006-09-01 22:17:45 +0000140%type<tstruct> Xception
Mark Slee31985722006-05-24 21:45:31 +0000141%type<tservice> Service
142
143%type<tfunction> Function
Mark Slee31985722006-05-24 21:45:31 +0000144%type<ttype> FunctionType
145%type<tservice> FunctionList
146
Mark Slee36bfa2e2007-01-19 20:09:51 +0000147%type<tstruct> Throws
148%type<tservice> Extends
149%type<tbool> Async
150%type<tbool> XsdAll
151%type<tbool> XsdOptional
152%type<id> CppType
Mark Slee52f643d2006-08-09 00:03:43 +0000153
Mark Slee31985722006-05-24 21:45:31 +0000154%%
155
Mark Sleef5377b32006-10-10 01:42:59 +0000156/**
157 * Thrift Grammar Implementation.
158 *
159 * For the most part this source file works its way top down from what you
160 * might expect to find in a typical .thrift file, i.e. type definitions and
161 * namespaces up top followed by service definitions using those types.
162 */
Mark Slee31985722006-05-24 21:45:31 +0000163
164Program:
Mark Sleef0712dc2006-10-25 19:03:57 +0000165 HeaderList DefinitionList
166 {
167 pdebug("Program -> Headers DefinitionList");
168 }
169
170HeaderList:
171 HeaderList Header
172 {
173 pdebug("HeaderList -> HeaderList Header");
174 }
175|
176 {
177 pdebug("HeaderList -> ");
178 }
179
180Header:
181 Include
182 {
183 pdebug("Header -> Include");
184 }
185| tok_namespace tok_identifier
186 {
187 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
188 if (g_parse_mode == PROGRAM) {
189 g_program->set_cpp_namespace($2);
190 g_program->set_java_package($2);
191 }
192 }
193| tok_cpp_namespace tok_identifier
194 {
195 pdebug("Header -> tok_cpp_namespace tok_identifier");
196 if (g_parse_mode == PROGRAM) {
197 g_program->set_cpp_namespace($2);
198 }
199 }
200| tok_cpp_include tok_literal
201 {
202 pdebug("Header -> tok_cpp_include tok_literal");
203 if (g_parse_mode == PROGRAM) {
204 g_program->add_cpp_include($2);
205 }
206 }
Mark Sleee888b372007-01-12 01:06:24 +0000207| tok_php_namespace tok_identifier
208 {
209 pdebug("Header -> tok_php_namespace tok_identifier");
210 if (g_parse_mode == PROGRAM) {
211 g_program->set_php_namespace($2);
212 }
213 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000214| tok_java_package tok_identifier
215 {
216 pdebug("Header -> tok_java_package tok_identifier");
217 if (g_parse_mode == PROGRAM) {
218 g_program->set_java_package($2);
219 }
220 }
221
222Include:
223 tok_include tok_literal
224 {
225 pdebug("Include -> tok_include tok_literal");
226 if (g_parse_mode == INCLUDES) {
227 std::string path = include_file(std::string($2));
228 if (!path.empty()) {
229 g_program->add_include(path);
230 }
231 }
232 }
Mark Slee31985722006-05-24 21:45:31 +0000233
234DefinitionList:
235 DefinitionList Definition
236 {
237 pdebug("DefinitionList -> DefinitionList Definition");
238 }
239|
240 {
241 pdebug("DefinitionList -> ");
242 }
243
244Definition:
Mark Slee30152872006-11-28 01:24:07 +0000245 Const
246 {
247 pdebug("Definition -> Const");
248 if (g_parse_mode == PROGRAM) {
249 g_program->add_const($1);
250 }
251 }
252| TypeDefinition
Mark Slee9cb7c612006-09-01 22:17:45 +0000253 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000254 pdebug("Definition -> TypeDefinition");
255 if (g_parse_mode == PROGRAM) {
256 g_scope->add_type($1->get_name(), $1);
257 if (g_parent_scope != NULL) {
258 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
259 }
260 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000261 }
Mark Slee31985722006-05-24 21:45:31 +0000262| Service
263 {
264 pdebug("Definition -> Service");
Mark Sleef0712dc2006-10-25 19:03:57 +0000265 if (g_parse_mode == PROGRAM) {
266 g_scope->add_service($1->get_name(), $1);
267 if (g_parent_scope != NULL) {
268 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
269 }
270 g_program->add_service($1);
271 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000272 }
273
Mark Sleef0712dc2006-10-25 19:03:57 +0000274TypeDefinition:
275 Typedef
Mark Slee9cb7c612006-09-01 22:17:45 +0000276 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000277 pdebug("TypeDefinition -> Typedef");
278 if (g_parse_mode == PROGRAM) {
279 g_program->add_typedef($1);
280 }
281 }
282| Enum
283 {
284 pdebug("TypeDefinition -> Enum");
285 if (g_parse_mode == PROGRAM) {
286 g_program->add_enum($1);
287 }
288 }
289| Struct
290 {
291 pdebug("TypeDefinition -> Struct");
292 if (g_parse_mode == PROGRAM) {
293 g_program->add_struct($1);
294 }
295 }
296| Xception
297 {
298 pdebug("TypeDefinition -> Xception");
299 if (g_parse_mode == PROGRAM) {
300 g_program->add_xception($1);
301 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000302 }
Mark Slee31985722006-05-24 21:45:31 +0000303
304Typedef:
305 tok_typedef DefinitionType tok_identifier
306 {
307 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000308 t_typedef *td = new t_typedef(g_program, $2, $3);
Mark Slee31985722006-05-24 21:45:31 +0000309 $$ = td;
310 }
311
312Enum:
313 tok_enum tok_identifier '{' EnumDefList '}'
314 {
315 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
316 $$ = $4;
317 $$->set_name($2);
318 }
319
Mark Slee207cb462006-11-02 18:43:12 +0000320CommaOrSemicolonOptional:
321 ','
322 {}
323| ';'
324 {}
325|
326 {}
327
Mark Slee31985722006-05-24 21:45:31 +0000328EnumDefList:
Mark Slee207cb462006-11-02 18:43:12 +0000329 EnumDefList EnumDef
Mark Slee31985722006-05-24 21:45:31 +0000330 {
331 pdebug("EnumDefList -> EnumDefList EnumDef");
332 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000333 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000334 }
335|
336 {
337 pdebug("EnumDefList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000338 $$ = new t_enum(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000339 }
340
341EnumDef:
Mark Slee207cb462006-11-02 18:43:12 +0000342 tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000343 {
Mark Slee30152872006-11-28 01:24:07 +0000344 pdebug("EnumDef -> tok_identifier = tok_int_constant");
Mark Slee31985722006-05-24 21:45:31 +0000345 if ($3 < 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000346 pwarning(1, "Negative value supplied for enum %s.\n", $1);
Mark Slee31985722006-05-24 21:45:31 +0000347 }
Mark Slee30152872006-11-28 01:24:07 +0000348 $$ = new t_enum_value($1, $3);
Mark Slee31985722006-05-24 21:45:31 +0000349 }
350|
Mark Slee04cc6052006-11-15 21:25:34 +0000351 tok_identifier CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000352 {
Mark Slee30152872006-11-28 01:24:07 +0000353 pdebug("EnumDef -> tok_identifier");
354 $$ = new t_enum_value($1);
355 }
356
357Const:
358 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
359 {
360 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
Mark Sleeaa7671d2006-11-29 03:19:31 +0000361 if (g_parse_mode == PROGRAM) {
362 $$ = new t_const($2, $3, $5);
363 validate_const_type($$);
364 } else {
365 $$ = NULL;
366 }
Mark Slee30152872006-11-28 01:24:07 +0000367 }
368
369ConstValue:
370 tok_int_constant
371 {
372 pdebug("ConstValue => tok_int_constant");
373 $$ = new t_const_value();
374 $$->set_integer($1);
375 }
376| tok_dub_constant
377 {
378 pdebug("ConstValue => tok_dub_constant");
379 $$ = new t_const_value();
380 $$->set_double($1);
381 }
382| tok_literal
383 {
384 pdebug("ConstValue => tok_literal");
385 $$ = new t_const_value();
386 $$->set_string($1);
387 }
Mark Slee67fc6342006-11-29 03:37:04 +0000388| tok_identifier
389 {
390 pdebug("ConstValue => tok_identifier");
391 $$ = new t_const_value();
392 $$->set_string($1);
393 }
Mark Slee30152872006-11-28 01:24:07 +0000394| ConstList
395 {
396 pdebug("ConstValue => ConstList");
397 $$ = $1;
398 }
399| ConstMap
400 {
401 pdebug("ConstValue => ConstMap");
402 $$ = $1;
403 }
404
405ConstList:
406 '[' ConstListContents ']'
407 {
408 pdebug("ConstList => [ ConstListContents ]");
409 $$ = $2;
410 }
411
412ConstListContents:
413 ConstListContents ConstValue CommaOrSemicolonOptional
414 {
415 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
416 $$ = $1;
417 $$->add_list($2);
418 }
419|
420 {
421 pdebug("ConstListContents =>");
422 $$ = new t_const_value();
423 $$->set_list();
424 }
425
426ConstMap:
427 '{' ConstMapContents '}'
428 {
429 pdebug("ConstMap => { ConstMapContents }");
430 $$ = $2;
431 }
432
433ConstMapContents:
434 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
435 {
436 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
437 $$ = $1;
438 $$->add_map($2, $4);
439 }
440|
441 {
442 pdebug("ConstMapContents =>");
443 $$ = new t_const_value();
444 $$->set_map();
Mark Slee31985722006-05-24 21:45:31 +0000445 }
446
447Struct:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000448 tok_struct tok_identifier XsdAll '{' FieldList '}'
Mark Slee31985722006-05-24 21:45:31 +0000449 {
450 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
Mark Slee782abbb2007-01-19 00:17:02 +0000451 $5->set_name($2);
452 $5->set_xsd_all($3);
453 $$ = $5;
Mark Slee9cb7c612006-09-01 22:17:45 +0000454 y_field_val = -1;
455 }
456
Mark Slee36bfa2e2007-01-19 20:09:51 +0000457XsdAll:
Mark Slee782abbb2007-01-19 00:17:02 +0000458 tok_xsd_all
459 {
460 $$ = true;
461 }
462|
463 {
464 $$ = false;
465 }
466
Mark Slee36bfa2e2007-01-19 20:09:51 +0000467XsdOptional:
468 tok_xsd_optional
469 {
470 $$ = true;
471 }
472|
473 {
474 $$ = false;
475 }
476
Mark Slee9cb7c612006-09-01 22:17:45 +0000477Xception:
478 tok_xception tok_identifier '{' FieldList '}'
479 {
480 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
481 $4->set_name($2);
482 $4->set_xception(true);
483 $$ = $4;
484 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000485 }
486
487Service:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000488 tok_service tok_identifier Extends '{' FunctionList '}'
Mark Slee31985722006-05-24 21:45:31 +0000489 {
490 pdebug("Service -> tok_service tok_identifier { FunctionList }");
Mark Sleef0712dc2006-10-25 19:03:57 +0000491 $$ = $5;
Mark Slee31985722006-05-24 21:45:31 +0000492 $$->set_name($2);
Mark Sleef0712dc2006-10-25 19:03:57 +0000493 $$->set_extends($3);
494 }
495
Mark Slee36bfa2e2007-01-19 20:09:51 +0000496Extends:
Mark Sleef0712dc2006-10-25 19:03:57 +0000497 tok_extends tok_identifier
498 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000499 pdebug("Extends -> tok_extends tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000500 $$ = NULL;
501 if (g_parse_mode == PROGRAM) {
502 $$ = g_scope->get_service($2);
503 if ($$ == NULL) {
504 yyerror("Service \"%s\" has not been defined.", $2);
505 exit(1);
506 }
507 }
508 }
509|
510 {
511 $$ = NULL;
Mark Slee31985722006-05-24 21:45:31 +0000512 }
513
514FunctionList:
Mark Slee207cb462006-11-02 18:43:12 +0000515 FunctionList Function
Mark Slee31985722006-05-24 21:45:31 +0000516 {
517 pdebug("FunctionList -> FunctionList Function");
518 $$ = $1;
519 $1->add_function($2);
520 }
521|
522 {
523 pdebug("FunctionList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000524 $$ = new t_service(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000525 }
526
527Function:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000528 Async FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000529 {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000530 $5->set_name(std::string($3) + "_args");
Mark Slee4e755ca2006-09-12 00:46:08 +0000531 $$ = new t_function($2, $3, $5, $7, $1);
Mark Slee9cb7c612006-09-01 22:17:45 +0000532 y_field_val = -1;
Mark Slee31985722006-05-24 21:45:31 +0000533 }
534
Mark Slee36bfa2e2007-01-19 20:09:51 +0000535Async:
Mark Slee52f643d2006-08-09 00:03:43 +0000536 tok_async
Mark Slee31985722006-05-24 21:45:31 +0000537 {
Mark Slee52f643d2006-08-09 00:03:43 +0000538 $$ = true;
539 }
540|
541 {
542 $$ = false;
Mark Slee31985722006-05-24 21:45:31 +0000543 }
544
Mark Slee36bfa2e2007-01-19 20:09:51 +0000545Throws:
Mark Slee9cb7c612006-09-01 22:17:45 +0000546 tok_throws '(' FieldList ')'
547 {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000548 pdebug("Throws -> tok_throws ( FieldList )");
Mark Slee9cb7c612006-09-01 22:17:45 +0000549 $$ = $3;
550 }
551|
552 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000553 $$ = new t_struct(g_program);
Mark Slee9cb7c612006-09-01 22:17:45 +0000554 }
555
Mark Slee31985722006-05-24 21:45:31 +0000556FieldList:
Mark Slee207cb462006-11-02 18:43:12 +0000557 FieldList Field
Mark Slee31985722006-05-24 21:45:31 +0000558 {
559 pdebug("FieldList -> FieldList , Field");
560 $$ = $1;
Mark Slee207cb462006-11-02 18:43:12 +0000561 $$->append($2);
Mark Slee31985722006-05-24 21:45:31 +0000562 }
563|
564 {
565 pdebug("FieldList -> ");
Mark Sleef0712dc2006-10-25 19:03:57 +0000566 $$ = new t_struct(g_program);
Mark Slee31985722006-05-24 21:45:31 +0000567 }
568
569Field:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000570 tok_int_constant ':' FieldType tok_identifier XsdOptional CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000571 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000572 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
573 if ($1 <= 0) {
574 pwarning(1, "Nonpositive value (%d) not allowed as a field key for '%s'.\n", $1, $4);
575 $1 = y_field_val--;
Mark Slee31985722006-05-24 21:45:31 +0000576 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000577 $$ = new t_field($3, $4, $1);
Mark Slee36bfa2e2007-01-19 20:09:51 +0000578 $$->set_xsd_optional($5);
Mark Slee31985722006-05-24 21:45:31 +0000579 }
Mark Slee36bfa2e2007-01-19 20:09:51 +0000580| FieldType tok_identifier XsdOptional CommaOrSemicolonOptional
Mark Slee31985722006-05-24 21:45:31 +0000581 {
Mark Sleee8540632006-05-30 09:24:40 +0000582 pdebug("Field -> FieldType tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000583 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 +0000584 $$ = new t_field($1, $2, y_field_val--);
Mark Slee36bfa2e2007-01-19 20:09:51 +0000585 $$->set_xsd_optional($3);
Mark Slee31985722006-05-24 21:45:31 +0000586 }
Mark Slee2329a832006-11-09 00:23:30 +0000587| FieldType tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
Mark Sleef0712dc2006-10-25 19:03:57 +0000588 {
Mark Slee2329a832006-11-09 00:23:30 +0000589 pwarning(1, "Trailing = id notation is deprecated. Use 'Id: Type Name' notation instead");
Mark Sleef0712dc2006-10-25 19:03:57 +0000590 pdebug("Field -> FieldType tok_identifier = tok_int_constant");
591 if ($4 <= 0) {
592 pwarning(1, "Nonpositive value (%d) not allowed as a field key for '%s'.\n", $4, $2);
593 $4 = y_field_val--;
594 }
595 $$ = new t_field($1, $2, $4);
596 }
Mark Slee31985722006-05-24 21:45:31 +0000597
598DefinitionType:
599 BaseType
600 {
601 pdebug("DefinitionType -> BaseType");
602 $$ = $1;
603 }
Mark Sleee8540632006-05-30 09:24:40 +0000604| ContainerType
605 {
606 pdebug("DefinitionType -> ContainerType");
607 $$ = $1;
608 }
Mark Slee31985722006-05-24 21:45:31 +0000609
610FunctionType:
611 FieldType
612 {
Mark Sleee8540632006-05-30 09:24:40 +0000613 pdebug("FunctionType -> FieldType");
Mark Slee31985722006-05-24 21:45:31 +0000614 $$ = $1;
615 }
616| tok_void
617 {
Mark Sleee8540632006-05-30 09:24:40 +0000618 pdebug("FunctionType -> tok_void");
Mark Sleef0712dc2006-10-25 19:03:57 +0000619 $$ = g_type_void;
Mark Slee31985722006-05-24 21:45:31 +0000620 }
621
622FieldType:
623 tok_identifier
624 {
Mark Sleee8540632006-05-30 09:24:40 +0000625 pdebug("FieldType -> tok_identifier");
Mark Sleef0712dc2006-10-25 19:03:57 +0000626 if (g_parse_mode == INCLUDES) {
627 // Ignore identifiers in include mode
628 $$ = NULL;
629 } else {
630 // Lookup the identifier in the current scope
631 $$ = g_scope->get_type($1);
632 if ($$ == NULL) {
633 yyerror("Type \"%s\" has not been defined.", $1);
634 exit(1);
635 }
Mark Sleee8540632006-05-30 09:24:40 +0000636 }
Mark Slee31985722006-05-24 21:45:31 +0000637 }
638| BaseType
639 {
Mark Sleee8540632006-05-30 09:24:40 +0000640 pdebug("FieldType -> BaseType");
641 $$ = $1;
642 }
643| ContainerType
644 {
645 pdebug("FieldType -> ContainerType");
Mark Slee31985722006-05-24 21:45:31 +0000646 $$ = $1;
647 }
648
649BaseType:
650 tok_string
651 {
652 pdebug("BaseType -> tok_string");
Mark Sleef0712dc2006-10-25 19:03:57 +0000653 $$ = g_type_string;
Mark Slee31985722006-05-24 21:45:31 +0000654 }
Mark Sleeb6200d82007-01-19 19:14:36 +0000655| tok_slist
656 {
657 pdebug("BaseType -> tok_slist");
658 $$ = g_type_slist;
659 }
Mark Slee78f58e22006-09-02 04:17:07 +0000660| tok_bool
661 {
662 pdebug("BaseType -> tok_bool");
Mark Sleef0712dc2006-10-25 19:03:57 +0000663 $$ = g_type_bool;
Mark Slee78f58e22006-09-02 04:17:07 +0000664 }
Mark Slee31985722006-05-24 21:45:31 +0000665| tok_byte
666 {
667 pdebug("BaseType -> tok_byte");
Mark Sleef0712dc2006-10-25 19:03:57 +0000668 $$ = g_type_byte;
Mark Slee31985722006-05-24 21:45:31 +0000669 }
Mark Slee9cb7c612006-09-01 22:17:45 +0000670| tok_i16
671 {
672 pdebug("BaseType -> tok_i16");
Mark Sleef0712dc2006-10-25 19:03:57 +0000673 $$ = g_type_i16;
Mark Slee9cb7c612006-09-01 22:17:45 +0000674 }
Mark Slee31985722006-05-24 21:45:31 +0000675| tok_i32
676 {
677 pdebug("BaseType -> tok_i32");
Mark Sleef0712dc2006-10-25 19:03:57 +0000678 $$ = g_type_i32;
Mark Slee31985722006-05-24 21:45:31 +0000679 }
Mark Slee31985722006-05-24 21:45:31 +0000680| tok_i64
681 {
682 pdebug("BaseType -> tok_i64");
Mark Sleef0712dc2006-10-25 19:03:57 +0000683 $$ = g_type_i64;
Mark Slee31985722006-05-24 21:45:31 +0000684 }
Mark Sleec98d0502006-09-06 02:42:25 +0000685| tok_double
686 {
687 pdebug("BaseType -> tok_double");
Mark Sleef0712dc2006-10-25 19:03:57 +0000688 $$ = g_type_double;
Mark Sleec98d0502006-09-06 02:42:25 +0000689 }
Mark Slee31985722006-05-24 21:45:31 +0000690
Mark Sleee8540632006-05-30 09:24:40 +0000691ContainerType:
692 MapType
693 {
694 pdebug("ContainerType -> MapType");
695 $$ = $1;
696 }
697| SetType
698 {
699 pdebug("ContainerType -> SetType");
700 $$ = $1;
701 }
702| ListType
703 {
704 pdebug("ContainerType -> ListType");
705 $$ = $1;
706 }
707
708MapType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000709 tok_map CppType '<' FieldType ',' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000710 {
711 pdebug("MapType -> tok_map <FieldType, FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000712 $$ = new t_map($4, $6);
713 if ($2 != NULL) {
714 ((t_container*)$$)->set_cpp_name(std::string($2));
715 }
Mark Sleee8540632006-05-30 09:24:40 +0000716 }
717
718SetType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000719 tok_set CppType '<' FieldType '>'
Mark Sleee8540632006-05-30 09:24:40 +0000720 {
721 pdebug("SetType -> tok_set<FieldType>");
Mark Slee4f8da1d2006-10-12 02:47:27 +0000722 $$ = new t_set($4);
723 if ($2 != NULL) {
724 ((t_container*)$$)->set_cpp_name(std::string($2));
725 }
Mark Sleee8540632006-05-30 09:24:40 +0000726 }
727
728ListType:
Mark Slee36bfa2e2007-01-19 20:09:51 +0000729 tok_list '<' FieldType '>' CppType
Mark Sleee8540632006-05-30 09:24:40 +0000730 {
731 pdebug("ListType -> tok_list<FieldType>");
Mark Sleef0712dc2006-10-25 19:03:57 +0000732 $$ = new t_list($3);
733 if ($5 != NULL) {
734 ((t_container*)$$)->set_cpp_name(std::string($5));
Mark Slee4f8da1d2006-10-12 02:47:27 +0000735 }
736 }
737
Mark Slee36bfa2e2007-01-19 20:09:51 +0000738CppType:
Mark Sleef0712dc2006-10-25 19:03:57 +0000739 '[' tok_cpp_type tok_literal ']'
Mark Slee4f8da1d2006-10-12 02:47:27 +0000740 {
Mark Sleef0712dc2006-10-25 19:03:57 +0000741 $$ = $3;
Mark Slee4f8da1d2006-10-12 02:47:27 +0000742 }
743|
744 {
745 $$ = NULL;
Mark Sleee8540632006-05-30 09:24:40 +0000746 }
747
Mark Slee31985722006-05-24 21:45:31 +0000748%%