handle_impl.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include "toolchain/parse/handle.h"
  6. namespace Carbon::Parse {
  7. static auto ExpectAsOrTypeExpression(Context& context) -> void {
  8. if (context.PositionIs(Lex::TokenKind::As)) {
  9. // as <expression> ...
  10. context.AddLeafNode(NodeKind::DefaultSelfImplAs, context.Consume());
  11. context.PushState(State::Expr);
  12. } else {
  13. // <expression> as <expression>...
  14. context.PushState(State::ImplBeforeAs);
  15. context.PushStateForExpr(PrecedenceGroup::ForImplAs());
  16. }
  17. }
  18. auto HandleImplAfterIntroducer(Context& context) -> void {
  19. auto state = context.PopState();
  20. state.state = State::DeclOrDefinitionAsImpl;
  21. context.PushState(state);
  22. if (context.PositionIs(Lex::TokenKind::Forall)) {
  23. // forall [<implicit parameter list>] ...
  24. context.PushState(State::ImplAfterForall);
  25. context.ConsumeAndDiscard();
  26. if (context.PositionIs(Lex::TokenKind::OpenSquareBracket)) {
  27. context.PushState(State::PatternListAsImplicit);
  28. } else {
  29. CARBON_DIAGNOSTIC(ImplExpectedAfterForall, Error,
  30. "Expected `[` after `forall` in `impl` declaration.");
  31. context.emitter().Emit(*context.position(), ImplExpectedAfterForall);
  32. context.ReturnErrorOnState();
  33. // If we aren't producing a node from the PatternListAsImplicit state,
  34. // we still need to create a node to be the child of the `ImplForall`
  35. // token created in the `ImplAfterForall` state.
  36. context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
  37. /*has_error=*/true);
  38. }
  39. } else {
  40. // One of:
  41. // as <expression> ...
  42. // <expression> as <expression>...
  43. ExpectAsOrTypeExpression(context);
  44. }
  45. }
  46. auto HandleImplAfterForall(Context& context) -> void {
  47. auto state = context.PopState();
  48. if (state.has_error) {
  49. context.ReturnErrorOnState();
  50. }
  51. context.AddNode(NodeKind::ImplForall, state.token, state.subtree_start,
  52. state.has_error);
  53. // One of:
  54. // as <expression> ...
  55. // <expression> as <expression>...
  56. ExpectAsOrTypeExpression(context);
  57. }
  58. auto HandleImplBeforeAs(Context& context) -> void {
  59. auto state = context.PopState();
  60. if (auto as = context.ConsumeIf(Lex::TokenKind::As)) {
  61. context.AddNode(NodeKind::TypeImplAs, *as, state.subtree_start,
  62. state.has_error);
  63. context.PushState(State::Expr);
  64. } else {
  65. if (!state.has_error) {
  66. CARBON_DIAGNOSTIC(ImplExpectedAs, Error,
  67. "Expected `as` in `impl` declaration.");
  68. context.emitter().Emit(*context.position(), ImplExpectedAs);
  69. }
  70. context.ReturnErrorOnState();
  71. }
  72. }
  73. } // namespace Carbon::Parse