handle_paren_expr.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, State::OnlyParenExprFinish);
  14. context.PushState(State::Expr);
  15. }
  16. static auto FinishParenExpr(Context& context,
  17. const Context::StateStackEntry& state) -> 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, State::TupleLiteralFinish);
  43. } else {
  44. context.PushState(state, State::ParenExprFinish);
  45. context.PushState(State::ExprAfterOpenParenFinish);
  46. context.PushState(State::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.state == State::ParenExprFinish)
  59. << "Unexpected parent state, found: " << finish_state.state;
  60. context.PushState(finish_state, State::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(state, State::TupleLiteralElementFinish);
  65. context.PushState(State::Expr);
  66. }
  67. }
  68. auto HandleTupleLiteralElementFinish(Context& context) -> void {
  69. auto state = context.PopState();
  70. if (context.ConsumeListToken(NodeKind::TupleLiteralComma,
  71. Lex::TokenKind::CloseParen, state.has_error) ==
  72. Context::ListTokenKind::Comma) {
  73. context.PushState(state);
  74. context.PushState(State::Expr);
  75. }
  76. }
  77. auto HandleParenExprFinish(Context& context) -> void {
  78. auto state = context.PopState();
  79. context.ReplacePlaceholderNode(state.subtree_start, NodeKind::ParenExprStart,
  80. state.token);
  81. FinishParenExpr(context, state);
  82. }
  83. auto HandleTupleLiteralFinish(Context& context) -> void {
  84. auto state = context.PopState();
  85. context.ReplacePlaceholderNode(state.subtree_start,
  86. NodeKind::TupleLiteralStart, state.token);
  87. context.AddNode(NodeKind::TupleLiteral, context.Consume(), state.has_error);
  88. }
  89. } // namespace Carbon::Parse