handle_statement.cpp 7.2 KB

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