parser_handle_namespace.cpp 1.3 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/parser/parser_context.h"
  5. namespace Carbon {
  6. auto ParserHandleNamespace(ParserContext& context) -> void {
  7. auto state = context.PopState();
  8. context.AddLeafNode(ParseNodeKind::NamespaceStart, context.Consume());
  9. state.state = ParserState::NamespaceFinish;
  10. context.PushState(state);
  11. context.PushState(ParserState::DeclarationNameAndParamsAsNone, state.token);
  12. }
  13. auto ParserHandleNamespaceFinish(ParserContext& context) -> void {
  14. auto state = context.PopState();
  15. if (state.has_error) {
  16. context.RecoverFromDeclarationError(state, ParseNodeKind::Namespace,
  17. /*skip_past_likely_end=*/true);
  18. return;
  19. }
  20. if (auto semi = context.ConsumeIf(TokenKind::Semi)) {
  21. context.AddNode(ParseNodeKind::Namespace, *semi, state.subtree_start,
  22. state.has_error);
  23. } else {
  24. context.EmitExpectedDeclarationSemi(TokenKind::Namespace);
  25. context.RecoverFromDeclarationError(state, ParseNodeKind::Namespace,
  26. /*skip_past_likely_end=*/true);
  27. }
  28. }
  29. } // namespace Carbon