PageRenderTime 24ms CodeModel.GetById 18ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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