handle_statement.cpp 8.4 KB

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