handle_period.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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(Lex::TokenKind::Identifier,
  18. NodeKind::IdentifierName)) {
  19. // OK, `.` identifier.
  20. } else if (context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::Base,
  21. NodeKind::BaseName)) {
  22. // OK, `.base`.
  23. } else if (node_kind != NodeKind::StructFieldDesignator &&
  24. context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::IntLiteral,
  25. NodeKind::IntLiteral)) {
  26. // OK, '.42'.
  27. } else if (paren_state != State::Invalid &&
  28. context.PositionIs(Lex::TokenKind::OpenParen)) {
  29. state.state = paren_state;
  30. context.PushState(state);
  31. context.PushState(State::OnlyParenExpr);
  32. return;
  33. } else {
  34. CARBON_DIAGNOSTIC(ExpectedIdentifierAfterPeriodOrArrow, Error,
  35. "expected identifier after `{0:->|.}`", BoolAsSelect);
  36. context.emitter().Emit(*context.position(),
  37. ExpectedIdentifierAfterPeriodOrArrow, is_arrow);
  38. // If we see a keyword, assume it was intended to be a name.
  39. // TODO: Should keywords be valid here?
  40. if (context.PositionKind().is_keyword()) {
  41. context.AddLeafNode(NodeKind::IdentifierName, context.Consume(),
  42. /*has_error=*/true);
  43. } else {
  44. context.AddLeafNode(NodeKind::IdentifierName, *context.position(),
  45. /*has_error=*/true);
  46. // Indicate the error to the parent state so that it can avoid producing
  47. // more errors.
  48. context.ReturnErrorOnState();
  49. }
  50. }
  51. context.AddNode(node_kind, dot, state.has_error);
  52. }
  53. auto HandlePeriodAsExpr(Context& context) -> void {
  54. HandlePeriodOrArrow(context, NodeKind::MemberAccessExpr,
  55. State::CompoundMemberAccess,
  56. /*is_arrow=*/false);
  57. }
  58. auto HandlePeriodAsStruct(Context& context) -> void {
  59. HandlePeriodOrArrow(context, NodeKind::StructFieldDesignator, State::Invalid,
  60. /*is_arrow=*/false);
  61. }
  62. auto HandleArrowExpr(Context& context) -> void {
  63. HandlePeriodOrArrow(context, NodeKind::PointerMemberAccessExpr,
  64. State::CompoundPointerMemberAccess,
  65. /*is_arrow=*/true);
  66. }
  67. auto HandleCompoundMemberAccess(Context& context) -> void {
  68. auto state = context.PopState();
  69. context.AddNode(NodeKind::MemberAccessExpr, state.token, state.has_error);
  70. }
  71. auto HandleCompoundPointerMemberAccess(Context& context) -> void {
  72. auto state = context.PopState();
  73. context.AddNode(NodeKind::PointerMemberAccessExpr, state.token,
  74. state.has_error);
  75. }
  76. } // namespace Carbon::Parse