Permalink
Browse files

Implemented better solution for the array initializer workaround hack.

  • Loading branch information...
1 parent 121c786 commit f7fb29804d33ffa1ae84e48cfdb1224817d7f657 @mkrueger mkrueger committed Apr 25, 2012
@@ -33,6 +33,18 @@ namespace ICSharpCode.NRefactory.CSharp
/// </summary>
public class ArrayInitializerExpression : Expression
{
+ /// <summary>
+ /// For ease of use purposes in the resolver the ast representation
+ /// of { a, b, c } is { {a}, {b}, {c} }.
+ /// If IsSingleElement is true then this array initializer expression is a generated one.
+ /// That has no meaning in the source code (and contains no brace tokens).
+ /// </summary>
+ public virtual bool IsSingleElement {
+ get {
+ return false;
+ }
+ }
+
public ArrayInitializerExpression()
{
}
@@ -111,6 +123,23 @@ protected internal override bool DoMatch(AstNode other, PatternMatching.Match ma
ArrayInitializerExpression o = other as ArrayInitializerExpression;
return o != null && this.Elements.DoMatch(o.Elements, match);
}
+
+ public static ArrayInitializerExpression CreateSingleElementInitializer ()
+ {
+ return new SingleArrayInitializerExpression();
+ }
+ /// <summary>
+ /// Single elements in array initializers are represented with this special class.
+ /// </summary>
+ class SingleArrayInitializerExpression : ArrayInitializerExpression
+ {
+ public override bool IsSingleElement {
+ get {
+ return true;
+ }
+ }
+
+ }
}
}
@@ -2731,12 +2731,14 @@ void AddConvertCollectionOrObjectInitializers(Expression init, CollectionOrObjec
var collectionInit = expr as CollectionElementInitializer;
if (collectionInit != null) {
AstNode parent;
- Console.WriteLine("single:" + collectionInit.IsSingle);
+ // For ease of use purposes in the resolver the ast representation
+ // of { a, b, c } is { {a}, {b}, {c} } - but the generated ArrayInitializerExpression
+ // can be identified by expr.IsSingleElement.
if (!collectionInit.IsSingle) {
parent = new ArrayInitializerExpression();
parent.AddChild(new CSharpTokenNode(Convert(collectionInit.Location)), Roles.LBrace);
} else {
- parent = init;
+ parent = ArrayInitializerExpression.CreateSingleElementInitializer ();
}
for (int i = 0; i < collectionInit.Arguments.Count; i++) {
var arg = collectionInit.Arguments [i] as CollectionElementInitializer.ElementInitializerArgument;
@@ -2752,8 +2754,8 @@ void AddConvertCollectionOrObjectInitializers(Expression init, CollectionOrObjec
var braceLocs = LocationsBag.GetLocations(expr);
if (braceLocs != null)
parent.AddChild(new CSharpTokenNode(Convert(braceLocs [0])), Roles.RBrace);
- init.AddChild((ArrayInitializerExpression)parent, Roles.Expression);
}
+ init.AddChild((ArrayInitializerExpression)parent, Roles.Expression);
} else {
var eleInit = expr as ElementInitializer;
if (eleInit != null) {
@@ -163,13 +163,13 @@ public void CollectionInitializer()
Type = new SimpleType("List", new PrimitiveType("int")),
Initializer = new ArrayInitializerExpression {
Elements = {
- new PrimitiveExpression(0),
- new PrimitiveExpression(1),
- new PrimitiveExpression(2)
- }}}
- );
+ new ArrayInitializerExpression(new PrimitiveExpression(0)),
+ new ArrayInitializerExpression(new PrimitiveExpression(1)),
+ new ArrayInitializerExpression(new PrimitiveExpression(2))
+ }}});
}
-
+
+
[Test]
public void DictionaryInitializer()
{
@@ -205,35 +205,31 @@ public void ComplexCollectionInitializer()
new ObjectCreateExpression {
Type = new SimpleType("List", new SimpleType("Contact")),
Initializer = new ArrayInitializerExpression(
- new ObjectCreateExpression {
- Type = new SimpleType("Contact"),
- Initializer = new ArrayInitializerExpression {
- Elements = {
- new NamedExpression("Name", new PrimitiveExpression("Chris")),
- new NamedExpression("PhoneNumbers", new ArrayInitializerExpression () {
- Elements = { new PrimitiveExpression("206-555-0101") }
- })
- }
- }
- },
- new ObjectCreateExpression {
- Type = new SimpleType("Contact"),
- Arguments = { new IdentifierExpression("additionalParameter") },
- Initializer = new ArrayInitializerExpression {
- Elements = {
- new NamedExpression("Name", new PrimitiveExpression("Bob")),
- new NamedExpression("PhoneNumbers", new ArrayInitializerExpression () {
- Elements = {
- new PrimitiveExpression("650-555-0199"),
- new PrimitiveExpression("425-882-8080")
- }
- })
- }
- }
- }
- )
- }
- );
+ new ArrayInitializerExpression(
+ new ObjectCreateExpression {
+ Type = new SimpleType("Contact"),
+ Initializer = new ArrayInitializerExpression {
+ Elements = {
+ new NamedExpression("Name", new PrimitiveExpression("Chris")),
+ new NamedExpression("PhoneNumbers", new ArrayInitializerExpression () {
+ Elements = { new ArrayInitializerExpression(new PrimitiveExpression("206-555-0101")) }
+ })
+ }}}),
+ new ArrayInitializerExpression(
+ new ObjectCreateExpression {
+ Type = new SimpleType("Contact"),
+ Arguments = { new IdentifierExpression("additionalParameter") },
+ Initializer = new ArrayInitializerExpression {
+ Elements = {
+ new NamedExpression("Name", new PrimitiveExpression("Bob")),
+ new NamedExpression("PhoneNumbers", new ArrayInitializerExpression () {
+ Elements = {
+ new ArrayInitializerExpression(new PrimitiveExpression("650-555-0199")),
+ new ArrayInitializerExpression(new PrimitiveExpression("425-882-8080"))
+ }
+ })
+ }}})
+ )});
}
[Test]
@@ -165,7 +165,6 @@ class TestClass {
#endregion
#region Lambda In Collection Initializer
- [Ignore("Got broken due to ArrayInitializer fix (the AST is now correct)")]
[Test]
public void LambdaInCollectionInitializer1()
{
@@ -260,7 +260,7 @@ class Test {
Assert.IsFalse(rr.IsError);
Assert.AreEqual("Test..ctor", rr.Member.FullName);
Assert.AreEqual("Test", rr.Type.ReflectionName);
- Assert.AreEqual(3, rr.InitializerStatements.Count);
+ Assert.AreEqual(5, rr.InitializerStatements.Count);
}
[Test]

0 comments on commit f7fb298

Please sign in to comment.