handle_var.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /*in_unused_pattern=*/false);
  23. }
  24. auto HandleVarAsRegular(Context& context) -> void {
  25. HandleVar(context, StateKind::VarFinishAsRegular);
  26. }
  27. auto HandleVarAsReturned(Context& context) -> void {
  28. auto returned_token = context.Consume();
  29. if (!context.PositionIs(Lex::TokenKind::Var)) {
  30. CARBON_DIAGNOSTIC(ExpectedVarAfterReturned, Error,
  31. "expected `var` after `returned`");
  32. context.emitter().Emit(*context.position(), ExpectedVarAfterReturned);
  33. context.AddLeafNode(NodeKind::EmptyDecl,
  34. context.SkipPastLikelyEnd(returned_token),
  35. /*has_error=*/true);
  36. context.PopAndDiscardState();
  37. return;
  38. }
  39. context.AddLeafNode(NodeKind::VariableIntroducer, context.Consume());
  40. HandleVar(context, StateKind::VarFinishAsRegular, returned_token);
  41. }
  42. auto HandleFieldDecl(Context& context) -> void {
  43. auto state = context.PopState();
  44. auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier);
  45. if (!identifier) {
  46. CARBON_DIAGNOSTIC(ExpectedFieldIdentifier, Error,
  47. "expected identifier in field declaration");
  48. context.emitter().Emit(*context.position(), ExpectedFieldIdentifier);
  49. }
  50. auto colon = context.ConsumeIf(Lex::TokenKind::Colon);
  51. if (identifier && !colon) {
  52. CARBON_DIAGNOSTIC(ExpectedFieldColon, Error,
  53. "expected `:` in field declaration");
  54. context.emitter().Emit(*context.position(), ExpectedFieldColon);
  55. }
  56. if (!identifier || !colon) {
  57. context.AddNode(NodeKind::FieldDecl,
  58. context.SkipPastLikelyEnd(*(context.position() - 1)),
  59. /*has_error=*/true);
  60. state.has_error = true;
  61. return;
  62. }
  63. context.PushState(state, StateKind::VarFinishAsField);
  64. context.AddLeafNode(NodeKind::IdentifierNameNotBeforeSignature, *identifier);
  65. state.token = *colon;
  66. context.PushState(state, StateKind::VarAfterPatternAsField);
  67. context.PushState(StateKind::Expr);
  68. }
  69. static auto HandleVarAfterPattern(Context& context, NodeKind pattern_kind,
  70. NodeKind init_kind) -> void {
  71. auto state = context.PopState();
  72. if (state.has_error) {
  73. if (auto after_pattern =
  74. context.FindNextOf({Lex::TokenKind::Equal, Lex::TokenKind::Semi})) {
  75. context.SkipTo(*after_pattern);
  76. }
  77. }
  78. context.AddNode(pattern_kind, state.token, state.has_error);
  79. if (context.PositionIs(Lex::TokenKind::Equal)) {
  80. context.AddLeafNode(init_kind,
  81. context.ConsumeChecked(Lex::TokenKind::Equal));
  82. context.PushState(StateKind::Expr);
  83. }
  84. }
  85. auto HandleVarAfterPatternAsVar(Context& context) -> void {
  86. HandleVarAfterPattern(context, NodeKind::VariablePattern,
  87. NodeKind::VariableInitializer);
  88. }
  89. auto HandleVarAfterPatternAsField(Context& context) -> void {
  90. HandleVarAfterPattern(context, NodeKind::FieldNameAndType,
  91. NodeKind::FieldInitializer);
  92. }
  93. static auto HandleVarFinish(Context& context, NodeKind node_kind) -> void {
  94. auto state = context.PopState();
  95. auto end_token = state.token;
  96. if (context.PositionIs(Lex::TokenKind::Semi)) {
  97. end_token = context.Consume();
  98. } else {
  99. // TODO: Disambiguate between statement and member declaration.
  100. context.DiagnoseExpectedDeclSemi(Lex::TokenKind::Var);
  101. state.has_error = true;
  102. end_token = context.SkipPastLikelyEnd(state.token);
  103. }
  104. context.AddNode(node_kind, end_token, state.has_error);
  105. }
  106. auto HandleVarFinishAsRegular(Context& context) -> void {
  107. HandleVarFinish(context, NodeKind::VariableDecl);
  108. }
  109. auto HandleVarFinishAsField(Context& context) -> void {
  110. HandleVarFinish(context, NodeKind::FieldDecl);
  111. }
  112. auto HandleVariablePattern(Context& context) -> void {
  113. auto state = context.PopState();
  114. if (state.in_var_pattern) {
  115. CARBON_DIAGNOSTIC(NestedVar, Error, "`var` nested within another `var`");
  116. context.emitter().Emit(*context.position(), NestedVar);
  117. state.has_error = true;
  118. }
  119. context.PushState(StateKind::FinishVariablePattern);
  120. context.ConsumeChecked(Lex::TokenKind::Var);
  121. context.PushStateForPattern(StateKind::Pattern, /*in_var_pattern=*/true,
  122. state.in_unused_pattern);
  123. }
  124. auto HandleFinishVariablePattern(Context& context) -> void {
  125. auto state = context.PopState();
  126. context.AddNode(NodeKind::VariablePattern, state.token, state.has_error);
  127. // Propagate errors to the parent state so that they can take different
  128. // actions on invalid patterns.
  129. if (state.has_error) {
  130. context.ReturnErrorOnState();
  131. }
  132. }
  133. } // namespace Carbon::Parse