handle_period.cpp 3.3 KB

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