Add initial parsing for 'extern library' (#4173)

This commit is contained in:
Jon Ross-Perkins
2024-07-26 20:47:25 +00:00
committed by GitHub
parent 37a8bfa488
commit ae675e61bd
14 changed files with 268 additions and 59 deletions
+37
View File
@@ -437,6 +437,43 @@ auto Context::RecoverFromDeclError(StateStackEntry state, NodeKind node_kind,
/*has_error=*/true);
}
auto Context::ParseLibraryName(bool accept_default)
-> std::optional<StringLiteralValueId> {
if (auto library_name_token = ConsumeIf(Lex::TokenKind::StringLiteral)) {
AddLeafNode(NodeKind::LibraryName, *library_name_token);
return tokens().GetStringLiteralValue(*library_name_token);
}
if (accept_default) {
if (auto default_token = ConsumeIf(Lex::TokenKind::Default)) {
AddLeafNode(NodeKind::DefaultLibrary, *default_token);
return StringLiteralValueId::Invalid;
}
}
CARBON_DIAGNOSTIC(
ExpectedLibraryNameOrDefault, Error,
"Expected `default` or a string literal to specify the library name.");
CARBON_DIAGNOSTIC(ExpectedLibraryName, Error,
"Expected a string literal to specify the library name.");
emitter().Emit(*position(), accept_default ? ExpectedLibraryNameOrDefault
: ExpectedLibraryName);
return std::nullopt;
}
auto Context::ParseLibrarySpecifier(bool accept_default)
-> std::optional<StringLiteralValueId> {
auto library_token = ConsumeChecked(Lex::TokenKind::Library);
auto library_subtree_start = tree().size();
auto library_id = ParseLibraryName(accept_default);
if (!library_id) {
AddLeafNode(NodeKind::LibraryName, *position_, /*has_error=*/true);
}
AddNode(NodeKind::LibrarySpecifier, library_token, library_subtree_start,
/*has_error=*/false);
return library_id;
}
auto Context::DiagnoseExpectedDeclSemi(Lex::TokenKind expected_kind) -> void {
CARBON_DIAGNOSTIC(ExpectedDeclSemi, Error,
"`{0}` declarations must end with a `;`.", Lex::TokenKind);