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

http://github.com/icsharpcode/ILSpy · C# · 164 lines · 110 code · 26 blank · 28 comment · 6 complexity · d49fa47cc347b5634c2bdad89e7d64d4 MD5 · raw file

  1. //
  2. // BlockStatement.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. namespace ICSharpCode.NRefactory.CSharp
  28. {
  29. /// <summary>
  30. /// { Statements }
  31. /// </summary>
  32. public class BlockStatement : Statement, IEnumerable<Statement>
  33. {
  34. public static readonly Role<Statement> StatementRole = new Role<Statement>("Statement", Statement.Null);
  35. #region Null
  36. public static readonly new BlockStatement Null = new NullBlockStatement ();
  37. sealed class NullBlockStatement : BlockStatement
  38. {
  39. public override bool IsNull {
  40. get {
  41. return true;
  42. }
  43. }
  44. public override void AcceptVisitor (IAstVisitor visitor)
  45. {
  46. visitor.VisitNullNode(this);
  47. }
  48. public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
  49. {
  50. return visitor.VisitNullNode(this);
  51. }
  52. public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
  53. {
  54. return visitor.VisitNullNode(this, data);
  55. }
  56. protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
  57. {
  58. return other == null || other.IsNull;
  59. }
  60. }
  61. #endregion
  62. #region PatternPlaceholder
  63. public static implicit operator BlockStatement(PatternMatching.Pattern pattern)
  64. {
  65. return pattern != null ? new PatternPlaceholder(pattern) : null;
  66. }
  67. sealed class PatternPlaceholder : BlockStatement, PatternMatching.INode
  68. {
  69. readonly PatternMatching.Pattern child;
  70. public PatternPlaceholder(PatternMatching.Pattern child)
  71. {
  72. this.child = child;
  73. }
  74. public override NodeType NodeType {
  75. get { return NodeType.Pattern; }
  76. }
  77. public override void AcceptVisitor (IAstVisitor visitor)
  78. {
  79. visitor.VisitPatternPlaceholder(this, child);
  80. }
  81. public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
  82. {
  83. return visitor.VisitPatternPlaceholder(this, child);
  84. }
  85. public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
  86. {
  87. return visitor.VisitPatternPlaceholder(this, child, data);
  88. }
  89. protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
  90. {
  91. return child.DoMatch(other, match);
  92. }
  93. bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo)
  94. {
  95. return child.DoMatchCollection(role, pos, match, backtrackingInfo);
  96. }
  97. }
  98. #endregion
  99. public CSharpTokenNode LBraceToken {
  100. get { return GetChildByRole (Roles.LBrace); }
  101. }
  102. public AstNodeCollection<Statement> Statements {
  103. get { return GetChildrenByRole (StatementRole); }
  104. }
  105. public CSharpTokenNode RBraceToken {
  106. get { return GetChildByRole (Roles.RBrace); }
  107. }
  108. public override void AcceptVisitor (IAstVisitor visitor)
  109. {
  110. visitor.VisitBlockStatement (this);
  111. }
  112. public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
  113. {
  114. return visitor.VisitBlockStatement (this);
  115. }
  116. public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
  117. {
  118. return visitor.VisitBlockStatement (this, data);
  119. }
  120. protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
  121. {
  122. BlockStatement o = other as BlockStatement;
  123. return o != null && !o.IsNull && this.Statements.DoMatch(o.Statements, match);
  124. }
  125. public void Add(Statement statement)
  126. {
  127. AddChild(statement, StatementRole);
  128. }
  129. IEnumerator<Statement> IEnumerable<Statement>.GetEnumerator()
  130. {
  131. return this.Statements.GetEnumerator();
  132. }
  133. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  134. {
  135. return this.Statements.GetEnumerator();
  136. }
  137. }
  138. }