From f5e89bfd3c61006e48af524c9902645c95c41d33 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sun, 20 Feb 2011 00:42:45 +0100 Subject: [PATCH] Add Choice pattern. --- .../CSharp/Ast/PatternMatching/Choice.cs | 33 +++++++++++++++++++ .../CSharp/Ast/PatternMatching/Match.cs | 10 ++++++ .../ICSharpCode.NRefactory.csproj | 1 + 3 files changed, 44 insertions(+) create mode 100644 ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Choice.cs diff --git a/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Choice.cs b/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Choice.cs new file mode 100644 index 0000000000..ba0954884d --- /dev/null +++ b/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Choice.cs @@ -0,0 +1,33 @@ +// 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; + +namespace ICSharpCode.NRefactory.CSharp.PatternMatching +{ + /// + /// Matches one of several alternatives. + /// + public class Choice : Pattern + { + public static readonly Role AlternativeRole = new Role("Alternative", AstNode.Null); + + public Choice(params AstNode[] alternatives) + { + foreach (AstNode node in alternatives) + AddChild(node, AlternativeRole); + } + + protected internal override bool DoMatch(AstNode other, Match match) + { + var checkPoint = match.CheckPoint(); + foreach (AstNode alt in GetChildrenByRole(AlternativeRole)) { + if (alt.DoMatch(other, match)) + return true; + else + match.RestoreCheckPoint(checkPoint); + } + return false; + } + } +} diff --git a/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Match.cs b/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Match.cs index 94a4d48329..4a44dc36fb 100644 --- a/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Match.cs +++ b/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Match.cs @@ -14,6 +14,16 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching { List> results = new List>(); + internal int CheckPoint() + { + return results.Count; + } + + internal void RestoreCheckPoint(int checkPoint) + { + results.RemoveRange(checkPoint, results.Count - checkPoint); + } + public IEnumerable this[string groupName] { get { foreach (var pair in results) { diff --git a/ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj b/ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj index 02a6a4f6f4..f750893d9e 100644 --- a/ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj +++ b/ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj @@ -95,6 +95,7 @@ +