handle_match.cpp 8.9 KB

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