better semantic style for PBS

This commit is contained in:
2026-04-02 18:35:58 +01:00
parent 275a7aa408
commit 4c1a5317dd
14 changed files with 1051 additions and 44 deletions
@@ -13,6 +13,7 @@ import p.studio.compiler.pbs.parser.PbsParser;
import p.studio.compiler.source.diagnostics.Diagnostic;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.compiler.services.PBSFrontendPhaseService;
import p.studio.compiler.workspaces.AssetSurfaceContextLoader;
import p.studio.compiler.workspaces.PipelineStage;
import p.studio.compiler.workspaces.stages.LoadSourcesPipelineStage;
@@ -70,7 +71,7 @@ final class LspSemanticReadPhase {
new OverlaySourceProviderFactoryImpl(vfsProjectDocument, requestedDocumentPath));
final BuilderPipelineContext context = BuilderPipelineContext.fromConfig(config);
final AnalysisRuntimeSnapshot snapshot = runAnalysisStages(context);
return index(snapshot, requestedDocumentPath);
return index(snapshot, context, requestedDocumentPath);
}
private static AnalysisRuntimeSnapshot runAnalysisStages(final BuilderPipelineContext context) {
@@ -129,6 +130,7 @@ final class LspSemanticReadPhase {
private static SemanticSession index(
final AnalysisRuntimeSnapshot runtimeSnapshot,
final BuilderPipelineContext context,
final Path requestedDocumentPath) {
final AnalysisSnapshot snapshot = runtimeSnapshot.analysisSnapshot();
final Map<Path, List<LspDiagnosticDTO>> diagnosticsByDocument = diagnosticsByDocument(
@@ -147,6 +149,10 @@ final class LspSemanticReadPhase {
Map.of(),
Map.of());
}
final Map<FileId, List<PbsAst.TopDecl>> importedSupplementalTopDeclsByFile = importedSupplementalTopDeclsByFile(
snapshot.frontendSpec(),
context);
final List<IndexedDocument> indexedDocuments = new ArrayList<>();
for (final FileId fileId : snapshot.fileTable()) {
final SourceHandle sourceHandle = snapshot.fileTable().get(fileId);
if (!isSourceRelated(snapshot.frontendSpec(), sourceHandle)) {
@@ -156,7 +162,17 @@ final class LspSemanticReadPhase {
final DiagnosticSink diagnostics = DiagnosticSink.empty();
final var tokens = PbsLexer.lex(source, fileId, diagnostics);
final PbsAst.File ast = PbsParser.parse(tokens, fileId, diagnostics, PbsParser.ParseMode.ORDINARY);
semanticIndex.index(sourceHandle.getCanonPath(), ast, tokens.asList());
final Path documentPath = sourceHandle.getCanonPath();
semanticIndex.registerDocument(documentPath, ast, tokens.asList());
indexedDocuments.add(new IndexedDocument(
fileId,
documentPath,
importedSupplementalTopDeclsByFile.getOrDefault(fileId, List.of())));
}
for (final IndexedDocument indexedDocument : indexedDocuments) {
semanticIndex.buildHighlights(
indexedDocument.documentPath(),
indexedDocument.visibleImportedTopDecls());
}
return new SemanticSession(
normalize(requestedDocumentPath),
@@ -169,6 +185,39 @@ final class LspSemanticReadPhase {
semanticIndex.tokensByDocument());
}
private static Map<FileId, List<PbsAst.TopDecl>> importedSupplementalTopDeclsByFile(
final FrontendSpec frontendSpec,
final BuilderPipelineContext context) {
if (context.resolvedWorkspace == null || context.fileTable == null || !"pbs".equals(frontendSpec.getLanguageId())) {
return Map.of();
}
final FESurfaceContext feSurfaceContext = new AssetSurfaceContextLoader().load(context.resolvedWorkspace.mainProject().getRootPath());
final FrontendPhaseContext frontendPhaseContext = new FrontendPhaseContext(
context.resolvedWorkspace.graph().projectTable(),
context.fileTable,
context.resolvedWorkspace.stack(),
context.resolvedWorkspace.stdlib(),
HostAdmissionContext.permissiveDefault(),
feSurfaceContext);
final var diagnostics = DiagnosticSink.empty();
final var issues = BuildingIssueSink.empty();
final var supplementalTopDecls = PBSFrontendPhaseService.importedSupplementalTopDeclsByFile(
frontendPhaseContext,
diagnostics,
issues);
final Map<FileId, List<PbsAst.TopDecl>> byFile = new LinkedHashMap<>();
for (final var entry : supplementalTopDecls.entrySet()) {
byFile.put(entry.getKey(), entry.getValue().asList());
}
return Map.copyOf(byFile);
}
private record IndexedDocument(
FileId fileId,
Path documentPath,
List<PbsAst.TopDecl> visibleImportedTopDecls) {
}
private static boolean isSourceRelated(final FrontendSpec frontendSpec, final SourceHandle sourceHandle) {
return frontendSpec.getAllowedExtensions().contains(sourceHandle.getExtension());
}
@@ -15,6 +15,7 @@ import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static p.studio.lsp.LspSemanticUtilities.normalize;
@@ -29,6 +30,22 @@ public final class SemanticIndex {
final Path documentPath,
final PbsAst.File ast,
final List<PbsToken> tokens) {
index(documentPath, ast, tokens, List.of());
}
public void index(
final Path documentPath,
final PbsAst.File ast,
final List<PbsToken> tokens,
final List<PbsAst.TopDecl> visibleImportedTopDecls) {
registerDocument(documentPath, ast, tokens);
buildHighlights(documentPath, visibleImportedTopDecls);
}
public void registerDocument(
final Path documentPath,
final PbsAst.File ast,
final List<PbsToken> tokens) {
final Path normalizedDocumentPath = normalize(documentPath);
tokensByDocument.put(normalizedDocumentPath, List.copyOf(tokens));
final List<LspSymbolDTO> documentSymbols = new ArrayList<>();
@@ -44,21 +61,49 @@ public final class SemanticIndex {
symbolsByName.computeIfAbsent(child.name(), ignored -> new ArrayList<>()).add(child);
}
}
documentSymbolsByDocument.put(normalizedDocumentPath, List.copyOf(documentSymbols));
}
public void buildHighlights(
final Path documentPath,
final List<PbsAst.TopDecl> visibleImportedTopDecls) {
final Path normalizedDocumentPath = normalize(documentPath);
final List<PbsToken> tokens = tokensByDocument.getOrDefault(normalizedDocumentPath, List.of());
final Map<String, LspSymbolKind> visibleKindsByName = new LinkedHashMap<>();
for (final var entry : symbolsByName.entrySet()) {
if (entry.getValue().isEmpty()) {
continue;
}
visibleKindsByName.putIfAbsent(entry.getKey(), entry.getValue().getFirst().kind());
}
for (final LspSymbolDTO symbol : documentSymbolsByDocument.getOrDefault(normalizedDocumentPath, List.of())) {
visibleKindsByName.putIfAbsent(symbol.name(), symbol.kind());
for (final LspSymbolDTO child : symbol.children()) {
visibleKindsByName.putIfAbsent(child.name(), child.kind());
}
}
for (final PbsAst.TopDecl topDecl : visibleImportedTopDecls) {
final LspSymbolKind visibleKind = symbolKindForTopDecl(topDecl);
final String visibleName = topDeclName(topDecl);
if (visibleKind == null || visibleName == null || visibleName.isBlank()) {
continue;
}
visibleKindsByName.putIfAbsent(visibleName, visibleKind);
}
semanticHighlightsByDocument.put(
normalizedDocumentPath,
buildSemanticHighlights(tokens, symbolsByName));
documentSymbolsByDocument.put(normalizedDocumentPath, List.copyOf(documentSymbols));
buildSemanticHighlights(tokens, visibleKindsByName));
}
private List<LspHighlightSpanDTO> buildSemanticHighlights(
final List<PbsToken> tokens,
final Map<String, List<LspSymbolDTO>> indexedSymbolsByName) {
final Map<String, LspSymbolKind> visibleKindsByName) {
final List<LspHighlightSpanDTO> highlights = new ArrayList<>();
for (final PbsToken token : tokens) {
if (token.kind() == PbsTokenKind.EOF) {
continue;
}
final String semanticKey = semanticKey(token, indexedSymbolsByName);
final String semanticKey = semanticKey(token, visibleKindsByName);
if (semanticKey == null || semanticKey.isBlank()) {
continue;
}
@@ -71,34 +116,129 @@ public final class SemanticIndex {
private String semanticKey(
final PbsToken token,
final Map<String, List<LspSymbolDTO>> indexedSymbolsByName) {
final Map<String, LspSymbolKind> visibleKindsByName) {
final PbsSemanticKind semanticKind = token.kind() == p.studio.compiler.pbs.lexer.PbsTokenKind.IDENTIFIER
? semanticKindForIdentifier(token.lexeme(), indexedSymbolsByName)
? semanticKindForIdentifier(token.lexeme(), visibleKindsByName)
: PbsSemanticKind.forToken(token);
return semanticKind == null ? null : semanticKind.semanticKey();
}
private PbsSemanticKind semanticKindForIdentifier(
final String lexeme,
final Map<String, List<LspSymbolDTO>> indexedSymbolsByName) {
final List<LspSymbolDTO> symbols = indexedSymbolsByName.getOrDefault(lexeme, List.of());
if (symbols.isEmpty()) {
final Map<String, LspSymbolKind> visibleKindsByName) {
final LspSymbolKind kind = visibleKindsByName.get(lexeme);
if (kind == null) {
return PbsSemanticKind.IDENTIFIER;
}
final LspSymbolKind kind = symbols.getFirst().kind();
return switch (kind) {
case FUNCTION, METHOD, CALLBACK -> PbsSemanticKind.FUNCTION;
case STRUCT, CONTRACT, HOST, BUILTIN_TYPE, SERVICE, ERROR, ENUM -> PbsSemanticKind.TYPE;
case GLOBAL, CONST -> PbsSemanticKind.BINDING;
case FUNCTION -> PbsSemanticKind.FUNCTION;
case METHOD -> PbsSemanticKind.METHOD;
case CONSTRUCTOR -> PbsSemanticKind.CONSTRUCTOR;
case STRUCT -> PbsSemanticKind.STRUCT;
case CONTRACT -> PbsSemanticKind.CONTRACT;
case HOST -> PbsSemanticKind.HOST;
case BUILTIN_TYPE -> PbsSemanticKind.BUILTIN_TYPE;
case SERVICE -> PbsSemanticKind.SERVICE;
case ERROR -> PbsSemanticKind.ERROR;
case ENUM -> PbsSemanticKind.ENUM;
case CALLBACK -> PbsSemanticKind.CALLBACK;
case GLOBAL -> PbsSemanticKind.GLOBAL;
case CONST -> PbsSemanticKind.CONST;
case IMPLEMENTS -> PbsSemanticKind.IMPLEMENTS;
default -> PbsSemanticKind.IDENTIFIER;
};
}
private String topDeclName(final PbsAst.TopDecl topDecl) {
if (topDecl instanceof PbsAst.FunctionDecl functionDecl) {
return functionDecl.name();
}
if (topDecl instanceof PbsAst.StructDecl structDecl) {
return structDecl.name();
}
if (topDecl instanceof PbsAst.ContractDecl contractDecl) {
return contractDecl.name();
}
if (topDecl instanceof PbsAst.HostDecl hostDecl) {
return hostDecl.name();
}
if (topDecl instanceof PbsAst.BuiltinTypeDecl builtinTypeDecl) {
return builtinTypeDecl.name();
}
if (topDecl instanceof PbsAst.ServiceDecl serviceDecl) {
return serviceDecl.name();
}
if (topDecl instanceof PbsAst.ErrorDecl errorDecl) {
return errorDecl.name();
}
if (topDecl instanceof PbsAst.EnumDecl enumDecl) {
return enumDecl.name();
}
if (topDecl instanceof PbsAst.CallbackDecl callbackDecl) {
return callbackDecl.name();
}
if (topDecl instanceof PbsAst.GlobalDecl globalDecl) {
return globalDecl.name();
}
if (topDecl instanceof PbsAst.ConstDecl constDecl) {
return constDecl.name();
}
if (topDecl instanceof PbsAst.ImplementsDecl implementsDecl) {
return implementsDecl.binderName();
}
return null;
}
private LspSymbolKind symbolKindForTopDecl(final PbsAst.TopDecl topDecl) {
if (topDecl instanceof PbsAst.FunctionDecl) {
return LspSymbolKind.FUNCTION;
}
if (topDecl instanceof PbsAst.StructDecl) {
return LspSymbolKind.STRUCT;
}
if (topDecl instanceof PbsAst.ContractDecl) {
return LspSymbolKind.CONTRACT;
}
if (topDecl instanceof PbsAst.HostDecl) {
return LspSymbolKind.HOST;
}
if (topDecl instanceof PbsAst.BuiltinTypeDecl) {
return LspSymbolKind.BUILTIN_TYPE;
}
if (topDecl instanceof PbsAst.ServiceDecl) {
return LspSymbolKind.SERVICE;
}
if (topDecl instanceof PbsAst.ErrorDecl) {
return LspSymbolKind.ERROR;
}
if (topDecl instanceof PbsAst.EnumDecl) {
return LspSymbolKind.ENUM;
}
if (topDecl instanceof PbsAst.CallbackDecl) {
return LspSymbolKind.CALLBACK;
}
if (topDecl instanceof PbsAst.GlobalDecl) {
return LspSymbolKind.GLOBAL;
}
if (topDecl instanceof PbsAst.ConstDecl) {
return LspSymbolKind.CONST;
}
if (topDecl instanceof PbsAst.ImplementsDecl) {
return LspSymbolKind.IMPLEMENTS;
}
return null;
}
private LspSymbolDTO symbolForTopDecl(
final Path documentPath,
final PbsAst.TopDecl topDecl) {
if (topDecl instanceof PbsAst.FunctionDecl functionDecl) {
return symbol(documentPath, functionDecl.name(), LspSymbolKind.FUNCTION, functionDecl.span(), List.of());
return symbol(
documentPath,
functionDecl.name(),
LspSymbolKind.FUNCTION,
functionDecl.span(),
localSymbolsInBlock(documentPath, functionDecl.body()));
}
if (topDecl instanceof PbsAst.StructDecl structDecl) {
return symbol(documentPath, structDecl.name(), LspSymbolKind.STRUCT, structDecl.span(), structChildren(documentPath, structDecl));
@@ -147,7 +287,12 @@ public final class SemanticIndex {
final PbsAst.StructDecl structDecl) {
final List<LspSymbolDTO> children = new ArrayList<>(functionChildren(documentPath, structDecl.methods().asList()));
for (final PbsAst.CtorDecl ctorDecl : structDecl.ctors()) {
children.add(symbol(documentPath, ctorDecl.name(), LspSymbolKind.CONSTRUCTOR, ctorDecl.span(), List.of()));
children.add(symbol(
documentPath,
ctorDecl.name(),
LspSymbolKind.CONSTRUCTOR,
ctorDecl.span(),
localSymbolsInBlock(documentPath, ctorDecl.body())));
}
return List.copyOf(children);
}
@@ -157,7 +302,174 @@ public final class SemanticIndex {
final List<PbsAst.FunctionDecl> functions) {
final List<LspSymbolDTO> children = new ArrayList<>();
for (final PbsAst.FunctionDecl functionDecl : functions) {
children.add(symbol(documentPath, functionDecl.name(), LspSymbolKind.METHOD, functionDecl.span(), List.of()));
children.add(symbol(
documentPath,
functionDecl.name(),
LspSymbolKind.METHOD,
functionDecl.span(),
localSymbolsInBlock(documentPath, functionDecl.body())));
}
return List.copyOf(children);
}
private List<LspSymbolDTO> localSymbolsInBlock(
final Path documentPath,
final PbsAst.Block block) {
final List<LspSymbolDTO> children = new ArrayList<>();
for (final PbsAst.Statement statement : block.statements().asList()) {
children.addAll(localSymbolsInStatement(documentPath, statement));
}
if (block.tailExpression() != null) {
children.addAll(localSymbolsInExpression(documentPath, block.tailExpression()));
}
return List.copyOf(children);
}
private List<LspSymbolDTO> localSymbolsInStatement(
final Path documentPath,
final PbsAst.Statement statement) {
return switch (statement) {
case PbsAst.LetStatement letStatement -> localSymbolsInExpression(documentPath, letStatement.initializer());
case PbsAst.AssignStatement assignStatement -> localSymbolsInExpression(documentPath, assignStatement.value());
case PbsAst.ReturnStatement returnStatement -> localSymbolsInNullableExpression(documentPath, returnStatement.value());
case PbsAst.IfStatement ifStatement -> List.of(symbol(
documentPath,
"if",
LspSymbolKind.IF,
ifStatement.span(),
children(
localSymbolsInBlock(documentPath, ifStatement.thenBlock()),
ifStatement.elseIf() == null ? List.of() : localSymbolsInStatement(documentPath, ifStatement.elseIf()),
ifStatement.elseBlock() == null ? List.of() : localSymbolsInBlock(documentPath, ifStatement.elseBlock()))));
case PbsAst.ForStatement forStatement -> List.of(symbol(
documentPath,
"for " + forStatement.iteratorName(),
LspSymbolKind.FOR,
forStatement.span(),
localSymbolsInBlock(documentPath, forStatement.body())));
case PbsAst.WhileStatement whileStatement -> List.of(symbol(
documentPath,
"while",
LspSymbolKind.WHILE,
whileStatement.span(),
localSymbolsInBlock(documentPath, whileStatement.body())));
case PbsAst.ExpressionStatement expressionStatement -> localSymbolsInExpression(documentPath, expressionStatement.expression());
case PbsAst.BreakStatement ignored -> List.of();
case PbsAst.ContinueStatement ignored -> List.of();
};
}
private List<LspSymbolDTO> localSymbolsInExpression(
final Path documentPath,
final PbsAst.Expression expression) {
return switch (expression) {
case PbsAst.IfExpr ifExpr -> List.of(symbol(
documentPath,
"if",
LspSymbolKind.IF,
ifExpr.span(),
children(
localSymbolsInBlock(documentPath, ifExpr.thenBlock()),
localSymbolsInExpression(documentPath, ifExpr.elseExpression()))));
case PbsAst.SwitchExpr switchExpr -> List.of(symbol(
documentPath,
"switch",
LspSymbolKind.SWITCH,
switchExpr.span(),
childrenFromSwitchArms(documentPath, switchExpr.arms().asList())));
case PbsAst.HandleExpr handleExpr -> List.of(symbol(
documentPath,
"handle",
LspSymbolKind.HANDLE,
handleExpr.span(),
childrenFromHandleArms(documentPath, handleExpr.arms().asList())));
case PbsAst.BlockExpr blockExpr -> localSymbolsInBlock(documentPath, blockExpr.block());
case PbsAst.ElseExpr elseExpr -> children(
localSymbolsInExpression(documentPath, elseExpr.optionalExpression()),
localSymbolsInExpression(documentPath, elseExpr.fallbackExpression()));
case PbsAst.UnaryExpr unaryExpr -> localSymbolsInExpression(documentPath, unaryExpr.expression());
case PbsAst.BinaryExpr binaryExpr -> children(
localSymbolsInExpression(documentPath, binaryExpr.left()),
localSymbolsInExpression(documentPath, binaryExpr.right()));
case PbsAst.ApplyExpr applyExpr -> children(
localSymbolsInExpression(documentPath, applyExpr.callee()),
localSymbolsInExpression(documentPath, applyExpr.argument()));
case PbsAst.AsExpr asExpr -> localSymbolsInExpression(documentPath, asExpr.expression());
case PbsAst.CallExpr callExpr -> {
final List<List<LspSymbolDTO>> nested = new ArrayList<>();
nested.add(localSymbolsInExpression(documentPath, callExpr.callee()));
for (final PbsAst.Expression argument : callExpr.arguments().asList()) {
nested.add(localSymbolsInExpression(documentPath, argument));
}
yield children(nested.toArray(List[]::new));
}
case PbsAst.MemberExpr memberExpr -> localSymbolsInExpression(documentPath, memberExpr.receiver());
case PbsAst.PropagateExpr propagateExpr -> localSymbolsInExpression(documentPath, propagateExpr.expression());
case PbsAst.GroupExpr groupExpr -> localSymbolsInExpression(documentPath, groupExpr.expression());
case PbsAst.NewExpr newExpr -> {
final List<List<LspSymbolDTO>> nested = new ArrayList<>();
for (final PbsAst.Expression argument : newExpr.arguments().asList()) {
nested.add(localSymbolsInExpression(documentPath, argument));
}
yield children(nested.toArray(List[]::new));
}
case PbsAst.BindExpr bindExpr -> localSymbolsInExpression(documentPath, bindExpr.contextExpression());
case PbsAst.SomeExpr someExpr -> localSymbolsInExpression(documentPath, someExpr.value());
case PbsAst.OkExpr okExpr -> localSymbolsInExpression(documentPath, okExpr.value());
case PbsAst.TupleExpr tupleExpr -> {
final List<List<LspSymbolDTO>> nested = new ArrayList<>();
for (final PbsAst.TupleItem item : tupleExpr.items().asList()) {
nested.add(localSymbolsInExpression(documentPath, item.expression()));
}
yield children(nested.toArray(List[]::new));
}
case PbsAst.IdentifierExpr ignored -> List.of();
case PbsAst.IntLiteralExpr ignored -> List.of();
case PbsAst.FloatLiteralExpr ignored -> List.of();
case PbsAst.BoundedLiteralExpr ignored -> List.of();
case PbsAst.StringLiteralExpr ignored -> List.of();
case PbsAst.BoolLiteralExpr ignored -> List.of();
case PbsAst.ThisExpr ignored -> List.of();
case PbsAst.NoneExpr ignored -> List.of();
case PbsAst.ErrExpr ignored -> List.of();
case PbsAst.UnitExpr ignored -> List.of();
};
}
private List<LspSymbolDTO> childrenFromSwitchArms(
final Path documentPath,
final List<PbsAst.SwitchArm> arms) {
final List<LspSymbolDTO> children = new ArrayList<>();
for (final PbsAst.SwitchArm arm : arms) {
children.addAll(localSymbolsInBlock(documentPath, arm.block()));
}
return List.copyOf(children);
}
private List<LspSymbolDTO> childrenFromHandleArms(
final Path documentPath,
final List<PbsAst.HandleArm> arms) {
final List<LspSymbolDTO> children = new ArrayList<>();
for (final PbsAst.HandleArm arm : arms) {
children.addAll(localSymbolsInBlock(documentPath, arm.block()));
}
return List.copyOf(children);
}
private List<LspSymbolDTO> localSymbolsInNullableExpression(
final Path documentPath,
final PbsAst.Expression expression) {
return expression == null ? List.of() : localSymbolsInExpression(documentPath, expression);
}
@SafeVarargs
private static List<LspSymbolDTO> children(final List<LspSymbolDTO>... parts) {
final List<LspSymbolDTO> children = new ArrayList<>();
for (final List<LspSymbolDTO> part : parts) {
if (part == null || part.isEmpty()) {
continue;
}
children.addAll(part);
}
return List.copyOf(children);
}
@@ -179,7 +491,7 @@ public final class SemanticIndex {
final Span span,
final List<LspSymbolDTO> children) {
return new LspSymbolDTO(
name,
Objects.requireNonNullElse(name, kind.name().toLowerCase()),
kind,
documentPath,
new LspRangeDTO((int) span.getStart(), (int) span.getEnd()),