parser_handle_statement.cpp 6.7 KB

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