parser_handle_declaration_scope_loop.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. // Handles an unrecognized declaration, adding an error node.
  7. static auto ParserHandleUnrecognizedDeclaration(ParserContext& context)
  8. -> void {
  9. CARBON_DIAGNOSTIC(UnrecognizedDeclaration, Error,
  10. "Unrecognized declaration introducer.");
  11. context.emitter().Emit(*context.position(), UnrecognizedDeclaration);
  12. auto cursor = *context.position();
  13. auto semi = context.SkipPastLikelyEnd(cursor);
  14. // Locate the EmptyDeclaration at the semi when found, but use the
  15. // original cursor location for an error when not.
  16. context.AddLeafNode(ParseNodeKind::EmptyDeclaration, semi ? *semi : cursor,
  17. /*has_error=*/true);
  18. }
  19. auto ParserHandleDeclarationScopeLoop(ParserContext& context) -> void {
  20. // This maintains the current state unless we're at the end of the scope.
  21. switch (context.PositionKind()) {
  22. case TokenKind::CloseCurlyBrace:
  23. case TokenKind::EndOfFile: {
  24. // This is the end of the scope, so the loop state ends.
  25. context.PopAndDiscardState();
  26. break;
  27. }
  28. case TokenKind::Class: {
  29. context.PushState(ParserState::TypeIntroducerAsClass);
  30. break;
  31. }
  32. case TokenKind::Constraint: {
  33. context.PushState(ParserState::TypeIntroducerAsNamedConstraint);
  34. break;
  35. }
  36. case TokenKind::Fn: {
  37. context.PushState(ParserState::FunctionIntroducer);
  38. break;
  39. }
  40. case TokenKind::Interface: {
  41. context.PushState(ParserState::TypeIntroducerAsInterface);
  42. break;
  43. }
  44. case TokenKind::Namespace: {
  45. context.PushState(ParserState::Namespace);
  46. break;
  47. }
  48. case TokenKind::Semi: {
  49. context.AddLeafNode(ParseNodeKind::EmptyDeclaration, context.Consume());
  50. break;
  51. }
  52. case TokenKind::Var: {
  53. context.PushState(ParserState::VarAsSemicolon);
  54. break;
  55. }
  56. default: {
  57. ParserHandleUnrecognizedDeclaration(context);
  58. break;
  59. }
  60. }
  61. }
  62. } // namespace Carbon