mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
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.
90 lines
2.8 KiB
C++
90 lines
2.8 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 {
|
|
|
|
// Handles VarAs(Semicolon|For).
|
|
static auto ParserHandleVar(ParserContext& context, ParserState finish_state)
|
|
-> void {
|
|
context.PopAndDiscardState();
|
|
|
|
// These will start at the `var`.
|
|
context.PushState(finish_state);
|
|
context.PushState(ParserState::VarAfterPattern);
|
|
|
|
context.AddLeafNode(ParseNodeKind::VariableIntroducer, context.Consume());
|
|
|
|
// This will start at the pattern.
|
|
context.PushState(ParserState::PatternAsVariable);
|
|
}
|
|
|
|
auto ParserHandleVarAsSemicolon(ParserContext& context) -> void {
|
|
ParserHandleVar(context, ParserState::VarFinishAsSemicolon);
|
|
}
|
|
|
|
auto ParserHandleVarAsFor(ParserContext& context) -> void {
|
|
ParserHandleVar(context, ParserState::VarFinishAsFor);
|
|
}
|
|
|
|
auto ParserHandleVarAfterPattern(ParserContext& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
if (state.has_error) {
|
|
if (auto after_pattern =
|
|
context.FindNextOf({TokenKind::Equal, TokenKind::Semi})) {
|
|
context.SkipTo(*after_pattern);
|
|
}
|
|
}
|
|
|
|
if (auto equals = context.ConsumeIf(TokenKind::Equal)) {
|
|
context.AddLeafNode(ParseNodeKind::VariableInitializer, *equals);
|
|
context.PushState(ParserState::Expression);
|
|
}
|
|
}
|
|
|
|
auto ParserHandleVarFinishAsSemicolon(ParserContext& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
auto end_token = state.token;
|
|
if (context.PositionIs(TokenKind::Semi)) {
|
|
end_token = context.Consume();
|
|
} else {
|
|
// TODO: Disambiguate between statement and member declaration.
|
|
context.EmitExpectedDeclarationSemi(TokenKind::Var);
|
|
state.has_error = true;
|
|
if (auto semi_token = context.SkipPastLikelyEnd(state.token)) {
|
|
end_token = *semi_token;
|
|
}
|
|
}
|
|
context.AddNode(ParseNodeKind::VariableDeclaration, end_token,
|
|
state.subtree_start, state.has_error);
|
|
}
|
|
|
|
auto ParserHandleVarFinishAsFor(ParserContext& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
auto end_token = state.token;
|
|
if (context.PositionIs(TokenKind::In)) {
|
|
end_token = context.Consume();
|
|
} else if (context.PositionIs(TokenKind::Colon)) {
|
|
CARBON_DIAGNOSTIC(ExpectedInNotColon, Error,
|
|
"`:` should be replaced by `in`.");
|
|
context.emitter().Emit(*context.position(), ExpectedInNotColon);
|
|
state.has_error = true;
|
|
end_token = context.Consume();
|
|
} else {
|
|
CARBON_DIAGNOSTIC(ExpectedIn, Error,
|
|
"Expected `in` after loop `var` declaration.");
|
|
context.emitter().Emit(*context.position(), ExpectedIn);
|
|
state.has_error = true;
|
|
}
|
|
|
|
context.AddNode(ParseNodeKind::ForIn, end_token, state.subtree_start,
|
|
state.has_error);
|
|
}
|
|
|
|
} // namespace Carbon
|