handle_expr.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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/lex/token_kind.h"
  5. #include "toolchain/parse/context.h"
  6. #include "toolchain/parse/handle.h"
  7. namespace Carbon::Parse {
  8. static auto DiagnoseStatementOperatorAsSubExpr(Context& context) -> void {
  9. CARBON_DIAGNOSTIC(StatementOperatorAsSubExpr, Error,
  10. "operator `{0}` can only be used as a complete statement",
  11. Lex::TokenKind);
  12. context.emitter().Emit(*context.position(), StatementOperatorAsSubExpr,
  13. context.PositionKind());
  14. }
  15. auto HandleExpr(Context& context) -> void {
  16. auto state = context.PopState();
  17. // Check for a prefix operator.
  18. if (auto operator_precedence =
  19. PrecedenceGroup::ForLeading(context.PositionKind())) {
  20. if (PrecedenceGroup::GetPriority(state.ambient_precedence,
  21. *operator_precedence) !=
  22. OperatorPriority::RightFirst) {
  23. // The precedence rules don't permit this prefix operator in this
  24. // context. Diagnose this, but carry on and parse it anyway.
  25. if (PrecedenceGroup::GetPriority(PrecedenceGroup::ForTopLevelExpr(),
  26. *operator_precedence) ==
  27. OperatorPriority::RightFirst) {
  28. CARBON_DIAGNOSTIC(
  29. UnaryOperatorRequiresParentheses, Error,
  30. "parentheses are required around this unary `{0}` operator",
  31. Lex::TokenKind);
  32. context.emitter().Emit(*context.position(),
  33. UnaryOperatorRequiresParentheses,
  34. context.PositionKind());
  35. } else {
  36. // This operator wouldn't be allowed even if parenthesized.
  37. DiagnoseStatementOperatorAsSubExpr(context);
  38. }
  39. } else {
  40. // Check that this operator follows the proper whitespace rules.
  41. context.DiagnoseOperatorFixity(Context::OperatorFixity::Prefix);
  42. }
  43. if (context.PositionIs(Lex::TokenKind::If)) {
  44. context.PushState(StateKind::IfExprFinish);
  45. context.PushState(StateKind::IfExprFinishCondition);
  46. } else {
  47. context.PushStateForExprLoop(StateKind::ExprLoopForPrefixOperator,
  48. state.ambient_precedence,
  49. *operator_precedence);
  50. }
  51. context.ConsumeAndDiscard();
  52. context.PushStateForExpr(*operator_precedence);
  53. } else {
  54. context.PushStateForExprLoop(StateKind::ExprLoop, state.ambient_precedence,
  55. PrecedenceGroup::ForPostfixExpr());
  56. context.PushState(StateKind::ExprInPostfix);
  57. }
  58. }
  59. auto HandleExprInPostfix(Context& context) -> void {
  60. auto state = context.PopState();
  61. // Continue to the loop state.
  62. state.kind = StateKind::ExprInPostfixLoop;
  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 (auto token_kind = context.PositionKind()) {
  67. case Lex::TokenKind::Identifier: {
  68. context.AddLeafNode(NodeKind::IdentifierNameExpr, context.Consume());
  69. context.PushState(state);
  70. break;
  71. }
  72. case Lex::TokenKind::Fn: {
  73. context.PushState(state);
  74. context.PushState(StateKind::LambdaIntroducer);
  75. break;
  76. }
  77. case Lex::TokenKind::False: {
  78. context.AddLeafNode(NodeKind::BoolLiteralFalse, context.Consume());
  79. context.PushState(state);
  80. break;
  81. }
  82. case Lex::TokenKind::True: {
  83. context.AddLeafNode(NodeKind::BoolLiteralTrue, context.Consume());
  84. context.PushState(state);
  85. break;
  86. }
  87. case Lex::TokenKind::CharLiteral: {
  88. context.AddLeafNode(NodeKind::CharLiteral, context.Consume());
  89. context.PushState(state);
  90. break;
  91. }
  92. case Lex::TokenKind::IntLiteral: {
  93. context.AddLeafNode(NodeKind::IntLiteral, context.Consume());
  94. context.PushState(state);
  95. break;
  96. }
  97. case Lex::TokenKind::RealLiteral: {
  98. context.AddLeafNode(NodeKind::RealLiteral, context.Consume());
  99. context.PushState(state);
  100. break;
  101. }
  102. case Lex::TokenKind::StringLiteral: {
  103. context.AddLeafNode(NodeKind::StringLiteral, context.Consume());
  104. context.PushState(state);
  105. break;
  106. }
  107. case Lex::TokenKind::Bool: {
  108. context.AddLeafNode(NodeKind::BoolTypeLiteral, context.Consume());
  109. context.PushState(state);
  110. break;
  111. }
  112. case Lex::TokenKind::Char: {
  113. context.AddLeafNode(NodeKind::CharTypeLiteral, context.Consume());
  114. context.PushState(state);
  115. break;
  116. }
  117. case Lex::TokenKind::IntTypeLiteral: {
  118. context.AddLeafNode(NodeKind::IntTypeLiteral, context.Consume());
  119. context.PushState(state);
  120. break;
  121. }
  122. case Lex::TokenKind::UnsignedIntTypeLiteral: {
  123. context.AddLeafNode(NodeKind::UnsignedIntTypeLiteral, context.Consume());
  124. context.PushState(state);
  125. break;
  126. }
  127. case Lex::TokenKind::FloatTypeLiteral: {
  128. context.AddLeafNode(NodeKind::FloatTypeLiteral, context.Consume());
  129. context.PushState(state);
  130. break;
  131. }
  132. case Lex::TokenKind::Str: {
  133. context.AddLeafNode(NodeKind::StringTypeLiteral, context.Consume());
  134. context.PushState(state);
  135. break;
  136. }
  137. case Lex::TokenKind::Type: {
  138. context.AddLeafNode(NodeKind::TypeTypeLiteral, context.Consume());
  139. context.PushState(state);
  140. break;
  141. }
  142. case Lex::TokenKind::Auto: {
  143. context.AddLeafNode(NodeKind::AutoTypeLiteral, context.Consume());
  144. context.PushState(state);
  145. break;
  146. }
  147. case Lex::TokenKind::OpenCurlyBrace: {
  148. context.PushState(state);
  149. context.PushState(StateKind::BraceExpr);
  150. break;
  151. }
  152. case Lex::TokenKind::OpenParen: {
  153. context.PushState(state);
  154. context.PushState(StateKind::ParenExpr);
  155. break;
  156. }
  157. case Lex::TokenKind::Array: {
  158. context.PushState(state);
  159. context.PushState(StateKind::ArrayExpr);
  160. break;
  161. }
  162. case Lex::TokenKind::Form: {
  163. context.PushState(state);
  164. context.PushState(StateKind::FormLiteral);
  165. break;
  166. }
  167. case Lex::TokenKind::Package: {
  168. context.AddLeafNode(NodeKind::PackageExpr, context.Consume());
  169. if (context.PositionKind() != Lex::TokenKind::Period) {
  170. CARBON_DIAGNOSTIC(ExpectedPeriodAfterPackage, Error,
  171. "expected `.` after `package` expression");
  172. context.emitter().Emit(*context.position(), ExpectedPeriodAfterPackage);
  173. state.has_error = true;
  174. }
  175. context.PushState(state);
  176. break;
  177. }
  178. case Lex::TokenKind::Core: {
  179. context.AddLeafNode(NodeKind::CoreNameExpr, context.Consume());
  180. context.PushState(state);
  181. break;
  182. }
  183. case Lex::TokenKind::Cpp: {
  184. context.AddLeafNode(NodeKind::CppNameExpr, context.Consume());
  185. context.PushState(state);
  186. break;
  187. }
  188. case Lex::TokenKind::SelfValueIdentifier: {
  189. context.AddLeafNode(NodeKind::SelfValueNameExpr, context.Consume());
  190. context.PushState(state);
  191. break;
  192. }
  193. case Lex::TokenKind::SelfTypeIdentifier: {
  194. context.AddLeafNode(NodeKind::SelfTypeNameExpr, context.Consume());
  195. context.PushState(state);
  196. break;
  197. }
  198. case Lex::TokenKind::Period: {
  199. // For periods, we look at the next token to form a designator like
  200. // `.Member` or `.Self`.
  201. auto period = context.Consume();
  202. if (context.ConsumeAndAddLeafNodeIf(
  203. Lex::TokenKind::Identifier,
  204. NodeKind::IdentifierNameNotBeforeSignature)) {
  205. // OK, `.` identifier.
  206. } else if (context.ConsumeAndAddLeafNodeIf(
  207. Lex::TokenKind::SelfTypeIdentifier,
  208. NodeKind::SelfTypeName)) {
  209. // OK, `.Self`.
  210. } else {
  211. CARBON_DIAGNOSTIC(ExpectedIdentifierOrSelfAfterPeriod, Error,
  212. "expected identifier or `Self` after `.`");
  213. context.emitter().Emit(*context.position(),
  214. ExpectedIdentifierOrSelfAfterPeriod);
  215. // Only consume if it is a number or word.
  216. if (context.PositionKind().is_keyword()) {
  217. context.AddLeafNode(NodeKind::IdentifierNameNotBeforeSignature,
  218. context.Consume(), /*has_error=*/true);
  219. } else if (context.PositionIs(Lex::TokenKind::IntLiteral)) {
  220. context.AddInvalidParse(context.Consume());
  221. } else {
  222. context.AddInvalidParse(*context.position());
  223. // Indicate the error to the parent state so that it can avoid
  224. // producing more errors. We only do this on this path where we don't
  225. // consume the token after the period, where we expect further errors
  226. // since we likely haven't recovered.
  227. context.ReturnErrorOnState();
  228. }
  229. state.has_error = true;
  230. }
  231. context.AddNode(NodeKind::DesignatorExpr, period, state.has_error);
  232. context.PushState(state);
  233. break;
  234. }
  235. default: {
  236. // If not already diagnosed in the lexer, diagnose it here.
  237. if (token_kind != Lex::TokenKind::Error) {
  238. CARBON_DIAGNOSTIC(ExpectedExpr, Error, "expected expression");
  239. CARBON_DIAGNOSTIC(ExpectedPattern, Error, "expected pattern");
  240. llvm::SmallVector<StateKind, 2> state_kinds(
  241. llvm::map_range(llvm::ArrayRef(context.state_stack()).take_back(2),
  242. [&](const Context::State& s) { return s.kind; }));
  243. bool in_pattern = state_kinds == llvm::ArrayRef{StateKind::ExprPattern,
  244. StateKind::ExprLoop};
  245. context.emitter().Emit(*context.position(),
  246. in_pattern ? ExpectedPattern : ExpectedExpr);
  247. }
  248. // Add a node to keep the parse tree balanced.
  249. context.AddInvalidParse(*context.position());
  250. context.ReturnErrorOnState();
  251. break;
  252. }
  253. }
  254. }
  255. auto HandleExprInPostfixLoop(Context& context) -> void {
  256. // This is a cyclic state that repeats, so this state is typically pushed back
  257. // on.
  258. auto state = context.PopState();
  259. state.token = *context.position();
  260. switch (context.PositionKind()) {
  261. case Lex::TokenKind::Period: {
  262. context.PushState(state);
  263. context.PushState(state, StateKind::PeriodAsExpr);
  264. break;
  265. }
  266. case Lex::TokenKind::MinusGreater: {
  267. context.PushState(state);
  268. context.PushState(state, StateKind::ArrowExpr);
  269. break;
  270. }
  271. case Lex::TokenKind::OpenParen: {
  272. context.PushState(state);
  273. context.PushState(state, StateKind::CallExpr);
  274. break;
  275. }
  276. case Lex::TokenKind::OpenSquareBracket: {
  277. context.PushState(state);
  278. context.PushState(state, StateKind::IndexExpr);
  279. break;
  280. }
  281. default: {
  282. if (state.has_error) {
  283. context.ReturnErrorOnState();
  284. }
  285. break;
  286. }
  287. }
  288. }
  289. auto HandleExprLoop(Context& context) -> void {
  290. auto state = context.PopState();
  291. auto operator_kind = context.PositionKind();
  292. auto trailing_operator = PrecedenceGroup::ForTrailing(
  293. operator_kind, context.IsTrailingOperatorInfix());
  294. if (!trailing_operator) {
  295. // TODO: Generalize this to handle a sequence of operator modifiers once we
  296. // have more than one.
  297. if (context.PositionIs(Lex::TokenKind::Unsafe)) {
  298. operator_kind = context.PositionKind(Lookahead::NextToken);
  299. trailing_operator = PrecedenceGroup::ForTrailing(
  300. operator_kind, context.IsTrailingOperatorInfix());
  301. }
  302. if (!trailing_operator) {
  303. if (state.has_error) {
  304. context.ReturnErrorOnState();
  305. }
  306. return;
  307. }
  308. }
  309. auto [operator_precedence, is_binary] = *trailing_operator;
  310. // TODO: If this operator is ambiguous with either the ambient precedence
  311. // or the LHS precedence, and there's a variant with a different fixity
  312. // that would work, use that one instead for error recovery.
  313. if (PrecedenceGroup::GetPriority(state.ambient_precedence,
  314. operator_precedence) !=
  315. OperatorPriority::RightFirst) {
  316. // The precedence rules don't permit this operator in this context. Try
  317. // again in the enclosing expression context.
  318. if (state.has_error) {
  319. context.ReturnErrorOnState();
  320. }
  321. return;
  322. }
  323. if (PrecedenceGroup::GetPriority(state.lhs_precedence, operator_precedence) !=
  324. OperatorPriority::LeftFirst) {
  325. // Either the LHS operator and this operator are ambiguous, or the
  326. // LHS operator is a unary operator that can't be nested within
  327. // this operator. Either way, parentheses are required.
  328. if (PrecedenceGroup::GetPriority(PrecedenceGroup::ForTopLevelExpr(),
  329. operator_precedence) ==
  330. OperatorPriority::RightFirst) {
  331. CARBON_DIAGNOSTIC(
  332. OperatorRequiresParentheses, Error,
  333. "parentheses are required to disambiguate operator precedence");
  334. context.emitter().Emit(*context.position(), OperatorRequiresParentheses);
  335. } else {
  336. // This operator wouldn't be allowed even if parenthesized.
  337. DiagnoseStatementOperatorAsSubExpr(context);
  338. }
  339. state.has_error = true;
  340. } else {
  341. context.DiagnoseOperatorFixity(is_binary
  342. ? Context::OperatorFixity::Infix
  343. : Context::OperatorFixity::Postfix);
  344. }
  345. // For operator modifiers, wrap the first operand in the modifier.
  346. if (context.PositionIs(Lex::TokenKind::Unsafe)) {
  347. if (context.PositionIs(Lex::TokenKind::As, Lookahead::NextToken)) {
  348. context.AddNode<NodeKind::UnsafeModifier>(context.Consume(),
  349. state.has_error);
  350. } else {
  351. CARBON_DIAGNOSTIC(ModifierNotAllowedOnOperator, Error,
  352. "`{0}` not allowed on operator `{1}`", Lex::TokenKind,
  353. Lex::TokenKind);
  354. context.emitter().Emit(*context.position(), ModifierNotAllowedOnOperator,
  355. context.PositionKind(),
  356. context.PositionKind(Lookahead::NextToken));
  357. context.Consume();
  358. state.has_error = true;
  359. }
  360. }
  361. state.token = context.Consume();
  362. state.lhs_precedence = operator_precedence;
  363. if (is_binary) {
  364. switch (operator_kind) {
  365. // For `and` and `or`, wrap the first operand in a virtual parse tree
  366. // node so that checking can insert control flow here.
  367. case Lex::TokenKind::And:
  368. context.AddNode(NodeKind::ShortCircuitOperandAnd, state.token,
  369. state.has_error);
  370. state.kind = StateKind::ExprLoopForShortCircuitOperatorAsAnd;
  371. break;
  372. case Lex::TokenKind::Or:
  373. context.AddNode(NodeKind::ShortCircuitOperandOr, state.token,
  374. state.has_error);
  375. state.kind = StateKind::ExprLoopForShortCircuitOperatorAsOr;
  376. break;
  377. // `where` also needs a virtual parse tree node, and parses its right
  378. // argument in a mode where it can handle requirement operators like
  379. // `impls` and `=`.
  380. case Lex::TokenKind::Where:
  381. context.AddNode(NodeKind::WhereOperand, state.token, state.has_error);
  382. context.PushState(state, StateKind::WhereFinish);
  383. context.PushState(StateKind::RequirementBegin);
  384. return;
  385. default:
  386. state.kind = StateKind::ExprLoopForInfixOperator;
  387. break;
  388. }
  389. context.PushState(state);
  390. context.PushStateForExpr(operator_precedence);
  391. } else {
  392. NodeKind node_kind;
  393. switch (operator_kind) {
  394. #define CARBON_PARSE_NODE_KIND(Name)
  395. #define CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR(Name) \
  396. case Lex::TokenKind::Name: \
  397. node_kind = NodeKind::PostfixOperator##Name; \
  398. break;
  399. #include "toolchain/parse/node_kind.def"
  400. default:
  401. CARBON_FATAL("Unexpected token kind for postfix operator: {0}",
  402. operator_kind);
  403. }
  404. context.AddNode(node_kind, state.token, state.has_error);
  405. state.has_error = false;
  406. context.PushState(state);
  407. }
  408. }
  409. // Adds the operator node and returns the main expression loop.
  410. static auto HandleExprLoopForOperator(Context& context, Context::State state,
  411. NodeKind node_kind) -> void {
  412. context.AddNode(node_kind, state.token, state.has_error);
  413. state.has_error = false;
  414. context.PushState(state, StateKind::ExprLoop);
  415. }
  416. auto HandleExprLoopForInfixOperator(Context& context) -> void {
  417. auto state = context.PopState();
  418. switch (auto token_kind = context.tokens().GetKind(state.token)) {
  419. #define CARBON_PARSE_NODE_KIND(Name)
  420. #define CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Name) \
  421. case Lex::TokenKind::Name: \
  422. HandleExprLoopForOperator(context, state, NodeKind::InfixOperator##Name); \
  423. break;
  424. #include "toolchain/parse/node_kind.def"
  425. default:
  426. CARBON_FATAL("Unexpected token kind for infix operator: {0}", token_kind);
  427. }
  428. }
  429. auto HandleExprLoopForPrefixOperator(Context& context) -> void {
  430. auto state = context.PopState();
  431. switch (auto token_kind = context.tokens().GetKind(state.token)) {
  432. #define CARBON_PARSE_NODE_KIND(Name)
  433. #define CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Name) \
  434. case Lex::TokenKind::Name: \
  435. HandleExprLoopForOperator(context, state, NodeKind::PrefixOperator##Name); \
  436. break;
  437. #include "toolchain/parse/node_kind.def"
  438. default:
  439. CARBON_FATAL("Unexpected token kind for prefix operator: {0}",
  440. token_kind);
  441. }
  442. }
  443. auto HandleExprLoopForShortCircuitOperatorAsAnd(Context& context) -> void {
  444. auto state = context.PopState();
  445. HandleExprLoopForOperator(context, state, NodeKind::ShortCircuitOperatorAnd);
  446. }
  447. auto HandleExprLoopForShortCircuitOperatorAsOr(Context& context) -> void {
  448. auto state = context.PopState();
  449. HandleExprLoopForOperator(context, state, NodeKind::ShortCircuitOperatorOr);
  450. }
  451. auto HandleExprStatementFinish(Context& context) -> void {
  452. auto state = context.PopState();
  453. if (auto semi = context.ConsumeIf(Lex::TokenKind::Semi)) {
  454. context.AddNode(NodeKind::ExprStatement, *semi, state.has_error);
  455. return;
  456. }
  457. if (!state.has_error) {
  458. CARBON_DIAGNOSTIC(ExpectedExprSemi, Error,
  459. "expected `;` after expression statement");
  460. context.emitter().Emit(*context.position(), ExpectedExprSemi);
  461. }
  462. context.AddNode(NodeKind::ExprStatement,
  463. context.SkipPastLikelyEnd(state.token), /*has_error=*/true);
  464. }
  465. } // namespace Carbon::Parse