parser_handle_expression.cpp 11 KB

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