handle_binding_pattern.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/format_providers.h"
  5. #include "toolchain/parse/context.h"
  6. #include "toolchain/parse/handle.h"
  7. namespace Carbon::Parse {
  8. auto HandleBindingPattern(Context& context) -> void {
  9. auto state = context.PopState();
  10. // Handle an invalid pattern introducer for parameters and variables.
  11. auto on_error = [&](bool expected_name) {
  12. if (!state.has_error) {
  13. CARBON_DIAGNOSTIC(
  14. ExpectedBindingPattern, Error,
  15. "expected {0:name|`:`, `:!`, or `:?`} in binding pattern",
  16. Diagnostics::BoolAsSelect);
  17. context.emitter().Emit(*context.position(), ExpectedBindingPattern,
  18. expected_name);
  19. state.has_error = true;
  20. }
  21. };
  22. // A `template` keyword may precede the name.
  23. auto template_token = context.ConsumeIf(Lex::TokenKind::Template);
  24. auto ref_token = context.ConsumeIf(Lex::TokenKind::Ref);
  25. if (ref_token && state.in_var_pattern) {
  26. CARBON_DIAGNOSTIC(RefInsideVar, Error, "found `ref` inside `var` pattern");
  27. context.emitter().Emit(*ref_token, RefInsideVar);
  28. state.has_error = true;
  29. }
  30. // The first item should be an identifier, the placeholder `_`, or `self`.
  31. if (auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier)) {
  32. context.AddLeafNode(NodeKind::IdentifierNameNotBeforeSignature,
  33. *identifier);
  34. } else if (auto self =
  35. context.ConsumeIf(Lex::TokenKind::SelfValueIdentifier)) {
  36. // Checking will validate the `self` is only declared in the implicit
  37. // parameter list of a function.
  38. context.AddLeafNode(NodeKind::SelfValueName, *self);
  39. } else if (auto underscore = context.ConsumeIf(Lex::TokenKind::Underscore)) {
  40. context.AddLeafNode(NodeKind::UnderscoreName, *underscore);
  41. } else {
  42. // Add a placeholder for the name.
  43. context.AddLeafNode(NodeKind::IdentifierNameNotBeforeSignature,
  44. *context.position(), /*has_error=*/true);
  45. on_error(/*expected_name=*/true);
  46. }
  47. if (auto token_kind = context.PositionKind();
  48. token_kind == Lex::TokenKind::Colon ||
  49. token_kind == Lex::TokenKind::ColonExclaim ||
  50. token_kind == Lex::TokenKind::ColonQuestion) {
  51. // Add the wrapper node for the `template` keyword if present.
  52. if (template_token) {
  53. if (token_kind != Lex::TokenKind::ColonExclaim && !state.has_error) {
  54. CARBON_DIAGNOSTIC(ExpectedGenericBindingPatternAfterTemplate, Error,
  55. "expected `:!` binding after `template`");
  56. context.emitter().Emit(*template_token,
  57. ExpectedGenericBindingPatternAfterTemplate);
  58. state.has_error = true;
  59. }
  60. context.AddNode(NodeKind::TemplateBindingName, *template_token,
  61. state.has_error);
  62. }
  63. if (ref_token) {
  64. if (token_kind != Lex::TokenKind::Colon && !state.has_error) {
  65. CARBON_DIAGNOSTIC(ExpectedRuntimeBindingPatternAfterRef, Error,
  66. "expected `:` binding after `ref`");
  67. context.emitter().Emit(*ref_token,
  68. ExpectedRuntimeBindingPatternAfterRef);
  69. state.has_error = true;
  70. }
  71. context.AddNode(NodeKind::RefBindingName, *ref_token, state.has_error);
  72. }
  73. switch (token_kind) {
  74. case Lex::TokenKind::Colon:
  75. state.kind = StateKind::BindingPatternFinishAsRegular;
  76. break;
  77. case Lex::TokenKind::ColonExclaim:
  78. state.kind = StateKind::BindingPatternFinishAsGeneric;
  79. break;
  80. case Lex::TokenKind::ColonQuestion:
  81. state.kind = StateKind::BindingPatternFinishAsForm;
  82. break;
  83. default:
  84. CARBON_FATAL("Unexpected token kind");
  85. }
  86. // Use the `:`, `:!`, or `:?` for the root node.
  87. state.token = context.Consume();
  88. if (token_kind == Lex::TokenKind::ColonExclaim) {
  89. // Add a virtual node before the compile time binding's type
  90. // expression.
  91. context.AddNode(NodeKind::CompileTimeBindingPatternStart, state.token,
  92. state.has_error);
  93. }
  94. context.PushState(state);
  95. context.PushStateForExpr(PrecedenceGroup::ForType());
  96. } else {
  97. on_error(/*expected_name=*/false);
  98. // Add a substitute for a type node.
  99. context.AddInvalidParse(*context.position());
  100. context.PushState(state, StateKind::BindingPatternFinishAsRegular);
  101. }
  102. }
  103. // Handles BindingPatternFinishAs(Generic|Regular).
  104. static auto HandleBindingPatternFinish(Context& context, StateKind finish_kind)
  105. -> void {
  106. auto state = context.PopState();
  107. auto node_kind = NodeKind::InvalidParse;
  108. if (state.in_var_pattern) {
  109. node_kind = NodeKind::VarBindingPattern;
  110. if (finish_kind == StateKind::BindingPatternFinishAsGeneric) {
  111. CARBON_DIAGNOSTIC(CompileTimeBindingInVarDecl, Error,
  112. "`var` pattern cannot declare a compile-time binding");
  113. context.emitter().Emit(*context.position(), CompileTimeBindingInVarDecl);
  114. state.has_error = true;
  115. }
  116. } else {
  117. switch (finish_kind) {
  118. case StateKind::BindingPatternFinishAsGeneric:
  119. node_kind = NodeKind::CompileTimeBindingPattern;
  120. break;
  121. case StateKind::BindingPatternFinishAsRegular:
  122. node_kind = NodeKind::LetBindingPattern;
  123. break;
  124. case StateKind::BindingPatternFinishAsForm:
  125. node_kind = NodeKind::FormBindingPattern;
  126. break;
  127. default:
  128. CARBON_FATAL("Unexpected StateKind {0}", finish_kind);
  129. }
  130. }
  131. context.AddNode(node_kind, state.token, state.has_error);
  132. // Propagate errors to the parent state so that they can take different
  133. // actions on invalid patterns.
  134. if (state.has_error) {
  135. context.ReturnErrorOnState();
  136. }
  137. }
  138. auto HandleBindingPatternFinishAsGeneric(Context& context) -> void {
  139. HandleBindingPatternFinish(context, StateKind::BindingPatternFinishAsGeneric);
  140. }
  141. auto HandleBindingPatternFinishAsRegular(Context& context) -> void {
  142. HandleBindingPatternFinish(context, StateKind::BindingPatternFinishAsRegular);
  143. }
  144. auto HandleBindingPatternFinishAsForm(Context& context) -> void {
  145. HandleBindingPatternFinish(context, StateKind::BindingPatternFinishAsForm);
  146. }
  147. } // namespace Carbon::Parse