handle_expression.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 DiagnoseStatementOperatorAsSubexpression(Context& context) -> void {
  7. CARBON_DIAGNOSTIC(StatementOperatorAsSubexpression, Error,
  8. "Operator `{0}` can only be used as a complete statement.",
  9. Lex::TokenKind);
  10. context.emitter().Emit(*context.position(), StatementOperatorAsSubexpression,
  11. context.PositionKind());
  12. }
  13. auto HandleExpression(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::ForTopLevelExpression(),
  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. DiagnoseStatementOperatorAsSubexpression(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::IfExpressionFinish);
  43. context.PushState(State::IfExpressionFinishCondition);
  44. } else {
  45. context.PushStateForExpressionLoop(State::ExpressionLoopForPrefix,
  46. state.ambient_precedence,
  47. *operator_precedence);
  48. }
  49. ++context.position();
  50. context.PushStateForExpression(*operator_precedence);
  51. } else {
  52. context.PushStateForExpressionLoop(State::ExpressionLoop,
  53. state.ambient_precedence,
  54. PrecedenceGroup::ForPostfixExpression());
  55. context.PushState(State::ExpressionInPostfix);
  56. }
  57. }
  58. auto HandleExpressionInPostfix(Context& context) -> void {
  59. auto state = context.PopState();
  60. // Continue to the loop state.
  61. state.state = State::ExpressionInPostfixLoop;
  62. // Parses a primary expression, which is either a terminal portion of an
  63. // expression tree, such as an identifier or literal, or a parenthesized
  64. // expression.
  65. switch (context.PositionKind()) {
  66. case Lex::TokenKind::Identifier: {
  67. context.AddLeafNode(NodeKind::NameExpression, context.Consume());
  68. context.PushState(state);
  69. break;
  70. }
  71. case Lex::TokenKind::False:
  72. case Lex::TokenKind::True:
  73. case Lex::TokenKind::IntegerLiteral:
  74. case Lex::TokenKind::RealLiteral:
  75. case Lex::TokenKind::StringLiteral:
  76. case Lex::TokenKind::Bool:
  77. case Lex::TokenKind::IntegerTypeLiteral:
  78. case Lex::TokenKind::UnsignedIntegerTypeLiteral:
  79. case Lex::TokenKind::FloatingPointTypeLiteral:
  80. case Lex::TokenKind::StringTypeLiteral:
  81. case Lex::TokenKind::Type: {
  82. context.AddLeafNode(NodeKind::Literal, context.Consume());
  83. context.PushState(state);
  84. break;
  85. }
  86. case Lex::TokenKind::OpenCurlyBrace: {
  87. context.PushState(state);
  88. context.PushState(State::BraceExpression);
  89. break;
  90. }
  91. case Lex::TokenKind::OpenParen: {
  92. context.PushState(state);
  93. context.PushState(State::ParenExpression);
  94. break;
  95. }
  96. case Lex::TokenKind::OpenSquareBracket: {
  97. context.PushState(state);
  98. context.PushState(State::ArrayExpression);
  99. break;
  100. }
  101. case Lex::TokenKind::SelfValueIdentifier: {
  102. context.AddLeafNode(NodeKind::SelfValueName, context.Consume());
  103. context.PushState(state);
  104. break;
  105. }
  106. case Lex::TokenKind::SelfTypeIdentifier: {
  107. context.AddLeafNode(NodeKind::SelfTypeNameExpression, context.Consume());
  108. context.PushState(state);
  109. break;
  110. }
  111. default: {
  112. // Add a node to keep the parse tree balanced.
  113. context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
  114. /*has_error=*/true);
  115. CARBON_DIAGNOSTIC(ExpectedExpression, Error, "Expected expression.");
  116. context.emitter().Emit(*context.position(), ExpectedExpression);
  117. context.ReturnErrorOnState();
  118. break;
  119. }
  120. }
  121. }
  122. auto HandleExpressionInPostfixLoop(Context& context) -> void {
  123. // This is a cyclic state that repeats, so this state is typically pushed back
  124. // on.
  125. auto state = context.PopState();
  126. state.token = *context.position();
  127. switch (context.PositionKind()) {
  128. case Lex::TokenKind::Period: {
  129. context.PushState(state);
  130. state.state = State::PeriodAsExpression;
  131. context.PushState(state);
  132. break;
  133. }
  134. case Lex::TokenKind::MinusGreater: {
  135. context.PushState(state);
  136. state.state = State::ArrowExpression;
  137. context.PushState(state);
  138. break;
  139. }
  140. case Lex::TokenKind::OpenParen: {
  141. context.PushState(state);
  142. state.state = State::CallExpression;
  143. context.PushState(state);
  144. break;
  145. }
  146. case Lex::TokenKind::OpenSquareBracket: {
  147. context.PushState(state);
  148. state.state = State::IndexExpression;
  149. context.PushState(state);
  150. break;
  151. }
  152. default: {
  153. if (state.has_error) {
  154. context.ReturnErrorOnState();
  155. }
  156. break;
  157. }
  158. }
  159. }
  160. auto HandleExpressionLoop(Context& context) -> void {
  161. auto state = context.PopState();
  162. auto operator_kind = context.PositionKind();
  163. auto trailing_operator = PrecedenceGroup::ForTrailing(
  164. operator_kind, context.IsTrailingOperatorInfix());
  165. if (!trailing_operator) {
  166. if (state.has_error) {
  167. context.ReturnErrorOnState();
  168. }
  169. return;
  170. }
  171. auto [operator_precedence, is_binary] = *trailing_operator;
  172. // TODO: If this operator is ambiguous with either the ambient precedence
  173. // or the LHS precedence, and there's a variant with a different fixity
  174. // that would work, use that one instead for error recovery.
  175. if (PrecedenceGroup::GetPriority(state.ambient_precedence,
  176. operator_precedence) !=
  177. OperatorPriority::RightFirst) {
  178. // The precedence rules don't permit this operator in this context. Try
  179. // again in the enclosing expression context.
  180. if (state.has_error) {
  181. context.ReturnErrorOnState();
  182. }
  183. return;
  184. }
  185. if (PrecedenceGroup::GetPriority(state.lhs_precedence, operator_precedence) !=
  186. OperatorPriority::LeftFirst) {
  187. // Either the LHS operator and this operator are ambiguous, or the
  188. // LHS operator is a unary operator that can't be nested within
  189. // this operator. Either way, parentheses are required.
  190. if (PrecedenceGroup::GetPriority(PrecedenceGroup::ForTopLevelExpression(),
  191. operator_precedence) ==
  192. OperatorPriority::RightFirst) {
  193. CARBON_DIAGNOSTIC(
  194. OperatorRequiresParentheses, Error,
  195. "Parentheses are required to disambiguate operator precedence.");
  196. context.emitter().Emit(*context.position(), OperatorRequiresParentheses);
  197. } else {
  198. // This operator wouldn't be allowed even if parenthesized.
  199. DiagnoseStatementOperatorAsSubexpression(context);
  200. }
  201. state.has_error = true;
  202. } else {
  203. context.DiagnoseOperatorFixity(is_binary
  204. ? Context::OperatorFixity::Infix
  205. : Context::OperatorFixity::Postfix);
  206. }
  207. state.token = context.Consume();
  208. state.lhs_precedence = operator_precedence;
  209. if (is_binary) {
  210. if (operator_kind == Lex::TokenKind::And ||
  211. operator_kind == Lex::TokenKind::Or) {
  212. // For `and` and `or`, wrap the first operand in a virtual parse tree
  213. // node so that semantics can insert control flow here.
  214. context.AddNode(NodeKind::ShortCircuitOperand, state.token,
  215. state.subtree_start, state.has_error);
  216. }
  217. state.state = State::ExpressionLoopForBinary;
  218. context.PushState(state);
  219. context.PushStateForExpression(operator_precedence);
  220. } else {
  221. context.AddNode(NodeKind::PostfixOperator, state.token, state.subtree_start,
  222. state.has_error);
  223. state.has_error = false;
  224. context.PushState(state);
  225. }
  226. }
  227. auto HandleExpressionLoopForBinary(Context& context) -> void {
  228. auto state = context.PopState();
  229. context.AddNode(NodeKind::InfixOperator, state.token, state.subtree_start,
  230. state.has_error);
  231. state.state = State::ExpressionLoop;
  232. state.has_error = false;
  233. context.PushState(state);
  234. }
  235. auto HandleExpressionLoopForPrefix(Context& context) -> void {
  236. auto state = context.PopState();
  237. context.AddNode(NodeKind::PrefixOperator, state.token, state.subtree_start,
  238. state.has_error);
  239. state.state = State::ExpressionLoop;
  240. state.has_error = false;
  241. context.PushState(state);
  242. }
  243. auto HandleIfExpressionFinishCondition(Context& context) -> void {
  244. auto state = context.PopState();
  245. context.AddNode(NodeKind::IfExpressionIf, state.token, state.subtree_start,
  246. state.has_error);
  247. if (context.PositionIs(Lex::TokenKind::Then)) {
  248. context.PushState(State::IfExpressionFinishThen);
  249. context.ConsumeChecked(Lex::TokenKind::Then);
  250. context.PushStateForExpression(
  251. *PrecedenceGroup::ForLeading(Lex::TokenKind::If));
  252. } else {
  253. // TODO: Include the location of the `if` token.
  254. CARBON_DIAGNOSTIC(ExpectedThenAfterIf, Error,
  255. "Expected `then` after `if` condition.");
  256. if (!state.has_error) {
  257. context.emitter().Emit(*context.position(), ExpectedThenAfterIf);
  258. }
  259. // Add placeholders for `IfExpressionThen` and final `Expression`.
  260. context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
  261. /*has_error=*/true);
  262. context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
  263. /*has_error=*/true);
  264. context.ReturnErrorOnState();
  265. }
  266. }
  267. auto HandleIfExpressionFinishThen(Context& context) -> void {
  268. auto state = context.PopState();
  269. context.AddNode(NodeKind::IfExpressionThen, state.token, state.subtree_start,
  270. state.has_error);
  271. if (context.PositionIs(Lex::TokenKind::Else)) {
  272. context.PushState(State::IfExpressionFinishElse);
  273. context.ConsumeChecked(Lex::TokenKind::Else);
  274. context.PushStateForExpression(
  275. *PrecedenceGroup::ForLeading(Lex::TokenKind::If));
  276. } else {
  277. // TODO: Include the location of the `if` token.
  278. CARBON_DIAGNOSTIC(ExpectedElseAfterIf, Error,
  279. "Expected `else` after `if ... then ...`.");
  280. if (!state.has_error) {
  281. context.emitter().Emit(*context.position(), ExpectedElseAfterIf);
  282. }
  283. // Add placeholder for the final `Expression`.
  284. context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
  285. /*has_error=*/true);
  286. context.ReturnErrorOnState();
  287. }
  288. }
  289. auto HandleIfExpressionFinishElse(Context& context) -> void {
  290. auto else_state = context.PopState();
  291. // Propagate the location of `else`.
  292. auto if_state = context.PopState();
  293. if_state.token = else_state.token;
  294. if_state.has_error |= else_state.has_error;
  295. context.PushState(if_state);
  296. }
  297. auto HandleIfExpressionFinish(Context& context) -> void {
  298. auto state = context.PopState();
  299. context.AddNode(NodeKind::IfExpressionElse, state.token, state.subtree_start,
  300. state.has_error);
  301. }
  302. auto HandleExpressionStatementFinish(Context& context) -> void {
  303. auto state = context.PopState();
  304. if (auto semi = context.ConsumeIf(Lex::TokenKind::Semi)) {
  305. context.AddNode(NodeKind::ExpressionStatement, *semi, state.subtree_start,
  306. state.has_error);
  307. return;
  308. }
  309. if (!state.has_error) {
  310. CARBON_DIAGNOSTIC(ExpectedExpressionSemi, Error,
  311. "Expected `;` after expression statement.");
  312. context.emitter().Emit(*context.position(), ExpectedExpressionSemi);
  313. }
  314. if (auto semi_token = context.SkipPastLikelyEnd(state.token)) {
  315. context.AddNode(NodeKind::ExpressionStatement, *semi_token,
  316. state.subtree_start,
  317. /*has_error=*/true);
  318. return;
  319. }
  320. // Found junk not even followed by a `;`, no node to add.
  321. context.ReturnErrorOnState();
  322. }
  323. } // namespace Carbon::Parse