handle_paren_condition.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 ParenConditionAs(If|While|Match).
  8. static auto HandleParenCondition(Context& context, NodeKind start_kind,
  9. State finish_state) -> void {
  10. auto state = context.PopState();
  11. std::optional<Lex::TokenIndex> open_paren =
  12. context.ConsumeAndAddOpenParen(state.token, start_kind);
  13. if (open_paren) {
  14. state.token = *open_paren;
  15. }
  16. context.PushState(state, finish_state);
  17. if (!open_paren && context.PositionIs(Lex::TokenKind::OpenCurlyBrace)) {
  18. // For an open curly, assume the condition was completely omitted.
  19. // Expression parsing would treat the { as a struct, but instead assume it's
  20. // a code block and just emit an invalid parse.
  21. context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
  22. /*has_error=*/true);
  23. } else {
  24. context.PushState(State::Expr);
  25. }
  26. }
  27. auto HandleParenConditionAsIf(Context& context) -> void {
  28. HandleParenCondition(context, NodeKind::IfConditionStart,
  29. State::ParenConditionFinishAsIf);
  30. }
  31. auto HandleParenConditionAsWhile(Context& context) -> void {
  32. HandleParenCondition(context, NodeKind::WhileConditionStart,
  33. State::ParenConditionFinishAsWhile);
  34. }
  35. auto HandleParenConditionAsMatch(Context& context) -> void {
  36. HandleParenCondition(context, NodeKind::MatchConditionStart,
  37. State::ParenConditionFinishAsMatch);
  38. }
  39. auto HandleParenConditionFinishAsIf(Context& context) -> void {
  40. auto state = context.PopState();
  41. context.ConsumeAndAddCloseSymbol(state.token, state, NodeKind::IfCondition);
  42. }
  43. auto HandleParenConditionFinishAsWhile(Context& context) -> void {
  44. auto state = context.PopState();
  45. context.ConsumeAndAddCloseSymbol(state.token, state,
  46. NodeKind::WhileCondition);
  47. }
  48. auto HandleParenConditionFinishAsMatch(Context& context) -> void {
  49. auto state = context.PopState();
  50. context.ConsumeAndAddCloseSymbol(state.token, state,
  51. NodeKind::MatchCondition);
  52. }
  53. } // namespace Carbon::Parse