mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 18:51:11 +01:00
Add initial parsing for 'extern library' (#4173)
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user