handle_statement.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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::Break: {
  11. context.PushState(StateKind::StatementBreakFinish);
  12. context.AddLeafNode(NodeKind::BreakStatementStart, context.Consume());
  13. break;
  14. }
  15. case Lex::TokenKind::Continue: {
  16. context.PushState(StateKind::StatementContinueFinish);
  17. context.AddLeafNode(NodeKind::ContinueStatementStart, context.Consume());
  18. break;
  19. }
  20. case Lex::TokenKind::For: {
  21. context.PushState(StateKind::StatementForFinish);
  22. context.PushState(StateKind::StatementForHeader);
  23. context.ConsumeAndDiscard();
  24. break;
  25. }
  26. case Lex::TokenKind::If: {
  27. context.PushState(StateKind::StatementIf);
  28. break;
  29. }
  30. case Lex::TokenKind::Return: {
  31. context.PushState(StateKind::StatementReturn);
  32. break;
  33. }
  34. case Lex::TokenKind::Returned: {
  35. // TODO: Consider handling this as a modifier.
  36. context.PushState(StateKind::VarAsReturned);
  37. break;
  38. }
  39. case Lex::TokenKind::While: {
  40. context.PushState(StateKind::StatementWhile);
  41. break;
  42. }
  43. case Lex::TokenKind::Match: {
  44. context.PushState(StateKind::MatchIntroducer);
  45. break;
  46. }
  47. #define CARBON_PARSE_NODE_KIND(Name)
  48. #define CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Name) case Lex::TokenKind::Name:
  49. #include "toolchain/parse/node_kind.def"
  50. case Lex::TokenKind::Adapt:
  51. case Lex::TokenKind::Alias:
  52. case Lex::TokenKind::Choice:
  53. case Lex::TokenKind::Class:
  54. case Lex::TokenKind::Constraint:
  55. case Lex::TokenKind::Fn:
  56. case Lex::TokenKind::Import:
  57. case Lex::TokenKind::Interface:
  58. case Lex::TokenKind::Let:
  59. case Lex::TokenKind::Library:
  60. case Lex::TokenKind::Namespace:
  61. // We intentionally don't handle Package here, because `package.` can be
  62. // used at the start of an expression, and it's not worth disambiguating it.
  63. case Lex::TokenKind::Var: {
  64. context.PushState(StateKind::Decl);
  65. break;
  66. }
  67. default: {
  68. context.PushState(StateKind::ExprStatementFinish);
  69. context.PushStateForExpr(PrecedenceGroup::ForExprStatement());
  70. break;
  71. }
  72. }
  73. }
  74. // Handles the `;` after a keyword statement.
  75. static auto HandleStatementKeywordFinish(Context& context, NodeKind node_kind)
  76. -> void {
  77. auto state = context.PopState();
  78. auto semi = context.ConsumeIf(Lex::TokenKind::Semi);
  79. if (!semi) {
  80. CARBON_DIAGNOSTIC(ExpectedStatementSemi, Error,
  81. "`{0}` statements must end with a `;`", Lex::TokenKind);
  82. context.emitter().Emit(*context.position(), ExpectedStatementSemi,
  83. context.tokens().GetKind(state.token));
  84. state.has_error = true;
  85. // Recover to the next semicolon if possible.
  86. semi = context.SkipPastLikelyEnd(state.token);
  87. }
  88. context.AddNode(node_kind, *semi, state.has_error);
  89. }
  90. auto HandleStatementBreakFinish(Context& context) -> void {
  91. HandleStatementKeywordFinish(context, NodeKind::BreakStatement);
  92. }
  93. auto HandleStatementContinueFinish(Context& context) -> void {
  94. HandleStatementKeywordFinish(context, NodeKind::ContinueStatement);
  95. }
  96. auto HandleStatementForHeader(Context& context) -> void {
  97. auto state = context.PopState();
  98. std::optional<Lex::TokenIndex> open_paren =
  99. context.ConsumeAndAddOpenParen(state.token, NodeKind::ForHeaderStart);
  100. if (open_paren) {
  101. state.token = *open_paren;
  102. }
  103. state.kind = StateKind::StatementForHeaderIn;
  104. if (context.PositionIs(Lex::TokenKind::Var)) {
  105. context.PushState(state);
  106. context.PushState(StateKind::VarAsFor);
  107. context.AddLeafNode(NodeKind::VariableIntroducer, context.Consume());
  108. } else {
  109. CARBON_DIAGNOSTIC(ExpectedVariableDecl, Error,
  110. "expected `var` declaration");
  111. context.emitter().Emit(*context.position(), ExpectedVariableDecl);
  112. if (auto next_in = context.FindNextOf({Lex::TokenKind::In})) {
  113. context.SkipTo(*next_in);
  114. context.ConsumeAndDiscard();
  115. }
  116. state.has_error = true;
  117. context.PushState(state);
  118. }
  119. }
  120. auto HandleStatementForHeaderIn(Context& context) -> void {
  121. auto state = context.PopState();
  122. context.PushState(state, StateKind::StatementForHeaderFinish);
  123. context.PushState(StateKind::Expr);
  124. }
  125. auto HandleStatementForHeaderFinish(Context& context) -> void {
  126. auto state = context.PopState();
  127. context.ConsumeAndAddCloseSymbol(state.token, state, NodeKind::ForHeader);
  128. context.PushState(StateKind::CodeBlock);
  129. }
  130. auto HandleStatementForFinish(Context& context) -> void {
  131. auto state = context.PopState();
  132. context.AddNode(NodeKind::ForStatement, state.token, state.has_error);
  133. }
  134. auto HandleStatementIf(Context& context) -> void {
  135. context.PopAndDiscardState();
  136. context.PushState(StateKind::StatementIfConditionFinish);
  137. context.PushState(StateKind::ParenConditionAsIf);
  138. context.ConsumeAndDiscard();
  139. }
  140. auto HandleStatementIfConditionFinish(Context& context) -> void {
  141. auto state = context.PopState();
  142. context.PushState(state, StateKind::StatementIfThenBlockFinish);
  143. context.PushState(StateKind::CodeBlock);
  144. }
  145. auto HandleStatementIfThenBlockFinish(Context& context) -> void {
  146. auto state = context.PopState();
  147. if (context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::Else,
  148. NodeKind::IfStatementElse)) {
  149. context.PushState(state, StateKind::StatementIfElseBlockFinish);
  150. // `else if` is permitted as a special case.
  151. context.PushState(context.PositionIs(Lex::TokenKind::If)
  152. ? StateKind::StatementIf
  153. : StateKind::CodeBlock);
  154. } else {
  155. context.AddNode(NodeKind::IfStatement, state.token, state.has_error);
  156. }
  157. }
  158. auto HandleStatementIfElseBlockFinish(Context& context) -> void {
  159. auto state = context.PopState();
  160. context.AddNode(NodeKind::IfStatement, state.token, state.has_error);
  161. }
  162. auto HandleStatementReturn(Context& context) -> void {
  163. auto state = context.PopState();
  164. context.PushState(state, StateKind::StatementReturnFinish);
  165. context.AddLeafNode(NodeKind::ReturnStatementStart, context.Consume());
  166. if (auto var_token = context.ConsumeIf(Lex::TokenKind::Var)) {
  167. // `return var;`
  168. context.AddLeafNode(NodeKind::ReturnVarModifier, *var_token);
  169. } else if (!context.PositionIs(Lex::TokenKind::Semi)) {
  170. // `return <expression>;`
  171. context.PushState(StateKind::Expr);
  172. } else {
  173. // `return;`
  174. }
  175. }
  176. auto HandleStatementReturnFinish(Context& context) -> void {
  177. HandleStatementKeywordFinish(context, NodeKind::ReturnStatement);
  178. }
  179. auto HandleStatementScopeLoop(Context& context) -> void {
  180. // This maintains the current state until we're at the end of the scope.
  181. auto token_kind = context.PositionKind();
  182. if (token_kind == Lex::TokenKind::CloseCurlyBrace) {
  183. auto state = context.PopState();
  184. if (state.has_error) {
  185. context.ReturnErrorOnState();
  186. }
  187. } else {
  188. context.PushState(StateKind::Statement);
  189. }
  190. }
  191. auto HandleStatementWhile(Context& context) -> void {
  192. context.PopAndDiscardState();
  193. context.PushState(StateKind::StatementWhileConditionFinish);
  194. context.PushState(StateKind::ParenConditionAsWhile);
  195. context.ConsumeAndDiscard();
  196. }
  197. auto HandleStatementWhileConditionFinish(Context& context) -> void {
  198. auto state = context.PopState();
  199. context.PushState(state, StateKind::StatementWhileBlockFinish);
  200. context.PushState(StateKind::CodeBlock);
  201. }
  202. auto HandleStatementWhileBlockFinish(Context& context) -> void {
  203. auto state = context.PopState();
  204. context.AddNode(NodeKind::WhileStatement, state.token, state.has_error);
  205. }
  206. } // namespace Carbon::Parse