mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
The goal of this change is to start refactoring the monolithic file into separate files that will hopefully pose fewer conflicts for developers, and make it easier to skip to handling of specific functionality. It additionally addresses a scaling issue with parser.cpp where the file would continue to get larger as more features are added. Switches Parser to a ParserContext, moves handlers to be free functions, and moves the controller logic into ParseTree. parser_handle_states.h does the declarations for handlers and little else; handlers are split out to individual files based on prefix (which is deliberately authored to cluster). A couple things I'm avoiding based on historical discussion are: - Having a subdirectory for all the handlers, such as `toolchain/parser/handlers/call_expression.cpp` - Putting handlers in a namespace, such as `Carbon::ParserHandler::CallExpression`. - The name of `Carbon::ParserHandlerCallExpression` is then necessary to minimize the chance of conflicts with semantics and lowering, where everything can be expected to be named similarly. I'm globbing handlers because it seems hard to see missed ones under this approach -- names are too boilerplate. I think this current setup could be split into target-per-file, but I'm not sure that's needed, so I'd delay until it becomes a build-time issue.
99 lines
3.6 KiB
C++
99 lines
3.6 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 {
|
|
|
|
auto ParserHandleFunctionIntroducer(ParserContext& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
context.AddLeafNode(ParseNodeKind::FunctionIntroducer, context.Consume());
|
|
|
|
state.state = ParserState::FunctionAfterParameters;
|
|
context.PushState(state);
|
|
state.state = ParserState::DeclarationNameAndParamsAsRequired;
|
|
context.PushState(state);
|
|
}
|
|
|
|
auto ParserHandleFunctionAfterParameters(ParserContext& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
// Regardless of whether there's a return type, we'll finish the signature.
|
|
state.state = ParserState::FunctionSignatureFinish;
|
|
context.PushState(state);
|
|
|
|
// If there is a return type, parse the expression before adding the return
|
|
// type nod.e
|
|
if (context.PositionIs(TokenKind::MinusGreater)) {
|
|
context.PushState(ParserState::FunctionReturnTypeFinish);
|
|
++context.position();
|
|
context.PushStateForExpression(PrecedenceGroup::ForType());
|
|
}
|
|
}
|
|
|
|
auto ParserHandleFunctionReturnTypeFinish(ParserContext& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
context.AddNode(ParseNodeKind::ReturnType, state.token, state.subtree_start,
|
|
state.has_error);
|
|
}
|
|
|
|
auto ParserHandleFunctionSignatureFinish(ParserContext& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
switch (context.PositionKind()) {
|
|
case TokenKind::Semi: {
|
|
context.AddNode(ParseNodeKind::FunctionDeclaration, context.Consume(),
|
|
state.subtree_start, state.has_error);
|
|
break;
|
|
}
|
|
case TokenKind::OpenCurlyBrace: {
|
|
if (auto decl_context = context.GetDeclarationContext();
|
|
decl_context == ParserContext::DeclarationContext::Interface ||
|
|
decl_context == ParserContext::DeclarationContext::NamedConstraint) {
|
|
CARBON_DIAGNOSTIC(
|
|
MethodImplNotAllowed, Error,
|
|
"Method implementations are not allowed in interfaces.");
|
|
context.emitter().Emit(*context.position(), MethodImplNotAllowed);
|
|
context.RecoverFromDeclarationError(state,
|
|
ParseNodeKind::FunctionDeclaration,
|
|
/*skip_past_likely_end=*/true);
|
|
break;
|
|
}
|
|
|
|
context.AddNode(ParseNodeKind::FunctionDefinitionStart, context.Consume(),
|
|
state.subtree_start, state.has_error);
|
|
// Any error is recorded on the FunctionDefinitionStart.
|
|
state.has_error = false;
|
|
state.state = ParserState::FunctionDefinitionFinish;
|
|
context.PushState(state);
|
|
context.PushState(ParserState::StatementScopeLoop);
|
|
break;
|
|
}
|
|
default: {
|
|
if (!state.has_error) {
|
|
context.emitter().Emit(*context.position(),
|
|
ExpectedDeclarationSemiOrDefinition,
|
|
TokenKind::Fn);
|
|
}
|
|
// Only need to skip if we've not already found a new line.
|
|
bool skip_past_likely_end =
|
|
context.tokens().GetLine(*context.position()) ==
|
|
context.tokens().GetLine(state.token);
|
|
context.RecoverFromDeclarationError(
|
|
state, ParseNodeKind::FunctionDeclaration, skip_past_likely_end);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
auto ParserHandleFunctionDefinitionFinish(ParserContext& context) -> void {
|
|
auto state = context.PopState();
|
|
context.AddNode(ParseNodeKind::FunctionDefinition, context.Consume(),
|
|
state.subtree_start, state.has_error);
|
|
}
|
|
|
|
} // namespace Carbon
|