summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-parser.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2024-03-23 11:25:51 -0700
committerGitHub <noreply@github.com>2024-03-23 11:25:51 -0700
commitc9df734b836a503dbc09c48bfd54b35facd0f105 (patch)
tree165ecf9668e2f25d71e4327dd24c5de7dfd698dc /source/slang/slang-parser.cpp
parenta23adc221b1ea26db3f3313226b629eb9e308b0f (diff)
Allow anonymous struct. (#3822)
Diffstat (limited to 'source/slang/slang-parser.cpp')
-rw-r--r--source/slang/slang-parser.cpp92
1 files changed, 68 insertions, 24 deletions
diff --git a/source/slang/slang-parser.cpp b/source/slang/slang-parser.cpp
index 4522f977d..f7a0376ce 100644
--- a/source/slang/slang-parser.cpp
+++ b/source/slang/slang-parser.cpp
@@ -4811,8 +4811,15 @@ namespace Slang
// Skip completion request token to prevent producing a type named completion request.
AdvanceIf(this, TokenType::CompletionRequest);
- // TODO: support `struct` declaration without tag
- rs->nameAndLoc = expectIdentifier(this);
+ if (LookAheadToken(TokenType::Identifier))
+ {
+ rs->nameAndLoc = expectIdentifier(this);
+ }
+ else
+ {
+ rs->nameAndLoc.name = generateName(this);
+ rs->nameAndLoc.loc = rs->loc;
+ }
return parseOptGenericDecl(this, [&](GenericDecl*)
{
// We allow for an inheritance clause on a `struct`
@@ -5423,6 +5430,27 @@ namespace Slang
return statement;
}
+ // Look ahead token, skipping all modifiers.
+ bool lookAheadTokenAfterModifiers(Parser* parser, const char* token)
+ {
+ TokenReader tokenPreview = parser->tokenReader;
+ for (Index i = 0;; i++)
+ {
+ if (tokenPreview.peekToken().getContent() == token)
+ return true;
+ else if (auto syntaxDecl = tryLookUpSyntaxDecl(parser, tokenPreview.peekToken().getName()))
+ {
+ if (syntaxDecl->syntaxClass.isSubClassOf<Modifier>())
+ {
+ tokenPreview.advanceToken();
+ continue;
+ }
+ }
+ break;
+ }
+ return false;
+ }
+
Stmt* Parser::parseBlockStatement()
{
if(!beginMatch(this, MatchedTokenType::CurlyBraces))
@@ -5446,12 +5474,44 @@ namespace Slang
}
Token closingBraceToken;
+ auto addStmt = [&](Stmt* stmt)
+ {
+ if (!body)
+ {
+ body = stmt;
+ }
+ else if (auto seqStmt = as<SeqStmt>(body))
+ {
+ seqStmt->stmts.add(stmt);
+ }
+ else
+ {
+ SeqStmt* newBody = astBuilder->create<SeqStmt>();
+ newBody->loc = blockStatement->loc;
+ newBody->stmts.add(body);
+ newBody->stmts.add(stmt);
+
+ body = newBody;
+ }
+ };
while (!AdvanceIfMatch(this, MatchedTokenType::CurlyBraces, &closingBraceToken))
{
- if (LookAheadToken("struct"))
+ if (lookAheadTokenAfterModifiers(this, "struct"))
{
- auto structDecl = ParseStruct();
- AddMember(scopeDecl, structDecl);
+ auto declBase = ParseDecl(this, scopeDecl);
+ if (auto declGroup = as<DeclGroup>(declBase))
+ {
+ for (auto subDecl : declGroup->decls)
+ {
+ if (auto varDecl = as<VarDeclBase>(subDecl))
+ {
+ auto declStmt = astBuilder->create<DeclStmt>();
+ declStmt->loc = varDecl->loc;
+ declStmt->decl = varDecl;
+ addStmt(declStmt);
+ }
+ }
+ }
continue;
}
else if (AdvanceIf(this, "typedef"))
@@ -5468,26 +5528,10 @@ namespace Slang
}
auto stmt = ParseStatement();
- if(stmt)
- {
- if (!body)
- {
- body = stmt;
- }
- else if (auto seqStmt = as<SeqStmt>(body))
- {
- seqStmt->stmts.add(stmt);
- }
- else
- {
- SeqStmt* newBody = astBuilder->create<SeqStmt>();
- newBody->loc = blockStatement->loc;
- newBody->stmts.add(body);
- newBody->stmts.add(stmt);
- body = newBody;
- }
- }
+ if (stmt)
+ addStmt(stmt);
+
TryRecover(this);
}
PopScope();