handle_expr.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. namespace Carbon::Parse {
  6. static auto DiagnoseStatementOperatorAsSubExpr(Context& context) -> void {
  7. CARBON_DIAGNOSTIC(StatementOperatorAsSubExpr, Error,
  8. "Operator `{0}` can only be used as a complete statement.",
  9. Lex::TokenKind);
  10. context.emitter().Emit(*context.position(), StatementOperatorAsSubExpr,
  11. context.PositionKind());
  12. }
  13. auto HandleExpr(Context& context) -> void {
  14. auto state = context.PopState();
  15. // Check for a prefix operator.
  16. if (auto operator_precedence =
  17. PrecedenceGroup::ForLeading(context.PositionKind())) {
  18. if (PrecedenceGroup::GetPriority(state.ambient_precedence,
  19. *operator_precedence) !=
  20. OperatorPriority::RightFirst) {
  21. // The precedence rules don't permit this prefix operator in this
  22. // context. Diagnose this, but carry on and parse it anyway.
  23. if (PrecedenceGroup::GetPriority(PrecedenceGroup::ForTopLevelExpr(),
  24. *operator_precedence) ==
  25. OperatorPriority::RightFirst) {
  26. CARBON_DIAGNOSTIC(
  27. UnaryOperatorRequiresParentheses, Error,
  28. "Parentheses are required around this unary `{0}` operator.",
  29. Lex::TokenKind);
  30. context.emitter().Emit(*context.position(),
  31. UnaryOperatorRequiresParentheses,
  32. context.PositionKind());
  33. } else {
  34. // This operator wouldn't be allowed even if parenthesized.
  35. DiagnoseStatementOperatorAsSubExpr(context);
  36. }
  37. } else {
  38. // Check that this operator follows the proper whitespace rules.
  39. context.DiagnoseOperatorFixity(Context::OperatorFixity::Prefix);
  40. }
  41. if (context.PositionIs(Lex::TokenKind::If)) {
  42. context.PushState(State::IfExprFinish);
  43. context.PushState(State::IfExprFinishCondition);
  44. } else {
  45. context.PushStateForExprLoop(State::ExprLoopForPrefix,
  46. state.ambient_precedence,
  47. *operator_precedence);
  48. }
  49. context.ConsumeAndDiscard();
  50. context.PushStateForExpr(*operator_precedence);
  51. } else {
  52. context.PushStateForExprLoop(State::ExprLoop, state.ambient_precedence,
  53. PrecedenceGroup::ForPostfixExpr());
  54. context.PushState(State::ExprInPostfix);
  55. }
  56. }
  57. auto HandleExprInPostfix(Context& context) -> void {
  58. auto state = context.PopState();
  59. // Continue to the loop state.
  60. state.state = State::ExprInPostfixLoop;
  61. // Parses a primary expression, which is either a terminal portion of an
  62. // expression tree, such as an identifier or literal, or a parenthesized
  63. // expression.
  64. switch (context.PositionKind()) {
  65. case Lex::TokenKind::Identifier: {
  66. context.AddLeafNode(NodeKind::IdentifierNameExpr, context.Consume());
  67. context.PushState(state);
  68. break;
  69. }
  70. case Lex::TokenKind::False: {
  71. context.AddLeafNode(NodeKind::BoolLiteralFalse, context.Consume());
  72. context.PushState(state);
  73. break;
  74. }
  75. case Lex::TokenKind::True: {
  76. context.AddLeafNode(NodeKind::BoolLiteralTrue, context.Consume());
  77. context.PushState(state);
  78. break;
  79. }
  80. case Lex::TokenKind::IntLiteral: {
  81. context.AddLeafNode(NodeKind::IntLiteral, context.Consume());
  82. context.PushState(state);
  83. break;
  84. }
  85. case Lex::TokenKind::RealLiteral: {
  86. context.AddLeafNode(NodeKind::RealLiteral, context.Consume());
  87. context.PushState(state);
  88. break;
  89. }
  90. case Lex::TokenKind::StringLiteral: {
  91. context.AddLeafNode(NodeKind::StringLiteral, context.Consume());
  92. context.PushState(state);
  93. break;
  94. }
  95. case Lex::TokenKind::Bool: {
  96. context.AddLeafNode(NodeKind::BoolTypeLiteral, context.Consume());
  97. context.PushState(state);
  98. break;
  99. }
  100. case Lex::TokenKind::IntTypeLiteral: {
  101. context.AddLeafNode(NodeKind::IntTypeLiteral, context.Consume());
  102. context.PushState(state);
  103. break;
  104. }
  105. case Lex::TokenKind::UnsignedIntTypeLiteral: {
  106. context.AddLeafNode(NodeKind::UnsignedIntTypeLiteral, context.Consume());
  107. context.PushState(state);
  108. break;
  109. }
  110. case Lex::TokenKind::FloatTypeLiteral: {
  111. context.AddLeafNode(NodeKind::FloatTypeLiteral, context.Consume());
  112. context.PushState(state);
  113. break;
  114. }
  115. case Lex::TokenKind::StringTypeLiteral: {
  116. context.AddLeafNode(NodeKind::StringTypeLiteral, context.Consume());
  117. context.PushState(state);
  118. break;
  119. }
  120. case Lex::TokenKind::Type: {
  121. context.AddLeafNode(NodeKind::TypeTypeLiteral, context.Consume());
  122. context.PushState(state);
  123. break;
  124. }
  125. case Lex::TokenKind::OpenCurlyBrace: {
  126. context.PushState(state);
  127. context.PushState(State::BraceExpr);
  128. break;
  129. }
  130. case Lex::TokenKind::OpenParen: {
  131. context.PushState(state);
  132. context.PushState(State::ParenExpr);
  133. break;
  134. }
  135. case Lex::TokenKind::OpenSquareBracket: {
  136. context.PushState(state);
  137. context.PushState(State::ArrayExpr);
  138. break;
  139. }
  140. case Lex::TokenKind::Package: {
  141. context.AddLeafNode(NodeKind::PackageExpr, context.Consume());
  142. context.PushState(state);
  143. break;
  144. }
  145. case Lex::TokenKind::SelfValueIdentifier: {
  146. context.AddLeafNode(NodeKind::SelfValueNameExpr, context.Consume());
  147. context.PushState(state);
  148. break;
  149. }
  150. case Lex::TokenKind::SelfTypeIdentifier: {
  151. context.AddLeafNode(NodeKind::SelfTypeNameExpr, context.Consume());
  152. context.PushState(state);
  153. break;
  154. }
  155. default: {
  156. // Add a node to keep the parse tree balanced.
  157. context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
  158. /*has_error=*/true);
  159. CARBON_DIAGNOSTIC(ExpectedExpr, Error, "Expected expression.");
  160. context.emitter().Emit(*context.position(), ExpectedExpr);
  161. context.ReturnErrorOnState();
  162. break;
  163. }
  164. }
  165. }
  166. auto HandleExprInPostfixLoop(Context& context) -> void {
  167. // This is a cyclic state that repeats, so this state is typically pushed back
  168. // on.
  169. auto state = context.PopState();
  170. state.token = *context.position();
  171. switch (context.PositionKind()) {
  172. case Lex::TokenKind::Period: {
  173. context.PushState(state);
  174. context.PushState(state, State::PeriodAsExpr);
  175. break;
  176. }
  177. case Lex::TokenKind::MinusGreater: {
  178. context.PushState(state);
  179. context.PushState(state, State::ArrowExpr);
  180. break;
  181. }
  182. case Lex::TokenKind::OpenParen: {
  183. context.PushState(state);
  184. context.PushState(state, State::CallExpr);
  185. break;
  186. }
  187. case Lex::TokenKind::OpenSquareBracket: {
  188. context.PushState(state);
  189. context.PushState(state, State::IndexExpr);
  190. break;
  191. }
  192. default: {
  193. if (state.has_error) {
  194. context.ReturnErrorOnState();
  195. }
  196. break;
  197. }
  198. }
  199. }
  200. auto HandleExprLoop(Context& context) -> void {
  201. auto state = context.PopState();
  202. auto operator_kind = context.PositionKind();
  203. auto trailing_operator = PrecedenceGroup::ForTrailing(
  204. operator_kind, context.IsTrailingOperatorInfix());
  205. if (!trailing_operator) {
  206. if (state.has_error) {
  207. context.ReturnErrorOnState();
  208. }
  209. return;
  210. }
  211. auto [operator_precedence, is_binary] = *trailing_operator;
  212. // TODO: If this operator is ambiguous with either the ambient precedence
  213. // or the LHS precedence, and there's a variant with a different fixity
  214. // that would work, use that one instead for error recovery.
  215. if (PrecedenceGroup::GetPriority(state.ambient_precedence,
  216. operator_precedence) !=
  217. OperatorPriority::RightFirst) {
  218. // The precedence rules don't permit this operator in this context. Try
  219. // again in the enclosing expression context.
  220. if (state.has_error) {
  221. context.ReturnErrorOnState();
  222. }
  223. return;
  224. }
  225. if (PrecedenceGroup::GetPriority(state.lhs_precedence, operator_precedence) !=
  226. OperatorPriority::LeftFirst) {
  227. // Either the LHS operator and this operator are ambiguous, or the
  228. // LHS operator is a unary operator that can't be nested within
  229. // this operator. Either way, parentheses are required.
  230. if (PrecedenceGroup::GetPriority(PrecedenceGroup::ForTopLevelExpr(),
  231. operator_precedence) ==
  232. OperatorPriority::RightFirst) {
  233. CARBON_DIAGNOSTIC(
  234. OperatorRequiresParentheses, Error,
  235. "Parentheses are required to disambiguate operator precedence.");
  236. context.emitter().Emit(*context.position(), OperatorRequiresParentheses);
  237. } else {
  238. // This operator wouldn't be allowed even if parenthesized.
  239. DiagnoseStatementOperatorAsSubExpr(context);
  240. }
  241. state.has_error = true;
  242. } else {
  243. context.DiagnoseOperatorFixity(is_binary
  244. ? Context::OperatorFixity::Infix
  245. : Context::OperatorFixity::Postfix);
  246. }
  247. state.token = context.Consume();
  248. state.lhs_precedence = operator_precedence;
  249. if (is_binary) {
  250. switch (operator_kind) {
  251. // For `and` and `or`, wrap the first operand in a virtual parse tree
  252. // node so that checking can insert control flow here.
  253. case Lex::TokenKind::And:
  254. context.AddNode(NodeKind::ShortCircuitOperandAnd, state.token,
  255. state.subtree_start, state.has_error);
  256. state.state = State::ExprLoopForShortCircuitOperatorAsAnd;
  257. break;
  258. case Lex::TokenKind::Or:
  259. context.AddNode(NodeKind::ShortCircuitOperandOr, state.token,
  260. state.subtree_start, state.has_error);
  261. state.state = State::ExprLoopForShortCircuitOperatorAsOr;
  262. break;
  263. default:
  264. state.state = State::ExprLoopForBinary;
  265. break;
  266. }
  267. context.PushState(state);
  268. context.PushStateForExpr(operator_precedence);
  269. } else {
  270. context.AddNode(NodeKind::PostfixOperator, state.token, state.subtree_start,
  271. state.has_error);
  272. state.has_error = false;
  273. context.PushState(state);
  274. }
  275. }
  276. // Adds the operator node and returns the the main expression loop.
  277. static auto HandleExprLoopForOperator(Context& context, NodeKind node_kind)
  278. -> void {
  279. auto state = context.PopState();
  280. context.AddNode(node_kind, state.token, state.subtree_start, state.has_error);
  281. state.has_error = false;
  282. context.PushState(state, State::ExprLoop);
  283. }
  284. auto HandleExprLoopForBinary(Context& context) -> void {
  285. HandleExprLoopForOperator(context, NodeKind::InfixOperator);
  286. }
  287. auto HandleExprLoopForPrefix(Context& context) -> void {
  288. HandleExprLoopForOperator(context, NodeKind::PrefixOperator);
  289. }
  290. auto HandleExprLoopForShortCircuitOperatorAsAnd(Context& context) -> void {
  291. HandleExprLoopForOperator(context, NodeKind::ShortCircuitOperatorAnd);
  292. }
  293. auto HandleExprLoopForShortCircuitOperatorAsOr(Context& context) -> void {
  294. HandleExprLoopForOperator(context, NodeKind::ShortCircuitOperatorOr);
  295. }
  296. auto HandleIfExprFinishCondition(Context& context) -> void {
  297. auto state = context.PopState();
  298. context.AddNode(NodeKind::IfExprIf, state.token, state.subtree_start,
  299. state.has_error);
  300. if (context.PositionIs(Lex::TokenKind::Then)) {
  301. context.PushState(State::IfExprFinishThen);
  302. context.ConsumeChecked(Lex::TokenKind::Then);
  303. context.PushStateForExpr(*PrecedenceGroup::ForLeading(Lex::TokenKind::If));
  304. } else {
  305. // TODO: Include the location of the `if` token.
  306. CARBON_DIAGNOSTIC(ExpectedThenAfterIf, Error,
  307. "Expected `then` after `if` condition.");
  308. if (!state.has_error) {
  309. context.emitter().Emit(*context.position(), ExpectedThenAfterIf);
  310. }
  311. // Add placeholders for `IfExprThen` and final `Expr`.
  312. context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
  313. /*has_error=*/true);
  314. context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
  315. /*has_error=*/true);
  316. context.ReturnErrorOnState();
  317. }
  318. }
  319. auto HandleIfExprFinishThen(Context& context) -> void {
  320. auto state = context.PopState();
  321. context.AddNode(NodeKind::IfExprThen, state.token, state.subtree_start,
  322. state.has_error);
  323. if (context.PositionIs(Lex::TokenKind::Else)) {
  324. context.PushState(State::IfExprFinishElse);
  325. context.ConsumeChecked(Lex::TokenKind::Else);
  326. context.PushStateForExpr(*PrecedenceGroup::ForLeading(Lex::TokenKind::If));
  327. } else {
  328. // TODO: Include the location of the `if` token.
  329. CARBON_DIAGNOSTIC(ExpectedElseAfterIf, Error,
  330. "Expected `else` after `if ... then ...`.");
  331. if (!state.has_error) {
  332. context.emitter().Emit(*context.position(), ExpectedElseAfterIf);
  333. }
  334. // Add placeholder for the final `Expr`.
  335. context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
  336. /*has_error=*/true);
  337. context.ReturnErrorOnState();
  338. }
  339. }
  340. auto HandleIfExprFinishElse(Context& context) -> void {
  341. auto else_state = context.PopState();
  342. // Propagate the location of `else`.
  343. auto if_state = context.PopState();
  344. if_state.token = else_state.token;
  345. if_state.has_error |= else_state.has_error;
  346. context.PushState(if_state);
  347. }
  348. auto HandleIfExprFinish(Context& context) -> void {
  349. auto state = context.PopState();
  350. context.AddNode(NodeKind::IfExprElse, state.token, state.subtree_start,
  351. state.has_error);
  352. }
  353. auto HandleExprStatementFinish(Context& context) -> void {
  354. auto state = context.PopState();
  355. if (auto semi = context.ConsumeIf(Lex::TokenKind::Semi)) {
  356. context.AddNode(NodeKind::ExprStatement, *semi, state.subtree_start,
  357. state.has_error);
  358. return;
  359. }
  360. if (!state.has_error) {
  361. CARBON_DIAGNOSTIC(ExpectedExprSemi, Error,
  362. "Expected `;` after expression statement.");
  363. context.emitter().Emit(*context.position(), ExpectedExprSemi);
  364. }
  365. context.AddNode(NodeKind::ExprStatement,
  366. context.SkipPastLikelyEnd(state.token), state.subtree_start,
  367. /*has_error=*/true);
  368. }
  369. } // namespace Carbon::Parse