handle_paren_expr.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. auto HandleParenExpr(Context& context) -> void {
  36. auto state = context.PopState();
  37. // Advance past the open paren. The placeholder will be replaced at the end
  38. // based on whether we determine this is a tuple or parenthesized expression.
  39. context.AddLeafNode(NodeKind::Placeholder,
  40. context.ConsumeChecked(Lex::TokenKind::OpenParen));
  41. if (context.PositionIs(Lex::TokenKind::CloseParen)) {
  42. context.PushState(state, StateKind::TupleLiteralFinish);
  43. } else {
  44. context.PushState(state, StateKind::ParenExprFinish);
  45. context.PushState(StateKind::ExprAfterOpenParenFinish);
  46. context.PushState(StateKind::Expr);
  47. }
  48. }
  49. auto HandleExprAfterOpenParenFinish(Context& context) -> void {
  50. auto state = context.PopState();
  51. auto list_token_kind = context.ConsumeListToken(
  52. NodeKind::TupleLiteralComma, Lex::TokenKind::CloseParen, state.has_error);
  53. if (list_token_kind == Context::ListTokenKind::Close) {
  54. return;
  55. }
  56. // We found a comma, so switch parent state to tuple handling.
  57. auto finish_state = context.PopState();
  58. CARBON_CHECK(finish_state.kind == StateKind::ParenExprFinish,
  59. "Unexpected parent state, found: {0}", finish_state.kind);
  60. context.PushState(finish_state, StateKind::TupleLiteralFinish);
  61. // If the comma is not immediately followed by a close paren, push handlers
  62. // for the next tuple element.
  63. if (list_token_kind != Context::ListTokenKind::CommaClose) {
  64. context.PushState(StateKind::TupleLiteralElementFinish);
  65. context.PushState(StateKind::Expr);
  66. }
  67. }
  68. auto HandleTupleLiteralElementFinish(Context& context) -> void {
  69. auto state = context.PopState();
  70. if (state.has_error) {
  71. context.ReturnErrorOnState();
  72. }
  73. if (context.ConsumeListToken(NodeKind::TupleLiteralComma,
  74. Lex::TokenKind::CloseParen, state.has_error) ==
  75. Context::ListTokenKind::Comma) {
  76. context.PushState(StateKind::TupleLiteralElementFinish);
  77. context.PushState(StateKind::Expr);
  78. }
  79. }
  80. auto HandleParenExprFinish(Context& context) -> void {
  81. auto state = context.PopState();
  82. context.ReplacePlaceholderNode(state.subtree_start, NodeKind::ParenExprStart,
  83. state.token);
  84. FinishParenExpr(context, state);
  85. }
  86. auto HandleTupleLiteralFinish(Context& context) -> void {
  87. auto state = context.PopState();
  88. context.ReplacePlaceholderNode(state.subtree_start,
  89. NodeKind::TupleLiteralStart, state.token);
  90. context.AddNode(NodeKind::TupleLiteral, context.Consume(), state.has_error);
  91. }
  92. auto HandleCallExpr(Context& context) -> void {
  93. auto state = context.PopState();
  94. context.PushState(state, StateKind::CallExprFinish);
  95. context.AddNode(NodeKind::CallExprStart, context.Consume(), state.has_error);
  96. if (!context.PositionIs(Lex::TokenKind::CloseParen)) {
  97. context.PushState(StateKind::TupleLiteralElementFinish);
  98. context.PushState(StateKind::Expr);
  99. }
  100. }
  101. auto HandleCallExprFinish(Context& context) -> void {
  102. auto state = context.PopState();
  103. context.AddNode(NodeKind::CallExpr, context.Consume(), state.has_error);
  104. }
  105. } // namespace Carbon::Parse