handle_match.cpp 8.8 KB

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