Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing for val table definition without a space before line termination #90

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading