handle_binding_pattern.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/parse/context.h"
  5. namespace Carbon::Parse {
  6. auto HandleBindingPattern(Context& context) -> void {
  7. auto state = context.PopState();
  8. // Parameters may have keywords prefixing the pattern. They become the parent
  9. // for the full BindingPattern.
  10. if (auto token = context.ConsumeIf(Lex::TokenKind::Template)) {
  11. context.PushState({.state = State::BindingPatternTemplate,
  12. .token = *token,
  13. .subtree_start = state.subtree_start});
  14. }
  15. if (auto token = context.ConsumeIf(Lex::TokenKind::Addr)) {
  16. context.PushState({.state = State::BindingPatternAddr,
  17. .token = *token,
  18. .subtree_start = state.subtree_start});
  19. }
  20. // Handle an invalid pattern introducer for parameters and variables.
  21. auto on_error = [&]() {
  22. CARBON_DIAGNOSTIC(ExpectedBindingPattern, Error,
  23. "Expected binding pattern.");
  24. context.emitter().Emit(*context.position(), ExpectedBindingPattern);
  25. // Add a placeholder for the type.
  26. context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
  27. /*has_error=*/true);
  28. state.has_error = true;
  29. context.PushState(state, State::BindingPatternFinishAsRegular);
  30. };
  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::IdentifierName, *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::IdentifierName, *context.position(),
  46. /*has_error=*/true);
  47. on_error();
  48. return;
  49. }
  50. if (auto kind = context.PositionKind();
  51. kind == Lex::TokenKind::Colon || kind == Lex::TokenKind::ColonExclaim) {
  52. state.state = kind == Lex::TokenKind::Colon
  53. ? State::BindingPatternFinishAsRegular
  54. : State::BindingPatternFinishAsGeneric;
  55. // Use the `:` or `:!` for the root node.
  56. state.token = context.Consume();
  57. context.PushState(state);
  58. context.PushStateForExpr(PrecedenceGroup::ForType());
  59. } else {
  60. on_error();
  61. return;
  62. }
  63. }
  64. // Handles BindingPatternFinishAs(Generic|Regular).
  65. static auto HandleBindingPatternFinish(Context& context, NodeKind node_kind)
  66. -> void {
  67. auto state = context.PopState();
  68. context.AddNode(node_kind, state.token, state.subtree_start, state.has_error);
  69. // Propagate errors to the parent state so that they can take different
  70. // actions on invalid patterns.
  71. if (state.has_error) {
  72. context.ReturnErrorOnState();
  73. }
  74. }
  75. auto HandleBindingPatternFinishAsGeneric(Context& context) -> void {
  76. HandleBindingPatternFinish(context, NodeKind::GenericBindingPattern);
  77. }
  78. auto HandleBindingPatternFinishAsRegular(Context& context) -> void {
  79. HandleBindingPatternFinish(context, NodeKind::BindingPattern);
  80. }
  81. auto HandleBindingPatternAddr(Context& context) -> void {
  82. auto state = context.PopState();
  83. context.AddNode(NodeKind::Addr, state.token, state.subtree_start,
  84. state.has_error);
  85. // If an error was encountered, propagate it while adding a node.
  86. if (state.has_error) {
  87. context.ReturnErrorOnState();
  88. }
  89. }
  90. auto HandleBindingPatternTemplate(Context& context) -> void {
  91. auto state = context.PopState();
  92. context.AddNode(NodeKind::Template, state.token, state.subtree_start,
  93. state.has_error);
  94. // If an error was encountered, propagate it while adding a node.
  95. if (state.has_error) {
  96. context.ReturnErrorOnState();
  97. }
  98. }
  99. } // namespace Carbon::Parse