handle_statement.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. auto HandleStatement(Context& context) -> void {
  7. context.PopAndDiscardState();
  8. switch (context.PositionKind()) {
  9. case Lex::TokenKind::Alias: {
  10. context.PushState(State::Alias);
  11. context.AddLeafNode(NodeKind::AliasIntroducer, context.Consume());
  12. break;
  13. }
  14. case Lex::TokenKind::Break: {
  15. context.PushState(State::StatementBreakFinish);
  16. context.AddLeafNode(NodeKind::BreakStatementStart, context.Consume());
  17. break;
  18. }
  19. case Lex::TokenKind::Continue: {
  20. context.PushState(State::StatementContinueFinish);
  21. context.AddLeafNode(NodeKind::ContinueStatementStart, context.Consume());
  22. break;
  23. }
  24. case Lex::TokenKind::For: {
  25. context.PushState(State::StatementForFinish);
  26. context.PushState(State::StatementForHeader);
  27. context.ConsumeAndDiscard();
  28. break;
  29. }
  30. case Lex::TokenKind::If: {
  31. context.PushState(State::StatementIf);
  32. break;
  33. }
  34. case Lex::TokenKind::Let: {
  35. context.PushState(State::Let);
  36. context.AddLeafNode(NodeKind::LetIntroducer, context.Consume());
  37. break;
  38. }
  39. case Lex::TokenKind::Return: {
  40. context.PushState(State::StatementReturn);
  41. break;
  42. }
  43. case Lex::TokenKind::Returned: {
  44. context.PushState(State::VarAsReturned);
  45. break;
  46. }
  47. case Lex::TokenKind::Var: {
  48. context.PushState(State::VarAsDecl);
  49. context.AddLeafNode(NodeKind::VariableIntroducer, context.Consume());
  50. break;
  51. }
  52. case Lex::TokenKind::While: {
  53. context.PushState(State::StatementWhile);
  54. break;
  55. }
  56. case Lex::TokenKind::Match: {
  57. context.PushState(State::MatchIntroducer);
  58. break;
  59. }
  60. default: {
  61. context.PushState(State::ExprStatementFinish);
  62. context.PushStateForExpr(PrecedenceGroup::ForExprStatement());
  63. break;
  64. }
  65. }
  66. }
  67. // Handles the `;` after a keyword statement.
  68. static auto HandleStatementKeywordFinish(Context& context, NodeKind node_kind)
  69. -> void {
  70. auto state = context.PopState();
  71. auto semi = context.ConsumeIf(Lex::TokenKind::Semi);
  72. if (!semi) {
  73. CARBON_DIAGNOSTIC(ExpectedStatementSemi, Error,
  74. "`{0}` statements must end with a `;`.", Lex::TokenKind);
  75. context.emitter().Emit(*context.position(), ExpectedStatementSemi,
  76. context.tokens().GetKind(state.token));
  77. state.has_error = true;
  78. // Recover to the next semicolon if possible.
  79. semi = context.SkipPastLikelyEnd(state.token);
  80. }
  81. context.AddNode(node_kind, *semi, state.subtree_start, state.has_error);
  82. }
  83. auto HandleStatementBreakFinish(Context& context) -> void {
  84. HandleStatementKeywordFinish(context, NodeKind::BreakStatement);
  85. }
  86. auto HandleStatementContinueFinish(Context& context) -> void {
  87. HandleStatementKeywordFinish(context, NodeKind::ContinueStatement);
  88. }
  89. auto HandleStatementForHeader(Context& context) -> void {
  90. auto state = context.PopState();
  91. std::optional<Lex::TokenIndex> open_paren =
  92. context.ConsumeAndAddOpenParen(state.token, NodeKind::ForHeaderStart);
  93. if (open_paren) {
  94. state.token = *open_paren;
  95. }
  96. state.state = State::StatementForHeaderIn;
  97. if (context.PositionIs(Lex::TokenKind::Var)) {
  98. context.PushState(state);
  99. context.PushState(State::VarAsFor);
  100. context.AddLeafNode(NodeKind::VariableIntroducer, context.Consume());
  101. } else {
  102. CARBON_DIAGNOSTIC(ExpectedVariableDecl, Error,
  103. "Expected `var` declaration.");
  104. context.emitter().Emit(*context.position(), ExpectedVariableDecl);
  105. if (auto next_in = context.FindNextOf({Lex::TokenKind::In})) {
  106. context.SkipTo(*next_in);
  107. context.ConsumeAndDiscard();
  108. }
  109. state.has_error = true;
  110. context.PushState(state);
  111. }
  112. }
  113. auto HandleStatementForHeaderIn(Context& context) -> void {
  114. auto state = context.PopState();
  115. context.PushState(state, State::StatementForHeaderFinish);
  116. context.PushState(State::Expr);
  117. }
  118. auto HandleStatementForHeaderFinish(Context& context) -> void {
  119. auto state = context.PopState();
  120. context.ConsumeAndAddCloseSymbol(state.token, state, NodeKind::ForHeader);
  121. context.PushState(State::CodeBlock);
  122. }
  123. auto HandleStatementForFinish(Context& context) -> void {
  124. auto state = context.PopState();
  125. context.AddNode(NodeKind::ForStatement, state.token, state.subtree_start,
  126. state.has_error);
  127. }
  128. auto HandleStatementIf(Context& context) -> void {
  129. context.PopAndDiscardState();
  130. context.PushState(State::StatementIfConditionFinish);
  131. context.PushState(State::ParenConditionAsIf);
  132. context.ConsumeAndDiscard();
  133. }
  134. auto HandleStatementIfConditionFinish(Context& context) -> void {
  135. auto state = context.PopState();
  136. context.PushState(state, State::StatementIfThenBlockFinish);
  137. context.PushState(State::CodeBlock);
  138. }
  139. auto HandleStatementIfThenBlockFinish(Context& context) -> void {
  140. auto state = context.PopState();
  141. if (context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::Else,
  142. NodeKind::IfStatementElse)) {
  143. context.PushState(state, State::StatementIfElseBlockFinish);
  144. // `else if` is permitted as a special case.
  145. context.PushState(context.PositionIs(Lex::TokenKind::If)
  146. ? State::StatementIf
  147. : State::CodeBlock);
  148. } else {
  149. context.AddNode(NodeKind::IfStatement, state.token, state.subtree_start,
  150. state.has_error);
  151. }
  152. }
  153. auto HandleStatementIfElseBlockFinish(Context& context) -> void {
  154. auto state = context.PopState();
  155. context.AddNode(NodeKind::IfStatement, state.token, state.subtree_start,
  156. state.has_error);
  157. }
  158. auto HandleStatementReturn(Context& context) -> void {
  159. auto state = context.PopState();
  160. context.PushState(state, State::StatementReturnFinish);
  161. context.AddLeafNode(NodeKind::ReturnStatementStart, context.Consume());
  162. if (auto var_token = context.ConsumeIf(Lex::TokenKind::Var)) {
  163. // `return var;`
  164. context.AddLeafNode(NodeKind::ReturnVarModifier, *var_token);
  165. } else if (!context.PositionIs(Lex::TokenKind::Semi)) {
  166. // `return <expression>;`
  167. context.PushState(State::Expr);
  168. } else {
  169. // `return;`
  170. }
  171. }
  172. auto HandleStatementReturnFinish(Context& context) -> void {
  173. HandleStatementKeywordFinish(context, NodeKind::ReturnStatement);
  174. }
  175. auto HandleStatementScopeLoop(Context& context) -> void {
  176. // This maintains the current state until we're at the end of the scope.
  177. auto token_kind = context.PositionKind();
  178. if (token_kind == Lex::TokenKind::CloseCurlyBrace) {
  179. auto state = context.PopState();
  180. if (state.has_error) {
  181. context.ReturnErrorOnState();
  182. }
  183. } else {
  184. context.PushState(State::Statement);
  185. }
  186. }
  187. auto HandleStatementWhile(Context& context) -> void {
  188. context.PopAndDiscardState();
  189. context.PushState(State::StatementWhileConditionFinish);
  190. context.PushState(State::ParenConditionAsWhile);
  191. context.ConsumeAndDiscard();
  192. }
  193. auto HandleStatementWhileConditionFinish(Context& context) -> void {
  194. auto state = context.PopState();
  195. context.PushState(state, State::StatementWhileBlockFinish);
  196. context.PushState(State::CodeBlock);
  197. }
  198. auto HandleStatementWhileBlockFinish(Context& context) -> void {
  199. auto state = context.PopState();
  200. context.AddNode(NodeKind::WhileStatement, state.token, state.subtree_start,
  201. state.has_error);
  202. }
  203. } // namespace Carbon::Parse