From: Mark Slee Date: Wed, 7 Feb 2007 02:40:59 +0000 (+0000) Subject: Thrift supports the "senum" type now X-Git-Tag: 0.2.0~1485 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=6a47fed5feef44de4c5505fff21126f09ec02c6e;p=common%2Fthrift.git Thrift supports the "senum" type now Summary: Enumerated lists of strings, enforced only in XSD Reviewed By: xsd git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664987 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/compiler/cpp/src/generate/t_xsd_generator.cc b/compiler/cpp/src/generate/t_xsd_generator.cc index 4c31d6c1..fb39753c 100644 --- a/compiler/cpp/src/generate/t_xsd_generator.cc +++ b/compiler/cpp/src/generate/t_xsd_generator.cc @@ -26,8 +26,23 @@ void t_xsd_generator::generate_typedef(t_typedef* ttypedef) { indent(s_xsd_types_) << "get_name() << "\">" << endl; indent_up(); - indent(s_xsd_types_) << - "get_type()) << "\" />" << endl; + if (ttypedef->get_type()->is_string() && ((t_base_type*)ttypedef->get_type())->is_string_enum()) { + indent(s_xsd_types_) << + "get_type()) << "\">" << endl; + indent_up(); + const vector& values = ((t_base_type*)ttypedef->get_type())->get_string_enum_vals(); + vector::const_iterator v_iter; + for (v_iter = values.begin(); v_iter != values.end(); ++v_iter) { + indent(s_xsd_types_) << + "" << endl; + } + indent_down(); + indent(s_xsd_types_) << + "" << endl; + } else { + indent(s_xsd_types_) << + "get_type()) << "\" />" << endl; + } indent_down(); indent(s_xsd_types_) << "" << endl << endl; diff --git a/compiler/cpp/src/parse/t_base_type.h b/compiler/cpp/src/parse/t_base_type.h index b0d41d60..c9548bb6 100644 --- a/compiler/cpp/src/parse/t_base_type.h +++ b/compiler/cpp/src/parse/t_base_type.h @@ -28,7 +28,8 @@ class t_base_type : public t_type { t_base_type(std::string name, t_base base) : t_type(name), base_(base), - string_list_(false) {} + string_list_(false), + string_enum_(false) {} t_base get_base() const { return base_; @@ -50,13 +51,32 @@ class t_base_type : public t_type { return base_ == TYPE_STRING && string_list_; } + void set_string_enum(bool val) { + string_enum_ = true; + } + + bool is_string_enum() const { + return base_ == TYPE_STRING && string_enum_; + } + + void add_string_enum_val(std::string val) { + string_enum_vals_.push_back(val); + } + + const std::vector& get_string_enum_vals() const { + return string_enum_vals_; + } + bool is_base_type() const { return true; } private: t_base base_; + bool string_list_; + bool string_enum_; + std::vector string_enum_vals_; }; #endif diff --git a/compiler/cpp/src/thriftl.ll b/compiler/cpp/src/thriftl.ll index 593df107..abda14f2 100644 --- a/compiler/cpp/src/thriftl.ll +++ b/compiler/cpp/src/thriftl.ll @@ -77,6 +77,7 @@ sliteral ("'"[^']*"'") "double" { return tok_double; } "string" { return tok_string; } "slist" { return tok_slist; } +"senum" { return tok_senum; } "map" { return tok_map; } "list" { return tok_list; } "set" { return tok_set; } diff --git a/compiler/cpp/src/thrifty.yy b/compiler/cpp/src/thrifty.yy index 19f1ba8e..519f20d0 100644 --- a/compiler/cpp/src/thrifty.yy +++ b/compiler/cpp/src/thrifty.yy @@ -33,6 +33,7 @@ int y_field_val = -1; double dconst; bool tbool; t_type* ttype; + t_base_type* tbase; t_typedef* ttypedef; t_enum* tenum; t_enum_value* tenumv; @@ -82,6 +83,7 @@ int y_field_val = -1; %token tok_byte %token tok_string %token tok_slist +%token tok_senum %token tok_i16 %token tok_i32 %token tok_i64 @@ -136,6 +138,10 @@ int y_field_val = -1; %type EnumDefList %type EnumDef +%type Senum +%type SenumDefList +%type SenumDef + %type Const %type ConstValue %type ConstList @@ -304,6 +310,13 @@ TypeDefinition: g_program->add_enum($1); } } +| Senum + { + pdebug("TypeDefinition -> Senum"); + if (g_parse_mode == PROGRAM) { + g_program->add_typedef($1); + } + } | Struct { pdebug("TypeDefinition -> Struct"); @@ -341,6 +354,13 @@ DocTextOptional: $$ = NULL; } +CommaOrSemicolonOptional: + ',' + {} +| ';' + {} +| + {} Enum: DocTextOptional tok_enum tok_identifier '{' EnumDefList '}' @@ -353,14 +373,6 @@ Enum: } } -CommaOrSemicolonOptional: - ',' - {} -| ';' - {} -| - {} - EnumDefList: EnumDefList EnumDef { @@ -396,6 +408,37 @@ EnumDef: } } +Senum: + DocTextOptional tok_senum tok_identifier '{' SenumDefList '}' + { + pdebug("Senum -> tok_senum tok_identifier { SenumDefList }"); + $$ = new t_typedef(g_program, $5, $3); + if ($1 != NULL) { + $$->set_doc($1); + } + } + +SenumDefList: + SenumDefList SenumDef + { + pdebug("SenumDefList -> SenumDefList SenumDef"); + $$ = $1; + $$->add_string_enum_val($2); + } +| + { + pdebug("SenumDefList -> "); + $$ = new t_base_type("string", t_base_type::TYPE_STRING); + $$->set_string_enum(true); + } + +SenumDef: + tok_literal CommaOrSemicolonOptional + { + pdebug("SenumDef -> tok_literal"); + $$ = $1; + } + Const: tok_const FieldType tok_identifier '=' ConstValue CommaOrSemicolonOptional {