handle_statement.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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, otherwise indicate the
  70. // keyword for the error.
  71. semi = context.SkipPastLikelyEnd(state.token);
  72. if (!semi) {
  73. semi = state.token;
  74. }
  75. }
  76. context.AddNode(node_kind, *semi, state.subtree_start, state.has_error);
  77. }
  78. auto HandleStatementBreakFinish(Context& context) -> void {
  79. HandleStatementKeywordFinish(context, NodeKind::BreakStatement);
  80. }
  81. auto HandleStatementContinueFinish(Context& context) -> void {
  82. HandleStatementKeywordFinish(context, NodeKind::ContinueStatement);
  83. }
  84. auto HandleStatementForHeader(Context& context) -> void {
  85. auto state = context.PopState();
  86. std::optional<Lex::TokenIndex> open_paren =
  87. context.ConsumeAndAddOpenParen(state.token, NodeKind::ForHeaderStart);
  88. if (open_paren) {
  89. state.token = *open_paren;
  90. }
  91. state.state = State::StatementForHeaderIn;
  92. if (context.PositionIs(Lex::TokenKind::Var)) {
  93. context.PushState(state);
  94. context.PushState(State::VarAsFor);
  95. context.AddLeafNode(NodeKind::VariableIntroducer, context.Consume());
  96. } else {
  97. CARBON_DIAGNOSTIC(ExpectedVariableDecl, Error,
  98. "Expected `var` declaration.");
  99. context.emitter().Emit(*context.position(), ExpectedVariableDecl);
  100. if (auto next_in = context.FindNextOf({Lex::TokenKind::In})) {
  101. context.SkipTo(*next_in);
  102. context.ConsumeAndDiscard();
  103. }
  104. state.has_error = true;
  105. context.PushState(state);
  106. }
  107. }
  108. auto HandleStatementForHeaderIn(Context& context) -> void {
  109. auto state = context.PopState();
  110. state.state = State::StatementForHeaderFinish;
  111. context.PushState(state);
  112. context.PushState(State::Expr);
  113. }
  114. auto HandleStatementForHeaderFinish(Context& context) -> void {
  115. auto state = context.PopState();
  116. context.ConsumeAndAddCloseSymbol(state.token, state, NodeKind::ForHeader);
  117. context.PushState(State::CodeBlock);
  118. }
  119. auto HandleStatementForFinish(Context& context) -> void {
  120. auto state = context.PopState();
  121. context.AddNode(NodeKind::ForStatement, state.token, state.subtree_start,
  122. state.has_error);
  123. }
  124. auto HandleStatementIf(Context& context) -> void {
  125. context.PopAndDiscardState();
  126. context.PushState(State::StatementIfConditionFinish);
  127. context.PushState(State::ParenConditionAsIf);
  128. context.ConsumeAndDiscard();
  129. }
  130. auto HandleStatementIfConditionFinish(Context& context) -> void {
  131. auto state = context.PopState();
  132. state.state = State::StatementIfThenBlockFinish;
  133. context.PushState(state);
  134. context.PushState(State::CodeBlock);
  135. }
  136. auto HandleStatementIfThenBlockFinish(Context& context) -> void {
  137. auto state = context.PopState();
  138. if (context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::Else,
  139. NodeKind::IfStatementElse)) {
  140. state.state = State::StatementIfElseBlockFinish;
  141. context.PushState(state);
  142. // `else if` is permitted as a special case.
  143. context.PushState(context.PositionIs(Lex::TokenKind::If)
  144. ? State::StatementIf
  145. : State::CodeBlock);
  146. } else {
  147. context.AddNode(NodeKind::IfStatement, state.token, state.subtree_start,
  148. state.has_error);
  149. }
  150. }
  151. auto HandleStatementIfElseBlockFinish(Context& context) -> void {
  152. auto state = context.PopState();
  153. context.AddNode(NodeKind::IfStatement, state.token, state.subtree_start,
  154. state.has_error);
  155. }
  156. auto HandleStatementReturn(Context& context) -> void {
  157. auto state = context.PopState();
  158. state.state = State::StatementReturnFinish;
  159. context.PushState(state);
  160. context.AddLeafNode(NodeKind::ReturnStatementStart, context.Consume());
  161. if (auto var_token = context.ConsumeIf(Lex::TokenKind::Var)) {
  162. // `return var;`
  163. context.AddLeafNode(NodeKind::ReturnVarModifier, *var_token);
  164. } else if (!context.PositionIs(Lex::TokenKind::Semi)) {
  165. // `return <expression>;`
  166. context.PushState(State::Expr);
  167. } else {
  168. // `return;`
  169. }
  170. }
  171. auto HandleStatementReturnFinish(Context& context) -> void {
  172. HandleStatementKeywordFinish(context, NodeKind::ReturnStatement);
  173. }
  174. auto HandleStatementScopeLoop(Context& context) -> void {
  175. // This maintains the current state until we're at the end of the scope.
  176. auto token_kind = context.PositionKind();
  177. if (token_kind == Lex::TokenKind::CloseCurlyBrace) {
  178. auto state = context.PopState();
  179. if (state.has_error) {
  180. context.ReturnErrorOnState();
  181. }
  182. } else {
  183. context.PushState(State::Statement);
  184. }
  185. }
  186. auto HandleStatementWhile(Context& context) -> void {
  187. context.PopAndDiscardState();
  188. context.PushState(State::StatementWhileConditionFinish);
  189. context.PushState(State::ParenConditionAsWhile);
  190. context.ConsumeAndDiscard();
  191. }
  192. auto HandleStatementWhileConditionFinish(Context& context) -> void {
  193. auto state = context.PopState();
  194. state.state = State::StatementWhileBlockFinish;
  195. context.PushState(state);
  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