implements PLN-0035 pbs inline type hint payload production
This commit is contained in:
+1
@@ -343,6 +343,7 @@ public final class PbsAst {
|
||||
public record LetStatement(
|
||||
boolean isConst,
|
||||
String name,
|
||||
Span nameSpan,
|
||||
TypeRef explicitType,
|
||||
Expression initializer,
|
||||
Span span) implements Statement {
|
||||
|
||||
+1
@@ -118,6 +118,7 @@ final class PbsBlockParser {
|
||||
return new PbsAst.LetStatement(
|
||||
isConst,
|
||||
name.lexeme(),
|
||||
context.span(name.start(), name.end()),
|
||||
explicitType,
|
||||
initializer,
|
||||
context.span(letToken.start(), semicolon.end()));
|
||||
|
||||
+62
-8
@@ -5,9 +5,12 @@ import p.studio.compiler.pbs.ast.PbsAst;
|
||||
import p.studio.compiler.pbs.semantics.PbsFlowSemanticSupport.Model;
|
||||
import p.studio.compiler.pbs.semantics.PbsFlowSemanticSupport.Scope;
|
||||
import p.studio.compiler.pbs.semantics.PbsFlowSemanticSupport.TypeView;
|
||||
import p.studio.compiler.services.PbsInlineHintSurface;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
final class PbsFlowBodyAnalyzer {
|
||||
private final PbsFlowTypeOps typeOps = new PbsFlowTypeOps();
|
||||
private final PbsFlowExpressionAnalyzer expressionAnalyzer = new PbsFlowExpressionAnalyzer(typeOps);
|
||||
@@ -16,24 +19,75 @@ final class PbsFlowBodyAnalyzer {
|
||||
typeOps,
|
||||
expressionAnalyzer,
|
||||
this::analyzeBlock);
|
||||
private final PbsFlowStatementAnalyzer statementAnalyzer = new PbsFlowStatementAnalyzer(
|
||||
typeOps,
|
||||
expressionAnalyzer,
|
||||
assignmentAnalyzer::analyzeAssignmentStatement);
|
||||
private final PbsFlowCallableBodyAnalyzer callableBodyAnalyzer = new PbsFlowCallableBodyAnalyzer(
|
||||
typeOps,
|
||||
completionAnalyzer,
|
||||
this::analyzeBlock);
|
||||
private PbsFlowStatementAnalyzer.InlineHintCollector inlineHintCollector = PbsFlowStatementAnalyzer.InlineHintCollector.noop();
|
||||
|
||||
public void validate(
|
||||
final PbsAst.File ast,
|
||||
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
|
||||
final FESurfaceContext feSurfaceContext,
|
||||
final DiagnosticSink diagnostics) {
|
||||
final var model = Model.from(ast, supplementalTopDecls, feSurfaceContext, diagnostics);
|
||||
final var previousCollector = inlineHintCollector;
|
||||
inlineHintCollector = PbsFlowStatementAnalyzer.InlineHintCollector.noop();
|
||||
try {
|
||||
final var model = Model.from(ast, supplementalTopDecls, feSurfaceContext, diagnostics);
|
||||
|
||||
for (final var topDecl : ast.topDecls()) {
|
||||
validateTopDecl(topDecl, model, diagnostics);
|
||||
for (final var topDecl : ast.topDecls()) {
|
||||
validateTopDecl(topDecl, model, diagnostics);
|
||||
}
|
||||
} finally {
|
||||
inlineHintCollector = previousCollector;
|
||||
}
|
||||
}
|
||||
|
||||
public ReadOnlyList<PbsInlineHintSurface> collectInlineHints(
|
||||
final PbsAst.File ast,
|
||||
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
|
||||
final FESurfaceContext feSurfaceContext,
|
||||
final DiagnosticSink diagnostics) {
|
||||
final var collector = new InlineHintsCollector();
|
||||
final var previousCollector = inlineHintCollector;
|
||||
inlineHintCollector = collector;
|
||||
try {
|
||||
final var model = Model.from(ast, supplementalTopDecls, feSurfaceContext, diagnostics);
|
||||
|
||||
for (final var topDecl : ast.topDecls()) {
|
||||
validateTopDecl(topDecl, model, diagnostics);
|
||||
}
|
||||
} finally {
|
||||
inlineHintCollector = previousCollector;
|
||||
}
|
||||
return collector.hints();
|
||||
}
|
||||
|
||||
private PbsFlowStatementAnalyzer statementAnalyzer() {
|
||||
return new PbsFlowStatementAnalyzer(
|
||||
typeOps,
|
||||
expressionAnalyzer,
|
||||
assignmentAnalyzer::analyzeAssignmentStatement,
|
||||
inlineHintCollector);
|
||||
}
|
||||
|
||||
private static final class InlineHintsCollector implements PbsFlowStatementAnalyzer.InlineHintCollector {
|
||||
private static final String TYPE_HINT_CATEGORY = "type";
|
||||
|
||||
private final ArrayList<PbsInlineHintSurface> hints = new ArrayList<>();
|
||||
private final PbsInlineHintSurfaceFormatter formatter = new PbsInlineHintSurfaceFormatter();
|
||||
|
||||
@Override
|
||||
public void collect(final PbsAst.LetStatement letStatement, final TypeView inferredType) {
|
||||
final var label = formatter.format(inferredType);
|
||||
if (label == null || letStatement.nameSpan() == null || letStatement.nameSpan().isNone()) {
|
||||
return;
|
||||
}
|
||||
hints.add(new PbsInlineHintSurface(letStatement.nameSpan(), label, TYPE_HINT_CATEGORY));
|
||||
}
|
||||
|
||||
ReadOnlyList<PbsInlineHintSurface> hints() {
|
||||
return ReadOnlyList.wrap(hints);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,6 +248,6 @@ final class PbsFlowBodyAnalyzer {
|
||||
final PbsAst.Block block,
|
||||
final PbsFlowBodyContext context,
|
||||
final boolean valueContext) {
|
||||
return statementAnalyzer.analyzeBlock(block, context, valueContext);
|
||||
return statementAnalyzer().analyzeBlock(block, context, valueContext);
|
||||
}
|
||||
}
|
||||
|
||||
+9
@@ -2,6 +2,7 @@ package p.studio.compiler.pbs.semantics;
|
||||
|
||||
import p.studio.compiler.messages.FESurfaceContext;
|
||||
import p.studio.compiler.pbs.ast.PbsAst;
|
||||
import p.studio.compiler.services.PbsInlineHintSurface;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
@@ -19,4 +20,12 @@ public final class PbsFlowSemanticsValidator {
|
||||
final DiagnosticSink diagnostics) {
|
||||
flowBodyAnalyzer.validate(ast, supplementalTopDecls, feSurfaceContext, diagnostics);
|
||||
}
|
||||
|
||||
public ReadOnlyList<PbsInlineHintSurface> collectInlineHints(
|
||||
final PbsAst.File ast,
|
||||
final ReadOnlyList<PbsAst.TopDecl> supplementalTopDecls,
|
||||
final FESurfaceContext feSurfaceContext,
|
||||
final DiagnosticSink diagnostics) {
|
||||
return flowBodyAnalyzer.collectInlineHints(ast, supplementalTopDecls, feSurfaceContext, diagnostics);
|
||||
}
|
||||
}
|
||||
|
||||
+19
-2
@@ -11,17 +11,30 @@ final class PbsFlowStatementAnalyzer {
|
||||
void analyze(PbsAst.AssignStatement assignStatement, PbsFlowBodyContext context);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
interface InlineHintCollector {
|
||||
void collect(PbsAst.LetStatement letStatement, TypeView inferredType);
|
||||
|
||||
static InlineHintCollector noop() {
|
||||
return (letStatement, inferredType) -> {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private final PbsFlowTypeOps typeOps;
|
||||
private final PbsFlowExpressionAnalyzer expressionAnalyzer;
|
||||
private final AssignmentStatementAnalyzer assignmentStatementAnalyzer;
|
||||
private final InlineHintCollector inlineHintCollector;
|
||||
|
||||
PbsFlowStatementAnalyzer(
|
||||
final PbsFlowTypeOps typeOps,
|
||||
final PbsFlowExpressionAnalyzer expressionAnalyzer,
|
||||
final AssignmentStatementAnalyzer assignmentStatementAnalyzer) {
|
||||
final AssignmentStatementAnalyzer assignmentStatementAnalyzer,
|
||||
final InlineHintCollector inlineHintCollector) {
|
||||
this.typeOps = typeOps;
|
||||
this.expressionAnalyzer = expressionAnalyzer;
|
||||
this.assignmentStatementAnalyzer = assignmentStatementAnalyzer;
|
||||
this.inlineHintCollector = inlineHintCollector;
|
||||
}
|
||||
|
||||
TypeView analyzeBlock(
|
||||
@@ -96,7 +109,11 @@ final class PbsFlowStatementAnalyzer {
|
||||
ExprUse.VALUE,
|
||||
true,
|
||||
this::analyzeNestedBlock);
|
||||
scope.bind(letStatement.name(), expected == null ? initializer.type() : expected, !letStatement.isConst());
|
||||
final var resolvedType = expected == null ? initializer.type() : expected;
|
||||
scope.bind(letStatement.name(), resolvedType, !letStatement.isConst());
|
||||
if (letStatement.explicitType() == null) {
|
||||
inlineHintCollector.collect(letStatement, resolvedType);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (statement instanceof PbsAst.ReturnStatement returnStatement) {
|
||||
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package p.studio.compiler.pbs.semantics;
|
||||
|
||||
import p.studio.compiler.pbs.semantics.PbsFlowSemanticSupport.TupleField;
|
||||
import p.studio.compiler.pbs.semantics.PbsFlowSemanticSupport.TypeView;
|
||||
|
||||
final class PbsInlineHintSurfaceFormatter {
|
||||
String format(final TypeView type) {
|
||||
if (type == null) {
|
||||
return null;
|
||||
}
|
||||
return switch (type.kind()) {
|
||||
case UNKNOWN, ASSET_NAMESPACE -> null;
|
||||
case UNIT -> "void";
|
||||
case INT, FLOAT, BOOL, STR -> type.name();
|
||||
case STRUCT,
|
||||
SERVICE,
|
||||
CONTRACT,
|
||||
CALLBACK,
|
||||
ENUM,
|
||||
ERROR,
|
||||
TYPE_REF,
|
||||
ADDRESSABLE -> type.name();
|
||||
case OPTIONAL -> {
|
||||
final var inner = format(type.inner());
|
||||
yield inner == null ? null : "optional " + inner;
|
||||
}
|
||||
case RESULT -> {
|
||||
final var errorType = format(type.errorType());
|
||||
final var payloadType = format(type.inner());
|
||||
if (errorType == null) {
|
||||
yield null;
|
||||
}
|
||||
yield payloadType == null
|
||||
? "result<%s>".formatted(errorType)
|
||||
: "result<%s> %s".formatted(errorType, payloadType);
|
||||
}
|
||||
case TUPLE -> formatTuple(type.tupleFields());
|
||||
};
|
||||
}
|
||||
|
||||
private String formatTuple(final java.util.List<TupleField> fields) {
|
||||
if (fields.isEmpty()) {
|
||||
return "()";
|
||||
}
|
||||
final var builder = new StringBuilder("(");
|
||||
for (int i = 0; i < fields.size(); i++) {
|
||||
if (i > 0) {
|
||||
builder.append(", ");
|
||||
}
|
||||
final var field = fields.get(i);
|
||||
final var fieldType = format(field.type());
|
||||
if (fieldType == null) {
|
||||
return null;
|
||||
}
|
||||
builder.append(field.label())
|
||||
.append(": ")
|
||||
.append(fieldType);
|
||||
}
|
||||
builder.append(')');
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
+38
-1
@@ -13,6 +13,7 @@ import p.studio.compiler.models.IRHiddenGlobalKind;
|
||||
import p.studio.compiler.models.IRSyntheticCallableKind;
|
||||
import p.studio.compiler.pbs.PbsFrontendCompiler;
|
||||
import p.studio.compiler.pbs.PbsReservedMetadataExtractor;
|
||||
import p.studio.compiler.pbs.semantics.PbsFlowSemanticsValidator;
|
||||
import p.studio.compiler.pbs.stdlib.InterfaceModuleLoader;
|
||||
import p.studio.compiler.pbs.stdlib.ResourceStdlibEnvironmentResolver;
|
||||
import p.studio.compiler.pbs.stdlib.StdlibEnvironmentResolver;
|
||||
@@ -112,16 +113,52 @@ public class PBSFrontendPhaseService implements FrontendPhaseService {
|
||||
final FrontendPhaseContext ctx,
|
||||
final DiagnosticSink diagnostics,
|
||||
final BuildingIssueSink issues) {
|
||||
return semanticReadSurface(ctx, diagnostics, issues).supplementalTopDeclsByFile();
|
||||
}
|
||||
|
||||
public static Map<FileId, ReadOnlyList<PbsInlineHintSurface>> inlineHintsByFile(
|
||||
final FrontendPhaseContext ctx,
|
||||
final DiagnosticSink diagnostics,
|
||||
final BuildingIssueSink issues) {
|
||||
return semanticReadSurface(ctx, diagnostics, issues).inlineHintsByFile();
|
||||
}
|
||||
|
||||
public static PbsSemanticReadSurface semanticReadSurface(
|
||||
final FrontendPhaseContext ctx,
|
||||
final DiagnosticSink diagnostics,
|
||||
final BuildingIssueSink issues) {
|
||||
final var service = new PBSFrontendPhaseService();
|
||||
final var assembly = service.moduleAssemblyService.assemble(ctx, ctx.nameTable(), diagnostics, issues);
|
||||
final var importedSemanticContexts = service.importedSemanticContextService.build(
|
||||
assembly.parsedSourceFiles(),
|
||||
assembly.moduleTable());
|
||||
final var flowSemanticsValidator = new PbsFlowSemanticsValidator();
|
||||
final Map<FileId, ReadOnlyList<p.studio.compiler.pbs.ast.PbsAst.TopDecl>> supplementalTopDeclsByFile = new LinkedHashMap<>();
|
||||
final Map<FileId, ReadOnlyList<PbsInlineHintSurface>> inlineHintsByFile = new LinkedHashMap<>();
|
||||
for (final var entry : importedSemanticContexts.entrySet()) {
|
||||
supplementalTopDeclsByFile.put(entry.getKey(), entry.getValue().supplementalTopDecls());
|
||||
}
|
||||
return Map.copyOf(supplementalTopDeclsByFile);
|
||||
for (final var parsedSourceFile : assembly.parsedSourceFiles()) {
|
||||
final var importedSemanticContext = importedSemanticContexts.getOrDefault(
|
||||
parsedSourceFile.fileId(),
|
||||
PbsImportedSemanticContext.empty());
|
||||
final var inlineHints = flowSemanticsValidator.collectInlineHints(
|
||||
parsedSourceFile.ast(),
|
||||
importedSemanticContext.supplementalTopDecls(),
|
||||
ctx.feSurfaceContext(),
|
||||
diagnostics);
|
||||
if (!inlineHints.isEmpty()) {
|
||||
inlineHintsByFile.put(parsedSourceFile.fileId(), inlineHints);
|
||||
}
|
||||
}
|
||||
return new PbsSemanticReadSurface(
|
||||
Map.copyOf(supplementalTopDeclsByFile),
|
||||
Map.copyOf(inlineHintsByFile));
|
||||
}
|
||||
|
||||
public record PbsSemanticReadSurface(
|
||||
Map<FileId, ReadOnlyList<p.studio.compiler.pbs.ast.PbsAst.TopDecl>> supplementalTopDeclsByFile,
|
||||
Map<FileId, ReadOnlyList<PbsInlineHintSurface>> inlineHintsByFile) {
|
||||
}
|
||||
|
||||
private IRBackend mergeCompiledSources(
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package p.studio.compiler.services;
|
||||
|
||||
import p.studio.compiler.source.Span;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public record PbsInlineHintSurface(
|
||||
Span anchorSpan,
|
||||
String label,
|
||||
String category) {
|
||||
|
||||
public PbsInlineHintSurface {
|
||||
anchorSpan = Objects.requireNonNull(anchorSpan, "anchorSpan");
|
||||
label = Objects.requireNonNull(label, "label").trim();
|
||||
category = Objects.requireNonNull(category, "category").trim();
|
||||
if (label.isBlank()) {
|
||||
throw new IllegalArgumentException("label cannot be blank");
|
||||
}
|
||||
if (category.isBlank()) {
|
||||
throw new IllegalArgumentException("category cannot be blank");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user