handle_namespace.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. auto HandleNamespace(Context& context) -> void {
  7. auto state = context.PopState();
  8. context.AddLeafNode(NodeKind::NamespaceStart, context.Consume());
  9. state.state = State::NamespaceFinish;
  10. context.PushState(state);
  11. context.PushState(State::DeclarationNameAndParamsAsNone, state.token);
  12. }
  13. auto HandleNamespaceFinish(Context& context) -> void {
  14. auto state = context.PopState();
  15. if (state.has_error) {
  16. context.RecoverFromDeclarationError(state, NodeKind::Namespace,
  17. /*skip_past_likely_end=*/true);
  18. return;
  19. }
  20. if (auto semi = context.ConsumeIf(Lex::TokenKind::Semi)) {
  21. context.AddNode(NodeKind::Namespace, *semi, state.subtree_start,
  22. state.has_error);
  23. } else {
  24. context.EmitExpectedDeclarationSemi(Lex::TokenKind::Namespace);
  25. context.RecoverFromDeclarationError(state, NodeKind::Namespace,
  26. /*skip_past_likely_end=*/true);
  27. }
  28. }
  29. } // namespace Carbon::Parse