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