Skip to content

Commit 40c79a0

Browse files
authored
Merge pull request #83 from Whitehouse112/regex_refactoring
#81 Empty node list is now supported. Regex refactoring
2 parents a1ccb98 + a7d733f commit 40c79a0

12 files changed

+225
-122
lines changed

DbcParserLib.Tests/NodeLineParserTests.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ private static ILineParser CreateParser()
2828
return new NodeLineParser(new SilentFailureObserver());
2929
}
3030

31+
[Test]
32+
public void EmptyNodeListIsAccepted()
33+
{
34+
var dbcBuilderMock = m_repository.Create<IDbcBuilder>();
35+
var nodeLineParser = CreateParser();
36+
var nextLineProviderMock = m_repository.Create<INextLineProvider>();
37+
38+
Assert.That(nodeLineParser.TryParse(" BU_: ", dbcBuilderMock.Object, nextLineProviderMock.Object), Is.True);
39+
}
40+
3141
[Test]
3242
public void EmptyCommentLineIsIgnored()
3343
{
@@ -95,7 +105,6 @@ public void FullLineIsParsed()
95105

96106
[TestCase("BU_: 0nodeName")]
97107
[TestCase("BU_:nodeName")]
98-
[TestCase("BU_:")]
99108
public void NodeSyntaxErrorIsObserved(string line)
100109
{
101110
var observerMock = m_repository.Create<IParseFailureObserver>();

DbcParserLib/Parsers/CommentLineParser.cs

+24-18
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ namespace DbcParserLib.Parsers
66
{
77
internal class CommentLineParser : ILineParser
88
{
9+
private const string CharGroup = "CharString";
10+
private const string NodeNameGroup = "NodeName";
11+
private const string MessageIdGroup = "MessageId";
12+
private const string SignalNameGroup = "SignalName";
13+
private const string EnvVarNameGroup = "EnvVarName";
914
private const string CommentLineStarter = "CM_ ";
10-
private const string GenericCommentParsingRegex = @"CM_\s+""*([^""]*)""*\s*;";
11-
private const string NodeParsingRegex = @"CM_ BU_\s+([a-zA-Z_][\w]*)\s+""*([^""]*)""*\s*;";
12-
private const string MessageParsingRegex = @"CM_ BO_\s+(\d+)\s+""*([^""]*)""*\s*;";
13-
private const string SignalParsingRegex = @"CM_ SG_\s+(\d+)\s+([a-zA-Z_][\w]*)\s+""*([^""]*)""*\s*;";
14-
private const string EnvironmentVariableParsingRegex = @"CM_ EV_\s+([a-zA-Z_][\w]*)\s+""*([^""]*)""*\s*;";
15+
16+
private readonly string m_genericCommentParsingRegex = $@"CM_\s+""*(?<{CharGroup}>[^""]*)""*\s*;";
17+
private readonly string m_nodeParsingRegex = $@"CM_ BU_\s+(?<{NodeNameGroup}>[a-zA-Z_][\w]*)\s+""*(?<{CharGroup}>[^""]*)""*\s*;";
18+
private readonly string m_messageParsingRegex = $@"CM_ BO_\s+(?<{MessageIdGroup}>\d+)\s+""*(?<{CharGroup}>[^""]*)""*\s*;";
19+
private readonly string m_signalParsingRegex = $@"CM_ SG_\s+(?<{MessageIdGroup}>\d+)\s+(?<{SignalNameGroup}>[a-zA-Z_][\w]*)\s+""*(?<{CharGroup}>[^""]*)""*\s*;";
20+
private readonly string m_environmentVariableParsingRegex = $@"CM_ EV_\s+(?<{EnvVarNameGroup}>[a-zA-Z_][\w]*)\s+""*(?<{CharGroup}>[^""]*)""*\s*;";
1521

1622
private readonly IParseFailureObserver m_observer;
1723

@@ -54,50 +60,50 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin
5460
return true;
5561
}
5662

57-
var match = Regex.Match(cleanLine, GenericCommentParsingRegex);
63+
var match = Regex.Match(cleanLine, m_genericCommentParsingRegex);
5864
if (match.Success)
5965
return true;
6066

6167
m_observer.CommentSyntaxError();
6268
return true;
6369
}
6470

65-
private static void SetSignalComment(string sigCommentStr, IParseFailureObserver observer, IDbcBuilder builder, INextLineProvider nextLineProvider)
71+
private void SetSignalComment(string sigCommentStr, IParseFailureObserver observer, IDbcBuilder builder, INextLineProvider nextLineProvider)
6672
{
67-
var match = Regex.Match(sigCommentStr, SignalParsingRegex);
73+
var match = Regex.Match(sigCommentStr, m_signalParsingRegex);
6874

6975
if (match.Success)
70-
builder.AddSignalComment(uint.Parse(match.Groups[1].Value), match.Groups[2].Value, match.Groups[3].Value);
76+
builder.AddSignalComment(uint.Parse(match.Groups[MessageIdGroup].Value), match.Groups[SignalNameGroup].Value, match.Groups[CharGroup].Value);
7177
else
7278
observer.CommentSyntaxError();
7379
}
7480

75-
private static void SetNodeComment(string sigCommentStr, IParseFailureObserver observer, IDbcBuilder builder, INextLineProvider nextLineProvider)
81+
private void SetNodeComment(string sigCommentStr, IParseFailureObserver observer, IDbcBuilder builder, INextLineProvider nextLineProvider)
7682
{
77-
var match = Regex.Match(sigCommentStr, NodeParsingRegex);
83+
var match = Regex.Match(sigCommentStr, m_nodeParsingRegex);
7884

7985
if (match.Success)
80-
builder.AddNodeComment(match.Groups[1].Value, match.Groups[2].Value);
86+
builder.AddNodeComment(match.Groups[NodeNameGroup].Value, match.Groups[CharGroup].Value);
8187
else
8288
observer.CommentSyntaxError();
8389
}
8490

85-
private static void SetMessageComment(string sigCommentStr, IParseFailureObserver observer, IDbcBuilder builder, INextLineProvider nextLineProvider)
91+
private void SetMessageComment(string sigCommentStr, IParseFailureObserver observer, IDbcBuilder builder, INextLineProvider nextLineProvider)
8692
{
87-
var match = Regex.Match(sigCommentStr, MessageParsingRegex);
93+
var match = Regex.Match(sigCommentStr, m_messageParsingRegex);
8894

8995
if (match.Success)
90-
builder.AddMessageComment(uint.Parse(match.Groups[1].Value), match.Groups[2].Value);
96+
builder.AddMessageComment(uint.Parse(match.Groups[MessageIdGroup].Value), match.Groups[CharGroup].Value);
9197
else
9298
observer.CommentSyntaxError();
9399
}
94100

95-
private static void SetEnvironmentVariableComment(string envCommentStr, IParseFailureObserver observer, IDbcBuilder builder)
101+
private void SetEnvironmentVariableComment(string envCommentStr, IParseFailureObserver observer, IDbcBuilder builder)
96102
{
97-
var match = Regex.Match(envCommentStr, EnvironmentVariableParsingRegex);
103+
var match = Regex.Match(envCommentStr, m_environmentVariableParsingRegex);
98104

99105
if (match.Success)
100-
builder.AddEnvironmentVariableComment(match.Groups[1].Value, match.Groups[2].Value);
106+
builder.AddEnvironmentVariableComment(match.Groups[EnvVarNameGroup].Value, match.Groups[CharGroup].Value);
101107
else
102108
observer.CommentSyntaxError();
103109
}

DbcParserLib/Parsers/EnvironmentDataVariableLineParser.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ namespace DbcParserLib.Parsers
55
{
66
internal class EnvironmentDataVariableLineParser : ILineParser
77
{
8+
private const string NameGroup = "Name";
9+
private const string DataSizeGroup = "DataSize";
810
private const string EnvironmentDataVariableLineStarter = "ENVVAR_DATA_ ";
9-
private const string EnvironmentDataVariableParsingRegex = @"ENVVAR_DATA_\s+([a-zA-Z_][\w]*)\s*:\s+(\d+)\s*;";
11+
12+
private readonly string m_environmentDataVariableParsingRegex = $@"ENVVAR_DATA_\s+(?<{NameGroup}>[a-zA-Z_][\w]*)\s*:\s+(?<{DataSizeGroup}>\d+)\s*;";
1013

1114
private readonly IParseFailureObserver m_observer;
1215

@@ -22,12 +25,12 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin
2225
if (cleanLine.StartsWith(EnvironmentDataVariableLineStarter) == false)
2326
return false;
2427

25-
var match = Regex.Match(cleanLine, EnvironmentDataVariableParsingRegex);
28+
var match = Regex.Match(cleanLine, m_environmentDataVariableParsingRegex);
2629
if (match.Success)
27-
builder.AddEnvironmentDataVariable(match.Groups[1].Value, uint.Parse(match.Groups[2].Value));
30+
builder.AddEnvironmentDataVariable(match.Groups[NameGroup].Value, uint.Parse(match.Groups[DataSizeGroup].Value));
2831
else
2932
m_observer.EnvironmentDataVariableSyntaxError();
30-
33+
3134
return true;
3235
}
3336
}

DbcParserLib/Parsers/EnvironmentVariableLineParser.cs

+30-17
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,21 @@ namespace DbcParserLib.Parsers
66
{
77
internal class EnvironmentVariableLineParser : ILineParser
88
{
9+
private const string NameGroup = "Name";
10+
private const string VarTypeGroup = "VarType";
11+
private const string MinGroup = "Min";
12+
private const string MaxGroup = "Max";
13+
private const string UnitGroup = "Unit";
14+
private const string InitialValueGroup = "InitialValue";
15+
private const string VarId = "VarId";
16+
private const string StringDataTypeGroup = "StringDataType";
17+
private const string AccessibilityGroup = "Accessibility2";
18+
private const string NodeGroup = "Node";
919
private const string EnvironmentVariableLineStarter = "EV_ ";
10-
private const string EnvironmentVariableParsingRegex = @"EV_\s+([a-zA-Z_][\w]*)\s*:\s+([012])\s+\[([\d\+\-eE.]+)\|([\d\+\-eE.]+)\]\s+""([^""]*)""\s+([\d\+\-eE.]+)\s+(\d+)\s+DUMMY_NODE_VECTOR(800){0,1}([0123])\s+((?:[a-zA-Z_][\w]*)(?:,[a-zA-Z_][\w]*)*)\s*;";
20+
21+
private readonly string m_environmentVariableParsingRegex = $@"EV_\s+(?<{NameGroup}>[a-zA-Z_][\w]*)\s*:\s+(?<{VarTypeGroup}>[012])\s+\[(?<{MinGroup}>[\d\+\-eE.]+)\|(?<{MaxGroup}>[\d\+\-eE.]+)\]" +
22+
$@"\s+""(?<{UnitGroup}>[^""]*)""\s+(?<{InitialValueGroup}>[\d\+\-eE.]+)\s+(?<{VarId}>\d+)\s+DUMMY_NODE_VECTOR(?<{StringDataTypeGroup}>800){{0,1}}" +
23+
$@"(?<{AccessibilityGroup}>[0123])\s+(?<{NodeGroup}>(?:[a-zA-Z_][\w]*)(?:,[a-zA-Z_][\w]*)*)\s*;";
1124

1225
private readonly IParseFailureObserver m_observer;
1326

@@ -23,16 +36,16 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin
2336
if (cleanLine.StartsWith(EnvironmentVariableLineStarter) == false)
2437
return false;
2538

26-
var match = Regex.Match(cleanLine, EnvironmentVariableParsingRegex);
39+
var match = Regex.Match(cleanLine, m_environmentVariableParsingRegex);
2740
if (match.Success)
2841
{
2942
var environmentVariable = new EnvironmentVariable()
3043
{
31-
Name = match.Groups[1].Value,
32-
Unit = match.Groups[5].Value,
44+
Name = match.Groups[NameGroup].Value,
45+
Unit = match.Groups[UnitGroup].Value,
3346
};
3447

35-
switch (uint.Parse(match.Groups[9].Value))
48+
switch (uint.Parse(match.Groups[AccessibilityGroup].Value))
3649
{
3750
case 0:
3851
environmentVariable.Access = EnvAccessibility.Unrestricted;
@@ -48,30 +61,30 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin
4861
break;
4962
}
5063

51-
if (match.Groups[8].Value == "800")
64+
if (match.Groups[StringDataTypeGroup].Value == "800")
5265
{
5366
environmentVariable.Type = EnvDataType.String;
5467
}
5568
else
5669
{
57-
switch (uint.Parse(match.Groups[2].Value))
70+
switch (uint.Parse(match.Groups[VarTypeGroup].Value))
5871
{
5972
case 0:
6073
environmentVariable.Type = EnvDataType.Integer;
6174
environmentVariable.IntegerEnvironmentVariable = new NumericEnvironmentVariable<int>()
6275
{
63-
Minimum = int.Parse(match.Groups[3].Value),
64-
Maximum = int.Parse(match.Groups[4].Value),
65-
Default = int.Parse(match.Groups[6].Value)
76+
Minimum = int.Parse(match.Groups[MinGroup].Value),
77+
Maximum = int.Parse(match.Groups[MaxGroup].Value),
78+
Default = int.Parse(match.Groups[InitialValueGroup].Value)
6679
};
6780
break;
6881
case 1:
6982
environmentVariable.Type = EnvDataType.Float;
7083
environmentVariable.FloatEnvironmentVariable = new NumericEnvironmentVariable<double>()
7184
{
72-
Minimum = double.Parse(match.Groups[3].Value),
73-
Maximum = double.Parse(match.Groups[4].Value),
74-
Default = double.Parse(match.Groups[6].Value)
85+
Minimum = double.Parse(match.Groups[MinGroup].Value),
86+
Maximum = double.Parse(match.Groups[MaxGroup].Value),
87+
Default = double.Parse(match.Groups[InitialValueGroup].Value)
7588
};
7689
break;
7790
case 2:
@@ -80,17 +93,17 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin
8093
}
8194
}
8295

83-
builder.AddEnvironmentVariable(match.Groups[1].Value, environmentVariable);
96+
builder.AddEnvironmentVariable(match.Groups[NameGroup].Value, environmentVariable);
8497

85-
var nodes = match.Groups[10].Value.Split(',');
98+
var nodes = match.Groups[NodeGroup].Value.Split(',');
8699
foreach (var node in nodes)
87100
{
88-
builder.AddNodeEnvironmentVariable(node, match.Groups[1].Value);
101+
builder.AddNodeEnvironmentVariable(node, match.Groups[NameGroup].Value);
89102
}
90103
}
91104
else
92105
m_observer.EnvironmentVariableSyntaxError();
93-
106+
94107
return true;
95108
}
96109
}

DbcParserLib/Parsers/MessageLineParser.cs

+16-11
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ namespace DbcParserLib.Parsers
77
{
88
internal class MessageLineParser : ILineParser
99
{
10+
private const string IdGroup = "Id";
11+
private const string NameGroup = "Name";
12+
private const string SizeGroup = "Size";
13+
private const string TransmitterGroup = "Transmitter";
1014
private const string MessageLineStarter = "BO_ ";
11-
private const string MessageRegex = @"BO_ (\d+)\s+([a-zA-Z_][\w]*)\s*:\s*(\d+)\s+([a-zA-Z_][\w]*)";
15+
16+
private readonly string m_messageRegex = $@"BO_ (?<{IdGroup}>\d+)\s+(?<{NameGroup}>[a-zA-Z_][\w]*)\s*:\s*(?<{SizeGroup}>\d+)\s+(?<{TransmitterGroup}>[a-zA-Z_][\w]*)";
1217

1318
private readonly IParseFailureObserver m_observer;
1419

@@ -19,25 +24,25 @@ public MessageLineParser(IParseFailureObserver observer)
1924

2025
public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider)
2126
{
22-
if(line.Trim().StartsWith(MessageLineStarter) == false)
27+
if (line.Trim().StartsWith(MessageLineStarter) == false)
2328
return false;
24-
25-
var match = Regex.Match(line, MessageRegex);
26-
if(match.Success)
29+
30+
var match = Regex.Match(line, m_messageRegex);
31+
if (match.Success)
2732
{
2833
var msg = new Message()
2934
{
30-
Name = match.Groups[2].Value,
31-
DLC = ushort.Parse(match.Groups[3].Value, CultureInfo.InvariantCulture),
32-
Transmitter = match.Groups[4].Value,
33-
ID = uint.Parse(match.Groups[1].Value, CultureInfo.InvariantCulture)
35+
Name = match.Groups[NameGroup].Value,
36+
DLC = ushort.Parse(match.Groups[SizeGroup].Value, CultureInfo.InvariantCulture),
37+
Transmitter = match.Groups[TransmitterGroup].Value,
38+
ID = uint.Parse(match.Groups[IdGroup].Value, CultureInfo.InvariantCulture)
3439
};
35-
40+
3641
builder.AddMessage(msg);
3742
}
3843
else
3944
m_observer.MessageSyntaxError();
40-
45+
4146
return true;
4247
}
4348
}

DbcParserLib/Parsers/NodeLineParser.cs

+11-3
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,33 @@ namespace DbcParserLib.Parsers
66
{
77
internal class NodeLineParser : ILineParser
88
{
9+
private const string NameGroup = "Name";
910
private const string NodeLineStarter = "BU_:";
10-
private const string NodeLineParsingRegex = @"BU_:((?:\s+(?:[a-zA-Z_][\w]*))+)";
11+
12+
private readonly string m_nodeLineParsingRegex = $@"BU_:(?<{NameGroup}>(?:\s+(?:[a-zA-Z_][\w]*))+)";
1113

1214
private readonly IParseFailureObserver m_observer;
15+
private readonly Regex m_regex;
1316

1417
public NodeLineParser(IParseFailureObserver observer)
1518
{
1619
m_observer = observer;
20+
m_regex = new Regex(m_nodeLineParsingRegex);
1721
}
1822

1923
public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLineProvider)
2024
{
2125
if (line.TrimStart().StartsWith(NodeLineStarter) == false)
2226
return false;
2327

24-
var match = Regex.Match(line, NodeLineParsingRegex);
28+
// Empty node list
29+
if (line.Trim().Equals(NodeLineStarter))
30+
return true;
31+
32+
var match = m_regex.Match(line);
2533
if (match.Success)
2634
{
27-
foreach (var nodeName in match.Groups[1].Value.TrimStart().SplitBySpace())
35+
foreach (var nodeName in match.Groups[NameGroup].Value.TrimStart().SplitBySpace())
2836
{
2937
var node = new Node()
3038
{

0 commit comments

Comments
 (0)