Browse Source

Add OptionalNode for pattern matching.

pull/100/head
Daniel Grunwald 15 years ago
parent
commit
0c3ef91971
  1. 1
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/IPatternAstVisitor.cs
  2. 42
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/OptionalNode.cs
  3. 5
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Pattern.cs
  4. 29
      NRefactory/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Placeholder.cs
  5. 10
      NRefactory/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs
  6. 1
      NRefactory/ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

1
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/IPatternAstVisitor.cs

@ -17,6 +17,7 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching @@ -17,6 +17,7 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching
S VisitChoice(Choice choice, T data);
S VisitNamedNode(NamedNode namedNode, T data);
S VisitRepeat(Repeat repeat, T data);
S VisitOptionalNode(OptionalNode optionalNode, T data);
S VisitIdentifierExpressionBackreference(IdentifierExpressionBackreference identifierExpressionBackreference, T data);
}
}

42
NRefactory/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/OptionalNode.cs

@ -0,0 +1,42 @@ @@ -0,0 +1,42 @@
// 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;
using System.Collections.Generic;
using System.Diagnostics;
namespace ICSharpCode.NRefactory.CSharp.PatternMatching
{
public class OptionalNode : Pattern
{
public static readonly Role<AstNode> ElementRole = new Role<AstNode>("Element", AstNode.Null);
public OptionalNode(AstNode childNode)
{
AddChild(childNode, ElementRole);
}
public OptionalNode(string groupName, AstNode childNode) : this(new NamedNode(groupName, childNode))
{
}
internal override bool DoMatchCollection(Role role, AstNode pos, Match match, Stack<Pattern.PossibleMatch> backtrackingStack)
{
backtrackingStack.Push(new PossibleMatch(pos, match.CheckPoint()));
return GetChildByRole(ElementRole).DoMatch(pos, match);
}
protected internal override bool DoMatch(AstNode other, Match match)
{
if (other == null || other.IsNull)
return true;
else
return GetChildByRole(ElementRole).DoMatch(other, match);
}
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
{
return ((IPatternAstVisitor<T, S>)visitor).VisitOptionalNode(this, data);
}
}
}

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

@ -74,6 +74,11 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching @@ -74,6 +74,11 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching
return p != null ? new AttributeSectionPlaceholder(p) : null;
}
public static implicit operator SwitchSection(Pattern p)
{
return p != null ? new SwitchSectionPlaceholder(p) : null;
}
// Make debugging easier by giving Patterns a ToString() implementation
public override string ToString()
{

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

@ -181,4 +181,33 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching @@ -181,4 +181,33 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching
return child.DoMatchCollection(role, pos, match, backtrackingStack);
}
}
sealed class SwitchSectionPlaceholder : SwitchSection
{
readonly AstNode child;
public SwitchSectionPlaceholder(AstNode child)
{
this.child = child;
}
public override NodeType NodeType {
get { return NodeType.Placeholder; }
}
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
{
return ((IPatternAstVisitor<T, S>)visitor).VisitPlaceholder(this, child, data);
}
protected internal override bool DoMatch(AstNode other, Match match)
{
return child.DoMatch(other, match);
}
internal override bool DoMatchCollection(Role role, AstNode pos, Match match, Stack<Pattern.PossibleMatch> backtrackingStack)
{
return child.DoMatchCollection(role, pos, match, backtrackingStack);
}
}
}

10
NRefactory/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs

@ -2170,6 +2170,16 @@ namespace ICSharpCode.NRefactory.CSharp @@ -2170,6 +2170,16 @@ namespace ICSharpCode.NRefactory.CSharp
RPar();
return EndNode(repeat);
}
object IPatternAstVisitor<object, object>.VisitOptionalNode(OptionalNode optionalNode, object data)
{
StartNode(optionalNode);
WriteKeyword("optional");
LPar();
optionalNode.GetChildByRole(OptionalNode.ElementRole).AcceptVisitor(this, data);
RPar();
return EndNode(optionalNode);
}
#endregion
}
}

1
NRefactory/ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

@ -107,6 +107,7 @@ @@ -107,6 +107,7 @@
<Compile Include="CSharp\Ast\PatternMatching\IPatternAstVisitor.cs" />
<Compile Include="CSharp\Ast\PatternMatching\Match.cs" />
<Compile Include="CSharp\Ast\PatternMatching\NamedNode.cs" />
<Compile Include="CSharp\Ast\PatternMatching\OptionalNode.cs" />
<Compile Include="CSharp\Ast\PatternMatching\Repeat.cs" />
<Compile Include="CSharp\Ast\PatternMatching\Pattern.cs" />
<Compile Include="CSharp\Ast\PatternMatching\Placeholder.cs" />

Loading…
Cancel
Save