handle_binding_pattern.cpp 5.4 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. // Handle an invalid pattern introducer for parameters and variables.
  11. auto on_error = [&](bool expected_name) {
  12. if (!state.has_error) {
  13. CARBON_DIAGNOSTIC(ExpectedBindingPattern, Error,
  14. "expected {0:name|`:` or `:!`} in binding pattern",
  15. Diagnostics::BoolAsSelect);
  16. context.emitter().Emit(*context.position(), ExpectedBindingPattern,
  17. expected_name);
  18. state.has_error = true;
  19. }
  20. };
  21. // A `template` keyword may precede the name.
  22. auto template_token = context.ConsumeIf(Lex::TokenKind::Template);
  23. auto ref_token = context.ConsumeIf(Lex::TokenKind::Ref);
  24. if (ref_token && state.in_var_pattern) {
  25. CARBON_DIAGNOSTIC(RefInsideVar, Error, "found `ref` inside `var` pattern");
  26. context.emitter().Emit(*ref_token, RefInsideVar);
  27. state.has_error = true;
  28. }
  29. // The first item should be an identifier, the placeholder `_`, or `self`.
  30. if (auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier)) {
  31. context.AddLeafNode(NodeKind::IdentifierNameNotBeforeParams, *identifier);
  32. } else if (auto self =
  33. context.ConsumeIf(Lex::TokenKind::SelfValueIdentifier)) {
  34. // Checking will validate the `self` is only declared in the implicit
  35. // parameter list of a function.
  36. context.AddLeafNode(NodeKind::SelfValueName, *self);
  37. } else if (auto underscore = context.ConsumeIf(Lex::TokenKind::Underscore)) {
  38. context.AddLeafNode(NodeKind::UnderscoreName, *underscore);
  39. } else {
  40. // Add a placeholder for the name.
  41. context.AddLeafNode(NodeKind::IdentifierNameNotBeforeParams,
  42. *context.position(), /*has_error=*/true);
  43. on_error(/*expected_name=*/true);
  44. }
  45. if (auto token_kind = context.PositionKind();
  46. token_kind == Lex::TokenKind::Colon ||
  47. token_kind == Lex::TokenKind::ColonExclaim) {
  48. // Add the wrapper node for the `template` keyword if present.
  49. if (template_token) {
  50. if (token_kind != Lex::TokenKind::ColonExclaim && !state.has_error) {
  51. CARBON_DIAGNOSTIC(ExpectedGenericBindingPatternAfterTemplate, Error,
  52. "expected `:!` binding after `template`");
  53. context.emitter().Emit(*template_token,
  54. ExpectedGenericBindingPatternAfterTemplate);
  55. state.has_error = true;
  56. }
  57. context.AddNode(NodeKind::TemplateBindingName, *template_token,
  58. state.has_error);
  59. }
  60. if (ref_token) {
  61. if (token_kind != Lex::TokenKind::Colon && !state.has_error) {
  62. CARBON_DIAGNOSTIC(ExpectedRuntimeBindingPatternAfterRef, Error,
  63. "expected `:` binding after `ref`");
  64. context.emitter().Emit(*ref_token,
  65. ExpectedRuntimeBindingPatternAfterRef);
  66. state.has_error = true;
  67. }
  68. context.AddNode(NodeKind::RefBindingName, *ref_token, state.has_error);
  69. }
  70. state.kind = token_kind == Lex::TokenKind::Colon
  71. ? StateKind::BindingPatternFinishAsRegular
  72. : StateKind::BindingPatternFinishAsGeneric;
  73. // Use the `:` or `:!` for the root node.
  74. state.token = context.Consume();
  75. if (token_kind == Lex::TokenKind::ColonExclaim) {
  76. // Add a virtual node before the compile time binding's type
  77. // expression.
  78. context.AddNode(NodeKind::CompileTimeBindingPatternStart, state.token,
  79. state.has_error);
  80. }
  81. context.PushState(state);
  82. context.PushStateForExpr(PrecedenceGroup::ForType());
  83. } else {
  84. on_error(/*expected_name=*/false);
  85. // Add a substitute for a type node.
  86. context.AddInvalidParse(*context.position());
  87. context.PushState(state, StateKind::BindingPatternFinishAsRegular);
  88. }
  89. }
  90. // Handles BindingPatternFinishAs(Generic|Regular).
  91. static auto HandleBindingPatternFinish(Context& context, bool is_compile_time)
  92. -> void {
  93. auto state = context.PopState();
  94. auto node_kind = NodeKind::InvalidParse;
  95. if (state.in_var_pattern) {
  96. node_kind = NodeKind::VarBindingPattern;
  97. if (is_compile_time) {
  98. CARBON_DIAGNOSTIC(CompileTimeBindingInVarDecl, Error,
  99. "`var` pattern cannot declare a compile-time binding");
  100. context.emitter().Emit(*context.position(), CompileTimeBindingInVarDecl);
  101. state.has_error = true;
  102. }
  103. } else {
  104. if (is_compile_time) {
  105. node_kind = NodeKind::CompileTimeBindingPattern;
  106. } else {
  107. node_kind = NodeKind::LetBindingPattern;
  108. }
  109. }
  110. context.AddNode(node_kind, state.token, state.has_error);
  111. // Propagate errors to the parent state so that they can take different
  112. // actions on invalid patterns.
  113. if (state.has_error) {
  114. context.ReturnErrorOnState();
  115. }
  116. }
  117. auto HandleBindingPatternFinishAsGeneric(Context& context) -> void {
  118. HandleBindingPatternFinish(context, /*is_compile_time=*/true);
  119. }
  120. auto HandleBindingPatternFinishAsRegular(Context& context) -> void {
  121. HandleBindingPatternFinish(context, /*is_compile_time=*/false);
  122. }
  123. } // namespace Carbon::Parse