Files
carbon-lang/toolchain/parser/parser_handle_parameter.cpp
T
Jon Ross-Perkins c9d2335a34 Refactor parser logic into separate files. (#2818)
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.
2023-05-15 14:44:54 -07:00

109 lines
4.2 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 ParameterAs(Deduced|Regular).
static auto ParserHandleParameter(ParserContext& context,
ParserState pattern_state,
ParserState finish_state) -> void {
context.PopAndDiscardState();
context.PushState(finish_state);
context.PushState(pattern_state);
}
auto ParserHandleParameterAsDeduced(ParserContext& context) -> void {
ParserHandleParameter(context, ParserState::PatternAsDeducedParameter,
ParserState::ParameterFinishAsDeduced);
}
auto ParserHandleParameterAsRegular(ParserContext& context) -> void {
ParserHandleParameter(context, ParserState::PatternAsParameter,
ParserState::ParameterFinishAsRegular);
}
// Handles ParameterFinishAs(Deduced|Regular).
static auto ParserHandleParameterFinish(ParserContext& context,
TokenKind close_token,
ParserState param_state) -> void {
auto state = context.PopState();
if (state.has_error) {
context.ReturnErrorOnState();
}
if (context.ConsumeListToken(ParseNodeKind::ParameterListComma, close_token,
state.has_error) ==
ParserContext::ListTokenKind::Comma) {
context.PushState(param_state);
}
}
auto ParserHandleParameterFinishAsDeduced(ParserContext& context) -> void {
ParserHandleParameterFinish(context, TokenKind::CloseSquareBracket,
ParserState::ParameterAsDeduced);
}
auto ParserHandleParameterFinishAsRegular(ParserContext& context) -> void {
ParserHandleParameterFinish(context, TokenKind::CloseParen,
ParserState::ParameterAsRegular);
}
// Handles ParameterListAs(Deduced|Regular).
static auto ParserHandleParameterList(ParserContext& context,
ParseNodeKind parse_node_kind,
TokenKind open_token_kind,
TokenKind close_token_kind,
ParserState param_state,
ParserState finish_state) -> void {
context.PopAndDiscardState();
context.PushState(finish_state);
context.AddLeafNode(parse_node_kind, context.ConsumeChecked(open_token_kind));
if (!context.PositionIs(close_token_kind)) {
context.PushState(param_state);
}
}
auto ParserHandleParameterListAsDeduced(ParserContext& context) -> void {
ParserHandleParameterList(context, ParseNodeKind::DeducedParameterListStart,
TokenKind::OpenSquareBracket,
TokenKind::CloseSquareBracket,
ParserState::ParameterAsDeduced,
ParserState::ParameterListFinishAsDeduced);
}
auto ParserHandleParameterListAsRegular(ParserContext& context) -> void {
ParserHandleParameterList(context, ParseNodeKind::ParameterListStart,
TokenKind::OpenParen, TokenKind::CloseParen,
ParserState::ParameterAsRegular,
ParserState::ParameterListFinishAsRegular);
}
// Handles ParameterListFinishAs(Deduced|Regular).
static auto ParserHandleParameterListFinish(ParserContext& context,
ParseNodeKind parse_node_kind,
TokenKind token_kind) -> void {
auto state = context.PopState();
context.AddNode(parse_node_kind, context.ConsumeChecked(token_kind),
state.subtree_start, state.has_error);
}
auto ParserHandleParameterListFinishAsDeduced(ParserContext& context) -> void {
ParserHandleParameterListFinish(context, ParseNodeKind::DeducedParameterList,
TokenKind::CloseSquareBracket);
}
auto ParserHandleParameterListFinishAsRegular(ParserContext& context) -> void {
ParserHandleParameterListFinish(context, ParseNodeKind::ParameterList,
TokenKind::CloseParen);
}
} // namespace Carbon