handle_base.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. #include "toolchain/parse/handle.h"
  6. namespace Carbon::Parse {
  7. // Handles a `base` declaration after the introducer.
  8. auto HandleBaseAfterIntroducer(Context& context) -> void {
  9. auto state = context.PopState();
  10. if (!context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::Colon,
  11. NodeKind::BaseColon)) {
  12. // TODO: If the next token isn't a colon or `class`, try to recover
  13. // based on whether we're in a class, whether we have an `extend`
  14. // modifier, and the following tokens.
  15. CARBON_DIAGNOSTIC(ExpectedAfterBase, Error,
  16. "`class` or `:` expected after `base`");
  17. context.emitter().Emit(*context.position(), ExpectedAfterBase);
  18. context.RecoverFromDeclError(state, NodeKind::BaseDecl,
  19. /*skip_past_likely_end=*/true);
  20. return;
  21. }
  22. state.state = State::BaseDecl;
  23. context.PushState(state);
  24. context.PushState(State::Expr);
  25. }
  26. // Handles processing of a complete `base: B` declaration.
  27. auto HandleBaseDecl(Context& context) -> void {
  28. auto state = context.PopState();
  29. context.AddNodeExpectingDeclSemi(state, NodeKind::BaseDecl,
  30. Lex::TokenKind::Base,
  31. /*is_def_allowed=*/false);
  32. }
  33. } // namespace Carbon::Parse