/NRefactory/ICSharpCode.NRefactory.CSharp/Ast/Expressions/AnonymousMethodExpression.cs

http://github.com/icsharpcode/ILSpy · C# · 117 lines · 70 code · 18 blank · 29 comment · 10 complexity · 68de3dab6164881e19e7b0056a621347 MD5 · raw file

  1. //
  2. // AnonymousMethodExpression.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. /// [async] delegate(Parameters) {Body}
  32. /// </summary>
  33. public class AnonymousMethodExpression : Expression
  34. {
  35. public readonly static TokenRole DelegateKeywordRole = new TokenRole ("delegate");
  36. public readonly static TokenRole AsyncModifierRole = LambdaExpression.AsyncModifierRole;
  37. bool isAsync;
  38. public bool IsAsync {
  39. get { return isAsync; }
  40. set { ThrowIfFrozen(); isAsync = value; }
  41. }
  42. // used to tell the difference between delegate {} and delegate () {}
  43. bool hasParameterList;
  44. public bool HasParameterList {
  45. get { return hasParameterList || Parameters.Any(); }
  46. set { ThrowIfFrozen(); hasParameterList = value; }
  47. }
  48. public CSharpTokenNode DelegateToken {
  49. get { return GetChildByRole (DelegateKeywordRole); }
  50. }
  51. public CSharpTokenNode LParToken {
  52. get { return GetChildByRole (Roles.LPar); }
  53. }
  54. public AstNodeCollection<ParameterDeclaration> Parameters {
  55. get { return GetChildrenByRole (Roles.Parameter); }
  56. }
  57. public CSharpTokenNode RParToken {
  58. get { return GetChildByRole (Roles.RPar); }
  59. }
  60. public BlockStatement Body {
  61. get { return GetChildByRole (Roles.Body); }
  62. set { SetChildByRole (Roles.Body, value); }
  63. }
  64. public AnonymousMethodExpression ()
  65. {
  66. }
  67. public AnonymousMethodExpression (BlockStatement body, IEnumerable<ParameterDeclaration> parameters = null)
  68. {
  69. if (parameters != null) {
  70. hasParameterList = true;
  71. foreach (var parameter in parameters) {
  72. AddChild (parameter, Roles.Parameter);
  73. }
  74. }
  75. AddChild (body, Roles.Body);
  76. }
  77. public AnonymousMethodExpression (BlockStatement body, params ParameterDeclaration[] parameters) : this (body, (IEnumerable<ParameterDeclaration>)parameters)
  78. {
  79. }
  80. public override void AcceptVisitor (IAstVisitor visitor)
  81. {
  82. visitor.VisitAnonymousMethodExpression (this);
  83. }
  84. public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
  85. {
  86. return visitor.VisitAnonymousMethodExpression (this);
  87. }
  88. public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
  89. {
  90. return visitor.VisitAnonymousMethodExpression (this, data);
  91. }
  92. protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
  93. {
  94. AnonymousMethodExpression o = other as AnonymousMethodExpression;
  95. return o != null && this.IsAsync == o.IsAsync && this.HasParameterList == o.HasParameterList
  96. && this.Parameters.DoMatch(o.Parameters, match) && this.Body.DoMatch(o.Body, match);
  97. }
  98. }
  99. }