implements PR009
This commit is contained in:
+43
-5
@@ -5,11 +5,16 @@ import p.studio.compiler.source.Span;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@Getter
|
||||
public class Diagnostic {
|
||||
private final Severity severity;
|
||||
private final String code;
|
||||
private final DiagnosticPhase phase;
|
||||
private final String templateId;
|
||||
private final Map<String, String> placeholders;
|
||||
private final String message;
|
||||
private final Span span;
|
||||
private final ReadOnlyList<RelatedSpan> related;
|
||||
@@ -20,10 +25,43 @@ public class Diagnostic {
|
||||
final String message,
|
||||
final Span span,
|
||||
final List<RelatedSpan> related) {
|
||||
this.severity = severity;
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
this.span = span;
|
||||
this.related = ReadOnlyList.wrap(related);
|
||||
this(
|
||||
severity,
|
||||
code,
|
||||
DiagnosticPhase.fromCode(code),
|
||||
code,
|
||||
Map.of(),
|
||||
message,
|
||||
span,
|
||||
related);
|
||||
}
|
||||
|
||||
public Diagnostic(
|
||||
final Severity severity,
|
||||
final String code,
|
||||
final DiagnosticPhase phase,
|
||||
final String templateId,
|
||||
final Map<String, String> placeholders,
|
||||
final String message,
|
||||
final Span span,
|
||||
final List<RelatedSpan> related) {
|
||||
this.severity = Objects.requireNonNull(severity);
|
||||
this.code = Objects.requireNonNull(code);
|
||||
this.phase = Objects.requireNonNull(phase);
|
||||
this.templateId = Objects.requireNonNull(templateId);
|
||||
this.placeholders = Map.copyOf(Objects.requireNonNull(placeholders));
|
||||
this.message = Objects.requireNonNull(message);
|
||||
this.span = Objects.requireNonNull(span);
|
||||
this.related = ReadOnlyList.wrap(List.copyOf(Objects.requireNonNull(related)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "%s[%s:%s] %s @ %s".formatted(
|
||||
severity,
|
||||
phase.id(),
|
||||
code,
|
||||
message,
|
||||
span);
|
||||
}
|
||||
}
|
||||
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package p.studio.compiler.source.diagnostics;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public enum DiagnosticPhase {
|
||||
SYNTAX("syntax"),
|
||||
STATIC_SEMANTICS("static-semantics"),
|
||||
MANIFEST_IMPORT_RESOLUTION("manifest-import-resolution"),
|
||||
LINKING("linking"),
|
||||
HOST_ADMISSION("host-admission"),
|
||||
LOAD_FACING_REJECTION("load-facing-rejection"),
|
||||
UNKNOWN("unknown"),
|
||||
;
|
||||
|
||||
private final String id;
|
||||
|
||||
DiagnosticPhase(final String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public static DiagnosticPhase fromCode(final String code) {
|
||||
if (code == null || code.isBlank()) {
|
||||
return UNKNOWN;
|
||||
}
|
||||
|
||||
final var normalized = code.toUpperCase(Locale.ROOT);
|
||||
if (normalized.startsWith("E_PARSE_") || normalized.startsWith("E_LEX_")
|
||||
|| normalized.startsWith("W_PARSE_") || normalized.startsWith("W_LEX_")) {
|
||||
return SYNTAX;
|
||||
}
|
||||
if (normalized.startsWith("E_SEM_") || normalized.startsWith("W_SEM_")) {
|
||||
return STATIC_SEMANTICS;
|
||||
}
|
||||
if (normalized.startsWith("E_LINK_") || normalized.startsWith("W_LINK_")) {
|
||||
return LINKING;
|
||||
}
|
||||
if (normalized.startsWith("E_MANIFEST_") || normalized.startsWith("W_MANIFEST_")
|
||||
|| normalized.startsWith("E_IMPORT_") || normalized.startsWith("W_IMPORT_")) {
|
||||
return MANIFEST_IMPORT_RESOLUTION;
|
||||
}
|
||||
if (normalized.startsWith("E_HOST_") || normalized.startsWith("W_HOST_")) {
|
||||
return HOST_ADMISSION;
|
||||
}
|
||||
if (normalized.startsWith("E_LOAD_") || normalized.startsWith("W_LOAD_")) {
|
||||
return LOAD_FACING_REJECTION;
|
||||
}
|
||||
return UNKNOWN;
|
||||
}
|
||||
}
|
||||
+58
-3
@@ -6,6 +6,7 @@ import p.studio.utilities.structures.ReadOnlyCollection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class DiagnosticSink implements ReadOnlyCollection<Diagnostic> {
|
||||
@@ -39,20 +40,29 @@ public class DiagnosticSink implements ReadOnlyCollection<Diagnostic> {
|
||||
public DiagnosticSink report(
|
||||
final Severity severity,
|
||||
final String code,
|
||||
final DiagnosticPhase phase,
|
||||
final String templateId,
|
||||
final Map<String, String> placeholders,
|
||||
final String message,
|
||||
final Span span) {
|
||||
return report(severity, code, message, span, List.of());
|
||||
return report(severity, code, phase, templateId, placeholders, message, span, List.of());
|
||||
}
|
||||
|
||||
public DiagnosticSink report(
|
||||
final Severity severity,
|
||||
final String code,
|
||||
final DiagnosticPhase phase,
|
||||
final String templateId,
|
||||
final Map<String, String> placeholders,
|
||||
final String message,
|
||||
final Span span,
|
||||
final List<RelatedSpan> related) {
|
||||
return report(new Diagnostic(
|
||||
Objects.requireNonNull(severity),
|
||||
Objects.requireNonNull(code),
|
||||
Objects.requireNonNull(phase),
|
||||
Objects.requireNonNull(templateId),
|
||||
Map.copyOf(Objects.requireNonNull(placeholders)),
|
||||
Objects.requireNonNull(message),
|
||||
Objects.requireNonNull(span),
|
||||
List.copyOf(Objects.requireNonNull(related))));
|
||||
@@ -60,16 +70,61 @@ public class DiagnosticSink implements ReadOnlyCollection<Diagnostic> {
|
||||
|
||||
public DiagnosticSink error(
|
||||
final String code,
|
||||
final String templateId,
|
||||
final Map<String, String> placeholders,
|
||||
final String message,
|
||||
final Span span) {
|
||||
return report(Severity.Error, code, message, span);
|
||||
return report(Severity.Error, code, DiagnosticPhase.fromCode(code), templateId, placeholders, message, span);
|
||||
}
|
||||
|
||||
public DiagnosticSink error(
|
||||
final String code,
|
||||
final String templateId,
|
||||
final Map<String, String> placeholders,
|
||||
final String message,
|
||||
final Span span,
|
||||
final List<RelatedSpan> related) {
|
||||
return report(Severity.Error, code, DiagnosticPhase.fromCode(code), templateId, placeholders, message, span, related);
|
||||
}
|
||||
|
||||
public DiagnosticSink error(
|
||||
final DiagnosticPhase phase,
|
||||
final String code,
|
||||
final String templateId,
|
||||
final Map<String, String> placeholders,
|
||||
final String message,
|
||||
final Span span) {
|
||||
return report(Severity.Error, code, phase, templateId, placeholders, message, span);
|
||||
}
|
||||
|
||||
public DiagnosticSink error(
|
||||
final DiagnosticPhase phase,
|
||||
final String code,
|
||||
final String templateId,
|
||||
final Map<String, String> placeholders,
|
||||
final String message,
|
||||
final Span span,
|
||||
final List<RelatedSpan> related) {
|
||||
return report(Severity.Error, code, phase, templateId, placeholders, message, span, related);
|
||||
}
|
||||
|
||||
public DiagnosticSink warning(
|
||||
final String code,
|
||||
final String templateId,
|
||||
final Map<String, String> placeholders,
|
||||
final String message,
|
||||
final Span span) {
|
||||
return report(Severity.Warning, code, message, span);
|
||||
return report(Severity.Warning, code, DiagnosticPhase.fromCode(code), templateId, placeholders, message, span);
|
||||
}
|
||||
|
||||
public DiagnosticSink warning(
|
||||
final DiagnosticPhase phase,
|
||||
final String code,
|
||||
final String templateId,
|
||||
final Map<String, String> placeholders,
|
||||
final String message,
|
||||
final Span span) {
|
||||
return report(Severity.Warning, code, phase, templateId, placeholders, message, span);
|
||||
}
|
||||
|
||||
public DiagnosticSink merge(final DiagnosticSink diagnostics) {
|
||||
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package p.studio.compiler.source.diagnostics;
|
||||
|
||||
import p.studio.compiler.source.Span;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public final class Diagnostics {
|
||||
private Diagnostics() {
|
||||
}
|
||||
|
||||
public static DiagnosticSink error(
|
||||
final DiagnosticSink sink,
|
||||
final String code,
|
||||
final String message,
|
||||
final Span span) {
|
||||
return sink.error(code, code, Map.of(), message, span);
|
||||
}
|
||||
|
||||
public static DiagnosticSink error(
|
||||
final DiagnosticSink sink,
|
||||
final String code,
|
||||
final String message,
|
||||
final Span span,
|
||||
final List<RelatedSpan> related) {
|
||||
return sink.error(code, code, Map.of(), message, span, related);
|
||||
}
|
||||
|
||||
public static DiagnosticSink warning(
|
||||
final DiagnosticSink sink,
|
||||
final String code,
|
||||
final String message,
|
||||
final Span span) {
|
||||
return sink.warning(code, code, Map.of(), message, span);
|
||||
}
|
||||
|
||||
public static DiagnosticSink report(
|
||||
final DiagnosticSink sink,
|
||||
final Severity severity,
|
||||
final String code,
|
||||
final String message,
|
||||
final Span span,
|
||||
final List<RelatedSpan> related) {
|
||||
return sink.report(
|
||||
severity,
|
||||
code,
|
||||
DiagnosticPhase.fromCode(code),
|
||||
code,
|
||||
Map.of(),
|
||||
message,
|
||||
span,
|
||||
related);
|
||||
}
|
||||
}
|
||||
+60
-4
@@ -3,6 +3,9 @@ package p.studio.compiler.source.diagnostics;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.source.Span;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@@ -12,8 +15,8 @@ class DiagnosticSinkTest {
|
||||
void shouldReportAndCountDiagnostics() {
|
||||
final var sink = DiagnosticSink.empty();
|
||||
|
||||
sink.error("E001", "Unknown symbol", Span.none());
|
||||
sink.warning("W001", "Unused declaration", Span.none());
|
||||
sink.error("E001", "E001", Map.of(), "Unknown symbol", Span.none());
|
||||
sink.warning("W001", "W001", Map.of(), "Unused declaration", Span.none());
|
||||
|
||||
assertEquals(2, sink.size());
|
||||
assertTrue(sink.hasErrors());
|
||||
@@ -25,9 +28,9 @@ class DiagnosticSinkTest {
|
||||
@Test
|
||||
void shouldMergeDiagnostics() {
|
||||
final var a = DiagnosticSink.empty()
|
||||
.warning("W010", "Unused import", Span.none());
|
||||
.warning("W010", "W010", Map.of(), "Unused import", Span.none());
|
||||
final var b = DiagnosticSink.empty()
|
||||
.error("E999", "Invalid type", Span.none());
|
||||
.error("E999", "E999", Map.of(), "Invalid type", Span.none());
|
||||
|
||||
a.merge(b);
|
||||
|
||||
@@ -39,4 +42,57 @@ class DiagnosticSinkTest {
|
||||
assertEquals(1, b.errorCount());
|
||||
assertEquals(0, b.warningCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldPopulateContractFieldsForLegacyEmission() {
|
||||
final var sink = DiagnosticSink.empty();
|
||||
|
||||
sink.error("E_PARSE_EXPECTED_TOKEN", "E_PARSE_EXPECTED_TOKEN", Map.of(), "Expected token", Span.none());
|
||||
|
||||
final var diagnostic = sink.stream().findFirst().orElseThrow();
|
||||
assertEquals("E_PARSE_EXPECTED_TOKEN", diagnostic.getCode());
|
||||
assertEquals(DiagnosticPhase.SYNTAX, diagnostic.getPhase());
|
||||
assertEquals("E_PARSE_EXPECTED_TOKEN", diagnostic.getTemplateId());
|
||||
assertTrue(diagnostic.getPlaceholders().isEmpty());
|
||||
assertEquals("Expected token", diagnostic.getMessage());
|
||||
assertTrue(diagnostic.getSpan().isNone());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldKeepExplicitPhaseTemplateAndPlaceholdersStable() {
|
||||
final var sink = DiagnosticSink.empty();
|
||||
|
||||
sink.error(
|
||||
DiagnosticPhase.LINKING,
|
||||
"E_LINK_UNRESOLVED_BARREL_ENTRY",
|
||||
"pbs.link.unresolved-barrel-entry",
|
||||
Map.of("module", "@app:core", "symbol", "foo"),
|
||||
"Barrel entry 'foo' does not resolve",
|
||||
Span.none());
|
||||
|
||||
final var diagnostic = sink.stream().findFirst().orElseThrow();
|
||||
assertEquals(DiagnosticPhase.LINKING, diagnostic.getPhase());
|
||||
assertEquals("pbs.link.unresolved-barrel-entry", diagnostic.getTemplateId());
|
||||
assertEquals(Map.of("module", "@app:core", "symbol", "foo"), diagnostic.getPlaceholders());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReportRelatedSpansAndRenderPhaseInToString() {
|
||||
final var sink = DiagnosticSink.empty();
|
||||
final var related = new RelatedSpan("First declaration is here", Span.none());
|
||||
|
||||
sink.error(
|
||||
"E_SEM_DUPLICATE_DECLARATION",
|
||||
"E_SEM_DUPLICATE_DECLARATION",
|
||||
Map.of(),
|
||||
"Duplicate declaration",
|
||||
Span.none(),
|
||||
List.of(related));
|
||||
|
||||
final var diagnostic = sink.stream().findFirst().orElseThrow();
|
||||
assertEquals(1, diagnostic.getRelated().size());
|
||||
assertEquals("First declaration is here", diagnostic.getRelated().getFirst().getMessage());
|
||||
assertTrue(diagnostic.toString().contains("static-semantics"));
|
||||
assertTrue(diagnostic.toString().contains("E_SEM_DUPLICATE_DECLARATION"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user