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.
This commit is contained in:
Jon Ross-Perkins
2023-05-15 14:44:54 -07:00
committed by GitHub
parent 8aca184cdb
commit c9d2335a34
22 changed files with 2259 additions and 2089 deletions
@@ -0,0 +1,41 @@
// 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 ParserHandleCodeBlock(ParserContext& context) -> void {
context.PopAndDiscardState();
context.PushState(ParserState::CodeBlockFinish);
if (context.ConsumeAndAddLeafNodeIf(TokenKind::OpenCurlyBrace,
ParseNodeKind::CodeBlockStart)) {
context.PushState(ParserState::StatementScopeLoop);
} else {
context.AddLeafNode(ParseNodeKind::CodeBlockStart, *context.position(),
/*has_error=*/true);
// Recover by parsing a single statement.
CARBON_DIAGNOSTIC(ExpectedCodeBlock, Error, "Expected braced code block.");
context.emitter().Emit(*context.position(), ExpectedCodeBlock);
context.PushState(ParserState::Statement);
}
}
auto ParserHandleCodeBlockFinish(ParserContext& context) -> void {
auto state = context.PopState();
// If the block started with an open curly, this is a close curly.
if (context.tokens().GetKind(state.token) == TokenKind::OpenCurlyBrace) {
context.AddNode(ParseNodeKind::CodeBlock, context.Consume(),
state.subtree_start, state.has_error);
} else {
context.AddNode(ParseNodeKind::CodeBlock, state.token, state.subtree_start,
/*has_error=*/true);
}
}
} // namespace Carbon