handle_statement.cpp 6.9 KB

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