Change struct literal parsing to use placeholders. (#3850)

This is achieving a similar goal as #3849, using placeholders instead of
an ambiguous start node to clarify structure and incrementally simplify
checking. The benefit isn't quite as big here because both paths are
structs, and so checking is more consistent than paren exprs versus
tuples. But I think this removes the only other multi-purpose parse
node.

This uses StructLiteral/StructTypeLiteral naming, reflecting equivalent
SemIR naming. Note, I would lean towards renaming StructLiteral to
StructValueLiteral, but I think consistency in naming takes precedence.
Any renames of StructLiteral might be better in a separate PR.

StructFieldType/StructFieldValue -> StructTypeField/StructField is
trying to making the reading more consistent with
StructTypeLiteral/StructLiteral. SemIR has StructTypeField but not a
value equivalent.
This commit is contained in:
Jon Ross-Perkins
2024-04-03 20:36:49 +00:00
committed by GitHub
parent b42612bcec
commit a034f86272
31 changed files with 129 additions and 130 deletions
+13 -11
View File
@@ -10,9 +10,8 @@ auto HandleBraceExpr(Context& context) -> void {
auto state = context.PopState();
context.PushState(state, State::BraceExprFinishAsUnknown);
CARBON_CHECK(context.ConsumeAndAddLeafNodeIf(
Lex::TokenKind::OpenCurlyBrace,
NodeKind::StructLiteralOrStructTypeLiteralStart));
CARBON_CHECK(context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::OpenCurlyBrace,
NodeKind::Placeholder));
if (!context.PositionIs(Lex::TokenKind::CloseCurlyBrace)) {
context.PushState(State::BraceExprParamAsUnknown);
}
@@ -163,12 +162,12 @@ static auto HandleBraceExprParamFinish(Context& context, NodeKind node_kind,
}
auto HandleBraceExprParamFinishAsType(Context& context) -> void {
HandleBraceExprParamFinish(context, NodeKind::StructFieldType,
HandleBraceExprParamFinish(context, NodeKind::StructTypeField,
State::BraceExprParamAsType);
}
auto HandleBraceExprParamFinishAsValue(Context& context) -> void {
HandleBraceExprParamFinish(context, NodeKind::StructFieldValue,
HandleBraceExprParamFinish(context, NodeKind::StructField,
State::BraceExprParamAsValue);
}
@@ -178,24 +177,27 @@ auto HandleBraceExprParamFinishAsUnknown(Context& context) -> void {
}
// Handles BraceExprFinishAs(Type|Value|Unknown).
static auto HandleBraceExprFinish(Context& context, NodeKind node_kind)
-> void {
static auto HandleBraceExprFinish(Context& context, NodeKind start_kind,
NodeKind end_kind) -> void {
auto state = context.PopState();
context.AddNode(node_kind, context.Consume(), state.subtree_start,
context.ReplacePlaceholderNode(state.subtree_start, start_kind, state.token);
context.AddNode(end_kind, context.Consume(), state.subtree_start,
state.has_error);
}
auto HandleBraceExprFinishAsType(Context& context) -> void {
HandleBraceExprFinish(context, NodeKind::StructTypeLiteral);
HandleBraceExprFinish(context, NodeKind::StructTypeLiteralStart,
NodeKind::StructTypeLiteral);
}
auto HandleBraceExprFinishAsValue(Context& context) -> void {
HandleBraceExprFinish(context, NodeKind::StructLiteral);
HandleBraceExprFinish(context, NodeKind::StructLiteralStart,
NodeKind::StructLiteral);
}
auto HandleBraceExprFinishAsUnknown(Context& context) -> void {
HandleBraceExprFinish(context, NodeKind::StructLiteral);
HandleBraceExprFinishAsValue(context);
}
} // namespace Carbon::Parse