1
ļ»æ// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
// software and associated documentation files (the "Software"), to deal in the Software
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
// to whom the Software is furnished to do so, subject to the following conditions:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
// DEALINGS IN THE SOFTWARE.
20
using System.Collections.Generic;
22
namespace ICSharpCode.NRefactory.CSharp
24
public class QueryExpression : Expression
26
public static readonly Role<QueryClause> ClauseRole = new Role<QueryClause>("Clause");
29
public new static readonly QueryExpression Null = new NullQueryExpression ();
31
sealed class NullQueryExpression : QueryExpression
33
public override bool IsNull {
39
public override void AcceptVisitor (IAstVisitor visitor)
43
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
48
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
53
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
55
return other == null || other.IsNull;
60
public AstNodeCollection<QueryClause> Clauses {
61
get { return GetChildrenByRole(ClauseRole); }
64
public override void AcceptVisitor (IAstVisitor visitor)
66
visitor.VisitQueryExpression (this);
69
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
71
return visitor.VisitQueryExpression (this);
74
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
76
return visitor.VisitQueryExpression (this, data);
79
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
81
QueryExpression o = other as QueryExpression;
82
return o != null && !o.IsNull && this.Clauses.DoMatch(o.Clauses, match);
86
public abstract class QueryClause : AstNode
88
public override NodeType NodeType {
89
get { return NodeType.QueryClause; }
94
/// Represents a query continuation.
95
/// "(from .. select ..) into Identifier" or "(from .. group .. by ..) into Identifier"
96
/// Note that "join .. into .." is not a query continuation!
98
/// This is always the first(!!) clause in a query expression.
99
/// The tree for "from a in b select c into d select e" looks like this:
100
/// new QueryExpression {
101
/// new QueryContinuationClause {
102
/// PrecedingQuery = new QueryExpression {
103
/// new QueryFromClause(a in b),
104
/// new QuerySelectClause(c)
108
/// new QuerySelectClause(e)
111
public class QueryContinuationClause : QueryClause
113
public static readonly Role<QueryExpression> PrecedingQueryRole = new Role<QueryExpression>("PrecedingQuery", QueryExpression.Null);
114
public static readonly TokenRole IntoKeywordRole = new TokenRole ("into");
116
public QueryExpression PrecedingQuery {
117
get { return GetChildByRole(PrecedingQueryRole); }
118
set { SetChildByRole(PrecedingQueryRole, value); }
121
public CSharpTokenNode IntoKeyword {
122
get { return GetChildByRole (IntoKeywordRole); }
125
public string Identifier {
127
return GetChildByRole (Roles.Identifier).Name;
130
SetChildByRole(Roles.Identifier, CSharp.Identifier.CreateĀ (value));
134
public Identifier IdentifierToken {
135
get { return GetChildByRole (Roles.Identifier); }
138
public override void AcceptVisitor (IAstVisitor visitor)
140
visitor.VisitQueryContinuationClause (this);
143
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
145
return visitor.VisitQueryContinuationClause (this);
148
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
150
return visitor.VisitQueryContinuationClause (this, data);
153
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
155
QueryContinuationClause o = other as QueryContinuationClause;
156
return o != null && MatchString(this.Identifier, o.Identifier) && this.PrecedingQuery.DoMatch(o.PrecedingQuery, match);
160
public class QueryFromClause : QueryClause
162
public static readonly TokenRole FromKeywordRole = new TokenRole ("from");
163
public static readonly TokenRole InKeywordRole = new TokenRole ("in");
165
public CSharpTokenNode FromKeyword {
166
get { return GetChildByRole (FromKeywordRole); }
169
public AstType Type {
170
get { return GetChildByRole (Roles.Type); }
171
set { SetChildByRole (Roles.Type, value); }
174
public string Identifier {
176
return GetChildByRole (Roles.Identifier).Name;
179
SetChildByRole(Roles.Identifier, CSharp.Identifier.CreateĀ (value));
183
public Identifier IdentifierToken {
184
get { return GetChildByRole(Roles.Identifier); }
187
public CSharpTokenNode InKeyword {
188
get { return GetChildByRole (InKeywordRole); }
191
public Expression Expression {
192
get { return GetChildByRole (Roles.Expression); }
193
set { SetChildByRole (Roles.Expression, value); }
196
public override void AcceptVisitor (IAstVisitor visitor)
198
visitor.VisitQueryFromClause (this);
201
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
203
return visitor.VisitQueryFromClause (this);
206
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
208
return visitor.VisitQueryFromClause (this, data);
211
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
213
QueryFromClause o = other as QueryFromClause;
214
return o != null && this.Type.DoMatch(o.Type, match) && MatchString(this.Identifier, o.Identifier)
215
&& this.Expression.DoMatch(o.Expression, match);
219
public class QueryLetClause : QueryClause
221
public readonly static TokenRole LetKeywordRole = new TokenRole ("let");
223
public CSharpTokenNode LetKeyword {
224
get { return GetChildByRole(LetKeywordRole); }
227
public string Identifier {
229
return GetChildByRole(Roles.Identifier).Name;
232
SetChildByRole(Roles.Identifier, CSharp.Identifier.CreateĀ (value));
236
public Identifier IdentifierToken {
237
get { return GetChildByRole(Roles.Identifier); }
240
public CSharpTokenNode AssignToken {
241
get { return GetChildByRole(Roles.Assign); }
244
public Expression Expression {
245
get { return GetChildByRole(Roles.Expression); }
246
set { SetChildByRole(Roles.Expression, value); }
249
public override void AcceptVisitor (IAstVisitor visitor)
251
visitor.VisitQueryLetClause (this);
254
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
256
return visitor.VisitQueryLetClause (this);
259
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
261
return visitor.VisitQueryLetClause (this, data);
264
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
266
QueryLetClause o = other as QueryLetClause;
267
return o != null && MatchString(this.Identifier, o.Identifier) && this.Expression.DoMatch(o.Expression, match);
272
public class QueryWhereClause : QueryClause
274
public readonly static TokenRole WhereKeywordRole = new TokenRole ("where");
276
public CSharpTokenNode WhereKeyword {
277
get { return GetChildByRole (WhereKeywordRole); }
280
public Expression Condition {
281
get { return GetChildByRole (Roles.Condition); }
282
set { SetChildByRole (Roles.Condition, value); }
285
public override void AcceptVisitor (IAstVisitor visitor)
287
visitor.VisitQueryWhereClause (this);
290
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
292
return visitor.VisitQueryWhereClause (this);
295
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
297
return visitor.VisitQueryWhereClause (this, data);
300
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
302
QueryWhereClause o = other as QueryWhereClause;
303
return o != null && this.Condition.DoMatch(o.Condition, match);
308
/// Represents a join or group join clause.
310
public class QueryJoinClause : QueryClause
312
public static readonly TokenRole JoinKeywordRole = new TokenRole ("join");
313
public static readonly Role<AstType> TypeRole = Roles.Type;
314
public static readonly Role<Identifier> JoinIdentifierRole = Roles.Identifier;
315
public static readonly TokenRole InKeywordRole = new TokenRole ("in");
316
public static readonly Role<Expression> InExpressionRole = Roles.Expression;
317
public static readonly TokenRole OnKeywordRole = new TokenRole ("on");
318
public static readonly Role<Expression> OnExpressionRole = new Role<Expression>("OnExpression", Expression.Null);
319
public static readonly TokenRole EqualsKeywordRole = new TokenRole ("equals");
320
public static readonly Role<Expression> EqualsExpressionRole = new Role<Expression>("EqualsExpression", Expression.Null);
321
public static readonly TokenRole IntoKeywordRole = new TokenRole ("into");
322
public static readonly Role<Identifier> IntoIdentifierRole = new Role<Identifier>("IntoIdentifier", Identifier.Null);
324
public bool IsGroupJoin {
325
get { return !string.IsNullOrEmpty(this.IntoIdentifier); }
328
public CSharpTokenNode JoinKeyword {
329
get { return GetChildByRole (JoinKeywordRole); }
332
public AstType Type {
333
get { return GetChildByRole (TypeRole); }
334
set { SetChildByRole (TypeRole, value); }
337
public string JoinIdentifier {
339
return GetChildByRole(JoinIdentifierRole).Name;
342
SetChildByRole(JoinIdentifierRole, Identifier.CreateĀ (value));
346
public Identifier JoinIdentifierToken {
347
get { return GetChildByRole(JoinIdentifierRole); }
350
public CSharpTokenNode InKeyword {
351
get { return GetChildByRole (InKeywordRole); }
354
public Expression InExpression {
355
get { return GetChildByRole (InExpressionRole); }
356
set { SetChildByRole (InExpressionRole, value); }
359
public CSharpTokenNode OnKeyword {
360
get { return GetChildByRole (OnKeywordRole); }
363
public Expression OnExpression {
364
get { return GetChildByRole (OnExpressionRole); }
365
set { SetChildByRole (OnExpressionRole, value); }
368
public CSharpTokenNode EqualsKeyword {
369
get { return GetChildByRole (EqualsKeywordRole); }
372
public Expression EqualsExpression {
373
get { return GetChildByRole (EqualsExpressionRole); }
374
set { SetChildByRole (EqualsExpressionRole, value); }
377
public CSharpTokenNode IntoKeyword {
378
get { return GetChildByRole (IntoKeywordRole); }
381
public string IntoIdentifier {
383
return GetChildByRole (IntoIdentifierRole).Name;
386
SetChildByRole(IntoIdentifierRole, Identifier.CreateĀ (value));
390
public Identifier IntoIdentifierToken {
391
get { return GetChildByRole(IntoIdentifierRole); }
394
public override void AcceptVisitor (IAstVisitor visitor)
396
visitor.VisitQueryJoinClause (this);
399
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
401
return visitor.VisitQueryJoinClause (this);
404
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
406
return visitor.VisitQueryJoinClause (this, data);
409
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
411
QueryJoinClause o = other as QueryJoinClause;
412
return o != null && this.IsGroupJoin == o.IsGroupJoin
413
&& this.Type.DoMatch(o.Type, match) && MatchString(this.JoinIdentifier, o.JoinIdentifier)
414
&& this.InExpression.DoMatch(o.InExpression, match) && this.OnExpression.DoMatch(o.OnExpression, match)
415
&& this.EqualsExpression.DoMatch(o.EqualsExpression, match)
416
&& MatchString(this.IntoIdentifier, o.IntoIdentifier);
420
public class QueryOrderClause : QueryClause
422
public static readonly TokenRole OrderbyKeywordRole = new TokenRole ("orderby");
423
public static readonly Role<QueryOrdering> OrderingRole = new Role<QueryOrdering>("Ordering");
425
public CSharpTokenNode OrderbyToken {
426
get { return GetChildByRole (OrderbyKeywordRole); }
429
public AstNodeCollection<QueryOrdering> Orderings {
430
get { return GetChildrenByRole (OrderingRole); }
433
public override void AcceptVisitor (IAstVisitor visitor)
435
visitor.VisitQueryOrderClause (this);
438
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
440
return visitor.VisitQueryOrderClause (this);
443
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
445
return visitor.VisitQueryOrderClause (this, data);
448
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
450
QueryOrderClause o = other as QueryOrderClause;
451
return o != null && this.Orderings.DoMatch(o.Orderings, match);
455
public class QueryOrdering : AstNode
457
public readonly static TokenRole AscendingKeywordRole = new TokenRole ("ascending");
458
public readonly static TokenRole DescendingKeywordRole = new TokenRole ("descending");
460
public override NodeType NodeType {
461
get { return NodeType.Unknown; }
464
public Expression Expression {
465
get { return GetChildByRole (Roles.Expression); }
466
set { SetChildByRole (Roles.Expression, value); }
469
public QueryOrderingDirection Direction {
474
public CSharpTokenNode DirectionToken {
475
get { return Direction == QueryOrderingDirection.Ascending ? GetChildByRole (AscendingKeywordRole) : GetChildByRole (DescendingKeywordRole); }
478
public override void AcceptVisitor (IAstVisitor visitor)
480
visitor.VisitQueryOrdering (this);
483
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
485
return visitor.VisitQueryOrdering (this);
488
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
490
return visitor.VisitQueryOrdering (this, data);
493
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
495
QueryOrdering o = other as QueryOrdering;
496
return o != null && this.Direction == o.Direction && this.Expression.DoMatch(o.Expression, match);
500
public enum QueryOrderingDirection
507
public class QuerySelectClause : QueryClause
509
public readonly static TokenRole SelectKeywordRole = new TokenRole ("select");
511
public CSharpTokenNode SelectKeyword {
512
get { return GetChildByRole (SelectKeywordRole); }
515
public Expression Expression {
516
get { return GetChildByRole (Roles.Expression); }
517
set { SetChildByRole (Roles.Expression, value); }
520
public override void AcceptVisitor (IAstVisitor visitor)
522
visitor.VisitQuerySelectClause (this);
525
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
527
return visitor.VisitQuerySelectClause (this);
530
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
532
return visitor.VisitQuerySelectClause (this, data);
535
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
537
QuerySelectClause o = other as QuerySelectClause;
538
return o != null && this.Expression.DoMatch(o.Expression, match);
542
public class QueryGroupClause : QueryClause
544
public static readonly TokenRole GroupKeywordRole = new TokenRole ("group");
545
public static readonly Role<Expression> ProjectionRole = new Role<Expression>("Projection", Expression.Null);
546
public static readonly TokenRole ByKeywordRole = new TokenRole ("by");
547
public static readonly Role<Expression> KeyRole = new Role<Expression>("Key", Expression.Null);
549
public CSharpTokenNode GroupKeyword {
550
get { return GetChildByRole (GroupKeywordRole); }
553
public Expression Projection {
554
get { return GetChildByRole (ProjectionRole); }
555
set { SetChildByRole (ProjectionRole, value); }
558
public CSharpTokenNode ByKeyword {
559
get { return GetChildByRole (ByKeywordRole); }
562
public Expression Key {
563
get { return GetChildByRole (KeyRole); }
564
set { SetChildByRole (KeyRole, value); }
567
public override void AcceptVisitor (IAstVisitor visitor)
569
visitor.VisitQueryGroupClause (this);
572
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
574
return visitor.VisitQueryGroupClause (this);
577
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
579
return visitor.VisitQueryGroupClause (this, data);
582
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
584
QueryGroupClause o = other as QueryGroupClause;
585
return o != null && this.Projection.DoMatch(o.Projection, match) && this.Key.DoMatch(o.Key, match);
b'\\ No newline at end of file'