handle_impl.cpp 2.8 KB

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