Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.11/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.CSharp-5.5.0/Ast/Expressions/QueryExpression.cs @ 12011

Last change on this file since 12011 was 11700, checked in by jkarder, 9 years ago

#2077: created branch and added first version

File size: 19.4 KB
Line 
1// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
2//
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:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
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.
18
19using System;
20using System.Collections.Generic;
21using System.Linq;
22namespace ICSharpCode.NRefactory.CSharp
23{
24  public class QueryExpression : Expression
25  {
26    public static readonly Role<QueryClause> ClauseRole = new Role<QueryClause>("Clause");
27   
28    #region Null
29    public new static readonly QueryExpression Null = new NullQueryExpression ();
30   
31    sealed class NullQueryExpression : QueryExpression
32    {
33      public override bool IsNull {
34        get {
35          return true;
36        }
37      }
38     
39      public override void AcceptVisitor (IAstVisitor visitor)
40      {
41        visitor.VisitNullNode(this);
42      }
43     
44      public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
45      {
46        return visitor.VisitNullNode(this);
47      }
48     
49      public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
50      {
51        return visitor.VisitNullNode(this, data);
52      }
53     
54      protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
55      {
56        return other == null || other.IsNull;
57      }
58    }
59    #endregion
60   
61    public AstNodeCollection<QueryClause> Clauses {
62      get { return GetChildrenByRole(ClauseRole); }
63    }
64   
65    public override void AcceptVisitor (IAstVisitor visitor)
66    {
67      visitor.VisitQueryExpression (this);
68    }
69     
70    public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
71    {
72      return visitor.VisitQueryExpression (this);
73    }
74   
75    public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
76    {
77      return visitor.VisitQueryExpression (this, data);
78    }
79   
80    protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
81    {
82      QueryExpression o = other as QueryExpression;
83      return o != null && !o.IsNull && this.Clauses.DoMatch(o.Clauses, match);
84    }
85
86    #region Builder methods
87    public override MemberReferenceExpression Member(string memberName)
88    {
89      return new MemberReferenceExpression { Target = this, MemberName = memberName };
90    }
91
92    public override IndexerExpression Indexer(IEnumerable<Expression> arguments)
93    {
94      IndexerExpression expr = new IndexerExpression();
95      expr.Target = new ParenthesizedExpression(this);
96      expr.Arguments.AddRange(arguments);
97      return expr;
98    }
99
100    public override IndexerExpression Indexer(params Expression[] arguments)
101    {
102      IndexerExpression expr = new IndexerExpression();
103      expr.Target = new ParenthesizedExpression(this);
104      expr.Arguments.AddRange(arguments);
105      return expr;
106    }
107
108    public override InvocationExpression Invoke(string methodName, IEnumerable<AstType> typeArguments, IEnumerable<Expression> arguments)
109    {
110      InvocationExpression ie = new InvocationExpression();
111      MemberReferenceExpression mre = new MemberReferenceExpression();
112      mre.Target = new ParenthesizedExpression(this);
113      mre.MemberName = methodName;
114      mre.TypeArguments.AddRange(typeArguments);
115      ie.Target = mre;
116      ie.Arguments.AddRange(arguments);
117      return ie;
118    }
119
120    public override InvocationExpression Invoke(IEnumerable<Expression> arguments)
121    {
122      InvocationExpression ie = new InvocationExpression();
123      ie.Target = new ParenthesizedExpression(this);
124      ie.Arguments.AddRange(arguments);
125      return ie;
126    }
127
128    public override InvocationExpression Invoke(params Expression[] arguments)
129    {
130      InvocationExpression ie = new InvocationExpression();
131      ie.Target = new ParenthesizedExpression(this);
132      ie.Arguments.AddRange(arguments);
133      return ie;
134    }
135
136    public override CastExpression CastTo(AstType type)
137    {
138      return new CastExpression { Type = type,  Expression = new ParenthesizedExpression(this) };
139    }
140
141    public override AsExpression CastAs(AstType type)
142    {
143      return new AsExpression { Type = type,  Expression = new ParenthesizedExpression(this) };
144    }
145
146    public override IsExpression IsType(AstType type)
147    {
148      return new IsExpression { Type = type,  Expression = new ParenthesizedExpression(this) };
149    }
150    #endregion
151  }
152 
153  public abstract class QueryClause : AstNode
154  {
155    public override NodeType NodeType {
156      get { return NodeType.QueryClause; }
157    }
158  }
159 
160  /// <summary>
161  /// Represents a query continuation.
162  /// "(from .. select ..) into Identifier" or "(from .. group .. by ..) into Identifier"
163  /// Note that "join .. into .." is not a query continuation!
164  ///
165  /// This is always the first(!!) clause in a query expression.
166  /// The tree for "from a in b select c into d select e" looks like this:
167  /// new QueryExpression {
168  ///   new QueryContinuationClause {
169  ///     PrecedingQuery = new QueryExpression {
170  ///       new QueryFromClause(a in b),
171  ///       new QuerySelectClause(c)
172  ///     },
173  ///     Identifier = d
174  ///   },
175  ///   new QuerySelectClause(e)
176  /// }
177  /// </summary>
178  public class QueryContinuationClause : QueryClause
179  {
180    public static readonly Role<QueryExpression> PrecedingQueryRole = new Role<QueryExpression>("PrecedingQuery", QueryExpression.Null);
181    public static readonly TokenRole IntoKeywordRole = new TokenRole ("into");
182   
183    public QueryExpression PrecedingQuery {
184      get { return GetChildByRole(PrecedingQueryRole); }
185      set { SetChildByRole(PrecedingQueryRole, value); }
186    }
187   
188    public CSharpTokenNode IntoKeyword {
189      get { return GetChildByRole (IntoKeywordRole); }
190    }
191
192    public string Identifier {
193      get {
194        return GetChildByRole (Roles.Identifier).Name;
195      }
196      set {
197        SetChildByRole(Roles.Identifier, CSharp.Identifier.Create (value));
198      }
199    }
200   
201    public Identifier IdentifierToken {
202      get { return GetChildByRole (Roles.Identifier); }
203    }
204   
205    public override void AcceptVisitor (IAstVisitor visitor)
206    {
207      visitor.VisitQueryContinuationClause (this);
208    }
209     
210    public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
211    {
212      return visitor.VisitQueryContinuationClause (this);
213    }
214   
215    public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
216    {
217      return visitor.VisitQueryContinuationClause (this, data);
218    }
219   
220    protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
221    {
222      QueryContinuationClause o = other as QueryContinuationClause;
223      return o != null && MatchString(this.Identifier, o.Identifier) && this.PrecedingQuery.DoMatch(o.PrecedingQuery, match);
224    }
225  }
226 
227  public class QueryFromClause : QueryClause
228  {
229    public static readonly TokenRole FromKeywordRole =  new TokenRole ("from");
230    public static readonly TokenRole InKeywordRole =  new TokenRole ("in");
231   
232    public CSharpTokenNode FromKeyword {
233      get { return GetChildByRole (FromKeywordRole); }
234    }
235
236    public AstType Type {
237      get { return GetChildByRole (Roles.Type); }
238      set { SetChildByRole (Roles.Type, value); }
239    }
240   
241    public string Identifier {
242      get {
243        return GetChildByRole (Roles.Identifier).Name;
244      }
245      set {
246        SetChildByRole(Roles.Identifier, CSharp.Identifier.Create (value));
247      }
248    }
249   
250    public Identifier IdentifierToken {
251      get { return GetChildByRole(Roles.Identifier); }
252    }
253   
254    public CSharpTokenNode InKeyword {
255      get { return GetChildByRole (InKeywordRole); }
256    }
257
258    public Expression Expression {
259      get { return GetChildByRole (Roles.Expression); }
260      set { SetChildByRole (Roles.Expression, value); }
261    }
262   
263    public override void AcceptVisitor (IAstVisitor visitor)
264    {
265      visitor.VisitQueryFromClause (this);
266    }
267     
268    public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
269    {
270      return visitor.VisitQueryFromClause (this);
271    }
272   
273    public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
274    {
275      return visitor.VisitQueryFromClause (this, data);
276    }
277   
278    protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
279    {
280      QueryFromClause o = other as QueryFromClause;
281      return o != null && this.Type.DoMatch(o.Type, match) && MatchString(this.Identifier, o.Identifier)
282        && this.Expression.DoMatch(o.Expression, match);
283    }
284  }
285 
286  public class QueryLetClause : QueryClause
287  {
288    public readonly static TokenRole LetKeywordRole = new TokenRole ("let");
289   
290    public CSharpTokenNode LetKeyword {
291      get { return GetChildByRole(LetKeywordRole); }
292    }
293   
294    public string Identifier {
295      get {
296        return GetChildByRole(Roles.Identifier).Name;
297      }
298      set {
299        SetChildByRole(Roles.Identifier, CSharp.Identifier.Create (value));
300      }
301    }
302   
303    public Identifier IdentifierToken {
304      get { return GetChildByRole(Roles.Identifier); }
305    }
306   
307    public CSharpTokenNode AssignToken {
308      get { return GetChildByRole(Roles.Assign); }
309    }
310   
311    public Expression Expression {
312      get { return GetChildByRole(Roles.Expression); }
313      set { SetChildByRole(Roles.Expression, value); }
314    }
315   
316    public override void AcceptVisitor (IAstVisitor visitor)
317    {
318      visitor.VisitQueryLetClause (this);
319    }
320     
321    public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
322    {
323      return visitor.VisitQueryLetClause (this);
324    }
325   
326    public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
327    {
328      return visitor.VisitQueryLetClause (this, data);
329    }
330   
331    protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
332    {
333      QueryLetClause o = other as QueryLetClause;
334      return o != null && MatchString(this.Identifier, o.Identifier) && this.Expression.DoMatch(o.Expression, match);
335    }
336  }
337 
338 
339  public class QueryWhereClause : QueryClause
340  {
341    public readonly static TokenRole WhereKeywordRole = new TokenRole ("where");
342
343    public CSharpTokenNode WhereKeyword {
344      get { return GetChildByRole (WhereKeywordRole); }
345    }
346   
347    public Expression Condition {
348      get { return GetChildByRole (Roles.Condition); }
349      set { SetChildByRole (Roles.Condition, value); }
350    }
351   
352    public override void AcceptVisitor (IAstVisitor visitor)
353    {
354      visitor.VisitQueryWhereClause (this);
355    }
356     
357    public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
358    {
359      return visitor.VisitQueryWhereClause (this);
360    }
361   
362    public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
363    {
364      return visitor.VisitQueryWhereClause (this, data);
365    }
366   
367    protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
368    {
369      QueryWhereClause o = other as QueryWhereClause;
370      return o != null && this.Condition.DoMatch(o.Condition, match);
371    }
372  }
373 
374  /// <summary>
375  /// Represents a join or group join clause.
376  /// </summary>
377  public class QueryJoinClause : QueryClause
378  {
379    public static readonly TokenRole JoinKeywordRole = new TokenRole ("join");
380    public static readonly Role<AstType> TypeRole = Roles.Type;
381    public static readonly Role<Identifier> JoinIdentifierRole = Roles.Identifier;
382    public static readonly TokenRole InKeywordRole =  new TokenRole ("in");
383    public static readonly Role<Expression> InExpressionRole = Roles.Expression;
384    public static readonly TokenRole OnKeywordRole =  new TokenRole ("on");
385    public static readonly Role<Expression> OnExpressionRole = new Role<Expression>("OnExpression", Expression.Null);
386    public static readonly TokenRole EqualsKeywordRole =  new TokenRole ("equals");
387    public static readonly Role<Expression> EqualsExpressionRole = new Role<Expression>("EqualsExpression", Expression.Null);
388    public static readonly TokenRole IntoKeywordRole =  new TokenRole ("into");
389    public static readonly Role<Identifier> IntoIdentifierRole = new Role<Identifier>("IntoIdentifier", Identifier.Null);
390   
391    public bool IsGroupJoin {
392      get { return !string.IsNullOrEmpty(this.IntoIdentifier); }
393    }
394   
395    public CSharpTokenNode JoinKeyword {
396      get { return GetChildByRole (JoinKeywordRole); }
397    }
398   
399    public AstType Type {
400      get { return GetChildByRole (TypeRole); }
401      set { SetChildByRole (TypeRole, value); }
402    }
403   
404    public string JoinIdentifier {
405      get {
406        return GetChildByRole(JoinIdentifierRole).Name;
407      }
408      set {
409        SetChildByRole(JoinIdentifierRole, Identifier.Create (value));
410      }
411    }
412   
413    public Identifier JoinIdentifierToken {
414      get { return GetChildByRole(JoinIdentifierRole); }
415    }
416   
417    public CSharpTokenNode InKeyword {
418      get { return GetChildByRole (InKeywordRole); }
419    }
420   
421    public Expression InExpression {
422      get { return GetChildByRole (InExpressionRole); }
423      set { SetChildByRole (InExpressionRole, value); }
424    }
425   
426    public CSharpTokenNode OnKeyword {
427      get { return GetChildByRole (OnKeywordRole); }
428    }
429   
430    public Expression OnExpression {
431      get { return GetChildByRole (OnExpressionRole); }
432      set { SetChildByRole (OnExpressionRole, value); }
433    }
434   
435    public CSharpTokenNode EqualsKeyword {
436      get { return GetChildByRole (EqualsKeywordRole); }
437    }
438   
439    public Expression EqualsExpression {
440      get { return GetChildByRole (EqualsExpressionRole); }
441      set { SetChildByRole (EqualsExpressionRole, value); }
442    }
443   
444    public CSharpTokenNode IntoKeyword {
445      get { return GetChildByRole (IntoKeywordRole); }
446    }
447   
448    public string IntoIdentifier {
449      get {
450        return GetChildByRole (IntoIdentifierRole).Name;
451      }
452      set {
453        SetChildByRole(IntoIdentifierRole, Identifier.Create (value));
454      }
455    }
456   
457    public Identifier IntoIdentifierToken {
458      get { return GetChildByRole(IntoIdentifierRole); }
459    }
460   
461    public override void AcceptVisitor (IAstVisitor visitor)
462    {
463      visitor.VisitQueryJoinClause (this);
464    }
465     
466    public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
467    {
468      return visitor.VisitQueryJoinClause (this);
469    }
470   
471    public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
472    {
473      return visitor.VisitQueryJoinClause (this, data);
474    }
475   
476    protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
477    {
478      QueryJoinClause o = other as QueryJoinClause;
479      return o != null && this.IsGroupJoin == o.IsGroupJoin
480        && this.Type.DoMatch(o.Type, match) && MatchString(this.JoinIdentifier, o.JoinIdentifier)
481        && this.InExpression.DoMatch(o.InExpression, match) && this.OnExpression.DoMatch(o.OnExpression, match)
482        && this.EqualsExpression.DoMatch(o.EqualsExpression, match)
483        && MatchString(this.IntoIdentifier, o.IntoIdentifier);
484    }
485  }
486 
487  public class QueryOrderClause : QueryClause
488  {
489    public static readonly TokenRole OrderbyKeywordRole = new TokenRole ("orderby");
490    public static readonly Role<QueryOrdering> OrderingRole = new Role<QueryOrdering>("Ordering");
491   
492    public CSharpTokenNode OrderbyToken {
493      get { return GetChildByRole (OrderbyKeywordRole); }
494    }
495   
496    public AstNodeCollection<QueryOrdering> Orderings {
497      get { return GetChildrenByRole (OrderingRole); }
498    }
499   
500    public override void AcceptVisitor (IAstVisitor visitor)
501    {
502      visitor.VisitQueryOrderClause (this);
503    }
504     
505    public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
506    {
507      return visitor.VisitQueryOrderClause (this);
508    }
509   
510    public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
511    {
512      return visitor.VisitQueryOrderClause (this, data);
513    }
514   
515    protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
516    {
517      QueryOrderClause o = other as QueryOrderClause;
518      return o != null && this.Orderings.DoMatch(o.Orderings, match);
519    }
520  }
521 
522  public class QueryOrdering : AstNode
523  {
524    public readonly static TokenRole AscendingKeywordRole = new TokenRole ("ascending");
525    public readonly static TokenRole DescendingKeywordRole = new TokenRole ("descending");
526
527    public override NodeType NodeType {
528      get { return NodeType.Unknown; }
529    }
530   
531    public Expression Expression {
532      get { return GetChildByRole (Roles.Expression); }
533      set { SetChildByRole (Roles.Expression, value); }
534    }
535   
536    public QueryOrderingDirection Direction {
537      get;
538      set;
539    }
540   
541    public CSharpTokenNode DirectionToken {
542      get { return Direction == QueryOrderingDirection.Ascending ? GetChildByRole (AscendingKeywordRole) : GetChildByRole (DescendingKeywordRole); }
543    }
544   
545    public override void AcceptVisitor (IAstVisitor visitor)
546    {
547      visitor.VisitQueryOrdering (this);
548    }
549     
550    public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
551    {
552      return visitor.VisitQueryOrdering (this);
553    }
554   
555    public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
556    {
557      return visitor.VisitQueryOrdering (this, data);
558    }
559   
560    protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
561    {
562      QueryOrdering o = other as QueryOrdering;
563      return o != null && this.Direction == o.Direction && this.Expression.DoMatch(o.Expression, match);
564    }
565  }
566 
567  public enum QueryOrderingDirection
568  {
569    None,
570    Ascending,
571    Descending
572  }
573 
574  public class QuerySelectClause : QueryClause
575  {
576    public readonly static TokenRole SelectKeywordRole = new TokenRole ("select");
577
578    public CSharpTokenNode SelectKeyword {
579      get { return GetChildByRole (SelectKeywordRole); }
580    }
581   
582    public Expression Expression {
583      get { return GetChildByRole (Roles.Expression); }
584      set { SetChildByRole (Roles.Expression, value); }
585    }
586   
587    public override void AcceptVisitor (IAstVisitor visitor)
588    {
589      visitor.VisitQuerySelectClause (this);
590    }
591     
592    public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
593    {
594      return visitor.VisitQuerySelectClause (this);
595    }
596   
597    public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
598    {
599      return visitor.VisitQuerySelectClause (this, data);
600    }
601   
602    protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
603    {
604      QuerySelectClause o = other as QuerySelectClause;
605      return o != null && this.Expression.DoMatch(o.Expression, match);
606    }
607  }
608 
609  public class QueryGroupClause : QueryClause
610  {
611    public static readonly TokenRole GroupKeywordRole = new TokenRole ("group");
612    public static readonly Role<Expression> ProjectionRole = new Role<Expression>("Projection", Expression.Null);
613    public static readonly TokenRole ByKeywordRole = new TokenRole ("by");
614    public static readonly Role<Expression> KeyRole = new Role<Expression>("Key", Expression.Null);
615   
616    public CSharpTokenNode GroupKeyword {
617      get { return GetChildByRole (GroupKeywordRole); }
618    }
619   
620    public Expression Projection {
621      get { return GetChildByRole (ProjectionRole); }
622      set { SetChildByRole (ProjectionRole, value); }
623    }
624   
625    public CSharpTokenNode ByKeyword {
626      get { return GetChildByRole (ByKeywordRole); }
627    }
628   
629    public Expression Key {
630      get { return GetChildByRole (KeyRole); }
631      set { SetChildByRole (KeyRole, value); }
632    }
633   
634    public override void AcceptVisitor (IAstVisitor visitor)
635    {
636      visitor.VisitQueryGroupClause (this);
637    }
638     
639    public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
640    {
641      return visitor.VisitQueryGroupClause (this);
642    }
643   
644    public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
645    {
646      return visitor.VisitQueryGroupClause (this, data);
647    }
648   
649    protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
650    {
651      QueryGroupClause o = other as QueryGroupClause;
652      return o != null && this.Projection.DoMatch(o.Projection, match) && this.Key.DoMatch(o.Key, match);
653    }
654  }
655}
Note: See TracBrowser for help on using the repository browser.