parser_handle_period.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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/parser/parser_context.h"
  5. namespace Carbon {
  6. // Handles PeriodAs variants.
  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. auto ParserHandlePeriod(ParserContext& context, ParseNodeKind node_kind)
  10. -> void {
  11. auto state = context.PopState();
  12. // `.` identifier
  13. auto dot = context.ConsumeChecked(TokenKind::Period);
  14. if (!context.ConsumeAndAddLeafNodeIf(TokenKind::Identifier,
  15. ParseNodeKind::Name)) {
  16. CARBON_DIAGNOSTIC(ExpectedIdentifierAfterDot, Error,
  17. "Expected identifier after `.`.");
  18. context.emitter().Emit(*context.position(), ExpectedIdentifierAfterDot);
  19. // If we see a keyword, assume it was intended to be a name.
  20. // TODO: Should keywords be valid here?
  21. if (context.PositionKind().is_keyword()) {
  22. context.AddLeafNode(ParseNodeKind::Name, context.Consume(),
  23. /*has_error=*/true);
  24. } else {
  25. context.AddLeafNode(ParseNodeKind::Name, *context.position(),
  26. /*has_error=*/true);
  27. // Indicate the error to the parent state so that it can avoid producing
  28. // more errors.
  29. context.ReturnErrorOnState();
  30. }
  31. }
  32. context.AddNode(node_kind, dot, state.subtree_start, state.has_error);
  33. }
  34. auto ParserHandlePeriodAsDeclaration(ParserContext& context) -> void {
  35. ParserHandlePeriod(context, ParseNodeKind::QualifiedDeclaration);
  36. }
  37. auto ParserHandlePeriodAsExpression(ParserContext& context) -> void {
  38. ParserHandlePeriod(context, ParseNodeKind::MemberAccessExpression);
  39. }
  40. auto ParserHandlePeriodAsStruct(ParserContext& context) -> void {
  41. ParserHandlePeriod(context, ParseNodeKind::StructFieldDesignator);
  42. }
  43. } // namespace Carbon