parser_handle_expression.cpp 13 KB

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