handle_binding_pattern.cpp 6.2 KB

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