handle_impl.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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(Carbon::Parse::Context& context) -> void {
  7. if (context.PositionIs(Lex::TokenKind::As)) {
  8. // as <expression> ...
  9. context.AddLeafNode(NodeKind::ImplAs, 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(Carbon::Parse::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(Carbon::Parse::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(Carbon::Parse::Context& context) -> void {
  58. auto state = context.PopState();
  59. if (state.has_error) {
  60. context.ReturnErrorOnState();
  61. return;
  62. }
  63. if (auto as = context.ConsumeIf(Lex::TokenKind::As)) {
  64. context.AddLeafNode(NodeKind::ImplAs, *as);
  65. context.PushState(State::Expr);
  66. } else {
  67. CARBON_DIAGNOSTIC(ImplExpectedAs, Error,
  68. "Expected `as` in `impl` declaration.");
  69. context.emitter().Emit(*context.position(), ImplExpectedAs);
  70. context.ReturnErrorOnState();
  71. }
  72. }
  73. } // namespace Carbon::Parse