handle_decl_scope_loop.cpp 4.0 KB

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