/NRefactory/ICSharpCode.NRefactory.CSharp/Ast/Statements/TryCatchStatement.cs

http://github.com/icsharpcode/ILSpy · C# · 241 lines · 171 code · 39 blank · 31 comment · 12 complexity · 5d7d6f1f08690f8b4455fb1253506e39 MD5 · raw file

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