Skip to content

Commit

Permalink
Fix parsing for val table definition without a space before line term…
Browse files Browse the repository at this point in the history
…ination
  • Loading branch information
Uight committed Sep 11, 2024
1 parent 407af99 commit 15bda65
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
17 changes: 16 additions & 1 deletion DbcParserLib.Tests/ValueTableLineParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,11 @@ public void ValueTableEnvironmentNameNotFoundErrorIsObserved()
lineParser.TryParse(line, dbcBuilder, nextLineProviderMock.Object);
}

[TestCase("VAL_TABLE_ tableName 0 \"Running\" 1 \"Idle\";")]
[TestCase("VAL_TABLE_ tableName 0 \"Running\" 1 \"Idle\"")]
[TestCase("VAL_TABLE_ tableName 0 \"Running\" 1 Idle;")]
[TestCase("VAL_TABLE_ \"tableName\" 0 \"Running\" 1 \"Idle\" ;")]
[TestCase("VAL_TABLE_ tableName 0 \"Running\" 1.5 \"Idle\" ;")]
[TestCase("VAL_TABLE_ tableName 0 \"Running\"1 \"Idle\";")]
public void ValueTableDefinitionSyntaxErrorIsObserved(string line)
{
var observerMock = m_repository.Create<IParseFailureObserver>();
Expand Down Expand Up @@ -368,5 +368,20 @@ public void ValueTableDefinitionContainsAdditionalSpacesBeforeSemicolon()

Assert.That(lineParser.TryParse(line, dbcBuilder, nextLineProviderMock.Object), Is.True);
}

[Test]
public void ValueTableDefinitionContainsNoSpacesBeforeSemicolon()
{
var tableName = "tableName";
var line = $"VAL_TABLE_ {tableName} 0 \"Running\" 1 \"Idle\";";

var observerMock = m_repository.Create<IParseFailureObserver>();
var nextLineProviderMock = m_repository.Create<INextLineProvider>();
var dbcBuilder = new DbcBuilder(observerMock.Object);

var lineParser = new ValueTableDefinitionLineParser(observerMock.Object);

Assert.That(lineParser.TryParse(line, dbcBuilder, nextLineProviderMock.Object), Is.True);
}
}
}
20 changes: 17 additions & 3 deletions DbcParserLib/Parsers/ValueTableDefinitionLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ internal class ValueTableDefinitionLineParser : ILineParser
{
private const string ValTableGroup = "ValTableName";
private const string ValueDescriptionGroup = "ValueDescription";
private const string EndValeDescriptionGroup = "ValueDescriptionEnd";
private const string ValueTableDefinitionLineStarter = "VAL_TABLE_ ";

private readonly string m_valueTableDefinitionParsingRegex = $@"VAL_TABLE_\s+(?<{ValTableGroup}>[a-zA-Z_][\w]*)\s+(?<{ValueDescriptionGroup}>(?:\d+\s+(?:""[^""]*"")\s+)*)\s*;";
private readonly string m_valueTableDefinitionParsingRegex = $@"VAL_TABLE_\s+(?<{ValTableGroup}>[a-zA-Z_][\w]*)\s+" +
$@"(?<{ValueDescriptionGroup}>(?:\d+\s+(?:""[^""]*"")\s+)*)(?<{EndValeDescriptionGroup}>(?:\d+\s+(?:""[^""]*"")\s*));";

private readonly IParseFailureObserver m_observer;

Expand All @@ -23,19 +25,31 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin
var cleanLine = line.Trim(' ');

if (cleanLine.StartsWith(ValueTableDefinitionLineStarter) == false)
{
return false;
}

var match = Regex.Match(cleanLine, m_valueTableDefinitionParsingRegex);
if (match.Success)
{
if (match.Groups[ValueDescriptionGroup].Value.TryParseToDict(out var valueTableDictionary))
var dictionary = string.IsNullOrEmpty(match.Groups[ValueDescriptionGroup].Value)
? match.Groups[EndValeDescriptionGroup].Value
: string.Concat(match.Groups[ValueDescriptionGroup].Value, match.Groups[EndValeDescriptionGroup].Value);

if (!string.IsNullOrEmpty(dictionary) && dictionary.TryParseToDict(out var valueTableDictionary))
{
builder.AddNamedValueTable(match.Groups[ValTableGroup].Value, valueTableDictionary);
}
else
{
m_observer.ValueTableDefinitionSyntaxError();
}
}
else
{
m_observer.ValueTableDefinitionSyntaxError();

}

return true;
}
}
Expand Down

0 comments on commit 15bda65

Please sign in to comment.