Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Enforce canonical empty ops fixture spelling

Yuechen Li075104e934ce
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentCorpusHarness.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentCorpusHarness.cs
index fce6d1e4..4402a09c 100644
--- a/Aetheris.Kernel.Firmament.Tests/FirmamentCorpusHarness.cs
+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentCorpusHarness.cs
@@ -1,6 +1,5 @@
using System.Text;
using System.Text.Json;
-using Aetheris.Kernel.Core.Diagnostics;
namespace Aetheris.Kernel.Firmament.Tests;
@@ -49,13 +48,11 @@ internal static class FirmamentCorpusHarness
throw new InvalidOperationException("Unable to locate repository root.");
}
- public static KernelDiagnostic CompileFirstDiagnostic(string fixtureText)
+ public static FirmamentCompileResult Compile(string fixtureText)
{
var compiler = new FirmamentCompiler();
var request = new FirmamentCompileRequest(new FirmamentSourceDocument(fixtureText));
- var result = compiler.Compile(request);
- Assert.False(result.Compilation.IsSuccess);
- return Assert.Single(result.Compilation.Diagnostics);
+ return compiler.Compile(request);
}
}
@@ -83,5 +80,7 @@ internal sealed class FirmamentExpectedDiagnostic
{
public string Source { get; init; } = string.Empty;
- public string Code { get; init; } = string.Empty;
+ public string KernelCode { get; init; } = string.Empty;
+
+ public string FirmamentCode { get; init; } = string.Empty;
}
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentCorpusScaffoldTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentCorpusScaffoldTests.cs
index a0a351bf..9988bf8d 100644
--- a/Aetheris.Kernel.Firmament.Tests/FirmamentCorpusScaffoldTests.cs
+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentCorpusScaffoldTests.cs
@@ -4,7 +4,7 @@ namespace Aetheris.Kernel.Firmament.Tests;
public sealed class FirmamentCorpusScaffoldTests
{
- private const string ManifestPath = "testdata/firmament/manifests/pre-m0.corpus.json";
+ private const string ManifestPath = "testdata/firmament/manifests/m0a.corpus.json";
[Fact]
public void Manifest_Exists_And_CanBeLoaded()
@@ -36,23 +36,44 @@ public sealed class FirmamentCorpusScaffoldTests
}
[Fact]
- public void CuratedCases_InvokeStubCompiler_WithExpectedCoarseOutcome()
+ public void Fixtures_UseCanonicalEmptyOpsSpelling()
{
var manifest = FirmamentCorpusHarness.LoadManifest(ManifestPath);
- foreach (var entry in manifest.Entries.OrderBy(e => e.Id, StringComparer.Ordinal))
+ foreach (var entry in manifest.Entries)
{
- Assert.Equal("stubNotImplemented", entry.ExpectedOutcome);
+ var fixtureText = FirmamentCorpusHarness.ReadFixtureText(entry.FixturePath);
+
+ Assert.Contains("ops[0]:", fixtureText, StringComparison.Ordinal);
+ Assert.DoesNotContain("ops: []", fixtureText, StringComparison.Ordinal);
+ Assert.DoesNotContain("\nops:\n", fixtureText, StringComparison.Ordinal);
+ }
+ }
+
+ [Fact]
+ public void CuratedCases_Compile_WithExpectedOutcomeAndDiagnostics()
+ {
+ var manifest = FirmamentCorpusHarness.LoadManifest(ManifestPath);
+ foreach (var entry in manifest.Entries.OrderBy(e => e.Id, StringComparer.Ordinal))
+ {
var fixtureText = FirmamentCorpusHarness.ReadFixtureText(entry.FixturePath);
- var diagnostic = FirmamentCorpusHarness.CompileFirstDiagnostic(fixtureText);
+ var result = FirmamentCorpusHarness.Compile(fixtureText);
- Assert.Equal(KernelDiagnosticCode.NotImplemented, diagnostic.Code);
- if (entry.ExpectedDiagnostic is not null)
+ if (string.Equals(entry.ExpectedOutcome, "success", StringComparison.Ordinal))
{
- Assert.Equal(entry.ExpectedDiagnostic.Source, diagnostic.Source);
- Assert.Equal(entry.ExpectedDiagnostic.Code, diagnostic.Code.ToString());
+ Assert.True(result.Compilation.IsSuccess);
+ continue;
}
+
+ Assert.Equal("failure", entry.ExpectedOutcome);
+ Assert.False(result.Compilation.IsSuccess);
+ var diagnostic = Assert.Single(result.Compilation.Diagnostics);
+
+ Assert.NotNull(entry.ExpectedDiagnostic);
+ Assert.Equal(entry.ExpectedDiagnostic!.Source, diagnostic.Source);
+ Assert.Equal(Enum.Parse<KernelDiagnosticCode>(entry.ExpectedDiagnostic.KernelCode), diagnostic.Code);
+ Assert.Contains(entry.ExpectedDiagnostic.FirmamentCode, diagnostic.Message, StringComparison.Ordinal);
}
}
}
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentScaffoldTests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentScaffoldTests.cs
index c54de9db..d227b5e9 100644
--- a/Aetheris.Kernel.Firmament.Tests/FirmamentScaffoldTests.cs
+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentScaffoldTests.cs
@@ -1,3 +1,4 @@
+using Aetheris.Kernel.Core.Diagnostics;
using Aetheris.Kernel.Firmament.Diagnostics;
namespace Aetheris.Kernel.Firmament.Tests;
@@ -17,13 +18,41 @@ public sealed class FirmamentScaffoldTests
}
[Fact]
- public void CompilerStub_ReturnsDeterministicNotImplementedFailure()
+ public void Compiler_Accepts_ValidMinimalTopLevelSkeleton()
{
var compiler = new FirmamentCompiler();
- var request = new FirmamentCompileRequest(new FirmamentSourceDocument(string.Empty));
+ var source = """
+ {
+ "firmament": { "version": "1" },
+ "model": { "name": "demo", "units": "mm" },
+ "ops": []
+ }
+ """;
- var first = compiler.Compile(request);
- var second = compiler.Compile(request);
+ var result = compiler.Compile(new FirmamentCompileRequest(new FirmamentSourceDocument(source)));
+
+ Assert.True(result.Compilation.IsSuccess);
+ var artifact = result.Compilation.Value;
+ Assert.Equal("firmament-top-level-skeleton-parsed", artifact.ArtifactKind);
+ Assert.NotNull(artifact.ParsedDocument);
+ Assert.Equal("1", artifact.ParsedDocument!.Firmament.Version);
+ Assert.Equal("demo", artifact.ParsedDocument.Model.Name);
+ Assert.Equal("mm", artifact.ParsedDocument.Model.Units);
+ }
+
+ [Fact]
+ public void Compiler_Rejects_MissingFirmament_And_Diagnostics_AreDeterministic()
+ {
+ var compiler = new FirmamentCompiler();
+ var source = """
+ {
+ "model": { "name": "demo", "units": "mm" },
+ "ops": []
+ }
+ """;
+
+ var first = compiler.Compile(new FirmamentCompileRequest(new FirmamentSourceDocument(source)));
+ var second = compiler.Compile(new FirmamentCompileRequest(new FirmamentSourceDocument(source)));
Assert.False(first.Compilation.IsSuccess);
Assert.False(second.Compilation.IsSuccess);
@@ -31,18 +60,100 @@ public sealed class FirmamentScaffoldTests
var firstDiagnostic = Assert.Single(first.Compilation.Diagnostics);
var secondDiagnostic = Assert.Single(second.Compilation.Diagnostics);
+ Assert.Equal(KernelDiagnosticCode.ValidationFailed, firstDiagnostic.Code);
Assert.Equal(firstDiagnostic.Code, secondDiagnostic.Code);
Assert.Equal(firstDiagnostic.Severity, secondDiagnostic.Severity);
Assert.Equal(firstDiagnostic.Message, secondDiagnostic.Message);
Assert.Equal(firstDiagnostic.Source, secondDiagnostic.Source);
+ Assert.Contains(FirmamentDiagnosticCodes.StructureMissingRequiredSection.Value, firstDiagnostic.Message, StringComparison.Ordinal);
}
+ [Fact]
+ public void Compiler_Rejects_MissingModel() =>
+ AssertSingleValidationError(
+ """
+ {
+ "firmament": { "version": "1" },
+ "ops": []
+ }
+ """,
+ FirmamentDiagnosticCodes.StructureMissingRequiredSection,
+ "Missing required top-level section 'model'.");
+
+ [Fact]
+ public void Compiler_Rejects_MissingOps() =>
+ AssertSingleValidationError(
+ """
+ {
+ "firmament": { "version": "1" },
+ "model": { "name": "demo", "units": "mm" }
+ }
+ """,
+ FirmamentDiagnosticCodes.StructureMissingRequiredSection,
+ "Missing required top-level section 'ops'.");
+
+ [Fact]
+ public void Compiler_Rejects_MissingVersion() =>
+ AssertSingleValidationError(
+ """
+ {
+ "firmament": { },
+ "model": { "name": "demo", "units": "mm" },
+ "ops": []
+ }
+ """,
+ FirmamentDiagnosticCodes.StructureMissingRequiredField,
+ "Missing required field 'version' in section 'firmament'.");
+
+ [Fact]
+ public void Compiler_Rejects_MissingName() =>
+ AssertSingleValidationError(
+ """
+ {
+ "firmament": { "version": "1" },
+ "model": { "units": "mm" },
+ "ops": []
+ }
+ """,
+ FirmamentDiagnosticCodes.StructureMissingRequiredField,
+ "Missing required field 'name' in section 'model'.");
+
+ [Fact]
+ public void Compiler_Rejects_MissingUnits() =>
+ AssertSingleValidationError(
+ """
+ {
+ "firmament": { "version": "1" },
+ "model": { "name": "demo" },
+ "ops": []
+ }
+ """,
+ FirmamentDiagnosticCodes.StructureMissingRequiredField,
+ "Missing required field 'units' in section 'model'.");
+
+ [Fact]
+ public void Compiler_Rejects_UnknownTopLevelSection() =>
+ AssertSingleValidationError(
+ """
+ {
+ "firmament": { "version": "1" },
+ "model": { "name": "demo", "units": "mm" },
+ "ops": [],
+ "other": {}
+ }
+ """,
+ FirmamentDiagnosticCodes.StructureUnknownTopLevelSection,
+ "Unknown top-level section 'other'.");
+
[Fact]
public void DiagnosticTaxonomyIdentifiers_AreStableAndNonEmpty()
{
Assert.Equal("firmament", FirmamentDiagnosticConventions.Source);
- Assert.StartsWith("FIRM-PARSE", FirmamentDiagnosticCodes.ParsePlaceholder.Value);
- Assert.StartsWith("FIRM-STRUCT", FirmamentDiagnosticCodes.StructurePlaceholder.Value);
+ Assert.StartsWith("FIRM-PARSE", FirmamentDiagnosticCodes.ParseInvalidDocumentSyntax.Value);
+ Assert.StartsWith("FIRM-STRUCT", FirmamentDiagnosticCodes.StructureMissingRequiredSection.Value);
+ Assert.StartsWith("FIRM-STRUCT", FirmamentDiagnosticCodes.StructureUnknownTopLevelSection.Value);
+ Assert.StartsWith("FIRM-STRUCT", FirmamentDiagnosticCodes.StructureMissingRequiredField.Value);
+ Assert.StartsWith("FIRM-STRUCT", FirmamentDiagnosticCodes.StructureInvalidSectionShape.Value);
Assert.StartsWith("FIRM-REF", FirmamentDiagnosticCodes.ReferencePlaceholder.Value);
Assert.StartsWith("FIRM-SEL", FirmamentDiagnosticCodes.SelectorPlaceholder.Value);
Assert.StartsWith("FIRM-SCHEMA", FirmamentDiagnosticCodes.SchemaPlaceholder.Value);
@@ -61,4 +172,17 @@ public sealed class FirmamentScaffoldTests
Assert.Equal(1, span.End.Line);
Assert.Equal(5, span.End.Column);
}
+
+ private static void AssertSingleValidationError(string source, FirmamentDiagnosticCode expectedFirmamentCode, string expectedMessageTail)
+ {
+ var compiler = new FirmamentCompiler();
+
+ var result = compiler.Compile(new FirmamentCompileRequest(new FirmamentSourceDocument(source)));
+
+ Assert.False(result.Compilation.IsSuccess);
+ var diagnostic = Assert.Single(result.Compilation.Diagnostics);
+ Assert.Equal(KernelDiagnosticCode.ValidationFailed, diagnostic.Code);
+ Assert.Equal(FirmamentDiagnosticConventions.Source, diagnostic.Source);
+ Assert.Equal($"[{expectedFirmamentCode.Value}] {expectedMessageTail}", diagnostic.Message);
+ }
}
diff --git a/Aetheris.Kernel.Firmament/Diagnostics/FirmamentDiagnosticCodes.cs b/Aetheris.Kernel.Firmament/Diagnostics/FirmamentDiagnosticCodes.cs
index 1e46b98d..334a3a9d 100644
--- a/Aetheris.Kernel.Firmament/Diagnostics/FirmamentDiagnosticCodes.cs
+++ b/Aetheris.Kernel.Firmament/Diagnostics/FirmamentDiagnosticCodes.cs
@@ -2,8 +2,13 @@ namespace Aetheris.Kernel.Firmament.Diagnostics;
public static class FirmamentDiagnosticCodes
{
- public static readonly FirmamentDiagnosticCode ParsePlaceholder = new($"{FirmamentDiagnosticConventions.ParsePrefix}-0001");
- public static readonly FirmamentDiagnosticCode StructurePlaceholder = new($"{FirmamentDiagnosticConventions.StructurePrefix}-0001");
+ public static readonly FirmamentDiagnosticCode ParseInvalidDocumentSyntax = new($"{FirmamentDiagnosticConventions.ParsePrefix}-0001");
+
+ public static readonly FirmamentDiagnosticCode StructureMissingRequiredSection = new($"{FirmamentDiagnosticConventions.StructurePrefix}-0001");
+ public static readonly FirmamentDiagnosticCode StructureUnknownTopLevelSection = new($"{FirmamentDiagnosticConventions.StructurePrefix}-0002");
+ public static readonly FirmamentDiagnosticCode StructureMissingRequiredField = new($"{FirmamentDiagnosticConventions.StructurePrefix}-0003");
+ public static readonly FirmamentDiagnosticCode StructureInvalidSectionShape = new($"{FirmamentDiagnosticConventions.StructurePrefix}-0004");
+
public static readonly FirmamentDiagnosticCode ReferencePlaceholder = new($"{FirmamentDiagnosticConventions.ReferencePrefix}-0001");
public static readonly FirmamentDiagnosticCode SelectorPlaceholder = new($"{FirmamentDiagnosticConventions.SelectorPrefix}-0001");
public static readonly FirmamentDiagnosticCode SchemaPlaceholder = new($"{FirmamentDiagnosticConventions.SchemaPrefix}-0001");
diff --git a/Aetheris.Kernel.Firmament/FirmamentCompilationArtifact.cs b/Aetheris.Kernel.Firmament/FirmamentCompilationArtifact.cs
index 2edd2964..c89ab3c1 100644
--- a/Aetheris.Kernel.Firmament/FirmamentCompilationArtifact.cs
+++ b/Aetheris.Kernel.Firmament/FirmamentCompilationArtifact.cs
@@ -1,3 +1,7 @@
+using Aetheris.Kernel.Firmament.ParsedModel;
+
namespace Aetheris.Kernel.Firmament;
-public sealed record FirmamentCompilationArtifact(string ArtifactKind = "pre-m0-placeholder");
+public sealed record FirmamentCompilationArtifact(
+ string ArtifactKind = "firmament-top-level-skeleton-parsed",
+ FirmamentParsedDocument? ParsedDocument = null);
diff --git a/Aetheris.Kernel.Firmament/FirmamentCompiler.cs b/Aetheris.Kernel.Firmament/FirmamentCompiler.cs
index a858ed2d..67444a2e 100644
--- a/Aetheris.Kernel.Firmament/FirmamentCompiler.cs
+++ b/Aetheris.Kernel.Firmament/FirmamentCompiler.cs
@@ -1,6 +1,5 @@
-using Aetheris.Kernel.Core.Diagnostics;
using Aetheris.Kernel.Core.Results;
-using Aetheris.Kernel.Firmament.Diagnostics;
+using Aetheris.Kernel.Firmament.Parsing;
namespace Aetheris.Kernel.Firmament;
@@ -10,13 +9,15 @@ public sealed class FirmamentCompiler
{
ArgumentNullException.ThrowIfNull(request);
- var diagnostic = new KernelDiagnostic(
- KernelDiagnosticCode.NotImplemented,
- KernelDiagnosticSeverity.Error,
- "Firmament compilation is not implemented yet (pre-M0 scaffold).",
- Source: FirmamentDiagnosticConventions.Source);
+ var parseResult = FirmamentTopLevelParser.Parse(request.Document.SourceText);
+ if (!parseResult.IsSuccess)
+ {
+ return new FirmamentCompileResult(
+ KernelResult<FirmamentCompilationArtifact>.Failure(parseResult.Diagnostics));
+ }
return new FirmamentCompileResult(
- KernelResult<FirmamentCompilationArtifact>.Failure([diagnostic]));
+ KernelResult<FirmamentCompilationArtifact>.Success(
+ new FirmamentCompilationArtifact(ParsedDocument: parseResult.Value)));
}
}
diff --git a/Aetheris.Kernel.Firmament/ParsedModel/FirmamentParsedDocument.cs b/Aetheris.Kernel.Firmament/ParsedModel/FirmamentParsedDocument.cs
new file mode 100644
index 00000000..5e4cc93e
--- /dev/null
+++ b/Aetheris.Kernel.Firmament/ParsedModel/FirmamentParsedDocument.cs
@@ -0,0 +1,14 @@
+namespace Aetheris.Kernel.Firmament.ParsedModel;
+
+public sealed record FirmamentParsedDocument(
+ FirmamentParsedHeader Firmament,
+ FirmamentParsedModelHeader Model,
+ FirmamentParsedOpsSection Ops,
+ bool HasSchema,
+ bool HasPmi);
+
+public sealed record FirmamentParsedHeader(string Version);
+
+public sealed record FirmamentParsedModelHeader(string Name, string Units);
+
+public sealed record FirmamentParsedOpsSection(string Shape);
diff --git a/Aetheris.Kernel.Firmament/Parsing/FirmamentTopLevelParser.cs b/Aetheris.Kernel.Firmament/Parsing/FirmamentTopLevelParser.cs
new file mode 100644
index 00000000..17b47c4a
--- /dev/null
+++ b/Aetheris.Kernel.Firmament/Parsing/FirmamentTopLevelParser.cs
@@ -0,0 +1,351 @@
+using System.Text.Json;
+using Aetheris.Kernel.Core.Diagnostics;
+using Aetheris.Kernel.Core.Results;
+using Aetheris.Kernel.Firmament.Diagnostics;
+using Aetheris.Kernel.Firmament.ParsedModel;
+
+namespace Aetheris.Kernel.Firmament.Parsing;
+
+internal static class FirmamentTopLevelParser
+{
+ public static KernelResult<FirmamentParsedDocument> Parse(string sourceText)
+ {
+ ArgumentNullException.ThrowIfNull(sourceText);
+
+ if (TryParseJson(sourceText, out var jsonRootResult))
+ {
+ return ParseFromRoot(jsonRootResult);
+ }
+
+ var toonResult = ParseToonTopLevel(sourceText);
+ if (!toonResult.IsSuccess)
+ {
+ return KernelResult<FirmamentParsedDocument>.Failure(toonResult.Diagnostics);
+ }
+
+ return ParseFromToon(toonResult.Value);
+ }
+
+ private static bool TryParseJson(string sourceText, out JsonElement root)
+ {
+ root = default;
+ try
+ {
+ using var document = JsonDocument.Parse(sourceText);
+ root = document.RootElement.Clone();
+ return true;
+ }
+ catch (JsonException)
+ {
+ return false;
+ }
+ }
+
+ private static KernelResult<FirmamentParsedDocument> ParseFromRoot(JsonElement root)
+ {
+ if (root.ValueKind != JsonValueKind.Object)
+ {
+ return KernelResult<FirmamentParsedDocument>.Failure(
+ [
+ CreateDiagnostic(
+ KernelDiagnosticCode.ValidationFailed,
+ FirmamentDiagnosticCodes.StructureInvalidSectionShape,
+ "Top-level Firmament document must be an object.")
+ ]);
+ }
+
+ var unknownSection = root
+ .EnumerateObject()
+ .Select(p => p.Name)
+ .Where(name => !KnownTopLevelSections.Contains(name))
+ .OrderBy(name => name, StringComparer.Ordinal)
+ .FirstOrDefault();
+
+ if (unknownSection is not null)
+ {
+ return KernelResult<FirmamentParsedDocument>.Failure(
+ [
+ CreateDiagnostic(
+ KernelDiagnosticCode.ValidationFailed,
+ FirmamentDiagnosticCodes.StructureUnknownTopLevelSection,
+ $"Unknown top-level section '{unknownSection}'.")
+ ]);
+ }
+
+ if (!root.TryGetProperty("firmament", out var firmamentSection))
+ {
+ return MissingSection("firmament");
+ }
+
+ if (!root.TryGetProperty("model", out var modelSection))
+ {
+ return MissingSection("model");
+ }
+
+ if (!root.TryGetProperty("ops", out var opsSection))
+ {
+ return MissingSection("ops");
+ }
+
+ if (firmamentSection.ValueKind != JsonValueKind.Object)
+ {
+ return InvalidShape("firmament", "object");
+ }
+
+ if (modelSection.ValueKind != JsonValueKind.Object)
+ {
+ return InvalidShape("model", "object");
+ }
+
+ if (opsSection.ValueKind != JsonValueKind.Array)
+ {
+ return InvalidShape("ops", "array");
+ }
+
+ if (!firmamentSection.TryGetProperty("version", out var versionElement))
+ {
+ return MissingField("firmament", "version");
+ }
+
+ if (!modelSection.TryGetProperty("name", out var nameElement))
+ {
+ return MissingField("model", "name");
+ }
+
+ if (!modelSection.TryGetProperty("units", out var unitsElement))
+ {
+ return MissingField("model", "units");
+ }
+
+ return KernelResult<FirmamentParsedDocument>.Success(
+ new FirmamentParsedDocument(
+ new FirmamentParsedHeader(versionElement.ToString()),
+ new FirmamentParsedModelHeader(nameElement.ToString(), unitsElement.ToString()),
+ new FirmamentParsedOpsSection("array"),
+ HasSchema: root.TryGetProperty("schema", out _),
+ HasPmi: root.TryGetProperty("pmi", out _)));
+ }
+
+ private static KernelResult<FirmamentParsedDocument> ParseFromToon(FirmamentToonTopLevel toon)
+ {
+ var unknownSection = toon.Sections.Keys
+ .Where(name => !KnownTopLevelSections.Contains(name))
+ .OrderBy(name => name, StringComparer.Ordinal)
+ .FirstOrDefault();
+
+ if (unknownSection is not null)
+ {
+ return KernelResult<FirmamentParsedDocument>.Failure(
+ [
+ CreateDiagnostic(
+ KernelDiagnosticCode.ValidationFailed,
+ FirmamentDiagnosticCodes.StructureUnknownTopLevelSection,
+ $"Unknown top-level section '{unknownSection}'.")
+ ]);
+ }
+
+ if (!toon.Sections.TryGetValue("firmament", out var firmamentSection))
+ {
+ return MissingSection("firmament");
+ }
+
+ if (!toon.Sections.TryGetValue("model", out var modelSection))
+ {
+ return MissingSection("model");
+ }
+
+ if (!toon.Sections.TryGetValue("ops", out var opsSection))
+ {
+ return MissingSection("ops");
+ }
+
+ if (!firmamentSection.IsObjectLike)
+ {
+ return InvalidShape("firmament", "object");
+ }
+
+ if (!modelSection.IsObjectLike)
+ {
+ return InvalidShape("model", "object");
+ }
+
+ if (!opsSection.IsArrayLike)
+ {
+ return InvalidShape("ops", "array");
+ }
+
+ if (!firmamentSection.Fields.TryGetValue("version", out var version))
+ {
+ return MissingField("firmament", "version");
+ }
+
+ if (!modelSection.Fields.TryGetValue("name", out var name))
+ {
+ return MissingField("model", "name");
+ }
+
+ if (!modelSection.Fields.TryGetValue("units", out var units))
+ {
+ return MissingField("model", "units");
+ }
+
+ return KernelResult<FirmamentParsedDocument>.Success(
+ new FirmamentParsedDocument(
+ new FirmamentParsedHeader(version),
+ new FirmamentParsedModelHeader(name, units),
+ new FirmamentParsedOpsSection("array"),
+ HasSchema: toon.Sections.ContainsKey("schema"),
+ HasPmi: toon.Sections.ContainsKey("pmi")));
+ }
+
+ private static KernelResult<FirmamentToonTopLevel> ParseToonTopLevel(string sourceText)
+ {
+ var sections = new Dictionary<string, FirmamentToonSection>(StringComparer.Ordinal);
+ string? currentObjectSection = null;
+
+ var lines = sourceText.Replace("\r\n", "\n", StringComparison.Ordinal).Replace("\r", "\n", StringComparison.Ordinal).Split('\n');
+ for (var index = 0; index < lines.Length; index++)
+ {
+ var rawLine = lines[index];
+ var line = rawLine.TrimEnd();
+
+ if (string.IsNullOrWhiteSpace(line))
+ {
+ continue;
+ }
+
+ if (char.IsWhiteSpace(rawLine[0]))
+ {
+ if (currentObjectSection is null)
+ {
+ return InvalidToonSyntax();
+ }
+
+ var field = line.Trim();
+ var separator = field.IndexOf(':');
+ if (separator <= 0)
+ {
+ return InvalidToonSyntax();
+ }
+
+ var fieldName = field[..separator].Trim();
+ var fieldValue = field[(separator + 1)..].Trim();
+ if (fieldName.Length == 0 || fieldValue.Length == 0)
+ {
+ return InvalidToonSyntax();
+ }
+
+ sections[currentObjectSection].Fields[fieldName] = fieldValue;
+ continue;
+ }
+
+ currentObjectSection = null;
+
+ var header = line.Trim();
+ if (!header.EndsWith(":", StringComparison.Ordinal))
+ {
+ return InvalidToonSyntax();
+ }
+
+ header = header[..^1].Trim();
+ if (header.Length == 0)
+ {
+ return InvalidToonSyntax();
+ }
+
+ if (TryParseArrayHeader(header, out var arraySectionName))
+ {
+ sections[arraySectionName] = FirmamentToonSection.ArrayLike();
+ continue;
+ }
+
+ if (sections.ContainsKey(header))
+ {
+ return InvalidToonSyntax();
+ }
+
+ sections[header] = FirmamentToonSection.ObjectLike();
+ currentObjectSection = header;
+ }
+
+ return KernelResult<FirmamentToonTopLevel>.Success(new FirmamentToonTopLevel(sections));
+ }
+
+ private static bool TryParseArrayHeader(string header, out string sectionName)
+ {
+ sectionName = string.Empty;
+
+ var bracketStart = header.IndexOf('[');
+ var bracketEnd = header.LastIndexOf(']');
+ if (bracketStart <= 0 || bracketEnd != header.Length - 1)
+ {
+ return false;
+ }
+
+ sectionName = header[..bracketStart].Trim();
+ var countText = header[(bracketStart + 1)..bracketEnd].Trim();
+ return sectionName.Length > 0
+ && int.TryParse(countText, out _);
+ }
+
+ private static KernelResult<FirmamentToonTopLevel> InvalidToonSyntax() =>
+ KernelResult<FirmamentToonTopLevel>.Failure(
+ [
+ CreateDiagnostic(
+ KernelDiagnosticCode.InvalidArgument,
+ FirmamentDiagnosticCodes.ParseInvalidDocumentSyntax,
+ "Firmament source must be valid canonical TOON-style text or JSON with an object root.")
+ ]);
+
+ private static readonly HashSet<string> KnownTopLevelSections =
+ [
+ "firmament",
+ "model",
+ "ops",
+ "schema",
+ "pmi"
+ ];
+
+ private static KernelResult<FirmamentParsedDocument> MissingSection(string sectionName) =>
+ KernelResult<FirmamentParsedDocument>.Failure(
+ [
+ CreateDiagnostic(
+ KernelDiagnosticCode.ValidationFailed,
+ FirmamentDiagnosticCodes.StructureMissingRequiredSection,
+ $"Missing required top-level section '{sectionName}'.")
+ ]);
+
+ private static KernelResult<FirmamentParsedDocument> MissingField(string sectionName, string fieldName) =>
+ KernelResult<FirmamentParsedDocument>.Failure(
+ [
+ CreateDiagnostic(
+ KernelDiagnosticCode.ValidationFailed,
+ FirmamentDiagnosticCodes.StructureMissingRequiredField,
+ $"Missing required field '{fieldName}' in section '{sectionName}'.")
+ ]);
+
+ private static KernelResult<FirmamentParsedDocument> InvalidShape(string sectionName, string expectedShape) =>
+ KernelResult<FirmamentParsedDocument>.Failure(
+ [
+ CreateDiagnostic(
+ KernelDiagnosticCode.ValidationFailed,
+ FirmamentDiagnosticCodes.StructureInvalidSectionShape,
+ $"Section '{sectionName}' must be a {expectedShape}.")
+ ]);
+
+ private static KernelDiagnostic CreateDiagnostic(KernelDiagnosticCode kernelCode, FirmamentDiagnosticCode firmamentCode, string message) =>
+ new(
+ kernelCode,
+ KernelDiagnosticSeverity.Error,
+ $"[{firmamentCode.Value}] {message}",
+ Source: FirmamentDiagnosticConventions.Source);
+
+ private sealed record FirmamentToonTopLevel(IReadOnlyDictionary<string, FirmamentToonSection> Sections);
+
+ private sealed record FirmamentToonSection(bool IsObjectLike, bool IsArrayLike, Dictionary<string, string> Fields)
+ {
+ public static FirmamentToonSection ObjectLike() => new(true, false, new Dictionary<string, string>(StringComparer.Ordinal));
+
+ public static FirmamentToonSection ArrayLike() => new(false, true, new Dictionary<string, string>(StringComparer.Ordinal));
+ }
+}
diff --git a/testdata/firmament/fixtures/m0a-missing-firmament.firmament b/testdata/firmament/fixtures/m0a-missing-firmament.firmament
new file mode 100644
index 00000000..0e4430e7
--- /dev/null
+++ b/testdata/firmament/fixtures/m0a-missing-firmament.firmament
@@ -0,0 +1,5 @@
+model:
+ name: demo
+ units: mm
+
+ops[0]:
diff --git a/testdata/firmament/fixtures/m0a-missing-version.firmament b/testdata/firmament/fixtures/m0a-missing-version.firmament
new file mode 100644
index 00000000..0e5f1fd7
--- /dev/null
+++ b/testdata/firmament/fixtures/m0a-missing-version.firmament
@@ -0,0 +1,7 @@
+firmament:
+
+model:
+ name: demo
+ units: mm
+
+ops[0]:
diff --git a/testdata/firmament/fixtures/m0a-unknown-section.firmament b/testdata/firmament/fixtures/m0a-unknown-section.firmament
new file mode 100644
index 00000000..ab23fbcd
--- /dev/null
+++ b/testdata/firmament/fixtures/m0a-unknown-section.firmament
@@ -0,0 +1,10 @@
+firmament:
+ version: 1
+
+model:
+ name: demo
+ units: mm
+
+ops[0]:
+
+unknown:
diff --git a/testdata/firmament/fixtures/m0a-valid-minimal.firmament b/testdata/firmament/fixtures/m0a-valid-minimal.firmament
new file mode 100644
index 00000000..4f815bc7
--- /dev/null
+++ b/testdata/firmament/fixtures/m0a-valid-minimal.firmament
@@ -0,0 +1,8 @@
+firmament:
+ version: 1
+
+model:
+ name: demo
+ units: mm
+
+ops[0]:
diff --git a/testdata/firmament/manifests/m0a.corpus.json b/testdata/firmament/manifests/m0a.corpus.json
new file mode 100644
index 00000000..b38be606
--- /dev/null
+++ b/testdata/firmament/manifests/m0a.corpus.json
@@ -0,0 +1,44 @@
+{
+ "version": "1",
+ "entries": [
+ {
+ "id": "m0a-valid-minimal",
+ "name": "Valid minimal top-level skeleton",
+ "fixturePath": "testdata/firmament/fixtures/m0a-valid-minimal.firmament",
+ "expectedOutcome": "success"
+ },
+ {
+ "id": "m0a-missing-firmament",
+ "name": "Missing required firmament section",
+ "fixturePath": "testdata/firmament/fixtures/m0a-missing-firmament.firmament",
+ "expectedOutcome": "failure",
+ "expectedDiagnostic": {
+ "source": "firmament",
+ "kernelCode": "ValidationFailed",
+ "firmamentCode": "FIRM-STRUCT-0001"
+ }
+ },
+ {
+ "id": "m0a-missing-version",
+ "name": "Missing required version field",
+ "fixturePath": "testdata/firmament/fixtures/m0a-missing-version.firmament",
+ "expectedOutcome": "failure",
+ "expectedDiagnostic": {
+ "source": "firmament",
+ "kernelCode": "ValidationFailed",
+ "firmamentCode": "FIRM-STRUCT-0003"
+ }
+ },
+ {
+ "id": "m0a-unknown-section",
+ "name": "Unknown top-level section",
+ "fixturePath": "testdata/firmament/fixtures/m0a-unknown-section.firmament",
+ "expectedOutcome": "failure",
+ "expectedDiagnostic": {
+ "source": "firmament",
+ "kernelCode": "ValidationFailed",
+ "firmamentCode": "FIRM-STRUCT-0002"
+ }
+ }
+ ]
+}