handle_period.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. 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::QualifiedDecl &&
  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 {
  24. CARBON_DIAGNOSTIC(ExpectedIdentifierAfterDotOrArrow, Error,
  25. "Expected identifier after `{0}`.", llvm::StringLiteral);
  26. context.emitter().Emit(
  27. *context.position(), ExpectedIdentifierAfterDotOrArrow,
  28. is_arrow ? llvm::StringLiteral("->") : llvm::StringLiteral("."));
  29. // If we see a keyword, assume it was intended to be a name.
  30. // TODO: Should keywords be valid here?
  31. if (context.PositionKind().is_keyword()) {
  32. context.AddLeafNode(NodeKind::IdentifierName, context.Consume(),
  33. /*has_error=*/true);
  34. } else {
  35. context.AddLeafNode(NodeKind::IdentifierName, *context.position(),
  36. /*has_error=*/true);
  37. // Indicate the error to the parent state so that it can avoid producing
  38. // more errors.
  39. context.ReturnErrorOnState();
  40. }
  41. }
  42. context.AddNode(node_kind, dot, state.subtree_start, state.has_error);
  43. }
  44. auto HandlePeriodAsDecl(Context& context) -> void {
  45. HandlePeriodOrArrow(context, NodeKind::QualifiedDecl,
  46. /*is_arrow=*/false);
  47. }
  48. auto HandlePeriodAsExpr(Context& context) -> void {
  49. HandlePeriodOrArrow(context, NodeKind::MemberAccessExpr,
  50. /*is_arrow=*/false);
  51. }
  52. auto HandlePeriodAsStruct(Context& context) -> void {
  53. HandlePeriodOrArrow(context, NodeKind::StructFieldDesignator,
  54. /*is_arrow=*/false);
  55. }
  56. auto HandleArrowExpr(Context& context) -> void {
  57. HandlePeriodOrArrow(context, NodeKind::PointerMemberAccessExpr,
  58. /*is_arrow=*/true);
  59. }
  60. } // namespace Carbon::Parse