handle_binding_pattern.cpp 6.0 KB

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