handle_let.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 HandleLet(Context& context) -> void {
  8. auto state = context.PopState();
  9. // These will start at the `let`.
  10. context.PushState(state, StateKind::LetFinish);
  11. context.PushState(state, StateKind::LetAfterPattern);
  12. // This will start at the pattern.
  13. context.PushState(StateKind::Pattern);
  14. }
  15. auto HandleLetAfterPattern(Context& context) -> void {
  16. auto state = context.PopState();
  17. if (state.has_error) {
  18. if (auto after_pattern =
  19. context.FindNextOf({Lex::TokenKind::Equal, Lex::TokenKind::Semi})) {
  20. context.SkipTo(*after_pattern);
  21. }
  22. }
  23. if (auto equals = context.ConsumeIf(Lex::TokenKind::Equal)) {
  24. context.AddLeafNode(NodeKind::LetInitializer, *equals);
  25. context.PushState(StateKind::Expr);
  26. }
  27. }
  28. auto HandleLetFinish(Context& context) -> void {
  29. auto state = context.PopState();
  30. auto end_token = state.token;
  31. if (context.PositionIs(Lex::TokenKind::Semi)) {
  32. end_token = context.Consume();
  33. } else {
  34. context.DiagnoseExpectedDeclSemi(Lex::TokenKind::Let);
  35. state.has_error = true;
  36. end_token = context.SkipPastLikelyEnd(state.token);
  37. }
  38. context.AddNode(NodeKind::LetDecl, end_token, state.has_error);
  39. }
  40. } // namespace Carbon::Parse