handle_binding_pattern.cpp 4.8 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. // Parameters may have keywords prefixing the pattern. They become the parent
  11. // for the full BindingPattern.
  12. if (auto token = context.ConsumeIf(Lex::TokenKind::Template)) {
  13. context.PushState({.state = State::BindingPatternTemplate,
  14. .in_var_pattern = state.in_var_pattern,
  15. .token = *token,
  16. .subtree_start = state.subtree_start});
  17. }
  18. if (auto token = context.ConsumeIf(Lex::TokenKind::Addr)) {
  19. context.PushState({.state = State::BindingPatternAddr,
  20. .in_var_pattern = state.in_var_pattern,
  21. .token = *token,
  22. .subtree_start = state.subtree_start});
  23. }
  24. // Handle an invalid pattern introducer for parameters and variables.
  25. auto on_error = [&](bool expected_name) {
  26. if (!state.has_error) {
  27. CARBON_DIAGNOSTIC(ExpectedBindingPattern, Error,
  28. "expected {0:name|`:` or `:!`} in binding pattern",
  29. BoolAsSelect);
  30. context.emitter().Emit(*context.position(), ExpectedBindingPattern,
  31. expected_name);
  32. state.has_error = true;
  33. }
  34. };
  35. // The first item should be an identifier or `self`.
  36. bool has_name = false;
  37. if (auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier)) {
  38. context.AddLeafNode(NodeKind::IdentifierNameNotBeforeParams, *identifier);
  39. has_name = true;
  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. has_name = true;
  46. }
  47. if (!has_name) {
  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 kind = context.PositionKind();
  54. kind == Lex::TokenKind::Colon || kind == Lex::TokenKind::ColonExclaim) {
  55. state.state = kind == Lex::TokenKind::Colon
  56. ? State::BindingPatternFinishAsRegular
  57. : State::BindingPatternFinishAsGeneric;
  58. // Use the `:` or `:!` for the root node.
  59. state.token = context.Consume();
  60. context.PushState(state);
  61. context.PushStateForExpr(PrecedenceGroup::ForType());
  62. } else {
  63. on_error(/*expected_name=*/false);
  64. // Add a substitute for a type node.
  65. context.AddInvalidParse(*context.position());
  66. context.PushState(state, State::BindingPatternFinishAsRegular);
  67. }
  68. }
  69. // Handles BindingPatternFinishAs(Generic|Regular).
  70. static auto HandleBindingPatternFinish(Context& context, bool is_compile_time)
  71. -> void {
  72. auto state = context.PopState();
  73. auto node_kind = NodeKind::InvalidParse;
  74. if (state.in_var_pattern) {
  75. node_kind = NodeKind::VarBindingPattern;
  76. if (is_compile_time) {
  77. CARBON_DIAGNOSTIC(
  78. CompileTimeBindingInVarDecl, Error,
  79. "`var` declaration cannot declare a compile-time binding");
  80. context.emitter().Emit(*context.position(), CompileTimeBindingInVarDecl);
  81. state.has_error = true;
  82. }
  83. } else {
  84. if (is_compile_time) {
  85. node_kind = NodeKind::CompileTimeBindingPattern;
  86. } else {
  87. node_kind = NodeKind::LetBindingPattern;
  88. }
  89. }
  90. context.AddNode(node_kind, state.token, state.has_error);
  91. // Propagate errors to the parent state so that they can take different
  92. // actions on invalid patterns.
  93. if (state.has_error) {
  94. context.ReturnErrorOnState();
  95. }
  96. }
  97. auto HandleBindingPatternFinishAsGeneric(Context& context) -> void {
  98. HandleBindingPatternFinish(context, /*is_compile_time=*/true);
  99. }
  100. auto HandleBindingPatternFinishAsRegular(Context& context) -> void {
  101. HandleBindingPatternFinish(context, /*is_compile_time=*/false);
  102. }
  103. auto HandleBindingPatternAddr(Context& context) -> void {
  104. auto state = context.PopState();
  105. context.AddNode(NodeKind::Addr, state.token, state.has_error);
  106. // If an error was encountered, propagate it while adding a node.
  107. if (state.has_error) {
  108. context.ReturnErrorOnState();
  109. }
  110. }
  111. auto HandleBindingPatternTemplate(Context& context) -> void {
  112. auto state = context.PopState();
  113. context.AddNode(NodeKind::Template, state.token, state.has_error);
  114. // If an error was encountered, propagate it while adding a node.
  115. if (state.has_error) {
  116. context.ReturnErrorOnState();
  117. }
  118. }
  119. } // namespace Carbon::Parse