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
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 16 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 @@ -1119,7 +1119,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 @@ -1139,7 +1139,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 @@ -1150,16 +1150,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 @@ -1174,11 +1174,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 @@ -1192,25 +1192,28 @@ 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) {
Error(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 @@ -1448,7 +1451,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
7 changes: 7 additions & 0 deletions DMCompiler/Compiler/DM/DMParserHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ protected bool Error(WarningCode code, string message) {
return level == ErrorLevel.Error;
}

protected bool Error(WarningCode code, Location location, string message) {
ErrorLevel level = DMCompiler.CodeToLevel(code);
if (Emissions.Count < MAX_EMISSIONS_RECORDED)
Emissions.Add(new CompilerEmission(level, code, location, message));
return level == ErrorLevel.Error;
}

/// <inheritdoc cref="Parser{SourceType}.Error(string, bool)"/>
[Obsolete("This is not a desirable way for DMParser to emit an error, as errors should emit an error code and not cause unnecessary throws. Use DMParser's overrides of this method, instead.")]
new protected void Error(string message, bool throwException = true) {
Expand Down
Loading