handle_package.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. auto HandlePackage(Context& context) -> void {
  7. auto state = context.PopState();
  8. context.AddLeafNode(NodeKind::PackageIntroducer, context.Consume());
  9. auto exit_on_parse_error = [&]() {
  10. auto semi_token = context.SkipPastLikelyEnd(state.token);
  11. return context.AddNode(NodeKind::PackageDirective,
  12. semi_token ? *semi_token : state.token,
  13. state.subtree_start,
  14. /*has_error=*/true);
  15. };
  16. if (!context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::Identifier,
  17. NodeKind::Name)) {
  18. CARBON_DIAGNOSTIC(ExpectedIdentifierAfterPackage, Error,
  19. "Expected identifier after `package`.");
  20. context.emitter().Emit(*context.position(), ExpectedIdentifierAfterPackage);
  21. exit_on_parse_error();
  22. return;
  23. }
  24. bool library_parsed = false;
  25. if (auto library_token = context.ConsumeIf(Lex::TokenKind::Library)) {
  26. auto library_start = context.tree().size();
  27. if (!context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::StringLiteral,
  28. NodeKind::Literal)) {
  29. CARBON_DIAGNOSTIC(
  30. ExpectedLibraryName, Error,
  31. "Expected a string literal to specify the library name.");
  32. context.emitter().Emit(*context.position(), ExpectedLibraryName);
  33. exit_on_parse_error();
  34. return;
  35. }
  36. context.AddNode(NodeKind::PackageLibrary, *library_token, library_start,
  37. /*has_error=*/false);
  38. library_parsed = true;
  39. }
  40. switch (auto api_or_impl_token =
  41. context.tokens().GetKind(*(context.position()))) {
  42. case Lex::TokenKind::Api: {
  43. context.AddLeafNode(NodeKind::PackageApi, context.Consume());
  44. break;
  45. }
  46. case Lex::TokenKind::Impl: {
  47. context.AddLeafNode(NodeKind::PackageImpl, context.Consume());
  48. break;
  49. }
  50. default: {
  51. if (!library_parsed &&
  52. api_or_impl_token == Lex::TokenKind::StringLiteral) {
  53. // If we come acroess a string literal and we didn't parse `library
  54. // "..."` yet, then most probably the user forgot to add `library`
  55. // before the library name.
  56. CARBON_DIAGNOSTIC(MissingLibraryKeyword, Error,
  57. "Missing `library` keyword.");
  58. context.emitter().Emit(*context.position(), MissingLibraryKeyword);
  59. } else {
  60. CARBON_DIAGNOSTIC(ExpectedApiOrImpl, Error,
  61. "Expected a `api` or `impl`.");
  62. context.emitter().Emit(*context.position(), ExpectedApiOrImpl);
  63. }
  64. exit_on_parse_error();
  65. return;
  66. }
  67. }
  68. if (!context.PositionIs(Lex::TokenKind::Semi)) {
  69. context.EmitExpectedDeclarationSemi(Lex::TokenKind::Package);
  70. exit_on_parse_error();
  71. return;
  72. }
  73. context.AddNode(NodeKind::PackageDirective, context.Consume(),
  74. state.subtree_start,
  75. /*has_error=*/false);
  76. }
  77. } // namespace Carbon::Parse