handle_statement.cpp 7.3 KB

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