Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.CSharp-5.5.0/Refactoring/PatternHelper.cs

Last change on this file was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 5.3 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 ICSharpCode.NRefactory.PatternMatching;
21using ICSharpCode.NRefactory.TypeSystem;
22
23namespace ICSharpCode.NRefactory.CSharp.Refactoring
24{
25  /// <summary>
26  /// Helper class for constructing pattern ASTs.
27  /// </summary>
28  public class PatternHelper
29  {
30    /// <summary>
31    /// Produces a choice pattern for <c>expr1 op expr2</c> or <c>expr2 op expr1</c>.
32    /// </summary>
33    public static Expression CommutativeOperator(Expression expr1, BinaryOperatorType op, Expression expr2)
34    {
35      return new Choice {
36        new BinaryOperatorExpression(expr1, op, expr2),
37        new BinaryOperatorExpression(expr2.Clone(), op, expr1.Clone())
38      };
39    }
40
41    public static Expression CommutativeOperatorWithOptionalParentheses(Expression expr1, BinaryOperatorType op, Expression expr2)
42    {
43      return OptionalParentheses(CommutativeOperator(OptionalParentheses(expr1), op, OptionalParentheses(expr2)));
44    }
45   
46    /// <summary>
47    /// Optionally allows parentheses around the given expression.
48    /// </summary>
49    public static Expression OptionalParentheses(Expression expr)
50    {
51      return new OptionalParenthesesPattern(expr);
52    }
53   
54    sealed class OptionalParenthesesPattern : Pattern
55    {
56      readonly INode child;
57     
58      public OptionalParenthesesPattern(INode child)
59      {
60        this.child = child;
61      }
62     
63      public override bool DoMatch(INode other, Match match)
64      {
65        INode unpacked = ParenthesizedExpression.UnpackParenthesizedExpression(other as Expression);
66        return child.DoMatch(unpacked, match);
67      }
68    }
69
70    /// <summary>
71    /// Optionally allow a block around the given statement;
72    /// </summary>
73    /// <returns>The statement.</returns>
74    public static Statement EmbeddedStatement(Statement statement)
75    {
76      return new OptionalBlockPattern(statement);
77    }
78
79    sealed class OptionalBlockPattern : Pattern
80    {
81      readonly INode child;
82
83      public OptionalBlockPattern(INode child)
84      {
85        this.child = child;
86      }
87
88      public override bool DoMatch(INode other, Match match)
89      {
90        INode unpacked = UnpackBlockStatement(other as Statement);
91        return child.DoMatch(unpacked, match);
92      }
93
94     
95      /// <summary>
96      /// Unpacks the given expression if it is a ParenthesizedExpression, CheckedExpression or UncheckedExpression.
97      /// </summary>
98      public static Statement UnpackBlockStatement(Statement stmt)
99      {
100        while (stmt is BlockStatement) {
101          stmt = stmt.GetChildByRole(BlockStatement.StatementRole);
102          if (stmt.GetNextSibling(s => s.Role == BlockStatement.StatementRole) != null)
103            return null;
104        }
105        return stmt;
106      }
107    }
108
109 
110    /// <summary>
111    /// Allows to give parameter declaration group names.
112    /// </summary>
113    public static ParameterDeclaration NamedParameter(string groupName)
114    {
115      return new NamedParameterDeclaration (groupName);
116    }
117
118    /// <summary>
119    /// Allows to give parameter declaration group names.
120    /// </summary>
121    public static ParameterDeclaration NamedParameter(string groupName, AstType type, string name, ParameterModifier modifier = ParameterModifier.None)
122    {
123      return new NamedParameterDeclaration (groupName, type, name, modifier);
124    }
125
126    sealed class NamedParameterDeclaration : ParameterDeclaration
127    {
128      readonly string groupName;
129      public string GroupName {
130        get { return groupName; }
131      }
132
133      public NamedParameterDeclaration(string groupName = null)
134      {
135        this.groupName = groupName;
136      }
137
138      public NamedParameterDeclaration(string groupName, AstType type, string name, ParameterModifier modifier = ParameterModifier.None) : base (type, name, modifier)
139      {
140        this.groupName = groupName;
141      }
142
143      protected internal override bool DoMatch(AstNode other, Match match)
144      {
145        match.Add(this.groupName, other);
146        return base.DoMatch(other, match);
147      }
148    }
149
150    /// <summary>
151    /// Matches any type
152    /// </summary>
153    public static AstType AnyType (bool doesMatchNullTypes = false)
154    {
155      if (doesMatchNullTypes)
156        return new OptionalNode(new AnyNode());
157      else
158        return new AnyNode();
159    }
160
161    /// <summary>
162    /// Matches any type
163    /// </summary>
164    public static AstType AnyType (string groupName, bool doesMatchNullTypes = false)
165    {
166      if (doesMatchNullTypes)
167        return new OptionalNode(new AnyNode(groupName));
168      else
169        return new AnyNode(groupName);
170    }
171  }
172}
Note: See TracBrowser for help on using the repository browser.