handle_period.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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:->|.}`", BoolAsSelect);
  37. context.emitter().Emit(*context.position(),
  38. ExpectedIdentifierAfterPeriodOrArrow, is_arrow);
  39. // If we see a keyword, assume it was intended to be a name.
  40. // TODO: Should keywords be valid here?
  41. if (context.PositionKind().is_keyword()) {
  42. context.AddLeafNode(NodeKind::IdentifierNameNotBeforeParams,
  43. context.Consume(), /*has_error=*/true);
  44. } else {
  45. context.AddLeafNode(NodeKind::IdentifierNameNotBeforeParams,
  46. *context.position(), /*has_error=*/true);
  47. // Indicate the error to the parent state so that it can avoid producing
  48. // more errors.
  49. context.ReturnErrorOnState();
  50. }
  51. }
  52. context.AddNode(node_kind, dot, state.has_error);
  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.has_error);
  71. }
  72. auto HandleCompoundPointerMemberAccess(Context& context) -> void {
  73. auto state = context.PopState();
  74. context.AddNode(NodeKind::PointerMemberAccessExpr, state.token,
  75. state.has_error);
  76. }
  77. } // namespace Carbon::Parse