handle_paren_expr.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. auto HandleOnlyParenExpr(Context& context) -> void {
  8. auto state = context.PopState();
  9. // Advance past the open paren.
  10. auto open_paren = context.ConsumeChecked(Lex::TokenKind::OpenParen);
  11. context.AddLeafNode(NodeKind::ParenExprStart, open_paren);
  12. state.token = open_paren;
  13. context.PushState(state, StateKind::OnlyParenExprFinish);
  14. context.PushState(StateKind::Expr);
  15. }
  16. static auto FinishParenExpr(Context& context, const Context::State& state)
  17. -> void {
  18. context.AddNode(NodeKind::ParenExpr, context.Consume(), state.has_error);
  19. }
  20. auto HandleOnlyParenExprFinish(Context& context) -> void {
  21. auto state = context.PopState();
  22. if (!context.PositionIs(Lex::TokenKind::CloseParen)) {
  23. if (!state.has_error) {
  24. CARBON_DIAGNOSTIC(UnexpectedTokenInCompoundMemberAccess, Error,
  25. "expected `)`");
  26. context.emitter().Emit(*context.position(),
  27. UnexpectedTokenInCompoundMemberAccess);
  28. state.has_error = true;
  29. }
  30. // Recover from the invalid token.
  31. context.SkipTo(context.tokens().GetMatchedClosingToken(state.token));
  32. }
  33. FinishParenExpr(context, state);
  34. }
  35. static auto HandleRefTagFinish(Context& context, StateKind state_kind) -> void {
  36. auto state = context.PopState();
  37. if (state_kind == StateKind::RefTagFinishAsAfterOpenParen &&
  38. !context.PositionIs(Lex::TokenKind::Comma)) {
  39. CARBON_DIAGNOSTIC(UnexpectedRef, Error,
  40. "found `ref` in unexpected position");
  41. context.emitter().Emit(state.token, UnexpectedRef);
  42. }
  43. context.AddNode(NodeKind::RefTag, state.token, state.has_error);
  44. }
  45. auto HandleRefTagFinishAsRegular(Context& context) -> void {
  46. HandleRefTagFinish(context, StateKind::RefTagFinishAsRegular);
  47. }
  48. auto HandleRefTagFinishAsAfterOpenParen(Context& context) -> void {
  49. HandleRefTagFinish(context, StateKind::RefTagFinishAsAfterOpenParen);
  50. }
  51. static auto StartTupleLiteralElement(Context& context) -> void {
  52. context.PushState(StateKind::TupleLiteralElementFinish);
  53. if (context.PositionIs(Lex::TokenKind::Ref)) {
  54. context.PushState(StateKind::RefTagFinishAsRegular);
  55. context.ConsumeChecked(Lex::TokenKind::Ref);
  56. }
  57. context.PushState(StateKind::Expr);
  58. }
  59. auto HandleParenExpr(Context& context) -> void {
  60. auto state = context.PopState();
  61. // Advance past the open paren. The placeholder will be replaced at the end
  62. // based on whether we determine this is a tuple or parenthesized expression.
  63. context.AddLeafNode(NodeKind::Placeholder,
  64. context.ConsumeChecked(Lex::TokenKind::OpenParen));
  65. if (context.PositionIs(Lex::TokenKind::CloseParen)) {
  66. context.PushState(state, StateKind::TupleLiteralFinish);
  67. } else {
  68. context.PushState(state, StateKind::ParenExprFinish);
  69. context.PushState(StateKind::ExprAfterOpenParenFinish);
  70. if (context.PositionIs(Lex::TokenKind::Ref)) {
  71. context.PushState(StateKind::RefTagFinishAsAfterOpenParen);
  72. context.ConsumeChecked(Lex::TokenKind::Ref);
  73. }
  74. context.PushState(StateKind::Expr);
  75. }
  76. }
  77. auto HandleExprAfterOpenParenFinish(Context& context) -> void {
  78. auto state = context.PopState();
  79. auto list_token_kind = context.ConsumeListToken(
  80. NodeKind::TupleLiteralComma, Lex::TokenKind::CloseParen, state.has_error);
  81. if (list_token_kind == Context::ListTokenKind::Close) {
  82. return;
  83. }
  84. // We found a comma, so switch parent state to tuple handling.
  85. auto finish_state = context.PopState();
  86. CARBON_CHECK(finish_state.kind == StateKind::ParenExprFinish,
  87. "Unexpected parent state, found: {0}", finish_state.kind);
  88. context.PushState(finish_state, StateKind::TupleLiteralFinish);
  89. // If the comma is not immediately followed by a close paren, push handlers
  90. // for the next tuple element.
  91. if (list_token_kind != Context::ListTokenKind::CommaClose) {
  92. StartTupleLiteralElement(context);
  93. }
  94. }
  95. auto HandleTupleLiteralElementFinish(Context& context) -> void {
  96. auto state = context.PopState();
  97. if (state.has_error) {
  98. context.ReturnErrorOnState();
  99. }
  100. if (context.ConsumeListToken(NodeKind::TupleLiteralComma,
  101. Lex::TokenKind::CloseParen, state.has_error) ==
  102. Context::ListTokenKind::Comma) {
  103. StartTupleLiteralElement(context);
  104. }
  105. }
  106. auto HandleParenExprFinish(Context& context) -> void {
  107. auto state = context.PopState();
  108. context.ReplacePlaceholderNode(state.subtree_start, NodeKind::ParenExprStart,
  109. state.token);
  110. FinishParenExpr(context, state);
  111. }
  112. auto HandleTupleLiteralFinish(Context& context) -> void {
  113. auto state = context.PopState();
  114. context.ReplacePlaceholderNode(state.subtree_start,
  115. NodeKind::TupleLiteralStart, state.token);
  116. context.AddNode(NodeKind::TupleLiteral, context.Consume(), state.has_error);
  117. }
  118. auto HandleCallExpr(Context& context) -> void {
  119. auto state = context.PopState();
  120. context.PushState(state, StateKind::CallExprFinish);
  121. context.AddNode(NodeKind::CallExprStart, context.Consume(), state.has_error);
  122. if (!context.PositionIs(Lex::TokenKind::CloseParen)) {
  123. StartTupleLiteralElement(context);
  124. }
  125. }
  126. auto HandleCallExprFinish(Context& context) -> void {
  127. auto state = context.PopState();
  128. context.AddNode(NodeKind::CallExpr, context.Consume(), state.has_error);
  129. }
  130. } // namespace Carbon::Parse