[mono-vb] Patch for array declarations

Bernie Solomon bernard@ugs.com
Tue, 17 Aug 2004 14:54:35 -0700


This is a multi-part message in MIME format.

------=_NextPart_000_00E5_01C4846A.1CCEC810
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

I attach a patch to the parser which copes with more variations of array
declarations where types are of the form "As Integer()" and related forms.
This actually hits quite a lot of places. For me this does not change
existing positive & negative test behaviour - I also attach a small test
which failed before and works now (I will turn this into a real test as
well).

Is this OK to commit?

Thanks

Bernie Solomon

------=_NextPart_000_00E5_01C4846A.1CCEC810
Content-Type: text/plain;
	name="t.vb"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="t.vb"

Imports System=0A=
=0A=
Class Test=0A=
    ReadOnly Property P As Integer()=0A=
        Get=0A=
            P =3D New Integer(2) {0,0,0}=0A=
        End Get=0A=
    End Property    =0A=
    ReadOnly Property P2 As Integer()(,)=0A=
        Get=0A=
            Dim A1(,) As Integer =3D {{1,2},{3,4},{5,6},{7,8}}=0A=
            Dim A2(3,1) As Integer=0A=
            A2(0,0)=3D11=0A=
            A2(0,1)=3D12=0A=
            A2(1,0)=3D13=0A=
            A2(1,1)=3D14=0A=
            A2(2,0)=3D15=0A=
            A2(2,1)=3D16=0A=
            A2(3,0)=3D17=0A=
            A2(3,1)=3D18=0A=
            P2 =3D New Integer(1)(,) { A1, A2 }=0A=
        End Get=0A=
    End Property    =0A=
    Function F() As Integer()=0A=
        F =3D P=0A=
    End Function=0A=
    Function F2() As Integer()(,)=0A=
        F2 =3D P2=0A=
    End Function=0A=
    Function F3(a As Integer()(,)) As Integer()(,)=0A=
        F3 =3D a=0A=
    End Function=0A=
    Sub S1A(a As Integer())=0A=
    End Sub=0A=
    Sub S1B(a() As Integer)=0A=
        S1A(a)=0A=
    End Sub=0A=
    Sub S2A(a As Integer()(,))=0A=
        If a.GetType().ToString() <> "System.Int32[,][]" Then=0A=
            Throw New Exception("#A1: type of array wrong")=0A=
        End If=0A=
        If a.Length <> 2 Then=0A=
            Throw New Exception("#A2: length of array wrong")=0A=
        End If=0A=
        If a.Rank <> 1 Then=0A=
            Throw New Exception("#A3: rank of array wrong")=0A=
        End If=0A=
'        For Each a As Integer(,) In ii=0A=
        Dim elem As Integer(,)=0A=
        For Each elem In a=0A=
            If elem.Length <> 8 Then=0A=
                Throw New Exception("#A4: length of array wrong")=0A=
            End If=0A=
            If elem.Rank <> 2 Then=0A=
                Throw New Exception("#A5: rank of array wrong")=0A=
            End If=0A=
            Console.WriteLine(elem.GetLength(0))=0A=
            Console.WriteLine(elem.GetLength(1))=0A=
'            For Each b As Integer In elem=0A=
            Dim b As Integer=0A=
            For Each b In elem=0A=
                Console.Write(b)=0A=
                Console.Write(" ")=0A=
            Next=0A=
            Console.WriteLine()=0A=
        Next=0A=
    End Sub=0A=
    Sub S2B(a()(,) As Integer)=0A=
        S2A(a)=0A=
    End Sub=0A=
    Shared Public Sub Main=0A=
        Dim t As New Test =0A=
        Dim i As Integer()=0A=
        Dim ai(3) As Integer=0A=
        Dim ii As Integer()(,)=0A=
        Dim jj()(,) As Integer=0A=
        t.S1A(t.P)=0A=
        t.S1B(t.P)=0A=
        t.S1A(t.F())=0A=
        t.S1B(t.F())=0A=
        t.S1B(t.F)=0A=
        t.S2B(t.P2)=0A=
        ii =3D t.P2=0A=
        jj =3D t.P2=0A=
    End Sub=0A=
End Class=0A=

------=_NextPart_000_00E5_01C4846A.1CCEC810
Content-Type: text/plain;
	name="mb-parser.diffs.txt"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="mb-parser.diffs.txt"

Index: mb-parser.jay=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: /cvs/public/mcs/mbas/mb-parser.jay,v=0A=
retrieving revision 1.129=0A=
diff -u -p -r1.129 mb-parser.jay=0A=
--- mb-parser.jay	17 Aug 2004 13:16:03 -0000	1.129=0A=
+++ mb-parser.jay	17 Aug 2004 18:13:36 -0000=0A=
@@ -1679,7 +1679,7 @@ interface_method_declaration=0A=
 	;=0A=
 =0A=
 interface_property_declaration=0A=
-	: PROPERTY identifier opt_type_character opt_property_parameters =
opt_type_spec logical_end_of_line=0A=
+	: PROPERTY identifier opt_type_character opt_property_parameters =
opt_type_with_ranks logical_end_of_line=0A=
 	  {=0A=
 		Expression ftype =3D ($5 =3D=3D null) ? (($3 =3D=3D null) ? =0A=
 				TypeManager.system_object_expr : (Expression) $3 ) : (Expression) =
$5;=0A=
@@ -1763,7 +1763,7 @@ property_declaration=0A=
 	;=0A=
 	=0A=
 abstract_propery_declaration=0A=
-	: MUSTOVERRIDE PROPERTY identifier opt_type_character =
opt_property_parameters opt_type_spec logical_end_of_line=0A=
+	: MUSTOVERRIDE PROPERTY identifier opt_type_character =
opt_property_parameters opt_type_with_ranks logical_end_of_line=0A=
 	  {	=0A=
 		Expression ftype =3D ($6 =3D=3D null) ? (($4 =3D=3D null) ? =0A=
 				TypeManager.system_object_expr : (Expression) $4 ) : (Expression) =
$6;=0A=
@@ -1820,7 +1820,7 @@ abstract_propery_declaration=0A=
 	=0A=
 	=0A=
  non_abstract_propery_declaration=0A=
-	  : PROPERTY identifier opt_type_character opt_property_parameters =
opt_type_spec opt_implement_clause logical_end_of_line=0A=
+	  : PROPERTY identifier opt_type_character opt_property_parameters =
opt_type_with_ranks opt_implement_clause logical_end_of_line=0A=
 	  {=0A=
 		get_implicit_value_parameter_type  =3D =0A=
 			($5 =3D=3D null) ? (($3 =3D=3D null) ? =0A=
@@ -2002,7 +2002,7 @@ opt_set_parameter=0A=
 		set_implicit_value_parameter_type =3D (Expression) =
get_implicit_value_parameter_type;=0A=
 		set_implicit_value_parameter_name =3D "Value";=0A=
 	}	=0A=
-	| OPEN_PARENS opt_parameter_modifier opt_identifier opt_type_spec =
CLOSE_PARENS=0A=
+	| OPEN_PARENS opt_parameter_modifier opt_identifier =
opt_type_with_ranks CLOSE_PARENS=0A=
 	{=0A=
 		Parameter.Modifier pm =3D (Parameter.Modifier)$2;=0A=
 		if ((pm | Parameter.Modifier.VAL) !=3D 0)=0A=
@@ -2123,7 +2123,7 @@ delegate_declaration=0A=
 	| DELEGATE FUNCTION	  =0A=
 	  identifier OPEN_PARENS =0A=
 	  opt_formal_parameter_list=0A=
-	  CLOSE_PARENS opt_type_spec logical_end_of_line=0A=
+	  CLOSE_PARENS opt_type_with_ranks logical_end_of_line=0A=
 	  {=0A=
 		Location l =3D lexer.Location;=0A=
 		=0A=
@@ -2240,7 +2240,7 @@ fixed_parameters=0A=
 fixed_parameter=0A=
 	: opt_attributes=0A=
 	  opt_parameter_modifier=0A=
-	  identifier opt_type_character opt_rank_specifiers opt_type_spec =
opt_variable_initializer=0A=
+	  identifier opt_type_character opt_rank_specifiers =
opt_type_with_ranks opt_variable_initializer=0A=
 	  {=0A=
 	  	Parameter.Modifier pm =3D (Parameter.Modifier)$2;=0A=
 	  	bool opt_parm =3D ((pm & Parameter.Modifier.OPTIONAL) !=3D 0);=0A=
@@ -2261,8 +2261,11 @@ fixed_parameter=0A=
 =0A=
 		ptype =3D (Expression)(($6 =3D=3D null) ? (($4 =3D=3D null) ? =
TypeManager.system_object_expr : $4) : $6);=0A=
 		if ($5 !=3D null)	{=0A=
-	  		string t =3D ptype.ToString() + VariableDeclaration.BuildRank =
((ArrayList) $5);=0A=
-	  		ptype =3D DecomposeQI (t, lexer.Location);=0A=
+	  		string t =3D ptype.ToString();=0A=
+	  		if (t.IndexOf('[') >=3D 0)=0A=
+				Report.Error (31087, lexer.Location, "Array types specified in too =
many places");=0A=
+			else	=0A=
+	  			ptype =3D DecomposeQI (t + VariableDeclaration.BuildRanks =
((ArrayList) $5, true, lexer.Location), lexer.Location);=0A=
 	  	}			=0A=
 		$$ =3D new Parameter (ptype, (string) $3, pm,=0A=
 					(Attributes) $1, (Expression) $7, opt_parm);=0A=
@@ -3191,7 +3194,7 @@ object_creation_expression=0A=
 	;=0A=
 	=0A=
 array_creation_expression=0A=
-	: object_creation_expression array_initializer=0A=
+	: object_creation_expression opt_rank_specifiers array_initializer=0A=
 	  {=0A=
 	  	New n =3D (New) $1;=0A=
 	  	ArrayList dims =3D new ArrayList();=0A=
@@ -3204,7 +3207,10 @@ array_creation_expression=0A=
 			=0A=
 	  	Expression atype =3D n.RequestedType;=0A=
 =0A=
-		ArrayList init =3D (ArrayList) $2;=0A=
+		if ($2 !=3D null)=0A=
+			atype =3D DecomposeQI (atype.ToString () + =
VariableDeclaration.BuildRanks ((ArrayList)$2, true, lexer.Location), =
lexer.Location);=0A=
+=0A=
+		ArrayList init =3D (ArrayList) $3;=0A=
 		if (init.Count =3D=3D 0)=0A=
 			init =3D null;=0A=
   	=0A=
@@ -3215,7 +3221,7 @@ array_creation_expression=0A=
 	  	else=0A=
 	  	{=0A=
 	  		string rank =3D VariableDeclaration.BuildRank (dims);=0A=
-	  		$$ =3D new ArrayCreation (atype, rank, (ArrayList) $2, =
lexer.Location); =0A=
+	  		$$ =3D new ArrayCreation (atype, rank, (ArrayList) $3, =
lexer.Location); =0A=
 	  	}=0A=
 	  	//Console.WriteLine ("Creating a new array of type " + =
(atype.ToString()) + " with rank '" + dims + "'");=0A=
 	  }=0A=
@@ -3334,7 +3340,6 @@ variable_declarator=0A=
 	    ArrayList names =3D (ArrayList) $1;=0A=
 		object varinit =3D $3;=0A=
 		ArrayList VarDeclarations =3D new ArrayList();=0A=
-	  	Expression vartype;=0A=
 	  	ArrayList a_dims =3D null;=0A=
 =0A=
 		if ((names.Count > 1) && (varinit !=3D null)) =0A=
@@ -3358,34 +3363,14 @@ variable_declarator=0A=
 =0A=
 	  		// Some checking is required for particularly weird declarations=0A=
 	  		// like Dim a As Integer(,)=0A=
-	  		if ($2 is Pair) {=0A=
-	  			vartype =3D (Expression) ((Pair) $2).First;=0A=
-				=0A=
-	  			/*if ($3 !=3D null && $3 is ArrayList)=0A=
-	  				Report.Error (205, "End of statement expected.");*/=0A=
-		  			=0A=
-				ArrayList args =3D (ArrayList) ((Pair) $2).Second;=0A=
-				if (current_rank_specifiers !=3D null)=0A=
+		  	Expression vartype =3D (Expression)$2;=0A=
+	  		if (vartype !=3D null) {=0A=
+				if (vartype.ToString().IndexOf('[') >=3D 0 && =
current_rank_specifiers !=3D null)=0A=
 					Report.Error (31087, lexer.Location,=0A=
 						 "Array types specified in too many places");	=0A=
-				=0A=
-				if (VariableDeclaration.IndexesSpecifiedInRank (args))	  	  =0A=
-					Report.Error (30638, "Array bounds cannot appear in type =
specifiers.");	=0A=
-				=0A=
-				current_rank_specifiers =3D new ArrayList ();=0A=
-				current_rank_specifiers.Add (args);=0A=
-				=0A=
-				/*string s_vartype =3D vartype.ToString();				=0A=
-				s_vartype +=3D "[";=0A=
-				if (args !=3D null)=0A=
-					for (int x =3D 0; x < args.Count; x++)=0A=
-						s_vartype +=3D ",";=0A=
-					=0A=
-				s_vartype +=3D "]";	=0A=
-				vartype =3D DecomposeQI(s_vartype, Location.Null);	*/=0A=
 	  		}=0A=
 	  		else=0A=
-				vartype =3D ($2 =3D=3D null) ? ((vname.Type =3D=3D null) ? =
TypeManager.system_object_expr : (Expression) vname.Type ) : =
(Expression) $2;=0A=
+				vartype =3D ((vname.Type =3D=3D null) ? =
TypeManager.system_object_expr : (Expression) vname.Type );=0A=
 =0A=
 			// if the variable is an array with explicit bound=0A=
 			// and having explicit initialization throw exception=0A=
@@ -3422,7 +3407,7 @@ variable_declarator=0A=
 					VariableDeclaration.VBFixIndexLists (ref a_dims);=0A=
 					varinit =3D VariableDeclaration.BuildArrayCreator(vartype, a_dims, =
(ArrayList) varinit, lexer.Location);=0A=
 				}=0A=
-				vartype =3D DecomposeQI (vartype.ToString() + =
VariableDeclaration.BuildRanks (this), lexer.Location);=0A=
+				vartype =3D DecomposeQI (vartype.ToString() + =
VariableDeclaration.BuildRanks (current_rank_specifiers, false, =
lexer.Location), lexer.Location);=0A=
 			}=0A=
 =0A=
 			if (vartype is New) {=0A=
@@ -3479,19 +3464,15 @@ opt_type_with_ranks=0A=
 	: opt_type_spec	=0A=
 	| AS type rank_specifiers=0A=
 	  {=0A=
-	  	$$ =3D TypeManager.system_object_expr; =0A=
+		$$ =3D DecomposeQI ($2.ToString() + VariableDeclaration.BuildRanks =
((ArrayList)$3, true, lexer.Location), lexer.Location);=0A=
 	  }=0A=
 	;=0A=
 	=0A=
 opt_type_decl=0A=
-	: opt_type_spec=0A=
+	: opt_type_with_ranks=0A=
 	  {=0A=
 	  	$$ =3D $1;=0A=
 	  }=0A=
-	| AS type OPEN_PARENS /*opt_argument_list*/ opt_dim_separators =
CLOSE_PARENS=0A=
-	  {=0A=
-	  	$$ =3D new Pair ($2, $4);=0A=
-	  }=0A=
 	| AS NEW type=0A=
 	  {=0A=
 	  	New n =3D new New ((Expression)$3, null, lexer.Location);=0A=
@@ -3594,59 +3575,37 @@ rank_specifiers=0A=
         ;	  	=0A=
 	=0A=
 rank_specifier=0A=
-	: OPEN_PARENS opt_dim_separators CLOSE_PARENS=0A=
+	: OPEN_PARENS opt_dim_specifiers CLOSE_PARENS=0A=
 	  {=0A=
 		$$ =3D $2;=0A=
 	  }=0A=
 	;=0A=
 		=0A=
-opt_dim_separators=0A=
+opt_dim_specifiers=0A=
 	: /* empty */=0A=
 	  {=0A=
-		ArrayList ds =3D new ArrayList();=0A=
-	  	ds.Add (new EmptyExpression());=0A=
-	  	$$ =3D ds;		=0A=
-	  }=0A=
-	| dim_separators=0A=
-	  {=0A=
-		ArrayList ds =3D (ArrayList) $1;=0A=
-	  	ds.Add (new EmptyExpression());=0A=
-	  	$$ =3D ds;	=0A=
-	  }	=0A=
-	| dim_specifiers=0A=
-	  {=0A=
-	  	$$ =3D $1;=0A=
-	  }	  =0A=
-	;=0A=
-=0A=
-dim_separators=0A=
-	: COMMA=0A=
-	  {=0A=
 	  	ArrayList ds =3D new ArrayList();=0A=
 	  	ds.Add (new EmptyExpression());=0A=
 	  	$$ =3D ds;=0A=
-	  }=0A=
-	| dim_separators COMMA=0A=
-	  {=0A=
-		ArrayList ds =3D (ArrayList) $1;=0A=
-	  	ds.Add (new EmptyExpression());=0A=
-	  	$$ =3D ds;		=0A=
-	  }=0A=
-	;=0A=
-=0A=
-dim_specifiers=0A=
-	: expression=0A=
+	  }	=0A=
+	| expression=0A=
 	  {=0A=
 	  	ArrayList ds =3D new ArrayList();=0A=
 	  	ds.Add ((Expression) $1);=0A=
 	  	$$ =3D ds;=0A=
 	  }	=0A=
-	| dim_specifiers COMMA expression=0A=
+	| opt_dim_specifiers COMMA expression=0A=
 	  {=0A=
 		ArrayList ds =3D (ArrayList) $1;=0A=
 	  	ds.Add ((Expression) $3);=0A=
 	  	$$ =3D ds;		=0A=
 	  }	=0A=
+	| opt_dim_specifiers COMMA =0A=
+	  {=0A=
+		ArrayList ds =3D (ArrayList) $1;=0A=
+	  	ds.Add (new EmptyExpression());=0A=
+	  	$$ =3D ds;		=0A=
+	  }	=0A=
 	;=0A=
 	=0A=
 primary_expression=0A=
@@ -4555,22 +4514,42 @@ public class VariableDeclaration {=0A=
 	=0A=
 	public static string BuildRank (ArrayList rank)=0A=
 	{=0A=
+		bool allEmpty;=0A=
+		return BuildRank(rank, out allEmpty);=0A=
+	}=0A=
+            =0A=
+	public static string BuildRank (ArrayList rank, out bool allEmpty)=0A=
+	{=0A=
 		string res =3D "";=0A=
 =0A=
 		res +=3D "[";=0A=
-		for (int x =3D 0; x < (rank.Count -1 ); x++)=0A=
-			res +=3D ",";=0A=
+		allEmpty =3D true;=0A=
+		bool first =3D true;=0A=
+		foreach (object e in rank) {=0A=
+			if (!(e is EmptyExpression))=0A=
+				allEmpty =3D false;=0A=
+			if (!first)=0A=
+				res +=3D ",";=0A=
+			first =3D false;=0A=
+		}=0A=
 			=0A=
 		res +=3D "]";=0A=
 		return res;=0A=
 	}=0A=
 		=0A=
-	public static string BuildRanks (Parser t)=0A=
+	public static string BuildRanks (ArrayList rank_specifiers, bool =
mustBeEmpty, Location loc)=0A=
 	{=0A=
 		string res =3D "";=0A=
 =0A=
-		foreach (ArrayList rank in t.current_rank_specifiers)=0A=
-			res +=3D BuildRank (rank);=0A=
+		bool allEmpty =3D true;=0A=
+		foreach (ArrayList rank in rank_specifiers) {=0A=
+			bool tmp;=0A=
+			res =3D BuildRank (rank, out tmp) + res;=0A=
+			if (!tmp)=0A=
+				allEmpty =3D false;=0A=
+		}=0A=
+		if (!allEmpty && mustBeEmpty)=0A=
+			Report.Error (30638, loc, "Array bounds cannot appear in type =
specifiers.");	=0A=
 =0A=
 		return res;=0A=
 	}	=0A=

------=_NextPart_000_00E5_01C4846A.1CCEC810--