Browse Source

Add Choice pattern.

newNRvisualizers
Daniel Grunwald 15 years ago
parent
commit
f5e89bfd3c
  1. 33
      ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Choice.cs
  2. 10
      ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Match.cs
  3. 1
      ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

33
ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Choice.cs

@ -0,0 +1,33 @@ @@ -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
{
/// <summary>
/// Matches one of several alternatives.
/// </summary>
public class Choice : Pattern
{
public static readonly Role<AstNode> AlternativeRole = new Role<AstNode>("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;
}
}
}

10
ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Match.cs

@ -14,6 +14,16 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching @@ -14,6 +14,16 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching
{
List<KeyValuePair<string, AstNode>> results = new List<KeyValuePair<string, AstNode>>();
internal int CheckPoint()
{
return results.Count;
}
internal void RestoreCheckPoint(int checkPoint)
{
results.RemoveRange(checkPoint, results.Count - checkPoint);
}
public IEnumerable<AstNode> this[string groupName] {
get {
foreach (var pair in results) {

1
ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

@ -95,6 +95,7 @@ @@ -95,6 +95,7 @@
<Compile Include="CSharp\Ast\Expressions\UncheckedExpression.cs" />
<Compile Include="CSharp\Ast\GeneralScope\TypeParameterDeclaration.cs" />
<Compile Include="CSharp\Ast\MemberType.cs" />
<Compile Include="CSharp\Ast\PatternMatching\Choice.cs" />
<Compile Include="CSharp\Ast\PatternMatching\AnyNode.cs" />
<Compile Include="CSharp\Ast\PatternMatching\Backreference.cs" />
<Compile Include="CSharp\Ast\PatternMatching\Match.cs" />

Loading…
Cancel
Save