handle_impl.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.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.has_error);
  61. context.PushState(State::Expr);
  62. } else {
  63. if (!state.has_error) {
  64. CARBON_DIAGNOSTIC(ImplExpectedAs, Error,
  65. "Expected `as` in `impl` declaration.");
  66. context.emitter().Emit(*context.position(), ImplExpectedAs);
  67. }
  68. context.ReturnErrorOnState();
  69. }
  70. }
  71. } // namespace Carbon::Parse