handle_impl.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.AddLeafNode(NodeKind::Forall, context.Consume());
  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.AddInvalidParse(*context.position());
  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. // One of:
  51. // as <expression> ...
  52. // <expression> as <expression>...
  53. ExpectAsOrTypeExpression(context);
  54. }
  55. auto HandleImplBeforeAs(Context& context) -> void {
  56. auto state = context.PopState();
  57. if (auto as = context.ConsumeIf(Lex::TokenKind::As)) {
  58. context.AddNode(NodeKind::TypeImplAs, *as, state.has_error);
  59. context.PushState(State::Expr);
  60. } else {
  61. if (!state.has_error) {
  62. CARBON_DIAGNOSTIC(ImplExpectedAs, Error,
  63. "expected `as` in `impl` declaration");
  64. context.emitter().Emit(*context.position(), ImplExpectedAs);
  65. }
  66. context.ReturnErrorOnState();
  67. }
  68. }
  69. } // namespace Carbon::Parse