handle_binding_pattern.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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({.state = State::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 kind = context.PositionKind();
  53. kind == Lex::TokenKind::Colon || kind == Lex::TokenKind::ColonExclaim) {
  54. // Add the wrapper node for the `template` keyword if present.
  55. if (template_token) {
  56. if (kind != Lex::TokenKind::ColonExclaim && !state.has_error) {
  57. CARBON_DIAGNOSTIC(ExpectedGenericBindingPatternAfterTemplate, Error,
  58. "expected `:!` binding after `template`");
  59. context.emitter().Emit(*template_token,
  60. ExpectedGenericBindingPatternAfterTemplate);
  61. state.has_error = true;
  62. }
  63. context.AddNode(NodeKind::TemplateBindingName, *template_token,
  64. state.has_error);
  65. }
  66. state.state = kind == Lex::TokenKind::Colon
  67. ? State::BindingPatternFinishAsRegular
  68. : State::BindingPatternFinishAsGeneric;
  69. // Use the `:` or `:!` for the root node.
  70. state.token = context.Consume();
  71. context.PushState(state);
  72. context.PushStateForExpr(PrecedenceGroup::ForType());
  73. } else {
  74. on_error(/*expected_name=*/false);
  75. // Add a substitute for a type node.
  76. context.AddInvalidParse(*context.position());
  77. context.PushState(state, State::BindingPatternFinishAsRegular);
  78. }
  79. }
  80. // Handles BindingPatternFinishAs(Generic|Regular).
  81. static auto HandleBindingPatternFinish(Context& context, bool is_compile_time)
  82. -> void {
  83. auto state = context.PopState();
  84. auto node_kind = NodeKind::InvalidParse;
  85. if (state.in_var_pattern) {
  86. node_kind = NodeKind::VarBindingPattern;
  87. if (is_compile_time) {
  88. CARBON_DIAGNOSTIC(CompileTimeBindingInVarDecl, Error,
  89. "`var` pattern cannot declare a compile-time binding");
  90. context.emitter().Emit(*context.position(), CompileTimeBindingInVarDecl);
  91. state.has_error = true;
  92. }
  93. } else {
  94. if (is_compile_time) {
  95. node_kind = NodeKind::CompileTimeBindingPattern;
  96. } else {
  97. node_kind = NodeKind::LetBindingPattern;
  98. }
  99. }
  100. context.AddNode(node_kind, state.token, state.has_error);
  101. // Propagate errors to the parent state so that they can take different
  102. // actions on invalid patterns.
  103. if (state.has_error) {
  104. context.ReturnErrorOnState();
  105. }
  106. }
  107. auto HandleBindingPatternFinishAsGeneric(Context& context) -> void {
  108. HandleBindingPatternFinish(context, /*is_compile_time=*/true);
  109. }
  110. auto HandleBindingPatternFinishAsRegular(Context& context) -> void {
  111. HandleBindingPatternFinish(context, /*is_compile_time=*/false);
  112. }
  113. auto HandleBindingPatternAddr(Context& context) -> void {
  114. auto state = context.PopState();
  115. context.AddNode(NodeKind::Addr, state.token, state.has_error);
  116. // If an error was encountered, propagate it while adding a node.
  117. if (state.has_error) {
  118. context.ReturnErrorOnState();
  119. }
  120. }
  121. } // namespace Carbon::Parse