2
// ArrayInitializerExpression.cs
5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
using System.Collections.Generic;
29
namespace ICSharpCode.NRefactory.CSharp
34
public class ArrayInitializerExpression : Expression
37
/// For ease of use purposes in the resolver the ast representation
38
/// of { a, b, c } is { {a}, {b}, {c} }.
39
/// If IsSingleElement is true then this array initializer expression is a generated one.
40
/// That has no meaning in the source code (and contains no brace tokens).
42
public virtual bool IsSingleElement {
48
public ArrayInitializerExpression()
52
public ArrayInitializerExpression(IEnumerable<Expression> elements)
54
this.Elements.AddRange(elements);
57
public ArrayInitializerExpression(params Expression[] elements)
59
this.Elements.AddRange(elements);
63
public new static readonly ArrayInitializerExpression Null = new NullArrayInitializerExpression ();
65
sealed class NullArrayInitializerExpression : ArrayInitializerExpression
67
public override bool IsNull {
73
public override void AcceptVisitor (IAstVisitor visitor)
77
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
82
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
87
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
89
return other == null || other.IsNull;
94
public CSharpTokenNode LBraceToken {
95
get { return GetChildByRole (Roles.LBrace); }
98
public AstNodeCollection<Expression> Elements {
99
get { return GetChildrenByRole(Roles.Expression); }
102
public CSharpTokenNode RBraceToken {
103
get { return GetChildByRole (Roles.RBrace); }
106
public override void AcceptVisitor (IAstVisitor visitor)
108
visitor.VisitArrayInitializerExpression (this);
111
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
113
return visitor.VisitArrayInitializerExpression (this);
116
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
118
return visitor.VisitArrayInitializerExpression (this, data);
121
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
123
ArrayInitializerExpression o = other as ArrayInitializerExpression;
124
return o != null && this.Elements.DoMatch(o.Elements, match);
127
public static ArrayInitializerExpression CreateSingleElementInitializer ()
129
return new SingleArrayInitializerExpression();
132
/// Single elements in array initializers are represented with this special class.
134
class SingleArrayInitializerExpression : ArrayInitializerExpression
136
public override bool IsSingleElement {
144
#region PatternPlaceholder
145
public static implicit operator ArrayInitializerExpression(PatternMatching.Pattern pattern)
147
return pattern != null ? new PatternPlaceholder(pattern) : null;
150
sealed class PatternPlaceholder : ArrayInitializerExpression, PatternMatching.INode
152
readonly PatternMatching.Pattern child;
154
public PatternPlaceholder(PatternMatching.Pattern child)
159
public override NodeType NodeType {
160
get { return NodeType.Pattern; }
163
public override void AcceptVisitor (IAstVisitor visitor)
165
visitor.VisitPatternPlaceholder(this, child);
168
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
170
return visitor.VisitPatternPlaceholder(this, child);
173
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
175
return visitor.VisitPatternPlaceholder(this, child, data);
178
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
180
return child.DoMatch(other, match);
183
bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo)
185
return child.DoMatchCollection(role, pos, match, backtrackingInfo);