From a2309996679350a249beeac34efd65ff703f5b23 Mon Sep 17 00:00:00 2001 From: David Reiss Date: Wed, 10 Dec 2008 01:52:48 +0000 Subject: [PATCH] THRIFT-121. Support arbitrary type annotations Adds syntax for attaching arbitrary key/value pairs to types. These annotations can be accessed by individual generators to alter the code they produce. This version supports annotations on container types and structures. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@724954 13f79535-47bb-0310-9956-ffa450edef68 --- compiler/cpp/src/parse/t_type.h | 12 ++++++ compiler/cpp/src/thrifty.yy | 66 ++++++++++++++++++++++++++++++--- test/AnnotationTest.thrift | 12 ++++++ 3 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 test/AnnotationTest.thrift diff --git a/compiler/cpp/src/parse/t_type.h b/compiler/cpp/src/parse/t_type.h index 09e386f7..6f178bd7 100644 --- a/compiler/cpp/src/parse/t_type.h +++ b/compiler/cpp/src/parse/t_type.h @@ -8,6 +8,7 @@ #define T_TYPE_H #include +#include #include #include "t_doc.h" @@ -114,6 +115,7 @@ class t_type : public t_doc { return rv; } + std::map annotations_; protected: t_type() : @@ -148,4 +150,14 @@ class t_type : public t_doc { uint8_t fingerprint_[fingerprint_len]; }; + +/** + * Placeholder struct for returning the key and value of an annotation + * during parsing. + */ +struct t_annotation { + std::string key; + std::string val; +}; + #endif diff --git a/compiler/cpp/src/thrifty.yy b/compiler/cpp/src/thrifty.yy index e5f1736a..f27e9ab9 100644 --- a/compiler/cpp/src/thrifty.yy +++ b/compiler/cpp/src/thrifty.yy @@ -56,6 +56,7 @@ int g_arglist = 0; t_field* tfield; char* dtext; t_field::e_req ereq; + t_annotation* tannot; } /** @@ -142,6 +143,7 @@ int g_arglist = 0; %type BaseType %type ContainerType +%type SimpleContainerType %type MapType %type SetType %type ListType @@ -152,6 +154,10 @@ int g_arglist = 0; %type Typedef %type DefinitionType +%type TypeAnnotations +%type TypeAnnotationList +%type TypeAnnotation + %type Field %type FieldIdentifier %type FieldRequiredness @@ -658,12 +664,16 @@ ConstMapContents: } Struct: - tok_struct tok_identifier XsdAll '{' FieldList '}' + tok_struct tok_identifier XsdAll '{' FieldList '}' TypeAnnotations { pdebug("Struct -> tok_struct tok_identifier { FieldList }"); - $5->set_name($2); $5->set_xsd_all($3); $$ = $5; + $$->set_name($2); + if ($7 != NULL) { + $$->annotations_ = $7->annotations_; + delete $7; + } y_field_val = -1; } @@ -997,20 +1007,30 @@ BaseType: $$ = g_type_double; } -ContainerType: +ContainerType: SimpleContainerType TypeAnnotations + { + pdebug("ContainerType -> SimpleContainerType TypeAnnotations"); + $$ = $1; + if ($2 != NULL) { + $$->annotations_ = $2->annotations_; + delete $2; + } + } + +SimpleContainerType: MapType { - pdebug("ContainerType -> MapType"); + pdebug("SimpleContainerType -> MapType"); $$ = $1; } | SetType { - pdebug("ContainerType -> SetType"); + pdebug("SimpleContainerType -> SetType"); $$ = $1; } | ListType { - pdebug("ContainerType -> ListType"); + pdebug("SimpleContainerType -> ListType"); $$ = $1; } @@ -1054,4 +1074,38 @@ CppType: $$ = NULL; } +TypeAnnotations: + '(' TypeAnnotationList ')' + { + pdebug("TypeAnnotations -> ( TypeAnnotationList )"); + $$ = $2; + } +| + { + $$ = NULL; + } + +TypeAnnotationList: + TypeAnnotationList TypeAnnotation + { + pdebug("TypeAnnotationList -> TypeAnnotationList , TypeAnnotation"); + $$ = $1; + $$->annotations_[$2->key] = $2->val; + delete $2; + } +| + { + /* Just use a dummy structure to hold the annotations. */ + $$ = new t_struct(g_program); + } + +TypeAnnotation: + tok_identifier '=' tok_literal CommaOrSemicolonOptional + { + pdebug("TypeAnnotation -> tok_identifier = tok_literal"); + $$ = new t_annotation; + $$->key = $1; + $$->val = $3; + } + %% diff --git a/test/AnnotationTest.thrift b/test/AnnotationTest.thrift new file mode 100644 index 00000000..64e8d82e --- /dev/null +++ b/test/AnnotationTest.thrift @@ -0,0 +1,12 @@ +typedef list ( cpp.template = "std::list" ) int_linked_list + +struct foo { + 1: i32 bar; + 2: i32 baz; + 3: i32 qux; + 4: i32 bop; +} ( + cpp.type = "DenseFoo", + python.type = "DenseFoo", + java.final = "", +) -- 2.17.1