handle_statement.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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::DeclAsRegular);
  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. context.PushState(state);
  106. context.PushState(StateKind::Pattern);
  107. }
  108. auto HandleStatementForHeaderIn(Context& context) -> void {
  109. auto state = context.PopState();
  110. auto end_token = state.token;
  111. if (context.PositionIs(Lex::TokenKind::In)) {
  112. end_token = context.Consume();
  113. } else if (context.PositionIs(Lex::TokenKind::Colon)) {
  114. CARBON_DIAGNOSTIC(ExpectedInNotColon, Error,
  115. "`:` should be replaced by `in`");
  116. context.emitter().Emit(*context.position(), ExpectedInNotColon);
  117. state.has_error = true;
  118. end_token = context.Consume();
  119. } else if (!state.has_error) {
  120. CARBON_DIAGNOSTIC(ExpectedIn, Error, "expected `in` after loop pattern");
  121. context.emitter().Emit(*context.position(), ExpectedIn);
  122. state.has_error = true;
  123. }
  124. context.AddNode(NodeKind::ForIn, end_token, state.has_error);
  125. context.PushState(state, StateKind::StatementForHeaderFinish);
  126. // If we had a parse error, try to skip to the closing paren rather than
  127. // parsing an expression.
  128. if (state.has_error) {
  129. auto open_token = context.state_stack().back().token;
  130. if (context.tokens().GetKind(open_token).is_opening_symbol()) {
  131. context.SkipTo(context.tokens().GetMatchedClosingToken(open_token));
  132. return;
  133. }
  134. }
  135. context.PushState(StateKind::Expr);
  136. }
  137. auto HandleStatementForHeaderFinish(Context& context) -> void {
  138. auto state = context.PopState();
  139. context.ConsumeAndAddCloseSymbol(state.token, state, NodeKind::ForHeader);
  140. context.PushState(StateKind::CodeBlock);
  141. }
  142. auto HandleStatementForFinish(Context& context) -> void {
  143. auto state = context.PopState();
  144. context.AddNode(NodeKind::ForStatement, state.token, state.has_error);
  145. }
  146. auto HandleStatementIf(Context& context) -> void {
  147. context.PopAndDiscardState();
  148. context.PushState(StateKind::StatementIfConditionFinish);
  149. context.PushState(StateKind::ParenConditionAsIf);
  150. context.ConsumeAndDiscard();
  151. }
  152. auto HandleStatementIfConditionFinish(Context& context) -> void {
  153. auto state = context.PopState();
  154. context.PushState(state, StateKind::StatementIfThenBlockFinish);
  155. context.PushState(StateKind::CodeBlock);
  156. }
  157. auto HandleStatementIfThenBlockFinish(Context& context) -> void {
  158. auto state = context.PopState();
  159. if (context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::Else,
  160. NodeKind::IfStatementElse)) {
  161. context.PushState(state, StateKind::StatementIfElseBlockFinish);
  162. // `else if` is permitted as a special case.
  163. context.PushState(context.PositionIs(Lex::TokenKind::If)
  164. ? StateKind::StatementIf
  165. : StateKind::CodeBlock);
  166. } else {
  167. context.AddNode(NodeKind::IfStatement, state.token, state.has_error);
  168. }
  169. }
  170. auto HandleStatementIfElseBlockFinish(Context& context) -> void {
  171. auto state = context.PopState();
  172. context.AddNode(NodeKind::IfStatement, state.token, state.has_error);
  173. }
  174. auto HandleStatementReturn(Context& context) -> void {
  175. auto state = context.PopState();
  176. context.PushState(state, StateKind::StatementReturnFinish);
  177. context.AddLeafNode(NodeKind::ReturnStatementStart, context.Consume());
  178. if (auto var_token = context.ConsumeIf(Lex::TokenKind::Var)) {
  179. // `return var;`
  180. context.AddLeafNode(NodeKind::ReturnVarModifier, *var_token);
  181. } else if (!context.PositionIs(Lex::TokenKind::Semi)) {
  182. // `return <expression>;`
  183. context.PushState(StateKind::Expr);
  184. } else {
  185. // `return;`
  186. }
  187. }
  188. auto HandleStatementReturnFinish(Context& context) -> void {
  189. HandleStatementKeywordFinish(context, NodeKind::ReturnStatement);
  190. }
  191. auto HandleStatementScopeLoop(Context& context) -> void {
  192. // This maintains the current state until we're at the end of the scope.
  193. auto token_kind = context.PositionKind();
  194. if (token_kind == Lex::TokenKind::CloseCurlyBrace) {
  195. auto state = context.PopState();
  196. if (state.has_error) {
  197. context.ReturnErrorOnState();
  198. }
  199. } else {
  200. context.PushState(StateKind::Statement);
  201. }
  202. }
  203. auto HandleStatementWhile(Context& context) -> void {
  204. context.PopAndDiscardState();
  205. context.PushState(StateKind::StatementWhileConditionFinish);
  206. context.PushState(StateKind::ParenConditionAsWhile);
  207. context.ConsumeAndDiscard();
  208. }
  209. auto HandleStatementWhileConditionFinish(Context& context) -> void {
  210. auto state = context.PopState();
  211. context.PushState(state, StateKind::StatementWhileBlockFinish);
  212. context.PushState(StateKind::CodeBlock);
  213. }
  214. auto HandleStatementWhileBlockFinish(Context& context) -> void {
  215. auto state = context.PopState();
  216. context.AddNode(NodeKind::WhileStatement, state.token, state.has_error);
  217. }
  218. } // namespace Carbon::Parse