@@ -6,8 +6,21 @@ namespace DbcParserLib.Parsers
6
6
{
7
7
internal class EnvironmentVariableLineParser : ILineParser
8
8
{
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" ;
9
19
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*;";
11
24
12
25
private readonly IParseFailureObserver m_observer ;
13
26
@@ -23,16 +36,16 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin
23
36
if ( cleanLine . StartsWith ( EnvironmentVariableLineStarter ) == false )
24
37
return false ;
25
38
26
- var match = Regex . Match ( cleanLine , EnvironmentVariableParsingRegex ) ;
39
+ var match = Regex . Match ( cleanLine , m_environmentVariableParsingRegex ) ;
27
40
if ( match . Success )
28
41
{
29
42
var environmentVariable = new EnvironmentVariable ( )
30
43
{
31
- Name = match . Groups [ 1 ] . Value ,
32
- Unit = match . Groups [ 5 ] . Value ,
44
+ Name = match . Groups [ NameGroup ] . Value ,
45
+ Unit = match . Groups [ UnitGroup ] . Value ,
33
46
} ;
34
47
35
- switch ( uint . Parse ( match . Groups [ 9 ] . Value ) )
48
+ switch ( uint . Parse ( match . Groups [ AccessibilityGroup ] . Value ) )
36
49
{
37
50
case 0 :
38
51
environmentVariable . Access = EnvAccessibility . Unrestricted ;
@@ -48,30 +61,30 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin
48
61
break ;
49
62
}
50
63
51
- if ( match . Groups [ 8 ] . Value == "800" )
64
+ if ( match . Groups [ StringDataTypeGroup ] . Value == "800" )
52
65
{
53
66
environmentVariable . Type = EnvDataType . String ;
54
67
}
55
68
else
56
69
{
57
- switch ( uint . Parse ( match . Groups [ 2 ] . Value ) )
70
+ switch ( uint . Parse ( match . Groups [ VarTypeGroup ] . Value ) )
58
71
{
59
72
case 0 :
60
73
environmentVariable . Type = EnvDataType . Integer ;
61
74
environmentVariable . IntegerEnvironmentVariable = new NumericEnvironmentVariable < int > ( )
62
75
{
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 )
66
79
} ;
67
80
break ;
68
81
case 1 :
69
82
environmentVariable . Type = EnvDataType . Float ;
70
83
environmentVariable . FloatEnvironmentVariable = new NumericEnvironmentVariable < double > ( )
71
84
{
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 )
75
88
} ;
76
89
break ;
77
90
case 2 :
@@ -80,17 +93,17 @@ public bool TryParse(string line, IDbcBuilder builder, INextLineProvider nextLin
80
93
}
81
94
}
82
95
83
- builder . AddEnvironmentVariable ( match . Groups [ 1 ] . Value , environmentVariable ) ;
96
+ builder . AddEnvironmentVariable ( match . Groups [ NameGroup ] . Value , environmentVariable ) ;
84
97
85
- var nodes = match . Groups [ 10 ] . Value . Split ( ',' ) ;
98
+ var nodes = match . Groups [ NodeGroup ] . Value . Split ( ',' ) ;
86
99
foreach ( var node in nodes )
87
100
{
88
- builder . AddNodeEnvironmentVariable ( node , match . Groups [ 1 ] . Value ) ;
101
+ builder . AddNodeEnvironmentVariable ( node , match . Groups [ NameGroup ] . Value ) ;
89
102
}
90
103
}
91
104
else
92
105
m_observer . EnvironmentVariableSyntaxError ( ) ;
93
-
106
+
94
107
return true ;
95
108
}
96
109
}
0 commit comments