handle_var.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // Handles VarAs(Semicolon|For).
  7. static auto HandleVar(Context& context, State finish_state) -> void {
  8. context.PopAndDiscardState();
  9. // These will start at the `var`.
  10. context.PushState(finish_state);
  11. context.PushState(State::VarAfterPattern);
  12. context.AddLeafNode(NodeKind::VariableIntroducer, context.Consume());
  13. // This will start at the pattern.
  14. context.PushState(State::PatternAsVariable);
  15. }
  16. auto HandleVarAsSemicolon(Context& context) -> void {
  17. HandleVar(context, State::VarFinishAsSemicolon);
  18. }
  19. auto HandleVarAsFor(Context& context) -> void {
  20. HandleVar(context, State::VarFinishAsFor);
  21. }
  22. auto HandleVarAfterPattern(Context& context) -> void {
  23. auto state = context.PopState();
  24. if (state.has_error) {
  25. if (auto after_pattern =
  26. context.FindNextOf({Lex::TokenKind::Equal, Lex::TokenKind::Semi})) {
  27. context.SkipTo(*after_pattern);
  28. }
  29. }
  30. if (auto equals = context.ConsumeIf(Lex::TokenKind::Equal)) {
  31. context.AddLeafNode(NodeKind::VariableInitializer, *equals);
  32. context.PushState(State::Expr);
  33. }
  34. }
  35. auto HandleVarFinishAsSemicolon(Context& context) -> void {
  36. auto state = context.PopState();
  37. auto end_token = state.token;
  38. if (context.PositionIs(Lex::TokenKind::Semi)) {
  39. end_token = context.Consume();
  40. } else {
  41. // TODO: Disambiguate between statement and member declaration.
  42. context.EmitExpectedDeclSemi(Lex::TokenKind::Var);
  43. state.has_error = true;
  44. if (auto semi_token = context.SkipPastLikelyEnd(state.token)) {
  45. end_token = *semi_token;
  46. }
  47. }
  48. context.AddNode(NodeKind::VariableDecl, end_token, state.subtree_start,
  49. state.has_error);
  50. }
  51. auto HandleVarFinishAsFor(Context& context) -> void {
  52. auto state = context.PopState();
  53. auto end_token = state.token;
  54. if (context.PositionIs(Lex::TokenKind::In)) {
  55. end_token = context.Consume();
  56. } else if (context.PositionIs(Lex::TokenKind::Colon)) {
  57. CARBON_DIAGNOSTIC(ExpectedInNotColon, Error,
  58. "`:` should be replaced by `in`.");
  59. context.emitter().Emit(*context.position(), ExpectedInNotColon);
  60. state.has_error = true;
  61. end_token = context.Consume();
  62. } else {
  63. CARBON_DIAGNOSTIC(ExpectedIn, Error,
  64. "Expected `in` after loop `var` declaration.");
  65. context.emitter().Emit(*context.position(), ExpectedIn);
  66. state.has_error = true;
  67. }
  68. context.AddNode(NodeKind::ForIn, end_token, state.subtree_start,
  69. state.has_error);
  70. }
  71. } // namespace Carbon::Parse