diff --git a/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Backreference.cs b/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Backreference.cs index c5573f01aa..db3e3ecebf 100644 --- a/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Backreference.cs +++ b/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Backreference.cs @@ -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; } } } diff --git a/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Choice.cs b/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Choice.cs index ba0954884d..6435cc3243 100644 --- a/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Choice.cs +++ b/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Choice.cs @@ -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 { /// /// Matches one of several alternatives. /// - public class Choice : Pattern + public class Choice : Pattern, IEnumerable { public static readonly Role AlternativeRole = new Role("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 } return false; } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetChildrenByRole(AlternativeRole).GetEnumerator(); + } } } diff --git a/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Match.cs b/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Match.cs index 4a44dc36fb..9b1ff905f3 100644 --- a/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Match.cs +++ b/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Match.cs @@ -24,13 +24,29 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching results.RemoveRange(checkPoint, results.Count - checkPoint); } - public IEnumerable this[string groupName] { - get { - foreach (var pair in results) { - if (pair.Key == groupName) - yield return pair.Value; - } + public IEnumerable Get(string groupName) + { + foreach (var pair in results) { + if (pair.Key == groupName) + yield return pair.Value; + } + } + + public IEnumerable Get(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) diff --git a/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Pattern.cs b/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Pattern.cs index 794806a86a..2e08092a91 100644 --- a/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Pattern.cs +++ b/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Pattern.cs @@ -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); diff --git a/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Placeholder.cs b/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Placeholder.cs index 16ca73cef0..f84faaa398 100644 --- a/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Placeholder.cs +++ b/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Placeholder.cs @@ -7,113 +7,113 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching { sealed class TypePlaceholder : AstType { - public static readonly Role PatternRole = new Role("Pattern"); + public static readonly Role ChildRole = new Role("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(IAstVisitor 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(IAstVisitor 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(IAstVisitor 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(IAstVisitor 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(IAstVisitor 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); } } } diff --git a/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs b/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs index 42f049978b..19499ecc6f 100644 --- a/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs +++ b/ICSharpCode.NRefactory/CSharp/OutputVisitor/OutputVisitor.cs @@ -1689,6 +1689,7 @@ namespace ICSharpCode.NRefactory.CSharp WriteModifiers(eventDeclaration.ModifierTokens); WriteKeyword("event"); eventDeclaration.ReturnType.AcceptVisitor(this, data); + Space(); WriteCommaSeparatedList(eventDeclaration.Variables); Semicolon(); return EndNode(eventDeclaration); @@ -1700,6 +1701,8 @@ namespace ICSharpCode.NRefactory.CSharp WriteAttributes(customEventDeclaration.Attributes); WriteModifiers(customEventDeclaration.ModifierTokens); WriteKeyword("event"); + customEventDeclaration.ReturnType.AcceptVisitor(this, data); + Space(); WritePrivateImplementationType(customEventDeclaration.PrivateImplementationType); WriteIdentifier(customEventDeclaration.Name); OpenBrace(policy.EventBraceStyle);