Browse Source

Add some improvements to pattern matching.

newNRvisualizers
Daniel Grunwald 15 years ago
parent
commit
bce988124a
  1. 2
      ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Backreference.cs
  2. 18
      ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Choice.cs
  3. 28
      ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Match.cs
  4. 5
      ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Pattern.cs
  5. 52
      ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Placeholder.cs

2
ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Backreference.cs

@ -22,7 +22,7 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching @@ -22,7 +22,7 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching
protected internal override bool DoMatch(AstNode other, Match match)
{
return match[referencedGroupName].Last().Match(other) != null;
return match.Get(referencedGroupName).Last().Match(other) != null;
}
}
}

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

@ -2,20 +2,25 @@ @@ -2,20 +2,25 @@
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using System.Collections;
namespace ICSharpCode.NRefactory.CSharp.PatternMatching
{
/// <summary>
/// Matches one of several alternatives.
/// </summary>
public class Choice : Pattern
public class Choice : Pattern, IEnumerable
{
public static readonly Role<AstNode> AlternativeRole = new Role<AstNode>("Alternative", AstNode.Null);
public Choice(params AstNode[] alternatives)
public void Add(string name, AstNode alternative)
{
foreach (AstNode node in alternatives)
AddChild(node, AlternativeRole);
AddChild(new NamedNode(name, alternative), AlternativeRole);
}
public void Add(AstNode alternative)
{
AddChild(alternative, AlternativeRole);
}
protected internal override bool DoMatch(AstNode other, Match match)
@ -29,5 +34,10 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching @@ -29,5 +34,10 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching
}
return false;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetChildrenByRole(AlternativeRole).GetEnumerator();
}
}
}

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

@ -24,13 +24,29 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching @@ -24,13 +24,29 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching
results.RemoveRange(checkPoint, results.Count - checkPoint);
}
public IEnumerable<AstNode> this[string groupName] {
get {
foreach (var pair in results) {
if (pair.Key == groupName)
yield return pair.Value;
}
public IEnumerable<AstNode> Get(string groupName)
{
foreach (var pair in results) {
if (pair.Key == groupName)
yield return pair.Value;
}
}
public IEnumerable<T> Get<T>(string groupName) where T : AstNode
{
foreach (var pair in results) {
if (pair.Key == groupName)
yield return (T)pair.Value;
}
}
public bool Has(string groupName)
{
foreach (var pair in results) {
if (pair.Key == groupName)
return true;
}
return false;
}
public void Add(string groupName, AstNode node)

5
ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Pattern.cs

@ -29,6 +29,11 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching @@ -29,6 +29,11 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching
return new ExpressionPlaceholder(this);
}
public Statement ToStatement()
{
return new StatementPlaceholder(this);
}
public BlockStatement ToBlock()
{
return new BlockStatementPlaceholder(this);

52
ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Placeholder.cs

@ -7,113 +7,113 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching @@ -7,113 +7,113 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching
{
sealed class TypePlaceholder : AstType
{
public static readonly Role<Pattern> PatternRole = new Role<Pattern>("Pattern");
public static readonly Role<AstNode> ChildRole = new Role<AstNode>("Child", AstNode.Null);
public TypePlaceholder(Pattern pattern)
public TypePlaceholder(AstNode child)
{
AddChild(pattern, TypePlaceholder.PatternRole);
AddChild(child, TypePlaceholder.ChildRole);
}
public override NodeType NodeType {
get { return NodeType.Pattern; }
get { return GetChildByRole(TypePlaceholder.ChildRole).NodeType; }
}
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
{
return default(S);
return GetChildByRole(TypePlaceholder.ChildRole).AcceptVisitor(visitor, data);
}
protected internal override bool DoMatch(AstNode other, Match match)
{
return GetChildByRole(TypePlaceholder.PatternRole).DoMatch(other, match);
return GetChildByRole(TypePlaceholder.ChildRole).DoMatch(other, match);
}
}
sealed class ExpressionPlaceholder : Expression
{
public ExpressionPlaceholder(Pattern pattern)
public ExpressionPlaceholder(AstNode child)
{
AddChild(pattern, TypePlaceholder.PatternRole);
AddChild(child, TypePlaceholder.ChildRole);
}
public override NodeType NodeType {
get { return NodeType.Pattern; }
get { return GetChildByRole(TypePlaceholder.ChildRole).NodeType; }
}
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
{
return default(S);
return GetChildByRole(TypePlaceholder.ChildRole).AcceptVisitor(visitor, data);
}
protected internal override bool DoMatch(AstNode other, Match match)
{
return GetChildByRole(TypePlaceholder.PatternRole).DoMatch(other, match);
return GetChildByRole(TypePlaceholder.ChildRole).DoMatch(other, match);
}
}
sealed class StatementPlaceholder : Statement
{
public StatementPlaceholder(Pattern pattern)
public StatementPlaceholder(AstNode child)
{
AddChild(pattern, TypePlaceholder.PatternRole);
AddChild(child, TypePlaceholder.ChildRole);
}
public override NodeType NodeType {
get { return NodeType.Pattern; }
get { return GetChildByRole(TypePlaceholder.ChildRole).NodeType; }
}
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
{
return default(S);
return GetChildByRole(TypePlaceholder.ChildRole).AcceptVisitor(visitor, data);
}
protected internal override bool DoMatch(AstNode other, Match match)
{
return GetChildByRole(TypePlaceholder.PatternRole).DoMatch(other, match);
return GetChildByRole(TypePlaceholder.ChildRole).DoMatch(other, match);
}
}
sealed class BlockStatementPlaceholder : BlockStatement
{
public BlockStatementPlaceholder(Pattern pattern)
public BlockStatementPlaceholder(AstNode child)
{
AddChild(pattern, TypePlaceholder.PatternRole);
AddChild(child, TypePlaceholder.ChildRole);
}
public override NodeType NodeType {
get { return NodeType.Pattern; }
get { return GetChildByRole(TypePlaceholder.ChildRole).NodeType; }
}
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
{
return default(S);
return GetChildByRole(TypePlaceholder.ChildRole).AcceptVisitor(visitor, data);
}
protected internal override bool DoMatch(AstNode other, Match match)
{
return GetChildByRole(TypePlaceholder.PatternRole).DoMatch(other, match);
return GetChildByRole(TypePlaceholder.ChildRole).DoMatch(other, match);
}
}
sealed class VariablePlaceholder : VariableInitializer
{
public VariablePlaceholder(Pattern pattern)
public VariablePlaceholder(AstNode child)
{
AddChild(pattern, TypePlaceholder.PatternRole);
AddChild(child, TypePlaceholder.ChildRole);
}
public override NodeType NodeType {
get { return NodeType.Pattern; }
get { return GetChildByRole(TypePlaceholder.ChildRole).NodeType; }
}
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
{
return default(S);
return GetChildByRole(TypePlaceholder.ChildRole).AcceptVisitor(visitor, data);
}
protected internal override bool DoMatch(AstNode other, Match match)
{
return GetChildByRole(TypePlaceholder.PatternRole).DoMatch(other, match);
return GetChildByRole(TypePlaceholder.ChildRole).DoMatch(other, match);
}
}
}

Loading…
Cancel
Save