Skip to content

Commit

Permalink
Cope with extra tokens after proc statements (#2116)
Browse files Browse the repository at this point in the history
  • Loading branch information
wixoaGit authored Dec 11, 2024
2 parents e2b4289 + 782bc7d commit 2a55a1c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// COMPILE ERROR OD3205
#pragma ExtraToken error

/proc/RunTest()
if(1).
ASSERT(TRUE)
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@
ASSERT(TRUE)
else
ASSERT(FALSE)
for(var/i in 1 to 1):
ASSERT(TRUE)
for(var/i in 1 to 1).
ASSERT(TRUE)
1 change: 1 addition & 0 deletions DMCompiler/Compiler/CompilerError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public enum WarningCode {
AssignmentInConditional = 3202,
PickWeightedSyntax = 3203,
AmbiguousInOrder = 3204,
ExtraToken = 3205,
RuntimeSearchOperator = 3300,

// 4000 - 4999 are reserved for runtime configuration. (TODO: Runtime doesn't know about configs yet!)
Expand Down
38 changes: 36 additions & 2 deletions DMCompiler/Compiler/DM/DMParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1125,8 +1125,10 @@ private DMASTProcStatementIf If() {

BracketWhitespace();
ConsumeRightParenthesis();
Whitespace();
Check(TokenType.DM_Colon);
if (Check(TokenType.DM_Colon) || Check(TokenType.DM_Period)) {
Emit(WarningCode.ExtraToken, loc, "Extra token at end of proc statement");
}

Whitespace();

DMASTProcStatement? procStatement = ProcStatement();
Expand Down Expand Up @@ -1165,6 +1167,10 @@ private DMASTProcStatement For() {
Whitespace();

if (Check(TokenType.DM_RightParenthesis)) {
if (Check(TokenType.DM_Colon) || Check(TokenType.DM_Period)) {
Emit(WarningCode.ExtraToken, loc, "Extra token at end of proc statement");
}

return new DMASTProcStatementInfLoop(loc, GetForBody(loc));
}

Expand All @@ -1185,6 +1191,10 @@ private DMASTProcStatement For() {
if (expr1 is DMASTAssign assign) {
ExpressionTo(out var endRange, out var step);
Consume(TokenType.DM_RightParenthesis, "Expected ')' in for after to expression");
if (Check(TokenType.DM_Colon) || Check(TokenType.DM_Period)) {
Emit(WarningCode.ExtraToken, loc, "Extra token at end of proc statement");
}

return new DMASTProcStatementFor(loc, new DMASTExpressionInRange(loc, assign.LHS, assign.RHS, endRange, step), null, null, dmTypes, GetForBody(loc));
} else {
Emit(WarningCode.BadExpression, "Expected = before to in for");
Expand All @@ -1197,15 +1207,27 @@ private DMASTProcStatement For() {
DMASTExpression? listExpr = Expression();
Whitespace();
Consume(TokenType.DM_RightParenthesis, "Expected ')' in for after expression 2");
if (Check(TokenType.DM_Colon) || Check(TokenType.DM_Period)) {
Emit(WarningCode.ExtraToken, loc, "Extra token at end of proc statement");
}

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");
if (Check(TokenType.DM_Colon) || Check(TokenType.DM_Period)) {
Emit(WarningCode.ExtraToken, loc, "Extra token at end of proc statement");
}

return new DMASTProcStatementFor(loc, expr1, null, null, dmTypes, GetForBody(loc));
}

if (Check(TokenType.DM_RightParenthesis)) {
if (Check(TokenType.DM_Colon) || Check(TokenType.DM_Period)) {
Emit(WarningCode.ExtraToken, loc, "Extra token at end of proc statement");
}

return new DMASTProcStatementFor(loc, expr1, null, null, dmTypes, GetForBody(loc));
}

Expand All @@ -1221,10 +1243,18 @@ private DMASTProcStatement For() {

if (!Check(ForSeparatorTypes)) {
Consume(TokenType.DM_RightParenthesis, "Expected ')' in for after expression 2");
if (Check(TokenType.DM_Colon) || Check(TokenType.DM_Period)) {
Emit(WarningCode.ExtraToken, loc, "Extra token at end of proc statement");
}

return new DMASTProcStatementFor(loc, expr1, expr2, null, dmTypes, GetForBody(loc));
}

if (Check(TokenType.DM_RightParenthesis)) {
if (Check(TokenType.DM_Colon) || Check(TokenType.DM_Period)) {
Emit(WarningCode.ExtraToken, loc, "Extra token at end of proc statement");
}

return new DMASTProcStatementFor(loc, expr1, expr2, null, dmTypes, GetForBody(loc));
}

Expand All @@ -1239,6 +1269,10 @@ private DMASTProcStatement For() {
}

Consume(TokenType.DM_RightParenthesis, "Expected ')' in for after expression 3");
if (Check(TokenType.DM_Colon) || Check(TokenType.DM_Period)) {
Emit(WarningCode.ExtraToken, loc, "Extra token at end of proc statement");
}

return new DMASTProcStatementFor(loc, expr1, expr2, expr3, dmTypes, GetForBody(loc));

DMASTProcBlockInner GetForBody(Location forLocation) {
Expand Down
1 change: 1 addition & 0 deletions DMCompiler/DMStandard/DefaultPragmaConfig.dm
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@
#pragma AssignmentInConditional warning
#pragma PickWeightedSyntax disabled
#pragma AmbiguousInOrder warning
#pragma ExtraToken warning
#pragma RuntimeSearchOperator disabled

0 comments on commit 2a55a1c

Please sign in to comment.