handle_var.cpp 5.6 KB

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