handle_choice.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. auto token = context.ConsumeIf(Lex::TokenKind::Identifier);
  40. if (!token) {
  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.AddLeafNode(NodeKind::IdentifierNameBeforeParams, *token);
  53. context.PushState(State::PatternListAsTuple);
  54. } else {
  55. context.AddLeafNode(NodeKind::IdentifierNameNotBeforeParams, *token);
  56. }
  57. }
  58. auto HandleChoiceAlternativeFinish(Context& context) -> void {
  59. const auto state = context.PopState();
  60. if (state.has_error) {
  61. context.ReturnErrorOnState();
  62. if (!context.PositionIs(Lex::TokenKind::CloseCurlyBrace)) {
  63. context.PushState(State::ChoiceAlternative);
  64. }
  65. return;
  66. }
  67. if (context.ConsumeListToken(
  68. NodeKind::ChoiceAlternativeListComma, Lex::TokenKind::CloseCurlyBrace,
  69. state.has_error) == Context::ListTokenKind::Comma) {
  70. context.PushState(State::ChoiceAlternative);
  71. }
  72. }
  73. auto HandleChoiceDefinitionFinish(Context& context) -> void {
  74. const auto state = context.PopState();
  75. context.AddNode(NodeKind::ChoiceDefinition,
  76. context.ConsumeChecked(Lex::TokenKind::CloseCurlyBrace),
  77. state.has_error);
  78. }
  79. } // namespace Carbon::Parse