handle_paren_expr.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.subtree_start,
  19. state.has_error);
  20. }
  21. auto HandleOnlyParenExprFinish(Context& context) -> void {
  22. auto state = context.PopState();
  23. if (!context.PositionIs(Lex::TokenKind::CloseParen)) {
  24. if (!state.has_error) {
  25. CARBON_DIAGNOSTIC(UnexpectedTokenInCompoundMemberAccess, Error,
  26. "Expected `)`.");
  27. context.emitter().Emit(*context.position(),
  28. UnexpectedTokenInCompoundMemberAccess);
  29. state.has_error = true;
  30. }
  31. // Recover from the invalid token.
  32. context.SkipTo(context.tokens().GetMatchedClosingToken(state.token));
  33. }
  34. FinishParenExpr(context, state);
  35. }
  36. auto HandleParenExpr(Context& context) -> void {
  37. auto state = context.PopState();
  38. // Advance past the open paren. The placeholder will be replaced at the end
  39. // based on whether we determine this is a tuple or parenthesized expression.
  40. context.AddLeafNode(NodeKind::Placeholder,
  41. context.ConsumeChecked(Lex::TokenKind::OpenParen));
  42. if (context.PositionIs(Lex::TokenKind::CloseParen)) {
  43. context.PushState(state, State::TupleLiteralFinish);
  44. } else {
  45. context.PushState(state, State::ParenExprFinish);
  46. context.PushState(State::ExprAfterOpenParenFinish);
  47. context.PushState(State::Expr);
  48. }
  49. }
  50. auto HandleExprAfterOpenParenFinish(Context& context) -> void {
  51. auto state = context.PopState();
  52. auto list_token_kind = context.ConsumeListToken(
  53. NodeKind::TupleLiteralComma, Lex::TokenKind::CloseParen, state.has_error);
  54. if (list_token_kind == Context::ListTokenKind::Close) {
  55. return;
  56. }
  57. // We found a comma, so switch parent state to tuple handling.
  58. auto finish_state = context.PopState();
  59. CARBON_CHECK(finish_state.state == State::ParenExprFinish)
  60. << "Unexpected parent state, found: " << finish_state.state;
  61. context.PushState(finish_state, State::TupleLiteralFinish);
  62. // If the comma is not immediately followed by a close paren, push handlers
  63. // for the next tuple element.
  64. if (list_token_kind != Context::ListTokenKind::CommaClose) {
  65. context.PushState(state, State::TupleLiteralElementFinish);
  66. context.PushState(State::Expr);
  67. }
  68. }
  69. auto HandleTupleLiteralElementFinish(Context& context) -> void {
  70. auto state = context.PopState();
  71. if (context.ConsumeListToken(NodeKind::TupleLiteralComma,
  72. Lex::TokenKind::CloseParen, state.has_error) ==
  73. Context::ListTokenKind::Comma) {
  74. context.PushState(state);
  75. context.PushState(State::Expr);
  76. }
  77. }
  78. auto HandleParenExprFinish(Context& context) -> void {
  79. auto state = context.PopState();
  80. context.ReplacePlaceholderNode(state.subtree_start, NodeKind::ParenExprStart,
  81. state.token);
  82. FinishParenExpr(context, state);
  83. }
  84. auto HandleTupleLiteralFinish(Context& context) -> void {
  85. auto state = context.PopState();
  86. context.ReplacePlaceholderNode(state.subtree_start,
  87. NodeKind::TupleLiteralStart, state.token);
  88. context.AddNode(NodeKind::TupleLiteral, context.Consume(),
  89. state.subtree_start, state.has_error);
  90. }
  91. } // namespace Carbon::Parse