Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.CSharp-5.5.0/Ast/Expressions/ArrayInitializerExpression.cs @ 11937

Last change on this file since 11937 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 5.5 KB
Line 
1//
2// ArrayInitializerExpression.cs
3//
4// Author:
5//       Mike Krüger <mkrueger@novell.com>
6//
7// Copyright (c) 2010 Novell, Inc (http://www.novell.com)
8//
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:
15//
16// The above copyright notice and this permission notice shall be included in
17// all copies or substantial portions of the Software.
18//
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
25// THE SOFTWARE.
26
27using System.Collections.Generic;
28
29namespace ICSharpCode.NRefactory.CSharp
30{
31  /// <summary>
32  /// { Elements }
33  /// </summary>
34  public class ArrayInitializerExpression : Expression
35  {
36    /// <summary>
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).
41    /// </summary>
42    public virtual bool IsSingleElement {
43      get {
44        return false;
45      }
46    }
47
48    public ArrayInitializerExpression()
49    {
50    }
51   
52    public ArrayInitializerExpression(IEnumerable<Expression> elements)
53    {
54      this.Elements.AddRange(elements);
55    }
56   
57    public ArrayInitializerExpression(params Expression[] elements)
58    {
59      this.Elements.AddRange(elements);
60    }
61   
62    #region Null
63    public new static readonly ArrayInitializerExpression Null = new NullArrayInitializerExpression ();
64   
65    sealed class NullArrayInitializerExpression : ArrayInitializerExpression
66    {
67      public override bool IsNull {
68        get {
69          return true;
70        }
71      }
72     
73      public override void AcceptVisitor (IAstVisitor visitor)
74      {
75        visitor.VisitNullNode(this);
76      }
77     
78      public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
79      {
80        return visitor.VisitNullNode(this);
81      }
82     
83      public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
84      {
85        return visitor.VisitNullNode(this, data);
86      }
87     
88      protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
89      {
90        return other == null || other.IsNull;
91      }
92    }
93    #endregion
94   
95    public CSharpTokenNode LBraceToken {
96      get { return GetChildByRole (Roles.LBrace); }
97    }
98   
99    public AstNodeCollection<Expression> Elements {
100      get { return GetChildrenByRole(Roles.Expression); }
101    }
102   
103    public CSharpTokenNode RBraceToken {
104      get { return GetChildByRole (Roles.RBrace); }
105    }
106   
107    public override void AcceptVisitor (IAstVisitor visitor)
108    {
109      visitor.VisitArrayInitializerExpression (this);
110    }
111   
112    public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
113    {
114      return visitor.VisitArrayInitializerExpression (this);
115    }
116   
117    public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
118    {
119      return visitor.VisitArrayInitializerExpression (this, data);
120    }
121   
122    protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
123    {
124      ArrayInitializerExpression o = other as ArrayInitializerExpression;
125      return o != null && this.Elements.DoMatch(o.Elements, match);
126    }
127
128    public static ArrayInitializerExpression CreateSingleElementInitializer ()
129    {
130      return new SingleArrayInitializerExpression();
131    }
132    /// <summary>
133    /// Single elements in array initializers are represented with this special class.
134    /// </summary>
135    class SingleArrayInitializerExpression : ArrayInitializerExpression
136    {
137      public override bool IsSingleElement {
138        get {
139          return true;
140        }
141      }
142     
143    }
144   
145    #region PatternPlaceholder
146    public static implicit operator ArrayInitializerExpression(PatternMatching.Pattern pattern)
147    {
148      return pattern != null ? new PatternPlaceholder(pattern) : null;
149    }
150   
151    sealed class PatternPlaceholder : ArrayInitializerExpression, PatternMatching.INode
152    {
153      readonly PatternMatching.Pattern child;
154     
155      public PatternPlaceholder(PatternMatching.Pattern child)
156      {
157        this.child = child;
158      }
159     
160      public override NodeType NodeType {
161        get { return NodeType.Pattern; }
162      }
163     
164      public override void AcceptVisitor (IAstVisitor visitor)
165      {
166        visitor.VisitPatternPlaceholder(this, child);
167      }
168     
169      public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
170      {
171        return visitor.VisitPatternPlaceholder(this, child);
172      }
173
174      public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
175      {
176        return visitor.VisitPatternPlaceholder(this, child, data);
177      }
178     
179      protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
180      {
181        return child.DoMatch(other, match);
182      }
183     
184      bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo)
185      {
186        return child.DoMatchCollection(role, pos, match, backtrackingInfo);
187      }
188    }
189    #endregion
190  }
191}
192
Note: See TracBrowser for help on using the repository browser.