handle_period.cpp 3.6 KB

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