forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersionRanges.cs
More file actions
188 lines (157 loc) · 7.1 KB
/
Copy pathVersionRanges.cs
File metadata and controls
188 lines (157 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using UnityEngine.Assertions;
namespace UnityEditor.Scripting.ScriptCompilation
{
internal struct VersionRanges<TVersion> where TVersion : struct, IVersion<TVersion>
{
private readonly Dictionary<ExpressionTypeKey, ExpressionTypeValue<TVersion>> m_ExpressionTypes;
private static readonly char[] k_LeftValidSymbols = new[] { '[', '(', };
private static readonly char[] k_RightValidSymbols = new[] { ']', ')', };
private static readonly TVersion m_versionTypeStaticFunctionalityProxy;
static VersionRanges()
{
// String representations of Version Types must not allow any of these characters, because
// they have other meaning inside a version range expression.
m_versionTypeStaticFunctionalityProxy = new TVersion();
IVersionTypeTraits versionTypeTraits = m_versionTypeStaticFunctionalityProxy.GetVersionTypeTraits();
Assert.IsFalse(versionTypeTraits.IsAllowedCharacter('*'));
Assert.IsFalse(versionTypeTraits.IsAllowedCharacter(','));
Assert.IsFalse(versionTypeTraits.IsAllowedCharacter('['));
Assert.IsFalse(versionTypeTraits.IsAllowedCharacter('('));
Assert.IsFalse(versionTypeTraits.IsAllowedCharacter(']'));
Assert.IsFalse(versionTypeTraits.IsAllowedCharacter(')'));
}
public VersionRanges(Dictionary<ExpressionTypeKey, ExpressionTypeValue<TVersion>> expressionTypes)
{
m_ExpressionTypes = expressionTypes;
}
public VersionDefineExpression<TVersion> GetExpression(string expression)
{
if (string.IsNullOrEmpty(expression))
{
throw new ArgumentNullException(nameof(expression));
}
ExpressionParsedData parsedExpressionData = ParseExpression(expression);
if (m_ExpressionTypes.ContainsKey(parsedExpressionData.GenerateExpressionTypeKey))
{
ExpressionTypeValue<TVersion> expressionTypeValue = m_ExpressionTypes[parsedExpressionData.GenerateExpressionTypeKey];
return new VersionDefineExpression<TVersion>(expressionTypeValue.IsValid, parsedExpressionData.leftVersion, parsedExpressionData.rightVersion)
{
AppliedRule = expressionTypeValue.AppliedRule,
};
}
throw new ExpressionNotValidException($"'{expression}' is not a valid expression");
}
private static bool Contains(string array, char doContain)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i] == doContain)
{
return true;
}
}
return false;
}
private static bool Contains(char[] array, char doContain)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i] == doContain)
{
return true;
}
}
return false;
}
private ExpressionParsedData ParseExpression(string expression)
{
ExpressionParsedData expressionParsedData = new ExpressionParsedData();
if (expression.Length == 0)
{
return expressionParsedData;
}
IVersionTypeTraits versionTypeTraits = m_versionTypeStaticFunctionalityProxy.GetVersionTypeTraits();
bool hasSeperator = Contains(expression, ',');
char leftSymbol = default(char);
char rightSymbol = default(char);
int begin = 0;
int end = expression.Length - 1;
if (!versionTypeTraits.IsAllowedFirstCharacter(expression[0]))
{
leftSymbol = expression[0];
if (!Contains(k_LeftValidSymbols, leftSymbol))
{
throw new ExpressionNotValidException($"Invalid character '{leftSymbol}' in expression", expression);
}
begin++;
}
var lastChar = expression[end];
if (!versionTypeTraits.IsAllowedLastCharacter(lastChar))
{
rightSymbol = lastChar;
if (!Contains(k_RightValidSymbols, rightSymbol))
{
throw new ExpressionNotValidException($"Invalid character '{rightSymbol}' in expression", expression);
}
end--;
}
if ((leftSymbol != default(char) && rightSymbol == default(char)) ||
(leftSymbol == default(char) && rightSymbol != default(char)))
{
throw new ExpressionNotValidException("Incomplete expression, missing symbol in start or end", expression);
}
int nextVersion;
string leftVersionString = PopVersionString(expression, begin, end, out nextVersion, versionTypeTraits);
var hasLeftVersion = !string.IsNullOrEmpty(leftVersionString);
if (hasLeftVersion)
{
expressionParsedData.leftVersion = (TVersion)m_versionTypeStaticFunctionalityProxy.Parse(leftVersionString);
}
int notNeeded;
string rightVersionString = PopVersionString(expression, nextVersion, end, out notNeeded, versionTypeTraits);
var hasRightVersion = !string.IsNullOrEmpty(rightVersionString);
if (hasRightVersion)
{
expressionParsedData.rightVersion = (TVersion)m_versionTypeStaticFunctionalityProxy.Parse(rightVersionString);
}
expressionParsedData.GenerateExpressionTypeKey = new ExpressionTypeKey(leftSymbol: leftSymbol, rightSymbol: rightSymbol, hasSeparator: hasSeperator, hasLeftVersion: hasLeftVersion, hasRightVersion: hasRightVersion);
return expressionParsedData;
}
private static string PopVersionString(string expression, int begin, int end, out int newBegin, IVersionTypeTraits versionTypeTraits)
{
newBegin = begin;
if (begin > end)
{
return null;
}
int count = 0;
while (newBegin <= end)
{
var value = expression[newBegin];
if (value == ',')
{
newBegin++;
break;
}
if (!versionTypeTraits.IsAllowedCharacter(value) && value != '*')
{
throw new ExpressionNotValidException($"'{value}' is not valid in the expression");
}
count++;
newBegin++;
}
return expression.Substring(begin, count);
}
private struct ExpressionParsedData
{
public TVersion leftVersion;
public TVersion rightVersion;
public ExpressionTypeKey GenerateExpressionTypeKey;
}
}
}