parser_handle_expression.cpp 7.6 KB

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