handle_let.cpp 3.8 KB

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