From 70774e4843459f5ab9184434575f1b2b74f8e93d Mon Sep 17 00:00:00 2001 From: ike709 Date: Wed, 10 Jan 2024 17:51:42 -0700 Subject: [PATCH] Fix empty `for()` loop body error location (#1592) Co-authored-by: ike709 Co-authored-by: wixoa --- DMCompiler/Compiler/DM/DMParser.cs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/DMCompiler/Compiler/DM/DMParser.cs b/DMCompiler/Compiler/DM/DMParser.cs index a4bc611c20..2bad848edb 100644 --- a/DMCompiler/Compiler/DM/DMParser.cs +++ b/DMCompiler/Compiler/DM/DMParser.cs @@ -1047,7 +1047,7 @@ private DMASTProcStatementSet[] ProcSetEnd(bool allowMultiple) { if (body == null) { DMASTProcStatement? statement = ProcStatement(); - if (statement == null) Error("Expected body or statement"); + if (statement == null) Error(WarningCode.BadExpression, "Expected body or statement"); body = new DMASTProcBlockInner(loc, statement); } @@ -1124,7 +1124,7 @@ private DMASTProcStatementSet[] ProcSetEnd(bool allowMultiple) { Whitespace(); if (Check(TokenType.DM_RightParenthesis)) { - return new DMASTProcStatementInfLoop(loc, GetForBody()); + return new DMASTProcStatementInfLoop(loc, GetForBody(loc)); } _allowVarDeclExpression = true; @@ -1144,7 +1144,7 @@ private DMASTProcStatementSet[] ProcSetEnd(bool allowMultiple) { if (expr1 is DMASTAssign assign) { ExpressionTo(out var endRange, out var step); Consume(TokenType.DM_RightParenthesis, "Expected ')' in for after to expression"); - return new DMASTProcStatementFor(loc, new DMASTExpressionInRange(loc, assign.Expression, assign.Value, endRange, step), null, null, dmTypes, GetForBody()); + return new DMASTProcStatementFor(loc, new DMASTExpressionInRange(loc, assign.Expression, assign.Value, endRange, step), null, null, dmTypes, GetForBody(loc)); } else { Error("Expected = before to in for"); } @@ -1155,16 +1155,16 @@ private DMASTProcStatementSet[] ProcSetEnd(bool allowMultiple) { DMASTExpression? listExpr = Expression(); Whitespace(); Consume(TokenType.DM_RightParenthesis, "Expected ')' in for after expression 2"); - return new DMASTProcStatementFor(loc, new DMASTExpressionIn(loc, expr1, listExpr), null, null, dmTypes, GetForBody()); + return new DMASTProcStatementFor(loc, new DMASTExpressionIn(loc, expr1, listExpr), null, null, dmTypes, GetForBody(loc)); } if (!Check(ForSeparatorTypes)) { Consume(TokenType.DM_RightParenthesis, "Expected ')' in for after expression 1"); - return new DMASTProcStatementFor(loc, expr1, null, null, dmTypes, GetForBody()); + return new DMASTProcStatementFor(loc, expr1, null, null, dmTypes, GetForBody(loc)); } if (Check(TokenType.DM_RightParenthesis)) { - return new DMASTProcStatementFor(loc, expr1, null, null, dmTypes, GetForBody()); + return new DMASTProcStatementFor(loc, expr1, null, null, dmTypes, GetForBody(loc)); } Whitespace(); @@ -1179,11 +1179,11 @@ private DMASTProcStatementSet[] ProcSetEnd(bool allowMultiple) { if (!Check(ForSeparatorTypes)) { Consume(TokenType.DM_RightParenthesis, "Expected ')' in for after expression 2"); - return new DMASTProcStatementFor(loc, expr1, expr2, null, dmTypes, GetForBody()); + return new DMASTProcStatementFor(loc, expr1, expr2, null, dmTypes, GetForBody(loc)); } if (Check(TokenType.DM_RightParenthesis)) { - return new DMASTProcStatementFor(loc, expr1, expr2, null, dmTypes, GetForBody()); + return new DMASTProcStatementFor(loc, expr1, expr2, null, dmTypes, GetForBody(loc)); } Whitespace(); @@ -1197,25 +1197,27 @@ private DMASTProcStatementSet[] ProcSetEnd(bool allowMultiple) { } Consume(TokenType.DM_RightParenthesis, "Expected ')' in for after expression 3"); - return new DMASTProcStatementFor(loc, expr1, expr2, expr3, dmTypes, GetForBody()); + return new DMASTProcStatementFor(loc, expr1, expr2, expr3, dmTypes, GetForBody(loc)); } return null; - DMASTProcBlockInner GetForBody() { + DMASTProcBlockInner GetForBody(Location forLocation) { Whitespace(); Newline(); DMASTProcBlockInner? body = ProcBlock(); if (body == null) { var loc = Current().Location; - DMASTProcStatement? statement; if (Check(TokenType.DM_Semicolon)) { statement = new DMASTProcStatementExpression(loc, new DMASTConstantNull(loc)); } else { statement = ProcStatement(); - if (statement == null) Error("Expected body or statement"); + if (statement == null) { + DMCompiler.Emit(WarningCode.BadExpression, forLocation, "Expected body or statement"); + statement = new DMASTProcStatementExpression(loc, new DMASTConstantNull(loc)); // just so we can continue compiling. + } } body = new DMASTProcBlockInner(loc, statement); } @@ -1453,7 +1455,7 @@ public DMASTProcStatementSwitch.SwitchCase[] SwitchInner() { if (tryBody == null) { DMASTProcStatement? statement = ProcStatement(); - if (statement == null) Error("Expected body or statement"); + if (statement == null) Error(WarningCode.BadExpression, "Expected body or statement"); tryBody = new DMASTProcBlockInner(loc,statement); }