mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-30 13:05:01 +01:00
Parsing and basic checking for abstract class and base class. (#3385)
For now, we require the same introducer to be used each time a class is declared, but see #3384. --------- Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
co-authored by
Jon Ross-Perkins
parent
49591d1360
commit
2715e2276e
@@ -22,75 +22,99 @@ static auto HandleUnrecognizedDecl(Context& context) -> void {
|
||||
auto HandleDeclScopeLoop(Context& context) -> void {
|
||||
// This maintains the current state unless we're at the end of the scope.
|
||||
|
||||
switch (auto position_kind = context.PositionKind()) {
|
||||
auto position_kind = context.PositionKind();
|
||||
switch (position_kind) {
|
||||
case Lex::TokenKind::CloseCurlyBrace:
|
||||
case Lex::TokenKind::EndOfFile: {
|
||||
// This is the end of the scope, so the loop state ends.
|
||||
context.PopAndDiscardState();
|
||||
break;
|
||||
return;
|
||||
}
|
||||
// `import` and `package` manage their packaging state.
|
||||
case Lex::TokenKind::Import: {
|
||||
context.PushState(State::Import);
|
||||
break;
|
||||
return;
|
||||
}
|
||||
case Lex::TokenKind::Package: {
|
||||
context.PushState(State::Package);
|
||||
return;
|
||||
}
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
default:
|
||||
// Because a non-packaging keyword was encountered, packaging is complete.
|
||||
// Misplaced packaging keywords may lead to this being re-triggered.
|
||||
if (context.packaging_state() !=
|
||||
Context::PackagingState::AfterNonPackagingDecl) {
|
||||
if (!context.first_non_packaging_token().is_valid()) {
|
||||
context.set_first_non_packaging_token(*context.position());
|
||||
}
|
||||
context.set_packaging_state(
|
||||
Context::PackagingState::AfterNonPackagingDecl);
|
||||
}
|
||||
switch (position_kind) {
|
||||
// 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.
|
||||
case Lex::TokenKind::Class: {
|
||||
context.PushState(State::TypeIntroducerAsClass);
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::Constraint: {
|
||||
context.PushState(State::TypeIntroducerAsNamedConstraint);
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::Fn: {
|
||||
context.PushState(State::FunctionIntroducer);
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::Interface: {
|
||||
context.PushState(State::TypeIntroducerAsInterface);
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::Namespace: {
|
||||
context.PushState(State::Namespace);
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::Semi: {
|
||||
context.AddLeafNode(NodeKind::EmptyDecl, context.Consume());
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::Var: {
|
||||
context.PushState(State::VarAsDecl);
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::Let: {
|
||||
context.PushState(State::Let);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
HandleUnrecognizedDecl(context);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Because a non-packaging keyword was encountered, packaging is complete.
|
||||
// Misplaced packaging keywords may lead to this being re-triggered.
|
||||
if (context.packaging_state() !=
|
||||
Context::PackagingState::AfterNonPackagingDecl) {
|
||||
if (!context.first_non_packaging_token().is_valid()) {
|
||||
context.set_first_non_packaging_token(*context.position());
|
||||
}
|
||||
context.set_packaging_state(Context::PackagingState::AfterNonPackagingDecl);
|
||||
}
|
||||
|
||||
// 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::Abstract:
|
||||
case Lex::TokenKind::Base: {
|
||||
if (context.PositionIs(Lex::TokenKind::Class, Lookahead::NextToken)) {
|
||||
context.PushState(State::TypeAfterIntroducerAsClass);
|
||||
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);
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user