handle_pattern.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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(DeducedParameter|FunctionParameter|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::DeducedParameter:
  22. case Context::PatternKind::Parameter: {
  23. CARBON_DIAGNOSTIC(ExpectedParameterName, Error,
  24. "Expected parameter declaration.");
  25. context.emitter().Emit(*context.position(), ExpectedParameterName);
  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, for deduced parameters, `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 (pattern_kind == Context::PatternKind::DeducedParameter) {
  54. if (auto self = context.ConsumeIf(Lex::TokenKind::SelfValueIdentifier)) {
  55. context.AddLeafNode(NodeKind::SelfValueName, *self);
  56. has_name = true;
  57. }
  58. }
  59. if (!has_name) {
  60. // Add a placeholder for the name.
  61. context.AddLeafNode(NodeKind::Name, *context.position(),
  62. /*has_error=*/true);
  63. on_error();
  64. return;
  65. }
  66. if (auto kind = context.PositionKind();
  67. kind == Lex::TokenKind::Colon || kind == Lex::TokenKind::ColonExclaim) {
  68. state.state = kind == Lex::TokenKind::Colon ? State::PatternFinishAsRegular
  69. : State::PatternFinishAsGeneric;
  70. // Use the `:` or `:!` for the root node.
  71. state.token = context.Consume();
  72. context.PushState(state);
  73. context.PushStateForExpression(PrecedenceGroup::ForType());
  74. } else {
  75. on_error();
  76. return;
  77. }
  78. }
  79. auto HandlePatternAsDeducedParameter(Context& context) -> void {
  80. HandlePattern(context, Context::PatternKind::DeducedParameter);
  81. }
  82. auto HandlePatternAsParameter(Context& context) -> void {
  83. HandlePattern(context, Context::PatternKind::Parameter);
  84. }
  85. auto HandlePatternAsVariable(Context& context) -> void {
  86. HandlePattern(context, Context::PatternKind::Variable);
  87. }
  88. auto HandlePatternAsLet(Context& context) -> void {
  89. HandlePattern(context, Context::PatternKind::Let);
  90. }
  91. // Handles PatternFinishAs(Generic|Regular).
  92. static auto HandlePatternFinish(Context& context, NodeKind node_kind) -> void {
  93. auto state = context.PopState();
  94. context.AddNode(node_kind, state.token, state.subtree_start, state.has_error);
  95. // Propagate errors to the parent state so that they can take different
  96. // actions on invalid patterns.
  97. if (state.has_error) {
  98. context.ReturnErrorOnState();
  99. }
  100. }
  101. auto HandlePatternFinishAsGeneric(Context& context) -> void {
  102. HandlePatternFinish(context, NodeKind::GenericPatternBinding);
  103. }
  104. auto HandlePatternFinishAsRegular(Context& context) -> void {
  105. HandlePatternFinish(context, NodeKind::PatternBinding);
  106. }
  107. auto HandlePatternAddress(Context& context) -> void {
  108. auto state = context.PopState();
  109. context.AddNode(NodeKind::Address, state.token, state.subtree_start,
  110. state.has_error);
  111. // If an error was encountered, propagate it while adding a node.
  112. if (state.has_error) {
  113. context.ReturnErrorOnState();
  114. }
  115. }
  116. auto HandlePatternTemplate(Context& context) -> void {
  117. auto state = context.PopState();
  118. context.AddNode(NodeKind::Template, state.token, state.subtree_start,
  119. state.has_error);
  120. // If an error was encountered, propagate it while adding a node.
  121. if (state.has_error) {
  122. context.ReturnErrorOnState();
  123. }
  124. }
  125. } // namespace Carbon::Parse