From 945d8dd3c4cea58f3d9f36e8fa123137f64e180e Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 10 Dec 2024 03:47:16 -0800 Subject: Fix parsing logic of for loops' initial statement. (#5813) --- source/slang/slang-parser.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'source/slang/slang-parser.cpp') diff --git a/source/slang/slang-parser.cpp b/source/slang/slang-parser.cpp index 172e3130f..4dc962ce6 100644 --- a/source/slang/slang-parser.cpp +++ b/source/slang/slang-parser.cpp @@ -5953,22 +5953,25 @@ ForStmt* Parser::ParseForStatement() FillPosition(stmt); ReadToken("for"); ReadToken(TokenType::LParent); - auto modifiers = ParseModifiers(this); - if (peekTypeName(this) || !modifiers.isEmpty()) - { - stmt->initialStatement = parseVarDeclrStatement(modifiers); - } - else + if (!LookAheadToken(TokenType::Semicolon)) { - if (!LookAheadToken(TokenType::Semicolon)) + stmt->initialStatement = ParseStatement(); + if (as(stmt->initialStatement) || as(stmt->initialStatement)) { - stmt->initialStatement = ParseExpressionStatement(); + // These are the only allowed form of initial statements of a for loop. } else { - ReadToken(TokenType::Semicolon); + sink->diagnose( + stmt->initialStatement->loc, + Diagnostics::unexpectedTokenExpectedTokenType, + "expression"); } } + else + { + ReadToken(TokenType::Semicolon); + } if (!LookAheadToken(TokenType::Semicolon)) stmt->predicateExpression = ParseExpression(); ReadToken(TokenType::Semicolon); -- cgit v1.2.3