Support for functions with a self parameter. (#3338)

So far, such functions can only be defined; calls are not supported yet.
This commit is contained in:
Richard Smith
2023-10-26 21:01:46 +00:00
committed by GitHub
parent e7b3d395b3
commit 04ae5a0531
26 changed files with 510 additions and 66 deletions
+7 -7
View File
@@ -51,17 +51,17 @@ static auto HandlePattern(Context& context, Context::PatternKind pattern_kind)
context.PushState(state);
};
// The first item should be an identifier or, in an implicit parameter list,
// `self`.
// The first item should be an identifier or `self`.
bool has_name = false;
if (auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier)) {
context.AddLeafNode(NodeKind::Name, *identifier);
has_name = true;
} else if (pattern_kind == Context::PatternKind::ImplicitParameter) {
if (auto self = context.ConsumeIf(Lex::TokenKind::SelfValueIdentifier)) {
context.AddLeafNode(NodeKind::SelfValueName, *self);
has_name = true;
}
} else if (auto self =
context.ConsumeIf(Lex::TokenKind::SelfValueIdentifier)) {
// Checking will validate the `self` is only declared in the implicit
// parameter list of a function.
context.AddLeafNode(NodeKind::SelfValueName, *self);
has_name = true;
}
if (!has_name) {
// Add a placeholder for the name.