Parse vars in classes the same as vars in other locations (#7188)

Class vars are still restricted to simple `name: type` bindings, not
full patterns. This is now handled in the check phase instead of during
parsing.

This is in preparation for supporting `static var`.
This commit is contained in:
Nicholas Bishop
2026-05-13 21:22:46 +00:00
committed by GitHub
parent 5706601096
commit bd918fb33b
91 changed files with 370 additions and 660 deletions
+8 -56
View File
@@ -19,7 +19,7 @@ static auto HandleVar(Context& context, StateKind finish_state_kind,
// TODO: is there a cleaner way to give VarAfterPattern access to the `var`
// token?
state.token = *(context.position() - 1);
context.PushState(state, StateKind::VarAfterPatternAsVar);
context.PushState(state, StateKind::VarAfterPattern);
if (returned_token.has_value()) {
context.AddLeafNode(NodeKind::ReturnedModifier, returned_token);
@@ -31,7 +31,7 @@ static auto HandleVar(Context& context, StateKind finish_state_kind,
}
auto HandleVarAsRegular(Context& context) -> void {
HandleVar(context, StateKind::VarFinishAsRegular);
HandleVar(context, StateKind::VarFinish);
}
auto HandleVarAsReturned(Context& context) -> void {
@@ -49,40 +49,10 @@ auto HandleVarAsReturned(Context& context) -> void {
}
context.AddLeafNode(NodeKind::VariableIntroducer, context.Consume());
HandleVar(context, StateKind::VarFinishAsRegular, returned_token);
HandleVar(context, StateKind::VarFinish, returned_token);
}
auto HandleFieldDecl(Context& context) -> void {
auto state = context.PopState();
auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier);
if (!identifier) {
CARBON_DIAGNOSTIC(ExpectedFieldIdentifier, Error,
"expected identifier in field declaration");
context.emitter().Emit(*context.position(), ExpectedFieldIdentifier);
}
auto colon = context.ConsumeIf(Lex::TokenKind::Colon);
if (identifier && !colon) {
CARBON_DIAGNOSTIC(ExpectedFieldColon, Error,
"expected `:` in field declaration");
context.emitter().Emit(*context.position(), ExpectedFieldColon);
}
if (!identifier || !colon) {
context.AddNode(NodeKind::FieldDecl,
context.SkipPastLikelyEnd(*(context.position() - 1)),
/*has_error=*/true);
state.has_error = true;
return;
}
context.PushState(state, StateKind::VarFinishAsField);
context.AddLeafNode(NodeKind::IdentifierNameNotBeforeSignature, *identifier);
state.token = *colon;
context.PushState(state, StateKind::VarAfterPatternAsField);
context.PushState(StateKind::Expr);
}
static auto HandleVarAfterPattern(Context& context, NodeKind pattern_kind,
NodeKind init_kind) -> void {
auto HandleVarAfterPattern(Context& context) -> void {
auto state = context.PopState();
if (state.has_error) {
@@ -92,26 +62,16 @@ static auto HandleVarAfterPattern(Context& context, NodeKind pattern_kind,
}
}
context.AddNode(pattern_kind, state.token, state.has_error);
context.AddNode(NodeKind::VariablePattern, state.token, state.has_error);
if (context.PositionIs(Lex::TokenKind::Equal)) {
context.AddLeafNode(init_kind,
context.AddLeafNode(NodeKind::VariableInitializer,
context.ConsumeChecked(Lex::TokenKind::Equal));
context.PushState(StateKind::Expr);
}
}
auto HandleVarAfterPatternAsVar(Context& context) -> void {
HandleVarAfterPattern(context, NodeKind::VariablePattern,
NodeKind::VariableInitializer);
}
auto HandleVarAfterPatternAsField(Context& context) -> void {
HandleVarAfterPattern(context, NodeKind::FieldNameAndType,
NodeKind::FieldInitializer);
}
static auto HandleVarFinish(Context& context, NodeKind node_kind) -> void {
auto HandleVarFinish(Context& context) -> void {
auto state = context.PopState();
auto end_token = state.token;
@@ -123,15 +83,7 @@ static auto HandleVarFinish(Context& context, NodeKind node_kind) -> void {
state.has_error = true;
end_token = context.SkipPastLikelyEnd(state.token);
}
context.AddNode(node_kind, end_token, state.has_error);
}
auto HandleVarFinishAsRegular(Context& context) -> void {
HandleVarFinish(context, NodeKind::VariableDecl);
}
auto HandleVarFinishAsField(Context& context) -> void {
HandleVarFinish(context, NodeKind::FieldDecl);
context.AddNode(NodeKind::VariableDecl, end_token, state.has_error);
}
auto HandleVariablePattern(Context& context) -> void {