handle_statement.cpp 7.2 KB

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