Support declaration modifier keywords (#3412)

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
josh11b
2023-12-05 22:45:57 +00:00
committed by GitHub
co-authored by Jon Ross-Perkins
parent 22dff46ed2
commit fada410559
54 changed files with 1779 additions and 244 deletions
+197 -75
View File
@@ -6,24 +6,61 @@
namespace Carbon::Parse {
// Handles an unrecognized declaration, adding an error node.
static auto HandleUnrecognizedDecl(Context& context) -> void {
static auto OutputInvalidParseSubtree(Context& context, int32_t subtree_start)
-> void {
auto cursor = *context.position();
// Consume to the next `;` or end of line. We ignore the return value since
// we only care how much was consumed, not whether it ended with a `;`.
// TODO: adjust the return of SkipPastLikelyEnd or create a new function
// to avoid going through these hoops.
context.SkipPastLikelyEnd(cursor);
// Set `iter` to the last token consumed, one before the current position.
auto iter = context.position();
--iter;
// Output an invalid parse subtree including everything up to the last token
// consumed.
context.ReplacePlaceholderNode(subtree_start, NodeKind::InvalidParseStart,
cursor, /*has_error=*/true);
context.AddNode(NodeKind::InvalidParseSubtree, *iter, subtree_start,
/*has_error=*/true);
}
// Handles an unrecognized declaration.
static auto HandleUnrecognizedDecl(Context& context, int32_t subtree_start)
-> void {
CARBON_DIAGNOSTIC(UnrecognizedDecl, Error,
"Unrecognized declaration introducer.");
context.emitter().Emit(*context.position(), UnrecognizedDecl);
auto cursor = *context.position();
auto semi = context.SkipPastLikelyEnd(cursor);
// Locate the EmptyDecl at the semi when found, but use the
// original cursor location for an error when not.
context.AddLeafNode(NodeKind::EmptyDecl, semi ? *semi : cursor,
/*has_error=*/true);
OutputInvalidParseSubtree(context, subtree_start);
}
static auto TokenIsModifierOrIntroducer(Lex::TokenKind token_kind) -> bool {
switch (token_kind) {
case Lex::TokenKind::Abstract:
case Lex::TokenKind::Base:
case Lex::TokenKind::Class:
case Lex::TokenKind::Constraint:
case Lex::TokenKind::Default:
case Lex::TokenKind::Final:
case Lex::TokenKind::Fn:
case Lex::TokenKind::Impl:
case Lex::TokenKind::Interface:
case Lex::TokenKind::Let:
case Lex::TokenKind::Private:
case Lex::TokenKind::Protected:
case Lex::TokenKind::Var:
case Lex::TokenKind::Virtual:
return true;
default:
return false;
}
}
auto HandleDeclScopeLoop(Context& context) -> void {
// This maintains the current state unless we're at the end of the scope.
auto position_kind = context.PositionKind();
switch (position_kind) {
switch (context.PositionKind()) {
case Lex::TokenKind::CloseCurlyBrace:
case Lex::TokenKind::FileEnd: {
// This is the end of the scope, so the loop state ends.
@@ -61,76 +98,161 @@ auto HandleDeclScopeLoop(Context& context) -> void {
// Remaining keywords are only valid after imports are complete, and so all
// result in a `set_packaging_state` call. Note, this may not always be
// necessary but is probably cheaper than validating.
switch (position_kind) {
case Lex::TokenKind::Base: {
if (context.PositionIs(Lex::TokenKind::Colon, Lookahead::NextToken)) {
context.PushState(State::BaseDecl);
context.PushState(State::Expr);
context.AddLeafNode(NodeKind::BaseIntroducer, context.Consume());
context.AddLeafNode(NodeKind::BaseColon, context.Consume());
return;
}
// TODO: If the next token isn't a colon or `class`, try to recover based
// on whether we're in a class, whether we have an `extend` modifier, and
// the following tokens.
[[fallthrough]];
}
case Lex::TokenKind::Abstract: {
if (context.PositionIs(Lex::TokenKind::Class, Lookahead::NextToken)) {
context.PushState(State::TypeAfterIntroducerAsClass);
// Create a state with the correct starting position, with a dummy kind until
// we see the declaration's introducer.
context.PushState(State::DeclScopeLoop);
auto state = context.PopState();
// Add a placeholder node, to be replaced by the declaration introducer once
// it is found.
context.AddLeafNode(NodeKind::Placeholder, *context.position());
auto introducer = [&](NodeKind node_kind, State next_state) {
context.ReplacePlaceholderNode(state.subtree_start, node_kind,
context.Consume());
// Reuse state here to retain its `subtree_start`
state.state = next_state;
context.PushState(state);
};
bool saw_modifier = false;
while (true) {
switch (context.PositionKind()) {
// If we see a access modifier keyword token, add it as a leaf node
// and repeat with the next token.
case Lex::TokenKind::Private:
case Lex::TokenKind::Protected: {
auto modifier_token = context.Consume();
auto class_token = context.Consume();
context.AddLeafNode(NodeKind::ClassIntroducer, class_token);
context.AddLeafNode(position_kind == Lex::TokenKind::Abstract
? NodeKind::AbstractModifier
: NodeKind::BaseModifier,
modifier_token);
context.AddLeafNode(NodeKind::AccessModifierKeyword, modifier_token);
saw_modifier = true;
break;
}
case Lex::TokenKind::Base:
// `base` may be followed by:
// - a colon
// => assume it is an introducer, as in `extend base: BaseType;`.
// - a modifier or an introducer
// => assume it is a modifier, as in `base class`; which is handled
// by falling through to the next case.
// Anything else is an error.
if (context.PositionIs(Lex::TokenKind::Colon, Lookahead::NextToken)) {
context.ReplacePlaceholderNode(
state.subtree_start, NodeKind::BaseIntroducer, context.Consume());
// Reuse state here to retain its `subtree_start`
state.state = State::BaseDecl;
context.PushState(state);
context.PushState(State::Expr);
context.AddLeafNode(NodeKind::BaseColon, context.Consume());
return;
} else if (!TokenIsModifierOrIntroducer(
context.PositionKind(Lookahead::NextToken))) {
// TODO: If the next token isn't a colon or `class`, try to recover
// based on whether we're in a class, whether we have an `extend`
// modifier, and the following tokens.
context.AddLeafNode(NodeKind::InvalidParse, context.Consume(),
/*has_error=*/true);
CARBON_DIAGNOSTIC(ExpectedAfterBase, Error,
"`class` or `:` expected after `base`.");
context.emitter().Emit(*context.position(), ExpectedAfterBase);
OutputInvalidParseSubtree(context, state.subtree_start);
return;
}
[[fallthrough]];
// If we see a declaration modifier keyword token, add it as a leaf node
// and repeat with the next token.
case Lex::TokenKind::Abstract:
case Lex::TokenKind::Default:
case Lex::TokenKind::Final:
case Lex::TokenKind::Virtual: {
auto modifier_token = context.Consume();
context.AddLeafNode(NodeKind::DeclModifierKeyword, modifier_token);
saw_modifier = true;
break;
}
case Lex::TokenKind::Impl: {
// `impl` is considered a declaration modifier if it is followed by
// another modifier or an introducer.
if (TokenIsModifierOrIntroducer(
context.PositionKind(Lookahead::NextToken))) {
context.AddLeafNode(NodeKind::DeclModifierKeyword, context.Consume());
saw_modifier = true;
} else {
// TODO: Treat this `impl` token as a declaration introducer
HandleUnrecognizedDecl(context, state.subtree_start);
return;
}
break;
}
// If we see a declaration introducer keyword token, replace the
// placeholder node and switch to a state to parse the rest of the
// declaration. We don't allow namespace or empty declarations here since
// they can't have modifiers and don't use bracketing parse nodes that
// would allow a variable number of modifier nodes.
case Lex::TokenKind::Class: {
introducer(NodeKind::ClassIntroducer,
State::TypeAfterIntroducerAsClass);
return;
}
case Lex::TokenKind::Constraint: {
introducer(NodeKind::NamedConstraintIntroducer,
State::TypeAfterIntroducerAsNamedConstraint);
return;
}
case Lex::TokenKind::Fn: {
introducer(NodeKind::FunctionIntroducer, State::FunctionIntroducer);
return;
}
case Lex::TokenKind::Interface: {
introducer(NodeKind::InterfaceIntroducer,
State::TypeAfterIntroducerAsInterface);
return;
}
case Lex::TokenKind::Var: {
introducer(NodeKind::VariableIntroducer, State::VarAsDecl);
return;
}
case Lex::TokenKind::Let: {
introducer(NodeKind::LetIntroducer, State::Let);
return;
}
// We don't allow namespace or empty declarations after a modifier since
// they can't have modifiers and don't use bracketing parse nodes that
// would allow a variable number of modifier nodes.
case Lex::TokenKind::Namespace: {
if (saw_modifier) {
CARBON_DIAGNOSTIC(NamespaceAfterModifiers, Error,
"`namespace` unexpected after modifiers.");
context.emitter().Emit(*context.position(), NamespaceAfterModifiers);
OutputInvalidParseSubtree(context, state.subtree_start);
} else {
introducer(NodeKind::NamespaceStart, State::Namespace);
}
return;
}
case Lex::TokenKind::Semi: {
if (saw_modifier) {
HandleUnrecognizedDecl(context, state.subtree_start);
} else {
context.ReplacePlaceholderNode(
state.subtree_start, NodeKind::EmptyDecl, context.Consume());
}
return;
}
default: {
// For anything else, report an error and output an invalid parse node
// or subtree.
HandleUnrecognizedDecl(context, state.subtree_start);
return;
}
break;
}
case Lex::TokenKind::Class: {
context.PushState(State::TypeAfterIntroducerAsClass);
context.AddLeafNode(NodeKind::ClassIntroducer, context.Consume());
return;
}
case Lex::TokenKind::Constraint: {
context.PushState(State::TypeAfterIntroducerAsNamedConstraint);
context.AddLeafNode(NodeKind::NamedConstraintIntroducer,
context.Consume());
return;
}
case Lex::TokenKind::Fn: {
context.PushState(State::FunctionIntroducer);
return;
}
case Lex::TokenKind::Interface: {
context.PushState(State::TypeAfterIntroducerAsInterface);
context.AddLeafNode(NodeKind::InterfaceIntroducer, context.Consume());
return;
}
case Lex::TokenKind::Namespace: {
context.PushState(State::Namespace);
return;
}
case Lex::TokenKind::Semi: {
context.AddLeafNode(NodeKind::EmptyDecl, context.Consume());
return;
}
case Lex::TokenKind::Var: {
context.PushState(State::VarAsDecl);
return;
}
case Lex::TokenKind::Let: {
context.PushState(State::Let);
return;
}
default: {
break;
}
}
HandleUnrecognizedDecl(context);
}
} // namespace Carbon::Parse