From 38faf8415d3fc8ae6e85e483de2253d057cc2b57 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Mon, 21 Feb 2011 17:56:54 +0100 Subject: [PATCH] Add support for 'any' operator and fix a bug in pattern matching. --- .../CSharp/Ast/AstNodeCollection.cs | 3 ++- .../CSharp/Ast/Expressions/AssignmentExpression.cs | 6 +++++- .../Ast/Expressions/BinaryOperatorExpression.cs | 12 +++++++++--- .../CSharp/Ast/Expressions/PrimitiveExpression.cs | 4 +++- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/ICSharpCode.NRefactory/CSharp/Ast/AstNodeCollection.cs b/ICSharpCode.NRefactory/CSharp/Ast/AstNodeCollection.cs index 0287b2eb2a..8ffc29157b 100644 --- a/ICSharpCode.NRefactory/CSharp/Ast/AstNodeCollection.cs +++ b/ICSharpCode.NRefactory/CSharp/Ast/AstNodeCollection.cs @@ -187,7 +187,8 @@ namespace ICSharpCode.NRefactory.CSharp success = cur1.DoMatch(cur2, match); } cur1 = cur1.NextSibling; - cur2 = cur2.NextSibling; + if (cur2 != null) + cur2 = cur2.NextSibling; } while (cur2 != null && cur2.Role != role) cur2 = cur2.NextSibling; diff --git a/ICSharpCode.NRefactory/CSharp/Ast/Expressions/AssignmentExpression.cs b/ICSharpCode.NRefactory/CSharp/Ast/Expressions/AssignmentExpression.cs index f3b31a7508..6db96ec894 100644 --- a/ICSharpCode.NRefactory/CSharp/Ast/Expressions/AssignmentExpression.cs +++ b/ICSharpCode.NRefactory/CSharp/Ast/Expressions/AssignmentExpression.cs @@ -75,7 +75,8 @@ namespace ICSharpCode.NRefactory.CSharp protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) { AssignmentExpression o = other as AssignmentExpression; - return o != null && this.Left.DoMatch(o.Left, match) && this.Operator == o.Operator && this.Right.DoMatch(o.Right, match); + return o != null && (this.Operator == AssignmentOperatorType.Any || this.Operator == o.Operator) + && this.Left.DoMatch(o.Left, match) && this.Right.DoMatch(o.Right, match); } public static string GetOperatorSymbol(AssignmentOperatorType op) @@ -136,5 +137,8 @@ namespace ICSharpCode.NRefactory.CSharp BitwiseOr, /// left ^= right ExclusiveOr, + + /// Any operator (for pattern matching) + Any } } diff --git a/ICSharpCode.NRefactory/CSharp/Ast/Expressions/BinaryOperatorExpression.cs b/ICSharpCode.NRefactory/CSharp/Ast/Expressions/BinaryOperatorExpression.cs index 464699b68c..171e3e31c2 100644 --- a/ICSharpCode.NRefactory/CSharp/Ast/Expressions/BinaryOperatorExpression.cs +++ b/ICSharpCode.NRefactory/CSharp/Ast/Expressions/BinaryOperatorExpression.cs @@ -1,6 +1,6 @@ // // BinaryOperatorExpression.cs -// +// // Author: // Mike Krüger // @@ -75,7 +75,8 @@ namespace ICSharpCode.NRefactory.CSharp protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) { BinaryOperatorExpression o = other as BinaryOperatorExpression; - return o != null && this.Left.DoMatch(o.Left, match) && this.Operator == o.Operator && this.Right.DoMatch(o.Right, match); + return o != null && (this.Operator == BinaryOperatorType.Any || this.Operator == o.Operator) + && this.Left.DoMatch(o.Left, match) && this.Right.DoMatch(o.Right, match); } public static string GetOperatorSymbol(BinaryOperatorType op) @@ -173,6 +174,11 @@ namespace ICSharpCode.NRefactory.CSharp ShiftRight, /// left ?? right - NullCoalescing + NullCoalescing, + + /// + /// Any binary operator (used in pattern matching) + /// + Any } } diff --git a/ICSharpCode.NRefactory/CSharp/Ast/Expressions/PrimitiveExpression.cs b/ICSharpCode.NRefactory/CSharp/Ast/Expressions/PrimitiveExpression.cs index ca1b274434..cc4fbe1c04 100644 --- a/ICSharpCode.NRefactory/CSharp/Ast/Expressions/PrimitiveExpression.cs +++ b/ICSharpCode.NRefactory/CSharp/Ast/Expressions/PrimitiveExpression.cs @@ -31,6 +31,8 @@ namespace ICSharpCode.NRefactory.CSharp /// public class PrimitiveExpression : Expression { + public static readonly object AnyValue = new object(); + AstLocation startLocation; public override AstLocation StartLocation { get { @@ -70,7 +72,7 @@ namespace ICSharpCode.NRefactory.CSharp protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) { PrimitiveExpression o = other as PrimitiveExpression; - return o != null && object.Equals(this.Value, o.Value); + return o != null && (this.Value == AnyValue || object.Equals(this.Value, o.Value)); } } }