handle_statement.cpp 7.8 KB

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