handle_match.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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, StateKind 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(), /*has_error=*/true);
  18. context.AddNode(starter, *context.position(), /*has_error=*/true);
  19. context.AddNode(complete, *context.position(), /*has_error=*/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(), /*has_error=*/true);
  31. context.AddNode(complete, *context.position(), /*has_error=*/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(StateKind::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, StateKind::MatchConditionFinish);
  52. context.PushState(StateKind::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. /*has_error=*/true);
  67. context.AddNode(NodeKind::MatchStatement, *context.position(),
  68. /*has_error=*/true);
  69. context.SkipPastLikelyEnd(*context.position());
  70. return;
  71. }
  72. context.AddNode(NodeKind::MatchStatementStart, context.Consume(),
  73. 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, StateKind::MatchStatementFinish);
  81. context.PushState(StateKind::MatchCaseLoop);
  82. }
  83. auto HandleMatchCaseLoop(Context& context) -> void {
  84. context.PopAndDiscardState();
  85. if (context.PositionIs(Lex::TokenKind::Case)) {
  86. context.PushState(StateKind::MatchCaseLoop);
  87. context.PushState(StateKind::MatchCaseIntroducer);
  88. } else if (context.PositionIs(Lex::TokenKind::Default)) {
  89. context.PushState(StateKind::MatchCaseLoopAfterDefault);
  90. context.PushState(StateKind::MatchDefaultIntroducer);
  91. } else if (!context.PositionIs(Lex::TokenKind::CloseCurlyBrace)) {
  92. EmitUnexpectedTokenAndRecover(context);
  93. context.PushState(StateKind::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,
  101. "unreachable case; `{0}` occurs after the `default`",
  102. Lex::TokenKind);
  103. context.emitter().Emit(*context.position(), UnreachableMatchCase, kind);
  104. context.ReturnErrorOnState();
  105. context.PushState(StateKind::MatchCaseLoopAfterDefault);
  106. context.SkipPastLikelyEnd(*context.position());
  107. return;
  108. } else if (kind != Lex::TokenKind::CloseCurlyBrace) {
  109. EmitUnexpectedTokenAndRecover(context);
  110. context.PushState(StateKind::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, StateKind::MatchCaseAfterPattern);
  117. context.PushState(StateKind::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. /*has_error=*/true);
  124. context.AddNode(NodeKind::MatchCase, *context.position(),
  125. /*has_error=*/true);
  126. context.SkipPastLikelyEnd(*context.position());
  127. return;
  128. }
  129. context.PushState(state, StateKind::MatchCaseStart);
  130. if (context.PositionIs(Lex::TokenKind::If)) {
  131. context.PushState(StateKind::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(StateKind::Expr);
  137. } else {
  138. if (!state.has_error) {
  139. CARBON_DIAGNOSTIC(ExpectedMatchCaseGuardOpenParen, Error,
  140. "expected `(` after `if`");
  141. context.emitter().Emit(*context.position(),
  142. ExpectedMatchCaseGuardOpenParen);
  143. }
  144. context.AddLeafNode(NodeKind::MatchCaseGuardStart, *context.position(),
  145. /*has_error=*/true);
  146. context.AddInvalidParse(*context.position());
  147. state = context.PopState();
  148. context.AddNode(NodeKind::MatchCaseGuard, *context.position(),
  149. /*has_error=*/true);
  150. state = context.PopState();
  151. context.AddNode(NodeKind::MatchCaseStart, *context.position(),
  152. /*has_error=*/true);
  153. context.AddNode(NodeKind::MatchCase, *context.position(),
  154. /*has_error=*/true);
  155. context.SkipPastLikelyEnd(*context.position());
  156. return;
  157. }
  158. }
  159. }
  160. auto HandleMatchCaseGuardFinish(Context& context) -> void {
  161. auto state = context.PopState();
  162. auto close_paren = context.ConsumeIf(Lex::TokenKind::CloseParen);
  163. if (close_paren) {
  164. context.AddNode(NodeKind::MatchCaseGuard, *close_paren, state.has_error);
  165. } else {
  166. if (!state.has_error) {
  167. CARBON_DIAGNOSTIC(ExpectedMatchCaseGuardCloseParen, Error,
  168. "expected `)`");
  169. context.emitter().Emit(*context.position(),
  170. ExpectedMatchCaseGuardCloseParen);
  171. }
  172. context.AddNode(NodeKind::MatchCaseGuard, *context.position(),
  173. /*has_error=*/true);
  174. context.ReturnErrorOnState();
  175. context.SkipPastLikelyEnd(*context.position());
  176. return;
  177. }
  178. }
  179. auto HandleMatchCaseStart(Context& context) -> void {
  180. HandleStatementsBlockStart(context, StateKind::MatchCaseFinish,
  181. NodeKind::MatchCaseEqualGreater,
  182. NodeKind::MatchCaseStart, NodeKind::MatchCase);
  183. }
  184. auto HandleMatchCaseFinish(Context& context) -> void {
  185. auto state = context.PopState();
  186. context.AddNode(NodeKind::MatchCase,
  187. context.ConsumeChecked(Lex::TokenKind::CloseCurlyBrace),
  188. state.has_error);
  189. }
  190. auto HandleMatchDefaultIntroducer(Context& context) -> void {
  191. context.AddLeafNode(NodeKind::MatchDefaultIntroducer, context.Consume());
  192. HandleStatementsBlockStart(context, StateKind::MatchDefaultFinish,
  193. NodeKind::MatchDefaultEqualGreater,
  194. NodeKind::MatchDefaultStart,
  195. NodeKind::MatchDefault);
  196. }
  197. auto HandleMatchDefaultFinish(Context& context) -> void {
  198. auto state = context.PopState();
  199. context.AddNode(NodeKind::MatchDefault,
  200. context.ConsumeChecked(Lex::TokenKind::CloseCurlyBrace),
  201. state.has_error);
  202. }
  203. auto HandleMatchStatementFinish(Context& context) -> void {
  204. auto state = context.PopState();
  205. context.AddNode(NodeKind::MatchStatement,
  206. context.ConsumeChecked(Lex::TokenKind::CloseCurlyBrace),
  207. state.has_error);
  208. }
  209. } // namespace Carbon::Parse