2 // Copyright (c) 2006- Facebook
3 // Distributed under the Thrift Software License
5 // See accompanying file LICENSE or visit the Thrift site at:
6 // http://developers.facebook.com/thrift/
11 * This parser is used on a thrift definition file.
13 * @author Mark Slee <mcslee@facebook.com>
19 #include "parse/t_program.h"
20 #include "parse/t_scope.h"
23 * This global variable is used for automatic numbering of field indices etc.
24 * when parsing the members of a struct. Field values are automatically
25 * assigned starting from -1 and working their way down.
33 * This structure is used by the parser to hold the data types associated with
34 * various parse nodes.
48 t_const_value* tconstv;
51 t_function* tfunction;
60 %token<id> tok_identifier
61 %token<id> tok_literal
62 %token<dtext> tok_doctext
63 %token<id> tok_st_identifier
68 %token<iconst> tok_int_constant
69 %token<dconst> tok_dub_constant
76 %token tok_cpp_namespace
77 %token tok_cpp_include
79 %token tok_php_namespace
81 %token tok_perl_package
82 %token tok_java_package
84 %token tok_xsd_optional
85 %token tok_xsd_nillable
86 %token tok_xsd_namespace
88 %token tok_ruby_namespace
89 %token tok_smalltalk_category
90 %token tok_smalltalk_prefix
91 %token tok_cocoa_prefix
94 * Base datatype keywords
109 * Complex type keywords
121 * Thrift language keywords
138 %type<ttype> BaseType
139 %type<ttype> ContainerType
142 %type<ttype> ListType
144 %type<tdoc> Definition
145 %type<ttype> TypeDefinition
147 %type<ttypedef> Typedef
148 %type<ttype> DefinitionType
151 %type<iconst> FieldIdentifier
152 %type<ereq> FieldRequiredness
153 %type<ttype> FieldType
154 %type<tconstv> FieldValue
155 %type<tstruct> FieldList
158 %type<tenum> EnumDefList
159 %type<tenumv> EnumDef
161 %type<ttypedef> Senum
162 %type<tbase> SenumDefList
166 %type<tconstv> ConstValue
167 %type<tconstv> ConstList
168 %type<tconstv> ConstListContents
169 %type<tconstv> ConstMap
170 %type<tconstv> ConstMapContents
172 %type<tstruct> Struct
173 %type<tstruct> Xception
174 %type<tservice> Service
176 %type<tfunction> Function
177 %type<ttype> FunctionType
178 %type<tservice> FunctionList
180 %type<tstruct> Throws
181 %type<tservice> Extends
184 %type<tbool> XsdOptional
185 %type<tbool> XsdNillable
186 %type<tstruct> XsdAttributes
189 %type<dtext> CaptureDocText
194 * Thrift Grammar Implementation.
196 * For the most part this source file works its way top down from what you
197 * might expect to find in a typical .thrift file, i.e. type definitions and
198 * namespaces up top followed by service definitions using those types.
202 HeaderList DefinitionList
204 pdebug("Program -> Headers DefinitionList");
206 TODO(dreiss): Decide whether full-program doctext is worth the trouble.
208 g_program->set_doc($1);
216 if (g_parse_mode == PROGRAM) {
224 /* TODO(dreiss): Try to DestroyDocText in all sorts or random places. */
227 if (g_parse_mode == PROGRAM) {
232 /* We have to DestroyDocText here, otherwise it catches the doctext
233 on the first real element. */
235 HeaderList DestroyDocText Header
237 pdebug("HeaderList -> HeaderList Header");
241 pdebug("HeaderList -> ");
247 pdebug("Header -> Include");
249 | tok_namespace tok_identifier
251 pwarning(1, "'namespace' is deprecated. Use 'cpp_namespace' and/or 'java_package' instead");
252 if (g_parse_mode == PROGRAM) {
253 g_program->set_cpp_namespace($2);
254 g_program->set_java_package($2);
257 | tok_cpp_namespace tok_identifier
259 pdebug("Header -> tok_cpp_namespace tok_identifier");
260 if (g_parse_mode == PROGRAM) {
261 g_program->set_cpp_namespace($2);
264 | tok_cpp_include tok_literal
266 pdebug("Header -> tok_cpp_include tok_literal");
267 if (g_parse_mode == PROGRAM) {
268 g_program->add_cpp_include($2);
271 | tok_php_namespace tok_identifier
273 pdebug("Header -> tok_php_namespace tok_identifier");
274 if (g_parse_mode == PROGRAM) {
275 g_program->set_php_namespace($2);
278 | tok_py_module tok_identifier
280 pdebug("Header -> tok_py_module tok_identifier");
281 if (g_parse_mode == PROGRAM) {
282 g_program->set_py_module($2);
285 | tok_perl_package tok_identifier
287 pdebug("Header -> tok_perl_namespace tok_identifier");
288 if (g_parse_mode == PROGRAM) {
289 g_program->set_perl_package($2);
292 | tok_ruby_namespace tok_identifier
294 pdebug("Header -> tok_ruby_namespace tok_identifier");
295 if (g_parse_mode == PROGRAM) {
296 g_program->set_ruby_namespace($2);
299 | tok_smalltalk_category tok_st_identifier
301 pdebug("Header -> tok_smalltalk_category tok_st_identifier");
302 if (g_parse_mode == PROGRAM) {
303 g_program->set_smalltalk_category($2);
306 | tok_smalltalk_prefix tok_identifier
308 pdebug("Header -> tok_smalltalk_prefix tok_identifier");
309 if (g_parse_mode == PROGRAM) {
310 g_program->set_smalltalk_prefix($2);
313 | tok_java_package tok_identifier
315 pdebug("Header -> tok_java_package tok_identifier");
316 if (g_parse_mode == PROGRAM) {
317 g_program->set_java_package($2);
320 | tok_cocoa_prefix tok_identifier
322 pdebug("Header -> tok_cocoa_prefix tok_identifier");
323 if (g_parse_mode == PROGRAM) {
324 g_program->set_cocoa_prefix($2);
327 | tok_xsd_namespace tok_literal
329 pdebug("Header -> tok_xsd_namespace tok_literal");
330 if (g_parse_mode == PROGRAM) {
331 g_program->set_xsd_namespace($2);
336 tok_include tok_literal
338 pdebug("Include -> tok_include tok_literal");
339 if (g_parse_mode == INCLUDES) {
340 std::string path = include_file(std::string($2));
342 g_program->add_include(path);
348 DefinitionList CaptureDocText Definition
350 pdebug("DefinitionList -> DefinitionList Definition");
351 if ($2 != NULL && $3 != NULL) {
357 pdebug("DefinitionList -> ");
363 pdebug("Definition -> Const");
364 if (g_parse_mode == PROGRAM) {
365 g_program->add_const($1);
371 pdebug("Definition -> TypeDefinition");
372 if (g_parse_mode == PROGRAM) {
373 g_scope->add_type($1->get_name(), $1);
374 if (g_parent_scope != NULL) {
375 g_parent_scope->add_type(g_parent_prefix + $1->get_name(), $1);
382 pdebug("Definition -> Service");
383 if (g_parse_mode == PROGRAM) {
384 g_scope->add_service($1->get_name(), $1);
385 if (g_parent_scope != NULL) {
386 g_parent_scope->add_service(g_parent_prefix + $1->get_name(), $1);
388 g_program->add_service($1);
396 pdebug("TypeDefinition -> Typedef");
397 if (g_parse_mode == PROGRAM) {
398 g_program->add_typedef($1);
403 pdebug("TypeDefinition -> Enum");
404 if (g_parse_mode == PROGRAM) {
405 g_program->add_enum($1);
410 pdebug("TypeDefinition -> Senum");
411 if (g_parse_mode == PROGRAM) {
412 g_program->add_typedef($1);
417 pdebug("TypeDefinition -> Struct");
418 if (g_parse_mode == PROGRAM) {
419 g_program->add_struct($1);
424 pdebug("TypeDefinition -> Xception");
425 if (g_parse_mode == PROGRAM) {
426 g_program->add_xception($1);
431 tok_typedef DefinitionType tok_identifier
433 pdebug("TypeDef -> tok_typedef DefinitionType tok_identifier");
434 t_typedef *td = new t_typedef(g_program, $2, $3);
438 CommaOrSemicolonOptional:
447 tok_enum tok_identifier '{' EnumDefList '}'
449 pdebug("Enum -> tok_enum tok_identifier { EnumDefList }");
457 pdebug("EnumDefList -> EnumDefList EnumDef");
463 pdebug("EnumDefList -> ");
464 $$ = new t_enum(g_program);
468 CaptureDocText tok_identifier '=' tok_int_constant CommaOrSemicolonOptional
470 pdebug("EnumDef -> tok_identifier = tok_int_constant");
472 pwarning(1, "Negative value supplied for enum %s.\n", $2);
474 $$ = new t_enum_value($2, $4);
478 if (g_parse_mode == PROGRAM) {
479 g_scope->add_constant($2, new t_const(g_type_i32, $2, new t_const_value($4)));
480 if (g_parent_scope != NULL) {
481 g_parent_scope->add_constant(g_parent_prefix + $2, new t_const(g_type_i32, $2, new t_const_value($4)));
486 CaptureDocText tok_identifier CommaOrSemicolonOptional
488 pdebug("EnumDef -> tok_identifier");
489 $$ = new t_enum_value($2);
496 tok_senum tok_identifier '{' SenumDefList '}'
498 pdebug("Senum -> tok_senum tok_identifier { SenumDefList }");
499 $$ = new t_typedef(g_program, $4, $2);
503 SenumDefList SenumDef
505 pdebug("SenumDefList -> SenumDefList SenumDef");
507 $$->add_string_enum_val($2);
511 pdebug("SenumDefList -> ");
512 $$ = new t_base_type("string", t_base_type::TYPE_STRING);
513 $$->set_string_enum(true);
517 tok_literal CommaOrSemicolonOptional
519 pdebug("SenumDef -> tok_literal");
524 tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional
526 pdebug("Const -> tok_const FieldType tok_identifier = ConstValue");
527 if (g_parse_mode == PROGRAM) {
528 $$ = new t_const($2, $3, $5);
529 validate_const_type($$);
531 g_scope->add_constant($3, $$);
532 if (g_parent_scope != NULL) {
533 g_parent_scope->add_constant(g_parent_prefix + $3, $$);
544 pdebug("ConstValue => tok_int_constant");
545 $$ = new t_const_value();
550 pdebug("ConstValue => tok_dub_constant");
551 $$ = new t_const_value();
556 pdebug("ConstValue => tok_literal");
557 $$ = new t_const_value($1);
561 pdebug("ConstValue => tok_identifier");
562 t_const* constant = g_scope->get_constant($1);
563 if (constant != NULL) {
564 $$ = constant->get_value();
566 if (g_parse_mode == PROGRAM) {
567 pwarning(1, "Constant strings should be quoted: %s\n", $1);
569 $$ = new t_const_value($1);
574 pdebug("ConstValue => ConstList");
579 pdebug("ConstValue => ConstMap");
584 '[' ConstListContents ']'
586 pdebug("ConstList => [ ConstListContents ]");
591 ConstListContents ConstValue CommaOrSemicolonOptional
593 pdebug("ConstListContents => ConstListContents ConstValue CommaOrSemicolonOptional");
599 pdebug("ConstListContents =>");
600 $$ = new t_const_value();
605 '{' ConstMapContents '}'
607 pdebug("ConstMap => { ConstMapContents }");
612 ConstMapContents ConstValue ':' ConstValue CommaOrSemicolonOptional
614 pdebug("ConstMapContents => ConstMapContents ConstValue CommaOrSemicolonOptional");
620 pdebug("ConstMapContents =>");
621 $$ = new t_const_value();
626 tok_struct tok_identifier XsdAll '{' FieldList '}'
628 pdebug("Struct -> tok_struct tok_identifier { FieldList }");
666 tok_xsd_attrs '{' FieldList '}'
676 tok_xception tok_identifier '{' FieldList '}'
678 pdebug("Xception -> tok_xception tok_identifier { FieldList }");
680 $4->set_xception(true);
686 tok_service tok_identifier Extends '{' FlagArgs FunctionList UnflagArgs '}'
688 pdebug("Service -> tok_service tok_identifier { FunctionList }");
705 tok_extends tok_identifier
707 pdebug("Extends -> tok_extends tok_identifier");
709 if (g_parse_mode == PROGRAM) {
710 $$ = g_scope->get_service($2);
712 yyerror("Service \"%s\" has not been defined.", $2);
723 FunctionList Function
725 pdebug("FunctionList -> FunctionList Function");
727 $1->add_function($2);
731 pdebug("FunctionList -> ");
732 $$ = new t_service(g_program);
736 CaptureDocText Async FunctionType tok_identifier '(' FieldList ')' Throws CommaOrSemicolonOptional
738 $6->set_name(std::string($4) + "_args");
739 $$ = new t_function($3, $4, $6, $8, $2);
757 tok_throws '(' FieldList ')'
759 pdebug("Throws -> tok_throws ( FieldList )");
764 $$ = new t_struct(g_program);
770 pdebug("FieldList -> FieldList , Field");
772 if (!($$->validate_field($2))) {
773 yyerror("Field identifier %d for \"%s\" has already been used", $2->get_key(), $2->get_name().c_str());
780 pdebug("FieldList -> ");
781 $$ = new t_struct(g_program);
785 CaptureDocText FieldIdentifier FieldRequiredness FieldType tok_identifier FieldValue XsdOptional XsdNillable XsdAttributes CommaOrSemicolonOptional
787 pdebug("tok_int_constant : Field -> FieldType tok_identifier");
789 pwarning(2, "No field key specified for %s, resulting protocol may have conflicts or not be backwards compatible!\n", $5);
791 $$ = new t_field($4, $5, $2);
794 validate_field_value($$, $6);
797 $$->set_xsd_optional($7);
798 $$->set_xsd_nillable($8);
803 $$->set_xsd_attrs($9);
811 pwarning(1, "Nonpositive value (%d) not allowed as a field key.\n", $1);
825 if (g_parse_mode == PROGRAM) {
826 pwarning(1, "required keyword is ignored in argument lists.\n");
828 $$ = t_field::OPT_IN_REQ_OUT;
830 $$ = t_field::REQUIRED;
836 if (g_parse_mode == PROGRAM) {
837 pwarning(1, "optional keyword is ignored in argument lists.\n");
839 $$ = t_field::OPT_IN_REQ_OUT;
841 $$ = t_field::OPTIONAL;
846 $$ = t_field::OPT_IN_REQ_OUT;
852 if (g_parse_mode == PROGRAM) {
866 pdebug("DefinitionType -> BaseType");
871 pdebug("DefinitionType -> ContainerType");
878 pdebug("FunctionType -> FieldType");
883 pdebug("FunctionType -> tok_void");
890 pdebug("FieldType -> tok_identifier");
891 if (g_parse_mode == INCLUDES) {
892 // Ignore identifiers in include mode
895 // Lookup the identifier in the current scope
896 $$ = g_scope->get_type($1);
898 yyerror("Type \"%s\" has not been defined.", $1);
905 pdebug("FieldType -> BaseType");
910 pdebug("FieldType -> ContainerType");
917 pdebug("BaseType -> tok_string");
922 pdebug("BaseType -> tok_binary");
927 pdebug("BaseType -> tok_slist");
932 pdebug("BaseType -> tok_bool");
937 pdebug("BaseType -> tok_byte");
942 pdebug("BaseType -> tok_i16");
947 pdebug("BaseType -> tok_i32");
952 pdebug("BaseType -> tok_i64");
957 pdebug("BaseType -> tok_double");
964 pdebug("ContainerType -> MapType");
969 pdebug("ContainerType -> SetType");
974 pdebug("ContainerType -> ListType");
979 tok_map CppType '<' FieldType ',' FieldType '>'
981 pdebug("MapType -> tok_map <FieldType, FieldType>");
982 $$ = new t_map($4, $6);
984 ((t_container*)$$)->set_cpp_name(std::string($2));
989 tok_set CppType '<' FieldType '>'
991 pdebug("SetType -> tok_set<FieldType>");
994 ((t_container*)$$)->set_cpp_name(std::string($2));
999 tok_list '<' FieldType '>' CppType
1001 pdebug("ListType -> tok_list<FieldType>");
1002 $$ = new t_list($3);
1004 ((t_container*)$$)->set_cpp_name(std::string($5));
1009 tok_cpp_type tok_literal