parser_handle_expression.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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::ExpressionIfFinish);
  26. context.PushState(ParserState::ExpressionThen);
  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::NameReference, context.Consume());
  51. context.PushState(state);
  52. break;
  53. }
  54. case TokenKind::IntegerLiteral:
  55. case TokenKind::RealLiteral:
  56. case TokenKind::StringLiteral:
  57. case TokenKind::IntegerTypeLiteral:
  58. case TokenKind::UnsignedIntegerTypeLiteral:
  59. case TokenKind::FloatingPointTypeLiteral:
  60. case TokenKind::StringTypeLiteral: {
  61. context.AddLeafNode(ParseNodeKind::Literal, context.Consume());
  62. context.PushState(state);
  63. break;
  64. }
  65. case TokenKind::OpenCurlyBrace: {
  66. context.PushState(state);
  67. context.PushState(ParserState::BraceExpression);
  68. break;
  69. }
  70. case TokenKind::OpenParen: {
  71. context.PushState(state);
  72. context.PushState(ParserState::ParenExpression);
  73. break;
  74. }
  75. case TokenKind::SelfValueIdentifier: {
  76. context.AddLeafNode(ParseNodeKind::SelfValueIdentifier,
  77. context.Consume());
  78. context.PushState(state);
  79. break;
  80. }
  81. case TokenKind::SelfTypeIdentifier: {
  82. context.AddLeafNode(ParseNodeKind::SelfTypeIdentifier, context.Consume());
  83. context.PushState(state);
  84. break;
  85. }
  86. default: {
  87. // Add a node to keep the parse tree balanced.
  88. context.AddLeafNode(ParseNodeKind::InvalidParse, *context.position(),
  89. /*has_error=*/true);
  90. CARBON_DIAGNOSTIC(ExpectedExpression, Error, "Expected expression.");
  91. context.emitter().Emit(*context.position(), ExpectedExpression);
  92. context.ReturnErrorOnState();
  93. break;
  94. }
  95. }
  96. }
  97. auto ParserHandleExpressionInPostfixLoop(ParserContext& context) -> void {
  98. // This is a cyclic state that repeats, so this state is typically pushed back
  99. // on.
  100. auto state = context.PopState();
  101. state.token = *context.position();
  102. switch (context.PositionKind()) {
  103. case TokenKind::Period: {
  104. context.PushState(state);
  105. state.state = ParserState::DesignatorAsExpression;
  106. context.PushState(state);
  107. break;
  108. }
  109. case TokenKind::OpenParen: {
  110. context.PushState(state);
  111. state.state = ParserState::CallExpression;
  112. context.PushState(state);
  113. break;
  114. }
  115. default: {
  116. if (state.has_error) {
  117. context.ReturnErrorOnState();
  118. }
  119. break;
  120. }
  121. }
  122. }
  123. auto ParserHandleExpressionLoop(ParserContext& context) -> void {
  124. auto state = context.PopState();
  125. auto trailing_operator = PrecedenceGroup::ForTrailing(
  126. context.PositionKind(), context.IsTrailingOperatorInfix());
  127. if (!trailing_operator) {
  128. if (state.has_error) {
  129. context.ReturnErrorOnState();
  130. }
  131. return;
  132. }
  133. auto [operator_precedence, is_binary] = *trailing_operator;
  134. // TODO: If this operator is ambiguous with either the ambient precedence
  135. // or the LHS precedence, and there's a variant with a different fixity
  136. // that would work, use that one instead for error recovery.
  137. if (PrecedenceGroup::GetPriority(state.ambient_precedence,
  138. operator_precedence) !=
  139. OperatorPriority::RightFirst) {
  140. // The precedence rules don't permit this operator in this context. Try
  141. // again in the enclosing expression context.
  142. if (state.has_error) {
  143. context.ReturnErrorOnState();
  144. }
  145. return;
  146. }
  147. if (PrecedenceGroup::GetPriority(state.lhs_precedence, operator_precedence) !=
  148. OperatorPriority::LeftFirst) {
  149. // Either the LHS operator and this operator are ambiguous, or the
  150. // LHS operator is a unary operator that can't be nested within
  151. // this operator. Either way, parentheses are required.
  152. context.emitter().Emit(*context.position(), OperatorRequiresParentheses);
  153. state.has_error = true;
  154. } else {
  155. context.DiagnoseOperatorFixity(
  156. is_binary ? ParserContext::OperatorFixity::Infix
  157. : ParserContext::OperatorFixity::Postfix);
  158. }
  159. state.token = context.Consume();
  160. state.lhs_precedence = operator_precedence;
  161. if (is_binary) {
  162. state.state = ParserState::ExpressionLoopForBinary;
  163. context.PushState(state);
  164. context.PushStateForExpression(operator_precedence);
  165. } else {
  166. context.AddNode(ParseNodeKind::PostfixOperator, state.token,
  167. state.subtree_start, state.has_error);
  168. state.has_error = false;
  169. context.PushState(state);
  170. }
  171. }
  172. auto ParserHandleExpressionLoopForBinary(ParserContext& context) -> void {
  173. auto state = context.PopState();
  174. context.AddNode(ParseNodeKind::InfixOperator, state.token,
  175. state.subtree_start, state.has_error);
  176. state.state = ParserState::ExpressionLoop;
  177. state.has_error = false;
  178. context.PushState(state);
  179. }
  180. auto ParserHandleExpressionLoopForPrefix(ParserContext& context) -> void {
  181. auto state = context.PopState();
  182. context.AddNode(ParseNodeKind::PrefixOperator, state.token,
  183. state.subtree_start, state.has_error);
  184. state.state = ParserState::ExpressionLoop;
  185. state.has_error = false;
  186. context.PushState(state);
  187. }
  188. auto ParserHandleExpressionThen(ParserContext& context) -> void {
  189. auto state = context.PopState();
  190. if (context.ConsumeAndAddLeafNodeIf(TokenKind::Then,
  191. ParseNodeKind::IfExpressionThen)) {
  192. context.PushState(ParserState::ExpressionElse);
  193. context.PushStateForExpression(*PrecedenceGroup::ForLeading(TokenKind::If));
  194. } else {
  195. // TODO: Include the location of the `if` token.
  196. CARBON_DIAGNOSTIC(ExpectedThenAfterIf, Error,
  197. "Expected `then` after `if` condition.");
  198. if (!state.has_error) {
  199. context.emitter().Emit(*context.position(), ExpectedThenAfterIf);
  200. }
  201. // Add placeholders for `then expression else expression`.
  202. for (int i = 0; i != 4; ++i) {
  203. context.AddLeafNode(ParseNodeKind::InvalidParse, *context.position(),
  204. /*has_error=*/true);
  205. }
  206. context.ReturnErrorOnState();
  207. }
  208. }
  209. auto ParserHandleExpressionElse(ParserContext& context) -> void {
  210. auto state = context.PopState();
  211. if (context.ConsumeAndAddLeafNodeIf(TokenKind::Else,
  212. ParseNodeKind::IfExpressionElse)) {
  213. context.PushStateForExpression(*PrecedenceGroup::ForLeading(TokenKind::If));
  214. } else {
  215. // TODO: Include the location of the `if` token.
  216. CARBON_DIAGNOSTIC(ExpectedElseAfterIf, Error,
  217. "Expected `else` after `if ... then ...`.");
  218. if (!state.has_error) {
  219. context.emitter().Emit(*context.position(), ExpectedElseAfterIf);
  220. }
  221. // Add placeholders for `else expression`.
  222. for (int i = 0; i != 2; ++i) {
  223. context.AddLeafNode(ParseNodeKind::InvalidParse, *context.position(),
  224. /*has_error=*/true);
  225. }
  226. context.ReturnErrorOnState();
  227. }
  228. }
  229. auto ParserHandleExpressionIfFinish(ParserContext& context) -> void {
  230. auto state = context.PopState();
  231. context.AddNode(ParseNodeKind::IfExpression, state.token, state.subtree_start,
  232. state.has_error);
  233. }
  234. auto ParserHandleExpressionStatementFinish(ParserContext& context) -> void {
  235. auto state = context.PopState();
  236. if (auto semi = context.ConsumeIf(TokenKind::Semi)) {
  237. context.AddNode(ParseNodeKind::ExpressionStatement, *semi,
  238. state.subtree_start, state.has_error);
  239. return;
  240. }
  241. if (!state.has_error) {
  242. CARBON_DIAGNOSTIC(ExpectedSemiAfterExpression, Error,
  243. "Expected `;` after expression.");
  244. context.emitter().Emit(*context.position(), ExpectedSemiAfterExpression);
  245. }
  246. if (auto semi_token = context.SkipPastLikelyEnd(state.token)) {
  247. context.AddNode(ParseNodeKind::ExpressionStatement, *semi_token,
  248. state.subtree_start,
  249. /*has_error=*/true);
  250. return;
  251. }
  252. // Found junk not even followed by a `;`, no node to add.
  253. context.ReturnErrorOnState();
  254. }
  255. } // namespace Carbon