Add this as a self type (#1708)

This commit is contained in:
Jen Basch
2026-09-02 09:04:15 -07:00
committed by GitHub
parent 1208fa9438
commit 2b697015c7
305 changed files with 1487 additions and 561 deletions
@@ -82,6 +82,7 @@ import org.pkl.parser.syntax.Type.NothingType;
import org.pkl.parser.syntax.Type.NullableType;
import org.pkl.parser.syntax.Type.ParenthesizedType;
import org.pkl.parser.syntax.Type.StringConstantType;
import org.pkl.parser.syntax.Type.ThisType;
import org.pkl.parser.syntax.Type.UnionType;
import org.pkl.parser.syntax.Type.UnknownType;
import org.pkl.parser.syntax.TypeAlias;
@@ -107,6 +108,11 @@ public abstract class BaseParserVisitor<T> implements ParserVisitor<T> {
return defaultValue();
}
@Override
public T visitThisType(ThisType type) {
return defaultValue();
}
@Override
public T visitStringConstantType(StringConstantType type) {
return visitChildren(type);
@@ -1161,6 +1161,7 @@ class GenericParserImpl {
case UNKNOWN -> make(NodeType.UNKNOWN_TYPE, next().span);
case NOTHING -> make(NodeType.NOTHING_TYPE, next().span);
case MODULE -> make(NodeType.MODULE_TYPE, next().span);
case THIS -> make(NodeType.THIS_TYPE, next().span);
case LPAREN -> {
var children = new ArrayList<Node>();
children.add(makeTerminal(next()));
@@ -1391,6 +1391,7 @@ final class ParserImpl {
case UNKNOWN -> typ = new Type.UnknownType(next().span);
case NOTHING -> typ = new Type.NothingType(next().span);
case MODULE -> typ = new Type.ModuleType(next().span);
case THIS -> typ = new Type.ThisType(next().span);
case LPAREN -> {
var tk = next();
var children = new ArrayList<Node>();
@@ -1,5 +1,5 @@
/*
* Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved.
* Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -82,6 +82,8 @@ public interface ParserVisitor<Result> {
Result visitModuleType(Type.ModuleType type);
Result visitThisType(Type.ThisType type);
Result visitStringConstantType(Type.StringConstantType type);
Result visitDeclaredType(Type.DeclaredType type);
@@ -61,6 +61,17 @@ public abstract sealed class Type extends AbstractNode {
}
}
public static final class ThisType extends Type {
public ThisType(Span span) {
super(span, List.of());
}
@Override
public <T> T accept(ParserVisitor<T> visitor) {
return visitor.visitThisType(this);
}
}
public static final class StringConstantType extends Type {
public StringConstantType(StringConstant str, Span span) {
super(span, List.of(str));
@@ -135,6 +135,7 @@ public enum NodeType {
UNKNOWN_TYPE(NodeKind.TYPE),
NOTHING_TYPE(NodeKind.TYPE),
MODULE_TYPE(NodeKind.TYPE),
THIS_TYPE(NodeKind.TYPE),
UNION_TYPE(NodeKind.TYPE),
FUNCTION_TYPE(NodeKind.TYPE),
FUNCTION_TYPE_PARAMETERS,
@@ -773,6 +773,10 @@ class SexpRenderer {
buf.append(tab)
buf.append("(moduleType)")
}
is ThisType -> {
buf.append(tab)
buf.append("(thisType)")
}
is StringConstantType -> renderStringConstantType(type)
is DeclaredType -> renderDeclaredType(type)
is ParenthesizedType -> renderParenthesizedType(type)