/NRefactory/ICSharpCode.NRefactory.VB/Ast/Expressions/LambdaExpression.cs

http://github.com/icsharpcode/ILSpy · C# · 124 lines · 100 code · 19 blank · 5 comment · 10 complexity · 12a0bf52d4cfb72c4a6f3a611e255da2 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. using System.Linq;
  6. namespace ICSharpCode.NRefactory.VB.Ast
  7. {
  8. public abstract class LambdaExpression : Expression
  9. {
  10. public static readonly Role<VBModifierToken> ModifierRole = AttributedNode.ModifierRole;
  11. public LambdaExpressionModifiers Modifiers {
  12. get { return GetModifiers(this); }
  13. set { SetModifiers(this, value); }
  14. }
  15. public AstNodeCollection<VBModifierToken> ModifierTokens {
  16. get { return GetChildrenByRole (ModifierRole); }
  17. }
  18. internal static LambdaExpressionModifiers GetModifiers(AstNode node)
  19. {
  20. LambdaExpressionModifiers m = 0;
  21. foreach (VBModifierToken t in node.GetChildrenByRole (ModifierRole)) {
  22. m |= (LambdaExpressionModifiers)t.Modifier;
  23. }
  24. return m;
  25. }
  26. internal static void SetModifiers(AstNode node, LambdaExpressionModifiers newValue)
  27. {
  28. LambdaExpressionModifiers oldValue = GetModifiers(node);
  29. AstNode insertionPos = null;
  30. foreach (Modifiers m in VBModifierToken.AllModifiers) {
  31. if ((m & (Modifiers)newValue) != 0) {
  32. if ((m & (Modifiers)oldValue) == 0) {
  33. // Modifier was added
  34. var newToken = new VBModifierToken(TextLocation.Empty, m);
  35. node.InsertChildAfter(insertionPos, newToken, ModifierRole);
  36. insertionPos = newToken;
  37. } else {
  38. // Modifier already exists
  39. insertionPos = node.GetChildrenByRole(ModifierRole).First(t => t.Modifier == m);
  40. }
  41. } else {
  42. if ((m & (Modifiers)oldValue) != 0) {
  43. // Modifier was removed
  44. node.GetChildrenByRole (ModifierRole).First(t => t.Modifier == m).Remove();
  45. }
  46. }
  47. }
  48. }
  49. public AstNodeCollection<ParameterDeclaration> Parameters {
  50. get { return GetChildrenByRole(Roles.Parameter); }
  51. }
  52. }
  53. public class SingleLineSubLambdaExpression : LambdaExpression
  54. {
  55. public static readonly Role<Statement> StatementRole = BlockStatement.StatementRole;
  56. public Statement EmbeddedStatement {
  57. get { return GetChildByRole(StatementRole); }
  58. set { SetChildByRole(StatementRole, value); }
  59. }
  60. protected internal override bool DoMatch(AstNode other, ICSharpCode.NRefactory.PatternMatching.Match match)
  61. {
  62. throw new NotImplementedException();
  63. }
  64. public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
  65. {
  66. return visitor.VisitSingleLineSubLambdaExpression(this, data);
  67. }
  68. }
  69. public class SingleLineFunctionLambdaExpression : LambdaExpression
  70. {
  71. public Expression EmbeddedExpression {
  72. get { return GetChildByRole(Roles.Expression); }
  73. set { SetChildByRole(Roles.Expression, value); }
  74. }
  75. protected internal override bool DoMatch(AstNode other, ICSharpCode.NRefactory.PatternMatching.Match match)
  76. {
  77. throw new NotImplementedException();
  78. }
  79. public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
  80. {
  81. return visitor.VisitSingleLineFunctionLambdaExpression(this, data);
  82. }
  83. }
  84. public class MultiLineLambdaExpression : LambdaExpression
  85. {
  86. public bool IsSub { get; set; }
  87. public BlockStatement Body {
  88. get { return GetChildByRole(Roles.Body); }
  89. set { SetChildByRole(Roles.Body, value); }
  90. }
  91. protected internal override bool DoMatch(AstNode other, ICSharpCode.NRefactory.PatternMatching.Match match)
  92. {
  93. throw new NotImplementedException();
  94. }
  95. public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
  96. {
  97. return visitor.VisitMultiLineLambdaExpression(this, data);
  98. }
  99. }
  100. public enum LambdaExpressionModifiers
  101. {
  102. Async = Modifiers.Async,
  103. Iterator = Modifiers.Iterator
  104. }
  105. }