handle_requirement.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/lex/token_kind.h"
  5. #include "toolchain/parse/context.h"
  6. #include "toolchain/parse/handle.h"
  7. namespace Carbon::Parse {
  8. auto HandleRequirementBegin(Context& context) -> void {
  9. auto state = context.PopState();
  10. // Peek ahead for `.designator = ...`, and give it special handling.
  11. if (context.PositionKind() == Lex::TokenKind::Period &&
  12. context.PositionKind(Lookahead::NextToken) ==
  13. Lex::TokenKind::Identifier &&
  14. context.PositionKind(static_cast<Lookahead>(2)) ==
  15. Lex::TokenKind::Equal) {
  16. auto period = context.Consume();
  17. context.AddNode(NodeKind::IdentifierNameNotBeforeSignature,
  18. context.Consume(),
  19. /*has_error=*/false);
  20. context.AddNode(NodeKind::DesignatorExpr, period, /*has_error=*/false);
  21. state.token = context.Consume();
  22. context.PushState(state, StateKind::RequirementOperatorFinish);
  23. } else {
  24. context.PushState(StateKind::RequirementOperator);
  25. }
  26. context.PushStateForExpr(PrecedenceGroup::ForRequirements());
  27. }
  28. auto HandleRequirementOperator(Context& context) -> void {
  29. auto state = context.PopState();
  30. switch (context.PositionKind()) {
  31. // Accept either `impls` or `==`
  32. case Lex::TokenKind::Impls:
  33. case Lex::TokenKind::EqualEqual:
  34. break;
  35. // Reject `=` since correct usage is consumed in `HandleRequirementBegin`.
  36. case Lex::TokenKind::Equal: {
  37. if (!state.has_error) {
  38. CARBON_DIAGNOSTIC(
  39. RequirementEqualAfterNonDesignator, Error,
  40. "requirement can only use `=` after `.member` designator");
  41. context.emitter().Emit(*context.position(),
  42. RequirementEqualAfterNonDesignator);
  43. }
  44. context.ReturnErrorOnState();
  45. return;
  46. }
  47. default: {
  48. if (!state.has_error) {
  49. CARBON_DIAGNOSTIC(
  50. ExpectedRequirementOperator, Error,
  51. "requirement should use `impls`, `=`, or `==` operator");
  52. context.emitter().Emit(*context.position(),
  53. ExpectedRequirementOperator);
  54. }
  55. context.ReturnErrorOnState();
  56. return;
  57. }
  58. }
  59. state.token = context.Consume();
  60. context.PushState(state, StateKind::RequirementOperatorFinish);
  61. context.PushStateForExpr(PrecedenceGroup::ForRequirements());
  62. }
  63. auto HandleRequirementOperatorFinish(Context& context) -> void {
  64. auto state = context.PopState();
  65. switch (auto token_kind = context.tokens().GetKind(state.token)) {
  66. case Lex::TokenKind::Impls: {
  67. context.AddNode(NodeKind::RequirementImpls, state.token, state.has_error);
  68. break;
  69. }
  70. case Lex::TokenKind::Equal: {
  71. context.AddNode(NodeKind::RequirementEqual, state.token, state.has_error);
  72. break;
  73. }
  74. case Lex::TokenKind::EqualEqual: {
  75. context.AddNode(NodeKind::RequirementEqualEqual, state.token,
  76. state.has_error);
  77. break;
  78. }
  79. default:
  80. // RequirementOperatorFinish state is only pushed in
  81. // HandleRequirementOperator on one of the three requirement operator
  82. // tokens.
  83. CARBON_FATAL("Unexpected token kind for requirement operator: {0}",
  84. token_kind);
  85. return;
  86. }
  87. if (state.has_error) {
  88. context.ReturnErrorOnState();
  89. }
  90. if (auto token = context.ConsumeIf(Lex::TokenKind::And)) {
  91. context.AddNode(NodeKind::RequirementAnd, *token, /*has_error=*/false);
  92. context.PushState(StateKind::RequirementBegin);
  93. }
  94. }
  95. auto HandleWhereFinish(Context& context) -> void {
  96. auto state = context.PopState();
  97. if (state.has_error) {
  98. context.ReturnErrorOnState();
  99. }
  100. context.AddNode(NodeKind::WhereExpr, state.token, state.has_error);
  101. }
  102. } // namespace Carbon::Parse