handle_binding_pattern.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. // The first item should be an identifier or `self`.
  32. bool has_name = false;
  33. if (auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier)) {
  34. context.AddLeafNode(NodeKind::IdentifierNameNotBeforeParams, *identifier);
  35. has_name = true;
  36. } else if (auto self =
  37. context.ConsumeIf(Lex::TokenKind::SelfValueIdentifier)) {
  38. // Checking will validate the `self` is only declared in the implicit
  39. // parameter list of a function.
  40. context.AddLeafNode(NodeKind::SelfValueName, *self);
  41. has_name = true;
  42. } else if (auto underscore = context.ConsumeIf(Lex::TokenKind::Underscore)) {
  43. context.AddLeafNode(NodeKind::UnderscoreName, *underscore);
  44. has_name = true;
  45. }
  46. if (!has_name) {
  47. // Add a placeholder for the name.
  48. context.AddLeafNode(NodeKind::IdentifierNameNotBeforeParams,
  49. *context.position(), /*has_error=*/true);
  50. on_error(/*expected_name=*/true);
  51. }
  52. if (auto token_kind = context.PositionKind();
  53. token_kind == Lex::TokenKind::Colon ||
  54. token_kind == Lex::TokenKind::ColonExclaim) {
  55. // Add the wrapper node for the `template` keyword if present.
  56. if (template_token) {
  57. if (token_kind != Lex::TokenKind::ColonExclaim && !state.has_error) {
  58. CARBON_DIAGNOSTIC(ExpectedGenericBindingPatternAfterTemplate, Error,
  59. "expected `:!` binding after `template`");
  60. context.emitter().Emit(*template_token,
  61. ExpectedGenericBindingPatternAfterTemplate);
  62. state.has_error = true;
  63. }
  64. context.AddNode(NodeKind::TemplateBindingName, *template_token,
  65. state.has_error);
  66. }
  67. state.kind = token_kind == Lex::TokenKind::Colon
  68. ? StateKind::BindingPatternFinishAsRegular
  69. : StateKind::BindingPatternFinishAsGeneric;
  70. // Use the `:` or `:!` for the root node.
  71. state.token = context.Consume();
  72. context.PushState(state);
  73. context.PushStateForExpr(PrecedenceGroup::ForType());
  74. } else {
  75. on_error(/*expected_name=*/false);
  76. // Add a substitute for a type node.
  77. context.AddInvalidParse(*context.position());
  78. context.PushState(state, StateKind::BindingPatternFinishAsRegular);
  79. }
  80. }
  81. // Handles BindingPatternFinishAs(Generic|Regular).
  82. static auto HandleBindingPatternFinish(Context& context, bool is_compile_time)
  83. -> void {
  84. auto state = context.PopState();
  85. auto node_kind = NodeKind::InvalidParse;
  86. if (state.in_var_pattern) {
  87. node_kind = NodeKind::VarBindingPattern;
  88. if (is_compile_time) {
  89. CARBON_DIAGNOSTIC(CompileTimeBindingInVarDecl, Error,
  90. "`var` pattern cannot declare a compile-time binding");
  91. context.emitter().Emit(*context.position(), CompileTimeBindingInVarDecl);
  92. state.has_error = true;
  93. }
  94. } else {
  95. if (is_compile_time) {
  96. node_kind = NodeKind::CompileTimeBindingPattern;
  97. } else {
  98. node_kind = NodeKind::LetBindingPattern;
  99. }
  100. }
  101. context.AddNode(node_kind, state.token, state.has_error);
  102. // Propagate errors to the parent state so that they can take different
  103. // actions on invalid patterns.
  104. if (state.has_error) {
  105. context.ReturnErrorOnState();
  106. }
  107. }
  108. auto HandleBindingPatternFinishAsGeneric(Context& context) -> void {
  109. HandleBindingPatternFinish(context, /*is_compile_time=*/true);
  110. }
  111. auto HandleBindingPatternFinishAsRegular(Context& context) -> void {
  112. HandleBindingPatternFinish(context, /*is_compile_time=*/false);
  113. }
  114. auto HandleBindingPatternAddr(Context& context) -> void {
  115. auto state = context.PopState();
  116. context.AddNode(NodeKind::Addr, state.token, state.has_error);
  117. // If an error was encountered, propagate it while adding a node.
  118. if (state.has_error) {
  119. context.ReturnErrorOnState();
  120. }
  121. }
  122. } // namespace Carbon::Parse