Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty for() loop body error location #1592

Merged
merged 4 commits into from
Jan 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions DMCompiler/Compiler/DM/DMParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand All @@ -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");
}
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}

Expand Down
Loading