From 37143802781d8d480361d7c23202347ae3acf094 Mon Sep 17 00:00:00 2001 From: Yong He Date: Mon, 14 Jul 2025 18:51:57 -0700 Subject: Fix language server crash. (#7756) * Fix language server crash. * Fix tests. * Fix. * Revert changes. --- source/slang/slang-parser.cpp | 82 +++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 45 deletions(-) (limited to 'source/slang/slang-parser.cpp') diff --git a/source/slang/slang-parser.cpp b/source/slang/slang-parser.cpp index 6401b3d06..0efa6a4b3 100644 --- a/source/slang/slang-parser.cpp +++ b/source/slang/slang-parser.cpp @@ -1859,53 +1859,46 @@ static Stmt* parseOptBody(Parser* parser) } } - if (parser->getStage() == ParsingStage::Decl) - { - // If we are at the initial parsing stage, just collect the tokens - // without actually parsing them. - if (peekTokenType(parser) != TokenType::LBrace) - { - return parser->parseBlockStatement(); - } - auto unparsedStmt = parser->astBuilder->create(); - unparsedStmt->currentScope = parser->currentScope; - unparsedStmt->outerScope = parser->outerScope; - unparsedStmt->sourceLanguage = parser->getSourceLanguage(); - unparsedStmt->isInVariadicGenerics = parser->isInVariadicGenerics; - parser->FillPosition(unparsedStmt); - List& tokens = unparsedStmt->tokens; - int braceDepth = 0; - for (;;) + // If we are at the initial parsing stage, just collect the tokens + // without actually parsing them. + if (peekTokenType(parser) != TokenType::LBrace) + { + return parser->parseBlockStatement(); + } + auto unparsedStmt = parser->astBuilder->create(); + unparsedStmt->currentScope = parser->currentScope; + unparsedStmt->outerScope = parser->outerScope; + unparsedStmt->sourceLanguage = parser->getSourceLanguage(); + unparsedStmt->isInVariadicGenerics = parser->isInVariadicGenerics; + parser->FillPosition(unparsedStmt); + List& tokens = unparsedStmt->tokens; + int braceDepth = 0; + for (;;) + { + auto token = parser->ReadToken(); + if (token.type == TokenType::EndOfFile) { - auto token = parser->ReadToken(); - if (token.type == TokenType::EndOfFile) - { - break; - } - if (token.type == TokenType::LBrace) - { - braceDepth++; - } - else if (token.type == TokenType::RBrace) - { - braceDepth--; - } - tokens.add(token); - if (braceDepth == 0) - { - break; - } + break; + } + if (token.type == TokenType::LBrace) + { + braceDepth++; + } + else if (token.type == TokenType::RBrace) + { + braceDepth--; + } + tokens.add(token); + if (braceDepth == 0) + { + break; } - Token eofToken; - eofToken.type = TokenType::EndOfFile; - eofToken.loc = parser->tokenReader.peekLoc(); - tokens.add(eofToken); - return unparsedStmt; } - - // If we are in the second stage of parsing, then we need to actually - // parse the block statement for real. - return parser->parseBlockStatement(); + Token eofToken; + eofToken.type = TokenType::EndOfFile; + eofToken.loc = parser->tokenReader.peekLoc(); + tokens.add(eofToken); + return unparsedStmt; } /// Complete parsing of a function using traditional (C-like) declarator syntax @@ -5500,7 +5493,6 @@ static EnumCaseDecl* parseEnumCaseDecl(Parser* parser) static Decl* parseEnumDecl(Parser* parser) { EnumDecl* decl = parser->astBuilder->create(); - parser->ReadToken("enum"); // HACK: allow the user to write `enum class` in case -- cgit v1.2.3