handle_statement.cpp 8.5 KB

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