handle_binding_pattern.cpp 5.3 KB

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