mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user