// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // This code is distributed under MIT X11 license (for details please see \doc\license.txt) using System; using System.Collections.Generic; using System.Linq; namespace ICSharpCode.NRefactory.CSharp { public class QueryExpression : Expression { public static readonly Role ClauseRole = new Role("Clause"); #region Null public new static readonly QueryExpression Null = new NullQueryExpression (); sealed class NullQueryExpression : QueryExpression { public override bool IsNull { get { return true; } } public override S AcceptVisitor (IAstVisitor visitor, T data) { return default (S); } protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) { return other == null || other.IsNull; } } #endregion public AstNodeCollection Clauses { get { return GetChildrenByRole(ClauseRole); } } public override S AcceptVisitor(IAstVisitor visitor, T data) { return visitor.VisitQueryExpression (this, data); } protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) { QueryExpression o = other as QueryExpression; return o != null && this.Clauses.DoMatch(o.Clauses, match); } } public abstract class QueryClause : AstNode { public override NodeType NodeType { get { return NodeType.QueryClause; } } protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) { QueryClause o = other as QueryClause; throw new NotImplementedException(); } } /// /// Represents a query continuation. /// "(from .. select ..) into Identifier" or "(from .. group .. by ..) into Identifier" /// Note that "join .. into .." is not a query continuation! /// /// This is always the first(!!) clause in a query expression. /// The tree for "from a in b select c into d select e" looks like this: /// new QueryExpression { /// new QueryContinuationClause { /// PrecedingQuery = new QueryExpression { /// new QueryFromClause(a in b), /// new QuerySelectClause(c) /// }, /// Identifier = d /// }, /// new QuerySelectClause(e) /// } /// public class QueryContinuationClause : QueryClause { public static readonly Role PrecedingQueryRole = new Role("PrecedingQuery", QueryExpression.Null); public static readonly Role IntoKeywordRole = Roles.Keyword; public QueryExpression PrecedingQuery { get { return GetChildByRole(PrecedingQueryRole); } set { SetChildByRole(PrecedingQueryRole, value); } } public string Identifier { get { return GetChildByRole (Roles.Identifier).Name; } set { SetChildByRole(Roles.Identifier, new Identifier(value, AstLocation.Empty)); } } public override S AcceptVisitor (IAstVisitor visitor, T data) { return visitor.VisitQueryContinuationClause (this, data); } } public class QueryFromClause : QueryClause { public static readonly Role FromKeywordRole = Roles.Keyword; public static readonly Role InKeywordRole = Roles.InKeyword; public AstType Type { get { return GetChildByRole (Roles.Type); } set { SetChildByRole (Roles.Type, value); } } public string Identifier { get { return GetChildByRole (Roles.Identifier).Name; } set { SetChildByRole(Roles.Identifier, new Identifier(value, AstLocation.Empty)); } } public Expression Expression { get { return GetChildByRole (Roles.Expression); } set { SetChildByRole (Roles.Expression, value); } } public override S AcceptVisitor (IAstVisitor visitor, T data) { return visitor.VisitQueryFromClause (this, data); } } public class QueryLetClause : QueryClause { public CSharpTokenNode LetKeyword { get { return GetChildByRole(Roles.Keyword); } } public string Identifier { get { return GetChildByRole(Roles.Identifier).Name; } set { SetChildByRole(Roles.Identifier, new Identifier(value, AstLocation.Empty)); } } public CSharpTokenNode AssignToken { get { return GetChildByRole(Roles.Assign); } } public Expression Expression { get { return GetChildByRole(Roles.Expression); } set { SetChildByRole(Roles.Expression, value); } } public override S AcceptVisitor (IAstVisitor visitor, T data) { return visitor.VisitQueryLetClause (this, data); } } public class QueryWhereClause : QueryClause { public CSharpTokenNode WhereKeyword { get { return GetChildByRole (Roles.Keyword); } } public Expression Condition { get { return GetChildByRole (Roles.Condition); } set { SetChildByRole (Roles.Condition, value); } } public override S AcceptVisitor (IAstVisitor visitor, T data) { return visitor.VisitQueryWhereClause (this, data); } } /// /// Represents a join or group join clause. /// public class QueryJoinClause : QueryClause { public static readonly Role JoinKeywordRole = Roles.Keyword; public static readonly Role TypeRole = Roles.Type; public static readonly Role JoinIdentifierRole = Roles.Identifier; public static readonly Role InKeywordRole = Roles.InKeyword; public static readonly Role InExpressionRole = Roles.Expression; public static readonly Role OnKeywordRole = new Role("OnKeyword", CSharpTokenNode.Null); public static readonly Role OnExpressionRole = new Role("OnExpression", Expression.Null); public static readonly Role EqualsKeywordRole = new Role("EqualsKeyword", CSharpTokenNode.Null); public static readonly Role EqualsExpressionRole = new Role("EqualsExpression", Expression.Null); public static readonly Role IntoKeywordRole = new Role("IntoKeyword", CSharpTokenNode.Null); public static readonly Role IntoIdentifierRole = new Role("IntoIdentifier", Identifier.Null); public bool IsGroupJoin { get { return !string.IsNullOrEmpty(this.IntoIdentifier); } } public CSharpTokenNode JoinKeyword { get { return GetChildByRole (JoinKeywordRole); } } public AstType Type { get { return GetChildByRole (TypeRole); } set { SetChildByRole (TypeRole, value); } } public string JoinIdentifier { get { return GetChildByRole(JoinIdentifierRole).Name; } set { SetChildByRole(JoinIdentifierRole, new Identifier(value, AstLocation.Empty)); } } public CSharpTokenNode InKeyword { get { return GetChildByRole (InKeywordRole); } } public Expression InExpression { get { return GetChildByRole (InExpressionRole); } set { SetChildByRole (InExpressionRole, value); } } public CSharpTokenNode OnKeyword { get { return GetChildByRole (OnKeywordRole); } } public Expression OnExpression { get { return GetChildByRole (OnExpressionRole); } set { SetChildByRole (OnExpressionRole, value); } } public CSharpTokenNode EqualsKeyword { get { return GetChildByRole (EqualsKeywordRole); } } public Expression EqualsExpression { get { return GetChildByRole (EqualsExpressionRole); } set { SetChildByRole (EqualsExpressionRole, value); } } public CSharpTokenNode IntoKeyword { get { return GetChildByRole (IntoKeywordRole); } } public string IntoIdentifier { get { return GetChildByRole (IntoIdentifierRole).Name; } set { SetChildByRole(IntoIdentifierRole, new Identifier(value, AstLocation.Empty)); } } public override S AcceptVisitor (IAstVisitor visitor, T data) { return visitor.VisitQueryJoinClause (this, data); } } public class QueryOrderClause : QueryClause { public static readonly Role OrderingRole = new Role("Ordering"); public CSharpTokenNode Keyword { get { return GetChildByRole (Roles.Keyword); } } public AstNodeCollection Orderings { get { return GetChildrenByRole (OrderingRole); } } public override S AcceptVisitor (IAstVisitor visitor, T data) { return visitor.VisitQueryOrderClause (this, data); } } public class QueryOrdering : AstNode { public override NodeType NodeType { get { return NodeType.Unknown; } } public Expression Expression { get { return GetChildByRole (Roles.Expression); } set { SetChildByRole (Roles.Expression, value); } } public QueryOrderingDirection Direction { get; set; } public CSharpTokenNode DirectionToken { get { return GetChildByRole (Roles.Keyword); } } public override S AcceptVisitor (IAstVisitor visitor, T data) { return visitor.VisitQueryOrdering (this, data); } protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) { QueryOrdering o = other as QueryOrdering; return o != null && this.Direction == o.Direction && this.Expression.DoMatch(o.Expression, match); } } public enum QueryOrderingDirection { None, Ascending, Descending } public class QuerySelectClause : QueryClause { public CSharpTokenNode SelectKeyword { get { return GetChildByRole (Roles.Keyword); } } public Expression Expression { get { return GetChildByRole (Roles.Expression); } set { SetChildByRole (Roles.Expression, value); } } public override S AcceptVisitor (IAstVisitor visitor, T data) { return visitor.VisitQuerySelectClause (this, data); } } public class QueryGroupClause : QueryClause { public static readonly Role GroupKeywordRole = Roles.Keyword; public static readonly Role ProjectionRole = new Role("Projection", Expression.Null); public static readonly Role ByKeywordRole = new Role("ByKeyword", CSharpTokenNode.Null); public static readonly Role KeyRole = new Role("Key", Expression.Null); public CSharpTokenNode GroupKeyword { get { return GetChildByRole (GroupKeywordRole); } } public Expression Projection { get { return GetChildByRole (ProjectionRole); } set { SetChildByRole (ProjectionRole, value); } } public CSharpTokenNode ByKeyword { get { return GetChildByRole (ByKeywordRole); } } public Expression Key { get { return GetChildByRole (KeyRole); } set { SetChildByRole (KeyRole, value); } } public override S AcceptVisitor (IAstVisitor visitor, T data) { return visitor.VisitQueryGroupClause (this, data); } } }