handle_let.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/diagnostics/diagnostic.h"
  5. #include "toolchain/lex/token_kind.h"
  6. #include "toolchain/parse/context.h"
  7. #include "toolchain/parse/handle.h"
  8. #include "toolchain/parse/node_kind.h"
  9. namespace Carbon::Parse {
  10. auto HandleLet(Context& context) -> void {
  11. auto state = context.PopState();
  12. // These will start at the `let`.
  13. context.PushState(state, StateKind::LetFinishAsRegular);
  14. context.PushState(state, StateKind::LetAfterPatternAsRegular);
  15. context.PushStateForPattern(StateKind::Pattern, /*in_var_pattern=*/false,
  16. /*in_unused_pattern=*/false,
  17. PrecedenceGroup::ForTopLevelPattern());
  18. }
  19. auto HandleAssociatedConstant(Context& context) -> void {
  20. auto state = context.PopState();
  21. // Parse the associated constant pattern: identifier :! type
  22. auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier);
  23. if (!identifier) {
  24. CARBON_DIAGNOSTIC(ExpectedAssociatedConstantIdentifier, Error,
  25. "expected identifier in associated constant declaration");
  26. context.emitter().Emit(*context.position(),
  27. ExpectedAssociatedConstantIdentifier);
  28. state.has_error = true;
  29. }
  30. auto colon_exclaim = context.ConsumeIf(Lex::TokenKind::ColonExclaim);
  31. if (identifier && !colon_exclaim) {
  32. CARBON_DIAGNOSTIC(ExpectedAssociatedConstantColonExclaim, Error,
  33. "found runtime binding pattern in associated constant "
  34. "declaration; expected a `:!` binding");
  35. context.emitter().Emit(*context.position(),
  36. ExpectedAssociatedConstantColonExclaim);
  37. state.has_error = true;
  38. }
  39. if (!identifier || !colon_exclaim) {
  40. auto end_token = context.SkipPastLikelyEnd(*(context.position() - 1));
  41. context.AddNode(NodeKind::AssociatedConstantDecl, end_token,
  42. /*has_error=*/true);
  43. state.has_error = true;
  44. return;
  45. }
  46. context.AddLeafNode(NodeKind::IdentifierNameNotBeforeSignature, *identifier);
  47. state.token = *colon_exclaim;
  48. context.PushState(state, StateKind::LetFinishAsAssociatedConstant);
  49. context.PushState(state, StateKind::LetAfterPatternAsAssociatedConstant);
  50. context.PushState(StateKind::Expr);
  51. }
  52. static auto HandleLetAfterPattern(Context& context, NodeKind init_kind)
  53. -> void {
  54. auto state = context.PopState();
  55. if (state.has_error) {
  56. if (auto after_pattern =
  57. context.FindNextOf({Lex::TokenKind::Equal, Lex::TokenKind::Semi})) {
  58. context.SkipTo(*after_pattern);
  59. }
  60. }
  61. if (auto equals = context.ConsumeIf(Lex::TokenKind::Equal)) {
  62. context.AddLeafNode(init_kind, *equals);
  63. context.PushState(StateKind::Expr);
  64. }
  65. }
  66. auto HandleLetAfterPatternAsRegular(Context& context) -> void {
  67. HandleLetAfterPattern(context, NodeKind::LetInitializer);
  68. }
  69. auto HandleLetAfterPatternAsAssociatedConstant(Context& context) -> void {
  70. auto state = context.PopState();
  71. context.AddNode(NodeKind::AssociatedConstantNameAndType, state.token,
  72. state.has_error);
  73. context.PushState(state);
  74. HandleLetAfterPattern(context, NodeKind::AssociatedConstantInitializer);
  75. }
  76. static auto HandleLetFinish(Context& context, NodeKind node_kind) -> void {
  77. auto state = context.PopState();
  78. auto end_token = state.token;
  79. if (context.PositionIs(Lex::TokenKind::Semi)) {
  80. end_token = context.Consume();
  81. } else {
  82. context.DiagnoseExpectedDeclSemi(Lex::TokenKind::Let);
  83. state.has_error = true;
  84. end_token = context.SkipPastLikelyEnd(state.token);
  85. }
  86. context.AddNode(node_kind, end_token, state.has_error);
  87. }
  88. auto HandleLetFinishAsRegular(Context& context) -> void {
  89. HandleLetFinish(context, NodeKind::LetDecl);
  90. }
  91. auto HandleLetFinishAsAssociatedConstant(Context& context) -> void {
  92. HandleLetFinish(context, NodeKind::AssociatedConstantDecl);
  93. }
  94. } // namespace Carbon::Parse