handle_binding_pattern.cpp 4.0 KB

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