2
// TryCatchStatement.cs
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
/// try TryBlock CatchClauses finally FinallyBlock
35
public class TryCatchStatement : Statement
37
public static readonly TokenRole TryKeywordRole = new TokenRole ("try");
38
public static readonly Role<BlockStatement> TryBlockRole = new Role<BlockStatement>("TryBlock", BlockStatement.Null);
39
public static readonly Role<CatchClause> CatchClauseRole = new Role<CatchClause>("CatchClause", CatchClause.Null);
40
public static readonly TokenRole FinallyKeywordRole = new TokenRole ("finally");
41
public static readonly Role<BlockStatement> FinallyBlockRole = new Role<BlockStatement>("FinallyBlock", BlockStatement.Null);
43
public CSharpTokenNode TryToken {
44
get { return GetChildByRole (TryKeywordRole); }
47
public BlockStatement TryBlock {
48
get { return GetChildByRole (TryBlockRole); }
49
set { SetChildByRole (TryBlockRole, value); }
52
public AstNodeCollection<CatchClause> CatchClauses {
53
get { return GetChildrenByRole (CatchClauseRole); }
56
public CSharpTokenNode FinallyToken {
57
get { return GetChildByRole (FinallyKeywordRole); }
60
public BlockStatement FinallyBlock {
61
get { return GetChildByRole (FinallyBlockRole); }
62
set { SetChildByRole (FinallyBlockRole, value); }
65
public override void AcceptVisitor (IAstVisitor visitor)
67
visitor.VisitTryCatchStatement (this);
70
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
72
return visitor.VisitTryCatchStatement (this);
75
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
77
return visitor.VisitTryCatchStatement (this, data);
80
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
82
TryCatchStatement o = other as TryCatchStatement;
83
return o != null && this.TryBlock.DoMatch(o.TryBlock, match) && this.CatchClauses.DoMatch(o.CatchClauses, match) && this.FinallyBlock.DoMatch(o.FinallyBlock, match);
88
/// catch (Type VariableName) { Body }
90
public class CatchClause : AstNode
92
public static readonly TokenRole CatchKeywordRole = new TokenRole ("catch");
95
public new static readonly CatchClause Null = new NullCatchClause ();
97
sealed class NullCatchClause : CatchClause
99
public override bool IsNull {
105
public override void AcceptVisitor (IAstVisitor visitor)
109
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
114
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
119
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
121
return other == null || other.IsNull;
126
#region PatternPlaceholder
127
public static implicit operator CatchClause(PatternMatching.Pattern pattern)
129
return pattern != null ? new PatternPlaceholder(pattern) : null;
132
sealed class PatternPlaceholder : CatchClause, PatternMatching.INode
134
readonly PatternMatching.Pattern child;
136
public PatternPlaceholder(PatternMatching.Pattern child)
141
public override NodeType NodeType {
142
get { return NodeType.Pattern; }
145
public override void AcceptVisitor (IAstVisitor visitor)
147
visitor.VisitPatternPlaceholder(this, child);
150
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
152
return visitor.VisitPatternPlaceholder(this, child);
155
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
157
return visitor.VisitPatternPlaceholder(this, child, data);
160
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
162
return child.DoMatch(other, match);
165
bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo)
167
return child.DoMatchCollection(role, pos, match, backtrackingInfo);
172
public override NodeType NodeType {
174
return NodeType.Unknown;
178
public CSharpTokenNode CatchToken {
179
get { return GetChildByRole (CatchKeywordRole); }
182
public CSharpTokenNode LParToken {
183
get { return GetChildByRole (Roles.LPar); }
186
public AstType Type {
187
get { return GetChildByRole (Roles.Type); }
188
set { SetChildByRole (Roles.Type, value); }
191
public string VariableName {
192
get { return GetChildByRole (Roles.Identifier).Name; }
194
if (string.IsNullOrEmpty(value))
195
SetChildByRole (Roles.Identifier, null);
197
SetChildByRole (Roles.Identifier, Identifier.Create (value));
201
public Identifier VariableNameToken {
203
return GetChildByRole (Roles.Identifier);
206
SetChildByRole(Roles.Identifier, value);
210
public CSharpTokenNode RParToken {
211
get { return GetChildByRole (Roles.RPar); }
214
public BlockStatement Body {
215
get { return GetChildByRole (Roles.Body); }
216
set { SetChildByRole (Roles.Body, value); }
219
public override void AcceptVisitor (IAstVisitor visitor)
221
visitor.VisitCatchClause (this);
224
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
226
return visitor.VisitCatchClause (this);
229
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
231
return visitor.VisitCatchClause (this, data);
234
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
236
CatchClause o = other as CatchClause;
237
return o != null && this.Type.DoMatch(o.Type, match) && MatchString(this.VariableName, o.VariableName) && this.Body.DoMatch(o.Body, match);