1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 04:22:20 +02:00

Improved semicolon handling for inline anonymous type references

This commit is contained in:
Brian Fiete 2025-01-04 12:47:45 -08:00
parent 34cfe603e6
commit a6a891d800
3 changed files with 36 additions and 8 deletions

View file

@ -15588,11 +15588,12 @@ void BfExprEvaluator::CheckObjectCreateTypeRef(BfType* expectingType, BfAstNode*
auto arrayType = (BfArrayType*)expectingType; auto arrayType = (BfArrayType*)expectingType;
expectingType = arrayType->mGenericTypeInfo->mTypeGenericArguments[0]; expectingType = arrayType->mGenericTypeInfo->mTypeGenericArguments[0];
} }
auto expectingTypeInst = expectingType->ToTypeInstance(); auto expectingTypeInst = expectingType->ToTypeInstance();
if (expectingTypeInst != NULL) if (expectingTypeInst != NULL)
{ {
autoComplete->AddTypeInstanceEntry(expectingTypeInst); if (!expectingTypeInst->IsAnonymous())
autoComplete->AddTypeInstanceEntry(expectingTypeInst);
} }
else else
autoComplete->mDefaultSelection = mModule->TypeToString(expectingType); autoComplete->mDefaultSelection = mModule->TypeToString(expectingType);

View file

@ -8230,13 +8230,24 @@ BfScopedInvocationTarget* BfReducer::CreateScopedInvocationTarget(BfAstNode*& ta
void BfReducer::InitAnonymousType(BfTypeDeclaration* typeDecl) void BfReducer::InitAnonymousType(BfTypeDeclaration* typeDecl)
{ {
auto block = BfNodeDynCast<BfBlock>(typeDecl->mDefineNode); auto block = BfNodeDynCast<BfBlock>(typeDecl->mDefineNode);
int blockId = 0;
if (blockId != NULL)
{
blockId = block->mParserBlockId;
}
else
{
auto parser = mSource->ToParser();
if (parser != NULL)
blockId = parser->mCurBlockId + typeDecl->mSrcStart;
}
String name; String name;
auto parserData = typeDecl->GetParserData(); auto parserData = typeDecl->GetParserData();
name = "_Anon_"; name = "_Anon_";
auto parseFileData = parserData->mParseFileData; auto parseFileData = parserData->mParseFileData;
int uniqueId = parseFileData->GetUniqueId(block->mParserBlockId); int uniqueId = parseFileData->GetUniqueId(blockId);
name += StrFormat("%d", uniqueId); name += StrFormat("%d", uniqueId);
int len = (int)name.length() + 1; int len = (int)name.length() + 1;
@ -9464,12 +9475,20 @@ BfAstNode* BfReducer::CreateTopLevelObject(BfTokenNode* tokenNode, BfAttributeDi
if (baseTypeIdx > 0) if (baseTypeIdx > 0)
{ {
bool hasComma = false;
if (auto tokenNode = BfNodeDynCast<BfTokenNode>(nextNode)) if (auto tokenNode = BfNodeDynCast<BfTokenNode>(nextNode))
{ {
if (tokenNode->mToken == BfToken_Semicolon) if ((tokenNode->mToken == BfToken_Semicolon) && (!isAnonymous))
{ break;
break; if (tokenNode->mToken == BfToken_Comma)
} hasComma = true;
}
if ((!hasComma) && (isAnonymous))
{
// End type declaration
break;
} }
BfTokenNode* commaToken = NULL; BfTokenNode* commaToken = NULL;
@ -9551,7 +9570,7 @@ BfAstNode* BfReducer::CreateTopLevelObject(BfTokenNode* tokenNode, BfAttributeDi
if (tokenNode != NULL) if (tokenNode != NULL)
{ {
if (tokenNode->GetToken() == BfToken_Semicolon) if ((tokenNode->GetToken() == BfToken_Semicolon) && (!isAnonymous))
{ {
typeDeclaration->mDefineNode = tokenNode; typeDeclaration->mDefineNode = tokenNode;
MoveNode(tokenNode, typeDeclaration); MoveNode(tokenNode, typeDeclaration);
@ -9560,6 +9579,9 @@ BfAstNode* BfReducer::CreateTopLevelObject(BfTokenNode* tokenNode, BfAttributeDi
} }
} }
if ((isAnonymous) && (!BfNodeIsA<BfBlock>(nextNode)))
return typeDeclaration;
auto blockNode = ExpectBlockAfter(typeDeclaration); auto blockNode = ExpectBlockAfter(typeDeclaration);
if (blockNode != NULL) if (blockNode != NULL)
{ {

View file

@ -45,6 +45,8 @@ class Anonymous
public int Val = 123; public int Val = 123;
} }
public static class : this(int x, int y) sValA = new .(3, 4) ~ delete _;
[Test] [Test]
public static void TestBasics() public static void TestBasics()
{ {
@ -85,5 +87,8 @@ class Anonymous
var str = ca.ToString(.. scope .()); var str = ca.ToString(.. scope .());
Test.Assert(str == "ClassA override"); Test.Assert(str == "ClassA override");
Test.Assert(sValA.x == 3);
Test.Assert(sValA.y == 4);
} }
} }