handle_decl_scope_loop.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. auto position_kind = context.PositionKind();
  21. switch (position_kind) {
  22. case Lex::TokenKind::CloseCurlyBrace:
  23. case Lex::TokenKind::FileEnd: {
  24. // This is the end of the scope, so the loop state ends.
  25. context.PopAndDiscardState();
  26. return;
  27. }
  28. // `import`, `library`, and `package` manage their packaging state.
  29. case Lex::TokenKind::Import: {
  30. context.PushState(State::Import);
  31. return;
  32. }
  33. case Lex::TokenKind::Library: {
  34. context.PushState(State::Library);
  35. return;
  36. }
  37. case Lex::TokenKind::Package: {
  38. context.PushState(State::Package);
  39. return;
  40. }
  41. default: {
  42. break;
  43. }
  44. }
  45. // Because a non-packaging keyword was encountered, packaging is complete.
  46. // Misplaced packaging keywords may lead to this being re-triggered.
  47. if (context.packaging_state() !=
  48. Context::PackagingState::AfterNonPackagingDecl) {
  49. if (!context.first_non_packaging_token().is_valid()) {
  50. context.set_first_non_packaging_token(*context.position());
  51. }
  52. context.set_packaging_state(Context::PackagingState::AfterNonPackagingDecl);
  53. }
  54. // Remaining keywords are only valid after imports are complete, and so all
  55. // result in a `set_packaging_state` call. Note, this may not always be
  56. // necessary but is probably cheaper than validating.
  57. switch (position_kind) {
  58. case Lex::TokenKind::Abstract:
  59. case Lex::TokenKind::Base: {
  60. if (context.PositionIs(Lex::TokenKind::Class, Lookahead::NextToken)) {
  61. context.PushState(State::TypeAfterIntroducerAsClass);
  62. auto modifier_token = context.Consume();
  63. auto class_token = context.Consume();
  64. context.AddLeafNode(NodeKind::ClassIntroducer, class_token);
  65. context.AddLeafNode(position_kind == Lex::TokenKind::Abstract
  66. ? NodeKind::AbstractModifier
  67. : NodeKind::BaseModifier,
  68. modifier_token);
  69. return;
  70. }
  71. break;
  72. }
  73. case Lex::TokenKind::Class: {
  74. context.PushState(State::TypeAfterIntroducerAsClass);
  75. context.AddLeafNode(NodeKind::ClassIntroducer, context.Consume());
  76. return;
  77. }
  78. case Lex::TokenKind::Constraint: {
  79. context.PushState(State::TypeAfterIntroducerAsNamedConstraint);
  80. context.AddLeafNode(NodeKind::NamedConstraintIntroducer,
  81. context.Consume());
  82. return;
  83. }
  84. case Lex::TokenKind::Fn: {
  85. context.PushState(State::FunctionIntroducer);
  86. return;
  87. }
  88. case Lex::TokenKind::Interface: {
  89. context.PushState(State::TypeAfterIntroducerAsInterface);
  90. context.AddLeafNode(NodeKind::InterfaceIntroducer, context.Consume());
  91. return;
  92. }
  93. case Lex::TokenKind::Namespace: {
  94. context.PushState(State::Namespace);
  95. return;
  96. }
  97. case Lex::TokenKind::Semi: {
  98. context.AddLeafNode(NodeKind::EmptyDecl, context.Consume());
  99. return;
  100. }
  101. case Lex::TokenKind::Var: {
  102. context.PushState(State::VarAsDecl);
  103. return;
  104. }
  105. case Lex::TokenKind::Let: {
  106. context.PushState(State::Let);
  107. return;
  108. }
  109. default: {
  110. break;
  111. }
  112. }
  113. HandleUnrecognizedDecl(context);
  114. }
  115. } // namespace Carbon::Parse