THRIFT-409. java: Add "union" to Thrift

This patch introduces new IDL syntax for creating Unions, explicityly single-valued structs. While the parser changes are portable, this patch only includes the actual generated code changes for the Java library. Other libraries can continue to generate a struct with the same fields and remain compatible until they are able to implement the full shebang.




git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@810300 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/thrifty.yy b/compiler/cpp/src/thrifty.yy
index 026e5c3..8f6e167 100644
--- a/compiler/cpp/src/thrifty.yy
+++ b/compiler/cpp/src/thrifty.yy
@@ -42,6 +42,8 @@
  */
 int y_field_val = -1;
 int g_arglist = 0;
+const int struct_is_struct = 0;
+const int struct_is_union = 1;
 
 %}
 
@@ -148,6 +150,7 @@
 %token tok_const
 %token tok_required
 %token tok_optional
+%token tok_union
 
 /**
  * Grammar nodes
@@ -193,6 +196,7 @@
 %type<tconstv>   ConstMap
 %type<tconstv>   ConstMapContents
 
+%type<iconst>    StructHead
 %type<tstruct>   Struct
 %type<tstruct>   Xception
 %type<tservice>  Service
@@ -679,11 +683,22 @@
       $$->set_map();
     }
 
+StructHead:
+  tok_struct
+    {
+      $$ = struct_is_struct;
+    }
+| tok_union
+    {
+      $$ = struct_is_union;
+    }
+
 Struct:
-  tok_struct tok_identifier XsdAll '{' FieldList '}' TypeAnnotations
+  StructHead tok_identifier XsdAll '{' FieldList '}' TypeAnnotations
     {
       pdebug("Struct -> tok_struct tok_identifier { FieldList }");
       $5->set_xsd_all($3);
+      $5->set_union($1 == struct_is_union);
       $$ = $5;
       $$->set_name($2);
       if ($7 != NULL) {
@@ -691,7 +706,7 @@
         delete $7;
       }
     }
-
+    
 XsdAll:
   tok_xsd_all
     {
@@ -1137,4 +1152,4 @@
       $$->val = $3;
     }
 
-%%
+%%
\ No newline at end of file