handle_choice.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 HandleChoiceIntroducer(Context& context) -> void {
  8. auto state = context.PopState();
  9. context.PushState(state, State::ChoiceDefinitionStart);
  10. context.PushState(State::DeclNameAndParams, state.token);
  11. }
  12. auto HandleChoiceDefinitionStart(Context& context) -> void {
  13. auto state = context.PopState();
  14. if (!context.PositionIs(Lex::TokenKind::OpenCurlyBrace)) {
  15. if (!state.has_error) {
  16. CARBON_DIAGNOSTIC(ExpectedChoiceDefinition, Error,
  17. "Choice definition expected.");
  18. context.emitter().Emit(*context.position(), ExpectedChoiceDefinition);
  19. }
  20. context.AddNode(NodeKind::ChoiceDefinitionStart, *context.position(),
  21. /*has_error=*/true);
  22. context.AddNode(NodeKind::ChoiceDefinition, *context.position(),
  23. /*has_error=*/true);
  24. context.SkipPastLikelyEnd(*context.position());
  25. return;
  26. }
  27. context.AddNode(NodeKind::ChoiceDefinitionStart, context.Consume(),
  28. state.has_error);
  29. state.has_error = false;
  30. state.state = State::ChoiceDefinitionFinish;
  31. context.PushState(state);
  32. if (!context.PositionIs(Lex::TokenKind::CloseCurlyBrace)) {
  33. context.PushState(State::ChoiceAlternative);
  34. }
  35. }
  36. auto HandleChoiceAlternative(Context& context) -> void {
  37. auto state = context.PopState();
  38. context.PushState(State::ChoiceAlternativeFinish);
  39. if (!context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::Identifier,
  40. NodeKind::IdentifierName)) {
  41. if (!state.has_error) {
  42. CARBON_DIAGNOSTIC(ExpectedChoiceAlternativeName, Error,
  43. "Expected choice alternative name.");
  44. context.emitter().Emit(*context.position(),
  45. ExpectedChoiceAlternativeName);
  46. }
  47. context.SkipPastLikelyEnd(*context.position());
  48. context.ReturnErrorOnState();
  49. return;
  50. }
  51. if (context.PositionIs(Lex::TokenKind::OpenParen)) {
  52. context.PushState(State::PatternListAsTuple);
  53. }
  54. }
  55. auto HandleChoiceAlternativeFinish(Context& context) -> void {
  56. const auto state = context.PopState();
  57. if (state.has_error) {
  58. context.ReturnErrorOnState();
  59. if (!context.PositionIs(Lex::TokenKind::CloseCurlyBrace)) {
  60. context.PushState(State::ChoiceAlternative);
  61. }
  62. return;
  63. }
  64. if (context.ConsumeListToken(
  65. NodeKind::ChoiceAlternativeListComma, Lex::TokenKind::CloseCurlyBrace,
  66. state.has_error) == Context::ListTokenKind::Comma) {
  67. context.PushState(State::ChoiceAlternative);
  68. }
  69. }
  70. auto HandleChoiceDefinitionFinish(Context& context) -> void {
  71. const auto state = context.PopState();
  72. context.AddNode(NodeKind::ChoiceDefinition,
  73. context.ConsumeChecked(Lex::TokenKind::CloseCurlyBrace),
  74. state.has_error);
  75. }
  76. } // namespace Carbon::Parse