Parse all kinds of declarations at function scope. (#4779)

These don't fully work in check and beyond yet, because they're not
added into lexical lookup, but already mostly do the right thing.

Per #3407, disallow namespace declarations anywhere other than at file
scope for now.

We don't treat statements starting with a packaging introducer keyword
(`package`, `library`, `import`) as declarations because they're
sufficiently unlikely to occur that the error recovery doesn't seem
important, and this avoids needing to disambiguate `package.` at the
start of an expression.
This commit is contained in:
Richard Smith
2025-01-10 07:03:10 +00:00
committed by GitHub
parent 1e5e2bc7e2
commit d42128ef9a
25 changed files with 1223 additions and 116 deletions
+14 -15
View File
@@ -242,21 +242,8 @@ static auto TryHandleAsModifier(Context& context) -> bool {
}
}
auto HandleDeclScopeLoop(Context& context) -> void {
// This maintains the current state unless we're at the end of the scope.
if (context.PositionIs(Lex::TokenKind::CloseCurlyBrace) ||
context.PositionIs(Lex::TokenKind::FileEnd)) {
// This is the end of the scope, so the loop state ends.
context.PopAndDiscardState();
return;
}
// Create a state with the correct starting position, with a dummy kind
// until we see the declaration's introducer.
Context::StateStackEntry state{.state = State::Invalid,
.token = *context.position(),
.subtree_start = context.tree().size()};
auto HandleDecl(Context& context) -> void {
auto state = context.PopState();
// Add a placeholder node, to be replaced by the declaration introducer once
// it is found.
@@ -271,4 +258,16 @@ auto HandleDeclScopeLoop(Context& context) -> void {
}
}
auto HandleDeclScopeLoop(Context& context) -> void {
// This maintains the current state unless we're at the end of the scope.
if (context.PositionIs(Lex::TokenKind::CloseCurlyBrace) ||
context.PositionIs(Lex::TokenKind::FileEnd)) {
// This is the end of the scope, so the loop state ends.
context.PopAndDiscardState();
return;
}
context.PushState(State::Decl);
}
} // namespace Carbon::Parse