Skip to content

Next Gen STEP AP242 Native-ish BREP Geometric Modeling Kernel

Harden STEP242 subset parser tokenization and robustness

Yuechen Li1a2657f15439
diff --git a/Aetheris.Kernel.Core.Tests/Step242/Step242SubsetParserRobustnessTests.cs b/Aetheris.Kernel.Core.Tests/Step242/Step242SubsetParserRobustnessTests.cs
new file mode 100644
index 00000000..b2515b47
--- /dev/null
+++ b/Aetheris.Kernel.Core.Tests/Step242/Step242SubsetParserRobustnessTests.cs
@@ -0,0 +1,106 @@
+using Aetheris.Kernel.Core.Step242;
+
+namespace Aetheris.Kernel.Core.Tests.Step242;
+
+public sealed class Step242SubsetParserRobustnessTests
+{
+ [Fact]
+ public void Parse_SupportsIsoWrapperMultilineCommentsAndWhitespace()
+ {
+ const string text = """
+ ISO-10303-21;
+ HEADER;
+ FILE_DESCRIPTION(('sample'),'2;1');
+ ENDSEC;
+ DATA;
+ /* multiline
+ comment */
+ #1 = cartesian_point
+ (
+ 'A',
+ (1E-3, -2.5E+02, .5)
+ )
+ ;
+ ENDSEC;
+ END-ISO-10303-21;
+ """;
+
+ var result = Step242SubsetParser.Parse(text);
+
+ Assert.True(result.IsSuccess, string.Join(" | ", result.Diagnostics.Select(d => d.Message)));
+ var entity = Assert.Single(result.Value.Entities);
+ Assert.Equal("CARTESIAN_POINT", entity.Name);
+ var coords = Assert.IsType<Step242ListValue>(entity.Arguments[1]);
+ Assert.Collection(
+ coords.Items,
+ item => Assert.Equal(1e-3d, Assert.IsType<Step242NumberValue>(item).Value, 6),
+ item => Assert.Equal(-250d, Assert.IsType<Step242NumberValue>(item).Value, 6),
+ item => Assert.Equal(0.5d, Assert.IsType<Step242NumberValue>(item).Value, 6));
+ }
+
+ [Fact]
+ public void Parse_SupportsEnumsLogicalDerivedOmittedAndNestedLists()
+ {
+ const string text = """
+ DATA;
+ #1=TEST_ENTITY((#2,#3),($,.T.,*),(),.UNSPECIFIED.,.POSITIVE.,.F.);
+ #2=ITEM();
+ #3=ITEM();
+ ENDSEC;
+ """;
+
+ var result = Step242SubsetParser.Parse(text);
+
+ Assert.True(result.IsSuccess, string.Join(" | ", result.Diagnostics.Select(d => d.Message)));
+ var entity = result.Value.Entities.Single(e => e.Id == 1);
+
+ var group = Assert.IsType<Step242ListValue>(entity.Arguments[1]);
+ Assert.Equal(3, group.Items.Count);
+ Assert.IsType<Step242OmittedValue>(group.Items[0]);
+ Assert.True(Assert.IsType<Step242BooleanValue>(group.Items[1]).Value);
+ Assert.IsType<Step242OmittedValue>(group.Items[2]);
+
+ Assert.Equal("UNSPECIFIED", Assert.IsType<Step242EnumValue>(entity.Arguments[3]).Value);
+ Assert.Equal("POSITIVE", Assert.IsType<Step242EnumValue>(entity.Arguments[4]).Value);
+ Assert.False(Assert.IsType<Step242BooleanValue>(entity.Arguments[5]).Value);
+ }
+
+ [Fact]
+ public void Parse_SupportsDoubledAndEncodedStringContentAsRaw()
+ {
+ const string text = """
+ DATA;
+ #1=ITEM('O''Brien','\X2\03A9\X0\');
+ ENDSEC;
+ """;
+
+ var result = Step242SubsetParser.Parse(text);
+
+ Assert.True(result.IsSuccess, string.Join(" | ", result.Diagnostics.Select(d => d.Message)));
+ var entity = Assert.Single(result.Value.Entities);
+ Assert.Equal("O'Brien", Assert.IsType<Step242StringValue>(entity.Arguments[0]).Value);
+ Assert.Equal("\\X2\\03A9\\X0\\", Assert.IsType<Step242StringValue>(entity.Arguments[1]).Value);
+ }
+
+ [Fact]
+ public void Parse_FailsDeterministically_ForMalformedInputs()
+ {
+ var cases = new Dictionary<string, string>
+ {
+ ["unterminated-string"] = "DATA;#1=ITEM('abc);ENDSEC;",
+ ["unbalanced-parens"] = "DATA;#1=ITEM((1,2);ENDSEC;",
+ ["bad-exponent"] = "DATA;#1=ITEM(1E+);ENDSEC;",
+ ["duplicate-id"] = "DATA;#1=ITEM();#1=OTHER();ENDSEC;",
+ ["ref-non-int"] = "DATA;#1=ITEM(#A);ENDSEC;",
+ ["malformed-enum"] = "DATA;#1=ITEM(.BAD);ENDSEC;"
+ };
+
+ foreach (var (name, text) in cases)
+ {
+ var result = Step242SubsetParser.Parse(text);
+ Assert.False(result.IsSuccess, $"Case '{name}' unexpectedly succeeded.");
+ Assert.NotEmpty(result.Diagnostics);
+ Assert.StartsWith("Parser.", result.Diagnostics[0].Source ?? string.Empty, StringComparison.Ordinal);
+ }
+ }
+}
diff --git a/Aetheris.Kernel.Core/Step242/Step242SubsetParser.cs b/Aetheris.Kernel.Core/Step242/Step242SubsetParser.cs
index 946f1d88..f660364c 100644
--- a/Aetheris.Kernel.Core/Step242/Step242SubsetParser.cs
+++ b/Aetheris.Kernel.Core/Step242/Step242SubsetParser.cs
@@ -41,6 +41,10 @@ internal static class Step242SubsetParser
{
return Failure(ex.Message, ex.SourceTag);
}
+ catch (Exception ex)
+ {
+ return Failure($"Unexpected parser failure: {ex.Message}", "Parser.Semantics");
+ }
var duplicateId = entities
.GroupBy(e => e.Id)
@@ -67,11 +71,54 @@ internal static class Step242SubsetParser
public char Peek() => text[_index];
+ private bool TryPeekNext(out char c)
+ {
+ var next = _index + 1;
+ if (next < text.Length)
+ {
+ c = text[next];
+ return true;
+ }
+
+ c = default;
+ return false;
+ }
+
public void SkipWhitespace()
{
- while (!End && char.IsWhiteSpace(text[_index]))
+ while (!End)
{
- _index++;
+ if (char.IsWhiteSpace(text[_index]))
+ {
+ _index++;
+ continue;
+ }
+
+ if (text[_index] == '/' && TryPeekNext(out var next) && next == '*')
+ {
+ _index += 2;
+ var terminated = false;
+ while (!End)
+ {
+ if (text[_index] == '*' && TryPeekNext(out next) && next == '/')
+ {
+ _index += 2;
+ terminated = true;
+ break;
+ }
+
+ _index++;
+ }
+
+ if (!terminated)
+ {
+ throw Error("Unterminated comment block.");
+ }
+
+ continue;
+ }
+
+ break;
}
}
@@ -83,6 +130,7 @@ internal static class Step242SubsetParser
Expect('=');
SkipWhitespace();
var name = ReadIdentifier();
+ name = name.ToUpperInvariant();
SkipWhitespace();
Expect('(');
var args = ReadArgumentList();
@@ -93,14 +141,42 @@ internal static class Step242SubsetParser
public void ReadToStatementTerminator()
{
+ var depth = 0;
while (!End)
{
var c = text[_index++];
+ if (c == '\'')
+ {
+ SkipStringLiteralBody();
+ continue;
+ }
+
+ if (c == '/' && !End && Peek() == '*')
+ {
+ _index++;
+ SkipCommentBody();
+ continue;
+ }
+
+ if (c == '(')
+ {
+ depth++;
+ continue;
+ }
+
+ if (c == ')' && depth > 0)
+ {
+ depth--;
+ continue;
+ }
+
if (c == ';')
{
return;
}
}
+
+ throw Error("Unable to recover statement boundary before end of text.", "Parser.Semantics");
}
private List<Step242Value> ReadArgumentList()
@@ -154,8 +230,9 @@ internal static class Step242SubsetParser
'#' => ReadReference(),
'\'' => ReadString(),
'$' => ReadOmitted(),
+ '*' => ReadDerivedOmitted(),
'(' => ReadList(),
- '.' => ReadEnumOrLogical(),
+ '.' => TryPeekNext(out var next) && char.IsDigit(next) ? ReadNumber() : ReadEnumOrLogical(),
'+' or '-' or >= '0' and <= '9' => ReadNumber(),
_ => throw Error($"Unsupported value token '{c}'.")
};
@@ -198,6 +275,12 @@ internal static class Step242SubsetParser
return Step242OmittedValue.Instance;
}
+ private Step242Value ReadDerivedOmitted()
+ {
+ Expect('*');
+ return Step242OmittedValue.Instance;
+ }
+
private Step242Value ReadList()
{
Expect('(');
@@ -241,6 +324,8 @@ internal static class Step242SubsetParser
var token = ReadIdentifier();
Expect('.');
+ token = token.ToUpperInvariant();
+
if (string.Equals(token, "T", StringComparison.OrdinalIgnoreCase))
{
return new Step242BooleanValue(true);
@@ -262,20 +347,29 @@ internal static class Step242SubsetParser
_index++;
}
+ var hasLeadingDigits = false;
while (!End && char.IsDigit(Peek()))
{
_index++;
+ hasLeadingDigits = true;
}
+ var hasFractionDigits = false;
if (!End && Peek() == '.')
{
_index++;
while (!End && char.IsDigit(Peek()))
{
_index++;
+ hasFractionDigits = true;
}
}
+ if (!hasLeadingDigits && !hasFractionDigits)
+ {
+ throw Error("Invalid numeric literal.");
+ }
+
if (!End && (Peek() == 'E' || Peek() == 'e'))
{
_index++;
@@ -284,9 +378,16 @@ internal static class Step242SubsetParser
_index++;
}
+ var exponentDigits = 0;
while (!End && char.IsDigit(Peek()))
{
_index++;
+ exponentDigits++;
+ }
+
+ if (exponentDigits == 0)
+ {
+ throw Error("Invalid exponent in numeric literal.");
}
}
@@ -341,7 +442,45 @@ internal static class Step242SubsetParser
_index++;
}
- private Step242ParseException Error(string message) => new($"{message} (position {_index}).", "Parser.Lexer");
+ private void SkipStringLiteralBody()
+ {
+ while (!End)
+ {
+ var c = text[_index++];
+ if (c != '\'')
+ {
+ continue;
+ }
+
+ if (!End && Peek() == '\'')
+ {
+ _index++;
+ continue;
+ }
+
+ return;
+ }
+
+ throw Error("Unterminated string literal.");
+ }
+
+ private void SkipCommentBody()
+ {
+ while (!End)
+ {
+ if (text[_index] == '*' && TryPeekNext(out var next) && next == '/')
+ {
+ _index += 2;
+ return;
+ }
+
+ _index++;
+ }
+
+ throw Error("Unterminated comment block.");
+ }
+
+ private Step242ParseException Error(string message, string sourceTag = "Parser.Lexer") => new($"{message} (position {_index}).", sourceTag);
}
}