handle_statement.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. case Lex::TokenKind::Match: {
  52. context.PushState(State::MatchIntroducer);
  53. break;
  54. }
  55. default: {
  56. context.PushState(State::ExprStatementFinish);
  57. context.PushStateForExpr(PrecedenceGroup::ForExprStatement());
  58. break;
  59. }
  60. }
  61. }
  62. // Handles the `;` after a keyword statement.
  63. static auto HandleStatementKeywordFinish(Context& context, NodeKind node_kind)
  64. -> void {
  65. auto state = context.PopState();
  66. auto semi = context.ConsumeIf(Lex::TokenKind::Semi);
  67. if (!semi) {
  68. CARBON_DIAGNOSTIC(ExpectedStatementSemi, Error,
  69. "`{0}` statements must end with a `;`.", Lex::TokenKind);
  70. context.emitter().Emit(*context.position(), ExpectedStatementSemi,
  71. context.tokens().GetKind(state.token));
  72. state.has_error = true;
  73. // Recover to the next semicolon if possible.
  74. semi = context.SkipPastLikelyEnd(state.token);
  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. context.PushState(state, State::StatementForHeaderFinish);
  111. context.PushState(State::Expr);
  112. }
  113. auto HandleStatementForHeaderFinish(Context& context) -> void {
  114. auto state = context.PopState();
  115. context.ConsumeAndAddCloseSymbol(state.token, state, NodeKind::ForHeader);
  116. context.PushState(State::CodeBlock);
  117. }
  118. auto HandleStatementForFinish(Context& context) -> void {
  119. auto state = context.PopState();
  120. context.AddNode(NodeKind::ForStatement, state.token, state.subtree_start,
  121. state.has_error);
  122. }
  123. auto HandleStatementIf(Context& context) -> void {
  124. context.PopAndDiscardState();
  125. context.PushState(State::StatementIfConditionFinish);
  126. context.PushState(State::ParenConditionAsIf);
  127. context.ConsumeAndDiscard();
  128. }
  129. auto HandleStatementIfConditionFinish(Context& context) -> void {
  130. auto state = context.PopState();
  131. context.PushState(state, State::StatementIfThenBlockFinish);
  132. context.PushState(State::CodeBlock);
  133. }
  134. auto HandleStatementIfThenBlockFinish(Context& context) -> void {
  135. auto state = context.PopState();
  136. if (context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::Else,
  137. NodeKind::IfStatementElse)) {
  138. context.PushState(state, State::StatementIfElseBlockFinish);
  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. context.PushState(state, State::StatementReturnFinish);
  156. context.AddLeafNode(NodeKind::ReturnStatementStart, context.Consume());
  157. if (auto var_token = context.ConsumeIf(Lex::TokenKind::Var)) {
  158. // `return var;`
  159. context.AddLeafNode(NodeKind::ReturnVarModifier, *var_token);
  160. } else if (!context.PositionIs(Lex::TokenKind::Semi)) {
  161. // `return <expression>;`
  162. context.PushState(State::Expr);
  163. } else {
  164. // `return;`
  165. }
  166. }
  167. auto HandleStatementReturnFinish(Context& context) -> void {
  168. HandleStatementKeywordFinish(context, NodeKind::ReturnStatement);
  169. }
  170. auto HandleStatementScopeLoop(Context& context) -> void {
  171. // This maintains the current state until we're at the end of the scope.
  172. auto token_kind = context.PositionKind();
  173. if (token_kind == Lex::TokenKind::CloseCurlyBrace) {
  174. auto state = context.PopState();
  175. if (state.has_error) {
  176. context.ReturnErrorOnState();
  177. }
  178. } else {
  179. context.PushState(State::Statement);
  180. }
  181. }
  182. auto HandleStatementWhile(Context& context) -> void {
  183. context.PopAndDiscardState();
  184. context.PushState(State::StatementWhileConditionFinish);
  185. context.PushState(State::ParenConditionAsWhile);
  186. context.ConsumeAndDiscard();
  187. }
  188. auto HandleStatementWhileConditionFinish(Context& context) -> void {
  189. auto state = context.PopState();
  190. context.PushState(state, State::StatementWhileBlockFinish);
  191. context.PushState(State::CodeBlock);
  192. }
  193. auto HandleStatementWhileBlockFinish(Context& context) -> void {
  194. auto state = context.PopState();
  195. context.AddNode(NodeKind::WhileStatement, state.token, state.subtree_start,
  196. state.has_error);
  197. }
  198. } // namespace Carbon::Parse