parser_handle_pattern.cpp 5.0 KB

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