handle_base.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/parse/context.h"
  5. namespace Carbon::Parse {
  6. // Handles a `base` declaration after the introducer.
  7. auto HandleBaseAfterIntroducer(Context& context) -> void {
  8. auto state = context.PopState();
  9. if (!context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::Colon,
  10. NodeKind::BaseColon)) {
  11. // TODO: If the next token isn't a colon or `class`, try to recover
  12. // based on whether we're in a class, whether we have an `extend`
  13. // modifier, and the following tokens.
  14. CARBON_DIAGNOSTIC(ExpectedAfterBase, Error,
  15. "`class` or `:` expected after `base`.");
  16. context.emitter().Emit(*context.position(), ExpectedAfterBase);
  17. context.RecoverFromDeclError(state, NodeKind::BaseDecl,
  18. /*skip_past_likely_end=*/true);
  19. return;
  20. }
  21. state.state = State::BaseDecl;
  22. context.PushState(state);
  23. context.PushState(State::Expr);
  24. }
  25. // Handles processing of a complete `base: B` declaration.
  26. auto HandleBaseDecl(Context& context) -> void {
  27. auto state = context.PopState();
  28. context.AddNodeExpectingDeclSemi(state, NodeKind::BaseDecl,
  29. Lex::TokenKind::Base,
  30. /*is_def_allowed=*/false);
  31. }
  32. } // namespace Carbon::Parse