handle_lambda.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. auto HandleLambdaIntroducer(Context& context) -> void {
  8. auto state = context.PopState();
  9. context.AddLeafNode(NodeKind::LambdaIntroducer, context.Consume());
  10. context.PushState(state, StateKind::LambdaAfterIntroducer);
  11. }
  12. auto HandleLambdaAfterIntroducer(Context& context) -> void {
  13. auto state = context.PopState();
  14. if (context.PositionIs(Lex::TokenKind::OpenSquareBracket)) {
  15. context.PushState(state, StateKind::LambdaAfterImplicitParams);
  16. context.PushState(StateKind::PatternListAsImplicit);
  17. } else if (context.PositionIs(Lex::TokenKind::OpenParen)) {
  18. context.PushState(state, StateKind::LambdaAfterParams);
  19. context.PushState(StateKind::PatternListAsExplicit);
  20. } else {
  21. // No implicit or explicit params.
  22. context.PushState(state, StateKind::LambdaAfterParams);
  23. }
  24. }
  25. auto HandleLambdaAfterImplicitParams(Context& context) -> void {
  26. auto state = context.PopState();
  27. if (context.PositionIs(Lex::TokenKind::OpenParen)) {
  28. context.PushState(state, StateKind::LambdaAfterParams);
  29. context.PushState(StateKind::PatternListAsExplicit);
  30. } else {
  31. // No explicit params after implicit params.
  32. context.PushState(state, StateKind::LambdaAfterParams);
  33. }
  34. }
  35. auto HandleLambdaAfterParams(Context& context) -> void {
  36. auto state = context.PopState();
  37. if (context.PositionIs(Lex::TokenKind::MinusGreater)) {
  38. // Has return type.
  39. context.PushState(state, StateKind::LambdaBody);
  40. context.PushState(StateKind::FunctionReturnTypeFinish);
  41. context.ConsumeAndDiscard();
  42. context.PushStateForExpr(PrecedenceGroup::ForType());
  43. } else if (context.PositionIs(Lex::TokenKind::EqualGreater)) {
  44. // Terse body `=> expr`
  45. context.AddLeafNode(NodeKind::TerseBodyArrow, context.Consume());
  46. context.PushState(state, StateKind::LambdaBodyFinish);
  47. context.PushStateForExpr(PrecedenceGroup::ForTopLevelExpr());
  48. } else if (context.PositionIs(Lex::TokenKind::OpenCurlyBrace)) {
  49. // Block body `{ ... }`
  50. context.PushState(state, StateKind::LambdaBodyFinish);
  51. context.PushState(StateKind::CodeBlock);
  52. } else {
  53. CARBON_DIAGNOSTIC(ExpectedLambdaBody, Error,
  54. "expected `->`, `=>`, or `{{`");
  55. context.emitter().Emit(*context.position(), ExpectedLambdaBody);
  56. state.has_error = true;
  57. context.ReturnErrorOnState();
  58. }
  59. }
  60. auto HandleLambdaBody(Context& context) -> void {
  61. auto state = context.PopState();
  62. // We arrive here after parsing return type.
  63. // So we look for `=>` or `{`.
  64. if (context.PositionIs(Lex::TokenKind::EqualGreater)) {
  65. // Terse body `=> expr`
  66. context.AddLeafNode(NodeKind::TerseBodyArrow, context.Consume());
  67. context.PushState(state, StateKind::LambdaBodyFinish);
  68. context.PushStateForExpr(PrecedenceGroup::ForTopLevelExpr());
  69. } else if (context.PositionIs(Lex::TokenKind::OpenCurlyBrace)) {
  70. // Block body `{ ... }`
  71. context.PushState(state, StateKind::LambdaBodyFinish);
  72. context.PushState(StateKind::CodeBlock);
  73. } else {
  74. CARBON_DIAGNOSTIC(ExpectedLambdaBodyAfterReturnType, Error,
  75. "expected `=>` or `{{` after return type");
  76. context.emitter().Emit(*context.position(),
  77. ExpectedLambdaBodyAfterReturnType);
  78. // Add a dummy node for the missing body without consuming the current
  79. // token.
  80. context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
  81. /*has_error=*/true);
  82. state.has_error = true;
  83. // Bundle all nodes into a complete lambda node.
  84. context.PushState(state, StateKind::LambdaBodyFinish);
  85. }
  86. }
  87. auto HandleLambdaBodyFinish(Context& context) -> void {
  88. auto state = context.PopState();
  89. context.AddNode(NodeKind::Lambda, state.token, state.has_error);
  90. }
  91. } // namespace Carbon::Parse