handle_declaration_scope_loop.cpp 2.1 KB

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