handle_var.cpp 5.4 KB

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