1 | // |
---|
2 | // TryCatchStatement.cs |
---|
3 | // |
---|
4 | // Author: |
---|
5 | // Mike Krüger <mkrueger@novell.com> |
---|
6 | // |
---|
7 | // Copyright (c) 2009 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 | |
---|
27 | using System.Collections.Generic; |
---|
28 | using System.Linq; |
---|
29 | |
---|
30 | namespace ICSharpCode.NRefactory.CSharp |
---|
31 | { |
---|
32 | /// <summary> |
---|
33 | /// try TryBlock CatchClauses finally FinallyBlock |
---|
34 | /// </summary> |
---|
35 | public class TryCatchStatement : Statement |
---|
36 | { |
---|
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); |
---|
42 | |
---|
43 | public CSharpTokenNode TryToken { |
---|
44 | get { return GetChildByRole (TryKeywordRole); } |
---|
45 | } |
---|
46 | |
---|
47 | public BlockStatement TryBlock { |
---|
48 | get { return GetChildByRole (TryBlockRole); } |
---|
49 | set { SetChildByRole (TryBlockRole, value); } |
---|
50 | } |
---|
51 | |
---|
52 | public AstNodeCollection<CatchClause> CatchClauses { |
---|
53 | get { return GetChildrenByRole (CatchClauseRole); } |
---|
54 | } |
---|
55 | |
---|
56 | public CSharpTokenNode FinallyToken { |
---|
57 | get { return GetChildByRole (FinallyKeywordRole); } |
---|
58 | } |
---|
59 | |
---|
60 | public BlockStatement FinallyBlock { |
---|
61 | get { return GetChildByRole (FinallyBlockRole); } |
---|
62 | set { SetChildByRole (FinallyBlockRole, value); } |
---|
63 | } |
---|
64 | |
---|
65 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
66 | { |
---|
67 | visitor.VisitTryCatchStatement (this); |
---|
68 | } |
---|
69 | |
---|
70 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
71 | { |
---|
72 | return visitor.VisitTryCatchStatement (this); |
---|
73 | } |
---|
74 | |
---|
75 | public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) |
---|
76 | { |
---|
77 | return visitor.VisitTryCatchStatement (this, data); |
---|
78 | } |
---|
79 | |
---|
80 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
81 | { |
---|
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); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | /// <summary> |
---|
88 | /// catch (Type VariableName) { Body } |
---|
89 | /// </summary> |
---|
90 | public class CatchClause : AstNode |
---|
91 | { |
---|
92 | public static readonly TokenRole CatchKeywordRole = new TokenRole ("catch"); |
---|
93 | |
---|
94 | #region Null |
---|
95 | public new static readonly CatchClause Null = new NullCatchClause (); |
---|
96 | |
---|
97 | sealed class NullCatchClause : CatchClause |
---|
98 | { |
---|
99 | public override bool IsNull { |
---|
100 | get { |
---|
101 | return true; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
106 | { |
---|
107 | visitor.VisitNullNode(this); |
---|
108 | } |
---|
109 | |
---|
110 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
111 | { |
---|
112 | return visitor.VisitNullNode(this); |
---|
113 | } |
---|
114 | |
---|
115 | public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) |
---|
116 | { |
---|
117 | return visitor.VisitNullNode(this, data); |
---|
118 | } |
---|
119 | |
---|
120 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
121 | { |
---|
122 | return other == null || other.IsNull; |
---|
123 | } |
---|
124 | } |
---|
125 | #endregion |
---|
126 | |
---|
127 | #region PatternPlaceholder |
---|
128 | public static implicit operator CatchClause(PatternMatching.Pattern pattern) |
---|
129 | { |
---|
130 | return pattern != null ? new PatternPlaceholder(pattern) : null; |
---|
131 | } |
---|
132 | |
---|
133 | sealed class PatternPlaceholder : CatchClause, PatternMatching.INode |
---|
134 | { |
---|
135 | readonly PatternMatching.Pattern child; |
---|
136 | |
---|
137 | public PatternPlaceholder(PatternMatching.Pattern child) |
---|
138 | { |
---|
139 | this.child = child; |
---|
140 | } |
---|
141 | |
---|
142 | public override NodeType NodeType { |
---|
143 | get { return NodeType.Pattern; } |
---|
144 | } |
---|
145 | |
---|
146 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
147 | { |
---|
148 | visitor.VisitPatternPlaceholder(this, child); |
---|
149 | } |
---|
150 | |
---|
151 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
152 | { |
---|
153 | return visitor.VisitPatternPlaceholder(this, child); |
---|
154 | } |
---|
155 | |
---|
156 | public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data) |
---|
157 | { |
---|
158 | return visitor.VisitPatternPlaceholder(this, child, data); |
---|
159 | } |
---|
160 | |
---|
161 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
162 | { |
---|
163 | return child.DoMatch(other, match); |
---|
164 | } |
---|
165 | |
---|
166 | bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo) |
---|
167 | { |
---|
168 | return child.DoMatchCollection(role, pos, match, backtrackingInfo); |
---|
169 | } |
---|
170 | } |
---|
171 | #endregion |
---|
172 | |
---|
173 | public override NodeType NodeType { |
---|
174 | get { |
---|
175 | return NodeType.Unknown; |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | public CSharpTokenNode CatchToken { |
---|
180 | get { return GetChildByRole (CatchKeywordRole); } |
---|
181 | } |
---|
182 | |
---|
183 | public CSharpTokenNode LParToken { |
---|
184 | get { return GetChildByRole (Roles.LPar); } |
---|
185 | } |
---|
186 | |
---|
187 | public AstType Type { |
---|
188 | get { return GetChildByRole (Roles.Type); } |
---|
189 | set { SetChildByRole (Roles.Type, value); } |
---|
190 | } |
---|
191 | |
---|
192 | public string VariableName { |
---|
193 | get { return GetChildByRole (Roles.Identifier).Name; } |
---|
194 | set { |
---|
195 | if (string.IsNullOrEmpty(value)) |
---|
196 | SetChildByRole (Roles.Identifier, null); |
---|
197 | else |
---|
198 | SetChildByRole (Roles.Identifier, Identifier.Create (value)); |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | public Identifier VariableNameToken { |
---|
203 | get { |
---|
204 | return GetChildByRole (Roles.Identifier); |
---|
205 | } |
---|
206 | set { |
---|
207 | SetChildByRole(Roles.Identifier, value); |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | public CSharpTokenNode RParToken { |
---|
212 | get { return GetChildByRole (Roles.RPar); } |
---|
213 | } |
---|
214 | |
---|
215 | public BlockStatement Body { |
---|
216 | get { return GetChildByRole (Roles.Body); } |
---|
217 | set { SetChildByRole (Roles.Body, value); } |
---|
218 | } |
---|
219 | |
---|
220 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
221 | { |
---|
222 | visitor.VisitCatchClause (this); |
---|
223 | } |
---|
224 | |
---|
225 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
226 | { |
---|
227 | return visitor.VisitCatchClause (this); |
---|
228 | } |
---|
229 | |
---|
230 | public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) |
---|
231 | { |
---|
232 | return visitor.VisitCatchClause (this, data); |
---|
233 | } |
---|
234 | |
---|
235 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
236 | { |
---|
237 | CatchClause o = other as CatchClause; |
---|
238 | return o != null && this.Type.DoMatch(o.Type, match) && MatchString(this.VariableName, o.VariableName) && this.Body.DoMatch(o.Body, match); |
---|
239 | } |
---|
240 | } |
---|
241 | } |
---|