Files
carbon-lang/toolchain/parser/parser_handle_declaration_name_and_params.cpp
T
Jon Ross-Perkins 446b0ce4ae Refactor declaration name context logic to its own class. (#2989)
This started with cleaning up the remaining Name/expression type punning
in the node stack, and grew. I'm factoring out a class because we've
previously expressed the desire to factor logic out of SemanticsContext
where possible, and this seemed like a reasonable cut.

NameExpression as the first node as a QualifiedExpression allows the
qualifier handling to consider Name in one less spot, an incremental
simplification. However, the additional complexity caused by this makes
me split ApplyNameQualifier/ApplyExpressionQualifier in order to avoid
repeat checks of the parse node's kind. The logic is still largely
shared, thus a couple helper functions. I think this is all fairly well
structured in the isolated class.

I can see that we may want to avoid passing SemanticsContext as an
argument in the future if it elides a step of lookup.
2023-07-17 21:55:29 +00:00

126 lines
4.3 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/parser/parser_context.h"
namespace Carbon {
// Handles DeclarationNameAndParamsAs(Optional|Required).
static auto ParserHandleDeclarationNameAndParams(ParserContext& context,
ParserState after_name)
-> void {
auto state = context.PopState();
// TODO: Should handle designated names.
if (auto identifier = context.ConsumeIf(TokenKind::Identifier)) {
state.state = after_name;
context.PushState(state);
if (context.PositionIs(TokenKind::Period)) {
// Because there's a qualifier, we process the first segment as an
// expression for simplicity. This just means semantics has one less thing
// to handle here.
context.AddLeafNode(ParseNodeKind::NameExpression, *identifier);
state.state = ParserState::PeriodAsDeclaration;
context.PushState(state);
} else {
context.AddLeafNode(ParseNodeKind::Name, *identifier);
}
} else {
CARBON_DIAGNOSTIC(ExpectedDeclarationName, Error,
"`{0}` introducer should be followed by a name.",
TokenKind);
context.emitter().Emit(*context.position(), ExpectedDeclarationName,
context.tokens().GetKind(state.token));
context.ReturnErrorOnState();
context.AddLeafNode(ParseNodeKind::InvalidParse, *context.position());
}
}
auto ParserHandleDeclarationNameAndParamsAsNone(ParserContext& context)
-> void {
ParserHandleDeclarationNameAndParams(
context, ParserState::DeclarationNameAndParamsAfterNameAsNone);
}
auto ParserHandleDeclarationNameAndParamsAsOptional(ParserContext& context)
-> void {
ParserHandleDeclarationNameAndParams(
context, ParserState::DeclarationNameAndParamsAfterNameAsOptional);
}
auto ParserHandleDeclarationNameAndParamsAsRequired(ParserContext& context)
-> void {
ParserHandleDeclarationNameAndParams(
context, ParserState::DeclarationNameAndParamsAfterNameAsRequired);
}
enum class Params {
None,
Optional,
Required,
};
static auto ParserHandleDeclarationNameAndParamsAfterName(
ParserContext& context, Params params) -> void {
auto state = context.PopState();
if (context.PositionIs(TokenKind::Period)) {
// Continue designator processing.
context.PushState(state);
state.state = ParserState::PeriodAsDeclaration;
context.PushState(state);
return;
}
if (params == Params::None) {
return;
}
if (context.PositionIs(TokenKind::OpenSquareBracket)) {
context.PushState(ParserState::DeclarationNameAndParamsAfterDeduced);
context.PushState(ParserState::ParameterListAsDeduced);
} else if (context.PositionIs(TokenKind::OpenParen)) {
context.PushState(ParserState::ParameterListAsRegular);
} else if (params == Params::Required) {
CARBON_DIAGNOSTIC(ParametersRequiredByIntroducer, Error,
"`{0}` requires a `(` for parameters.", TokenKind);
context.emitter().Emit(*context.position(), ParametersRequiredByIntroducer,
context.tokens().GetKind(state.token));
context.ReturnErrorOnState();
}
}
auto ParserHandleDeclarationNameAndParamsAfterNameAsNone(ParserContext& context)
-> void {
ParserHandleDeclarationNameAndParamsAfterName(context, Params::None);
}
auto ParserHandleDeclarationNameAndParamsAfterNameAsOptional(
ParserContext& context) -> void {
ParserHandleDeclarationNameAndParamsAfterName(context, Params::Optional);
}
auto ParserHandleDeclarationNameAndParamsAfterNameAsRequired(
ParserContext& context) -> void {
ParserHandleDeclarationNameAndParamsAfterName(context, Params::Required);
}
auto ParserHandleDeclarationNameAndParamsAfterDeduced(ParserContext& context)
-> void {
context.PopAndDiscardState();
if (context.PositionIs(TokenKind::OpenParen)) {
context.PushState(ParserState::ParameterListAsRegular);
} else {
CARBON_DIAGNOSTIC(
ParametersRequiredByDeduced, Error,
"A `(` for parameters is required after deduced parameters.");
context.emitter().Emit(*context.position(), ParametersRequiredByDeduced);
context.ReturnErrorOnState();
}
}
} // namespace Carbon