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

Don't throw WarningCode.EmptyBlock on semicolon bodies #1491

Closed
Closed
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions DMCompiler/Compiler/DM/DMAST.cs
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,12 @@ public sealed class DMASTProcBlockInner : DMASTNode {
/// </remarks>
public readonly DMASTProcStatement[] SetStatements;

/// <summary>
/// This is used to determine if we should flag for <c>WarningCode.EmptyBlock</c>,
/// which we don't do if there was a manually entered semicolon within.
/// </summary>
public bool NotCompletelyEmpty;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitely open to a wording change on this


/// <summary> Initializes an empty block. </summary>
public DMASTProcBlockInner(Location location) : base(location) {
Statements = Array.Empty<DMASTProcStatement>();
Expand Down
57 changes: 47 additions & 10 deletions DMCompiler/Compiler/DM/DMParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ public DMASTFile File() {
var loc = Current().Location;
if (Check(TokenType.DM_LeftCurlyBracket)) {
DMASTProcBlockInner? block;
bool hadSemicolon = false;

Whitespace();
Newline();
Expand All @@ -535,9 +536,13 @@ public DMASTFile File() {
List<DMASTProcStatement> setStatements = new(); // set statements are weird and must be held separately.

do {
(List<DMASTProcStatement>? stmts, List<DMASTProcStatement>? setStmts) = ProcBlockInner(); // Hope you understand tuples
if (stmts is not null) statements.AddRange(stmts);
if (setStmts is not null) setStatements.AddRange(setStmts);
(List<DMASTProcStatement>? stmts, List<DMASTProcStatement>? setStmts, bool innerHadSemicolon) = ProcBlockInner(); // Hope you understand tuples
if (stmts is not null)
statements.AddRange(stmts);
if (setStmts is not null)
setStatements.AddRange(setStmts);
if (innerHadSemicolon)
hadSemicolon = innerHadSemicolon;

if (!Check(TokenType.DM_RightCurlyBracket)) {
Error(WarningCode.BadToken, "Expected end of braced block");
Expand All @@ -550,7 +555,10 @@ public DMASTFile File() {
}
} while (true);

block = new DMASTProcBlockInner(loc, statements.ToArray(), setStatements.ToArray());
block = new DMASTProcBlockInner(loc, statements.ToArray(), setStatements.ToArray())
{
NotCompletelyEmpty = hadSemicolon
};
}

return block;
Expand All @@ -564,13 +572,16 @@ public DMASTFile File() {
if (Check(TokenType.DM_Indent)) {
List<DMASTProcStatement> statements = new();
List<DMASTProcStatement> setStatements = new(); // set statements are weird and must be held separately.
bool hadSemicolon = false;

do {
(List<DMASTProcStatement>? statements, List<DMASTProcStatement>? setStatements) blockInner = ProcBlockInner();
(List<DMASTProcStatement>? statements, List<DMASTProcStatement>? setStatements, bool hadSemicolon) blockInner = ProcBlockInner();
if (blockInner.statements is not null)
statements.AddRange(blockInner.statements);
if (blockInner.setStatements is not null)
setStatements.AddRange(blockInner.setStatements);
if (blockInner.hadSemicolon)
hadSemicolon = blockInner.hadSemicolon;

if (!Check(TokenType.DM_Dedent)) {
Error("Expected end of proc statement", throwException: false);
Expand All @@ -581,17 +592,26 @@ public DMASTFile File() {
}
} while (true);

return new DMASTProcBlockInner(loc, statements.ToArray(), setStatements.ToArray());
return new DMASTProcBlockInner(loc, statements.ToArray(), setStatements.ToArray())
{
NotCompletelyEmpty = hadSemicolon
};
}

return null;
}

public (List<DMASTProcStatement>?, List<DMASTProcStatement>?) ProcBlockInner() {
/// <summary>
/// Parses the inner of a proc block
/// </summary>
/// <remarks> We return if there was a semicolon within for <c>WarningCode.EmptyBlock</c></remarks>
/// <returns>(list of proc statements, list of set statements, had a semicolon within)</returns>
public (List<DMASTProcStatement>?, List<DMASTProcStatement>?, bool) ProcBlockInner() {
List<DMASTProcStatement> procStatements = new();
List<DMASTProcStatement> setStatements = new(); // We have to store them separately because they're evaluated first

DMASTProcStatement? statement = null;
bool hadSemicolon = false;
do {
Whitespace();

Expand All @@ -611,11 +631,14 @@ public DMASTFile File() {
DMASTProcBlockInner? blockInner = ProcBlock();
if (blockInner != null) procStatements.AddRange(blockInner.Statements);
}
} while (Delimiter() || statement is DMASTProcStatementLabel);
} while (DelimiterHasSemicolon(ref hadSemicolon) || statement is DMASTProcStatementLabel);

Whitespace();

if (procStatements.Count == 0) return (null,null);
return (procStatements, setStatements);
if (procStatements.Count == 0)
return (null, null, hadSemicolon);

return (procStatements, setStatements, hadSemicolon);
}

public DMASTProcStatement? ProcStatement() {
Expand Down Expand Up @@ -2617,5 +2640,19 @@ private bool Delimiter() {

return hasDelimiter;
}

private bool DelimiterHasSemicolon(ref bool hasSemicolon) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also this

bool hasDelimiter = false;
while (
// Once it is set to true, keep it that way
(!hasSemicolon && (hasSemicolon = Current().Type == TokenType.DM_Semicolon))
|| Check(TokenType.DM_Semicolon)
|| Newline()
) {
hasDelimiter = true;
}

return hasDelimiter;
}
}
}
2 changes: 1 addition & 1 deletion DMCompiler/DM/Visitors/DMProcBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private void ProcessBlockInner(DMASTProcBlockInner block, bool silenceEmptyBlock
DMCompiler.Emit(e.Error);
}
}
if(!silenceEmptyBlockWarning && block.Statements.Length == 0) { // If this block has no real statements
if(!silenceEmptyBlockWarning && block.Statements.Length == 0 && !block.NotCompletelyEmpty) { // If this block has no real statements
// Not an error in BYOND, but we do have an emission for this!
if(block.SetStatements.Length != 0) { // Give a more articulate message about this, since it's kinda weird
DMCompiler.Emit(WarningCode.EmptyBlock,block.Location,"Empty block detected - set statements are executed outside of, before, and unconditional to, this block");
Expand Down
Loading