handle_binding_pattern.cpp 4.9 KB

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