handle_binding_pattern.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. .token = *token,
  15. .subtree_start = state.subtree_start});
  16. }
  17. if (auto token = context.ConsumeIf(Lex::TokenKind::Addr)) {
  18. context.PushState({.state = State::BindingPatternAddr,
  19. .token = *token,
  20. .subtree_start = state.subtree_start});
  21. }
  22. // Handle an invalid pattern introducer for parameters and variables.
  23. auto on_error = [&](bool expected_name) {
  24. if (!state.has_error) {
  25. CARBON_DIAGNOSTIC(ExpectedBindingPattern, Error,
  26. "expected {0:name|`:` or `:!`} in binding pattern",
  27. BoolAsSelect);
  28. context.emitter().Emit(*context.position(), ExpectedBindingPattern,
  29. expected_name);
  30. state.has_error = true;
  31. }
  32. };
  33. // The first item should be an identifier or `self`.
  34. bool has_name = false;
  35. if (auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier)) {
  36. context.AddLeafNode(NodeKind::IdentifierName, *identifier);
  37. has_name = true;
  38. } else if (auto self =
  39. context.ConsumeIf(Lex::TokenKind::SelfValueIdentifier)) {
  40. // Checking will validate the `self` is only declared in the implicit
  41. // parameter list of a function.
  42. context.AddLeafNode(NodeKind::SelfValueName, *self);
  43. has_name = true;
  44. }
  45. if (!has_name) {
  46. // Add a placeholder for the name.
  47. context.AddLeafNode(NodeKind::IdentifierName, *context.position(),
  48. /*has_error=*/true);
  49. on_error(/*expected_name=*/true);
  50. }
  51. if (auto kind = context.PositionKind();
  52. kind == Lex::TokenKind::Colon || kind == Lex::TokenKind::ColonExclaim) {
  53. state.state = kind == Lex::TokenKind::Colon
  54. ? State::BindingPatternFinishAsRegular
  55. : State::BindingPatternFinishAsGeneric;
  56. // Use the `:` or `:!` for the root node.
  57. state.token = context.Consume();
  58. context.PushState(state);
  59. context.PushStateForExpr(PrecedenceGroup::ForType());
  60. } else {
  61. on_error(/*expected_name=*/false);
  62. // Add a substitute for a type node.
  63. context.AddInvalidParse(*context.position());
  64. context.PushState(state, State::BindingPatternFinishAsRegular);
  65. }
  66. }
  67. // Handles BindingPatternFinishAs(Generic|Regular).
  68. static auto HandleBindingPatternFinish(Context& context, NodeKind node_kind)
  69. -> void {
  70. auto state = context.PopState();
  71. context.AddNode(node_kind, state.token, state.has_error);
  72. // Propagate errors to the parent state so that they can take different
  73. // actions on invalid patterns.
  74. if (state.has_error) {
  75. context.ReturnErrorOnState();
  76. }
  77. }
  78. auto HandleBindingPatternFinishAsGeneric(Context& context) -> void {
  79. HandleBindingPatternFinish(context, NodeKind::CompileTimeBindingPattern);
  80. }
  81. auto HandleBindingPatternFinishAsRegular(Context& context) -> void {
  82. HandleBindingPatternFinish(context, NodeKind::BindingPattern);
  83. }
  84. auto HandleBindingPatternAddr(Context& context) -> void {
  85. auto state = context.PopState();
  86. context.AddNode(NodeKind::Addr, state.token, state.has_error);
  87. // If an error was encountered, propagate it while adding a node.
  88. if (state.has_error) {
  89. context.ReturnErrorOnState();
  90. }
  91. }
  92. auto HandleBindingPatternTemplate(Context& context) -> void {
  93. auto state = context.PopState();
  94. context.AddNode(NodeKind::Template, state.token, state.has_error);
  95. // If an error was encountered, propagate it while adding a node.
  96. if (state.has_error) {
  97. context.ReturnErrorOnState();
  98. }
  99. }
  100. } // namespace Carbon::Parse