mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
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:
@@ -0,0 +1,64 @@
|
||||
// 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 an unrecognized declaration, adding an error node.
|
||||
static auto ParserHandleUnrecognizedDeclaration(ParserContext& context)
|
||||
-> void {
|
||||
CARBON_DIAGNOSTIC(UnrecognizedDeclaration, Error,
|
||||
"Unrecognized declaration introducer.");
|
||||
context.emitter().Emit(*context.position(), UnrecognizedDeclaration);
|
||||
auto cursor = *context.position();
|
||||
auto semi = context.SkipPastLikelyEnd(cursor);
|
||||
// Locate the EmptyDeclaration at the semi when found, but use the
|
||||
// original cursor location for an error when not.
|
||||
context.AddLeafNode(ParseNodeKind::EmptyDeclaration, semi ? *semi : cursor,
|
||||
/*has_error=*/true);
|
||||
}
|
||||
|
||||
auto ParserHandleDeclarationScopeLoop(ParserContext& context) -> void {
|
||||
// This maintains the current state unless we're at the end of the scope.
|
||||
|
||||
switch (context.PositionKind()) {
|
||||
case TokenKind::CloseCurlyBrace:
|
||||
case TokenKind::EndOfFile: {
|
||||
// This is the end of the scope, so the loop state ends.
|
||||
context.PopAndDiscardState();
|
||||
break;
|
||||
}
|
||||
case TokenKind::Class: {
|
||||
context.PushState(ParserState::TypeIntroducerAsClass);
|
||||
break;
|
||||
}
|
||||
case TokenKind::Constraint: {
|
||||
context.PushState(ParserState::TypeIntroducerAsNamedConstraint);
|
||||
break;
|
||||
}
|
||||
case TokenKind::Fn: {
|
||||
context.PushState(ParserState::FunctionIntroducer);
|
||||
break;
|
||||
}
|
||||
case TokenKind::Interface: {
|
||||
context.PushState(ParserState::TypeIntroducerAsInterface);
|
||||
break;
|
||||
}
|
||||
case TokenKind::Semi: {
|
||||
context.AddLeafNode(ParseNodeKind::EmptyDeclaration, context.Consume());
|
||||
break;
|
||||
}
|
||||
case TokenKind::Var: {
|
||||
context.PushState(ParserState::VarAsSemicolon);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
ParserHandleUnrecognizedDeclaration(context);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Carbon
|
||||
Reference in New Issue
Block a user