parser_handle_paren_condition.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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/parser/parser_context.h"
  5. namespace Carbon {
  6. // Handles ParenConditionAs(If|While).
  7. static auto ParserHandleParenCondition(ParserContext& context,
  8. ParseNodeKind start_kind,
  9. ParserState finish_state) -> void {
  10. auto state = context.PopState();
  11. context.ConsumeAndAddOpenParen(state.token, start_kind);
  12. state.state = finish_state;
  13. context.PushState(state);
  14. context.PushState(ParserState::Expression);
  15. }
  16. auto ParserHandleParenConditionAsIf(ParserContext& context) -> void {
  17. ParserHandleParenCondition(context, ParseNodeKind::IfConditionStart,
  18. ParserState::ParenConditionFinishAsIf);
  19. }
  20. auto ParserHandleParenConditionAsWhile(ParserContext& context) -> void {
  21. ParserHandleParenCondition(context, ParseNodeKind::WhileConditionStart,
  22. ParserState::ParenConditionFinishAsWhile);
  23. }
  24. auto ParserHandleParenConditionFinishAsIf(ParserContext& context) -> void {
  25. auto state = context.PopState();
  26. context.ConsumeAndAddCloseSymbol(
  27. *(TokenizedBuffer::TokenIterator(state.token) + 1), state,
  28. ParseNodeKind::IfCondition);
  29. }
  30. auto ParserHandleParenConditionFinishAsWhile(ParserContext& context) -> void {
  31. auto state = context.PopState();
  32. context.ConsumeAndAddCloseSymbol(
  33. *(TokenizedBuffer::TokenIterator(state.token) + 1), state,
  34. ParseNodeKind::WhileCondition);
  35. }
  36. } // namespace Carbon