parser_handle_pattern.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/parser/parser_context.h"
  5. namespace Carbon::Parse {
  6. // Handles PatternAs(DeducedParameter|FunctionParameter|Variable).
  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(TokenKind::Template, State::PatternTemplate,
  14. state.subtree_start);
  15. context.ConsumeIfPatternKeyword(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. }
  35. // Add a placeholder for the type.
  36. context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
  37. /*has_error=*/true);
  38. state.state = State::PatternFinishAsRegular;
  39. state.has_error = true;
  40. context.PushState(state);
  41. };
  42. // The first item should be an identifier or, for deduced parameters, `self`.
  43. bool has_name = false;
  44. if (auto identifier = context.ConsumeIf(TokenKind::Identifier)) {
  45. context.AddLeafNode(NodeKind::Name, *identifier);
  46. has_name = true;
  47. } else if (pattern_kind == Context::PatternKind::DeducedParameter) {
  48. if (auto self = context.ConsumeIf(TokenKind::SelfValueIdentifier)) {
  49. context.AddLeafNode(NodeKind::SelfValueName, *self);
  50. has_name = true;
  51. }
  52. }
  53. if (!has_name) {
  54. // Add a placeholder for the name.
  55. context.AddLeafNode(NodeKind::Name, *context.position(),
  56. /*has_error=*/true);
  57. on_error();
  58. return;
  59. }
  60. if (auto kind = context.PositionKind();
  61. kind == TokenKind::Colon || kind == TokenKind::ColonExclaim) {
  62. state.state = kind == TokenKind::Colon ? State::PatternFinishAsRegular
  63. : State::PatternFinishAsGeneric;
  64. // Use the `:` or `:!` for the root node.
  65. state.token = context.Consume();
  66. context.PushState(state);
  67. context.PushStateForExpression(PrecedenceGroup::ForType());
  68. } else {
  69. on_error();
  70. return;
  71. }
  72. }
  73. auto HandlePatternAsDeducedParameter(Context& context) -> void {
  74. HandlePattern(context, Context::PatternKind::DeducedParameter);
  75. }
  76. auto HandlePatternAsParameter(Context& context) -> void {
  77. HandlePattern(context, Context::PatternKind::Parameter);
  78. }
  79. auto HandlePatternAsVariable(Context& context) -> void {
  80. HandlePattern(context, Context::PatternKind::Variable);
  81. }
  82. // Handles PatternFinishAs(Generic|Regular).
  83. static auto HandlePatternFinish(Context& context, NodeKind node_kind) -> void {
  84. auto state = context.PopState();
  85. context.AddNode(node_kind, state.token, state.subtree_start, state.has_error);
  86. // Propagate errors to the parent state so that they can take different
  87. // actions on invalid patterns.
  88. if (state.has_error) {
  89. context.ReturnErrorOnState();
  90. }
  91. }
  92. auto HandlePatternFinishAsGeneric(Context& context) -> void {
  93. HandlePatternFinish(context, NodeKind::GenericPatternBinding);
  94. }
  95. auto HandlePatternFinishAsRegular(Context& context) -> void {
  96. HandlePatternFinish(context, NodeKind::PatternBinding);
  97. }
  98. auto HandlePatternAddress(Context& context) -> void {
  99. auto state = context.PopState();
  100. context.AddNode(NodeKind::Address, state.token, state.subtree_start,
  101. state.has_error);
  102. // If an error was encountered, propagate it while adding a node.
  103. if (state.has_error) {
  104. context.ReturnErrorOnState();
  105. }
  106. }
  107. auto HandlePatternTemplate(Context& context) -> void {
  108. auto state = context.PopState();
  109. context.AddNode(NodeKind::Template, 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. } // namespace Carbon::Parse