From c4cf23b0e7931e581e03cde57f2af52561a7d7c5 Mon Sep 17 00:00:00 2001 From: Uight Date: Wed, 28 Aug 2024 21:40:59 +0200 Subject: [PATCH 01/34] Move some more logic to the nextline provider allowing to split a single line into "virtual" lines by ";" --- .../PropertiesLineParserTests.cs | 57 ++++++++++--------- DbcParserLib/NextLineProvider.cs | 40 ++++++++++++- DbcParserLib/Parser.cs | 17 ++++-- 3 files changed, 78 insertions(+), 36 deletions(-) diff --git a/DbcParserLib.Tests/PropertiesLineParserTests.cs b/DbcParserLib.Tests/PropertiesLineParserTests.cs index 70ba944..a460776 100644 --- a/DbcParserLib.Tests/PropertiesLineParserTests.cs +++ b/DbcParserLib.Tests/PropertiesLineParserTests.cs @@ -12,6 +12,7 @@ namespace DbcParserLib.Tests public class PropertiesLineParserTests { private MockRepository m_repository; + private SilentFailureObserver m_observer = new SilentFailureObserver(); [SetUp] public void Setup() @@ -47,10 +48,10 @@ private static bool ParseLine(string line, List lineParser, IDbcBui [Test] public void IntDefinitionCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_observer); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_observer); Assert.IsTrue(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" INT 5 10;", customPropertyLineParsers, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" 7;", customPropertyLineParsers, builder, nextLineProvider)); } @@ -58,12 +59,12 @@ public void IntDefinitionCustomPropertyIsParsedTest() [Test] public void IntDefinitionCustomPropertyNoBoundariesIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_observer); var message = new Message { ID = 2394947585 }; builder.AddMessage(message); var msgCycleTimeLineParser = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_observer); Assert.IsTrue(ParseLine(@"BA_DEF_ BO_ ""AttributeName"" INT 0 0;", msgCycleTimeLineParser, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" 150;", msgCycleTimeLineParser, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_ ""AttributeName"" BO_ 2394947585 100;", msgCycleTimeLineParser, builder, nextLineProvider)); @@ -76,10 +77,10 @@ public void IntDefinitionCustomPropertyNoBoundariesIsParsedTest() [Test] public void HexDefinitionCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_observer); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_observer); Assert.IsTrue(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" HEX 5 10;", customPropertyLineParsers, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" 7;", customPropertyLineParsers, builder, nextLineProvider)); } @@ -87,12 +88,12 @@ public void HexDefinitionCustomPropertyIsParsedTest() [Test] public void HexDefinitionCustomPropertyNoBoundariesIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_observer); var message = new Message { ID = 2394947585 }; builder.AddMessage(message); var msgCycleTimeLineParser = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_observer); Assert.IsTrue(ParseLine(@"BA_DEF_ BO_ ""AttributeName"" HEX 0 0;", msgCycleTimeLineParser, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" 150;", msgCycleTimeLineParser, builder, nextLineProvider)); @@ -104,10 +105,10 @@ public void HexDefinitionCustomPropertyNoBoundariesIsParsedTest() [Test] public void FloatDefinitionCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_observer); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_observer); Assert.IsTrue(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" FLOAT 5 10.5;", customPropertyLineParsers, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" 7.5;", customPropertyLineParsers, builder, nextLineProvider)); } @@ -115,12 +116,12 @@ public void FloatDefinitionCustomPropertyIsParsedTest() [Test] public void FloatDefinitionCustomPropertyNoBoundariesIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_observer); var message = new Message { ID = 2394947585 }; builder.AddMessage(message); var msgCycleTimeLineParser = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_observer); Assert.IsTrue(ParseLine(@"BA_DEF_ BO_ ""AttributeName"" FLOAT 0 0;", msgCycleTimeLineParser, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" 150.0;", msgCycleTimeLineParser, builder, nextLineProvider)); @@ -151,10 +152,10 @@ public void ScientificNotationDefinitionCustomPropertyIsParsedTest() [Test] public void StringDefinitionCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_observer); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_observer); Assert.IsTrue(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" STRING;", customPropertyLineParsers, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" ""DefaultString"";", customPropertyLineParsers, builder, nextLineProvider)); } @@ -222,12 +223,12 @@ public void EnumDefinitionCustomPropertyMoreWhiteSpaceIsParsedTest() [Test] public void MsgCycleTimePropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_observer); var message = new Message { ID = 2394947585 }; builder.AddMessage(message); var msgCycleTimeLineParser = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_observer); Assert.IsTrue(ParseLine(@"BA_DEF_ BO_ ""GenMsgCycleTime"" INT 0 0;", msgCycleTimeLineParser, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""GenMsgCycleTime"" 150;", msgCycleTimeLineParser, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_ ""GenMsgCycleTime"" BO_ 2394947585 100;", msgCycleTimeLineParser, builder, nextLineProvider)); @@ -240,14 +241,14 @@ public void MsgCycleTimePropertyIsParsedTest() [Test] public void SigInitialValueIntegerPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_observer); var message = new Message { ID = 2394947585 }; builder.AddMessage(message); var signal = new Signal { Name = "sig_name" }; builder.AddSignal(signal); var sigInitialValueLineParser = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_observer); Assert.IsTrue(ParseLine(@"BA_DEF_ SG_ ""GenSigStartValue"" INT 0 200;", sigInitialValueLineParser, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""GenSigStartValue"" 150;", sigInitialValueLineParser, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_ ""GenSigStartValue"" SG_ 2394947585 sig_name 40;", sigInitialValueLineParser, builder, nextLineProvider)); @@ -261,14 +262,14 @@ public void SigInitialValueIntegerPropertyIsParsedTest() [Test] public void SigInitialValueHexPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_observer); var message = new Message { ID = 2394947585 }; builder.AddMessage(message); var signal = new Signal { Name = "sig_name" }; builder.AddSignal(signal); var sigInitialValueLineParser = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_observer); Assert.IsTrue(ParseLine(@"BA_DEF_ SG_ ""GenSigStartValue"" HEX 0 200;", sigInitialValueLineParser, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""GenSigStartValue"" 150;", sigInitialValueLineParser, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_ ""GenSigStartValue"" SG_ 2394947585 sig_name 40;", sigInitialValueLineParser, builder, nextLineProvider)); @@ -282,12 +283,12 @@ public void SigInitialValueHexPropertyIsParsedTest() [Test] public void NodeCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_observer); var node = new Node { Name = "Node1" }; builder.AddNode(node); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_observer); Assert.IsTrue(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" HEX 0 200;", customPropertyLineParsers, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" 150;", customPropertyLineParsers, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_ ""AttributeName"" BU_ Node1 40;", customPropertyLineParsers, builder, nextLineProvider)); @@ -300,13 +301,13 @@ public void NodeCustomPropertyIsParsedTest() [Test] public void NodeScientificNotationCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_observer); var node = new Node { Name = "Node1" }; builder.AddNode(node); var dbc = builder.Build(); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_observer); Assert.IsTrue(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" FLOAT 0 10;", customPropertyLineParsers, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" 5;", customPropertyLineParsers, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_ ""AttributeName"" BU_ Node1 0.7e1;", customPropertyLineParsers, builder, nextLineProvider)); @@ -316,12 +317,12 @@ public void NodeScientificNotationCustomPropertyIsParsedTest() [Test] public void NodeMultipleCustomPropertyAreParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_observer); var node = new Node { Name = "Node1" }; builder.AddNode(node); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_observer); Assert.IsTrue(ParseLine(@"BA_DEF_ BU_ ""AttributeName1"" INT 0 200;", customPropertyLineParsers, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""AttributeName1"" 100;", customPropertyLineParsers, builder, nextLineProvider)); @@ -338,14 +339,14 @@ public void NodeMultipleCustomPropertyAreParsedTest() [Test] public void CustomPropertyIsAssignedToDifferentNodesTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_observer); var node1 = new Node { Name = "Node1" }; var node2 = new Node { Name = "Node2" }; builder.AddNode(node1); builder.AddNode(node2); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_observer); Assert.IsTrue(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" INT 0 200;", customPropertyLineParsers, builder, nextLineProvider)); Assert.IsTrue(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" 100;", customPropertyLineParsers, builder, nextLineProvider)); diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index 45a8d6f..18b3de2 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -1,25 +1,61 @@ -using System.IO; +using DbcParserLib.Observers; +using System; +using System.IO; namespace DbcParserLib { public class NextLineProvider : INextLineProvider { private TextReader m_reader; + private IParseFailureObserver m_observer; + private string m_lineMemory; - public NextLineProvider(TextReader reader) + public NextLineProvider(TextReader reader, IParseFailureObserver observer) { m_reader = reader; + m_observer = observer; } public bool TryGetLine(out string line) { line = null; + if (m_lineMemory != null) + { + line = m_lineMemory; + line = line.Trim(); + line = HandleMultipleDefinitionsPerLine(line); + + return true; + } + if (m_reader.Peek() >= 0) { + m_observer.CurrentLine++; line = m_reader.ReadLine(); + line = line.Trim(); + line = HandleMultipleDefinitionsPerLine(line); + return true; } return false; } + + private string HandleMultipleDefinitionsPerLine(string line) + { + int definitionTerminationLocation = line.IndexOf(";", StringComparison.Ordinal); + + if (definitionTerminationLocation >= 0) + { + if (definitionTerminationLocation + 1 == line.Length) + { + return line; + } + var firstLinePart = line.Substring(0, definitionTerminationLocation + 1); + m_lineMemory = line.Substring(definitionTerminationLocation + 2, line.Length - 1).Trim(); + + return firstLinePart; + } + return line; + } } } \ No newline at end of file diff --git a/DbcParserLib/Parser.cs b/DbcParserLib/Parser.cs index f4ec32b..c08ea24 100644 --- a/DbcParserLib/Parser.cs +++ b/DbcParserLib/Parser.cs @@ -65,24 +65,29 @@ private static Dbc ParseFromReader(TextReader reader) m_parseObserver.Clear(); var builder = new DbcBuilder(m_parseObserver); - var nextLineProvider = new NextLineProvider(reader); + var nextLineProvider = new NextLineProvider(reader, m_parseObserver); - while (reader.Peek() >= 0) - ParseLine(reader.ReadLine(), builder, nextLineProvider); + while (nextLineProvider.TryGetLine(out var line)) + { + ParseLine(line, builder, nextLineProvider); + } return builder.Build(); } private static void ParseLine(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - m_parseObserver.CurrentLine++; if (string.IsNullOrWhiteSpace(line)) + { return; + } - foreach(var parser in LineParsers) + foreach (var parser in LineParsers) { - if(parser.TryParse(line, builder, nextLineProvider)) + if (parser.TryParse(line, builder, nextLineProvider)) + { break; + } } } } From f3e9dc5f82d68527a62d1ed7965d972a9cf430ff Mon Sep 17 00:00:00 2001 From: Uight Date: Thu, 29 Aug 2024 10:32:45 +0200 Subject: [PATCH 02/34] NextLineProvider now allows PeakLine --- DbcParserLib/NextLineProvider.cs | 20 ++++++++++++++++++++ DbcParserLib/Parsers/CommentLineParser.cs | 1 - 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index 18b3de2..8b9783c 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -9,6 +9,7 @@ public class NextLineProvider : INextLineProvider private TextReader m_reader; private IParseFailureObserver m_observer; private string m_lineMemory; + private bool m_isVirtualLine; public NextLineProvider(TextReader reader, IParseFailureObserver observer) { @@ -21,6 +22,10 @@ public bool TryGetLine(out string line) line = null; if (m_lineMemory != null) { + if (m_isVirtualLine == false) + { + m_observer.CurrentLine++; + } line = m_lineMemory; line = line.Trim(); line = HandleMultipleDefinitionsPerLine(line); @@ -52,10 +57,25 @@ private string HandleMultipleDefinitionsPerLine(string line) } var firstLinePart = line.Substring(0, definitionTerminationLocation + 1); m_lineMemory = line.Substring(definitionTerminationLocation + 2, line.Length - 1).Trim(); + m_isVirtualLine = true; return firstLinePart; } return line; } + + public string PeakNextLine() + { + if (m_lineMemory == null) + { + m_lineMemory = m_reader.ReadLine(); + m_isVirtualLine = false; + return m_lineMemory; + } + else + { + return m_lineMemory; + } + } } } \ No newline at end of file diff --git a/DbcParserLib/Parsers/CommentLineParser.cs b/DbcParserLib/Parsers/CommentLineParser.cs index ba1c2cb..d1b163f 100644 --- a/DbcParserLib/Parsers/CommentLineParser.cs +++ b/DbcParserLib/Parsers/CommentLineParser.cs @@ -109,7 +109,6 @@ private static string GetNextLines(string currentLine, IParseFailureObserver obs while (nextLineProvider.TryGetLine(out var nextLine)) { - observer.CurrentLine++; stringBuilder.AppendLine(nextLine); if (nextLine.EndsWith(";")) break; From 3790268a489036822f9ee42213d4c483227feb7d Mon Sep 17 00:00:00 2001 From: Uight Date: Sat, 31 Aug 2024 11:05:42 +0200 Subject: [PATCH 03/34] Multiline support via NextLineProvider --- DbcParserLib/NextLineProvider.cs | 110 +++++++++++++++++++++- DbcParserLib/Parsers/CommentLineParser.cs | 11 +-- 2 files changed, 111 insertions(+), 10 deletions(-) diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index 8b9783c..3f3dae0 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -1,6 +1,7 @@ using DbcParserLib.Observers; using System; using System.IO; +using System.Linq; namespace DbcParserLib { @@ -11,6 +12,59 @@ public class NextLineProvider : INextLineProvider private string m_lineMemory; private bool m_isVirtualLine; + private string lineTermination = ";"; + + private readonly string[] keywords = new[] + { + "VERSION", + "FILTER", + + "NS_DESC_", + "NS_", + + "CM_", + + "BA_DEF_DEF_REL_", + "BA_DEF_REL_", + "BA_REL_", + "BA_DEF_SGTYPE_", + "BA_SGTYPE_", + "BA_DEF_DEF_", + "BA_DEF_", + "BA_", + + "CAT_DEF_", + "CAT_", + + "SGTYPE_VAL_", + "SGTYPE_", + + "SIGTYPE_VALTYPE_", + + "VAL_TABLE_", + "VAL_", + + "SIG_GROUP_", + "SIG_VALTYPE_", + "SIG_TYPE_REF_", + + "EV_DATA_", + "ENVVAR_DATA_", + + "BO_TX_BU_", + "BO_", + + "BU_SG_REL_", + "BU_EV_REL_", + "BU_BO_REL_", + "BU_", + + "SG_MUL_VAL_", + "SG_", + + "BS_", + }; + public NextLineProvider(TextReader reader, IParseFailureObserver observer) { m_reader = reader; @@ -26,9 +80,11 @@ public bool TryGetLine(out string line) { m_observer.CurrentLine++; } - line = m_lineMemory; + line = m_lineMemory; line = line.Trim(); + m_lineMemory = null; line = HandleMultipleDefinitionsPerLine(line); + line = HandleMultiline(line); return true; } @@ -39,6 +95,7 @@ public bool TryGetLine(out string line) line = m_reader.ReadLine(); line = line.Trim(); line = HandleMultipleDefinitionsPerLine(line); + line = HandleMultiline(line); return true; } @@ -47,7 +104,7 @@ public bool TryGetLine(out string line) private string HandleMultipleDefinitionsPerLine(string line) { - int definitionTerminationLocation = line.IndexOf(";", StringComparison.Ordinal); + int definitionTerminationLocation = line.IndexOf(lineTermination, StringComparison.Ordinal); if (definitionTerminationLocation >= 0) { @@ -64,11 +121,36 @@ private string HandleMultipleDefinitionsPerLine(string line) return line; } - public string PeakNextLine() + private string HandleMultiline(string line) + { + if (line.EndsWith(lineTermination)) + { + return line; + } + var nextLine = PeakNextLine(); + if (string.IsNullOrWhiteSpace(nextLine)) + { + return line; + } + if (CheckNextLineParsing(PeakNextLine())) + { + return line; + } + + TryGetLine(out var actualNextLine); + return CombineLines(line, actualNextLine); + } + + private string PeakNextLine() { if (m_lineMemory == null) { - m_lineMemory = m_reader.ReadLine(); + var nextLine = m_reader.ReadLine(); + if (nextLine is null) + { + return nextLine; + } + m_lineMemory = nextLine; m_isVirtualLine = false; return m_lineMemory; } @@ -77,5 +159,25 @@ public string PeakNextLine() return m_lineMemory; } } + + private bool CheckNextLineParsing(string nextLine) + { + nextLine = nextLine.TrimStart(); + return keywords.Any(prefix => nextLine.StartsWith(prefix)); + } + + private string CombineLines(string currentLine, string nextLine) + { + currentLine = currentLine.TrimEnd('\r', '\n'); + + string combinedLine = currentLine + " " + nextLine; + + return combinedLine; + + /*var stringBuilder = new StringBuilder(); + stringBuilder.AppendLine(currentLine); + stringBuilder.AppendLine(nextLine); + return stringBuilder.ToString();*/ + } } } \ No newline at end of file diff --git a/DbcParserLib/Parsers/CommentLineParser.cs b/DbcParserLib/Parsers/CommentLineParser.cs index d1b163f..60db859 100644 --- a/DbcParserLib/Parsers/CommentLineParser.cs +++ b/DbcParserLib/Parsers/CommentLineParser.cs @@ -22,13 +22,13 @@ public CommentLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim(); + var cleanLine = line; //line.Trim(); if (cleanLine.StartsWith(CommentLineStarter) == false) return false; - if (!cleanLine.EndsWith(";")) - cleanLine = GetNextLines(cleanLine, m_observer, nextLineProvider); + /*if (!cleanLine.EndsWith(";")) + cleanLine = GetNextLines(cleanLine, m_observer, nextLineProvider);*/ if (cleanLine.StartsWith("CM_ SG_")) { @@ -102,7 +102,7 @@ private static void SetEnvironmentVariableComment(string envCommentStr, IParseFa observer.CommentSyntaxError(); } - private static string GetNextLines(string currentLine, IParseFailureObserver observer, INextLineProvider nextLineProvider) + /*private static string GetNextLines(string currentLine, IParseFailureObserver observer, INextLineProvider nextLineProvider) { var stringBuilder = new StringBuilder(); stringBuilder.AppendLine(currentLine); @@ -114,7 +114,6 @@ private static string GetNextLines(string currentLine, IParseFailureObserver obs break; } return stringBuilder.ToString(); - } - + }*/ } } \ No newline at end of file From ae60b3bed9b0b39c3816b4e4ddf4d156f78f8d96 Mon Sep 17 00:00:00 2001 From: Uight Date: Sat, 31 Aug 2024 11:34:01 +0200 Subject: [PATCH 04/34] Resolve merge conflicts --- .../PropertiesLineParserTests.cs | 62 +++++++++---------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/DbcParserLib.Tests/PropertiesLineParserTests.cs b/DbcParserLib.Tests/PropertiesLineParserTests.cs index 734e93b..a9ba6da 100644 --- a/DbcParserLib.Tests/PropertiesLineParserTests.cs +++ b/DbcParserLib.Tests/PropertiesLineParserTests.cs @@ -12,6 +12,7 @@ namespace DbcParserLib.Tests public class PropertiesLineParserTests { private MockRepository m_repository; + private static SilentFailureObserver m_silentFailureObserver = new SilentFailureObserver(); [SetUp] public void Setup() @@ -27,10 +28,9 @@ public void Teardown() private static List CreateParser() { - var observer = new SilentFailureObserver(); return new List() { - new PropertiesLineParser(observer), - new PropertiesDefinitionLineParser(observer) + new PropertiesLineParser(m_silentFailureObserver), + new PropertiesDefinitionLineParser(m_silentFailureObserver) }; } @@ -47,10 +47,10 @@ private static bool ParseLine(string line, List lineParser, IDbcBui [Test] public void IntDefinitionCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.That(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" INT 5 10;", customPropertyLineParsers, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" 7;", customPropertyLineParsers, builder, nextLineProvider), Is.True); } @@ -58,12 +58,12 @@ public void IntDefinitionCustomPropertyIsParsedTest() [Test] public void IntDefinitionCustomPropertyNoBoundariesIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var message = new Message { ID = 2394947585 }; builder.AddMessage(message); var msgCycleTimeLineParser = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.That(ParseLine(@"BA_DEF_ BO_ ""AttributeName"" INT 0 0;", msgCycleTimeLineParser, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" 150;", msgCycleTimeLineParser, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_ ""AttributeName"" BO_ 2394947585 100;", msgCycleTimeLineParser, builder, nextLineProvider), Is.True); @@ -76,10 +76,10 @@ public void IntDefinitionCustomPropertyNoBoundariesIsParsedTest() [Test] public void HexDefinitionCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.That(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" HEX 5 10;", customPropertyLineParsers, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" 7;", customPropertyLineParsers, builder, nextLineProvider), Is.True); } @@ -87,12 +87,12 @@ public void HexDefinitionCustomPropertyIsParsedTest() [Test] public void HexDefinitionCustomPropertyNoBoundariesIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var message = new Message { ID = 2394947585 }; builder.AddMessage(message); var msgCycleTimeLineParser = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.That(ParseLine(@"BA_DEF_ BO_ ""AttributeName"" HEX 0 0;", msgCycleTimeLineParser, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" 150;", msgCycleTimeLineParser, builder, nextLineProvider), Is.True); @@ -104,10 +104,10 @@ public void HexDefinitionCustomPropertyNoBoundariesIsParsedTest() [Test] public void FloatDefinitionCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.That(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" FLOAT 5 10.5;", customPropertyLineParsers, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" 7.5;", customPropertyLineParsers, builder, nextLineProvider), Is.True); } @@ -115,12 +115,12 @@ public void FloatDefinitionCustomPropertyIsParsedTest() [Test] public void FloatDefinitionCustomPropertyNoBoundariesIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var message = new Message { ID = 2394947585 }; builder.AddMessage(message); var msgCycleTimeLineParser = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.That(ParseLine(@"BA_DEF_ BO_ ""AttributeName"" FLOAT 0 0;", msgCycleTimeLineParser, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" 150.0;", msgCycleTimeLineParser, builder, nextLineProvider), Is.True); @@ -151,10 +151,10 @@ public void ScientificNotationDefinitionCustomPropertyIsParsedTest() [Test] public void StringDefinitionCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.That(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" STRING;", customPropertyLineParsers, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" ""DefaultString"";", customPropertyLineParsers, builder, nextLineProvider), Is.True); } @@ -222,12 +222,12 @@ public void EnumDefinitionCustomPropertyMoreWhiteSpaceIsParsedTest() [Test] public void MsgCycleTimePropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var message = new Message { ID = 2394947585 }; builder.AddMessage(message); var msgCycleTimeLineParser = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.That(ParseLine(@"BA_DEF_ BO_ ""GenMsgCycleTime"" INT 0 0;", msgCycleTimeLineParser, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_DEF_DEF_ ""GenMsgCycleTime"" 150;", msgCycleTimeLineParser, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_ ""GenMsgCycleTime"" BO_ 2394947585 100;", msgCycleTimeLineParser, builder, nextLineProvider), Is.True); @@ -240,14 +240,14 @@ public void MsgCycleTimePropertyIsParsedTest() [Test] public void SigInitialValueIntegerPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var message = new Message { ID = 2394947585 }; builder.AddMessage(message); var signal = new Signal { Name = "sig_name" }; builder.AddSignal(signal); var sigInitialValueLineParser = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.That(ParseLine(@"BA_DEF_ SG_ ""GenSigStartValue"" INT 0 200;", sigInitialValueLineParser, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_DEF_DEF_ ""GenSigStartValue"" 150;", sigInitialValueLineParser, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_ ""GenSigStartValue"" SG_ 2394947585 sig_name 40;", sigInitialValueLineParser, builder, nextLineProvider), Is.True); @@ -261,14 +261,14 @@ public void SigInitialValueIntegerPropertyIsParsedTest() [Test] public void SigInitialValueHexPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var message = new Message { ID = 2394947585 }; builder.AddMessage(message); var signal = new Signal { Name = "sig_name" }; builder.AddSignal(signal); var sigInitialValueLineParser = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.That(ParseLine(@"BA_DEF_ SG_ ""GenSigStartValue"" HEX 0 200;", sigInitialValueLineParser, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_DEF_DEF_ ""GenSigStartValue"" 150;", sigInitialValueLineParser, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_ ""GenSigStartValue"" SG_ 2394947585 sig_name 40;", sigInitialValueLineParser, builder, nextLineProvider), Is.True); @@ -282,12 +282,12 @@ public void SigInitialValueHexPropertyIsParsedTest() [Test] public void NodeCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var node = new Node { Name = "Node1" }; builder.AddNode(node); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.That(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" HEX 0 200;", customPropertyLineParsers, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" 150;", customPropertyLineParsers, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_ ""AttributeName"" BU_ Node1 40;", customPropertyLineParsers, builder, nextLineProvider), Is.True); @@ -300,13 +300,13 @@ public void NodeCustomPropertyIsParsedTest() [Test] public void NodeScientificNotationCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var node = new Node { Name = "Node1" }; builder.AddNode(node); var dbc = builder.Build(); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.That(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" FLOAT 0 10;", customPropertyLineParsers, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" 5;", customPropertyLineParsers, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_ ""AttributeName"" BU_ Node1 0.7e1;", customPropertyLineParsers, builder, nextLineProvider), Is.True); @@ -316,12 +316,12 @@ public void NodeScientificNotationCustomPropertyIsParsedTest() [Test] public void NodeMultipleCustomPropertyAreParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var node = new Node { Name = "Node1" }; builder.AddNode(node); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.That(ParseLine(@"BA_DEF_ BU_ ""AttributeName1"" INT 0 200;", customPropertyLineParsers, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_DEF_DEF_ ""AttributeName1"" 100;", customPropertyLineParsers, builder, nextLineProvider), Is.True); @@ -338,14 +338,14 @@ public void NodeMultipleCustomPropertyAreParsedTest() [Test] public void CustomPropertyIsAssignedToDifferentNodesTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var node1 = new Node { Name = "Node1" }; var node2 = new Node { Name = "Node2" }; builder.AddNode(node1); builder.AddNode(node2); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.That(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" INT 0 200;", customPropertyLineParsers, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" 100;", customPropertyLineParsers, builder, nextLineProvider), Is.True); From 57558901be23628598a93cf8a46dc754838d2424 Mon Sep 17 00:00:00 2001 From: Uight Date: Sat, 31 Aug 2024 12:23:09 +0200 Subject: [PATCH 05/34] intermediate updateing tests --- DbcParserLib.Tests/CommentLineParserTests.cs | 84 +++++++++++--------- DbcParserLib/ExtensionsAndHelpers.cs | 7 ++ DbcParserLib/Helpers.cs | 40 ---------- DbcParserLib/NextLineProvider.cs | 11 +-- DbcParserLib/Parsers/SignalLineParser.cs | 3 +- 5 files changed, 62 insertions(+), 83 deletions(-) delete mode 100644 DbcParserLib/Helpers.cs diff --git a/DbcParserLib.Tests/CommentLineParserTests.cs b/DbcParserLib.Tests/CommentLineParserTests.cs index c56a878..26aa7de 100644 --- a/DbcParserLib.Tests/CommentLineParserTests.cs +++ b/DbcParserLib.Tests/CommentLineParserTests.cs @@ -3,6 +3,7 @@ using Moq; using System.Collections.Generic; using DbcParserLib.Observers; +using System.IO; namespace DbcParserLib.Tests { @@ -92,20 +93,22 @@ public void FullLineIsParsed() [Test] public void FullMultilineIsParsed() { - var multiLineComment = new[] - { - "CM_ SG_ 75 channelName \"This is the first line", - "this is the second line", - "this is the third line\";" - }; - var expectedText = Helpers.ConcatenateTextComment(multiLineComment, 23); + var dbcString = @"CM_ SG_ 75 channelName \""This is the first line"" +""this is the second line"" +""this is the third line\"";"; + + var expectedText = "This is the first line\r\nthis is the second line\r\nthis is the third line"; var dbcBuilderMock = m_repository.Create(); dbcBuilderMock.Setup(mock => mock.AddSignalComment(75, "channelName", expectedText)); var commentLineParser = CreateParser(); - var reader = new ArrayBasedLineProvider(multiLineComment); - Assert.That(commentLineParser.TryParse(multiLineComment[0], dbcBuilderMock.Object, reader), Is.True); + using (var reader = new StringReader(dbcString)) + { + var nextLineProvider = new NextLineProvider(reader, new SilentFailureObserver()); + nextLineProvider.TryGetLine(out var line); + Assert.That(commentLineParser.TryParse(line, dbcBuilderMock.Object, nextLineProvider), Is.True); + } } [Test] @@ -122,20 +125,23 @@ public void FullLineIsParsedAndRobustToWhiteSpace() [Test] public void FullMultilineIsParsedAndRobustToWhiteSpace() { - var multiLineComment = new[] - { - "CM_ SG_ 75 channelName \"This is the first line", - " this is the second line", - " this is the third line\";" - }; - var expectedText = Helpers.ConcatenateTextComment(multiLineComment, 23); + var dbcString = @"CM_ SG_ 75 channelName \""This is the first line"" +"" this is the second line"" +"" this is the third line\"";"; + + // Spaces at linestart are always removed + var expectedText = "This is the first line\r\nthis is the second line\r\nthis is the third line"; var dbcBuilderMock = m_repository.Create(); dbcBuilderMock.Setup(mock => mock.AddSignalComment(75, "channelName", expectedText)); var commentLineParser = CreateParser(); - var reader = new ArrayBasedLineProvider(multiLineComment); - Assert.That(commentLineParser.TryParse(multiLineComment[0], dbcBuilderMock.Object, reader), Is.True); + using (var reader = new StringReader(dbcString)) + { + var nextLineProvider = new NextLineProvider(reader, new SilentFailureObserver()); + nextLineProvider.TryGetLine(out var line); + Assert.That(commentLineParser.TryParse(line, dbcBuilderMock.Object, nextLineProvider), Is.True); + } } [Test] @@ -152,20 +158,23 @@ public void FullLineIsParsedForMessageAndRobustToWhiteSpace() [Test] public void FullMultilineIsParsedForMessageAndRobustToWhiteSpace() { - var multiLineComment = new[] - { - "CM_ BO_ 75 \"This is the first line", - " this is the second line", - " this is the third line\";" - }; - var expectedText = Helpers.ConcatenateTextComment(multiLineComment, 11); + var dbcString = @"CM_ BO_ 75 \""This is the first line"" +"" this is the second line"" +"" this is the third line\"";"; + + // Spaces at linestart are always removed + var expectedText = "This is the first line\r\nthis is the second line\r\nthis is the third line"; var dbcBuilderMock = m_repository.Create(); dbcBuilderMock.Setup(mock => mock.AddMessageComment(75, expectedText)); var commentLineParser = CreateParser(); - var reader = new ArrayBasedLineProvider(multiLineComment); - Assert.That(commentLineParser.TryParse(multiLineComment[0], dbcBuilderMock.Object, reader), Is.True); + using (var reader = new StringReader(dbcString)) + { + var nextLineProvider = new NextLineProvider(reader, new SilentFailureObserver()); + nextLineProvider.TryGetLine(out var line); + Assert.That(commentLineParser.TryParse(line, dbcBuilderMock.Object, nextLineProvider), Is.True); + } } [Test] @@ -202,20 +211,23 @@ public void FullLineIsParsedForNodeAndRobustToWhiteSpace() [Test] public void FullMultilineIsParsedForNodeAndRobustToWhiteSpace() { - var multiLineComment = new[] - { - "CM_ BU_ node_name \"This is the first line", - " this is the second line", - " this is the third line\";" - }; - var expectedText = Helpers.ConcatenateTextComment(multiLineComment, 18); + var dbcString = @"CM_ BU_ node_name \""This is the first line"" +"" this is the second line"" +"" this is the third line\"";"; + + // Spaces at linestart are always removed + var expectedText = "This is the first line\r\nthis is the second line\r\nthis is the third line"; var dbcBuilderMock = m_repository.Create(); dbcBuilderMock.Setup(mock => mock.AddNodeComment("node_name", expectedText)); var commentLineParser = CreateParser(); - var reader = new ArrayBasedLineProvider(multiLineComment); - Assert.That(commentLineParser.TryParse(multiLineComment[0], dbcBuilderMock.Object, reader), Is.True); + using (var reader = new StringReader(dbcString)) + { + var nextLineProvider = new NextLineProvider(reader, new SilentFailureObserver()); + nextLineProvider.TryGetLine(out var line); + Assert.That(commentLineParser.TryParse(line, dbcBuilderMock.Object, nextLineProvider), Is.True); + } } [Test] diff --git a/DbcParserLib/ExtensionsAndHelpers.cs b/DbcParserLib/ExtensionsAndHelpers.cs index 459b8f4..85f402f 100644 --- a/DbcParserLib/ExtensionsAndHelpers.cs +++ b/DbcParserLib/ExtensionsAndHelpers.cs @@ -116,6 +116,13 @@ internal static bool InitialValue(this Signal signal, out double initialValue) return false; } + private static readonly string[] SpaceArray = { " " }; + public static readonly string[] CommaSpaceSeparator = { " ", "," }; + + public static string[] SplitBySpace(this string value) + { + return value.Split(SpaceArray, System.StringSplitOptions.RemoveEmptyEntries); + } } internal class StringToDictionaryParser diff --git a/DbcParserLib/Helpers.cs b/DbcParserLib/Helpers.cs deleted file mode 100644 index c0b2466..0000000 --- a/DbcParserLib/Helpers.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System.Text; -using System.Text.RegularExpressions; - -namespace DbcParserLib -{ - public static class Helpers - { - private static readonly string[] SpaceArray = new[] { " " }; - public const string Space = " "; - public const string Comma = ","; - - public static string[] SplitBySpace(this string value) - { - return value.Split(SpaceArray, System.StringSplitOptions.RemoveEmptyEntries); - } - - public static string ConvertToMultiLine(string[] records, int offset) - { - var sb = new StringBuilder(); - for (var i = offset; i < records.Length - 1; i += 2) - { - sb.AppendFormat("{0} {1} {2}", records[i], records[i+1], '\n'); - } - - return sb.ToString(); - } - - public static string ConcatenateTextComment(string[] strings, int startingIndex) - { - var sb = new StringBuilder(); - foreach(var s in strings ) - { - sb.AppendLine(Regex.Replace(s, @"""|;", "")); - } - var commentText = sb.ToString().Substring(startingIndex); - return commentText.Substring(0, commentText.Length - 2); - } - } - -} \ No newline at end of file diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index 3f3dae0..be163fa 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Linq; +using System.Text; namespace DbcParserLib { @@ -168,16 +169,16 @@ private bool CheckNextLineParsing(string nextLine) private string CombineLines(string currentLine, string nextLine) { - currentLine = currentLine.TrimEnd('\r', '\n'); + /*currentLine = currentLine.TrimEnd('\r', '\n'); string combinedLine = currentLine + " " + nextLine; - return combinedLine; + return combinedLine;*/ - /*var stringBuilder = new StringBuilder(); + var stringBuilder = new StringBuilder(); stringBuilder.AppendLine(currentLine); - stringBuilder.AppendLine(nextLine); - return stringBuilder.ToString();*/ + stringBuilder.Append(nextLine); + return stringBuilder.ToString(); } } } \ No newline at end of file diff --git a/DbcParserLib/Parsers/SignalLineParser.cs b/DbcParserLib/Parsers/SignalLineParser.cs index a9b3d32..caed06a 100644 --- a/DbcParserLib/Parsers/SignalLineParser.cs +++ b/DbcParserLib/Parsers/SignalLineParser.cs @@ -10,7 +10,6 @@ internal class SignalLineParser : ILineParser { private const string SignalLineStarter = "SG_ "; private const string SignedSymbol = "-"; - private static readonly string[] m_commaSpaceSeparator = new string[] { Helpers.Space, Helpers.Comma }; private const string SignalRegex = @"\s*SG_\s+([\w]+)\s*([Mm\d]*)\s*:\s*(\d+)\|(\d+)@([01])([+-])\s+\(([\d\+\-eE.]+),([\d\+\-eE.]+)\)\s+\[([\d\+\-eE.]+)\|([\d\+\-eE.]+)\]\s+""(.*)""\s+([\w\s,]+)"; private readonly IParseFailureObserver m_observer; @@ -43,7 +42,7 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin Minimum = double.Parse(match.Groups[9].Value, CultureInfo.InvariantCulture), Maximum = double.Parse(match.Groups[10].Value, CultureInfo.InvariantCulture), Unit = match.Groups[11].Value, - Receiver = match.Groups[12].Value.Split(m_commaSpaceSeparator, StringSplitOptions.RemoveEmptyEntries) // can be multiple receivers splitted by "," + Receiver = match.Groups[12].Value.Split(ExtensionsAndHelpers.CommaSpaceSeparator, StringSplitOptions.RemoveEmptyEntries) // can be multiple receivers splitted by "," }; builder.AddSignal(sig); From 638b649b0fafaf19c9c526692f4acb43695c0daa Mon Sep 17 00:00:00 2001 From: Uight Date: Sat, 31 Aug 2024 12:47:37 +0200 Subject: [PATCH 06/34] Fix changed tests (still not running as \r\n is still removed) --- DbcParserLib.Tests/CommentLineParserTests.cs | 39 ++++++++++++------- DbcParserLib/NextLineProvider.cs | 8 ++-- DbcParserLib/Parsers/CommentLineParser.cs | 40 +++++--------------- 3 files changed, 39 insertions(+), 48 deletions(-) diff --git a/DbcParserLib.Tests/CommentLineParserTests.cs b/DbcParserLib.Tests/CommentLineParserTests.cs index 26aa7de..9ed8f71 100644 --- a/DbcParserLib.Tests/CommentLineParserTests.cs +++ b/DbcParserLib.Tests/CommentLineParserTests.cs @@ -50,13 +50,24 @@ public void RandomStartIsIgnored() } [Test] + // Should parse as it is a comment but should be observed as error + // This however would be catched previously by the IgnoreLineParser public void OnlyPrefixIsIgnored() { var dbcBuilderMock = m_repository.Create(); - var commentLineParser = CreateParser(); + + var counter = 0; + var failureObserverMock = new Mock(); + failureObserverMock + .Setup(observer => observer.CommentSyntaxError()) + .Callback(() => counter++); + + var commentLineParser = new CommentLineParser(failureObserverMock.Object); + var nextLineProviderMock = m_repository.Create(); - Assert.That(commentLineParser.TryParse("CM_ ", dbcBuilderMock.Object, nextLineProviderMock.Object), Is.False); + Assert.That(commentLineParser.TryParse("CM_ ", dbcBuilderMock.Object, nextLineProviderMock.Object), Is.True); + Assert.That(counter, Is.EqualTo(1)); } [Test] @@ -93,9 +104,9 @@ public void FullLineIsParsed() [Test] public void FullMultilineIsParsed() { - var dbcString = @"CM_ SG_ 75 channelName \""This is the first line"" -""this is the second line"" -""this is the third line\"";"; + var dbcString = @"CM_ SG_ 75 channelName ""This is the first line +this is the second line +this is the third line"";"; var expectedText = "This is the first line\r\nthis is the second line\r\nthis is the third line"; @@ -125,9 +136,9 @@ public void FullLineIsParsedAndRobustToWhiteSpace() [Test] public void FullMultilineIsParsedAndRobustToWhiteSpace() { - var dbcString = @"CM_ SG_ 75 channelName \""This is the first line"" -"" this is the second line"" -"" this is the third line\"";"; + var dbcString = @"CM_ SG_ 75 channelName ""This is the first line + this is the second line + this is the third line"";"; // Spaces at linestart are always removed var expectedText = "This is the first line\r\nthis is the second line\r\nthis is the third line"; @@ -158,9 +169,9 @@ public void FullLineIsParsedForMessageAndRobustToWhiteSpace() [Test] public void FullMultilineIsParsedForMessageAndRobustToWhiteSpace() { - var dbcString = @"CM_ BO_ 75 \""This is the first line"" -"" this is the second line"" -"" this is the third line\"";"; + var dbcString = @"CM_ BO_ 75 ""This is the first line + this is the second line + this is the third line"";"; // Spaces at linestart are always removed var expectedText = "This is the first line\r\nthis is the second line\r\nthis is the third line"; @@ -211,9 +222,9 @@ public void FullLineIsParsedForNodeAndRobustToWhiteSpace() [Test] public void FullMultilineIsParsedForNodeAndRobustToWhiteSpace() { - var dbcString = @"CM_ BU_ node_name \""This is the first line"" -"" this is the second line"" -"" this is the third line\"";"; + var dbcString = @"CM_ BU_ node_name ""This is the first line + this is the second line + this is the third line"";"; // Spaces at linestart are always removed var expectedText = "This is the first line\r\nthis is the second line\r\nthis is the third line"; diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index be163fa..d942246 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -169,16 +169,16 @@ private bool CheckNextLineParsing(string nextLine) private string CombineLines(string currentLine, string nextLine) { - /*currentLine = currentLine.TrimEnd('\r', '\n'); + currentLine = currentLine.TrimEnd('\r', '\n'); string combinedLine = currentLine + " " + nextLine; - return combinedLine;*/ + return combinedLine; - var stringBuilder = new StringBuilder(); + /*var stringBuilder = new StringBuilder(); stringBuilder.AppendLine(currentLine); stringBuilder.Append(nextLine); - return stringBuilder.ToString(); + return stringBuilder.ToString();*/ } } } \ No newline at end of file diff --git a/DbcParserLib/Parsers/CommentLineParser.cs b/DbcParserLib/Parsers/CommentLineParser.cs index 60db859..980a748 100644 --- a/DbcParserLib/Parsers/CommentLineParser.cs +++ b/DbcParserLib/Parsers/CommentLineParser.cs @@ -1,4 +1,3 @@ -using System.Text; using System.Text.RegularExpressions; using DbcParserLib.Observers; @@ -22,39 +21,34 @@ public CommentLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line; //line.Trim(); - - if (cleanLine.StartsWith(CommentLineStarter) == false) + if (line.StartsWith(CommentLineStarter) == false) return false; - /*if (!cleanLine.EndsWith(";")) - cleanLine = GetNextLines(cleanLine, m_observer, nextLineProvider);*/ - - if (cleanLine.StartsWith("CM_ SG_")) + if (line.StartsWith("CM_ SG_")) { - SetSignalComment(cleanLine, m_observer, builder, nextLineProvider); + SetSignalComment(line, m_observer, builder, nextLineProvider); return true; } - if (cleanLine.StartsWith("CM_ BU_")) + if (line.StartsWith("CM_ BU_")) { - SetNodeComment(cleanLine, m_observer, builder, nextLineProvider); + SetNodeComment(line, m_observer, builder, nextLineProvider); return true; } - if (cleanLine.StartsWith("CM_ BO_")) + if (line.StartsWith("CM_ BO_")) { - SetMessageComment(cleanLine, m_observer, builder, nextLineProvider); + SetMessageComment(line, m_observer, builder, nextLineProvider); return true; } - if (cleanLine.StartsWith("CM_ EV_")) + if (line.StartsWith("CM_ EV_")) { - SetEnvironmentVariableComment(cleanLine, m_observer, builder); + SetEnvironmentVariableComment(line, m_observer, builder); return true; } - var match = Regex.Match(cleanLine, GenericCommentParsingRegex); + var match = Regex.Match((string)line, GenericCommentParsingRegex); if (match.Success) return true; @@ -101,19 +95,5 @@ private static void SetEnvironmentVariableComment(string envCommentStr, IParseFa else observer.CommentSyntaxError(); } - - /*private static string GetNextLines(string currentLine, IParseFailureObserver observer, INextLineProvider nextLineProvider) - { - var stringBuilder = new StringBuilder(); - stringBuilder.AppendLine(currentLine); - - while (nextLineProvider.TryGetLine(out var nextLine)) - { - stringBuilder.AppendLine(nextLine); - if (nextLine.EndsWith(";")) - break; - } - return stringBuilder.ToString(); - }*/ } } \ No newline at end of file From a29e7996495e796be63b16d8ef1e0459dbe44398 Mon Sep 17 00:00:00 2001 From: Uight Date: Sat, 31 Aug 2024 12:50:40 +0200 Subject: [PATCH 07/34] All current Test running --- DbcParserLib/NextLineProvider.cs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index d942246..f34d4cb 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -49,6 +49,7 @@ public class NextLineProvider : INextLineProvider "SIG_VALTYPE_", "SIG_TYPE_REF_", + "EV_", "EV_DATA_", "ENVVAR_DATA_", @@ -169,16 +170,10 @@ private bool CheckNextLineParsing(string nextLine) private string CombineLines(string currentLine, string nextLine) { - currentLine = currentLine.TrimEnd('\r', '\n'); - - string combinedLine = currentLine + " " + nextLine; - - return combinedLine; - - /*var stringBuilder = new StringBuilder(); + var stringBuilder = new StringBuilder(); stringBuilder.AppendLine(currentLine); stringBuilder.Append(nextLine); - return stringBuilder.ToString();*/ + return stringBuilder.ToString(); } } } \ No newline at end of file From 99cbf4658dc0ea7523ec664001807c7108273584 Mon Sep 17 00:00:00 2001 From: Uight Date: Sat, 31 Aug 2024 13:11:40 +0200 Subject: [PATCH 08/34] dummy add the three cases from the tikcet at strings --- DbcParserLib.Tests/ParserTests.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/DbcParserLib.Tests/ParserTests.cs b/DbcParserLib.Tests/ParserTests.cs index 460cf3b..668a95c 100644 --- a/DbcParserLib.Tests/ParserTests.cs +++ b/DbcParserLib.Tests/ParserTests.cs @@ -419,5 +419,20 @@ public void MultipleEnvironmentVariableToOneNodeTest() Assert.That(dbc.Nodes.First().EnvironmentVariables.Last().Key, Is.EqualTo("EnvVarName3")); Assert.That(dbc.Nodes.First().EnvironmentVariables.Last().Value.Type, Is.EqualTo(EnvDataType.Data)); } + + [Test] + public void MultilineTestcases() + { + var dbcString1 = @"VAL_ 134 TEST_BuckleSwitch 0 ""Buckled "" 1 "" Unbuckle "" 2 ""Not Used"" 3 ""Not Used +Default value: 0x0"";"; + + var dbcString2 = @"BO_ 1160 DAS_steeringControl: 4 NEO + SG_ DAS_steeringControlType : 23|2@0+ + (1,0) [0|0] "" EPAS + SG_ DAS_steeringControlChecksum : 31|8@0+ (1,0) [0|0] "" EPAS, OTHER + SG_ DAS_steeringControlCounter : 19|4@0+ (1,0) [0|0] "" EPAS"; + + var dbcString3 = "VAL_ 1160 DAS_steeringControlType 1 \"ANGLE_CONTROL\" 3 \"DISABLED\" 0 \"NONE\" 2 \"RESERVED\" ; VAL_ 1160 DAS_steeringAngleRequest 16384 \"ZERO_ANGLE\" ;"; + } } } \ No newline at end of file From 0e7eb84734e489b55a3514aa5462c5409adab997 Mon Sep 17 00:00:00 2001 From: Uight Date: Sat, 31 Aug 2024 13:35:44 +0200 Subject: [PATCH 09/34] Basic test + bugfix multidefinition in one line --- DbcParserLib.Tests/ParserTests.cs | 52 ++++++++++++++++++++++++++----- DbcParserLib/NextLineProvider.cs | 2 +- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/DbcParserLib.Tests/ParserTests.cs b/DbcParserLib.Tests/ParserTests.cs index 668a95c..d16854e 100644 --- a/DbcParserLib.Tests/ParserTests.cs +++ b/DbcParserLib.Tests/ParserTests.cs @@ -3,6 +3,7 @@ using NUnit.Framework; using System.Linq; using DbcParserLib.Model; +using DbcParserLib.Observers; namespace DbcParserLib.Tests { @@ -421,18 +422,55 @@ public void MultipleEnvironmentVariableToOneNodeTest() } [Test] - public void MultilineTestcases() + public void MultilineValueTableTest() { - var dbcString1 = @"VAL_ 134 TEST_BuckleSwitch 0 ""Buckled "" 1 "" Unbuckle "" 2 ""Not Used"" 3 ""Not Used + var dbcString = @" +BO_ 134 TestMessage: 1 Test + SG_ TEST_BuckleSwitch : 0|8@0+ (1,0) [0|0] """" Receiver + +VAL_ 134 TEST_BuckleSwitch 0 ""Buckled "" 1 "" Unbuckle "" 2 ""Not Used"" 3 ""Not Used Default value: 0x0"";"; - var dbcString2 = @"BO_ 1160 DAS_steeringControl: 4 NEO + var failureObserver = new SimpleFailureObserver(); + Parser.SetParsingFailuresObserver(failureObserver); + var dbc = Parser.Parse(dbcString); + var errorList = failureObserver.GetErrorList(); + + Assert.That(errorList, Has.Count.EqualTo(0)); + } + + [Test] + public void MultilineSignalTest() + { + var dbcString = @"BO_ 1160 DAS_steeringControl: 4 NEO SG_ DAS_steeringControlType : 23|2@0+ - (1,0) [0|0] "" EPAS - SG_ DAS_steeringControlChecksum : 31|8@0+ (1,0) [0|0] "" EPAS, OTHER - SG_ DAS_steeringControlCounter : 19|4@0+ (1,0) [0|0] "" EPAS"; + (1,0) [0|0] """" EPAS + SG_ DAS_steeringControlChecksum : 31|8@0+ (1,0) [0|0] """" EPAS, OTHER + SG_ DAS_steeringControlCounter : 19|4@0+ (1,0) [0|0] """" EPAS"; + + var failureObserver = new SimpleFailureObserver(); + Parser.SetParsingFailuresObserver(failureObserver); + var dbc = Parser.Parse(dbcString); + var errorList = failureObserver.GetErrorList(); + + Assert.That(errorList, Has.Count.EqualTo(0)); + } + + [Test] + public void SingleLineMultiValueTableTest() + { + var dbcString = @"BO_ 1160 DAS_steeringControl: 4 NEO + SG_ DAS_steeringControlType : 23|2@0+ (1,0) [0|0] """" EPAS + SG_ DAS_steeringAngleRequest : 0|16@0+ (1,0) [0|0] """" EPAS + +VAL_ 1160 DAS_steeringControlType 1 ""ANGLE_CONTROL"" 3 ""DISABLED"" 0 ""NONE"" 2 ""RESERVED"" ; VAL_ 1160 DAS_steeringAngleRequest 16384 ""ZERO_ANGLE"" ;"; + + var failureObserver = new SimpleFailureObserver(); + Parser.SetParsingFailuresObserver(failureObserver); + var dbc = Parser.Parse(dbcString); + var errorList = failureObserver.GetErrorList(); - var dbcString3 = "VAL_ 1160 DAS_steeringControlType 1 \"ANGLE_CONTROL\" 3 \"DISABLED\" 0 \"NONE\" 2 \"RESERVED\" ; VAL_ 1160 DAS_steeringAngleRequest 16384 \"ZERO_ANGLE\" ;"; + Assert.That(errorList, Has.Count.EqualTo(0)); } } } \ No newline at end of file diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index f34d4cb..bf05f07 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -115,7 +115,7 @@ private string HandleMultipleDefinitionsPerLine(string line) return line; } var firstLinePart = line.Substring(0, definitionTerminationLocation + 1); - m_lineMemory = line.Substring(definitionTerminationLocation + 2, line.Length - 1).Trim(); + m_lineMemory = line.Substring(definitionTerminationLocation + 2, line.Length - 1 - firstLinePart.Length).Trim(); m_isVirtualLine = true; return firstLinePart; From 17ace3fb2559fa528a6e4594fbe2032de005d4c1 Mon Sep 17 00:00:00 2001 From: Uight Date: Sat, 31 Aug 2024 14:47:28 +0200 Subject: [PATCH 10/34] Complete the tests --- DbcParserLib.Tests/ParserTests.cs | 62 +++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/DbcParserLib.Tests/ParserTests.cs b/DbcParserLib.Tests/ParserTests.cs index d16854e..c78a15b 100644 --- a/DbcParserLib.Tests/ParserTests.cs +++ b/DbcParserLib.Tests/ParserTests.cs @@ -437,16 +437,39 @@ VAL_ 134 TEST_BuckleSwitch 0 ""Buckled "" 1 "" Unbuckle "" 2 ""Not Used"" 3 ""No var errorList = failureObserver.GetErrorList(); Assert.That(errorList, Has.Count.EqualTo(0)); + Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Signals.Count(), Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Count, Is.EqualTo(4)); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Last().Key, Is.EqualTo(3)); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Last().Value, Is.EqualTo("Not Used\r\nDefault value: 0x0")); } [Test] - public void MultilineSignalTest() + public void MultilineSignalTestEOF() + { + var dbcString = @"BO_ 1160 DAS_steeringControl: 4 NEO + SG_ DAS_steeringControlType : 23|2@0+ + (1,0) [0|0] """" EPAS"; + + var failureObserver = new SimpleFailureObserver(); + Parser.SetParsingFailuresObserver(failureObserver); + var dbc = Parser.Parse(dbcString); + var errorList = failureObserver.GetErrorList(); + + Assert.That(errorList, Has.Count.EqualTo(0)); + Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Signals.Count(), Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Signals.First().Receiver.Count, Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Signals.First().Receiver.First(), Is.EqualTo("EPAS")); + } + + [Test] + public void MultilineSignalTestNextLineEmpty() { var dbcString = @"BO_ 1160 DAS_steeringControl: 4 NEO SG_ DAS_steeringControlType : 23|2@0+ (1,0) [0|0] """" EPAS - SG_ DAS_steeringControlChecksum : 31|8@0+ (1,0) [0|0] """" EPAS, OTHER - SG_ DAS_steeringControlCounter : 19|4@0+ (1,0) [0|0] """" EPAS"; +"; var failureObserver = new SimpleFailureObserver(); Parser.SetParsingFailuresObserver(failureObserver); @@ -454,6 +477,31 @@ public void MultilineSignalTest() var errorList = failureObserver.GetErrorList(); Assert.That(errorList, Has.Count.EqualTo(0)); + Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Signals.Count(), Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Signals.First().Receiver.Count, Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Signals.First().Receiver.First(), Is.EqualTo("EPAS")); + } + + [Test] + public void MultilineSignalTestNextLineOtherDefinition() + { + var dbcString = @"BO_ 1160 DAS_steeringControl: 4 NEO + SG_ DAS_steeringControlType : 23|2@0+ + (1,0) [0|0] """" EPAS + SG_ DAS_steeringAngleRequest : 0|16@0+ (1,0) [0|0] """" EPAS +"; + + var failureObserver = new SimpleFailureObserver(); + Parser.SetParsingFailuresObserver(failureObserver); + var dbc = Parser.Parse(dbcString); + var errorList = failureObserver.GetErrorList(); + + Assert.That(errorList, Has.Count.EqualTo(0)); + Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Signals.Count(), Is.EqualTo(2)); + Assert.That(dbc.Messages.First().Signals.First().Receiver.Count, Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Signals.First().Receiver.First(), Is.EqualTo("EPAS")); } [Test] @@ -471,6 +519,14 @@ public void SingleLineMultiValueTableTest() var errorList = failureObserver.GetErrorList(); Assert.That(errorList, Has.Count.EqualTo(0)); + Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Signals.Count(), Is.EqualTo(2)); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Count, Is.EqualTo(4)); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Last().Key, Is.EqualTo(2)); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Last().Value, Is.EqualTo("RESERVED")); + Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Count, Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Last().Key, Is.EqualTo(16384)); + Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Last().Value, Is.EqualTo("ZERO_ANGLE")); } } } \ No newline at end of file From a271ffad45dd823f846e3d73bafab2362c9d7102 Mon Sep 17 00:00:00 2001 From: Uight Date: Sat, 31 Aug 2024 16:32:09 +0200 Subject: [PATCH 11/34] All line parsers use line trim and replace newline by space. Test adjusted for signal --- DbcParserLib.Tests/CommentLineParserTests.cs | 15 ++----------- DbcParserLib.Tests/ParserTests.cs | 2 +- DbcParserLib.Tests/SignalLineParserTests.cs | 5 ++--- DbcParserLib/ExtensionsAndHelpers.cs | 5 +++++ .../Observers/IParseFailureObserver.cs | 2 +- .../Observers/SilentFailureObserver.cs | 2 +- .../Observers/SimpleFailureObserver.cs | 4 ++-- DbcParserLib/Parsers/CommentLineParser.cs | 22 ++++++++++--------- .../EnvironmentDataVariableLineParser.cs | 2 +- .../Parsers/EnvironmentVariableLineParser.cs | 2 +- DbcParserLib/Parsers/MessageLineParser.cs | 6 +++-- DbcParserLib/Parsers/NodeLineParser.cs | 6 +++-- .../Parsers/PropertiesDefinitionLineParser.cs | 2 +- DbcParserLib/Parsers/PropertiesLineParser.cs | 2 +- DbcParserLib/Parsers/SignalLineParser.cs | 6 +++-- .../Parsers/SignalValueTypeLineParser.cs | 2 +- DbcParserLib/Parsers/UnknownLineParser.cs | 3 +-- .../Parsers/ValueTableDefinitionLineParser.cs | 2 +- DbcParserLib/Parsers/ValueTableLineParser.cs | 2 +- 19 files changed, 46 insertions(+), 46 deletions(-) diff --git a/DbcParserLib.Tests/CommentLineParserTests.cs b/DbcParserLib.Tests/CommentLineParserTests.cs index 9ed8f71..a2b9fb8 100644 --- a/DbcParserLib.Tests/CommentLineParserTests.cs +++ b/DbcParserLib.Tests/CommentLineParserTests.cs @@ -50,24 +50,13 @@ public void RandomStartIsIgnored() } [Test] - // Should parse as it is a comment but should be observed as error - // This however would be catched previously by the IgnoreLineParser public void OnlyPrefixIsIgnored() { var dbcBuilderMock = m_repository.Create(); - - var counter = 0; - var failureObserverMock = new Mock(); - failureObserverMock - .Setup(observer => observer.CommentSyntaxError()) - .Callback(() => counter++); - - var commentLineParser = new CommentLineParser(failureObserverMock.Object); - + var commentLineParser = CreateParser(); var nextLineProviderMock = m_repository.Create(); - Assert.That(commentLineParser.TryParse("CM_ ", dbcBuilderMock.Object, nextLineProviderMock.Object), Is.True); - Assert.That(counter, Is.EqualTo(1)); + Assert.That(commentLineParser.TryParse("CM_ ", dbcBuilderMock.Object, nextLineProviderMock.Object), Is.False); } [Test] diff --git a/DbcParserLib.Tests/ParserTests.cs b/DbcParserLib.Tests/ParserTests.cs index c78a15b..09f0b9b 100644 --- a/DbcParserLib.Tests/ParserTests.cs +++ b/DbcParserLib.Tests/ParserTests.cs @@ -441,7 +441,7 @@ VAL_ 134 TEST_BuckleSwitch 0 ""Buckled "" 1 "" Unbuckle "" 2 ""Not Used"" 3 ""No Assert.That(dbc.Messages.First().Signals.Count(), Is.EqualTo(1)); Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Count, Is.EqualTo(4)); Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Last().Key, Is.EqualTo(3)); - Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Last().Value, Is.EqualTo("Not Used\r\nDefault value: 0x0")); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Last().Value, Is.EqualTo("Not Used Default value: 0x0")); } [Test] diff --git a/DbcParserLib.Tests/SignalLineParserTests.cs b/DbcParserLib.Tests/SignalLineParserTests.cs index 0f1990c..138c4a3 100644 --- a/DbcParserLib.Tests/SignalLineParserTests.cs +++ b/DbcParserLib.Tests/SignalLineParserTests.cs @@ -49,13 +49,13 @@ public void RandomStartIsIgnored() } [Test] - public void OnlyPrefixWithSpacesIsAcceptedWithNoInteractions() + public void OnlyPrefixIsIgnored() { var dbcBuilderMock = m_repository.Create(); var signalLineParser = CreateParser(); var nextLineProviderMock = m_repository.Create(); - Assert.That(signalLineParser.TryParse("SG_ ", dbcBuilderMock.Object, nextLineProviderMock.Object), Is.True); + Assert.That(signalLineParser.TryParse("SG_ ", dbcBuilderMock.Object, nextLineProviderMock.Object), Is.False); } [Test] @@ -192,7 +192,6 @@ public void ParseSignalWithDifferentColonSpaces() [TestCase("SG_ qGearboxOilMin : 0|16@1+ (0.1,0) [0|6553.5] l/min NATEC")] [TestCase("SG_ \"qGearboxOilMin\" : 0|16@1+ (0.1,0) [0|6553.5] \"l/min\" NATEC")] [TestCase("SG_ qGearboxOilMin 0|16@1+ (0.1,0) [0|6553.5] \"l/min\" NATEC")] - [TestCase("SG_ ")] public void SignalSyntaxErrorIsObserved(string line) { var observerMock = m_repository.Create(); diff --git a/DbcParserLib/ExtensionsAndHelpers.cs b/DbcParserLib/ExtensionsAndHelpers.cs index 85f402f..074ca27 100644 --- a/DbcParserLib/ExtensionsAndHelpers.cs +++ b/DbcParserLib/ExtensionsAndHelpers.cs @@ -123,6 +123,11 @@ public static string[] SplitBySpace(this string value) { return value.Split(SpaceArray, System.StringSplitOptions.RemoveEmptyEntries); } + + public static string ReplaceNewlinesWithSpace(this string input) + { + return input.Replace("\r\n", " "); + } } internal class StringToDictionaryParser diff --git a/DbcParserLib/Observers/IParseFailureObserver.cs b/DbcParserLib/Observers/IParseFailureObserver.cs index 8f4639e..cb22e2e 100644 --- a/DbcParserLib/Observers/IParseFailureObserver.cs +++ b/DbcParserLib/Observers/IParseFailureObserver.cs @@ -35,7 +35,7 @@ public interface IParseFailureObserver void TableMapNameNotFound(string tableName); void PropertyValueOutOfBound(string propertyName, string value); void PropertyValueOutOfIndex(string propertyName, string index); - void UnknownLine(); + void UnknownLine(string line); void NoMessageFound(); void Clear(); } diff --git a/DbcParserLib/Observers/SilentFailureObserver.cs b/DbcParserLib/Observers/SilentFailureObserver.cs index 2dd0d16..234570a 100644 --- a/DbcParserLib/Observers/SilentFailureObserver.cs +++ b/DbcParserLib/Observers/SilentFailureObserver.cs @@ -128,7 +128,7 @@ public void PropertyValueOutOfIndex(string propertyName, string index) { } - public void UnknownLine() + public void UnknownLine(string line) { } diff --git a/DbcParserLib/Observers/SimpleFailureObserver.cs b/DbcParserLib/Observers/SimpleFailureObserver.cs index 41330e2..aa68eb2 100644 --- a/DbcParserLib/Observers/SimpleFailureObserver.cs +++ b/DbcParserLib/Observers/SimpleFailureObserver.cs @@ -169,9 +169,9 @@ public void PropertyValueOutOfIndex(string propertyName, string index) AddError($"Out of index value [{index}] for '{propertyName}' property"); } - public void UnknownLine() + public void UnknownLine(string line) { - AddError("Unknown syntax"); + AddError($"Unknown syntax in line: '{line}'"); } public void NoMessageFound() diff --git a/DbcParserLib/Parsers/CommentLineParser.cs b/DbcParserLib/Parsers/CommentLineParser.cs index 980a748..acdd9fc 100644 --- a/DbcParserLib/Parsers/CommentLineParser.cs +++ b/DbcParserLib/Parsers/CommentLineParser.cs @@ -21,34 +21,36 @@ public CommentLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - if (line.StartsWith(CommentLineStarter) == false) + var cleanLine = line.Trim(); + + if (cleanLine.StartsWith(CommentLineStarter) == false) return false; - if (line.StartsWith("CM_ SG_")) + if (cleanLine.StartsWith("CM_ SG_")) { - SetSignalComment(line, m_observer, builder, nextLineProvider); + SetSignalComment(cleanLine, m_observer, builder, nextLineProvider); return true; } - if (line.StartsWith("CM_ BU_")) + if (cleanLine.StartsWith("CM_ BU_")) { - SetNodeComment(line, m_observer, builder, nextLineProvider); + SetNodeComment(cleanLine, m_observer, builder, nextLineProvider); return true; } - if (line.StartsWith("CM_ BO_")) + if (cleanLine.StartsWith("CM_ BO_")) { - SetMessageComment(line, m_observer, builder, nextLineProvider); + SetMessageComment(cleanLine, m_observer, builder, nextLineProvider); return true; } - if (line.StartsWith("CM_ EV_")) + if (cleanLine.StartsWith("CM_ EV_")) { - SetEnvironmentVariableComment(line, m_observer, builder); + SetEnvironmentVariableComment(cleanLine, m_observer, builder); return true; } - var match = Regex.Match((string)line, GenericCommentParsingRegex); + var match = Regex.Match((string)cleanLine, GenericCommentParsingRegex); if (match.Success) return true; diff --git a/DbcParserLib/Parsers/EnvironmentDataVariableLineParser.cs b/DbcParserLib/Parsers/EnvironmentDataVariableLineParser.cs index 7acd8b9..2de625b 100644 --- a/DbcParserLib/Parsers/EnvironmentDataVariableLineParser.cs +++ b/DbcParserLib/Parsers/EnvironmentDataVariableLineParser.cs @@ -17,7 +17,7 @@ public EnvironmentDataVariableLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim(' '); + var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); if (cleanLine.StartsWith(EnvironmentDataVariableLineStarter) == false) return false; diff --git a/DbcParserLib/Parsers/EnvironmentVariableLineParser.cs b/DbcParserLib/Parsers/EnvironmentVariableLineParser.cs index 825e612..ab14a92 100644 --- a/DbcParserLib/Parsers/EnvironmentVariableLineParser.cs +++ b/DbcParserLib/Parsers/EnvironmentVariableLineParser.cs @@ -18,7 +18,7 @@ public EnvironmentVariableLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim(' '); + var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); if (cleanLine.StartsWith(EnvironmentVariableLineStarter) == false) return false; diff --git a/DbcParserLib/Parsers/MessageLineParser.cs b/DbcParserLib/Parsers/MessageLineParser.cs index 48712f4..12ee2b2 100644 --- a/DbcParserLib/Parsers/MessageLineParser.cs +++ b/DbcParserLib/Parsers/MessageLineParser.cs @@ -19,10 +19,12 @@ public MessageLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - if(line.Trim().StartsWith(MessageLineStarter) == false) + var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); + + if (cleanLine.StartsWith(MessageLineStarter) == false) return false; - var match = Regex.Match(line, MessageRegex); + var match = Regex.Match(cleanLine, MessageRegex); if(match.Success) { var msg = new Message() diff --git a/DbcParserLib/Parsers/NodeLineParser.cs b/DbcParserLib/Parsers/NodeLineParser.cs index 6cceca2..0fc3595 100644 --- a/DbcParserLib/Parsers/NodeLineParser.cs +++ b/DbcParserLib/Parsers/NodeLineParser.cs @@ -18,10 +18,12 @@ public NodeLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - if (line.TrimStart().StartsWith(NodeLineStarter) == false) + var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); + + if (cleanLine.StartsWith(NodeLineStarter) == false) return false; - var match = Regex.Match(line, NodeLineParsingRegex); + var match = Regex.Match(cleanLine, NodeLineParsingRegex); if (match.Success) { foreach (var nodeName in match.Groups[1].Value.TrimStart().SplitBySpace()) diff --git a/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs b/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs index a20b6e2..e0be764 100644 --- a/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs +++ b/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs @@ -21,7 +21,7 @@ public PropertiesDefinitionLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim(' '); + var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); if (cleanLine.StartsWith(PropertiesDefinitionLineStarter) == false && cleanLine.StartsWith(PropertiesDefinitionDefaultLineStarter) == false) diff --git a/DbcParserLib/Parsers/PropertiesLineParser.cs b/DbcParserLib/Parsers/PropertiesLineParser.cs index 4a09bcb..2d812a2 100644 --- a/DbcParserLib/Parsers/PropertiesLineParser.cs +++ b/DbcParserLib/Parsers/PropertiesLineParser.cs @@ -18,7 +18,7 @@ public PropertiesLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim(' '); + var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); if (cleanLine.StartsWith(PropertiesLineStarter) == false) return false; diff --git a/DbcParserLib/Parsers/SignalLineParser.cs b/DbcParserLib/Parsers/SignalLineParser.cs index caed06a..5625849 100644 --- a/DbcParserLib/Parsers/SignalLineParser.cs +++ b/DbcParserLib/Parsers/SignalLineParser.cs @@ -21,10 +21,12 @@ public SignalLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - if (line.TrimStart().StartsWith(SignalLineStarter) == false) + var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); + + if (cleanLine.StartsWith(SignalLineStarter) == false) return false; - var match = Regex.Match(line, SignalRegex); + var match = Regex.Match(cleanLine, SignalRegex); if (match.Success) { var factorStr = match.Groups[7].Value; diff --git a/DbcParserLib/Parsers/SignalValueTypeLineParser.cs b/DbcParserLib/Parsers/SignalValueTypeLineParser.cs index bdac460..cc2bdb0 100644 --- a/DbcParserLib/Parsers/SignalValueTypeLineParser.cs +++ b/DbcParserLib/Parsers/SignalValueTypeLineParser.cs @@ -18,7 +18,7 @@ public SignalValueTypeLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim(' '); + var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); if (cleanLine.StartsWith(SignalValueTypeStarter) == false) return false; diff --git a/DbcParserLib/Parsers/UnknownLineParser.cs b/DbcParserLib/Parsers/UnknownLineParser.cs index 211bb43..66fa320 100644 --- a/DbcParserLib/Parsers/UnknownLineParser.cs +++ b/DbcParserLib/Parsers/UnknownLineParser.cs @@ -13,8 +13,7 @@ public UnknownLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - // Throw or log or add a specific entry in builder maybe? - m_observer.UnknownLine(); + m_observer.UnknownLine(line); return true; } } diff --git a/DbcParserLib/Parsers/ValueTableDefinitionLineParser.cs b/DbcParserLib/Parsers/ValueTableDefinitionLineParser.cs index c55a89e..21f6493 100644 --- a/DbcParserLib/Parsers/ValueTableDefinitionLineParser.cs +++ b/DbcParserLib/Parsers/ValueTableDefinitionLineParser.cs @@ -17,7 +17,7 @@ public ValueTableDefinitionLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim(' '); + var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); if (cleanLine.StartsWith(ValueTableDefinitionLineStarter) == false) return false; diff --git a/DbcParserLib/Parsers/ValueTableLineParser.cs b/DbcParserLib/Parsers/ValueTableLineParser.cs index 48e0dc8..8568c8e 100644 --- a/DbcParserLib/Parsers/ValueTableLineParser.cs +++ b/DbcParserLib/Parsers/ValueTableLineParser.cs @@ -18,7 +18,7 @@ public ValueTableLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim(' '); + var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); if (cleanLine.StartsWith(ValueTableLineStarter) == false) return false; From 0c5ee3578dd111fec4aa8085871f4a56510c52cc Mon Sep 17 00:00:00 2001 From: Uight Date: Sat, 31 Aug 2024 17:07:00 +0200 Subject: [PATCH 12/34] allow other newline chars --- DbcParserLib.Tests/ParserTests.cs | 18 ++++++++++++++++++ DbcParserLib/ExtensionsAndHelpers.cs | 9 ++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/DbcParserLib.Tests/ParserTests.cs b/DbcParserLib.Tests/ParserTests.cs index 09f0b9b..9782ec0 100644 --- a/DbcParserLib.Tests/ParserTests.cs +++ b/DbcParserLib.Tests/ParserTests.cs @@ -444,6 +444,24 @@ VAL_ 134 TEST_BuckleSwitch 0 ""Buckled "" 1 "" Unbuckle "" 2 ""Not Used"" 3 ""No Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Last().Value, Is.EqualTo("Not Used Default value: 0x0")); } + [Test] + public void MultilineValueTableTestWithOnlyNewLine() + { + var dbcString = "BO_ 134 TestMessage: 1 Test \n SG_ TEST_BuckleSwitch : 0|8@0+ (1,0) [0|0] \"\" Receiver\n VAL_ 134 TEST_BuckleSwitch 0 \"Buckled \" 1 \" Unbuckle \" 2 \"Not Used\" 3 \"Not Used \nDefault value: 0x0\";"; + + var failureObserver = new SimpleFailureObserver(); + Parser.SetParsingFailuresObserver(failureObserver); + var dbc = Parser.Parse(dbcString); + var errorList = failureObserver.GetErrorList(); + + Assert.That(errorList, Has.Count.EqualTo(0)); + Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Signals.Count(), Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Count, Is.EqualTo(4)); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Last().Key, Is.EqualTo(3)); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Last().Value, Is.EqualTo("Not Used Default value: 0x0")); + } + [Test] public void MultilineSignalTestEOF() { diff --git a/DbcParserLib/ExtensionsAndHelpers.cs b/DbcParserLib/ExtensionsAndHelpers.cs index 074ca27..012b763 100644 --- a/DbcParserLib/ExtensionsAndHelpers.cs +++ b/DbcParserLib/ExtensionsAndHelpers.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Collections.Generic; using DbcParserLib.Model; +using System.Text.RegularExpressions; namespace DbcParserLib { @@ -124,9 +125,15 @@ public static string[] SplitBySpace(this string value) return value.Split(SpaceArray, System.StringSplitOptions.RemoveEmptyEntries); } + // Sequence of return codes was taken from the internals of "String.ReplaceLineEndings" method. + private const string NewLineCharsExceptLineFeed = "\r\f\u0085\u2028\u2029\n"; + private static readonly string pattern = $"[{Regex.Escape(NewLineCharsExceptLineFeed)}]+"; + public static string ReplaceNewlinesWithSpace(this string input) { - return input.Replace("\r\n", " "); + // Would like to use "String.ReplaceLineEndings" but its unavailable because of the target frameworks + // Feel free to optimate + return Regex.Replace(input, pattern, " "); } } From 1eed82f11c25819248602e5b290338bac8bc5fbc Mon Sep 17 00:00:00 2001 From: Uight Date: Sat, 31 Aug 2024 17:08:45 +0200 Subject: [PATCH 13/34] small rename --- DbcParserLib/ExtensionsAndHelpers.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DbcParserLib/ExtensionsAndHelpers.cs b/DbcParserLib/ExtensionsAndHelpers.cs index 012b763..f3b4b28 100644 --- a/DbcParserLib/ExtensionsAndHelpers.cs +++ b/DbcParserLib/ExtensionsAndHelpers.cs @@ -126,8 +126,8 @@ public static string[] SplitBySpace(this string value) } // Sequence of return codes was taken from the internals of "String.ReplaceLineEndings" method. - private const string NewLineCharsExceptLineFeed = "\r\f\u0085\u2028\u2029\n"; - private static readonly string pattern = $"[{Regex.Escape(NewLineCharsExceptLineFeed)}]+"; + private const string NewLineChars = "\r\f\u0085\u2028\u2029\n"; + private static readonly string pattern = $"[{Regex.Escape(NewLineChars)}]+"; public static string ReplaceNewlinesWithSpace(this string input) { From a5fddc1743c36397ecca462452b94c503dc8888b Mon Sep 17 00:00:00 2001 From: Uight Date: Mon, 2 Sep 2024 18:15:25 +0200 Subject: [PATCH 14/34] some changes from the we --- DbcParserLib/NextLineProvider.cs | 98 ++++++++----------- .../EnvironmentDataVariableLineParser.cs | 2 +- .../Parsers/EnvironmentVariableLineParser.cs | 2 +- DbcParserLib/Parsers/MessageLineParser.cs | 2 +- DbcParserLib/Parsers/NodeLineParser.cs | 2 +- .../Parsers/PropertiesDefinitionLineParser.cs | 2 +- DbcParserLib/Parsers/PropertiesLineParser.cs | 2 +- DbcParserLib/Parsers/SignalLineParser.cs | 2 +- .../Parsers/SignalValueTypeLineParser.cs | 2 +- .../Parsers/ValueTableDefinitionLineParser.cs | 2 +- DbcParserLib/Parsers/ValueTableLineParser.cs | 2 +- DbcParserLib/PeekableTextReader.cs | 38 +++++++ 12 files changed, 91 insertions(+), 65 deletions(-) create mode 100644 DbcParserLib/PeekableTextReader.cs diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index bf05f07..47e2d66 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -1,5 +1,6 @@ using DbcParserLib.Observers; using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; @@ -8,10 +9,8 @@ namespace DbcParserLib { public class NextLineProvider : INextLineProvider { - private TextReader m_reader; - private IParseFailureObserver m_observer; - private string m_lineMemory; - private bool m_isVirtualLine; + private PeekableTextReader m_reader; + private string m_virtualLineMemory; private string lineTermination = ";"; @@ -69,33 +68,26 @@ public class NextLineProvider : INextLineProvider public NextLineProvider(TextReader reader, IParseFailureObserver observer) { - m_reader = reader; - m_observer = observer; + m_reader = new PeekableTextReader(reader, observer); } public bool TryGetLine(out string line) { line = null; - if (m_lineMemory != null) + if (m_virtualLineMemory != null) { - if (m_isVirtualLine == false) - { - m_observer.CurrentLine++; - } - line = m_lineMemory; - line = line.Trim(); - m_lineMemory = null; + line = m_virtualLineMemory.Trim(); + m_virtualLineMemory = null; line = HandleMultipleDefinitionsPerLine(line); line = HandleMultiline(line); return true; } - if (m_reader.Peek() >= 0) + var readLine = m_reader.ReadLine(); + if (readLine != null) { - m_observer.CurrentLine++; - line = m_reader.ReadLine(); - line = line.Trim(); + line = readLine.Trim(); line = HandleMultipleDefinitionsPerLine(line); line = HandleMultiline(line); @@ -114,52 +106,56 @@ private string HandleMultipleDefinitionsPerLine(string line) { return line; } - var firstLinePart = line.Substring(0, definitionTerminationLocation + 1); - m_lineMemory = line.Substring(definitionTerminationLocation + 2, line.Length - 1 - firstLinePart.Length).Trim(); - m_isVirtualLine = true; - return firstLinePart; + var partAfterTermination = line.Substring(definitionTerminationLocation + 2, line.Length - 2 - definitionTerminationLocation).Trim(); + + if (CheckNextLineParsing(partAfterTermination)) // check if the remaining line is a new definition. otherwise assume your reading a comment + { + m_virtualLineMemory = partAfterTermination; + return line.Substring(0, definitionTerminationLocation + 1); + } + + // Assuming the line is a comment now => dont check for further occurences of the termination for now as they most likely will also just be comment or end of line + // Would be a very special case were a comment is followed by a definition in the very same line. Could be handled but not for now } return line; } private string HandleMultiline(string line) { - if (line.EndsWith(lineTermination)) - { - return line; - } - var nextLine = PeakNextLine(); - if (string.IsNullOrWhiteSpace(nextLine)) + // This check is not verified yet. A comment is allowed to contain termination char. Why not at end of line? + // Current comment parsing would fail in this condition + /*if (line.EndsWith(lineTermination)) { return line; - } - if (CheckNextLineParsing(PeakNextLine())) + }*/ + var stringsList = new List { line }; + var nextLine = m_reader.PeekLine(); + + while (nextLine != null && CheckNextLineParsing(nextLine.Trim()) == false) { - return line; + //Just add line for the moment; Dont assume that a following line contains the end of the first definition + an additional definition + stringsList.Add(m_reader.ReadLine().Trim()); + nextLine = m_reader.PeekLine(); } - TryGetLine(out var actualNextLine); - return CombineLines(line, actualNextLine); - } - - private string PeakNextLine() - { - if (m_lineMemory == null) + //Remove trailing empty lines but never the first line; return empty string initial line was empty; + for (int i = stringsList.Count - 1; i > 0; i--) { - var nextLine = m_reader.ReadLine(); - if (nextLine is null) + if (string.IsNullOrEmpty(stringsList[i])) { - return nextLine; + stringsList.RemoveAt(i); } - m_lineMemory = nextLine; - m_isVirtualLine = false; - return m_lineMemory; } - else + + var stringBuilder = new StringBuilder(); + for (int i = 0; i < stringsList.Count - 1; i++) { - return m_lineMemory; + stringBuilder.AppendLine(stringsList[i]); } + stringBuilder.Append(stringsList.Last()); + + return stringBuilder.ToString(); } private bool CheckNextLineParsing(string nextLine) @@ -167,13 +163,5 @@ private bool CheckNextLineParsing(string nextLine) nextLine = nextLine.TrimStart(); return keywords.Any(prefix => nextLine.StartsWith(prefix)); } - - private string CombineLines(string currentLine, string nextLine) - { - var stringBuilder = new StringBuilder(); - stringBuilder.AppendLine(currentLine); - stringBuilder.Append(nextLine); - return stringBuilder.ToString(); - } } -} \ No newline at end of file +} diff --git a/DbcParserLib/Parsers/EnvironmentDataVariableLineParser.cs b/DbcParserLib/Parsers/EnvironmentDataVariableLineParser.cs index 2de625b..fdeb282 100644 --- a/DbcParserLib/Parsers/EnvironmentDataVariableLineParser.cs +++ b/DbcParserLib/Parsers/EnvironmentDataVariableLineParser.cs @@ -17,7 +17,7 @@ public EnvironmentDataVariableLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); + var cleanLine = line.ReplaceNewlinesWithSpace().Trim(); if (cleanLine.StartsWith(EnvironmentDataVariableLineStarter) == false) return false; diff --git a/DbcParserLib/Parsers/EnvironmentVariableLineParser.cs b/DbcParserLib/Parsers/EnvironmentVariableLineParser.cs index ab14a92..f44729f 100644 --- a/DbcParserLib/Parsers/EnvironmentVariableLineParser.cs +++ b/DbcParserLib/Parsers/EnvironmentVariableLineParser.cs @@ -18,7 +18,7 @@ public EnvironmentVariableLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); + var cleanLine = line.ReplaceNewlinesWithSpace().Trim(); if (cleanLine.StartsWith(EnvironmentVariableLineStarter) == false) return false; diff --git a/DbcParserLib/Parsers/MessageLineParser.cs b/DbcParserLib/Parsers/MessageLineParser.cs index 12ee2b2..7736407 100644 --- a/DbcParserLib/Parsers/MessageLineParser.cs +++ b/DbcParserLib/Parsers/MessageLineParser.cs @@ -19,7 +19,7 @@ public MessageLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); + var cleanLine = line.ReplaceNewlinesWithSpace().Trim(); if (cleanLine.StartsWith(MessageLineStarter) == false) return false; diff --git a/DbcParserLib/Parsers/NodeLineParser.cs b/DbcParserLib/Parsers/NodeLineParser.cs index 0fc3595..23d9dff 100644 --- a/DbcParserLib/Parsers/NodeLineParser.cs +++ b/DbcParserLib/Parsers/NodeLineParser.cs @@ -18,7 +18,7 @@ public NodeLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); + var cleanLine = line.ReplaceNewlinesWithSpace().Trim(); if (cleanLine.StartsWith(NodeLineStarter) == false) return false; diff --git a/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs b/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs index e0be764..cd4ea2e 100644 --- a/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs +++ b/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs @@ -21,7 +21,7 @@ public PropertiesDefinitionLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); + var cleanLine = line.ReplaceNewlinesWithSpace().Trim(); if (cleanLine.StartsWith(PropertiesDefinitionLineStarter) == false && cleanLine.StartsWith(PropertiesDefinitionDefaultLineStarter) == false) diff --git a/DbcParserLib/Parsers/PropertiesLineParser.cs b/DbcParserLib/Parsers/PropertiesLineParser.cs index 2d812a2..bd558b3 100644 --- a/DbcParserLib/Parsers/PropertiesLineParser.cs +++ b/DbcParserLib/Parsers/PropertiesLineParser.cs @@ -18,7 +18,7 @@ public PropertiesLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); + var cleanLine = line.ReplaceNewlinesWithSpace().Trim(); if (cleanLine.StartsWith(PropertiesLineStarter) == false) return false; diff --git a/DbcParserLib/Parsers/SignalLineParser.cs b/DbcParserLib/Parsers/SignalLineParser.cs index 5625849..73788fe 100644 --- a/DbcParserLib/Parsers/SignalLineParser.cs +++ b/DbcParserLib/Parsers/SignalLineParser.cs @@ -21,7 +21,7 @@ public SignalLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); + var cleanLine = line.ReplaceNewlinesWithSpace().Trim(); if (cleanLine.StartsWith(SignalLineStarter) == false) return false; diff --git a/DbcParserLib/Parsers/SignalValueTypeLineParser.cs b/DbcParserLib/Parsers/SignalValueTypeLineParser.cs index cc2bdb0..d581397 100644 --- a/DbcParserLib/Parsers/SignalValueTypeLineParser.cs +++ b/DbcParserLib/Parsers/SignalValueTypeLineParser.cs @@ -18,7 +18,7 @@ public SignalValueTypeLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); + var cleanLine = line.ReplaceNewlinesWithSpace().Trim(); if (cleanLine.StartsWith(SignalValueTypeStarter) == false) return false; diff --git a/DbcParserLib/Parsers/ValueTableDefinitionLineParser.cs b/DbcParserLib/Parsers/ValueTableDefinitionLineParser.cs index 21f6493..453fcde 100644 --- a/DbcParserLib/Parsers/ValueTableDefinitionLineParser.cs +++ b/DbcParserLib/Parsers/ValueTableDefinitionLineParser.cs @@ -17,7 +17,7 @@ public ValueTableDefinitionLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); + var cleanLine = line.ReplaceNewlinesWithSpace().Trim(); if (cleanLine.StartsWith(ValueTableDefinitionLineStarter) == false) return false; diff --git a/DbcParserLib/Parsers/ValueTableLineParser.cs b/DbcParserLib/Parsers/ValueTableLineParser.cs index 8568c8e..574e4df 100644 --- a/DbcParserLib/Parsers/ValueTableLineParser.cs +++ b/DbcParserLib/Parsers/ValueTableLineParser.cs @@ -18,7 +18,7 @@ public ValueTableLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim().ReplaceNewlinesWithSpace(); + var cleanLine = line.ReplaceNewlinesWithSpace().Trim(); if (cleanLine.StartsWith(ValueTableLineStarter) == false) return false; diff --git a/DbcParserLib/PeekableTextReader.cs b/DbcParserLib/PeekableTextReader.cs new file mode 100644 index 0000000..583972d --- /dev/null +++ b/DbcParserLib/PeekableTextReader.cs @@ -0,0 +1,38 @@ +using DbcParserLib.Observers; +using System.Collections.Generic; +using System.IO; + +namespace DbcParserLib +{ + internal class PeekableTextReader + { + private IParseFailureObserver m_observer; + private readonly TextReader m_underlying; + private readonly Queue m_bufferedLines; + + public PeekableTextReader(TextReader underlying, IParseFailureObserver observer) + { + m_underlying = underlying; + m_bufferedLines = new Queue(); + m_observer = observer; + } + + public string PeekLine() + { + string line = m_underlying.ReadLine(); + if (line == null) + return null; + m_bufferedLines.Enqueue(line); + return line; + } + + + public string ReadLine() + { + if (m_bufferedLines.Count > 0) + return m_bufferedLines.Dequeue(); + m_observer.CurrentLine++; + return m_underlying.ReadLine(); + } + } +} From abd1d07ed65824dd84370f2af22e9c7b0974a724 Mon Sep 17 00:00:00 2001 From: Uight Date: Mon, 2 Sep 2024 18:26:33 +0200 Subject: [PATCH 15/34] Upstream merge cleanup --- DbcParserLib.Tests/ParserTests.cs | 94 +++++++++---------- .../PropertiesLineParserTests.cs | 8 +- DbcParserLib/ExtensionsAndHelpers.cs | 7 +- DbcParserLib/Helpers.cs | 42 --------- .../Parsers/PropertiesDefinitionLineParser.cs | 6 +- DbcParserLib/Parsers/PropertiesLineParser.cs | 4 +- 6 files changed, 61 insertions(+), 100 deletions(-) delete mode 100644 DbcParserLib/Helpers.cs diff --git a/DbcParserLib.Tests/ParserTests.cs b/DbcParserLib.Tests/ParserTests.cs index ecf6b13..e6462d1 100644 --- a/DbcParserLib.Tests/ParserTests.cs +++ b/DbcParserLib.Tests/ParserTests.cs @@ -421,6 +421,53 @@ public void MultipleEnvironmentVariableToOneNodeTest() Assert.That(dbc.Nodes.First().EnvironmentVariables.Last().Value.Type, Is.EqualTo(EnvDataType.Data)); } + [Test] + public void CheckGlobalPropertiesTest() + { + var file = @"..\..\..\..\DbcFiles\ext_multiplexed.dbc"; + var dbc = Parser.ParseFromPath(file); + + Assert.That(dbc.GlobalProperties.Count(), Is.EqualTo(3)); + + var dbName = dbc.GlobalProperties.FirstOrDefault(x => x.CustomPropertyDefinition.Name.Equals("DBName")); + Assert.That(dbName, Is.Not.Null); + Assert.That(dbName.CustomPropertyDefinition.DataType, Is.EqualTo(CustomPropertyDataType.String)); + Assert.That(dbName.CustomPropertyDefinition.StringCustomProperty, Is.Not.Null); + Assert.That(dbName.CustomPropertyDefinition.StringCustomProperty.Default, Is.EqualTo(string.Empty)); + Assert.That(dbName.StringCustomProperty, Is.Not.Null); + Assert.That(dbName.StringCustomProperty.Value, Is.EqualTo("z_mx")); + + var busType = dbc.GlobalProperties.FirstOrDefault(x => x.CustomPropertyDefinition.Name.Equals("BusType")); + Assert.That(busType, Is.Not.Null); + Assert.That(busType.CustomPropertyDefinition.DataType, Is.EqualTo(CustomPropertyDataType.String)); + Assert.That(busType.CustomPropertyDefinition.StringCustomProperty, Is.Not.Null); + Assert.That(busType.CustomPropertyDefinition.StringCustomProperty.Default, Is.EqualTo("CAN")); + Assert.That(busType.StringCustomProperty, Is.Not.Null); + Assert.That(busType.StringCustomProperty.Value, Is.EqualTo("CAN")); + + var protocolType = dbc.GlobalProperties.FirstOrDefault(x => x.CustomPropertyDefinition.Name.Equals("ProtocolType")); + Assert.That(protocolType, Is.Not.Null); + Assert.That(protocolType.CustomPropertyDefinition.DataType, Is.EqualTo(CustomPropertyDataType.String)); + Assert.That(protocolType.CustomPropertyDefinition.StringCustomProperty, Is.Not.Null); + Assert.That(protocolType.CustomPropertyDefinition.StringCustomProperty.Default, Is.EqualTo("J1939")); + Assert.That(protocolType.StringCustomProperty, Is.Not.Null); + Assert.That(protocolType.StringCustomProperty.Value, Is.EqualTo("J1939")); + + + var targetMessage = dbc.Messages.FirstOrDefault(x => x.ID == 201391870); // Extended ID + Assert.That(targetMessage, Is.Not.Null); + Assert.That(targetMessage.CustomProperties, Has.Count.EqualTo(10)); + Assert.That(targetMessage.CustomProperties["VFrameFormat"].EnumCustomProperty.Value, Is.EqualTo("J1939PG")); + + Assert.That(targetMessage.Signals.Count, Is.EqualTo(8)); + + var floatSignal = targetMessage.Signals.FirstOrDefault(x => x.Name.Equals("S6")); + Assert.That(floatSignal, Is.Not.Null); + Assert.That(floatSignal.ValueType, Is.EqualTo(DbcValueType.IEEEFloat)); // Set with a property + + // Should check the extended multiplexing stuff once implemented + } + [Test] public void MultilineValueTableTest() { @@ -546,52 +593,5 @@ public void SingleLineMultiValueTableTest() Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Last().Key, Is.EqualTo(16384)); Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Last().Value, Is.EqualTo("ZERO_ANGLE")); } - - [Test] - public void CheckGlobalPropertiesTest() - { - var file = @"..\..\..\..\DbcFiles\ext_multiplexed.dbc"; - var dbc = Parser.ParseFromPath(file); - - Assert.That(dbc.GlobalProperties.Count(), Is.EqualTo(3)); - - var dbName = dbc.GlobalProperties.FirstOrDefault(x => x.CustomPropertyDefinition.Name.Equals("DBName")); - Assert.That(dbName, Is.Not.Null); - Assert.That(dbName.CustomPropertyDefinition.DataType, Is.EqualTo(CustomPropertyDataType.String)); - Assert.That(dbName.CustomPropertyDefinition.StringCustomProperty, Is.Not.Null); - Assert.That(dbName.CustomPropertyDefinition.StringCustomProperty.Default, Is.EqualTo(string.Empty)); - Assert.That(dbName.StringCustomProperty, Is.Not.Null); - Assert.That(dbName.StringCustomProperty.Value, Is.EqualTo("z_mx")); - - var busType = dbc.GlobalProperties.FirstOrDefault(x => x.CustomPropertyDefinition.Name.Equals("BusType")); - Assert.That(busType, Is.Not.Null); - Assert.That(busType.CustomPropertyDefinition.DataType, Is.EqualTo(CustomPropertyDataType.String)); - Assert.That(busType.CustomPropertyDefinition.StringCustomProperty, Is.Not.Null); - Assert.That(busType.CustomPropertyDefinition.StringCustomProperty.Default, Is.EqualTo("CAN")); - Assert.That(busType.StringCustomProperty, Is.Not.Null); - Assert.That(busType.StringCustomProperty.Value, Is.EqualTo("CAN")); - - var protocolType = dbc.GlobalProperties.FirstOrDefault(x => x.CustomPropertyDefinition.Name.Equals("ProtocolType")); - Assert.That(protocolType, Is.Not.Null); - Assert.That(protocolType.CustomPropertyDefinition.DataType, Is.EqualTo(CustomPropertyDataType.String)); - Assert.That(protocolType.CustomPropertyDefinition.StringCustomProperty, Is.Not.Null); - Assert.That(protocolType.CustomPropertyDefinition.StringCustomProperty.Default, Is.EqualTo("J1939")); - Assert.That(protocolType.StringCustomProperty, Is.Not.Null); - Assert.That(protocolType.StringCustomProperty.Value, Is.EqualTo("J1939")); - - - var targetMessage = dbc.Messages.FirstOrDefault(x => x.ID == 201391870); // Extended ID - Assert.That(targetMessage, Is.Not.Null); - Assert.That(targetMessage.CustomProperties, Has.Count.EqualTo(10)); - Assert.That(targetMessage.CustomProperties["VFrameFormat"].EnumCustomProperty.Value, Is.EqualTo("J1939PG")); - - Assert.That(targetMessage.Signals.Count, Is.EqualTo(8)); - - var floatSignal = targetMessage.Signals.FirstOrDefault(x => x.Name.Equals("S6")); - Assert.That(floatSignal, Is.Not.Null); - Assert.That(floatSignal.ValueType, Is.EqualTo(DbcValueType.IEEEFloat)); // Set with a property - - // Should check the extended multiplexing stuff once implemented - } } } \ No newline at end of file diff --git a/DbcParserLib.Tests/PropertiesLineParserTests.cs b/DbcParserLib.Tests/PropertiesLineParserTests.cs index 8f4f0b8..55d9b54 100644 --- a/DbcParserLib.Tests/PropertiesLineParserTests.cs +++ b/DbcParserLib.Tests/PropertiesLineParserTests.cs @@ -162,10 +162,10 @@ public void StringDefinitionCustomPropertyIsParsedTest() [Test] public void StringDefinitionCustomPropertyOnEnvironmentVariableIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.That(ParseLine(@"BA_DEF_ EV_ ""AttributeName"" STRING;", customPropertyLineParsers, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" ""DefaultString"";", customPropertyLineParsers, builder, nextLineProvider), Is.True); } @@ -173,10 +173,10 @@ public void StringDefinitionCustomPropertyOnEnvironmentVariableIsParsedTest() [Test] public void StringDefinitionCustomPropertyAsGlobalIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.That(ParseLine(@"BA_DEF_ ""AttributeName"" STRING;", customPropertyLineParsers, builder, nextLineProvider), Is.True); Assert.That(ParseLine(@"BA_DEF_DEF_ ""AttributeName"" ""DefaultString"";", customPropertyLineParsers, builder, nextLineProvider), Is.True); diff --git a/DbcParserLib/ExtensionsAndHelpers.cs b/DbcParserLib/ExtensionsAndHelpers.cs index 2d1f8fa..40efd2f 100644 --- a/DbcParserLib/ExtensionsAndHelpers.cs +++ b/DbcParserLib/ExtensionsAndHelpers.cs @@ -8,6 +8,9 @@ namespace DbcParserLib { public static class ExtensionsAndHelpers { + public const string DoubleQuotes = "\""; + public const string Space = " "; + public static bool Motorola(this Signal signal) { return signal.Msb(); @@ -161,7 +164,7 @@ public static bool ParseString(string text, out IReadOnlyDictionary private bool ParseKey(string text, int offset) { - var index = text.IndexOf(Helpers.DoubleQuotes, offset, StringComparison.InvariantCulture); + var index = text.IndexOf(ExtensionsAndHelpers.DoubleQuotes, offset, StringComparison.InvariantCulture); if(index == -1) return true; @@ -172,7 +175,7 @@ private bool ParseKey(string text, int offset) private bool ParseValue(string text, int offset, int key) { - var index = text.IndexOf(Helpers.DoubleQuotes, offset, StringComparison.InvariantCulture); + var index = text.IndexOf(ExtensionsAndHelpers.DoubleQuotes, offset, StringComparison.InvariantCulture); if (index == -1) return false; diff --git a/DbcParserLib/Helpers.cs b/DbcParserLib/Helpers.cs deleted file mode 100644 index 113c8a1..0000000 --- a/DbcParserLib/Helpers.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Text; -using System.Text.RegularExpressions; - -namespace DbcParserLib -{ - public static class Helpers - { - 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) - { - return value.Split(SpaceArray, System.StringSplitOptions.RemoveEmptyEntries); - } - - public static string ConvertToMultiLine(string[] records, int offset) - { - var sb = new StringBuilder(); - for (var i = offset; i < records.Length - 1; i += 2) - { - sb.AppendFormat("{0} {1} {2}", records[i], records[i+1], '\n'); - } - - return sb.ToString(); - } - - public static string ConcatenateTextComment(string[] strings, int startingIndex) - { - var sb = new StringBuilder(); - foreach(var s in strings ) - { - sb.AppendLine(Regex.Replace(s, @"""|;", "")); - } - var commentText = sb.ToString().Substring(startingIndex); - return commentText.Substring(0, commentText.Length - 2); - } - } - -} \ No newline at end of file diff --git a/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs b/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs index 02c007b..ece0172 100644 --- a/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs +++ b/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs @@ -32,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(Helpers.DoubleQuotes, ""), !match.Groups[2].Value.StartsWith(Helpers.DoubleQuotes)); + builder.AddCustomPropertyDefaultValue(match.Groups[1].Value, match.Groups[2].Value.Replace(ExtensionsAndHelpers.DoubleQuotes, ""), !match.Groups[2].Value.StartsWith(ExtensionsAndHelpers.DoubleQuotes)); else m_observer.PropertyDefaultSyntaxError(); return true; @@ -100,8 +100,8 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin { Values = match.Groups[11] .Value - .Replace(Helpers.DoubleQuotes, string.Empty) - .Replace(Helpers.Space, string.Empty) + .Replace(ExtensionsAndHelpers.DoubleQuotes, string.Empty) + .Replace(ExtensionsAndHelpers.Space, string.Empty) .Split(',') }; } diff --git a/DbcParserLib/Parsers/PropertiesLineParser.cs b/DbcParserLib/Parsers/PropertiesLineParser.cs index 4918602..759188f 100644 --- a/DbcParserLib/Parsers/PropertiesLineParser.cs +++ b/DbcParserLib/Parsers/PropertiesLineParser.cs @@ -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(Helpers.DoubleQuotes); - var stringValue = match.Groups[9].Value.Replace(Helpers.DoubleQuotes, string.Empty); + var isNumeric = !match.Groups[9].Value.StartsWith(ExtensionsAndHelpers.DoubleQuotes); + var stringValue = match.Groups[9].Value.Replace(ExtensionsAndHelpers.DoubleQuotes, string.Empty); if (match.Groups[2].Value == "BU_") builder.AddNodeCustomProperty(match.Groups[1].Value, match.Groups[3].Value, stringValue, isNumeric); From 2e68caebd69221908620b86fc85c60d155a55f07 Mon Sep 17 00:00:00 2001 From: Uight Date: Mon, 2 Sep 2024 20:35:06 +0200 Subject: [PATCH 16/34] Logic for multiline read changed --- DbcParserLib.Tests/ParserTests.cs | 61 +++++++++++++++++++++++++++++++ DbcParserLib/NextLineProvider.cs | 57 ++++++++++++++++++++--------- 2 files changed, 101 insertions(+), 17 deletions(-) diff --git a/DbcParserLib.Tests/ParserTests.cs b/DbcParserLib.Tests/ParserTests.cs index e6462d1..43f60be 100644 --- a/DbcParserLib.Tests/ParserTests.cs +++ b/DbcParserLib.Tests/ParserTests.cs @@ -593,5 +593,66 @@ public void SingleLineMultiValueTableTest() Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Last().Key, Is.EqualTo(16384)); Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Last().Value, Is.EqualTo("ZERO_ANGLE")); } + + [Test] + public void CommentWithEmptyLines() + { + var dbcString = @" +BO_ 1043 BLINKERS: 8 XXX + +CM_ BO_ 1043 ""Message comment first line + +third line"";"; + + + var failureObserver = new SimpleFailureObserver(); + Parser.SetParsingFailuresObserver(failureObserver); + var dbc = Parser.Parse(dbcString); + var errorList = failureObserver.GetErrorList(); + + Assert.That(errorList, Has.Count.EqualTo(0)); + Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Comment, Is.EqualTo($"Message comment first line{Environment.NewLine}{Environment.NewLine}third line")); + } + + [Test] + public void CommentEndLineWithTermination() + { + var dbcString = @" +BO_ 1043 BLINKERS: 8 XXX + +CM_ BO_ 1043 ""Message comment first line; +second line"";"; + + + var failureObserver = new SimpleFailureObserver(); + Parser.SetParsingFailuresObserver(failureObserver); + var dbc = Parser.Parse(dbcString); + var errorList = failureObserver.GetErrorList(); + + Assert.That(errorList, Has.Count.EqualTo(0)); + Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Comment, Is.EqualTo($"Message comment first line;{Environment.NewLine}second line")); + } + + [Test] + public void CommentContainingTermination() + { + var dbcString = @" +BO_ 1043 BLINKERS: 8 XXX + +CM_ BO_ 1043 ""Message; comment; first; line +second; line"";"; + + + var failureObserver = new SimpleFailureObserver(); + Parser.SetParsingFailuresObserver(failureObserver); + var dbc = Parser.Parse(dbcString); + var errorList = failureObserver.GetErrorList(); + + Assert.That(errorList, Has.Count.EqualTo(0)); + Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Comment, Is.EqualTo($"Message; comment; first; line{Environment.NewLine}second; line")); + } } } \ No newline at end of file diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index 47e2d66..1ea0603 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -78,9 +78,17 @@ public bool TryGetLine(out string line) { line = m_virtualLineMemory.Trim(); m_virtualLineMemory = null; + + if (string.IsNullOrEmpty(line)) + { + Console.WriteLine($"Line: '{line}'"); //ToDo: remove + return true; + } + line = HandleMultipleDefinitionsPerLine(line); line = HandleMultiline(line); + Console.WriteLine($"Line: '{line}'"); //ToDo: remove return true; } @@ -88,9 +96,17 @@ public bool TryGetLine(out string line) if (readLine != null) { line = readLine.Trim(); + + if (string.IsNullOrEmpty(line)) + { + Console.WriteLine($"Line: '{line}'"); //ToDo: remove + return true; + } + line = HandleMultipleDefinitionsPerLine(line); line = HandleMultiline(line); + Console.WriteLine($"Line: '{line}'"); //ToDo: remove return true; } return false; @@ -123,29 +139,36 @@ private string HandleMultipleDefinitionsPerLine(string line) private string HandleMultiline(string line) { - // This check is not verified yet. A comment is allowed to contain termination char. Why not at end of line? - // Current comment parsing would fail in this condition - /*if (line.EndsWith(lineTermination)) - { - return line; - }*/ var stringsList = new List { line }; - var nextLine = m_reader.PeekLine(); - while (nextLine != null && CheckNextLineParsing(nextLine.Trim()) == false) + var numEmptyLines = 0; + while (true) { - //Just add line for the moment; Dont assume that a following line contains the end of the first definition + an additional definition - stringsList.Add(m_reader.ReadLine().Trim()); - nextLine = m_reader.PeekLine(); - } + var checkLine = m_reader.PeekLine(); - //Remove trailing empty lines but never the first line; return empty string initial line was empty; - for (int i = stringsList.Count - 1; i > 0; i--) - { - if (string.IsNullOrEmpty(stringsList[i])) + if (checkLine is null) + { + break; + } + + if (string.IsNullOrEmpty(checkLine.Trim())) { - stringsList.RemoveAt(i); + numEmptyLines++; + continue; } + + if (CheckNextLineParsing(checkLine.Trim()) == false) + { + for (int i = 0; i < numEmptyLines; i++) + { + stringsList.Add(m_reader.ReadLine().Trim()); + } + numEmptyLines = 0; + stringsList.Add(m_reader.ReadLine().Trim()); + continue; + } + + break; } var stringBuilder = new StringBuilder(); From c687070e7e528a780b26095a12611891ae614ad2 Mon Sep 17 00:00:00 2001 From: Uight Date: Mon, 2 Sep 2024 20:40:29 +0200 Subject: [PATCH 17/34] Move virtualLine memory to textreader component --- DbcParserLib/NextLineProvider.cs | 26 ++++---------------------- DbcParserLib/PeekableTextReader.cs | 12 +++++++++++- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index 1ea0603..0f04804 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -10,9 +10,8 @@ namespace DbcParserLib public class NextLineProvider : INextLineProvider { private PeekableTextReader m_reader; - private string m_virtualLineMemory; - private string lineTermination = ";"; + private const string lineTermination = ";"; private readonly string[] keywords = new[] { @@ -74,23 +73,6 @@ public NextLineProvider(TextReader reader, IParseFailureObserver observer) public bool TryGetLine(out string line) { line = null; - if (m_virtualLineMemory != null) - { - line = m_virtualLineMemory.Trim(); - m_virtualLineMemory = null; - - if (string.IsNullOrEmpty(line)) - { - Console.WriteLine($"Line: '{line}'"); //ToDo: remove - return true; - } - - line = HandleMultipleDefinitionsPerLine(line); - line = HandleMultiline(line); - - Console.WriteLine($"Line: '{line}'"); //ToDo: remove - return true; - } var readLine = m_reader.ReadLine(); if (readLine != null) @@ -123,11 +105,11 @@ private string HandleMultipleDefinitionsPerLine(string line) return line; } - var partAfterTermination = line.Substring(definitionTerminationLocation + 2, line.Length - 2 - definitionTerminationLocation).Trim(); + var partAfterTermination = line.Substring(definitionTerminationLocation + 2, line.Length - 2 - definitionTerminationLocation); - if (CheckNextLineParsing(partAfterTermination)) // check if the remaining line is a new definition. otherwise assume your reading a comment + if (CheckNextLineParsing(partAfterTermination.TrimStart())) // check if the remaining line is a new definition. otherwise assume your reading a comment { - m_virtualLineMemory = partAfterTermination; + m_reader.AddVirtualLine(partAfterTermination); return line.Substring(0, definitionTerminationLocation + 1); } diff --git a/DbcParserLib/PeekableTextReader.cs b/DbcParserLib/PeekableTextReader.cs index 583972d..3e32ae0 100644 --- a/DbcParserLib/PeekableTextReader.cs +++ b/DbcParserLib/PeekableTextReader.cs @@ -9,6 +9,7 @@ internal class PeekableTextReader private IParseFailureObserver m_observer; private readonly TextReader m_underlying; private readonly Queue m_bufferedLines; + private string m_virtualLineMemory; public PeekableTextReader(TextReader underlying, IParseFailureObserver observer) { @@ -25,10 +26,19 @@ public string PeekLine() m_bufferedLines.Enqueue(line); return line; } - + public void AddVirtualLine(string line) + { + m_virtualLineMemory = line; + } public string ReadLine() { + if (m_virtualLineMemory != null) + { + var temp = m_virtualLineMemory; + m_virtualLineMemory = null; + return temp; + } if (m_bufferedLines.Count > 0) return m_bufferedLines.Dequeue(); m_observer.CurrentLine++; From a6c1946190fda3b5f5cb45c8d9c2a58028a9c544 Mon Sep 17 00:00:00 2001 From: Uight Date: Mon, 2 Sep 2024 20:47:01 +0200 Subject: [PATCH 18/34] Could autocorrect missing ";" at line end --- DbcParserLib.Tests/ParserTests.cs | 20 ++++++++++++++++++++ DbcParserLib/NextLineProvider.cs | 5 +++++ 2 files changed, 25 insertions(+) diff --git a/DbcParserLib.Tests/ParserTests.cs b/DbcParserLib.Tests/ParserTests.cs index 43f60be..33dfb99 100644 --- a/DbcParserLib.Tests/ParserTests.cs +++ b/DbcParserLib.Tests/ParserTests.cs @@ -654,5 +654,25 @@ public void CommentContainingTermination() Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); Assert.That(dbc.Messages.First().Comment, Is.EqualTo($"Message; comment; first; line{Environment.NewLine}second; line")); } + + [Test] + public void CommentMissingTerminationGetsCorrected() + { + var dbcString = @" +BO_ 1043 BLINKERS: 8 XXX + +CM_ BO_ 1043 ""Message comment first line +second line"""; + + + var failureObserver = new SimpleFailureObserver(); + Parser.SetParsingFailuresObserver(failureObserver); + var dbc = Parser.Parse(dbcString); + var errorList = failureObserver.GetErrorList(); + + Assert.That(errorList, Has.Count.EqualTo(0)); + Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Comment, Is.EqualTo($"Message comment first line{Environment.NewLine}second line")); + } } } \ No newline at end of file diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index 0f04804..f01b258 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -88,6 +88,11 @@ public bool TryGetLine(out string line) line = HandleMultipleDefinitionsPerLine(line); line = HandleMultiline(line); + if (line.EndsWith(lineTermination) == false) //correct missing terminations + { + line = line + lineTermination; + } + Console.WriteLine($"Line: '{line}'"); //ToDo: remove return true; } From f46995f61299606db2ce369b18b3413447dac3c2 Mon Sep 17 00:00:00 2001 From: Uight Date: Mon, 2 Sep 2024 21:27:00 +0200 Subject: [PATCH 19/34] fox wrong observer lines and support multidefinition right after multiline --- DbcParserLib.Tests/ParserTests.cs | 30 ++++++++++++++++++++++++++++++ DbcParserLib/NextLineProvider.cs | 15 +++++++++++---- DbcParserLib/PeekableTextReader.cs | 2 +- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/DbcParserLib.Tests/ParserTests.cs b/DbcParserLib.Tests/ParserTests.cs index 33dfb99..8492427 100644 --- a/DbcParserLib.Tests/ParserTests.cs +++ b/DbcParserLib.Tests/ParserTests.cs @@ -674,5 +674,35 @@ CM_ BO_ 1043 ""Message comment first line Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); Assert.That(dbc.Messages.First().Comment, Is.EqualTo($"Message comment first line{Environment.NewLine}second line")); } + + [Test] + public void HandleMultiDefinitionLineAfterMultiLine() + { + var dbcString = @" +BO_ 1160 DAS_steeringControl: 4 NEO + SG_ DAS_steeringControlType : 23|2@0+ (1,0) [0|0] """" EPAS + SG_ DAS_steeringAngleRequest : 0|16@0+ (1,0) [0|0] """" EPAS + +CM_ BO_ 1160 ""Message comment first line +second line""; VAL_ 1160 DAS_steeringControlType 1 ""ANGLE_CONTROL"" 3 ""DISABLED"" 0 ""NONE"" 2 ""RESERVED""; VAL_ 1160 DAS_steeringAngleRequest 16384 ""ZERO_ANGLE""; "; + + + var failureObserver = new SimpleFailureObserver(); + Parser.SetParsingFailuresObserver(failureObserver); + var dbc = Parser.Parse(dbcString); + var errorList = failureObserver.GetErrorList(); + + Assert.That(errorList, Has.Count.EqualTo(0)); + Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Comment, Is.EqualTo($"Message comment first line{Environment.NewLine}second line")); + + Assert.That(dbc.Messages.First().Signals.Count(), Is.EqualTo(2)); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Count, Is.EqualTo(4)); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Last().Key, Is.EqualTo(2)); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Last().Value, Is.EqualTo("RESERVED")); + Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Count, Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Last().Key, Is.EqualTo(16384)); + Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Last().Value, Is.EqualTo("ZERO_ANGLE")); + } } } \ No newline at end of file diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index f01b258..9a5ada5 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -88,7 +88,8 @@ public bool TryGetLine(out string line) line = HandleMultipleDefinitionsPerLine(line); line = HandleMultiline(line); - if (line.EndsWith(lineTermination) == false) //correct missing terminations + var test = line; + if (line.EndsWith(lineTermination) == false && keywords.Any(prefix => test.Equals(prefix)) == false) //correct missing terminations { line = line + lineTermination; } @@ -138,20 +139,26 @@ private string HandleMultiline(string line) break; } - if (string.IsNullOrEmpty(checkLine.Trim())) + checkLine = checkLine.Trim(); + + if (string.IsNullOrEmpty(checkLine)) { numEmptyLines++; continue; } - if (CheckNextLineParsing(checkLine.Trim()) == false) + if (CheckNextLineParsing(checkLine) == false) { for (int i = 0; i < numEmptyLines; i++) { stringsList.Add(m_reader.ReadLine().Trim()); } numEmptyLines = 0; - stringsList.Add(m_reader.ReadLine().Trim()); + + var lineToAdd = m_reader.ReadLine().Trim(); + lineToAdd = HandleMultipleDefinitionsPerLine(lineToAdd); + + stringsList.Add(lineToAdd); continue; } diff --git a/DbcParserLib/PeekableTextReader.cs b/DbcParserLib/PeekableTextReader.cs index 3e32ae0..3efd705 100644 --- a/DbcParserLib/PeekableTextReader.cs +++ b/DbcParserLib/PeekableTextReader.cs @@ -39,9 +39,9 @@ public string ReadLine() m_virtualLineMemory = null; return temp; } + m_observer.CurrentLine++; if (m_bufferedLines.Count > 0) return m_bufferedLines.Dequeue(); - m_observer.CurrentLine++; return m_underlying.ReadLine(); } } From d2e91dd63a5fe6eeb7584b8414da0e19c287baa5 Mon Sep 17 00:00:00 2001 From: Uight Date: Mon, 2 Sep 2024 21:34:05 +0200 Subject: [PATCH 20/34] Add test for last case --- DbcParserLib.Tests/ParserTests.cs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/DbcParserLib.Tests/ParserTests.cs b/DbcParserLib.Tests/ParserTests.cs index 8492427..3f4011f 100644 --- a/DbcParserLib.Tests/ParserTests.cs +++ b/DbcParserLib.Tests/ParserTests.cs @@ -704,5 +704,35 @@ CM_ BO_ 1160 ""Message comment first line Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Last().Key, Is.EqualTo(16384)); Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Last().Value, Is.EqualTo("ZERO_ANGLE")); } + + [Test] + public void CommentWithTerminationAndMultiDefinitionInLine() + { + var dbcString = @" +BO_ 1160 DAS_steeringControl: 4 NEO + SG_ DAS_steeringControlType : 23|2@0+ (1,0) [0|0] """" EPAS + SG_ DAS_steeringAngleRequest : 0|16@0+ (1,0) [0|0] """" EPAS + +CM_ BO_ 1160 ""This is a very fine comment; Right?""; VAL_ 1160 DAS_steeringControlType 1 ""ANGLE_CONTROL"" 3 ""DISABLED"" 0 ""NONE"" 2 ""RESERVED""; +VAL_ 1160 DAS_steeringAngleRequest 16384 ""ZERO_ANGLE""; "; + + + var failureObserver = new SimpleFailureObserver(); + Parser.SetParsingFailuresObserver(failureObserver); + var dbc = Parser.Parse(dbcString); + var errorList = failureObserver.GetErrorList(); + + Assert.That(errorList, Has.Count.EqualTo(0)); + Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Comment, Is.EqualTo("This is a very fine comment; Right?")); + + Assert.That(dbc.Messages.First().Signals.Count(), Is.EqualTo(2)); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Count, Is.EqualTo(4)); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Last().Key, Is.EqualTo(2)); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Last().Value, Is.EqualTo("RESERVED")); + Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Count, Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Last().Key, Is.EqualTo(16384)); + Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Last().Value, Is.EqualTo("ZERO_ANGLE")); + } } } \ No newline at end of file From 26b31192263970f284cd8db0844d5c70f4338fc1 Mon Sep 17 00:00:00 2001 From: Uight Date: Mon, 2 Sep 2024 21:58:30 +0200 Subject: [PATCH 21/34] ass multidefinitionsupport even after ";" --- DbcParserLib.Tests/ParserTests.cs | 2 +- DbcParserLib/NextLineProvider.cs | 15 +++++++-------- DbcParserLib/PeekableTextReader.cs | 3 ++- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/DbcParserLib.Tests/ParserTests.cs b/DbcParserLib.Tests/ParserTests.cs index 3f4011f..38253d0 100644 --- a/DbcParserLib.Tests/ParserTests.cs +++ b/DbcParserLib.Tests/ParserTests.cs @@ -712,7 +712,7 @@ public void CommentWithTerminationAndMultiDefinitionInLine() BO_ 1160 DAS_steeringControl: 4 NEO SG_ DAS_steeringControlType : 23|2@0+ (1,0) [0|0] """" EPAS SG_ DAS_steeringAngleRequest : 0|16@0+ (1,0) [0|0] """" EPAS - + CM_ BO_ 1160 ""This is a very fine comment; Right?""; VAL_ 1160 DAS_steeringControlType 1 ""ANGLE_CONTROL"" 3 ""DISABLED"" 0 ""NONE"" 2 ""RESERVED""; VAL_ 1160 DAS_steeringAngleRequest 16384 ""ZERO_ANGLE""; "; diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index 9a5ada5..b27b4db 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -81,7 +81,6 @@ public bool TryGetLine(out string line) if (string.IsNullOrEmpty(line)) { - Console.WriteLine($"Line: '{line}'"); //ToDo: remove return true; } @@ -94,7 +93,6 @@ public bool TryGetLine(out string line) line = line + lineTermination; } - Console.WriteLine($"Line: '{line}'"); //ToDo: remove return true; } return false; @@ -104,7 +102,8 @@ private string HandleMultipleDefinitionsPerLine(string line) { int definitionTerminationLocation = line.IndexOf(lineTermination, StringComparison.Ordinal); - if (definitionTerminationLocation >= 0) + var lastTerminationLocation = -1; + while (definitionTerminationLocation > lastTerminationLocation) { if (definitionTerminationLocation + 1 == line.Length) { @@ -112,15 +111,15 @@ private string HandleMultipleDefinitionsPerLine(string line) } var partAfterTermination = line.Substring(definitionTerminationLocation + 2, line.Length - 2 - definitionTerminationLocation); - - if (CheckNextLineParsing(partAfterTermination.TrimStart())) // check if the remaining line is a new definition. otherwise assume your reading a comment + + if (CheckNextLineParsing(partAfterTermination.TrimStart())) { - m_reader.AddVirtualLine(partAfterTermination); + m_reader.SetVirtualLine(partAfterTermination); return line.Substring(0, definitionTerminationLocation + 1); } - // Assuming the line is a comment now => dont check for further occurences of the termination for now as they most likely will also just be comment or end of line - // Would be a very special case were a comment is followed by a definition in the very same line. Could be handled but not for now + lastTerminationLocation = definitionTerminationLocation; + definitionTerminationLocation = definitionTerminationLocation + 1 + partAfterTermination.IndexOf(lineTermination, StringComparison.Ordinal); } return line; } diff --git a/DbcParserLib/PeekableTextReader.cs b/DbcParserLib/PeekableTextReader.cs index 3efd705..5ec3920 100644 --- a/DbcParserLib/PeekableTextReader.cs +++ b/DbcParserLib/PeekableTextReader.cs @@ -26,7 +26,8 @@ public string PeekLine() m_bufferedLines.Enqueue(line); return line; } - public void AddVirtualLine(string line) + + public void SetVirtualLine(string line) { m_virtualLineMemory = line; } From d4440e76d6f31ee1e0bbdd55c4e51fd70626a613 Mon Sep 17 00:00:00 2001 From: Uight Date: Tue, 3 Sep 2024 18:30:56 +0200 Subject: [PATCH 22/34] Merge conflixts resolved --- DbcParserLib/ExtensionsAndHelpers.cs | 5 ++--- DbcParserLib/Parsers/MessageLineParser.cs | 6 ++++-- DbcParserLib/Parsers/NodeLineParser.cs | 6 ++++-- DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs | 8 ++++---- DbcParserLib/Parsers/PropertiesLineParser.cs | 6 +++--- DbcParserLib/Parsers/SignalLineParser.cs | 9 +++++---- 6 files changed, 22 insertions(+), 18 deletions(-) diff --git a/DbcParserLib/ExtensionsAndHelpers.cs b/DbcParserLib/ExtensionsAndHelpers.cs index 40efd2f..5da49c5 100644 --- a/DbcParserLib/ExtensionsAndHelpers.cs +++ b/DbcParserLib/ExtensionsAndHelpers.cs @@ -10,6 +10,8 @@ public static class ExtensionsAndHelpers { public const string DoubleQuotes = "\""; public const string Space = " "; + public static readonly string[] CommaSpaceSeparator = { " ", "," }; + private static readonly string[] SpaceArray = { " " }; public static bool Motorola(this Signal signal) { @@ -120,9 +122,6 @@ internal static bool InitialValue(this Signal signal, out double initialValue) return false; } - private static readonly string[] SpaceArray = { " " }; - public static readonly string[] CommaSpaceSeparator = { " ", "," }; - public static string[] SplitBySpace(this string value) { return value.Split(SpaceArray, System.StringSplitOptions.RemoveEmptyEntries); diff --git a/DbcParserLib/Parsers/MessageLineParser.cs b/DbcParserLib/Parsers/MessageLineParser.cs index b4342a0..b14d910 100644 --- a/DbcParserLib/Parsers/MessageLineParser.cs +++ b/DbcParserLib/Parsers/MessageLineParser.cs @@ -24,10 +24,12 @@ public MessageLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - if (line.Trim().StartsWith(MessageLineStarter) == false) + var cleanLine = line.ReplaceNewlinesWithSpace().Trim(); + + if (cleanLine.StartsWith(MessageLineStarter) == false) return false; - var match = Regex.Match(line, m_messageRegex); + var match = Regex.Match(cleanLine, m_messageRegex); if (match.Success) { var msg = new Message() diff --git a/DbcParserLib/Parsers/NodeLineParser.cs b/DbcParserLib/Parsers/NodeLineParser.cs index a0ea2ec..f7c78e5 100644 --- a/DbcParserLib/Parsers/NodeLineParser.cs +++ b/DbcParserLib/Parsers/NodeLineParser.cs @@ -22,11 +22,13 @@ public NodeLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - if (line.TrimStart().StartsWith(NodeLineStarter) == false) + var cleanLine = line.ReplaceNewlinesWithSpace().Trim(); + + if (cleanLine.StartsWith(NodeLineStarter) == false) return false; // Empty node list - if (line.Trim().Equals(NodeLineStarter)) + if (cleanLine.Equals(NodeLineStarter)) return true; var match = m_regex.Match(line); diff --git a/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs b/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs index a36a989..a347da9 100644 --- a/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs +++ b/DbcParserLib/Parsers/PropertiesDefinitionLineParser.cs @@ -38,7 +38,7 @@ public PropertiesDefinitionLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim(' '); + var cleanLine = line.ReplaceNewlinesWithSpace().Trim(); if (cleanLine.StartsWith(PropertiesDefinitionLineStarter) == false && cleanLine.StartsWith(PropertiesDefinitionDefaultLineStarter) == false) @@ -48,7 +48,7 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin { var match = Regex.Match(cleanLine, m_propertyDefinitionDefaultParsingRegex); if (match.Success) - builder.AddCustomPropertyDefaultValue(match.Groups[AttributeNameGroup].Value, match.Groups[AttributeValueGroup].Value.Replace(Helpers.DoubleQuotes, ""), !match.Groups[AttributeValueGroup].Value.StartsWith(Helpers.DoubleQuotes)); + builder.AddCustomPropertyDefaultValue(match.Groups[AttributeNameGroup].Value, match.Groups[AttributeValueGroup].Value.Replace(ExtensionsAndHelpers.DoubleQuotes, ""), !match.Groups[AttributeValueGroup].Value.StartsWith(ExtensionsAndHelpers.DoubleQuotes)); else m_observer.PropertyDefaultSyntaxError(); return true; @@ -116,8 +116,8 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin { Values = match.Groups[EnumValueGroup] .Value - .Replace(Helpers.DoubleQuotes, string.Empty) - .Replace(Helpers.Space, string.Empty) + .Replace(ExtensionsAndHelpers.DoubleQuotes, string.Empty) + .Replace(ExtensionsAndHelpers.Space, string.Empty) .Split(',') }; } diff --git a/DbcParserLib/Parsers/PropertiesLineParser.cs b/DbcParserLib/Parsers/PropertiesLineParser.cs index 031f0b9..85d16f2 100644 --- a/DbcParserLib/Parsers/PropertiesLineParser.cs +++ b/DbcParserLib/Parsers/PropertiesLineParser.cs @@ -29,7 +29,7 @@ public PropertiesLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - var cleanLine = line.Trim(' '); + var cleanLine = line.ReplaceNewlinesWithSpace().Trim(); if (cleanLine.StartsWith(PropertiesLineStarter) == false) return false; @@ -37,8 +37,8 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin var match = Regex.Match(cleanLine, m_propertyParsingRegex); if (match.Success) { - var isNumeric = !match.Groups[AttributeValueGroup].Value.StartsWith(Helpers.DoubleQuotes); - var stringValue = match.Groups[AttributeValueGroup].Value.Replace(Helpers.DoubleQuotes, string.Empty); + var isNumeric = !match.Groups[AttributeValueGroup].Value.StartsWith(ExtensionsAndHelpers.DoubleQuotes); + var stringValue = match.Groups[AttributeValueGroup].Value.Replace(ExtensionsAndHelpers.DoubleQuotes, string.Empty); if (match.Groups[IsNodeEnvGroup].Value.Equals(string.Empty) == false) builder.AddNodeCustomProperty(match.Groups[AttributeNameGroup].Value, match.Groups[NodeEnvNameGroup].Value, stringValue, isNumeric); diff --git a/DbcParserLib/Parsers/SignalLineParser.cs b/DbcParserLib/Parsers/SignalLineParser.cs index c0be5e4..11bc915 100644 --- a/DbcParserLib/Parsers/SignalLineParser.cs +++ b/DbcParserLib/Parsers/SignalLineParser.cs @@ -22,7 +22,6 @@ internal class SignalLineParser : ILineParser private const string ReceiverGroup = "Receiver"; private const string SignalLineStarter = "SG_ "; private const string SignedSymbol = "-"; - private static readonly string[] CommaSpaceSeparator = { Helpers.Space, Helpers.Comma }; private readonly string m_signalRegex = $@"\s*SG_\s+(?<{NameGroup}>[\w]+)\s*(?<{MultiplexerGroup}>[Mm\d]*)\s*:\s*(?<{StartBirGroup}>\d+)\|(?<{SizeGroup}>\d+)@(?<{ByteOrderGroup}>[01])" + $@"(?<{ValueTypeGroup}>[+-])\s+\((?<{FactorGroup}>[\d\+\-eE.]+),(?<{OffsetGroup}>[\d\+\-eE.]+)\)\s+\[(?<{MinGroup}>[\d\+\-eE.]+)\|(?<{MaxGroup}>[\d\+\-eE.]+)\]" + @@ -37,10 +36,12 @@ public SignalLineParser(IParseFailureObserver observer) public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider) { - if (line.TrimStart().StartsWith(SignalLineStarter) == false) + var cleanLine = line.ReplaceNewlinesWithSpace().Trim(); + + if (cleanLine.StartsWith(SignalLineStarter) == false) return false; - var match = Regex.Match(line, m_signalRegex); + var match = Regex.Match(cleanLine, m_signalRegex); if (match.Success) { var factorStr = match.Groups[FactorGroup].Value; @@ -58,7 +59,7 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin Minimum = double.Parse(match.Groups[MinGroup].Value, CultureInfo.InvariantCulture), Maximum = double.Parse(match.Groups[MaxGroup].Value, CultureInfo.InvariantCulture), Unit = match.Groups[UnitGroup].Value, - Receiver = match.Groups[ReceiverGroup].Value.Split(CommaSpaceSeparator, StringSplitOptions.RemoveEmptyEntries) // can be multiple receivers splitted by "," + Receiver = match.Groups[ReceiverGroup].Value.Split(ExtensionsAndHelpers.CommaSpaceSeparator, StringSplitOptions.RemoveEmptyEntries) // can be multiple receivers splitted by "," }; builder.AddSignal(sig); From bfcce6a9d7af7f924049f8930693db4078f1b0a7 Mon Sep 17 00:00:00 2001 From: Uight Date: Tue, 3 Sep 2024 19:24:21 +0200 Subject: [PATCH 23/34] small cleanup --- DbcParserLib/NextLineProvider.cs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index b27b4db..a2bcd79 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -13,7 +13,7 @@ public class NextLineProvider : INextLineProvider private const string lineTermination = ";"; - private readonly string[] keywords = new[] + private readonly string[] keywords = { "VERSION", "FILTER", @@ -47,9 +47,9 @@ public class NextLineProvider : INextLineProvider "SIG_VALTYPE_", "SIG_TYPE_REF_", - "EV_", "EV_DATA_", "ENVVAR_DATA_", + "EV_", "BO_TX_BU_", "BO_", @@ -112,7 +112,7 @@ private string HandleMultipleDefinitionsPerLine(string line) var partAfterTermination = line.Substring(definitionTerminationLocation + 2, line.Length - 2 - definitionTerminationLocation); - if (CheckNextLineParsing(partAfterTermination.TrimStart())) + if (CheckLineIsDefinition(partAfterTermination.TrimStart())) { m_reader.SetVirtualLine(partAfterTermination); return line.Substring(0, definitionTerminationLocation + 1); @@ -146,7 +146,7 @@ private string HandleMultiline(string line) continue; } - if (CheckNextLineParsing(checkLine) == false) + if (CheckLineIsDefinition(checkLine) == false) { for (int i = 0; i < numEmptyLines; i++) { @@ -174,10 +174,9 @@ private string HandleMultiline(string line) return stringBuilder.ToString(); } - private bool CheckNextLineParsing(string nextLine) + private bool CheckLineIsDefinition(string line) { - nextLine = nextLine.TrimStart(); - return keywords.Any(prefix => nextLine.StartsWith(prefix)); + return keywords.Any(keywordPrefix => line.StartsWith(keywordPrefix)); } } } From 02a26419a471ccf3975d41279c109d288e9d904b Mon Sep 17 00:00:00 2001 From: Uight Date: Tue, 3 Sep 2024 19:26:31 +0200 Subject: [PATCH 24/34] simplyfy multiline handling --- DbcParserLib/NextLineProvider.cs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index a2bcd79..6c9f018 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -126,7 +126,8 @@ private string HandleMultipleDefinitionsPerLine(string line) private string HandleMultiline(string line) { - var stringsList = new List { line }; + var stringBuilder = new StringBuilder(); + stringBuilder.AppendLine(line); var numEmptyLines = 0; while (true) @@ -150,27 +151,20 @@ private string HandleMultiline(string line) { for (int i = 0; i < numEmptyLines; i++) { - stringsList.Add(m_reader.ReadLine().Trim()); + stringBuilder.AppendLine(m_reader.ReadLine().Trim()); } numEmptyLines = 0; var lineToAdd = m_reader.ReadLine().Trim(); lineToAdd = HandleMultipleDefinitionsPerLine(lineToAdd); - stringsList.Add(lineToAdd); + stringBuilder.AppendLine(lineToAdd); continue; } break; } - var stringBuilder = new StringBuilder(); - for (int i = 0; i < stringsList.Count - 1; i++) - { - stringBuilder.AppendLine(stringsList[i]); - } - stringBuilder.Append(stringsList.Last()); - return stringBuilder.ToString(); } From e153eeff7cf2e31cd577982791f130a325678f1e Mon Sep 17 00:00:00 2001 From: Uight Date: Tue, 3 Sep 2024 19:35:55 +0200 Subject: [PATCH 25/34] missed a unused using --- DbcParserLib/NextLineProvider.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index 6c9f018..2f7b19b 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -1,6 +1,5 @@ using DbcParserLib.Observers; using System; -using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; From 9adafcf826227c0487aa906232d11bebf2197ec2 Mon Sep 17 00:00:00 2001 From: Uight Date: Tue, 3 Sep 2024 19:46:15 +0200 Subject: [PATCH 26/34] More cleanup --- DbcParserLib/NextLineProvider.cs | 32 ++++++++++++----------- DbcParserLib/Parsers/CommentLineParser.cs | 3 --- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index 2f7b19b..85075c0 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -74,27 +74,29 @@ public bool TryGetLine(out string line) line = null; var readLine = m_reader.ReadLine(); - if (readLine != null) + + if (readLine is null) { - line = readLine.Trim(); + return false; + } - if (string.IsNullOrEmpty(line)) - { - return true; - } + line = readLine.Trim(); - line = HandleMultipleDefinitionsPerLine(line); - line = HandleMultiline(line); + if (string.IsNullOrEmpty(line)) + { + return true; + } - var test = line; - if (line.EndsWith(lineTermination) == false && keywords.Any(prefix => test.Equals(prefix)) == false) //correct missing terminations - { - line = line + lineTermination; - } + line = HandleMultipleDefinitionsPerLine(line); + line = HandleMultiline(line); - return true; + var test = line; + if (line.EndsWith(lineTermination) == false && keywords.Any(prefix => test.Equals(prefix)) == false) //correct missing terminations + { + line = line + lineTermination; } - return false; + + return true; } private string HandleMultipleDefinitionsPerLine(string line) diff --git a/DbcParserLib/Parsers/CommentLineParser.cs b/DbcParserLib/Parsers/CommentLineParser.cs index 2ec0bf9..c580dab 100644 --- a/DbcParserLib/Parsers/CommentLineParser.cs +++ b/DbcParserLib/Parsers/CommentLineParser.cs @@ -33,9 +33,6 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin if (cleanLine.StartsWith(CommentLineStarter) == false) return false; - if (!cleanLine.EndsWith(";")) - cleanLine = GetNextLines(cleanLine, m_observer, nextLineProvider); - if (cleanLine.StartsWith("CM_ SG_")) { SetSignalComment(cleanLine, m_observer, builder, nextLineProvider); From 62cf4de12a901ee919c7f753e7cb3045df7173e2 Mon Sep 17 00:00:00 2001 From: Uight Date: Thu, 5 Sep 2024 17:53:19 +0200 Subject: [PATCH 27/34] merge conflicts resolved --- .../PropertiesLineParserTests.cs | 67 ++++++++++--------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/DbcParserLib.Tests/PropertiesLineParserTests.cs b/DbcParserLib.Tests/PropertiesLineParserTests.cs index 742dcee..ee93c17 100644 --- a/DbcParserLib.Tests/PropertiesLineParserTests.cs +++ b/DbcParserLib.Tests/PropertiesLineParserTests.cs @@ -12,6 +12,7 @@ namespace DbcParserLib.Tests public class PropertiesLineParserTests { private MockRepository m_repository; + private static readonly SilentFailureObserver m_silentFailureObserver = new SilentFailureObserver(); [SetUp] public void Setup() @@ -27,7 +28,7 @@ public void Teardown() private static List CreateParser() { - var observer = new SilentFailureObserver(); + var observer = m_silentFailureObserver; return new List() { new PropertiesLineParser(observer), new PropertiesDefinitionLineParser(observer) @@ -47,10 +48,10 @@ private static bool ParseLine(string line, List lineParser, IDbcBui [Test] public void IntDefinitionCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.Multiple(() => { Assert.That(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" INT 5 10;", customPropertyLineParsers, builder, nextLineProvider), Is.True); @@ -61,12 +62,12 @@ public void IntDefinitionCustomPropertyIsParsedTest() [Test] public void IntDefinitionCustomPropertyNoBoundariesIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var message = new Message { ID = 2394947585 }; builder.AddMessage(message); var msgCycleTimeLineParser = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.Multiple(() => { Assert.That(ParseLine(@"BA_DEF_ BO_ ""AttributeName"" INT 0 0;", msgCycleTimeLineParser, builder, nextLineProvider), Is.True); @@ -85,10 +86,10 @@ public void IntDefinitionCustomPropertyNoBoundariesIsParsedTest() [Test] public void HexDefinitionCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.Multiple(() => { Assert.That(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" HEX 5 10;", customPropertyLineParsers, builder, nextLineProvider), Is.True); @@ -99,12 +100,12 @@ public void HexDefinitionCustomPropertyIsParsedTest() [Test] public void HexDefinitionCustomPropertyNoBoundariesIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var message = new Message { ID = 2394947585 }; builder.AddMessage(message); var msgCycleTimeLineParser = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.Multiple(() => { Assert.That(ParseLine(@"BA_DEF_ BO_ ""AttributeName"" HEX 0 0;", msgCycleTimeLineParser, builder, nextLineProvider), Is.True); @@ -122,10 +123,10 @@ public void HexDefinitionCustomPropertyNoBoundariesIsParsedTest() [Test] public void FloatDefinitionCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.Multiple(() => { Assert.That(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" FLOAT 5 10.5;", customPropertyLineParsers, builder, nextLineProvider), Is.True); @@ -136,12 +137,12 @@ public void FloatDefinitionCustomPropertyIsParsedTest() [Test] public void FloatDefinitionCustomPropertyNoBoundariesIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var message = new Message { ID = 2394947585 }; builder.AddMessage(message); var msgCycleTimeLineParser = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.Multiple(() => { Assert.That(ParseLine(@"BA_DEF_ BO_ ""AttributeName"" FLOAT 0 0;", msgCycleTimeLineParser, builder, nextLineProvider), Is.True); @@ -181,10 +182,10 @@ public void ScientificNotationDefinitionCustomPropertyIsParsedTest() [Test] public void StringDefinitionCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.Multiple(() => { Assert.That(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" STRING;", customPropertyLineParsers, builder, nextLineProvider), Is.True); @@ -195,10 +196,10 @@ public void StringDefinitionCustomPropertyIsParsedTest() [Test] public void StringDefinitionCustomPropertyOnEnvironmentVariableIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.Multiple(() => { Assert.That(ParseLine(@"BA_DEF_ EV_ ""AttributeName"" STRING;", customPropertyLineParsers, builder, nextLineProvider), Is.True); @@ -209,10 +210,10 @@ public void StringDefinitionCustomPropertyOnEnvironmentVariableIsParsedTest() [Test] public void StringDefinitionCustomPropertyAsGlobalIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.Multiple(() => { Assert.That(ParseLine(@"BA_DEF_ ""AttributeName"" STRING;", customPropertyLineParsers, builder, nextLineProvider), Is.True); @@ -357,12 +358,12 @@ public void EnumDefinitionCustomPropertyWithWhiteSpaceBetweenEntriesIsParsedTest [Test] public void MsgCycleTimePropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var message = new Message { ID = 2394947585 }; builder.AddMessage(message); var msgCycleTimeLineParser = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.Multiple(() => { Assert.That(ParseLine(@"BA_DEF_ BO_ ""GenMsgCycleTime"" INT 0 0;", msgCycleTimeLineParser, builder, nextLineProvider), Is.True); @@ -381,14 +382,14 @@ public void MsgCycleTimePropertyIsParsedTest() [Test] public void SigInitialValueIntegerPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var message = new Message { ID = 2394947585 }; builder.AddMessage(message); var signal = new Signal { Name = "sig_name" }; builder.AddSignal(signal); var sigInitialValueLineParser = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.Multiple(() => { Assert.That(ParseLine(@"BA_DEF_ SG_ ""GenSigStartValue"" INT 0 200;", sigInitialValueLineParser, builder, nextLineProvider), Is.True); @@ -408,14 +409,14 @@ public void SigInitialValueIntegerPropertyIsParsedTest() [Test] public void SigInitialValueHexPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var message = new Message { ID = 2394947585 }; builder.AddMessage(message); var signal = new Signal { Name = "sig_name" }; builder.AddSignal(signal); var sigInitialValueLineParser = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.Multiple(() => { Assert.That(ParseLine(@"BA_DEF_ SG_ ""GenSigStartValue"" HEX 0 200;", sigInitialValueLineParser, builder, nextLineProvider), Is.True); @@ -435,12 +436,12 @@ public void SigInitialValueHexPropertyIsParsedTest() [Test] public void NodeCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var node = new Node { Name = "Node1" }; builder.AddNode(node); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.Multiple(() => { Assert.That(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" HEX 0 200;", customPropertyLineParsers, builder, nextLineProvider), Is.True); @@ -459,13 +460,13 @@ public void NodeCustomPropertyIsParsedTest() [Test] public void NodeScientificNotationCustomPropertyIsParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var node = new Node { Name = "Node1" }; builder.AddNode(node); var dbc = builder.Build(); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.Multiple(() => { Assert.That(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" FLOAT 0 10;", customPropertyLineParsers, builder, nextLineProvider), Is.True); @@ -478,12 +479,12 @@ public void NodeScientificNotationCustomPropertyIsParsedTest() [Test] public void NodeMultipleCustomPropertyAreParsedTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var node = new Node { Name = "Node1" }; builder.AddNode(node); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.Multiple(() => { Assert.That(ParseLine(@"BA_DEF_ BU_ ""AttributeName1"" INT 0 200;", customPropertyLineParsers, builder, nextLineProvider), Is.True); @@ -503,14 +504,14 @@ public void NodeMultipleCustomPropertyAreParsedTest() [Test] public void CustomPropertyIsAssignedToDifferentNodesTest() { - var builder = new DbcBuilder(new SilentFailureObserver()); + var builder = new DbcBuilder(m_silentFailureObserver); var node1 = new Node { Name = "Node1" }; var node2 = new Node { Name = "Node2" }; builder.AddNode(node1); builder.AddNode(node2); var customPropertyLineParsers = CreateParser(); - var nextLineProvider = new NextLineProvider(new StringReader(string.Empty)); + var nextLineProvider = new NextLineProvider(new StringReader(string.Empty), m_silentFailureObserver); Assert.Multiple(() => { Assert.That(ParseLine(@"BA_DEF_ BU_ ""AttributeName"" INT 0 200;", customPropertyLineParsers, builder, nextLineProvider), Is.True); From 72fded5339e970928fd164af0da89637187dee1b Mon Sep 17 00:00:00 2001 From: Uight Date: Thu, 19 Dec 2024 15:43:11 +0100 Subject: [PATCH 28/34] Resolve merge conflicts --- DbcParserLib/Observers/IParseFailureObserver.cs | 2 +- DbcParserLib/Observers/SilentFailureObserver.cs | 2 +- DbcParserLib/Observers/SimpleFailureObserver.cs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/DbcParserLib/Observers/IParseFailureObserver.cs b/DbcParserLib/Observers/IParseFailureObserver.cs index 875be68..d88c442 100644 --- a/DbcParserLib/Observers/IParseFailureObserver.cs +++ b/DbcParserLib/Observers/IParseFailureObserver.cs @@ -38,7 +38,7 @@ public interface IParseFailureObserver void PropertyValueOutOfIndex(string propertyName, string index); void ExtraMessageTransmittersSyntaxError(); void ExtraMessageTransmittersDuplicate(uint messageId, string duplicateTransmitter); - void UnknownLine(); + void UnknownLine(string line); void NoMessageFound(); void Clear(); } diff --git a/DbcParserLib/Observers/SilentFailureObserver.cs b/DbcParserLib/Observers/SilentFailureObserver.cs index eb96a9c..3db1b66 100644 --- a/DbcParserLib/Observers/SilentFailureObserver.cs +++ b/DbcParserLib/Observers/SilentFailureObserver.cs @@ -140,7 +140,7 @@ public void ExtraMessageTransmittersDuplicate(uint messageId, string duplicateTr { } - public void UnknownLine() + public void UnknownLine(string line) { } diff --git a/DbcParserLib/Observers/SimpleFailureObserver.cs b/DbcParserLib/Observers/SimpleFailureObserver.cs index 45e0a12..a4dd823 100644 --- a/DbcParserLib/Observers/SimpleFailureObserver.cs +++ b/DbcParserLib/Observers/SimpleFailureObserver.cs @@ -184,9 +184,9 @@ public void ExtraMessageTransmittersDuplicate(uint messageId, string duplicateTr AddError($"Duplicate additional transmitter '{duplicateTransmitter}' in message '{messageId}'"); } - public void UnknownLine() + public void UnknownLine(string line) { - AddError("Unknown syntax"); + AddError($"Unknown syntax in line: '{line}'"); } public void NoMessageFound() From 7a5a8ea79e9c5517a81a1dadafc1ded30689dc59 Mon Sep 17 00:00:00 2001 From: Uight Date: Thu, 19 Dec 2024 15:56:05 +0100 Subject: [PATCH 29/34] Add testcase for issue 97 --- DbcParserLib.Tests/ParserTests.cs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/DbcParserLib.Tests/ParserTests.cs b/DbcParserLib.Tests/ParserTests.cs index a23d6b3..181ac46 100644 --- a/DbcParserLib.Tests/ParserTests.cs +++ b/DbcParserLib.Tests/ParserTests.cs @@ -818,5 +818,35 @@ public void CommentWithTerminationAndMultiDefinitionInLine() Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Last().Key, Is.EqualTo(16384)); Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Last().Value, Is.EqualTo("ZERO_ANGLE")); } + + [Test] + public void CommentFromIssue97() + { + var dbcString = @" +BO_ 256 New_Message_1: 8 Vector__XXX + SG_ New_Signal_2 : 8|8@1- (1,0) [0|0] """" Vector__XXX + SG_ New_Signal_1 : 0|8@1- (1,0) [0|0] """" Vector__XXX + +CM_ SG_ 256 New_Signal_1 ""Flag to indicate newly created object in CAN bus. +Object history flag +0x0: new object in the cycle; +0x1: object existed in previous cycle""; +BA_DEF_ ""DBName"" STRING ;"; + + var failureObserver = new SimpleFailureObserver(); + Parser.SetParsingFailuresObserver(failureObserver); + var dbc = Parser.Parse(dbcString); + var errorList = failureObserver.GetErrorList(); + + Assert.That(errorList, Has.Count.EqualTo(0)); + Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Comment, Is.Null); + + Assert.That(dbc.Messages.First().Signals.Count(), Is.EqualTo(2)); + + Assert.That(dbc.Messages.First().Signals.First().Name, Is.EqualTo("New_Signal_2")); + Assert.That(dbc.Messages.First().Signals.Last().Name, Is.EqualTo("New_Signal_1")); + Assert.That(dbc.Messages.First().Signals.Last().Comment, Is.EqualTo("Flag to indicate newly created object in CAN bus.\r\nObject history flag\r\n0x0: new object in the cycle;\r\n0x1: object existed in previous cycle")); + } } } \ No newline at end of file From 297e3404bdb636d79d034ccf8b343671042a8428 Mon Sep 17 00:00:00 2001 From: Uight Date: Thu, 19 Dec 2024 16:44:22 +0100 Subject: [PATCH 30/34] use Env new line in test --- DbcParserLib.Tests/ParserTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DbcParserLib.Tests/ParserTests.cs b/DbcParserLib.Tests/ParserTests.cs index 181ac46..13f8fa2 100644 --- a/DbcParserLib.Tests/ParserTests.cs +++ b/DbcParserLib.Tests/ParserTests.cs @@ -846,7 +846,7 @@ Object history flag Assert.That(dbc.Messages.First().Signals.First().Name, Is.EqualTo("New_Signal_2")); Assert.That(dbc.Messages.First().Signals.Last().Name, Is.EqualTo("New_Signal_1")); - Assert.That(dbc.Messages.First().Signals.Last().Comment, Is.EqualTo("Flag to indicate newly created object in CAN bus.\r\nObject history flag\r\n0x0: new object in the cycle;\r\n0x1: object existed in previous cycle")); + Assert.That(dbc.Messages.First().Signals.Last().Comment, Is.EqualTo($"Flag to indicate newly created object in CAN bus.{Environment.NewLine}Object history flag{Environment.NewLine}0x0: new object in the cycle;{Environment.NewLine}0x1: object existed in previous cycle")); } } } \ No newline at end of file From 840ae99fdf12af276bc3ea501aeb403b2038775c Mon Sep 17 00:00:00 2001 From: Uight Date: Thu, 19 Dec 2024 16:47:40 +0100 Subject: [PATCH 31/34] Add a test to parse a full file including namespaces and so on which currently failes --- DbcParserLib.Tests/ParserTests.cs | 67 +++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/DbcParserLib.Tests/ParserTests.cs b/DbcParserLib.Tests/ParserTests.cs index 13f8fa2..c87bd24 100644 --- a/DbcParserLib.Tests/ParserTests.cs +++ b/DbcParserLib.Tests/ParserTests.cs @@ -848,5 +848,72 @@ Object history flag Assert.That(dbc.Messages.First().Signals.Last().Name, Is.EqualTo("New_Signal_1")); Assert.That(dbc.Messages.First().Signals.Last().Comment, Is.EqualTo($"Flag to indicate newly created object in CAN bus.{Environment.NewLine}Object history flag{Environment.NewLine}0x0: new object in the cycle;{Environment.NewLine}0x1: object existed in previous cycle")); } + + + [Test] + public void ParseFullFile() + { + var dbcString = @" +VERSION """" + +NS_ : + NS_DESC_ + CM_ + BA_DEF_ + BA_ + VAL_ + CAT_DEF_ + CAT_ + FILTER + BA_DEF_DEF_ + EV_DATA_ + ENVVAR_DATA_ + SGTYPE_ + SGTYPE_VAL_ + BA_DEF_SGTYPE_ + BA_SGTYPE_ + SIG_TYPE_REF_ + VAL_TABLE_ + SIG_GROUP_ + SIG_VALTYPE_ + SIGTYPE_VALTYPE_ + BO_TX_BU_ + BA_DEF_REL_ + BA_REL_ + BA_DEF_DEF_REL_ + BU_SG_REL_ + BU_EV_REL_ + BU_BO_REL_ + SG_MUL_VAL_ + +BS_: + +BU_: + +BO_ 1160 DAS_steeringControl: 4 NEO + SG_ DAS_steeringControlType : 23|2@0+ (1,0) [0|0] """" EPAS + SG_ DAS_steeringAngleRequest : 0|16@0+ (1,0) [0|0] """" EPAS + +CM_ BO_ 1160 ""This is a very fine comment; Right?""; +VAL_ 1160 DAS_steeringControlType 1 ""ANGLE_CONTROL"" 3 ""DISABLED"" 0 ""NONE"" 2 ""RESERVED""; +VAL_ 1160 DAS_steeringAngleRequest 16384 ""ZERO_ANGLE"";"; + + var failureObserver = new SimpleFailureObserver(); + Parser.SetParsingFailuresObserver(failureObserver); + var dbc = Parser.Parse(dbcString); + var errorList = failureObserver.GetErrorList(); + + Assert.That(errorList, Has.Count.EqualTo(0)); + Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Comment, Is.EqualTo("This is a very fine comment; Right?")); + + Assert.That(dbc.Messages.First().Signals.Count(), Is.EqualTo(2)); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Count, Is.EqualTo(4)); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Last().Key, Is.EqualTo(2)); + Assert.That(dbc.Messages.First().Signals.First().ValueTableMap.Last().Value, Is.EqualTo("RESERVED")); + Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Count, Is.EqualTo(1)); + Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Last().Key, Is.EqualTo(16384)); + Assert.That(dbc.Messages.First().Signals.Last().ValueTableMap.Last().Value, Is.EqualTo("ZERO_ANGLE")); + } } } \ No newline at end of file From 95a26d9488a25c660ac60997d94ec38361a90346 Mon Sep 17 00:00:00 2001 From: Uight Date: Thu, 19 Dec 2024 16:49:00 +0100 Subject: [PATCH 32/34] Remove dumb missing termiantion correction; Vector Candb++ doesnt do that and just failes parsing --- DbcParserLib.Tests/ParserTests.cs | 20 -------------------- DbcParserLib/NextLineProvider.cs | 6 ------ 2 files changed, 26 deletions(-) diff --git a/DbcParserLib.Tests/ParserTests.cs b/DbcParserLib.Tests/ParserTests.cs index c87bd24..152964a 100644 --- a/DbcParserLib.Tests/ParserTests.cs +++ b/DbcParserLib.Tests/ParserTests.cs @@ -739,26 +739,6 @@ public void CommentContainingTermination() Assert.That(dbc.Messages.First().Comment, Is.EqualTo($"Message; comment; first; line{Environment.NewLine}second; line")); } - [Test] - public void CommentMissingTerminationGetsCorrected() - { - var dbcString = @" -BO_ 1043 BLINKERS: 8 XXX - -CM_ BO_ 1043 ""Message comment first line -second line"""; - - - var failureObserver = new SimpleFailureObserver(); - Parser.SetParsingFailuresObserver(failureObserver); - var dbc = Parser.Parse(dbcString); - var errorList = failureObserver.GetErrorList(); - - Assert.That(errorList, Has.Count.EqualTo(0)); - Assert.That(dbc.Messages.Count(), Is.EqualTo(1)); - Assert.That(dbc.Messages.First().Comment, Is.EqualTo($"Message comment first line{Environment.NewLine}second line")); - } - [Test] public void HandleMultiDefinitionLineAfterMultiLine() { diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index 85075c0..62ea2e2 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -90,12 +90,6 @@ public bool TryGetLine(out string line) line = HandleMultipleDefinitionsPerLine(line); line = HandleMultiline(line); - var test = line; - if (line.EndsWith(lineTermination) == false && keywords.Any(prefix => test.Equals(prefix)) == false) //correct missing terminations - { - line = line + lineTermination; - } - return true; } From b3d08c3a10b8879cec4b2e413b56e4d9b57ba29e Mon Sep 17 00:00:00 2001 From: Uight Date: Thu, 19 Dec 2024 18:41:52 +0100 Subject: [PATCH 33/34] Bugfix for multiple terminations in one line --- DbcParserLib/NextLineProvider.cs | 35 ++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/DbcParserLib/NextLineProvider.cs b/DbcParserLib/NextLineProvider.cs index 62ea2e2..e1ecbfb 100644 --- a/DbcParserLib/NextLineProvider.cs +++ b/DbcParserLib/NextLineProvider.cs @@ -95,27 +95,40 @@ public bool TryGetLine(out string line) private string HandleMultipleDefinitionsPerLine(string line) { - int definitionTerminationLocation = line.IndexOf(lineTermination, StringComparison.Ordinal); + var lineTerminationIndex = line.IndexOf(lineTermination, StringComparison.Ordinal); + var afterTerminationPosition = lineTerminationIndex + lineTermination.Length; - var lastTerminationLocation = -1; - while (definitionTerminationLocation > lastTerminationLocation) + // If line has no termination just return the line + if (lineTerminationIndex < 0) { - if (definitionTerminationLocation + 1 == line.Length) - { - return line; - } + return line; + } - var partAfterTermination = line.Substring(definitionTerminationLocation + 2, line.Length - 2 - definitionTerminationLocation); + while (afterTerminationPosition < line.Length) + { + var partAfterTermination = line.Substring(afterTerminationPosition); + // If part after the termination is another keyword then add the remaining line as virtual line + // and return line up to and including line termination; if (CheckLineIsDefinition(partAfterTermination.TrimStart())) { m_reader.SetVirtualLine(partAfterTermination); - return line.Substring(0, definitionTerminationLocation + 1); + return line.Substring(0, afterTerminationPosition); + } + + var indexOfNextTermination = partAfterTermination.IndexOf(lineTermination, StringComparison.Ordinal); + + if (indexOfNextTermination < 0) + { + // You run in here if the line has atleast one termination but no followup definition + // Should only occure in comments but these should end in termination char so will most likely + // lead to an parsing error + return line; } - lastTerminationLocation = definitionTerminationLocation; - definitionTerminationLocation = definitionTerminationLocation + 1 + partAfterTermination.IndexOf(lineTermination, StringComparison.Ordinal); + afterTerminationPosition = afterTerminationPosition + indexOfNextTermination + lineTermination.Length; } + // If the terminationcharacter is found at the end of the line and no subdefinitions where found retrun the line return line; } From 5c635172a5bff933b250e08c3b27e04de6ed3c4f Mon Sep 17 00:00:00 2001 From: Uight Date: Thu, 19 Dec 2024 20:20:53 +0100 Subject: [PATCH 34/34] Remove abilities to have comments without termination if termination char is in comment. And add tests --- DbcParserLib.Tests/CommentLineParserTests.cs | 6 ++++++ DbcParserLib/Parsers/CommentLineParser.cs | 10 +++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/DbcParserLib.Tests/CommentLineParserTests.cs b/DbcParserLib.Tests/CommentLineParserTests.cs index a2b9fb8..b6cf4de 100644 --- a/DbcParserLib.Tests/CommentLineParserTests.cs +++ b/DbcParserLib.Tests/CommentLineParserTests.cs @@ -267,6 +267,12 @@ public void AnotherMalformedLineIsAcceptedWithoutInteraction() [TestCase("CM_ BO_ 865 \"Test with incorrect \"syntax\"\";")] [TestCase("CM_ EV_ VarName \"Test with incorrect \"syntax\"\";")] [TestCase("CM_ \"Test with incorrect \"syntax\"\";")] + [TestCase("CM_ BO_ 1160 \"This is a very fine comment;\"")] + [TestCase("CM_ BO_ 1160 \"This is a very fine comment\"")] + [TestCase("CM_ BO_ 1160 \"This is a very fine comment; Right?\"")] + [TestCase("CM_ SG_ SignalName \"This is a very fine comment; Right?\"")] + [TestCase("CM_ BU_ NodeName \"This is a very fine comment; Right?\"")] + [TestCase("CM_ EV_ VarName \"This is a very fine comment; Right?\"")] public void CommentSyntaxErrorIsObserved(string commentLine) { var observerMock = m_repository.Create(); diff --git a/DbcParserLib/Parsers/CommentLineParser.cs b/DbcParserLib/Parsers/CommentLineParser.cs index c580dab..2c8787e 100644 --- a/DbcParserLib/Parsers/CommentLineParser.cs +++ b/DbcParserLib/Parsers/CommentLineParser.cs @@ -13,11 +13,11 @@ internal class CommentLineParser : ILineParser private const string EnvVarNameGroup = "EnvVarName"; private const string CommentLineStarter = "CM_ "; - private readonly string m_genericCommentParsingRegex = $@"CM_\s+""*(?<{CharGroup}>[^""]*)""*\s*;"; - private readonly string m_nodeParsingRegex = $@"CM_ BU_\s+(?<{NodeNameGroup}>[a-zA-Z_][\w]*)\s+""*(?<{CharGroup}>[^""]*)""*\s*;"; - private readonly string m_messageParsingRegex = $@"CM_ BO_\s+(?<{MessageIdGroup}>\d+)\s+""*(?<{CharGroup}>[^""]*)""*\s*;"; - private readonly string m_signalParsingRegex = $@"CM_ SG_\s+(?<{MessageIdGroup}>\d+)\s+(?<{SignalNameGroup}>[a-zA-Z_][\w]*)\s+""*(?<{CharGroup}>[^""]*)""*\s*;"; - private readonly string m_environmentVariableParsingRegex = $@"CM_ EV_\s+(?<{EnvVarNameGroup}>[a-zA-Z_][\w]*)\s+""*(?<{CharGroup}>[^""]*)""*\s*;"; + private readonly string m_genericCommentParsingRegex = $@"CM_\s+""*(?<{CharGroup}>[^""]*)""*\s*;$"; + private readonly string m_nodeParsingRegex = $@"CM_ BU_\s+(?<{NodeNameGroup}>[a-zA-Z_][\w]*)\s+""*(?<{CharGroup}>[^""]*)""*\s*;$"; + private readonly string m_messageParsingRegex = $@"CM_ BO_\s+(?<{MessageIdGroup}>\d+)\s+""*(?<{CharGroup}>[^""]*)""*\s*;$"; + private readonly string m_signalParsingRegex = $@"CM_ SG_\s+(?<{MessageIdGroup}>\d+)\s+(?<{SignalNameGroup}>[a-zA-Z_][\w]*)\s+""*(?<{CharGroup}>[^""]*)""*\s*;$"; + private readonly string m_environmentVariableParsingRegex = $@"CM_ EV_\s+(?<{EnvVarNameGroup}>[a-zA-Z_][\w]*)\s+""*(?<{CharGroup}>[^""]*)""*\s*;$"; private readonly IParseFailureObserver m_observer;