From 254bf370118b183c9ac3f0894f09b6dddefc835f Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Tue, 18 Apr 2006 12:21:16 +0000 Subject: [PATCH] Fixed exception when parsing incomplete Select Case statement. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/2.0@1314 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/Src/Parser/CSharp/Parser.cs | 4 ++-- .../Project/Src/Parser/CSharp/cs.ATG | 4 ++-- .../Project/Src/Parser/VBNet/Parser.cs | 4 ++-- .../Project/Src/Parser/VBNet/VBNET.ATG | 4 ++-- .../NRefactory/Test/Parser/ParseUtilVBNet.cs | 18 ++++++++++---- .../Parser/Statements/SwitchStatementTests.cs | 24 +++++++++++++++---- 6 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs b/src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs index ebedf04772..3a2416ada3 100644 --- a/src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs +++ b/src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs @@ -4186,14 +4186,14 @@ out SwitchSection stmt) { out label); #line 1971 "cs.ATG" - switchSection.SwitchLabels.Add(label); + if (label != null) { switchSection.SwitchLabels.Add(label); } while (la.kind == 54 || la.kind == 62) { SwitchLabel( #line 1973 "cs.ATG" out label); #line 1973 "cs.ATG" - switchSection.SwitchLabels.Add(label); + if (label != null) { switchSection.SwitchLabels.Add(label); } } #line 1975 "cs.ATG" diff --git a/src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG b/src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG index ce497567ec..463c9b5908 100644 --- a/src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG +++ b/src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG @@ -1968,9 +1968,9 @@ SwitchSection CaseLabel label; .) = - SwitchLabel (. switchSection.SwitchLabels.Add(label); .) + SwitchLabel (. if (label != null) { switchSection.SwitchLabels.Add(label); } .) { - SwitchLabel (. switchSection.SwitchLabels.Add(label); .) + SwitchLabel (. if (label != null) { switchSection.SwitchLabels.Add(label); } .) } (. compilationUnit.BlockStart(switchSection); .) Statement { Statement } diff --git a/src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs b/src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs index 9e796267b6..a7c61f333b 100644 --- a/src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs +++ b/src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs @@ -5875,7 +5875,7 @@ out List caseClauses) { out caseClause); #line 2800 "VBNET.ATG" - caseClauses.Add(caseClause); + if (caseClause != null) { caseClauses.Add(caseClause); } while (la.kind == 12) { lexer.NextToken(); CaseClause( @@ -5883,7 +5883,7 @@ out caseClause); out caseClause); #line 2801 "VBNET.ATG" - caseClauses.Add(caseClause); + if (caseClause != null) { caseClauses.Add(caseClause); } } } diff --git a/src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG b/src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG index e28f19be1b..9eae7cb3b5 100644 --- a/src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG +++ b/src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG @@ -2797,8 +2797,8 @@ CaseClauses caseClauses> caseClauses = new List(); CaseLabel caseClause = null; .) = - CaseClause (. caseClauses.Add(caseClause); .) - { "," CaseClause (. caseClauses.Add(caseClause); .) } + CaseClause (. if (caseClause != null) { caseClauses.Add(caseClause); } .) + { "," CaseClause (. if (caseClause != null) { caseClauses.Add(caseClause); } .) } . /* 19.8.2 */ diff --git a/src/Libraries/NRefactory/Test/Parser/ParseUtilVBNet.cs b/src/Libraries/NRefactory/Test/Parser/ParseUtilVBNet.cs index cc1f30b3ce..7d146cab79 100644 --- a/src/Libraries/NRefactory/Test/Parser/ParseUtilVBNet.cs +++ b/src/Libraries/NRefactory/Test/Parser/ParseUtilVBNet.cs @@ -37,24 +37,34 @@ namespace ICSharpCode.NRefactory.Tests.AST return (T)parser.CompilationUnit.Children[0]; } - public static T ParseTypeMember(string typeMember) where T : INode + public static T ParseTypeMember(string typeMember, bool expectErrors) where T : INode { - TypeDeclaration td = ParseGlobal("Class TestClass\n " + typeMember + "\n End Class\n"); + TypeDeclaration td = ParseGlobal("Class TestClass\n " + typeMember + "\n End Class\n", expectErrors); Assert.IsTrue(td.Children.Count > 0); Type type = typeof(T); Assert.IsTrue(type.IsAssignableFrom(td.Children[0].GetType()), String.Format("Parsed expression was {0} instead of {1} ({2})", td.GetType(), type, td)); return (T)td.Children[0]; } - public static T ParseStatement(string statement) where T : INode + public static T ParseTypeMember(string typeMember) where T : INode + { + return ParseTypeMember(typeMember, false); + } + + public static T ParseStatement(string statement, bool expectErrors) where T : INode { - MethodDeclaration md = ParseTypeMember("Sub A()\n " + statement + "\nEnd Sub\n"); + MethodDeclaration md = ParseTypeMember("Sub A()\n " + statement + "\nEnd Sub\n", expectErrors); Assert.IsTrue(md.Body.Children.Count > 0); Type type = typeof(T); Assert.IsTrue(type.IsAssignableFrom(md.Body.Children[0].GetType()), String.Format("Parsed expression was {0} instead of {1} ({2})", md.GetType(), type, md)); return (T)md.Body.Children[0]; } + public static T ParseStatement(string statement) where T : INode + { + return ParseStatement(statement, false); + } + public static T ParseExpression(string expr) where T : INode { return ParseExpression(expr, false); diff --git a/src/Libraries/NRefactory/Test/Parser/Statements/SwitchStatementTests.cs b/src/Libraries/NRefactory/Test/Parser/Statements/SwitchStatementTests.cs index 208438b0af..40dc5650d6 100644 --- a/src/Libraries/NRefactory/Test/Parser/Statements/SwitchStatementTests.cs +++ b/src/Libraries/NRefactory/Test/Parser/Statements/SwitchStatementTests.cs @@ -20,13 +20,29 @@ namespace ICSharpCode.NRefactory.Tests.AST [Test] public void CSharpSwitchStatementTest() { - SwitchStatement switchStmt = ParseUtilCSharp.ParseStatement("switch (a) { case 5: break; case 6: break; default: break; }"); - // TODO : Extend test. + SwitchStatement switchStmt = ParseUtilCSharp.ParseStatement("switch (a) { case 4: case 5: break; case 6: break; default: break; }"); + Assert.AreEqual("a", ((IdentifierExpression)switchStmt.SwitchExpression).Identifier); + // TODO: Extend test } #endregion #region VB.NET - // TODO - #endregion + [Test] + public void VBSwitchStatementTest() + { + SwitchStatement switchStmt = ParseUtilVBNet.ParseStatement("Select Case a\n Case 4, 5\n Case 6\n Case Else\n End Select"); + Assert.AreEqual("a", ((IdentifierExpression)switchStmt.SwitchExpression).Identifier); + // TODO: Extend test + } + + [Test] + public void InvalidVBSwitchStatementTest() + { + SwitchStatement switchStmt = ParseUtilVBNet.ParseStatement("Select Case a\n Case \n End Select", true); + Assert.AreEqual("a", ((IdentifierExpression)switchStmt.SwitchExpression).Identifier); + SwitchSection sec = switchStmt.SwitchSections[0]; + Assert.AreEqual(0, sec.SwitchLabels.Count); + } + #endregion } }