5
// Mike KrĆ¼ger <mkrueger@novell.com>
7
// Copyright (c) 2009 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;
30
namespace ICSharpCode.NRefactory.CSharp
33
/// switch (Expression) { SwitchSections }
35
public class SwitchStatement : Statement
37
public static readonly TokenRole SwitchKeywordRole = new TokenRole ("switch");
38
public static readonly Role<SwitchSection> SwitchSectionRole = new Role<SwitchSection>("SwitchSection");
40
public CSharpTokenNode SwitchToken {
41
get { return GetChildByRole (SwitchKeywordRole); }
44
public CSharpTokenNode LParToken {
45
get { return GetChildByRole (Roles.LPar); }
48
public Expression Expression {
49
get { return GetChildByRole (Roles.Expression); }
50
set { SetChildByRole (Roles.Expression, value); }
53
public CSharpTokenNode RParToken {
54
get { return GetChildByRole (Roles.RPar); }
57
public CSharpTokenNode LBraceToken {
58
get { return GetChildByRole (Roles.LBrace); }
61
public AstNodeCollection<SwitchSection> SwitchSections {
62
get { return GetChildrenByRole (SwitchSectionRole); }
65
public CSharpTokenNode RBraceToken {
66
get { return GetChildByRole (Roles.RBrace); }
69
public override void AcceptVisitor (IAstVisitor visitor)
71
visitor.VisitSwitchStatement (this);
74
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
76
return visitor.VisitSwitchStatement (this);
79
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
81
return visitor.VisitSwitchStatement (this, data);
84
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
86
SwitchStatement o = other as SwitchStatement;
87
return o != null && this.Expression.DoMatch(o.Expression, match) && this.SwitchSections.DoMatch(o.SwitchSections, match);
91
public class SwitchSection : AstNode
93
#region PatternPlaceholder
94
public static implicit operator SwitchSection(PatternMatching.Pattern pattern)
96
return pattern != null ? new PatternPlaceholder(pattern) : null;
99
sealed class PatternPlaceholder : SwitchSection, PatternMatching.INode
101
readonly PatternMatching.Pattern child;
103
public PatternPlaceholder(PatternMatching.Pattern child)
108
public override NodeType NodeType {
109
get { return NodeType.Pattern; }
112
public override void AcceptVisitor (IAstVisitor visitor)
114
visitor.VisitPatternPlaceholder(this, child);
117
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
119
return visitor.VisitPatternPlaceholder(this, child);
122
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
124
return visitor.VisitPatternPlaceholder(this, child, data);
127
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
129
return child.DoMatch(other, match);
132
bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo)
134
return child.DoMatchCollection(role, pos, match, backtrackingInfo);
139
public static readonly Role<CaseLabel> CaseLabelRole = new Role<CaseLabel>("CaseLabel");
141
public override NodeType NodeType {
143
return NodeType.Unknown;
147
public AstNodeCollection<CaseLabel> CaseLabels {
148
get { return GetChildrenByRole (CaseLabelRole); }
151
public AstNodeCollection<Statement> Statements {
152
get { return GetChildrenByRole (Roles.EmbeddedStatement); }
155
public override void AcceptVisitor (IAstVisitor visitor)
157
visitor.VisitSwitchSection (this);
160
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
162
return visitor.VisitSwitchSection (this);
165
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
167
return visitor.VisitSwitchSection (this, data);
170
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
172
SwitchSection o = other as SwitchSection;
173
return o != null && this.CaseLabels.DoMatch(o.CaseLabels, match) && this.Statements.DoMatch(o.Statements, match);
177
public class CaseLabel : AstNode
179
public static readonly TokenRole CaseKeywordRole = new TokenRole ("case");
180
public static readonly TokenRole DefaultKeywordRole = new TokenRole ("default");
182
public override NodeType NodeType {
184
return NodeType.Unknown;
189
/// Gets or sets the expression. The expression can be null - if the expression is null, it's the default switch section.
191
public Expression Expression {
192
get { return GetChildByRole (Roles.Expression); }
193
set { SetChildByRole (Roles.Expression, value); }
196
public CSharpTokenNode ColonToken {
197
get { return GetChildByRole (Roles.Colon); }
204
public CaseLabel (Expression expression)
206
this.Expression = expression;
209
public override void AcceptVisitor (IAstVisitor visitor)
211
visitor.VisitCaseLabel (this);
214
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
216
return visitor.VisitCaseLabel (this);
219
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
221
return visitor.VisitCaseLabel (this, data);
224
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
226
CaseLabel o = other as CaseLabel;
227
return o != null && this.Expression.DoMatch(o.Expression, match);