parser_handle_package.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. auto ParserHandlePackage(ParserContext& context) -> void {
  7. auto state = context.PopState();
  8. context.AddLeafNode(ParseNodeKind::PackageIntroducer, context.Consume());
  9. auto exit_on_parse_error = [&]() {
  10. auto semi_token = context.SkipPastLikelyEnd(state.token);
  11. return context.AddNode(ParseNodeKind::PackageDirective,
  12. semi_token ? *semi_token : state.token,
  13. state.subtree_start,
  14. /*has_error=*/true);
  15. };
  16. if (!context.ConsumeAndAddLeafNodeIf(TokenKind::Identifier,
  17. ParseNodeKind::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(TokenKind::Library)) {
  26. auto library_start = context.tree().size();
  27. if (!context.ConsumeAndAddLeafNodeIf(TokenKind::StringLiteral,
  28. ParseNodeKind::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(ParseNodeKind::PackageLibrary, *library_token,
  37. library_start,
  38. /*has_error=*/false);
  39. library_parsed = true;
  40. }
  41. switch (auto api_or_impl_token =
  42. context.tokens().GetKind(*(context.position()))) {
  43. case TokenKind::Api: {
  44. context.AddLeafNode(ParseNodeKind::PackageApi, context.Consume());
  45. break;
  46. }
  47. case TokenKind::Impl: {
  48. context.AddLeafNode(ParseNodeKind::PackageImpl, context.Consume());
  49. break;
  50. }
  51. default: {
  52. if (!library_parsed && api_or_impl_token == 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(TokenKind::Semi)) {
  69. CARBON_DIAGNOSTIC(ExpectedSemiToEndPackageDirective, Error,
  70. "Expected `;` to end package directive.");
  71. context.emitter().Emit(*context.position(),
  72. ExpectedSemiToEndPackageDirective);
  73. exit_on_parse_error();
  74. return;
  75. }
  76. context.AddNode(ParseNodeKind::PackageDirective, context.Consume(),
  77. state.subtree_start,
  78. /*has_error=*/false);
  79. }
  80. } // namespace Carbon