handle_period.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/parse/context.h"
  5. namespace Carbon::Parse {
  6. // Handles PeriodAs variants and ArrowExpr.
  7. // TODO: This currently only supports identifiers on the rhs, but will in the
  8. // future need to handle things like `object.(Interface.member)` for qualifiers.
  9. static auto HandlePeriodOrArrow(Context& context, NodeKind node_kind,
  10. State paren_state, bool is_arrow) -> void {
  11. auto state = context.PopState();
  12. // We're handling `.something` or `->something`.
  13. auto dot = context.ConsumeChecked(is_arrow ? Lex::TokenKind::MinusGreater
  14. : Lex::TokenKind::Period);
  15. if (context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::Identifier,
  16. NodeKind::IdentifierName)) {
  17. // OK, `.` identifier.
  18. } else if (node_kind != NodeKind::QualifiedName &&
  19. context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::Base,
  20. NodeKind::BaseName)) {
  21. // OK, `.base`. This is allowed in any name context other than declaring a
  22. // new qualified name: `fn Namespace.base() {}`
  23. } else if (paren_state != State::Invalid &&
  24. context.PositionIs(Lex::TokenKind::OpenParen)) {
  25. state.state = paren_state;
  26. context.PushState(state);
  27. context.PushState(State::ParenExpr);
  28. return;
  29. } else {
  30. CARBON_DIAGNOSTIC(ExpectedIdentifierAfterDotOrArrow, Error,
  31. "Expected identifier after `{0}`.", llvm::StringLiteral);
  32. context.emitter().Emit(
  33. *context.position(), ExpectedIdentifierAfterDotOrArrow,
  34. is_arrow ? llvm::StringLiteral("->") : llvm::StringLiteral("."));
  35. // If we see a keyword, assume it was intended to be a name.
  36. // TODO: Should keywords be valid here?
  37. if (context.PositionKind().is_keyword()) {
  38. context.AddLeafNode(NodeKind::IdentifierName, context.Consume(),
  39. /*has_error=*/true);
  40. } else {
  41. context.AddLeafNode(NodeKind::IdentifierName, *context.position(),
  42. /*has_error=*/true);
  43. // Indicate the error to the parent state so that it can avoid producing
  44. // more errors.
  45. context.ReturnErrorOnState();
  46. }
  47. }
  48. context.AddNode(node_kind, dot, state.subtree_start, state.has_error);
  49. }
  50. auto HandlePeriodAsDecl(Context& context) -> void {
  51. HandlePeriodOrArrow(context, NodeKind::QualifiedName, State::Invalid,
  52. /*is_arrow=*/false);
  53. }
  54. auto HandlePeriodAsExpr(Context& context) -> void {
  55. HandlePeriodOrArrow(context, NodeKind::MemberAccessExpr,
  56. State::CompoundMemberAccess,
  57. /*is_arrow=*/false);
  58. }
  59. auto HandlePeriodAsStruct(Context& context) -> void {
  60. HandlePeriodOrArrow(context, NodeKind::StructFieldDesignator, State::Invalid,
  61. /*is_arrow=*/false);
  62. }
  63. auto HandleArrowExpr(Context& context) -> void {
  64. HandlePeriodOrArrow(context, NodeKind::PointerMemberAccessExpr,
  65. State::CompoundPointerMemberAccess,
  66. /*is_arrow=*/true);
  67. }
  68. auto HandleCompoundMemberAccess(Context& context) -> void {
  69. auto state = context.PopState();
  70. context.AddNode(NodeKind::MemberAccessExpr, state.token, state.subtree_start,
  71. state.has_error);
  72. }
  73. auto HandleCompoundPointerMemberAccess(Context& context) -> void {
  74. auto state = context.PopState();
  75. context.AddNode(NodeKind::PointerMemberAccessExpr, state.token,
  76. state.subtree_start, state.has_error);
  77. }
  78. } // namespace Carbon::Parse