handle_require.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. auto HandleRequireAfterIntroducer(Context& context) -> void {
  8. auto state = context.PopState();
  9. state.kind = StateKind::RequireDecl;
  10. context.PushState(state);
  11. if (context.PositionIs(Lex::TokenKind::Impls)) {
  12. // impls <expression> ...
  13. context.AddLeafNode(NodeKind::RequireDefaultSelfImpls, context.Consume());
  14. context.PushState(StateKind::Expr);
  15. } else {
  16. // <expression> impls <expression>...
  17. context.PushState(StateKind::RequireBeforeImpls);
  18. context.PushStateForExpr(PrecedenceGroup::ForRequirements());
  19. }
  20. }
  21. auto HandleRequireBeforeImpls(Context& context) -> void {
  22. auto state = context.PopState();
  23. if (auto impls = context.ConsumeIf(Lex::TokenKind::Impls)) {
  24. context.AddNode(NodeKind::RequireTypeImpls, *impls, state.has_error);
  25. context.PushState(StateKind::Expr);
  26. } else {
  27. if (!state.has_error) {
  28. CARBON_DIAGNOSTIC(RequireExpectedImpls, Error,
  29. "expected `impls` in `require` declaration");
  30. context.emitter().Emit(*context.position(), RequireExpectedImpls);
  31. }
  32. context.ReturnErrorOnState();
  33. }
  34. }
  35. auto HandleRequireDecl(Context& context) -> void {
  36. auto state = context.PopState();
  37. context.AddNodeExpectingDeclSemi(state, NodeKind::RequireDecl,
  38. Lex::TokenKind::Require,
  39. /*is_def_allowed=*/false);
  40. }
  41. } // namespace Carbon::Parse