handle_declaration_scope_loop.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 HandleUnrecognizedDecl(Context& context) -> void {
  8. CARBON_DIAGNOSTIC(UnrecognizedDecl, Error,
  9. "Unrecognized declaration introducer.");
  10. context.emitter().Emit(*context.position(), UnrecognizedDecl);
  11. auto cursor = *context.position();
  12. auto semi = context.SkipPastLikelyEnd(cursor);
  13. // Locate the EmptyDecl at the semi when found, but use the
  14. // original cursor location for an error when not.
  15. context.AddLeafNode(NodeKind::EmptyDecl, semi ? *semi : cursor,
  16. /*has_error=*/true);
  17. }
  18. auto HandleDeclScopeLoop(Context& context) -> void {
  19. // This maintains the current state unless we're at the end of the scope.
  20. switch (auto position_kind = 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. // `import` and `package` manage their packaging state.
  28. case Lex::TokenKind::Import: {
  29. context.PushState(State::Import);
  30. break;
  31. }
  32. case Lex::TokenKind::Package: {
  33. context.PushState(State::Package);
  34. break;
  35. }
  36. default:
  37. // Because a non-packaging keyword was encountered, packaging is complete.
  38. // Misplaced packaging keywords may lead to this being re-triggered.
  39. if (context.packaging_state() !=
  40. Context::PackagingState::AfterNonPackagingDecl) {
  41. if (!context.first_non_packaging_token().is_valid()) {
  42. context.set_first_non_packaging_token(*context.position());
  43. }
  44. context.set_packaging_state(
  45. Context::PackagingState::AfterNonPackagingDecl);
  46. }
  47. switch (position_kind) {
  48. // Remaining keywords are only valid after imports are complete, and
  49. // so all result in a `set_packaging_state` call. Note, this may not
  50. // always be necessary but is probably cheaper than validating.
  51. case Lex::TokenKind::Class: {
  52. context.PushState(State::TypeIntroducerAsClass);
  53. break;
  54. }
  55. case Lex::TokenKind::Constraint: {
  56. context.PushState(State::TypeIntroducerAsNamedConstraint);
  57. break;
  58. }
  59. case Lex::TokenKind::Fn: {
  60. context.PushState(State::FunctionIntroducer);
  61. break;
  62. }
  63. case Lex::TokenKind::Interface: {
  64. context.PushState(State::TypeIntroducerAsInterface);
  65. break;
  66. }
  67. case Lex::TokenKind::Namespace: {
  68. context.PushState(State::Namespace);
  69. break;
  70. }
  71. case Lex::TokenKind::Semi: {
  72. context.AddLeafNode(NodeKind::EmptyDecl, context.Consume());
  73. break;
  74. }
  75. case Lex::TokenKind::Var: {
  76. context.PushState(State::VarAsSemicolon);
  77. break;
  78. }
  79. case Lex::TokenKind::Let: {
  80. context.PushState(State::Let);
  81. break;
  82. }
  83. default: {
  84. HandleUnrecognizedDecl(context);
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. } // namespace Carbon::Parse