handle_var.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // Handles VarAs(Regular|Returned).
  8. static auto HandleVar(Context& context, StateKind finish_state_kind,
  9. Lex::TokenIndex returned_token = Lex::TokenIndex::None)
  10. -> void {
  11. auto state = context.PopState();
  12. // The finished variable declaration will start at the `var` or `returned`.
  13. context.PushState(state, finish_state_kind);
  14. // TODO: is there a cleaner way to give VarAfterPattern access to the `var`
  15. // token?
  16. state.token = *(context.position() - 1);
  17. context.PushState(state, StateKind::VarAfterPatternAsVar);
  18. if (returned_token.has_value()) {
  19. context.AddLeafNode(NodeKind::ReturnedModifier, returned_token);
  20. }
  21. context.PushStateForPattern(StateKind::Pattern, /*in_var_pattern=*/true);
  22. }
  23. auto HandleVarAsRegular(Context& context) -> void {
  24. HandleVar(context, StateKind::VarFinishAsRegular);
  25. }
  26. auto HandleVarAsReturned(Context& context) -> void {
  27. auto returned_token = context.Consume();
  28. if (!context.PositionIs(Lex::TokenKind::Var)) {
  29. CARBON_DIAGNOSTIC(ExpectedVarAfterReturned, Error,
  30. "expected `var` after `returned`");
  31. context.emitter().Emit(*context.position(), ExpectedVarAfterReturned);
  32. context.AddLeafNode(NodeKind::EmptyDecl,
  33. context.SkipPastLikelyEnd(returned_token),
  34. /*has_error=*/true);
  35. context.PopAndDiscardState();
  36. return;
  37. }
  38. context.AddLeafNode(NodeKind::VariableIntroducer, context.Consume());
  39. HandleVar(context, StateKind::VarFinishAsRegular, returned_token);
  40. }
  41. auto HandleVarAsFor(Context& context) -> void {
  42. auto state = context.PopState();
  43. // The finished variable declaration will start at the `var`.
  44. context.PushState(state, StateKind::VarFinishAsFor);
  45. context.PushState(StateKind::Pattern);
  46. }
  47. auto HandleFieldDecl(Context& context) -> void {
  48. auto state = context.PopState();
  49. auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier);
  50. if (!identifier) {
  51. CARBON_DIAGNOSTIC(ExpectedFieldIdentifier, Error,
  52. "expected identifier in field declaration");
  53. context.emitter().Emit(*context.position(), ExpectedFieldIdentifier);
  54. }
  55. auto colon = context.ConsumeIf(Lex::TokenKind::Colon);
  56. if (identifier && !colon) {
  57. CARBON_DIAGNOSTIC(ExpectedFieldColon, Error,
  58. "expected `:` in field declaration");
  59. context.emitter().Emit(*context.position(), ExpectedFieldColon);
  60. }
  61. if (!identifier || !colon) {
  62. context.AddNode(NodeKind::FieldDecl,
  63. context.SkipPastLikelyEnd(*(context.position() - 1)),
  64. /*has_error=*/true);
  65. state.has_error = true;
  66. return;
  67. }
  68. context.PushState(state, StateKind::VarFinishAsField);
  69. context.AddLeafNode(NodeKind::IdentifierNameNotBeforeParams, *identifier);
  70. state.token = *colon;
  71. context.PushState(state, StateKind::VarAfterPatternAsField);
  72. context.PushState(StateKind::Expr);
  73. }
  74. static auto HandleVarAfterPattern(Context& context, NodeKind pattern_kind,
  75. NodeKind init_kind) -> void {
  76. auto state = context.PopState();
  77. if (state.has_error) {
  78. if (auto after_pattern =
  79. context.FindNextOf({Lex::TokenKind::Equal, Lex::TokenKind::Semi})) {
  80. context.SkipTo(*after_pattern);
  81. }
  82. }
  83. context.AddNode(pattern_kind, state.token, state.has_error);
  84. if (context.PositionIs(Lex::TokenKind::Equal)) {
  85. context.AddLeafNode(init_kind,
  86. context.ConsumeChecked(Lex::TokenKind::Equal));
  87. context.PushState(StateKind::Expr);
  88. }
  89. }
  90. auto HandleVarAfterPatternAsVar(Context& context) -> void {
  91. HandleVarAfterPattern(context, NodeKind::VariablePattern,
  92. NodeKind::VariableInitializer);
  93. }
  94. auto HandleVarAfterPatternAsField(Context& context) -> void {
  95. HandleVarAfterPattern(context, NodeKind::FieldNameAndType,
  96. NodeKind::FieldInitializer);
  97. }
  98. static auto HandleVarFinish(Context& context, NodeKind node_kind) -> void {
  99. auto state = context.PopState();
  100. auto end_token = state.token;
  101. if (context.PositionIs(Lex::TokenKind::Semi)) {
  102. end_token = context.Consume();
  103. } else {
  104. // TODO: Disambiguate between statement and member declaration.
  105. context.DiagnoseExpectedDeclSemi(Lex::TokenKind::Var);
  106. state.has_error = true;
  107. end_token = context.SkipPastLikelyEnd(state.token);
  108. }
  109. context.AddNode(node_kind, end_token, state.has_error);
  110. }
  111. auto HandleVarFinishAsRegular(Context& context) -> void {
  112. HandleVarFinish(context, NodeKind::VariableDecl);
  113. }
  114. auto HandleVarFinishAsField(Context& context) -> void {
  115. HandleVarFinish(context, NodeKind::FieldDecl);
  116. }
  117. auto HandleVarFinishAsFor(Context& context) -> void {
  118. auto state = context.PopState();
  119. context.AddNode(NodeKind::VariablePattern, state.token, state.has_error);
  120. auto end_token = state.token;
  121. if (context.PositionIs(Lex::TokenKind::In)) {
  122. end_token = context.Consume();
  123. } else if (context.PositionIs(Lex::TokenKind::Colon)) {
  124. CARBON_DIAGNOSTIC(ExpectedInNotColon, Error,
  125. "`:` should be replaced by `in`");
  126. context.emitter().Emit(*context.position(), ExpectedInNotColon);
  127. state.has_error = true;
  128. end_token = context.Consume();
  129. } else {
  130. CARBON_DIAGNOSTIC(ExpectedIn, Error,
  131. "expected `in` after loop `var` declaration");
  132. context.emitter().Emit(*context.position(), ExpectedIn);
  133. state.has_error = true;
  134. }
  135. context.AddNode(NodeKind::ForIn, end_token, state.has_error);
  136. }
  137. auto HandleVariablePattern(Context& context) -> void {
  138. auto state = context.PopState();
  139. if (state.in_var_pattern) {
  140. CARBON_DIAGNOSTIC(NestedVar, Error, "`var` nested within another `var`");
  141. context.emitter().Emit(*context.position(), NestedVar);
  142. state.has_error = true;
  143. }
  144. context.PushState(StateKind::FinishVariablePattern);
  145. context.ConsumeChecked(Lex::TokenKind::Var);
  146. context.PushStateForPattern(StateKind::Pattern, /*in_var_pattern=*/true);
  147. }
  148. auto HandleFinishVariablePattern(Context& context) -> void {
  149. auto state = context.PopState();
  150. context.AddNode(NodeKind::VariablePattern, state.token, state.has_error);
  151. // Propagate errors to the parent state so that they can take different
  152. // actions on invalid patterns.
  153. if (state.has_error) {
  154. context.ReturnErrorOnState();
  155. }
  156. }
  157. } // namespace Carbon::Parse