mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Decouple associated constants from let (#5973)
Decouples associated constants from being special cased in let handlers. Enforces associated constant grammar restrictions in parsing instead of checking. Closes #5411
This commit is contained in:
@@ -2,8 +2,11 @@
|
||||
// Exceptions. See /LICENSE for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
#include "toolchain/diagnostics/diagnostic.h"
|
||||
#include "toolchain/lex/token_kind.h"
|
||||
#include "toolchain/parse/context.h"
|
||||
#include "toolchain/parse/handle.h"
|
||||
#include "toolchain/parse/node_kind.h"
|
||||
|
||||
namespace Carbon::Parse {
|
||||
|
||||
@@ -11,14 +14,53 @@ auto HandleLet(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
// These will start at the `let`.
|
||||
context.PushState(state, StateKind::LetFinish);
|
||||
context.PushState(state, StateKind::LetAfterPattern);
|
||||
context.PushState(state, StateKind::LetFinishAsRegular);
|
||||
context.PushState(state, StateKind::LetAfterPatternAsRegular);
|
||||
|
||||
// This will start at the pattern.
|
||||
context.PushState(StateKind::Pattern);
|
||||
}
|
||||
|
||||
auto HandleLetAfterPattern(Context& context) -> void {
|
||||
auto HandleAssociatedConstant(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
// Parse the associated constant pattern: identifier :! type
|
||||
auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier);
|
||||
if (!identifier) {
|
||||
CARBON_DIAGNOSTIC(ExpectedAssociatedConstantIdentifier, Error,
|
||||
"expected identifier in associated constant declaration");
|
||||
context.emitter().Emit(*context.position(),
|
||||
ExpectedAssociatedConstantIdentifier);
|
||||
state.has_error = true;
|
||||
}
|
||||
|
||||
auto colon_exclaim = context.ConsumeIf(Lex::TokenKind::ColonExclaim);
|
||||
if (identifier && !colon_exclaim) {
|
||||
CARBON_DIAGNOSTIC(ExpectedAssociatedConstantColonExclaim, Error,
|
||||
"found runtime binding pattern in associated constant "
|
||||
"declaration; expected a `:!` binding");
|
||||
context.emitter().Emit(*context.position(),
|
||||
ExpectedAssociatedConstantColonExclaim);
|
||||
state.has_error = true;
|
||||
}
|
||||
|
||||
if (!identifier || !colon_exclaim) {
|
||||
auto end_token = context.SkipPastLikelyEnd(*(context.position() - 1));
|
||||
context.AddNode(NodeKind::AssociatedConstantDecl, end_token,
|
||||
/*has_error=*/true);
|
||||
state.has_error = true;
|
||||
return;
|
||||
}
|
||||
|
||||
context.AddLeafNode(NodeKind::IdentifierNameNotBeforeParams, *identifier);
|
||||
state.token = *colon_exclaim;
|
||||
context.PushState(state, StateKind::LetFinishAsAssociatedConstant);
|
||||
context.PushState(state, StateKind::LetAfterPatternAsAssociatedConstant);
|
||||
context.PushState(StateKind::Expr);
|
||||
}
|
||||
|
||||
static auto HandleLetAfterPattern(Context& context, NodeKind init_kind)
|
||||
-> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
if (state.has_error) {
|
||||
@@ -29,12 +71,24 @@ auto HandleLetAfterPattern(Context& context) -> void {
|
||||
}
|
||||
|
||||
if (auto equals = context.ConsumeIf(Lex::TokenKind::Equal)) {
|
||||
context.AddLeafNode(NodeKind::LetInitializer, *equals);
|
||||
context.AddLeafNode(init_kind, *equals);
|
||||
context.PushState(StateKind::Expr);
|
||||
}
|
||||
}
|
||||
|
||||
auto HandleLetFinish(Context& context) -> void {
|
||||
auto HandleLetAfterPatternAsRegular(Context& context) -> void {
|
||||
HandleLetAfterPattern(context, NodeKind::LetInitializer);
|
||||
}
|
||||
|
||||
auto HandleLetAfterPatternAsAssociatedConstant(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
context.AddNode(NodeKind::AssociatedConstantNameAndType, state.token,
|
||||
state.has_error);
|
||||
context.PushState(state);
|
||||
HandleLetAfterPattern(context, NodeKind::AssociatedConstantInitializer);
|
||||
}
|
||||
|
||||
static auto HandleLetFinish(Context& context, NodeKind node_kind) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
auto end_token = state.token;
|
||||
@@ -45,7 +99,15 @@ auto HandleLetFinish(Context& context) -> void {
|
||||
state.has_error = true;
|
||||
end_token = context.SkipPastLikelyEnd(state.token);
|
||||
}
|
||||
context.AddNode(NodeKind::LetDecl, end_token, state.has_error);
|
||||
context.AddNode(node_kind, end_token, state.has_error);
|
||||
}
|
||||
|
||||
auto HandleLetFinishAsRegular(Context& context) -> void {
|
||||
HandleLetFinish(context, NodeKind::LetDecl);
|
||||
}
|
||||
|
||||
auto HandleLetFinishAsAssociatedConstant(Context& context) -> void {
|
||||
HandleLetFinish(context, NodeKind::AssociatedConstantDecl);
|
||||
}
|
||||
|
||||
} // namespace Carbon::Parse
|
||||
|
||||
Reference in New Issue
Block a user