handle_pattern.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. // Handles PatternAs(ImplicitParam|FunctionParam|Variable|Let).
  7. static auto HandlePattern(Context& context, Context::PatternKind pattern_kind)
  8. -> void {
  9. auto state = context.PopState();
  10. // Parameters may have keywords prefixing the pattern. They become the parent
  11. // for the full PatternBinding.
  12. if (pattern_kind != Context::PatternKind::Variable) {
  13. context.ConsumeIfPatternKeyword(
  14. Lex::TokenKind::Template, State::PatternTemplate, state.subtree_start);
  15. context.ConsumeIfPatternKeyword(Lex::TokenKind::Addr, State::PatternAddress,
  16. state.subtree_start);
  17. }
  18. // Handle an invalid pattern introducer for parameters and variables.
  19. auto on_error = [&]() {
  20. switch (pattern_kind) {
  21. case Context::PatternKind::ImplicitParam:
  22. case Context::PatternKind::Param: {
  23. CARBON_DIAGNOSTIC(ExpectedParamName, Error,
  24. "Expected parameter declaration.");
  25. context.emitter().Emit(*context.position(), ExpectedParamName);
  26. break;
  27. }
  28. case Context::PatternKind::Variable: {
  29. CARBON_DIAGNOSTIC(ExpectedVariableName, Error,
  30. "Expected pattern in `var` declaration.");
  31. context.emitter().Emit(*context.position(), ExpectedVariableName);
  32. break;
  33. }
  34. case Context::PatternKind::Let: {
  35. CARBON_DIAGNOSTIC(ExpectedLetBindingName, Error,
  36. "Expected pattern in `let` declaration.");
  37. context.emitter().Emit(*context.position(), ExpectedLetBindingName);
  38. break;
  39. }
  40. }
  41. // Add a placeholder for the type.
  42. context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
  43. /*has_error=*/true);
  44. state.state = State::PatternFinishAsRegular;
  45. state.has_error = true;
  46. context.PushState(state);
  47. };
  48. // The first item should be an identifier or `self`.
  49. bool has_name = false;
  50. if (auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier)) {
  51. context.AddLeafNode(NodeKind::Name, *identifier);
  52. has_name = true;
  53. } else if (auto self =
  54. context.ConsumeIf(Lex::TokenKind::SelfValueIdentifier)) {
  55. // Checking will validate the `self` is only declared in the implicit
  56. // parameter list of a function.
  57. context.AddLeafNode(NodeKind::SelfValueName, *self);
  58. has_name = true;
  59. }
  60. if (!has_name) {
  61. // Add a placeholder for the name.
  62. context.AddLeafNode(NodeKind::Name, *context.position(),
  63. /*has_error=*/true);
  64. on_error();
  65. return;
  66. }
  67. if (auto kind = context.PositionKind();
  68. kind == Lex::TokenKind::Colon || kind == Lex::TokenKind::ColonExclaim) {
  69. state.state = kind == Lex::TokenKind::Colon ? State::PatternFinishAsRegular
  70. : State::PatternFinishAsGeneric;
  71. // Use the `:` or `:!` for the root node.
  72. state.token = context.Consume();
  73. context.PushState(state);
  74. context.PushStateForExpr(PrecedenceGroup::ForType());
  75. } else {
  76. on_error();
  77. return;
  78. }
  79. }
  80. auto HandlePatternAsImplicitParam(Context& context) -> void {
  81. HandlePattern(context, Context::PatternKind::ImplicitParam);
  82. }
  83. auto HandlePatternAsParam(Context& context) -> void {
  84. HandlePattern(context, Context::PatternKind::Param);
  85. }
  86. auto HandlePatternAsVariable(Context& context) -> void {
  87. HandlePattern(context, Context::PatternKind::Variable);
  88. }
  89. auto HandlePatternAsLet(Context& context) -> void {
  90. HandlePattern(context, Context::PatternKind::Let);
  91. }
  92. // Handles PatternFinishAs(Generic|Regular).
  93. static auto HandlePatternFinish(Context& context, NodeKind node_kind) -> void {
  94. auto state = context.PopState();
  95. context.AddNode(node_kind, state.token, state.subtree_start, state.has_error);
  96. // Propagate errors to the parent state so that they can take different
  97. // actions on invalid patterns.
  98. if (state.has_error) {
  99. context.ReturnErrorOnState();
  100. }
  101. }
  102. auto HandlePatternFinishAsGeneric(Context& context) -> void {
  103. HandlePatternFinish(context, NodeKind::GenericPatternBinding);
  104. }
  105. auto HandlePatternFinishAsRegular(Context& context) -> void {
  106. HandlePatternFinish(context, NodeKind::PatternBinding);
  107. }
  108. auto HandlePatternAddress(Context& context) -> void {
  109. auto state = context.PopState();
  110. context.AddNode(NodeKind::Address, state.token, state.subtree_start,
  111. state.has_error);
  112. // If an error was encountered, propagate it while adding a node.
  113. if (state.has_error) {
  114. context.ReturnErrorOnState();
  115. }
  116. }
  117. auto HandlePatternTemplate(Context& context) -> void {
  118. auto state = context.PopState();
  119. context.AddNode(NodeKind::Template, state.token, state.subtree_start,
  120. state.has_error);
  121. // If an error was encountered, propagate it while adding a node.
  122. if (state.has_error) {
  123. context.ReturnErrorOnState();
  124. }
  125. }
  126. } // namespace Carbon::Parse