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
+22 -15
View File
@@ -11,11 +11,6 @@ auto HandleStatement(Context& context) -> void {
context.PopAndDiscardState();
switch (context.PositionKind()) {
case Lex::TokenKind::Alias: {
context.PushState(State::Alias);
context.AddLeafNode(NodeKind::AliasIntroducer, context.Consume());
break;
}
case Lex::TokenKind::Break: {
context.PushState(State::StatementBreakFinish);
context.AddLeafNode(NodeKind::BreakStatementStart, context.Consume());
@@ -36,24 +31,15 @@ auto HandleStatement(Context& context) -> void {
context.PushState(State::StatementIf);
break;
}
case Lex::TokenKind::Let: {
context.PushState(State::Let);
context.AddLeafNode(NodeKind::LetIntroducer, context.Consume());
break;
}
case Lex::TokenKind::Return: {
context.PushState(State::StatementReturn);
break;
}
case Lex::TokenKind::Returned: {
// TODO: Consider handling this as a modifier.
context.PushState(State::VarAsReturned);
break;
}
case Lex::TokenKind::Var: {
context.PushState(State::VarAsDecl);
context.AddLeafNode(NodeKind::VariableIntroducer, context.Consume());
break;
}
case Lex::TokenKind::While: {
context.PushState(State::StatementWhile);
break;
@@ -62,6 +48,27 @@ auto HandleStatement(Context& context) -> void {
context.PushState(State::MatchIntroducer);
break;
}
#define CARBON_PARSE_NODE_KIND(...)
#define CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Name, ...) \
case Lex::TokenKind::Name:
#include "toolchain/parse/node_kind.def"
case Lex::TokenKind::Adapt:
case Lex::TokenKind::Alias:
case Lex::TokenKind::Choice:
case Lex::TokenKind::Class:
case Lex::TokenKind::Constraint:
case Lex::TokenKind::Fn:
case Lex::TokenKind::Import:
case Lex::TokenKind::Interface:
case Lex::TokenKind::Let:
case Lex::TokenKind::Library:
case Lex::TokenKind::Namespace:
// We intentionally don't handle Package here, because `package.` can be
// used at the start of an expression, and it's not worth disambiguating it.
case Lex::TokenKind::Var: {
context.PushState(State::Decl);
break;
}
default: {
context.PushState(State::ExprStatementFinish);
context.PushStateForExpr(PrecedenceGroup::ForExprStatement());