From a642c61a4e31b928ce48984ecd0d807e686d29f7 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Sat, 12 Mar 2011 17:31:41 +0100 Subject: [PATCH] Enable using patterns in place of catch clauses. --- .../CSharp/Ast/PatternMatching/Match.cs | 2 ++ .../CSharp/Ast/PatternMatching/Pattern.cs | 5 ++++ .../CSharp/Ast/PatternMatching/Placeholder.cs | 29 +++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/NRefactory/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Match.cs b/NRefactory/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Match.cs index 9b1ff905f..5e76a428c 100644 --- a/NRefactory/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Match.cs +++ b/NRefactory/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Match.cs @@ -12,6 +12,8 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching /// public sealed class Match { + // TODO: maybe we should add an implicit Match->bool conversion? (operator implicit bool(Match m) { return m != null; }) + List> results = new List>(); internal int CheckPoint() diff --git a/NRefactory/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Pattern.cs b/NRefactory/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Pattern.cs index aa8d95fb7..6d893b58c 100644 --- a/NRefactory/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Pattern.cs +++ b/NRefactory/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Pattern.cs @@ -79,6 +79,11 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching return p != null ? new SwitchSectionPlaceholder(p) : null; } + public static implicit operator CatchClause(Pattern p) + { + return p != null ? new CatchClausePlaceholder(p) : null; + } + // Make debugging easier by giving Patterns a ToString() implementation public override string ToString() { diff --git a/NRefactory/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Placeholder.cs b/NRefactory/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Placeholder.cs index 2e3c2b592..b30ca58f2 100644 --- a/NRefactory/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Placeholder.cs +++ b/NRefactory/ICSharpCode.NRefactory/CSharp/Ast/PatternMatching/Placeholder.cs @@ -210,4 +210,33 @@ namespace ICSharpCode.NRefactory.CSharp.PatternMatching return child.DoMatchCollection(role, pos, match, backtrackingStack); } } + + sealed class CatchClausePlaceholder : CatchClause + { + readonly AstNode child; + + public CatchClausePlaceholder(AstNode child) + { + this.child = child; + } + + public override NodeType NodeType { + get { return NodeType.Placeholder; } + } + + public override S AcceptVisitor(IAstVisitor visitor, T data) + { + return ((IPatternAstVisitor)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 backtrackingStack) + { + return child.DoMatchCollection(role, pos, match, backtrackingStack); + } + } }