handle_match.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. static auto HandleStatementsBlockStart(Context& context, State finish,
  8. NodeKind equal_greater, NodeKind starter,
  9. NodeKind complete) -> void {
  10. auto state = context.PopState();
  11. if (!context.PositionIs(Lex::TokenKind::EqualGreater)) {
  12. if (!state.has_error) {
  13. CARBON_DIAGNOSTIC(ExpectedMatchCaseArrow, Error,
  14. "Expected `=>` introducing statement block.");
  15. context.emitter().Emit(*context.position(), ExpectedMatchCaseArrow);
  16. }
  17. context.AddLeafNode(equal_greater, *context.position(), true);
  18. context.AddNode(starter, *context.position(), true);
  19. context.AddNode(complete, *context.position(), true);
  20. context.SkipPastLikelyEnd(*context.position());
  21. return;
  22. }
  23. context.AddLeafNode(equal_greater, context.Consume());
  24. if (!context.PositionIs(Lex::TokenKind::OpenCurlyBrace)) {
  25. if (!state.has_error) {
  26. CARBON_DIAGNOSTIC(ExpectedMatchCaseBlock, Error,
  27. "Expected `{{` after `=>`.");
  28. context.emitter().Emit(*context.position(), ExpectedMatchCaseBlock);
  29. }
  30. context.AddNode(starter, *context.position(), true);
  31. context.AddNode(complete, *context.position(), true);
  32. context.SkipPastLikelyEnd(*context.position());
  33. return;
  34. }
  35. context.AddNode(starter, context.Consume(), state.has_error);
  36. context.PushState(state, finish);
  37. context.PushState(State::StatementScopeLoop);
  38. }
  39. static auto EmitUnexpectedTokenAndRecover(Context& context) -> void {
  40. CARBON_DIAGNOSTIC(UnexpectedTokenInMatchCasesBlock, Error,
  41. "Unexpected `{0}`; expected `case`, `default` or `}`.",
  42. Lex::TokenKind);
  43. context.emitter().Emit(*context.position(), UnexpectedTokenInMatchCasesBlock,
  44. context.PositionKind());
  45. context.ReturnErrorOnState();
  46. context.SkipPastLikelyEnd(*context.position());
  47. }
  48. auto HandleMatchIntroducer(Context& context) -> void {
  49. auto state = context.PopState();
  50. context.AddLeafNode(NodeKind::Placeholder, *context.position());
  51. context.PushState(state, State::MatchConditionFinish);
  52. context.PushState(State::ParenConditionAsMatch);
  53. context.ConsumeAndDiscard();
  54. }
  55. auto HandleMatchConditionFinish(Context& context) -> void {
  56. auto state = context.PopState();
  57. context.ReplacePlaceholderNode(state.subtree_start, NodeKind::MatchIntroducer,
  58. state.token);
  59. if (!context.PositionIs(Lex::TokenKind::OpenCurlyBrace)) {
  60. if (!state.has_error) {
  61. CARBON_DIAGNOSTIC(ExpectedMatchCasesBlock, Error,
  62. "Expected `{{` starting block with cases.");
  63. context.emitter().Emit(*context.position(), ExpectedMatchCasesBlock);
  64. }
  65. context.AddNode(NodeKind::MatchStatementStart, *context.position(), true);
  66. context.AddNode(NodeKind::MatchStatement, *context.position(), true);
  67. context.SkipPastLikelyEnd(*context.position());
  68. return;
  69. }
  70. context.AddNode(NodeKind::MatchStatementStart, context.Consume(),
  71. state.has_error);
  72. state.has_error = false;
  73. if (context.PositionIs(Lex::TokenKind::CloseCurlyBrace)) {
  74. CARBON_DIAGNOSTIC(ExpectedMatchCases, Error, "Expected cases.");
  75. context.emitter().Emit(*context.position(), ExpectedMatchCases);
  76. state.has_error = true;
  77. }
  78. context.PushState(state, State::MatchStatementFinish);
  79. context.PushState(State::MatchCaseLoop);
  80. }
  81. auto HandleMatchCaseLoop(Context& context) -> void {
  82. context.PopAndDiscardState();
  83. if (context.PositionIs(Lex::TokenKind::Case)) {
  84. context.PushState(State::MatchCaseLoop);
  85. context.PushState(State::MatchCaseIntroducer);
  86. } else if (context.PositionIs(Lex::TokenKind::Default)) {
  87. context.PushState(State::MatchCaseLoopAfterDefault);
  88. context.PushState(State::MatchDefaultIntroducer);
  89. } else if (!context.PositionIs(Lex::TokenKind::CloseCurlyBrace)) {
  90. EmitUnexpectedTokenAndRecover(context);
  91. context.PushState(State::MatchCaseLoop);
  92. }
  93. }
  94. auto HandleMatchCaseLoopAfterDefault(Context& context) -> void {
  95. context.PopAndDiscardState();
  96. Lex::TokenKind kind = context.PositionKind();
  97. if (kind == Lex::TokenKind::Case or kind == Lex::TokenKind::Default) {
  98. CARBON_DIAGNOSTIC(UnreachableMatchCase, Error, "Unreachable case; `{0}{1}",
  99. Lex::TokenKind, std::string);
  100. context.emitter().Emit(*context.position(), UnreachableMatchCase, kind,
  101. "` occurs after the `default`");
  102. context.ReturnErrorOnState();
  103. context.PushState(State::MatchCaseLoopAfterDefault);
  104. context.SkipPastLikelyEnd(*context.position());
  105. return;
  106. } else if (kind != Lex::TokenKind::CloseCurlyBrace) {
  107. EmitUnexpectedTokenAndRecover(context);
  108. context.PushState(State::MatchCaseLoopAfterDefault);
  109. }
  110. }
  111. auto HandleMatchCaseIntroducer(Context& context) -> void {
  112. auto state = context.PopState();
  113. context.AddLeafNode(NodeKind::MatchCaseIntroducer, context.Consume());
  114. context.PushState(state, State::MatchCaseAfterPattern);
  115. context.PushState(State::Pattern);
  116. }
  117. auto HandleMatchCaseAfterPattern(Context& context) -> void {
  118. auto state = context.PopState();
  119. if (state.has_error) {
  120. context.AddNode(NodeKind::MatchCaseStart, *context.position(), true);
  121. context.AddNode(NodeKind::MatchCase, *context.position(), true);
  122. context.SkipPastLikelyEnd(*context.position());
  123. return;
  124. }
  125. context.PushState(state, State::MatchCaseStart);
  126. if (context.PositionIs(Lex::TokenKind::If)) {
  127. context.PushState(State::MatchCaseGuardFinish);
  128. context.AddLeafNode(NodeKind::MatchCaseGuardIntroducer, context.Consume());
  129. auto open_paren = context.ConsumeIf(Lex::TokenKind::OpenParen);
  130. if (open_paren) {
  131. context.AddLeafNode(NodeKind::MatchCaseGuardStart, *open_paren);
  132. context.PushState(State::Expr);
  133. } else {
  134. context.AddLeafNode(NodeKind::MatchCaseGuardStart, *context.position(),
  135. true);
  136. context.AddLeafNode(NodeKind::InvalidParse, *context.position(), true);
  137. state = context.PopState();
  138. context.AddNode(NodeKind::MatchCaseGuard, *context.position(), true);
  139. state = context.PopState();
  140. context.AddNode(NodeKind::MatchCaseStart, *context.position(), true);
  141. context.AddNode(NodeKind::MatchCase, *context.position(), true);
  142. context.SkipPastLikelyEnd(*context.position());
  143. return;
  144. }
  145. }
  146. }
  147. auto HandleMatchCaseGuardFinish(Context& context) -> void {
  148. auto state = context.PopState();
  149. auto close_paren = context.ConsumeIf(Lex::TokenKind::CloseParen);
  150. if (close_paren) {
  151. context.AddNode(NodeKind::MatchCaseGuard, *close_paren, state.has_error);
  152. } else {
  153. context.AddNode(NodeKind::MatchCaseGuard, *context.position(), true);
  154. context.ReturnErrorOnState();
  155. context.SkipPastLikelyEnd(*context.position());
  156. return;
  157. }
  158. }
  159. auto HandleMatchCaseStart(Context& context) -> void {
  160. HandleStatementsBlockStart(context, State::MatchCaseFinish,
  161. NodeKind::MatchCaseEqualGreater,
  162. NodeKind::MatchCaseStart, NodeKind::MatchCase);
  163. }
  164. auto HandleMatchCaseFinish(Context& context) -> void {
  165. auto state = context.PopState();
  166. context.AddNode(NodeKind::MatchCase,
  167. context.ConsumeChecked(Lex::TokenKind::CloseCurlyBrace),
  168. state.has_error);
  169. }
  170. auto HandleMatchDefaultIntroducer(Context& context) -> void {
  171. context.AddLeafNode(NodeKind::MatchDefaultIntroducer, context.Consume());
  172. HandleStatementsBlockStart(
  173. context, State::MatchDefaultFinish, NodeKind::MatchDefaultEqualGreater,
  174. NodeKind::MatchDefaultStart, NodeKind::MatchDefault);
  175. }
  176. auto HandleMatchDefaultFinish(Context& context) -> void {
  177. auto state = context.PopState();
  178. context.AddNode(NodeKind::MatchDefault,
  179. context.ConsumeChecked(Lex::TokenKind::CloseCurlyBrace),
  180. state.has_error);
  181. }
  182. auto HandleMatchStatementFinish(Context& context) -> void {
  183. auto state = context.PopState();
  184. context.AddNode(NodeKind::MatchStatement,
  185. context.ConsumeChecked(Lex::TokenKind::CloseCurlyBrace),
  186. state.has_error);
  187. }
  188. } // namespace Carbon::Parse