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

http://github.com/icsharpcode/ILSpy · C# · 123 lines · 97 code · 21 blank · 5 comment · 7 complexity · bc967ec1e21d5ee4698da15f60692695 MD5 · raw file

  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
  2. // This code is distributed under MIT X11 license (for details please see \doc\license.txt)
  3. using System;
  4. using System.Collections.Generic;
  5. namespace ICSharpCode.NRefactory.VB.Ast
  6. {
  7. /// <summary>
  8. /// { Statements }
  9. /// </summary>
  10. public class BlockStatement : Statement, IEnumerable<Statement>
  11. {
  12. public static readonly Role<Statement> StatementRole = new Role<Statement>("Statement", Statement.Null);
  13. #region Null
  14. public static readonly new BlockStatement Null = new NullBlockStatement();
  15. sealed class NullBlockStatement : BlockStatement
  16. {
  17. public override bool IsNull {
  18. get {
  19. return true;
  20. }
  21. }
  22. public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
  23. {
  24. return default(S);
  25. }
  26. protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
  27. {
  28. return other == null || other.IsNull;
  29. }
  30. }
  31. #endregion
  32. #region PatternPlaceholder
  33. public static implicit operator BlockStatement(PatternMatching.Pattern pattern)
  34. {
  35. return pattern != null ? new PatternPlaceholder(pattern) : null;
  36. }
  37. sealed class PatternPlaceholder : BlockStatement, PatternMatching.INode
  38. {
  39. readonly PatternMatching.Pattern child;
  40. public PatternPlaceholder(PatternMatching.Pattern child)
  41. {
  42. this.child = child;
  43. }
  44. public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
  45. {
  46. return visitor.VisitPatternPlaceholder(this, child, data);
  47. }
  48. protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
  49. {
  50. return child.DoMatch(other, match);
  51. }
  52. bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo)
  53. {
  54. return child.DoMatchCollection(role, pos, match, backtrackingInfo);
  55. }
  56. }
  57. #endregion
  58. public AstNodeCollection<Statement> Statements {
  59. get { return GetChildrenByRole (StatementRole); }
  60. }
  61. public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
  62. {
  63. return visitor.VisitBlockStatement (this, data);
  64. }
  65. protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
  66. {
  67. BlockStatement o = other as BlockStatement;
  68. return o != null && !(o is CatchBlock) && !o.IsNull && this.Statements.DoMatch(o.Statements, match);
  69. }
  70. #region Builder methods
  71. public void Add(Statement statement)
  72. {
  73. AddChild(statement, StatementRole);
  74. }
  75. public void Add(Expression expression)
  76. {
  77. AddChild(new ExpressionStatement { Expression = expression }, StatementRole);
  78. }
  79. public void AddRange(IEnumerable<Statement> statements)
  80. {
  81. foreach (Statement st in statements)
  82. AddChild(st, StatementRole);
  83. }
  84. public void AddAssignment(Expression left, Expression right)
  85. {
  86. Add(new AssignmentExpression(left, AssignmentOperatorType.Assign, right));
  87. }
  88. public void AddReturnStatement(Expression expression)
  89. {
  90. Add(new ReturnStatement { Expression = expression });
  91. }
  92. #endregion
  93. IEnumerator<Statement> IEnumerable<Statement>.GetEnumerator()
  94. {
  95. return this.Statements.GetEnumerator();
  96. }
  97. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  98. {
  99. return this.Statements.GetEnumerator();
  100. }
  101. }
  102. }