Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Expose static tables and improve compile-time record binding

Yuechen Li9f023982a577
diff --git a/Aetheris.CLI/CliRunner.cs b/Aetheris.CLI/CliRunner.cs
index 6806e062..f4953e9b 100644
--- a/Aetheris.CLI/CliRunner.cs
+++ b/Aetheris.CLI/CliRunner.cs
@@ -483,6 +483,22 @@ public static class CliRunner
model = document?.ModelName,
units = document?.Units,
bodies = document?.Solids.Select(solid => new { name = solid.Name, kind = solid.RecordType }).ToArray() ?? [],
+ tables = document?.StaticAuthoring?.Tables?.Select(table => new
+ {
+ table.Name,
+ rowType = table.RowType,
+ keyField = table.KeyField,
+ rowCount = table.RowCount,
+ columns = table.Columns,
+ table.SourceSpan
+ }).ToArray() ?? [],
+ templateInstances = document?.ConceptIr?.TemplateInstantiations?.Select(instance => new
+ {
+ instance.Template,
+ instance.Instance,
+ instance.SpecializationIdentity,
+ records = instance.RecordArguments?.ToDictionary(pair => pair.Key, pair => new { pair.Value.RecordType, pair.Value.StaticValue, pair.Value.Provenance, members = pair.Value.Members })
+ }).ToArray() ?? [],
features,
pmi = document?.Pmi?.Select(item => new { item.Kind, item.Name, item.Target }).ToArray() ?? [],
assertions = document?.VolumeAssertions?.Select(assertion => new { assertion.Id, assertion.TargetBodyId, assertion.ExpectedMm3, assertion.ToleranceMm3 }).ToArray() ?? [],
diff --git a/Aetheris.Kernel.Firmament.Tests/FirmamentV2StaticTablesWithM1Tests.cs b/Aetheris.Kernel.Firmament.Tests/FirmamentV2StaticTablesWithM1Tests.cs
new file mode 100644
index 00000000..e9a3eb6e
--- /dev/null
+++ b/Aetheris.Kernel.Firmament.Tests/FirmamentV2StaticTablesWithM1Tests.cs
@@ -0,0 +1,137 @@
+using Aetheris.Kernel.Firmament.FirmamentV2;
+using System.Diagnostics;
+
+namespace Aetheris.Kernel.Firmament.Tests;
+
+public sealed class FirmamentV2StaticTablesWithM1Tests
+{
+ [Fact]
+ public void ColumnarTable_KeyLookup_WithDerivation_AndNestedRecordTemplateBinding_AreErasedBeforeAir()
+ {
+ var parse = FirmamentV2Parser.Parse(Source());
+ Assert.True(parse.IsSuccess, string.Join(Environment.NewLine, parse.Diagnostics));
+ var staticAuthoring = parse.Document!.StaticAuthoring!;
+ var table = Assert.Single(staticAuthoring.Tables!);
+ Assert.Equal("WidgetStandards", table.Name);
+ Assert.Equal("StandardRow", table.RowType);
+ Assert.Equal("Kind", table.KeyField);
+ Assert.Equal(2, table.RowCount);
+ Assert.Equal(["Small", "Large"], table.Columns["Kind"]);
+
+ var instance = Assert.Single(parse.Document.ConceptIr!.TemplateInstantiations!);
+ var spec = Assert.Single(instance.RecordArguments!).Value;
+ Assert.Equal("TallSmall", spec.StaticValue);
+ Assert.Equal("30mm", spec.Members["Height"]);
+ Assert.Equal("40mm", spec.Members["Standard.Width"]);
+ Assert.Contains("Table:WidgetStandards row:0 key:Small", spec.Provenance, StringComparison.Ordinal);
+ Assert.Contains("derivedFrom:Base", spec.Provenance, StringComparison.Ordinal);
+ Assert.Equal([40d, 20d, 30d], parse.Document.Solid.Box!.Size);
+ Assert.DoesNotContain("Static Table", parse.Document.ConceptIr.MaterializedStruct.SourceSpelling, StringComparison.Ordinal);
+ }
+
+ [Theory]
+ [InlineData("Width: [40mm]", "firmament-template-table-unequal-column-length")]
+ [InlineData("Width: [40deg, 80mm]", "firmament-template-table-column-type-mismatch")]
+ [InlineData("Kind: [Small, Small]", "firmament-template-table-duplicate-key")]
+ [InlineData("Unknown: [1mm, 2mm]", "firmament-template-table-unknown-column")]
+ public void Table_ReportsDeterministicTypedDiagnostics(string replacement, string diagnostic)
+ {
+ var parse = FirmamentV2Parser.Parse(Source().Replace("Width: [40mm, 80mm]", replacement, StringComparison.Ordinal));
+ Assert.Contains(parse.Diagnostics, value => value.StartsWith(diagnostic, StringComparison.Ordinal));
+ }
+
+ [Theory]
+ [InlineData("Height: 30deg", "firmament-template-with-field-type-mismatch")]
+ [InlineData("Missing: 30mm", "firmament-template-with-unknown-field")]
+ [InlineData("Height: 30mm Height: 31mm", "firmament-template-with-duplicate-field")]
+ public void With_ReportsTypedImmutableRecordDiagnostics(string replacement, string diagnostic)
+ {
+ var parse = FirmamentV2Parser.Parse(Source().Replace("Height: 30mm", replacement, StringComparison.Ordinal));
+ Assert.Contains(parse.Diagnostics, value => value.StartsWith(diagnostic, StringComparison.Ordinal));
+ }
+
+ [Fact]
+ public void ThousandRowColumnarTable_BindsASelectedRowWithinTheStaticCompilerBudget()
+ {
+ var indices = string.Join(", ", Enumerable.Range(0, 1000));
+ var widths = string.Join(", ", Enumerable.Repeat("40mm", 1000));
+ var depths = string.Join(", ", Enumerable.Repeat("20mm", 1000));
+ var source = Source()
+ .Replace("Enum WidgetKind { Small Large }", string.Empty, StringComparison.Ordinal)
+ .Replace("Kind: WidgetKind", "Index: int", StringComparison.Ordinal)
+ .Replace("Key: Kind", "Key: Index", StringComparison.Ordinal)
+ .Replace("Kind: [Small, Large]", "Index: [" + indices + "]", StringComparison.Ordinal)
+ .Replace("Width: [40mm, 80mm]", "Width: [" + widths + "]", StringComparison.Ordinal)
+ .Replace("Depth: [20mm, 30mm]", "Depth: [" + depths + "]", StringComparison.Ordinal)
+ .Replace("WidgetStandards[Small]", "WidgetStandards[500]", StringComparison.Ordinal);
+ var stopwatch = Stopwatch.StartNew();
+ var parse = FirmamentV2Parser.Parse(source);
+ stopwatch.Stop();
+
+ Assert.True(parse.IsSuccess, string.Join(Environment.NewLine, parse.Diagnostics));
+ Assert.Equal(1000, Assert.Single(parse.Document!.StaticAuthoring!.Tables!).RowCount);
+ Assert.True(stopwatch.Elapsed < TimeSpan.FromSeconds(5), $"Static Table parse took {stopwatch.Elapsed}.");
+ }
+
+ private static string Source() => """
+ Model TableDogfood {
+ Units: mm
+ Enum WidgetKind { Small Large }
+ Concept WidgetConcept {
+ Bounds: Box3
+ TopPlane: Plane
+ ChamferDistance: Length
+ }
+ Record StandardRow {
+ Kind: WidgetKind
+ Width: Length
+ Depth: Length
+ }
+ Record WidgetSpec {
+ Standard: StandardRow
+ Height: Length
+ }
+ Static Table WidgetStandards: StandardRow Key: Kind {
+ Kind: [Small, Large]
+ Width: [40mm, 80mm]
+ Depth: [20mm, 30mm]
+ }
+ Static SmallStandard = WidgetStandards[Small]
+ Static Base: WidgetSpec = WidgetSpec {
+ Standard: SmallStandard
+ Height: 20mm
+ }
+ Static TallSmall = Base with {
+ Height: 30mm
+ }
+ Template < Spec: WidgetSpec >
+ Struct Widget: WidgetConcept {
+ Require Positive => Spec.Standard.Width > 0mm && Spec.Height > 0mm
+ Concept Struct Design: WidgetConcept {
+ Bounds: Box3 {
+ Size: [Spec.Standard.Width, Spec.Standard.Depth, Spec.Height]
+ }
+ TopPlane: Bounds.Face(+Z)
+ ChamferDistance: 1mm
+ }
+ Box Body {
+ Bounds: Design.Bounds
+ }
+ Modify Body {
+ EdgeFinish TopBreak {
+ Face: Design.TopPlane
+ Target: Boundary
+ Kind: Chamfer
+ Distance: Design.ChamferDistance
+ }
+ }
+ Expose {
+ Bounds: Design.Bounds
+ TopPlane: Body.Top
+ ChamferDistance: Design.ChamferDistance
+ }
+ }
+ Struct Example = Widget < Spec: TallSmall >
+ }
+ """;
+}
diff --git a/Aetheris.Kernel.Firmament/FirmamentBuildAndExport.cs b/Aetheris.Kernel.Firmament/FirmamentBuildAndExport.cs
index 489af242..cb8d28f7 100644
--- a/Aetheris.Kernel.Firmament/FirmamentBuildAndExport.cs
+++ b/Aetheris.Kernel.Firmament/FirmamentBuildAndExport.cs
@@ -349,7 +349,11 @@ public static class FirmamentBuildAndExport
if (!step.IsSuccess) return KernelResult<FirmamentStepExportResult>.Failure(step.Diagnostics);
var instantiation = document.ConceptIr?.TemplateInstantiations?.SingleOrDefault();
var record = instantiation?.RecordArguments?.SingleOrDefault().Value;
- var parameters = record?.Members ?? authored.Parameters;
+ // A nested standards Record is still ordinary static data. Publish its leaf fields
+ // alongside instance fields so existing construction reports remain ergonomic.
+ var parameters = record is null ? authored.Parameters : record.Members
+ .Where(pair => !pair.Key.Contains('.', StringComparison.Ordinal) || pair.Key.StartsWith("Standard.", StringComparison.Ordinal))
+ .ToDictionary(pair => pair.Key.StartsWith("Standard.", StringComparison.Ordinal) ? pair.Key["Standard.".Length..] : pair.Key, pair => pair.Value, StringComparer.Ordinal);
var semantics = built.Value.Semantics.Descendants.Select(descendant => new FirmamentStandardPartSemanticReport(
descendant.StableId, descendant.Kind.ToString(), descendant.ParentStableId, descendant.Face?.Value, descendant.Metadata)).ToArray();
var report = new FirmamentStandardPartReport("ExactCoaxialPart", instantiation?.Template, built.Value.DeterministicSignature, parameters, semantics);
diff --git a/Aetheris.Kernel.Firmament/FirmamentV2/CanonicalStaticAuthoring.cs b/Aetheris.Kernel.Firmament/FirmamentV2/CanonicalStaticAuthoring.cs
index 16a62c82..a97c1531 100644
--- a/Aetheris.Kernel.Firmament/FirmamentV2/CanonicalStaticAuthoring.cs
+++ b/Aetheris.Kernel.Firmament/FirmamentV2/CanonicalStaticAuthoring.cs
@@ -21,10 +21,19 @@ internal static class CanonicalStaticAuthoring
var recordTypes = new List<FirmamentV2RecordTypeDecl>();
var arrays = new List<FirmamentV2StaticArrayDecl>();
var staticRecords = new List<FirmamentV2StaticRecordDecl>();
+ var tables = new List<FirmamentV2StaticTableDecl>();
var templates = new List<Template>();
var patterns = new List<FirmamentV2CanonicalPatternDecl>();
var requires = new List<FirmamentV2RequireDecl>();
+ // Enums are static type declarations. Modern template binding has consumed their
+ // variants already, so keep them out of the material grammar just like Records.
+ foreach (Match header in Regex.Matches(source, @"\bEnum\s+[A-Za-z_]\w*\s*\{", RegexOptions.CultureInvariant))
+ {
+ var close = MatchPair(source, source.IndexOf('{', header.Index), '{', '}');
+ if (close >= 0) changes.Add((header.Index, close - header.Index + 1, string.Empty));
+ }
+
foreach (Match header in Regex.Matches(source, @"\bRecord\s+(?<name>[A-Za-z_]\w*)\s*\{", RegexOptions.CultureInvariant))
{
var close = MatchPair(source, source.IndexOf('{', header.Index), '{', '}');
@@ -38,6 +47,19 @@ internal static class CanonicalStaticAuthoring
}
var recordByName = recordTypes.ToDictionary(x => x.Name, StringComparer.Ordinal);
+ // Tables share Record typing with Static values, but intentionally preserve their
+ // columnar spelling for source inspection. The template binder has already checked
+ // cells and lookups before this phase erases the compile-time declaration.
+ foreach (Match header in Regex.Matches(source, @"\bStatic\s+Table\s+(?<name>[A-Za-z_]\w*)\s*:\s*(?<type>[A-Za-z_]\w*)(?:\s+Key\s*:\s*(?<key>[A-Za-z_]\w*))?\s*\{", RegexOptions.CultureInvariant))
+ {
+ var open = source.IndexOf('{', header.Index); var close = MatchPair(source, open, '{', '}');
+ if (close < 0) { diagnostics.Add(Prefix + "table-malformed:" + header.Groups["name"].Value); continue; }
+ var columns = TableColumns(source, open + 1, close);
+ var rowCount = columns.Count == 0 ? 0 : columns.First().Value.Count;
+ tables.Add(new(header.Groups["name"].Value, header.Groups["type"].Value, header.Groups["key"].Success ? header.Groups["key"].Value : null, columns, rowCount, new(header.Index, close - header.Index + 1)));
+ changes.Add((header.Index, close - header.Index + 1, string.Empty));
+ }
+
foreach (Match header in Regex.Matches(source, @"\bStatic\s+(?<name>[A-Za-z_]\w*)\s*:\s*(?<type>[A-Za-z_]\w*)\s*=\s*(?<literal>[A-Za-z_]\w*)\s*\{", RegexOptions.CultureInvariant))
{
var open = source.IndexOf('{', header.Index); var close = MatchPair(source, open, '{', '}');
@@ -54,6 +76,17 @@ internal static class CanonicalStaticAuthoring
changes.Add((header.Index, close - header.Index + 1, string.Empty));
}
+ // `with` and Table row selection are compile-time Record expressions. Their actual
+ // member maps are owned by the common template binder; this phase retains no runtime
+ // representation and simply ensures source is erased before material parsing.
+ foreach (Match declaration in Regex.Matches(source, @"\bStatic\s+[A-Za-z_]\w*\s*(?::\s*[A-Za-z_]\w*)?\s*=\s*[A-Za-z_]\w*\s+with\s*\{", RegexOptions.CultureInvariant))
+ {
+ var open = source.IndexOf('{', declaration.Index); var close = MatchPair(source, open, '{', '}');
+ if (close >= 0) changes.Add((declaration.Index, close - declaration.Index + 1, string.Empty));
+ }
+ foreach (Match declaration in Regex.Matches(source, @"\bStatic\s+[A-Za-z_]\w*\s*(?::\s*[A-Za-z_]\w*)?\s*=\s*[A-Za-z_]\w*\s*\[[^\]]+\]", RegexOptions.CultureInvariant))
+ changes.Add((declaration.Index, declaration.Length, string.Empty));
+
foreach (Match header in Regex.Matches(source, @"\bStatic\s+(?<name>[A-Za-z_]\w*)\s*:\s*(?<type>[A-Za-z_]\w*)\[\]\s*=\s*\[", RegexOptions.CultureInvariant))
{
var open = source.IndexOf('[', header.Index + header.Value.LastIndexOf('[')); var close = MatchPair(source, open, '[', ']');
@@ -172,7 +205,7 @@ internal static class CanonicalStaticAuthoring
.Where(change => !erasures.Any(erase => erase.Start < change.Start && change.Start < erase.Start + erase.Length))
.OrderByDescending(change => change.Start))
source = source.Remove(change.Start, change.Length).Insert(change.Start, change.Text);
- return new(source, new(recordTypes, arrays, templates.Select(t => new FirmamentV2CanonicalTemplateDecl(t.Name, t.Type, t.Parameter, t.Body, t.Span)).ToArray(), patterns, requires, semanticConstraints, projections, staticRecords));
+ return new(source, new(recordTypes, arrays, templates.Select(t => new FirmamentV2CanonicalTemplateDecl(t.Name, t.Type, t.Parameter, t.Body, t.Span)).ToArray(), patterns, requires, semanticConstraints, projections, staticRecords, tables));
}
private static string? Instantiate(Template template, IReadOnlyDictionary<string, string> values, string id, bool patterned, List<string> diagnostics)
@@ -196,6 +229,17 @@ internal static class CanonicalStaticAuthoring
}
private static Dictionary<string, string> Fields(string body) => Regex.Matches(body, "\\b(?<name>[A-Za-z_]\\w*)\\s*:\\s*(?<value>\"[^\"]*\"|(?:Point2|Vector2|PlusMinus)\\s*\\([^)]*\\)|[A-Za-z_]\\w*|[-+]?\\d+(?:\\.\\d+)?(?:mm|deg)?)", RegexOptions.CultureInvariant)
.Cast<Match>().ToDictionary(m => m.Groups["name"].Value, m => m.Groups["value"].Value, StringComparer.Ordinal);
+ private static IReadOnlyDictionary<string, IReadOnlyList<string>> TableColumns(string source, int start, int end)
+ {
+ var columns = new Dictionary<string, IReadOnlyList<string>>(StringComparer.Ordinal);
+ foreach (Match column in Regex.Matches(source[start..end], @"\b(?<name>[A-Za-z_]\w*)\s*:\s*\[", RegexOptions.CultureInvariant))
+ {
+ var open = start + column.Index + column.Value.LastIndexOf('['); var close = MatchPair(source, open, '[', ']');
+ if (close < 0 || close > end) continue;
+ columns[column.Groups["name"].Value] = Regex.Matches(source[(open + 1)..close], "\"[^\"]*\"|[-+]?\\d+(?:\\.\\d+)?(?:mm|deg)?|[A-Za-z_]\\w*", RegexOptions.CultureInvariant).Cast<Match>().Select(match => match.Value).ToArray();
+ }
+ return columns;
+ }
private static Dictionary<string, string> RequireFields(string body) => Regex.Matches(body, @"(?<name>[A-Za-z_]\w*)\s*:\s*(?<value>.*?)(?=\s+[A-Za-z_]\w*\s*:|$)", RegexOptions.CultureInvariant | RegexOptions.Singleline)
.Cast<Match>().ToDictionary(m => m.Groups["name"].Value, m => m.Groups["value"].Value.Trim(), StringComparer.Ordinal);
private static bool TrySubject(string text, out string subject, out string property)
diff --git a/Aetheris.Kernel.Firmament/FirmamentV2/FirmamentV2Ast.cs b/Aetheris.Kernel.Firmament/FirmamentV2/FirmamentV2Ast.cs
index 435e3cdf..ea16bdaa 100644
--- a/Aetheris.Kernel.Firmament/FirmamentV2/FirmamentV2Ast.cs
+++ b/Aetheris.Kernel.Firmament/FirmamentV2/FirmamentV2Ast.cs
@@ -226,10 +226,12 @@ public sealed record FirmamentV2ProfileDecl(string Name, string Frame, double Fr
public sealed record FirmamentV2ComposeDecl(string Name, IReadOnlyList<string> Operations, FirmamentV2SourceSpan SourceSpan);
public sealed record FirmamentV2SelectionDecl(string Name, string Target, string Source, string Requirement, FirmamentV2SourceSpan SourceSpan);
/// <summary>Normalized, erased-before-materialization evidence for canonical static authoring.</summary>
-public sealed record FirmamentV2StaticAuthoringDocument(IReadOnlyList<FirmamentV2RecordTypeDecl> RecordTypes, IReadOnlyList<FirmamentV2StaticArrayDecl> Arrays, IReadOnlyList<FirmamentV2CanonicalTemplateDecl> Templates, IReadOnlyList<FirmamentV2CanonicalPatternDecl> Patterns, IReadOnlyList<FirmamentV2RequireDecl> Requires, IReadOnlyList<FirmamentV2SemanticConstraint>? SemanticConstraints = null, IReadOnlyDictionary<string, FirmamentV2PmiProjection>? PmiProjections = null, IReadOnlyList<FirmamentV2StaticRecordDecl>? StaticRecords = null);
+public sealed record FirmamentV2StaticAuthoringDocument(IReadOnlyList<FirmamentV2RecordTypeDecl> RecordTypes, IReadOnlyList<FirmamentV2StaticArrayDecl> Arrays, IReadOnlyList<FirmamentV2CanonicalTemplateDecl> Templates, IReadOnlyList<FirmamentV2CanonicalPatternDecl> Patterns, IReadOnlyList<FirmamentV2RequireDecl> Requires, IReadOnlyList<FirmamentV2SemanticConstraint>? SemanticConstraints = null, IReadOnlyDictionary<string, FirmamentV2PmiProjection>? PmiProjections = null, IReadOnlyList<FirmamentV2StaticRecordDecl>? StaticRecords = null, IReadOnlyList<FirmamentV2StaticTableDecl>? Tables = null);
public sealed record FirmamentV2RecordTypeDecl(string Name, IReadOnlyDictionary<string, string> Fields, FirmamentV2SourceSpan SourceSpan);
public sealed record FirmamentV2StaticArrayDecl(string Name, string ElementType, IReadOnlyList<IReadOnlyDictionary<string, string>> Elements, FirmamentV2SourceSpan SourceSpan);
public sealed record FirmamentV2StaticRecordDecl(string Name, string RecordType, IReadOnlyDictionary<string, string> Fields, FirmamentV2SourceSpan SourceSpan);
+/// <summary>Columnar compile-time table evidence. Rows are created only on static lookup and never enter AIR.</summary>
+public sealed record FirmamentV2StaticTableDecl(string Name, string RowType, string? KeyField, IReadOnlyDictionary<string, IReadOnlyList<string>> Columns, int RowCount, FirmamentV2SourceSpan SourceSpan);
public sealed record FirmamentV2CanonicalTemplateDecl(string Name, string ParameterType, string ParameterName, string Body, FirmamentV2SourceSpan SourceSpan);
public sealed record FirmamentV2CanonicalPatternDecl(string Name, string Source, string Template, int GeneratedCount, IReadOnlyList<string> GeneratedIds, FirmamentV2SourceSpan SourceSpan);
public sealed record FirmamentV2RequireDecl(string Name, string Expression, bool Value, FirmamentV2SourceSpan SourceSpan, string? Provenance = null, string? Subject = null, string? Expected = null, string? ToleranceSource = null);
diff --git a/Aetheris.Kernel.Firmament/FirmamentV2/FirmamentV2TemplateExpansion.cs b/Aetheris.Kernel.Firmament/FirmamentV2/FirmamentV2TemplateExpansion.cs
index a94b8d7d..3d6ce9de 100644
--- a/Aetheris.Kernel.Firmament/FirmamentV2/FirmamentV2TemplateExpansion.cs
+++ b/Aetheris.Kernel.Firmament/FirmamentV2/FirmamentV2TemplateExpansion.cs
@@ -15,8 +15,9 @@ internal sealed record TemplateValueParameterIr(string Name, string TypeName, st
internal sealed record TemplateApplicationIr(string TargetKind, string InstanceName, string TemplateName, ImmutableArray<TemplateArgumentIr> Arguments, FirmamentV2SourceSpan SourceSpan);
internal sealed record TemplateArgumentIr(string Name, string Expression, FirmamentV2SourceSpan SourceSpan);
internal sealed record TemplateRecordTypeIr(string Name, ImmutableDictionary<string, string> Fields, FirmamentV2SourceSpan SourceSpan);
-internal sealed record TemplateStaticRecordIr(string Name, string TypeName, ImmutableDictionary<string, string> Fields, FirmamentV2SourceSpan SourceSpan);
-internal sealed record BoundTemplateRecordIr(string Parameter, string TypeName, string StaticName, ImmutableDictionary<string, string> Fields, FirmamentV2SourceSpan SourceSpan);
+internal sealed record TemplateStaticRecordIr(string Name, string TypeName, ImmutableDictionary<string, string> Fields, FirmamentV2SourceSpan SourceSpan, string Provenance = "StaticRecord");
+internal sealed record TemplateStaticTableIr(string Name, string RowType, string? KeyField, ImmutableDictionary<string, ImmutableArray<string>> Columns, FirmamentV2SourceSpan SourceSpan);
+internal sealed record BoundTemplateRecordIr(string Parameter, string TypeName, string StaticName, ImmutableDictionary<string, string> Fields, FirmamentV2SourceSpan SourceSpan, string Provenance);
internal sealed record BoundTemplateArguments(ImmutableDictionary<string, string> TypeArguments, ImmutableDictionary<string, string> ValueArguments, ImmutableDictionary<string, BoundTemplateRecordIr> RecordArguments, ImmutableArray<string> DefaultedArguments);
internal sealed record TemplateSpecializationIr(TemplateDeclarationIr Template, TemplateApplicationIr Application, BoundTemplateArguments Arguments, string SpecializationIdentity, ImmutableArray<string> GeneratedDeclarationPaths);
@@ -46,13 +47,27 @@ internal static class FirmamentV2TemplateExpansion
internal const string UnknownRecordMember = Prefix + "unknown-record-member";
internal const string MaterializedRecordArgument = Prefix + "materialized-value-not-compile-time-record";
internal const string UnsupportedRecordValue = Prefix + "unsupported-record-value-form";
+ internal const string WithBaseNotRecord = Prefix + "with-base-not-record";
+ internal const string WithUnknownField = Prefix + "with-unknown-field";
+ internal const string WithDuplicateField = Prefix + "with-duplicate-field";
+ internal const string WithTypeMismatch = Prefix + "with-field-type-mismatch";
+ internal const string TableUnknownRowType = Prefix + "table-unknown-row-type";
+ internal const string TableMissingColumn = Prefix + "table-missing-column";
+ internal const string TableUnknownColumn = Prefix + "table-unknown-column";
+ internal const string TableColumnTypeMismatch = Prefix + "table-column-type-mismatch";
+ internal const string TableUnequalColumns = Prefix + "table-unequal-column-length";
+ internal const string TableDuplicateKey = Prefix + "table-duplicate-key";
+ internal const string TableMissingKey = Prefix + "table-missing-key-field";
+ internal const string TableInvalidKey = Prefix + "table-invalid-key-type";
+ internal const string TableLookupTypeMismatch = Prefix + "table-lookup-key-type-mismatch";
+ internal const string TableKeyNotFound = Prefix + "table-key-not-found";
+ internal const string TableIndexOutOfRange = Prefix + "table-index-out-of-range";
internal sealed record Result(string Source, IReadOnlyList<ConceptIrTemplateInstantiation> Instantiations);
public static Result? Expand(string source, List<string> diagnostics)
{
var declarations = ParseDeclarations(source, diagnostics);
- if (declarations.Length == 0) return new(source, []);
var byName = declarations.ToDictionary(d => d.Name, StringComparer.Ordinal);
DetectTemplateCycles(declarations, byName, diagnostics);
var applications = ParseApplications(source, byName, diagnostics);
@@ -60,7 +75,8 @@ internal static class FirmamentV2TemplateExpansion
var enums = ParseEnums(source);
var recordTypes = ParseRecordTypes(source, diagnostics);
- var staticRecords = ParseStaticRecords(source, recordTypes, enums, diagnostics);
+ var tables = ParseStaticTables(source, recordTypes, enums, diagnostics);
+ var staticRecords = ParseStaticRecords(source, recordTypes, enums, tables, diagnostics);
var changes = declarations.Select(d => (d.SourceSpan.Start, d.SourceSpan.Length, Text: string.Empty)).ToList();
var instantiations = new List<ConceptIrTemplateInstantiation>();
foreach (var application in applications)
@@ -78,7 +94,7 @@ internal static class FirmamentV2TemplateExpansion
body = RemoveRequires(body);
changes.Add((application.SourceSpan.Start, application.SourceSpan.Length, $"{template.TargetKind} {application.InstanceName}{template.HeaderTail} {{{body}}}"));
var recordArguments = bound.RecordArguments.ToDictionary(pair => pair.Key, pair => new ConceptIrTemplateRecordArgument(
- pair.Value.TypeName, pair.Value.StaticName, pair.Value.Fields, pair.Value.SourceSpan, "StaticRecord"), StringComparer.Ordinal);
+ pair.Value.TypeName, pair.Value.StaticName, pair.Value.Fields, pair.Value.SourceSpan, pair.Value.Provenance), StringComparer.Ordinal);
instantiations.Add(new(template.Name, application.InstanceName, bound.TypeArguments, bound.ValueArguments, bound.DefaultedArguments,
specialization.SpecializationIdentity, specialization.GeneratedDeclarationPaths, template.SourceSpan, application.SourceSpan,
SelectedMatchArms: selectedMatches, RecordArguments: recordArguments, RequireResults: requireResults));
@@ -199,7 +215,7 @@ internal static class FirmamentV2TemplateExpansion
{
if (!string.Equals(staticRecord.TypeName, recordType.Name, StringComparison.Ordinal))
{ diagnostics.Add(WrongRecordType + $":{parameter.Name}:expected-{recordType.Name}:actual-{staticRecord.TypeName}"); return; }
- records[parameter.Name] = new(parameter.Name, recordType.Name, staticRecord.Name, staticRecord.Fields, staticRecord.SourceSpan);
+ records[parameter.Name] = new(parameter.Name, recordType.Name, staticRecord.Name, FlattenRecordFields(staticRecord, recordTypes, staticRecords), staticRecord.SourceSpan, staticRecord.Provenance);
values[parameter.Name] = staticRecord.Name;
return;
}
@@ -210,16 +226,36 @@ internal static class FirmamentV2TemplateExpansion
}
}
+ private static ImmutableDictionary<string, string> FlattenRecordFields(TemplateStaticRecordIr record,
+ IReadOnlyDictionary<string, TemplateRecordTypeIr> recordTypes, IReadOnlyDictionary<string, TemplateStaticRecordIr> staticRecords)
+ {
+ var result = ImmutableDictionary.CreateBuilder<string, string>(StringComparer.Ordinal);
+ void Add(TemplateStaticRecordIr value, string prefix)
+ {
+ foreach (var field in value.Fields)
+ {
+ var path = prefix + field.Key;
+ result[path] = field.Value;
+ if (recordTypes[value.TypeName].Fields.TryGetValue(field.Key, out var type)
+ && recordTypes.ContainsKey(type) && staticRecords.TryGetValue(field.Value, out var nested)) Add(nested, path + ".");
+ }
+ }
+ Add(record, string.Empty);
+ return result.ToImmutable();
+ }
+
private static IReadOnlyDictionary<string, ImmutableHashSet<string>> ParseEnums(string source) => Regex.Matches(source, @"\bEnum\s+(?<name>[A-Za-z_][A-Za-z0-9_]*)\s*\{(?<body>.*?)\}", RegexOptions.Singleline | RegexOptions.CultureInvariant).Cast<Match>().ToDictionary(
m => m.Groups["name"].Value,
m => Regex.Matches(m.Groups["body"].Value, @"[A-Za-z_][A-Za-z0-9_]*", RegexOptions.CultureInvariant).Select(v => v.Value).ToImmutableHashSet(StringComparer.Ordinal), StringComparer.Ordinal);
- private static bool TypeMatches(string value, string type, IReadOnlyDictionary<string, ImmutableHashSet<string>> enums) => type switch
+ private static bool TypeMatches(string value, string type, IReadOnlyDictionary<string, ImmutableHashSet<string>> enums,
+ IReadOnlyDictionary<string, TemplateRecordTypeIr>? recordTypes = null, IReadOnlyDictionary<string, TemplateStaticRecordIr>? staticRecords = null) => type switch
{
"Length" => Regex.IsMatch(value, @"^[-+]?[0-9]+(?:\.[0-9]+)?mm$", RegexOptions.CultureInvariant),
"Angle" => Regex.IsMatch(value, @"^[-+]?[0-9]+(?:\.[0-9]+)?deg$", RegexOptions.CultureInvariant),
"String" => Regex.IsMatch(value, "^\"[^\"]*\"$", RegexOptions.CultureInvariant),
"int" => long.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out _), "float" => double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out _), "bool" => value is "true" or "false",
_ when enums.TryGetValue(type, out var variants) => variants.Contains(value),
+ _ when recordTypes?.ContainsKey(type) == true && staticRecords?.TryGetValue(value, out var record) == true => record.TypeName == type,
_ => false
};
private static bool IsBuiltInValueType(string type, IReadOnlyDictionary<string, ImmutableHashSet<string>> enums) =>
@@ -240,8 +276,45 @@ internal static class FirmamentV2TemplateExpansion
return result;
}
- private static IReadOnlyDictionary<string, TemplateStaticRecordIr> ParseStaticRecords(string source, IReadOnlyDictionary<string, TemplateRecordTypeIr> recordTypes,
+ private static IReadOnlyDictionary<string, TemplateStaticTableIr> ParseStaticTables(string source, IReadOnlyDictionary<string, TemplateRecordTypeIr> recordTypes,
IReadOnlyDictionary<string, ImmutableHashSet<string>> enums, List<string> diagnostics)
+ {
+ var tables = new Dictionary<string, TemplateStaticTableIr>(StringComparer.Ordinal);
+ foreach (Match header in Regex.Matches(source, @"\bStatic\s+Table\s+(?<name>[A-Za-z_]\w*)\s*:\s*(?<type>[A-Za-z_]\w*)(?:\s+Key\s*:\s*(?<key>[A-Za-z_]\w*))?\s*\{", RegexOptions.CultureInvariant))
+ {
+ var open = source.IndexOf('{', header.Index); var close = Matching(source, open, '{', '}');
+ var name = header.Groups["name"].Value; var type = header.Groups["type"].Value; var key = header.Groups["key"].Success ? header.Groups["key"].Value : null;
+ if (close < 0 || !recordTypes.TryGetValue(type, out var rowType)) { diagnostics.Add(TableUnknownRowType + ":" + name + ":" + type); continue; }
+ var columns = ImmutableDictionary.CreateBuilder<string, ImmutableArray<string>>(StringComparer.Ordinal);
+ var body = source[(open + 1)..close];
+ foreach (Match column in Regex.Matches(body, @"\b(?<name>[A-Za-z_]\w*)\s*:\s*\[", RegexOptions.CultureInvariant))
+ {
+ var columnOpen = open + 1 + column.Index + column.Value.LastIndexOf('['); var columnClose = Matching(source, columnOpen, '[', ']');
+ if (columnClose < 0 || columnClose > close) { diagnostics.Add(TableUnequalColumns + ":" + name + ":malformed-" + column.Groups["name"].Value); continue; }
+ var columnName = column.Groups["name"].Value;
+ if (columns.ContainsKey(columnName)) { diagnostics.Add(TableDuplicateKey + ":" + name + ":duplicate-column-" + columnName); continue; }
+ columns[columnName] = SplitValues(source[(columnOpen + 1)..columnClose]).ToImmutableArray();
+ }
+ foreach (var field in rowType.Fields.Keys.Where(field => !columns.ContainsKey(field))) diagnostics.Add(TableMissingColumn + ":" + name + ":" + field);
+ foreach (var column in columns.Keys.Where(column => !rowType.Fields.ContainsKey(column))) diagnostics.Add(TableUnknownColumn + ":" + name + ":" + column);
+ foreach (var column in columns)
+ if (rowType.Fields.TryGetValue(column.Key, out var expected))
+ foreach (var value in column.Value.Where(value => !TypeMatches(value, expected, enums, recordTypes, null))) diagnostics.Add(TableColumnTypeMismatch + ":" + name + "." + column.Key + ":expected-" + expected + ":actual-" + value);
+ var lengths = columns.Values.Select(values => values.Length).Distinct().ToArray();
+ if (lengths.Length > 1) diagnostics.Add(TableUnequalColumns + ":" + name);
+ if (key is not null)
+ {
+ if (!rowType.Fields.TryGetValue(key, out var keyType) || !columns.ContainsKey(key)) diagnostics.Add(TableMissingKey + ":" + name + ":" + key);
+ else if (keyType is not ("String" or "int") && !enums.ContainsKey(keyType)) diagnostics.Add(TableInvalidKey + ":" + name + ":" + keyType);
+ else if (columns[key].Distinct(StringComparer.Ordinal).Count() != columns[key].Length) diagnostics.Add(TableDuplicateKey + ":" + name + ":" + key);
+ }
+ tables[name] = new(name, type, key, columns.ToImmutable(), new(header.Index, close - header.Index + 1));
+ }
+ return tables;
+ }
+
+ private static IReadOnlyDictionary<string, TemplateStaticRecordIr> ParseStaticRecords(string source, IReadOnlyDictionary<string, TemplateRecordTypeIr> recordTypes,
+ IReadOnlyDictionary<string, ImmutableHashSet<string>> enums, IReadOnlyDictionary<string, TemplateStaticTableIr> tables, List<string> diagnostics)
{
var result = new Dictionary<string, TemplateStaticRecordIr>(StringComparer.Ordinal);
foreach (Match header in Regex.Matches(source, @"\bStatic\s+(?<name>[A-Za-z_]\w*)\s*:\s*(?<type>[A-Za-z_]\w*)\s*=\s*(?<literal>[A-Za-z_]\w*)\s*\{", RegexOptions.CultureInvariant))
@@ -253,16 +326,101 @@ internal static class FirmamentV2TemplateExpansion
var fields = ParseRecordFields(source[(open + 1)..close]);
foreach (var missing in recordType.Fields.Keys.Where(field => !fields.ContainsKey(field))) diagnostics.Add(Prefix + "record-missing-field:" + name + ":" + missing);
foreach (var extra in fields.Keys.Where(field => !recordType.Fields.ContainsKey(field))) diagnostics.Add(Prefix + "record-extra-field:" + name + ":" + extra);
- foreach (var field in fields.Where(field => recordType.Fields.TryGetValue(field.Key, out var expected) && !TypeMatches(field.Value, expected, enums)))
+ foreach (var field in fields.Where(field => recordType.Fields.TryGetValue(field.Key, out var expected) && !recordTypes.ContainsKey(expected) && !TypeMatches(field.Value, expected, enums, recordTypes, result)))
diagnostics.Add(Prefix + "record-field-type-mismatch:" + name + "." + field.Key + ":expected-" + recordType.Fields[field.Key] + ":actual-" + field.Value);
result[name] = new(name, typeName, fields.ToImmutableDictionary(StringComparer.Ordinal), new(header.Index, close - header.Index + 1));
}
+ foreach (Match declaration in Regex.Matches(source, @"\bStatic\s+(?<name>[A-Za-z_]\w*)\s*(?::\s*(?<type>[A-Za-z_]\w*))?\s*=\s*(?<table>[A-Za-z_]\w*)\s*\[\s*(?<lookup>[^\]]+)\s*\]", RegexOptions.CultureInvariant))
+ {
+ if (!tables.TryGetValue(declaration.Groups["table"].Value, out var table)) continue;
+ var lookup = declaration.Groups["lookup"].Value.Trim(); var index = -1;
+ if (int.TryParse(lookup, NumberStyles.Integer, CultureInfo.InvariantCulture, out var numeric)) index = numeric;
+ else if (table.KeyField is null) diagnostics.Add(TableLookupTypeMismatch + ":" + table.Name + ":index-required");
+ else
+ {
+ var keyType = recordTypes[table.RowType].Fields[table.KeyField];
+ if (!TypeMatches(lookup, keyType, enums, recordTypes, result)) diagnostics.Add(TableLookupTypeMismatch + ":" + table.Name + ":expected-" + keyType + ":actual-" + lookup);
+ else index = table.Columns[table.KeyField].IndexOf(lookup);
+ if (index < 0) diagnostics.Add(TableKeyNotFound + ":" + table.Name + ":" + lookup);
+ }
+ var rowCount = table.Columns.Count == 0 ? 0 : table.Columns.First().Value.Length;
+ if (index < 0 || index >= rowCount) { if (int.TryParse(lookup, out _)) diagnostics.Add(TableIndexOutOfRange + ":" + table.Name + ":" + lookup); continue; }
+ var requestedType = declaration.Groups["type"].Success ? declaration.Groups["type"].Value : table.RowType;
+ if (!string.Equals(requestedType, table.RowType, StringComparison.Ordinal)) { diagnostics.Add(WrongRecordType + ":" + declaration.Groups["name"].Value + ":expected-" + requestedType + ":actual-" + table.RowType); continue; }
+ var fields = table.Columns.ToImmutableDictionary(column => column.Key, column => column.Value[index], StringComparer.Ordinal);
+ var keyDetail = table.KeyField is null ? string.Empty : " key:" + table.Columns[table.KeyField][index];
+ result[declaration.Groups["name"].Value] = new(declaration.Groups["name"].Value, table.RowType, fields, new(declaration.Index, declaration.Length), "Table:" + table.Name + " row:" + index + keyDetail);
+ }
+ // Derived records are resolved in source order. This gives Static declarations the same
+ // acyclic dependency discipline as existing template defaults without runtime values.
+ var pending = Regex.Matches(source, @"\bStatic\s+(?<name>[A-Za-z_]\w*)\s*(?::\s*(?<type>[A-Za-z_]\w*))?\s*=\s*(?<base>[A-Za-z_]\w*)\s+with\s*\{", RegexOptions.CultureInvariant).Cast<Match>().ToArray();
+ while (pending.Length > 0)
+ {
+ var progressed = false;
+ var remaining = new List<Match>();
+ foreach (var declaration in pending)
+ {
+ var open = source.IndexOf('{', declaration.Index); var close = Matching(source, open, '{', '}');
+ var name = declaration.Groups["name"].Value; var baseName = declaration.Groups["base"].Value;
+ if (close < 0) { diagnostics.Add(WithBaseNotRecord + ":" + name + ":malformed"); continue; }
+ if (!result.TryGetValue(baseName, out var baseRecord)) { remaining.Add(declaration); continue; }
+ var type = declaration.Groups["type"].Success ? declaration.Groups["type"].Value : baseRecord.TypeName;
+ if (!string.Equals(type, baseRecord.TypeName, StringComparison.Ordinal)) { diagnostics.Add(WrongRecordType + ":" + name + ":expected-" + type + ":actual-" + baseRecord.TypeName); continue; }
+ var overrides = ParseRecordFields(source[(open + 1)..close]);
+ var overrideNames = Regex.Matches(source[(open + 1)..close], @"\b(?<name>[A-Za-z_]\w*)\s*:", RegexOptions.CultureInvariant).Cast<Match>().Select(match => match.Groups["name"].Value).ToArray();
+ foreach (var duplicate in overrideNames.GroupBy(value => value, StringComparer.Ordinal).Where(group => group.Count() > 1)) diagnostics.Add(WithDuplicateField + ":" + name + ":" + duplicate.Key);
+ foreach (var field in overrides.Keys.Where(field => !recordTypes[baseRecord.TypeName].Fields.ContainsKey(field))) diagnostics.Add(WithUnknownField + ":" + name + ":" + field);
+ foreach (var field in overrideNames.Where(field => recordTypes[baseRecord.TypeName].Fields.ContainsKey(field) && !overrides.ContainsKey(field))) diagnostics.Add(WithTypeMismatch + ":" + name + "." + field + ":collection-or-unsupported-value");
+ foreach (var field in overrides.Where(field => recordTypes[baseRecord.TypeName].Fields.TryGetValue(field.Key, out var expected) && !TypeMatches(field.Value, expected, enums, recordTypes, result))) diagnostics.Add(WithTypeMismatch + ":" + name + "." + field.Key + ":expected-" + recordTypes[baseRecord.TypeName].Fields[field.Key] + ":actual-" + field.Value);
+ var fields = baseRecord.Fields.SetItems(overrides);
+ var inherited = baseRecord.Fields.Values.Concat(overrides.Values).Where(result.ContainsKey).Select(value => result[value].Provenance).Distinct(StringComparer.Ordinal).ToArray();
+ result[name] = new(name, type, fields, new(declaration.Index, close - declaration.Index + 1), baseRecord.Provenance + "; derivedFrom:" + baseName + (inherited.Length == 0 ? string.Empty : "; origin:" + string.Join("|", inherited)) + "; overrides:" + string.Join(",", overrides.Keys.OrderBy(value => value, StringComparer.Ordinal)));
+ progressed = true;
+ }
+ if (!progressed)
+ {
+ foreach (var declaration in remaining) diagnostics.Add(WithBaseNotRecord + ":" + declaration.Groups["name"].Value + ":" + declaration.Groups["base"].Value);
+ break;
+ }
+ pending = remaining.ToArray();
+ }
+ foreach (Match declaration in Regex.Matches(source, @"\bStatic\s+(?<name>[A-Za-z_]\w*)\s*(?::\s*(?<type>[A-Za-z_]\w*))?\s*=\s*(?<table>[A-Za-z_]\w*)\s*\[\s*(?<lookup>[^\]]+)\s*\]", RegexOptions.CultureInvariant))
+ {
+ if (!tables.TryGetValue(declaration.Groups["table"].Value, out var table)) continue;
+ var lookup = declaration.Groups["lookup"].Value.Trim(); var index = -1;
+ if (int.TryParse(lookup, NumberStyles.Integer, CultureInfo.InvariantCulture, out var numeric)) index = numeric;
+ else if (table.KeyField is null) diagnostics.Add(TableLookupTypeMismatch + ":" + table.Name + ":index-required");
+ else
+ {
+ var keyType = recordTypes[table.RowType].Fields[table.KeyField];
+ if (!TypeMatches(lookup, keyType, enums, recordTypes, result)) diagnostics.Add(TableLookupTypeMismatch + ":" + table.Name + ":expected-" + keyType + ":actual-" + lookup);
+ else index = table.Columns[table.KeyField].IndexOf(lookup);
+ if (index < 0) diagnostics.Add(TableKeyNotFound + ":" + table.Name + ":" + lookup);
+ }
+ var rowCount = table.Columns.Count == 0 ? 0 : table.Columns.First().Value.Length;
+ if (index < 0 || index >= rowCount) { if (int.TryParse(lookup, out _)) diagnostics.Add(TableIndexOutOfRange + ":" + table.Name + ":" + lookup); continue; }
+ var requestedType = declaration.Groups["type"].Success ? declaration.Groups["type"].Value : table.RowType;
+ if (!string.Equals(requestedType, table.RowType, StringComparison.Ordinal)) { diagnostics.Add(WrongRecordType + ":" + declaration.Groups["name"].Value + ":expected-" + requestedType + ":actual-" + table.RowType); continue; }
+ var fields = table.Columns.ToImmutableDictionary(column => column.Key, column => column.Value[index], StringComparer.Ordinal);
+ var keyDetail = table.KeyField is null ? string.Empty : " key:" + table.Columns[table.KeyField][index];
+ result[declaration.Groups["name"].Value] = new(declaration.Groups["name"].Value, table.RowType, fields, new(declaration.Index, declaration.Length), "Table:" + table.Name + " row:" + index + keyDetail);
+ }
+ foreach (var record in result.Values.ToArray())
+ {
+ var origins = record.Fields.Values.Where(result.ContainsKey).Select(value => result[value].Provenance).Where(value => value != record.Provenance).Distinct(StringComparer.Ordinal).ToArray();
+ if (origins.Length > 0 && !record.Provenance.Contains("origin:", StringComparison.Ordinal)) result[record.Name] = record with { Provenance = record.Provenance + "; origin:" + string.Join("|", origins) };
+ }
+ foreach (var record in result.Values)
+ foreach (var field in record.Fields.Where(field => recordTypes[record.TypeName].Fields.TryGetValue(field.Key, out var expected) && !TypeMatches(field.Value, expected, enums, recordTypes, result)))
+ diagnostics.Add(Prefix + "record-field-type-mismatch:" + record.Name + "." + field.Key + ":expected-" + recordTypes[record.TypeName].Fields[field.Key] + ":actual-" + field.Value);
return result;
}
+ private static IReadOnlyList<string> SplitValues(string text) => Regex.Matches(text, "\"[^\"]*\"|[-+]?\\d+(?:\\.\\d+)?(?:mm|deg)?|[A-Za-z_]\\w*", RegexOptions.CultureInvariant).Cast<Match>().Select(match => match.Value).ToArray();
+
private static Dictionary<string, string> ParseRecordFields(string body) => Regex.Matches(body,
"\\b(?<name>[A-Za-z_]\\w*)\\s*:\\s*(?<value>\"[^\"]*\"|(?:Point2|Point3|Vector2|Vector3|Axis)\\s*\\([^)]*\\)|[A-Za-z_]\\w*|[-+]?\\d+(?:\\.\\d+)?(?:mm|deg)?)",
- RegexOptions.CultureInvariant).Cast<Match>().ToDictionary(m => m.Groups["name"].Value, m => m.Groups["value"].Value, StringComparer.Ordinal);
+ RegexOptions.CultureInvariant).Cast<Match>().GroupBy(m => m.Groups["name"].Value, StringComparer.Ordinal).ToDictionary(group => group.Key, group => group.Last().Groups["value"].Value, StringComparer.Ordinal);
private static bool ConceptExists(string concept, string source) => Regex.IsMatch(source, $@"\bConcept\s+{Regex.Escape(concept)}\s*\{{", RegexOptions.CultureInvariant);
private static bool Satisfies(string type, string concept, string source)
{
@@ -275,8 +433,8 @@ internal static class FirmamentV2TemplateExpansion
{
foreach (var record in bound.RecordArguments.Values)
{
- foreach (var field in record.Fields)
- body = Regex.Replace(body, $@"\b{Regex.Escape(record.Parameter)}\s*\.\s*{Regex.Escape(field.Key)}\b", _ => field.Value, RegexOptions.CultureInvariant);
+ foreach (var field in record.Fields.OrderByDescending(field => field.Key.Length))
+ body = Regex.Replace(body, $@"\b{Regex.Escape(record.Parameter)}\s*\.\s*{Regex.Escape(field.Key).Replace("\\.", @"\s*\.\s*", StringComparison.Ordinal)}\b", _ => field.Value, RegexOptions.CultureInvariant);
}
return bound.TypeArguments.Concat(bound.ValueArguments.Where(pair => !bound.RecordArguments.ContainsKey(pair.Key)))
.Aggregate(body, (result, pair) => Regex.Replace(result, $@"\b{Regex.Escape(pair.Key)}\b", pair.Value));
diff --git a/Aetheris.Server/Api/CadmataFixtureService.cs b/Aetheris.Server/Api/CadmataFixtureService.cs
index a060b795..ab68e9ff 100644
--- a/Aetheris.Server/Api/CadmataFixtureService.cs
+++ b/Aetheris.Server/Api/CadmataFixtureService.cs
@@ -124,6 +124,7 @@ internal static class CadmataFixtureService
metadata["recordParameter"] = recordArgument?.Key ?? string.Empty;
metadata["recordType"] = recordArgument?.Value.RecordType ?? string.Empty;
metadata["recordSource"] = recordArgument?.Value.StaticValue ?? string.Empty;
+ metadata["recordProvenance"] = recordArgument?.Value.Provenance ?? string.Empty;
foreach (var require in instantiation.RequireResults ?? new Dictionary<string, string>()) metadata["require." + require.Key] = require.Value;
}
if (semantic.Metadata is not null) metadata["engineeringMetadata"] = semantic.Metadata;
diff --git a/README.md b/README.md
index e2f558ed..e9f5de8d 100644
--- a/README.md
+++ b/README.md
@@ -8,6 +8,26 @@
Firmament is a deterministic DSL for generating CAD geometry (STEP AP242) via Aetheris.
+## Static engineering data
+
+Firmament `Record` values are immutable typed specifications. A finite standards
+catalog can be authored directly as a columnar static Table, selected as a
+Record, and derived without copying it:
+
+```firmament
+Static Table Sizes: BoltRow Key: Size {
+ Size: [M8, M10]
+ Diameter: [8mm, 10mm]
+}
+Static M8 = Sizes[M8]
+Static M8Long = M8 with { Length: 80mm }
+```
+
+Tables and `with` values are compile-time-only: they feed typed Template Record
+parameters and are erased before geometry AIR. A Table is not a runtime
+dataframe, SQL surface, or spreadsheet import format. See
+[the M1 language note](docs/preview2/firmament-tables-with-m1.md).
+
## Preview 1 release
Aetheris 2.0.0-preview.1 is available as a complete Windows x64 bundle and as
diff --git a/docs/preview2/firmament-tables-with-m1.md b/docs/preview2/firmament-tables-with-m1.md
new file mode 100644
index 00000000..e90dc8ec
--- /dev/null
+++ b/docs/preview2/firmament-tables-with-m1.md
@@ -0,0 +1,59 @@
+# Firmament Tables and immutable Record derivation (M1)
+
+## Audit and decision
+
+Before M1, Firmament had typed `Record` declarations, scalar `Static` record
+literals, a limited static array expansion route, member substitution for typed
+Template parameters, and erase-before-AIR semantics. It did **not** have a
+`with` expression. Static Records were represented during template binding as a
+typed immutable field map; arrays were separately represented as lists of those
+maps. This M1 extends that binding path rather than adding a runtime data system.
+
+The canonical Table form is:
+
+```firmament
+Static Table ISO4017: HexBoltStandardRow Key: Size {
+ Size: [M8, M10]
+ NominalDiameter: [8mm, 10mm]
+ Pitch: [1.25mm, 1.5mm]
+}
+```
+
+`Table` is a finite, compile-time-only, columnar declaration. Its one row type
+must be a `Record`; every Record field has exactly one typed column; columns have
+one shared row count (zero is allowed). Table storage remains columnar in the
+static IR and inspection document. Indexing creates an ordinary immutable Record
+view, not a runtime table value:
+
+```firmament
+Static M8 = ISO4017[M8]
+Static M8Long = M8 with { Length: 80mm }
+```
+
+Keys are optional. When declared, the key field must be `Enum`, `String`, or
+`int`, exist as a column, and have unique values. Numeric lookup uses existing
+bounded static-index semantics; keyed lookup is exact only.
+
+## `with`
+
+`BaseRecord with { Field: Value }` returns a fresh value with exactly the base
+Record type. It is immutable, preserves all omitted fields, composes in source
+order, and is erased before Feature AIR. The binder rejects non-Records, unknown
+or duplicate fields, incompatible scalar/collection values, wrong Record types,
+and materialized values. Static dependency resolution detects unresolved/cyclic
+derivations as static-record failures.
+
+Provenance is retained on the bound Record argument: Table name, row, key, base
+Record, and sorted overridden fields are carried to `TemplateInstance` and
+Cadmata metadata. It is source-map evidence, not an opaque generated literal.
+
+## Diagnostics and limits
+
+M1 diagnoses unknown/non-Record row types, missing/unknown columns, column type
+mismatches, unequal lengths, invalid/duplicate keys, lookup type/key/bounds
+errors, and all `with` type errors. It deliberately has no filter, join, query,
+CSV import, runtime iteration, mutation, or dataframe semantics.
+
+Tables are dependency-sensitive: a specialization identity hashes the resolved
+bound Record leaves, so an unrelated row does not change an instance selected
+from a different row. Row order and keys are deterministic.
diff --git a/docs/preview2/hexbolt-standards-data-m1.md b/docs/preview2/hexbolt-standards-data-m1.md
new file mode 100644
index 00000000..9f5f9831
--- /dev/null
+++ b/docs/preview2/hexbolt-standards-data-m1.md
@@ -0,0 +1,17 @@
+# HexBolt standards data M1
+
+`hexbolt_template_m2.firmament` now separates `HexBoltStandardRow` (head and
+shank family geometry) from `HexBoltSpec` (stable identity, length, thread
+extent, designation, and property class). `HexBoltSpec.Standard` is a nested
+Record. Templates access e.g. `Spec.Standard.HeadAcrossFlats`; nested static
+member paths are resolved during template specialization.
+
+The fixture's `McMasterThreadlessHexBoltGeometry` is a keyed columnar Table.
+Its M8 row is the audited McMaster-Carr 91180A151 threadless STEP reference
+already used by the M1 oracle. The M10 row is explicitly provisional existing
+fixture/test data, not a claim of ISO 4017 certification. `M10x50` derives from
+the M8 instance with `with`; the 8.25 × 37.5 mm nonstandard fixture still authors
+an arbitrary `HexBoltStandardRow` and `HexBoltSpec` directly.
+
+This is intentionally not a complete ISO dataset. The next standards milestone
+should add cited rows and a broader catalog without changing compiler logic.
diff --git a/docs/preview2/hexbolt-template-m2.md b/docs/preview2/hexbolt-template-m2.md
index 14f8c432..21c7b14a 100644
--- a/docs/preview2/hexbolt-template-m2.md
+++ b/docs/preview2/hexbolt-template-m2.md
@@ -9,7 +9,8 @@ under-head blend, cylindrical shank, and conical tip. M2 preserves that path.
M2 moves family authoring to
`testdata/firmament/examples/hexbolt_template_m2.firmament`. The canonical source
-declares `BoltConcept`, the family-specific `HexBoltSpec`, scalar Static records,
+declares `BoltConcept`, `HexBoltStandardRow`, the instance-specific `HexBoltSpec`,
+a keyed columnar Static Table, and immutable derived Static records,
and `Template < Spec: HexBoltSpec > Struct HexBolt: BoltConcept`. It contains no
`StandardPart Family: HexBolt` dispatch.
@@ -58,8 +59,10 @@ constraint solver.
## Dogfood and comparison
The same Template source contains data for the McMaster 91180A151 M8 × 35
-reference, M10 × 50, and a deliberately nonstandard 8.25 × 37.5 mm bolt. Tests
-bind each Static record without changing geometry source.
+reference, M10 × 50 derived with `with`, and a deliberately nonstandard 8.25 ×
+37.5 mm bolt. Tests bind each Static record without changing geometry source.
+See [hexbolt-standards-data-m1.md](hexbolt-standards-data-m1.md) for data
+provenance and the standards/instance boundary.
Their deterministic construction signatures are respectively
`b2e86e279016f55625e074153f76561144dc7c3c85cbc943c3e3492a7e19acda`,
diff --git a/testdata/firmament/examples/hexbolt_template_m2.firmament b/testdata/firmament/examples/hexbolt_template_m2.firmament
index f38a49de..aa65cacd 100644
--- a/testdata/firmament/examples/hexbolt_template_m2.firmament
+++ b/testdata/firmament/examples/hexbolt_template_m2.firmament
@@ -9,10 +9,16 @@ Model HexBoltTemplateM2 {
Axis: Axis
}
- Record HexBoltSpec {
- StableId: String
+ Enum MetricBoltSize {
+ M8
+ M10
+ }
+
+ // McMaster 91180A151 is the audited M8 source. The M10 values below are
+ // existing fixture/test data, retained as a provisional authoring example.
+ Record HexBoltStandardRow {
+ Size: MetricBoltSize
NominalDiameter: Length
- Length: Length
HeadAcrossFlats: Length
HeadHeight: Length
TopFlatDiameter: Length
@@ -20,47 +26,52 @@ Model HexBoltTemplateM2 {
UnderHeadRadius: Length
TipChamferLength: Length
TipDiameter: Length
+ }
+
+ Record HexBoltSpec {
+ StableId: String
+ Standard: HexBoltStandardRow
+ Length: Length
ThreadLength: Length
ThreadDesignation: String
PropertyClass: String
}
+ Static Table McMasterThreadlessHexBoltGeometry: HexBoltStandardRow Key: Size {
+ Size: [M8, M10]
+ NominalDiameter: [8mm, 10mm]
+ HeadAcrossFlats: [13mm, 17mm]
+ HeadHeight: [5.3mm, 6.4mm]
+ TopFlatDiameter: [12.35mm, 16mm]
+ TopChamferAngle: [25deg, 25deg]
+ UnderHeadRadius: [0.2mm, 0.2mm]
+ TipChamferLength: [0.9375mm, 0.9375mm]
+ TipDiameter: [6.125mm, 8mm]
+ }
+
+ Static M8Standard = McMasterThreadlessHexBoltGeometry[M8]
+ Static M10Standard = McMasterThreadlessHexBoltGeometry[M10]
+
Static McMaster91180A151: HexBoltSpec = HexBoltSpec {
StableId: "McMaster91180A151"
- NominalDiameter: 8mm
+ Standard: M8Standard
Length: 35mm
- HeadAcrossFlats: 13mm
- HeadHeight: 5.3mm
- TopFlatDiameter: 12.35mm
- TopChamferAngle: 25deg
- UnderHeadRadius: 0.2mm
- TipChamferLength: 0.9375mm
- TipDiameter: 6.125mm
ThreadLength: 22mm
ThreadDesignation: "M8 x 1.25"
PropertyClass: "8.8"
}
- Static M10x50: HexBoltSpec = HexBoltSpec {
+ Static M10x50 = McMaster91180A151 with {
StableId: "M10x50"
- NominalDiameter: 10mm
+ Standard: M10Standard
Length: 50mm
- HeadAcrossFlats: 17mm
- HeadHeight: 6.4mm
- TopFlatDiameter: 16mm
- TopChamferAngle: 25deg
- UnderHeadRadius: 0.2mm
- TipChamferLength: 0.9375mm
- TipDiameter: 8mm
ThreadLength: 26mm
ThreadDesignation: "M10 x 1.5"
- PropertyClass: "8.8"
}
- Static Nonstandard825x375: HexBoltSpec = HexBoltSpec {
- StableId: "Nonstandard825x375"
+ Static NonstandardGeometry: HexBoltStandardRow = HexBoltStandardRow {
+ Size: M8
NominalDiameter: 8.25mm
- Length: 37.5mm
HeadAcrossFlats: 13.4mm
HeadHeight: 5.5mm
TopFlatDiameter: 12.7mm
@@ -68,6 +79,12 @@ Model HexBoltTemplateM2 {
UnderHeadRadius: 0.22mm
TipChamferLength: 1mm
TipDiameter: 6.3mm
+ }
+
+ Static Nonstandard825x375: HexBoltSpec = HexBoltSpec {
+ StableId: "Nonstandard825x375"
+ Standard: NonstandardGeometry
+ Length: 37.5mm
ThreadLength: 23mm
ThreadDesignation: "M8.25 experimental"
PropertyClass: "8.8"
@@ -75,39 +92,39 @@ Model HexBoltTemplateM2 {
Template < Spec: HexBoltSpec >
Struct HexBolt: BoltConcept {
- Require PositiveNominal => Spec.NominalDiameter > 0mm
- Require HeadWiderThanShank => Spec.HeadAcrossFlats > Spec.NominalDiameter
- Require TopFlatInsideHex => Spec.TopFlatDiameter < Spec.HeadAcrossFlats
- Require PositiveHead => Spec.HeadHeight > 0mm
- Require ConeAngleValid => Spec.TopChamferAngle > 0deg && Spec.TopChamferAngle < 90deg
- Require BlendNonnegative => Spec.UnderHeadRadius >= 0mm
- Require TipReducesDiameter => Spec.TipDiameter < Spec.NominalDiameter
- Require TipFitsLength => Spec.TipChamferLength > 0mm && Spec.TipChamferLength < Spec.Length
+ Require PositiveNominal => Spec.Standard.NominalDiameter > 0mm
+ Require HeadWiderThanShank => Spec.Standard.HeadAcrossFlats > Spec.Standard.NominalDiameter
+ Require TopFlatInsideHex => Spec.Standard.TopFlatDiameter < Spec.Standard.HeadAcrossFlats
+ Require PositiveHead => Spec.Standard.HeadHeight > 0mm
+ Require ConeAngleValid => Spec.Standard.TopChamferAngle > 0deg && Spec.Standard.TopChamferAngle < 90deg
+ Require BlendNonnegative => Spec.Standard.UnderHeadRadius >= 0mm
+ Require TipReducesDiameter => Spec.Standard.TipDiameter < Spec.Standard.NominalDiameter
+ Require TipFitsLength => Spec.Standard.TipChamferLength > 0mm && Spec.Standard.TipChamferLength < Spec.Length
Require ThreadFits => Spec.ThreadLength >= 0mm && Spec.ThreadLength <= Spec.Length
ExactCoaxialPart Body {
Axis: +X
RegularPolygonPrism Head {
Sides: 6
- AcrossFlats: Spec.HeadAcrossFlats
- AxialHeight: Spec.HeadHeight
+ AcrossFlats: Spec.Standard.HeadAcrossFlats
+ AxialHeight: Spec.Standard.HeadHeight
}
CoaxialConeTrim TopTreatment {
- FlatDiameter: Spec.TopFlatDiameter
- ConeAngle: Spec.TopChamferAngle
+ FlatDiameter: Spec.Standard.TopFlatDiameter
+ ConeAngle: Spec.Standard.TopChamferAngle
IntersectionCurve: ExactConic
}
BoundedTorusBlend HeadToShaft {
- BlendRadius: Spec.UnderHeadRadius
+ BlendRadius: Spec.Standard.UnderHeadRadius
SplitPolicy: PeriodicHalfSupports
}
Cylinder Shaft {
- ShaftDiameter: Spec.NominalDiameter
+ ShaftDiameter: Spec.Standard.NominalDiameter
AxialLength: Spec.Length
}
CoaxialConeFrustum EndTreatment {
- EndChamferLength: Spec.TipChamferLength
- EndDiameter: Spec.TipDiameter
+ EndChamferLength: Spec.Standard.TipChamferLength
+ EndDiameter: Spec.Standard.TipDiameter
}
SemanticAxialRegion ThreadRegion {
SemanticAxialRegionLength: Spec.ThreadLength
@@ -121,7 +138,7 @@ Model HexBoltTemplateM2 {
}
Expose {
- NominalDiameter: Spec.NominalDiameter
+ NominalDiameter: Spec.Standard.NominalDiameter
Length: Spec.Length
ThreadLength: Spec.ThreadLength
ThreadDesignation: Spec.ThreadDesignation
diff --git a/tools/vscode-firmament/syntaxes/firmament.tmLanguage.json b/tools/vscode-firmament/syntaxes/firmament.tmLanguage.json
index 8930f62b..ad876d78 100644
--- a/tools/vscode-firmament/syntaxes/firmament.tmLanguage.json
+++ b/tools/vscode-firmament/syntaxes/firmament.tmLanguage.json
@@ -49,9 +49,9 @@
},
{
"name": "keyword.declaration.firmament",
- "match": "\\b(?:Model|Box|Cylinder|RoundedBox|Frustum|Concept|Struct|Construction|Plane|Path|Profile|Compose|Modify|Selection|Record|Static|Template|Pattern|Match|Require|Pmi|Datum|Assert|InlineStep|Recognize|Replace|EdgeFinish|Hole|Slot|Region|Segment|Loop|Base|Extrude)\\b"
+ "match": "\\b(?:Model|Box|Cylinder|RoundedBox|Frustum|Concept|Struct|Construction|Plane|Path|Profile|Compose|Modify|Selection|Record|Static|Table|Template|Pattern|Match|Require|Pmi|Datum|Assert|InlineStep|Recognize|Replace|EdgeFinish|Hole|Slot|Region|Segment|Loop|Base|Extrude)\\b"
},
- { "name": "keyword.control.firmament", "match": "\\b(?:With|From|Using|On|Over|As|Current)\\b" }
+ { "name": "keyword.control.firmament", "match": "\\b(?:with|With|From|Using|On|Over|As|Current|Key)\\b" }
]
},
"fields": {