Files
carbon-lang/toolchain/parser/parser_handle_package.cpp
T
Jon Ross-Perkins 918c089e03 Add namespace support. (#2940)
This handles namespacing of functions. Parsing and semantics are changed
significantly, while lowering works without changes. Variables can't be
namespaced yet because they're dealing with patterns, and I didn't dig
through that code.

Most of the logic is done through the new name declaration stack, which
is necessary because semantics isn't quite sure where the declaration
name ends. It'd be complex for parsing to send a signal about this,
probably involving node variants and rewrites of the tree, and this
solution seems to work well. Unfortunately this means a new stack, but
that may be inevitable due to the extra information needing to be
tracked.

Note this doesn't deal with scoped lookups of non-namespace things,
which we'll need for generics. That'll probably involve pushing resolved
scopes onto a stack (or maybe just setting a singleton value?) to affect
contextual name lookup. But, I think the basics are there to make it
work when we can test the behavior.

This renames "designator expression" to "qualified expression" and adds
"qualified declaration" in order to use terminology more consistent with
C++.

Namespaces will probably need to be considered for name mangling down
the line, but this still uses the basic name.
2023-07-06 20:43:40 +00:00

91 lines
3.2 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/parser/parser_context.h"
namespace Carbon {
auto ParserHandlePackage(ParserContext& context) -> void {
auto state = context.PopState();
context.AddLeafNode(ParseNodeKind::PackageIntroducer, context.Consume());
auto exit_on_parse_error = [&]() {
auto semi_token = context.SkipPastLikelyEnd(state.token);
return context.AddNode(ParseNodeKind::PackageDirective,
semi_token ? *semi_token : state.token,
state.subtree_start,
/*has_error=*/true);
};
if (!context.ConsumeAndAddLeafNodeIf(TokenKind::Identifier,
ParseNodeKind::Name)) {
CARBON_DIAGNOSTIC(ExpectedIdentifierAfterPackage, Error,
"Expected identifier after `package`.");
context.emitter().Emit(*context.position(), ExpectedIdentifierAfterPackage);
exit_on_parse_error();
return;
}
bool library_parsed = false;
if (auto library_token = context.ConsumeIf(TokenKind::Library)) {
auto library_start = context.tree().size();
if (!context.ConsumeAndAddLeafNodeIf(TokenKind::StringLiteral,
ParseNodeKind::Literal)) {
CARBON_DIAGNOSTIC(
ExpectedLibraryName, Error,
"Expected a string literal to specify the library name.");
context.emitter().Emit(*context.position(), ExpectedLibraryName);
exit_on_parse_error();
return;
}
context.AddNode(ParseNodeKind::PackageLibrary, *library_token,
library_start,
/*has_error=*/false);
library_parsed = true;
}
switch (auto api_or_impl_token =
context.tokens().GetKind(*(context.position()))) {
case TokenKind::Api: {
context.AddLeafNode(ParseNodeKind::PackageApi, context.Consume());
break;
}
case TokenKind::Impl: {
context.AddLeafNode(ParseNodeKind::PackageImpl, context.Consume());
break;
}
default: {
if (!library_parsed && api_or_impl_token == TokenKind::StringLiteral) {
// If we come acroess a string literal and we didn't parse `library
// "..."` yet, then most probably the user forgot to add `library`
// before the library name.
CARBON_DIAGNOSTIC(MissingLibraryKeyword, Error,
"Missing `library` keyword.");
context.emitter().Emit(*context.position(), MissingLibraryKeyword);
} else {
CARBON_DIAGNOSTIC(ExpectedApiOrImpl, Error,
"Expected a `api` or `impl`.");
context.emitter().Emit(*context.position(), ExpectedApiOrImpl);
}
exit_on_parse_error();
return;
}
}
if (!context.PositionIs(TokenKind::Semi)) {
context.EmitExpectedDeclarationSemi(TokenKind::Package);
exit_on_parse_error();
return;
}
context.AddNode(ParseNodeKind::PackageDirective, context.Consume(),
state.subtree_start,
/*has_error=*/false);
}
} // namespace Carbon