parser_handle_expression.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. context.PushStateForExpressionLoop(ParserState::ExpressionLoopForPrefix,
  25. state.ambient_precedence,
  26. *operator_precedence);
  27. ++context.position();
  28. context.PushStateForExpression(*operator_precedence);
  29. } else {
  30. context.PushStateForExpressionLoop(ParserState::ExpressionLoop,
  31. state.ambient_precedence,
  32. PrecedenceGroup::ForPostfixExpression());
  33. context.PushState(ParserState::ExpressionInPostfix);
  34. }
  35. }
  36. auto ParserHandleExpressionInPostfix(ParserContext& context) -> void {
  37. auto state = context.PopState();
  38. // Continue to the loop state.
  39. state.state = ParserState::ExpressionInPostfixLoop;
  40. // Parses a primary expression, which is either a terminal portion of an
  41. // expression tree, such as an identifier or literal, or a parenthesized
  42. // expression.
  43. switch (context.PositionKind()) {
  44. case TokenKind::Identifier: {
  45. context.AddLeafNode(ParseNodeKind::NameReference, context.Consume());
  46. context.PushState(state);
  47. break;
  48. }
  49. case TokenKind::IntegerLiteral:
  50. case TokenKind::RealLiteral:
  51. case TokenKind::StringLiteral:
  52. case TokenKind::IntegerTypeLiteral:
  53. case TokenKind::UnsignedIntegerTypeLiteral:
  54. case TokenKind::FloatingPointTypeLiteral:
  55. case TokenKind::StringTypeLiteral: {
  56. context.AddLeafNode(ParseNodeKind::Literal, context.Consume());
  57. context.PushState(state);
  58. break;
  59. }
  60. case TokenKind::OpenCurlyBrace: {
  61. context.PushState(state);
  62. context.PushState(ParserState::BraceExpression);
  63. break;
  64. }
  65. case TokenKind::OpenParen: {
  66. context.PushState(state);
  67. context.PushState(ParserState::ParenExpression);
  68. break;
  69. }
  70. case TokenKind::SelfValueIdentifier: {
  71. context.AddLeafNode(ParseNodeKind::SelfValueIdentifier,
  72. context.Consume());
  73. context.PushState(state);
  74. break;
  75. }
  76. case TokenKind::SelfTypeIdentifier: {
  77. context.AddLeafNode(ParseNodeKind::SelfTypeIdentifier, context.Consume());
  78. context.PushState(state);
  79. break;
  80. }
  81. default: {
  82. // Add a node to keep the parse tree balanced.
  83. context.AddLeafNode(ParseNodeKind::InvalidParse, *context.position(),
  84. /*has_error=*/true);
  85. CARBON_DIAGNOSTIC(ExpectedExpression, Error, "Expected expression.");
  86. context.emitter().Emit(*context.position(), ExpectedExpression);
  87. context.ReturnErrorOnState();
  88. break;
  89. }
  90. }
  91. }
  92. auto ParserHandleExpressionInPostfixLoop(ParserContext& context) -> void {
  93. // This is a cyclic state that repeats, so this state is typically pushed back
  94. // on.
  95. auto state = context.PopState();
  96. state.token = *context.position();
  97. switch (context.PositionKind()) {
  98. case TokenKind::Period: {
  99. context.PushState(state);
  100. state.state = ParserState::DesignatorAsExpression;
  101. context.PushState(state);
  102. break;
  103. }
  104. case TokenKind::OpenParen: {
  105. context.PushState(state);
  106. state.state = ParserState::CallExpression;
  107. context.PushState(state);
  108. break;
  109. }
  110. default: {
  111. if (state.has_error) {
  112. context.ReturnErrorOnState();
  113. }
  114. break;
  115. }
  116. }
  117. }
  118. auto ParserHandleExpressionLoop(ParserContext& context) -> void {
  119. auto state = context.PopState();
  120. auto trailing_operator = PrecedenceGroup::ForTrailing(
  121. context.PositionKind(), context.IsTrailingOperatorInfix());
  122. if (!trailing_operator) {
  123. if (state.has_error) {
  124. context.ReturnErrorOnState();
  125. }
  126. return;
  127. }
  128. auto [operator_precedence, is_binary] = *trailing_operator;
  129. // TODO: If this operator is ambiguous with either the ambient precedence
  130. // or the LHS precedence, and there's a variant with a different fixity
  131. // that would work, use that one instead for error recovery.
  132. if (PrecedenceGroup::GetPriority(state.ambient_precedence,
  133. operator_precedence) !=
  134. OperatorPriority::RightFirst) {
  135. // The precedence rules don't permit this operator in this context. Try
  136. // again in the enclosing expression context.
  137. if (state.has_error) {
  138. context.ReturnErrorOnState();
  139. }
  140. return;
  141. }
  142. if (PrecedenceGroup::GetPriority(state.lhs_precedence, operator_precedence) !=
  143. OperatorPriority::LeftFirst) {
  144. // Either the LHS operator and this operator are ambiguous, or the
  145. // LHS operator is a unary operator that can't be nested within
  146. // this operator. Either way, parentheses are required.
  147. context.emitter().Emit(*context.position(), OperatorRequiresParentheses);
  148. state.has_error = true;
  149. } else {
  150. context.DiagnoseOperatorFixity(
  151. is_binary ? ParserContext::OperatorFixity::Infix
  152. : ParserContext::OperatorFixity::Postfix);
  153. }
  154. state.token = context.Consume();
  155. state.lhs_precedence = operator_precedence;
  156. if (is_binary) {
  157. state.state = ParserState::ExpressionLoopForBinary;
  158. context.PushState(state);
  159. context.PushStateForExpression(operator_precedence);
  160. } else {
  161. context.AddNode(ParseNodeKind::PostfixOperator, state.token,
  162. state.subtree_start, state.has_error);
  163. state.has_error = false;
  164. context.PushState(state);
  165. }
  166. }
  167. auto ParserHandleExpressionLoopForBinary(ParserContext& context) -> void {
  168. auto state = context.PopState();
  169. context.AddNode(ParseNodeKind::InfixOperator, state.token,
  170. state.subtree_start, state.has_error);
  171. state.state = ParserState::ExpressionLoop;
  172. state.has_error = false;
  173. context.PushState(state);
  174. }
  175. auto ParserHandleExpressionLoopForPrefix(ParserContext& context) -> void {
  176. auto state = context.PopState();
  177. context.AddNode(ParseNodeKind::PrefixOperator, state.token,
  178. state.subtree_start, state.has_error);
  179. state.state = ParserState::ExpressionLoop;
  180. state.has_error = false;
  181. context.PushState(state);
  182. }
  183. auto ParserHandleExpressionStatementFinish(ParserContext& context) -> void {
  184. auto state = context.PopState();
  185. if (auto semi = context.ConsumeIf(TokenKind::Semi)) {
  186. context.AddNode(ParseNodeKind::ExpressionStatement, *semi,
  187. state.subtree_start, state.has_error);
  188. return;
  189. }
  190. if (!state.has_error) {
  191. CARBON_DIAGNOSTIC(ExpectedSemiAfterExpression, Error,
  192. "Expected `;` after expression.");
  193. context.emitter().Emit(*context.position(), ExpectedSemiAfterExpression);
  194. }
  195. if (auto semi_token = context.SkipPastLikelyEnd(state.token)) {
  196. context.AddNode(ParseNodeKind::ExpressionStatement, *semi_token,
  197. state.subtree_start,
  198. /*has_error=*/true);
  199. return;
  200. }
  201. // Found junk not even followed by a `;`, no node to add.
  202. context.ReturnErrorOnState();
  203. }
  204. } // namespace Carbon