Browse Source

Fixed NRefactory bugs regarding "else if"-statements.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1014 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 20 years ago
parent
commit
9833db1c49
  1. 14
      src/AddIns/BackendBindings/Boo/NRefactoryToBooConverter/Project/ConvertVisitorStatements.cs
  2. 7
      src/AddIns/BackendBindings/Boo/NRefactoryToBooConverter/Test/StatementTests.cs
  3. 34
      src/Libraries/NRefactory/Project/Src/Output/CSharp/CSharpOutputVisitor.cs
  4. 812
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs
  5. 11
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG
  6. 16
      src/Libraries/NRefactory/Test/Output/CSharp/CSharpOutputTest.cs
  7. 12
      src/Libraries/NRefactory/Test/Output/VBNet/VBNetOutputTest.cs
  8. 50
      src/Libraries/NRefactory/Test/Parser/Statements/IfElseStatementTests.cs

14
src/AddIns/BackendBindings/Boo/NRefactoryToBooConverter/Project/ConvertVisitorStatements.cs

@ -400,17 +400,25 @@ namespace NRefactoryToBooConverter @@ -400,17 +400,25 @@ namespace NRefactoryToBooConverter
public object Visit(IfElseStatement ifElseStatement, object data)
{
B.IfStatement ifs = new B.IfStatement(GetLexicalInfo(ifElseStatement));
B.IfStatement outerIf = ifs;
ifs.EndSourceLocation = GetLocation(ifElseStatement.EndLocation);
ifs.Condition = ConvertExpression(ifElseStatement.Condition);
ifs.TrueBlock = ConvertBlock(ifElseStatement.TrueStatement);
if (ifElseStatement.HasElseIfSections) {
// TODO: implement elseif
AddError(ifElseStatement, "ElseIfSections are not supported (because Daniel was lazy)");
foreach (ElseIfSection sec in ifElseStatement.ElseIfSections) {
B.IfStatement elif = new B.IfStatement(GetLexicalInfo(sec));
elif.EndSourceLocation = GetLocation(sec.EndLocation);
elif.Condition = ConvertExpression(sec.Condition);
elif.TrueBlock = ConvertBlock(sec.EmbeddedStatement);
ifs.FalseBlock = new B.Block();
ifs.FalseBlock.Add(elif);
ifs = elif;
}
}
if (ifElseStatement.HasElseStatements) {
ifs.FalseBlock = ConvertBlock(ifElseStatement.FalseStatement);
}
return ifs;
return outerIf;
}
public object Visit(ElseIfSection elseIfSection, object data)

7
src/AddIns/BackendBindings/Boo/NRefactoryToBooConverter/Test/StatementTests.cs

@ -25,6 +25,13 @@ namespace NRefactoryToBooConverter.Tests @@ -25,6 +25,13 @@ namespace NRefactoryToBooConverter.Tests
TestStatement("if (a) B(); else C();", "if a:\n\tB()\nelse:\n\tC()");
}
[Test]
public void IfElseIfElseStatement()
{
TestStatement("if (a) C(); else if (b) D(); else E();",
"if a:\n\tC()\nelse:\n\tif b:\n\t\tD()\n\telse:\n\t\tE()");
}
[Test]
public void ForLoop()
{

34
src/Libraries/NRefactory/Project/Src/Output/CSharp/CSharpOutputVisitor.cs

@ -1017,15 +1017,8 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -1017,15 +1017,8 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.PrintToken(Tokens.OpenParenthesis);
nodeTracker.TrackedVisit(ifElseStatement.Condition, data);
outputFormatter.PrintToken(Tokens.CloseParenthesis);
if (ifElseStatement.TrueStatement.Count > 1) {
outputFormatter.PrintToken(Tokens.OpenCurlyBrace);
}
foreach (Statement stmt in ifElseStatement.TrueStatement) {
nodeTracker.TrackedVisit(stmt, data);
}
if (ifElseStatement.TrueStatement.Count > 1) {
outputFormatter.PrintToken(Tokens.CloseCurlyBrace);
}
PrintIfSection(ifElseStatement.TrueStatement);
foreach (ElseIfSection elseIfSection in ifElseStatement.ElseIfSections) {
nodeTracker.TrackedVisit(elseIfSection, data);
@ -1034,18 +1027,29 @@ namespace ICSharpCode.NRefactory.PrettyPrinter @@ -1034,18 +1027,29 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
if (ifElseStatement.HasElseStatements) {
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.Else);
if (ifElseStatement.FalseStatement.Count > 1) {
PrintIfSection(ifElseStatement.FalseStatement);
}
return null;
}
void PrintIfSection(List<Statement> statements)
{
if (statements.Count != 1 || !(statements[0] is BlockStatement)) {
outputFormatter.Space();
}
if (statements.Count != 1) {
outputFormatter.PrintToken(Tokens.OpenCurlyBrace);
}
foreach (Statement stmt in ifElseStatement.FalseStatement) {
nodeTracker.TrackedVisit(stmt, data);
foreach (Statement stmt in statements) {
nodeTracker.TrackedVisit(stmt, null);
}
if (ifElseStatement.FalseStatement.Count > 1) {
if (statements.Count != 1) {
outputFormatter.PrintToken(Tokens.CloseCurlyBrace);
}
if (statements.Count != 1 || !(statements[0] is BlockStatement)) {
outputFormatter.Space();
}
return null;
}
public object Visit(ElseIfSection elseIfSection, object data)

812
src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs

File diff suppressed because it is too large Load Diff

11
src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG

@ -1872,7 +1872,15 @@ EmbeddedStatement<out Statement statement> @@ -1872,7 +1872,15 @@ EmbeddedStatement<out Statement statement>
"(" Expr<out expr> ")"
EmbeddedStatement<out embeddedStatement>
[ "else" EmbeddedStatement<out elseStatement> ]
(. statement = elseStatement != null ? (Statement)new IfElseStatement(expr, embeddedStatement, elseStatement) : (Statement)new IfElseStatement(expr, embeddedStatement); .)
(. statement = elseStatement != null ? new IfElseStatement(expr, embeddedStatement, elseStatement) : new IfElseStatement(expr, embeddedStatement); .)
(. if (elseStatement is IfElseStatement && (elseStatement as IfElseStatement).TrueStatement.Count == 1) {
/* else if-section (otherwise we would have a BlockStatment) */
(statement as IfElseStatement).ElseIfSections.Add(
new ElseIfSection((elseStatement as IfElseStatement).Condition,
(elseStatement as IfElseStatement).TrueStatement[0]));
(statement as IfElseStatement).ElseIfSections.AddRange((elseStatement as IfElseStatement).ElseIfSections);
(statement as IfElseStatement).FalseStatement = (elseStatement as IfElseStatement).FalseStatement;
} .)
| "switch" (. List<SwitchSection> switchSections = new List<SwitchSection>(); SwitchSection switchSection; .)
"(" Expr<out expr> ")"
"{" { SwitchSection<out switchSection> (. switchSections.Add(switchSection); .) }
@ -1901,6 +1909,7 @@ EmbeddedStatement<out Statement statement> @@ -1901,6 +1909,7 @@ EmbeddedStatement<out Statement statement>
| "break" (. statement = new YieldStatement(new BreakStatement()); .) ) ";"
| "return" [ Expr<out expr> ] ";" (. statement = new ReturnStatement(expr); .)
| "throw" [ Expr<out expr> ] ";" (. statement = new ThrowStatement(expr); .)
/*--- expression statement: */
| StatementExpr<out statement> ";"
/*--- try statement: */

16
src/Libraries/NRefactory/Test/Output/CSharp/CSharpOutputTest.cs

@ -108,6 +108,22 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -108,6 +108,22 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
TestStatement("object[] a = new object[] {1, 2, 3};");
}
[Test]
public void IfStatement()
{
TestStatement("if (a) { m1(); } else { m2(); }");
TestStatement("if (a) m1(); else m2(); ");
TestStatement("if (a) {\n" +
"\tm1();\n" +
"} else if (b) {\n" +
"\tm2();\n" +
"} else {\n" +
"\tm3();\n" +
"}");
}
[Test]
public void Assignment()
{

12
src/Libraries/NRefactory/Test/Output/VBNet/VBNetOutputTest.cs

@ -108,6 +108,18 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -108,6 +108,18 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
TestStatement("Dim a As Object() = New Object(10) {}");
}
[Test]
public void IfStatement()
{
TestStatement("If a Then\n" +
"\tm1()\n" +
"ElseIf b Then\n" +
"\tm2()\n" +
"Else\n" +
"\tm3()\n" +
"End If");
}
[Test]
public void Assignment()
{

50
src/Libraries/NRefactory/Test/Parser/Statements/IfElseStatementTests.cs

@ -39,6 +39,24 @@ namespace ICSharpCode.NRefactory.Tests.AST @@ -39,6 +39,24 @@ namespace ICSharpCode.NRefactory.Tests.AST
Assert.IsTrue(ifElseStatement.TrueStatement[0] is BlockStatement, "Statement was: " + ifElseStatement.TrueStatement[0]);
Assert.IsTrue(ifElseStatement.FalseStatement[0] is BlockStatement, "Statement was: " + ifElseStatement.FalseStatement[0]);
}
[Test]
public void CSharpIfElseIfStatementTest()
{
IfElseStatement ifElseStatement = ParseUtilCSharp.ParseStatement<IfElseStatement>("if (1) { } else if (2) { } else if (3) { } else { }");
Assert.IsFalse(ifElseStatement.Condition.IsNull);
Assert.IsTrue(ifElseStatement.ElseIfSections.Count == 2, "elseif section count != 2:" + ifElseStatement.ElseIfSections.Count);
Assert.IsTrue(ifElseStatement.TrueStatement.Count == 1, "true count != 1:" + ifElseStatement.TrueStatement.Count);
Assert.IsTrue(ifElseStatement.FalseStatement.Count == 1, "false count != 1:" + ifElseStatement.FalseStatement.Count);
Assert.IsTrue(ifElseStatement.TrueStatement[0] is BlockStatement, "Statement was: " + ifElseStatement.TrueStatement[0]);
Assert.IsTrue(ifElseStatement.FalseStatement[0] is BlockStatement, "Statement was: " + ifElseStatement.FalseStatement[0]);
Assert.IsTrue(ifElseStatement.ElseIfSections[0].EmbeddedStatement is BlockStatement, "Statement was: " + ifElseStatement.ElseIfSections[0].EmbeddedStatement);
Assert.IsTrue(ifElseStatement.ElseIfSections[1].EmbeddedStatement is BlockStatement, "Statement was: " + ifElseStatement.ElseIfSections[1].EmbeddedStatement);
Assert.AreEqual(2, (ifElseStatement.ElseIfSections[0].Condition as PrimitiveExpression).Value);
Assert.AreEqual(3, (ifElseStatement.ElseIfSections[1].Condition as PrimitiveExpression).Value);
}
#endregion
#region VB.NET
@ -62,6 +80,38 @@ namespace ICSharpCode.NRefactory.Tests.AST @@ -62,6 +80,38 @@ namespace ICSharpCode.NRefactory.Tests.AST
Assert.IsTrue(ifElseStatement.TrueStatement[0] is BlockStatement, "Statement was: " + ifElseStatement.TrueStatement[0]);
}
[Test]
public void VBNetElseIfStatementTest()
{
IfElseStatement ifElseStatement = ParseUtilVBNet.ParseStatement<IfElseStatement>("If True THEN\n" +
"END\n" +
"ElseIf False Then\n" +
"Stop\n" +
"End If");
Assert.IsFalse(ifElseStatement.Condition.IsNull);
Assert.IsTrue(ifElseStatement.TrueStatement.Count == 1, "true count != 1:" + ifElseStatement.TrueStatement.Count);
Assert.IsTrue(ifElseStatement.FalseStatement.Count == 0, "false count != 0:" + ifElseStatement.FalseStatement.Count);
Assert.IsFalse((bool)(ifElseStatement.ElseIfSections[0].Condition as PrimitiveExpression).Value);
Assert.IsTrue(ifElseStatement.TrueStatement[0] is BlockStatement, "Statement was: " + ifElseStatement.TrueStatement[0]);
Assert.IsTrue(ifElseStatement.ElseIfSections[0].EmbeddedStatement.Children[0] is StopStatement, "Statement was: " + ifElseStatement.ElseIfSections[0].EmbeddedStatement.Children[0]);
}
[Test]
public void VBNetElse_IfStatementTest()
{
IfElseStatement ifElseStatement = ParseUtilVBNet.ParseStatement<IfElseStatement>("If True THEN\n" +
"END\n" +
"Else If False Then\n" +
"Stop\n" +
"End If");
Assert.IsFalse(ifElseStatement.Condition.IsNull);
Assert.IsTrue(ifElseStatement.TrueStatement.Count == 1, "true count != 1:" + ifElseStatement.TrueStatement.Count);
Assert.IsTrue(ifElseStatement.FalseStatement.Count == 0, "false count != 0:" + ifElseStatement.FalseStatement.Count);
Assert.IsFalse((bool)(ifElseStatement.ElseIfSections[0].Condition as PrimitiveExpression).Value);
Assert.IsTrue(ifElseStatement.TrueStatement[0] is BlockStatement, "Statement was: " + ifElseStatement.TrueStatement[0]);
Assert.IsTrue(ifElseStatement.ElseIfSections[0].EmbeddedStatement.Children[0] is StopStatement, "Statement was: " + ifElseStatement.ElseIfSections[0].EmbeddedStatement.Children[0]);
}
#endregion
}
}

Loading…
Cancel
Save