Skip to content

Commit

Permalink
More robust ENUM parsing fo properties (now tolerates spacing between…
Browse files Browse the repository at this point in the history
… elements) and added some constant strings
  • Loading branch information
angalbiati committed Sep 2, 2024
1 parent edc092a commit a1ccb98
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 12 deletions.
31 changes: 31 additions & 0 deletions DbcParserLib.Tests/PropertiesLineParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,37 @@ public void EnumDefinitionCustomPropertyMoreWhiteSpaceIsParsedTest()
Assert.That(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" ""Val2"";", customPropertyLineParsers, dbcBuilderMock.Object, nextLineProviderMock.Object), Is.True);
}

[Test]
public void EnumDefinitionCustomPropertyWithWhiteSpaceNetweenEntriesIsParsedTest()
{
var dbcBuilderMock = m_repository.Create<IDbcBuilder>();
var nextLineProviderMock = m_repository.Create<INextLineProvider>();

dbcBuilderMock.Setup(mock => mock.AddCustomProperty(It.IsAny<CustomPropertyObjectType>(), It.IsAny<CustomPropertyDefinition>()))
.Callback<CustomPropertyObjectType, CustomPropertyDefinition>((_, customProperty) =>
{
Assert.That(customProperty.Name, Is.EqualTo("AttributeName"));
Assert.That(customProperty.DataType, Is.EqualTo(CustomPropertyDataType.Enum));
Assert.That(customProperty.EnumCustomProperty.Values.Length, Is.EqualTo(4));
Assert.That(customProperty.EnumCustomProperty.Values[0], Is.EqualTo("Val1"));
Assert.That(customProperty.EnumCustomProperty.Values[1], Is.EqualTo("Val2"));
Assert.That(customProperty.EnumCustomProperty.Values[2], Is.EqualTo("Val3"));
Assert.That(customProperty.EnumCustomProperty.Values[3], Is.EqualTo("Val4"));
});

dbcBuilderMock.Setup(mock => mock.AddCustomPropertyDefaultValue(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
.Callback<string, string, bool>((propertyName, value, isNumeric) =>
{
Assert.That(propertyName, Is.EqualTo("AttributeName"));
Assert.That(value, Is.EqualTo("Val2"));
Assert.That(isNumeric, Is.EqualTo(false));
});

var customPropertyLineParsers = CreateParser();
Assert.That(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" ENUM ""Val1"", ""Val2"", ""Val3"",""Val4"" ;", customPropertyLineParsers, dbcBuilderMock.Object, nextLineProviderMock.Object), Is.True);
Assert.That(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" ""Val2"";", customPropertyLineParsers, dbcBuilderMock.Object, nextLineProviderMock.Object), Is.True);
}

[Test]
public void MsgCycleTimePropertyIsParsedTest()
{
Expand Down
1 change: 0 additions & 1 deletion DbcParserLib.Tests/PropertiesParsingFailuresTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ public void DuplicateCustomPropertyErrorIsObserved()
[TestCase("BA_DEF_ BU_ \"attributeName\" STRING 0;")]
[TestCase("BA_DEF_ attributeName STRING")]
[TestCase("BA_DEF_ BU_ \"Ciao\" ENUM \"Cyclic\"\"Event\",\"CyclicIfActive\",\"SpontanWithDelay\",\"CyclicAndSpontan\";")]
[TestCase("BA_DEF_ BU_ \"Ciao\" ENUM \"Cyclic\",\"Event\", \"CyclicIfActive\",\"SpontanWithDelay\",\"CyclicAndSpontan\";")]
public void PropertyDefinitionSyntaxErrorIsObserved(string line)
{
var observerMock = m_repository.Create<IParseFailureObserver>();
Expand Down
8 changes: 4 additions & 4 deletions DbcParserLib/ExtensionsAndHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ internal static bool InitialValue(this Signal signal, out double initialValue)

internal class StringToDictionaryParser
{
private IDictionary<int, string> m_dictionary;
private readonly IDictionary<int, string> m_dictionary;

private StringToDictionaryParser(IDictionary<int, string> dict)
{
Expand All @@ -142,18 +142,18 @@ public static bool ParseString(string text, out IReadOnlyDictionary<int, string>

private bool ParseKey(string text, int offset)
{
var index = text.IndexOf("\"", offset, StringComparison.InvariantCulture);
var index = text.IndexOf(Helpers.DoubleQuotes, offset, StringComparison.InvariantCulture);
if(index == -1)
return true;

var key = text.Substring(offset, index - offset);
offset = index + 1;
return int.TryParse(key, out var intKey) ? ParseValue(text, offset, intKey) : false;
return int.TryParse(key, out var intKey) && ParseValue(text, offset, intKey);
}

private bool ParseValue(string text, int offset, int key)
{
var index = text.IndexOf("\"", offset, StringComparison.InvariantCulture);
var index = text.IndexOf(Helpers.DoubleQuotes, offset, StringComparison.InvariantCulture);
if (index == -1)
return false;

Expand Down
4 changes: 3 additions & 1 deletion DbcParserLib/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ namespace DbcParserLib
{
public static class Helpers
{
private static readonly string[] SpaceArray = new[] { " " };
public const string Space = " ";
public const string Comma = ",";
public const string DoubleQuotes = "\"";

private static readonly string[] SpaceArray = new[] { Space };

public static string[] SplitBySpace(this string value)
{
Expand Down
11 changes: 8 additions & 3 deletions DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DbcParserLib.Model;
using DbcParserLib.Observers;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;

namespace DbcParserLib.Parsers
Expand All @@ -9,7 +10,7 @@ internal class PropertiesDefinitionLineParser : ILineParser
{
private const string PropertiesDefinitionLineStarter = "BA_DEF_ ";
private const string PropertiesDefinitionDefaultLineStarter = "BA_DEF_DEF_ ";
private const string PropertyDefinitionParsingRegex = @"BA_DEF_(?:\s+(BU_|BO_|SG_|EV_))?\s+""([a-zA-Z_][\w]*)""\s+(?:(?:(INT|HEX)\s+(-?\d+)\s+(-?\d+))|(?:(FLOAT)\s+([\d\+\-eE.]+)\s+([\d\+\-eE.]+))|(STRING)|(?:(ENUM)\s+((?:""[^""]*"",+)*(?:""[^""]*""))))\s*;";
private const string PropertyDefinitionParsingRegex = @"BA_DEF_(?:\s+(BU_|BO_|SG_|EV_))?\s+""([a-zA-Z_][\w]*)""\s+(?:(?:(INT|HEX)\s+(-?\d+)\s+(-?\d+))|(?:(FLOAT)\s+([\d\+\-eE.]+)\s+([\d\+\-eE.]+))|(STRING)|(?:(ENUM)\s+((?:""[^""]*"",\s*)*(?:""[^""]*""))))\s*;";
private const string PropertyDefinitionDefaultParsingRegex = @"BA_DEF_DEF_\s+""([a-zA-Z_][\w]*)""\s+(-?\d+|[\d\+\-eE.]+|""[^""]*"")\s*;";

private readonly IParseFailureObserver m_observer;
Expand All @@ -31,7 +32,7 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin
{
var match = Regex.Match(cleanLine, PropertyDefinitionDefaultParsingRegex);
if (match.Success)
builder.AddCustomPropertyDefaultValue(match.Groups[1].Value, match.Groups[2].Value.Replace("\"", ""), !match.Groups[2].Value.StartsWith("\""));
builder.AddCustomPropertyDefaultValue(match.Groups[1].Value, match.Groups[2].Value.Replace(Helpers.DoubleQuotes, ""), !match.Groups[2].Value.StartsWith(Helpers.DoubleQuotes));
else
m_observer.PropertyDefaultSyntaxError();
return true;
Expand Down Expand Up @@ -97,7 +98,11 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin
dataType = CustomPropertyDataType.Enum;
customProperty.EnumCustomProperty = new EnumCustomPropertyDefinition
{
Values = match.Groups[11].Value.Replace("\"", "").Split(',')
Values = match.Groups[11]
.Value
.Replace(Helpers.DoubleQuotes, string.Empty)
.Replace(Helpers.Space, string.Empty)
.Split(',')
};
}
customProperty.DataType = dataType;
Expand Down
4 changes: 2 additions & 2 deletions DbcParserLib/Parsers/PropertiesLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin
var match = Regex.Match(cleanLine, PropertyParsingRegex);
if (match.Success)
{
var isNumeric = !match.Groups[9].Value.StartsWith("\"");
var stringValue = match.Groups[9].Value.Replace("\"", "");
var isNumeric = !match.Groups[9].Value.StartsWith(Helpers.DoubleQuotes);
var stringValue = match.Groups[9].Value.Replace(Helpers.DoubleQuotes, string.Empty);

if (match.Groups[2].Value == "BU_")
builder.AddNodeCustomProperty(match.Groups[1].Value, match.Groups[3].Value, stringValue, isNumeric);
Expand Down
2 changes: 1 addition & 1 deletion DbcParserLib/Parsers/ValueTableLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin

if (!string.IsNullOrEmpty(dictionary) && dictionary.TryParseToDict(out var valueTableDictionary))
{
if (match.Groups[3].Value != "")
if (match.Groups[3].Value != string.Empty)
builder.LinkTableValuesToEnvironmentVariable(match.Groups[3].Value, valueTableDictionary);
else
builder.LinkTableValuesToSignal(uint.Parse(match.Groups[1].Value), match.Groups[2].Value,
Expand Down

0 comments on commit a1ccb98

Please sign in to comment.