forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemVersionRanges.cs
More file actions
181 lines (151 loc) · 6.06 KB
/
Copy pathSemVersionRanges.cs
File metadata and controls
181 lines (151 loc) · 6.06 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
// 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 System.Linq;
namespace UnityEditor.Scripting.ScriptCompilation
{
internal struct SemVersionRanges
{
private readonly Dictionary<ExpressionTypeKey, ExpressionTypeValue> m_ExpressionTypes;
private static readonly char[] k_LeftValidSymbols = new[] { '[', '(', };
private static readonly char[] k_RightValidSymbols = new[] { ']', ')', };
public SemVersionRanges(Dictionary<ExpressionTypeKey, ExpressionTypeValue> expressionTypes)
{
m_ExpressionTypes = expressionTypes;
}
public VersionDefineExpression GetExpression(string expression)
{
if (string.IsNullOrEmpty(expression))
{
throw new ArgumentNullException(nameof(expression));
}
ExpressionParsedData parsedExpressionData = ParseExpression(expression);
if (m_ExpressionTypes.ContainsKey(parsedExpressionData.GenerateExpressionTypeKey))
{
ExpressionTypeValue expressionTypeValue = m_ExpressionTypes[parsedExpressionData.GenerateExpressionTypeKey];
return new VersionDefineExpression(expressionTypeValue.IsValid, parsedExpressionData.leftSemVersion, parsedExpressionData.rightSemVersion)
{
AppliedRule = expressionTypeValue.AppliedRule,
};
}
throw new ArgumentException("Not Found");
}
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 static bool IsCharDigit(char c)
{
return (c >= '0' && c <= '9');
}
private static bool IsCharLetter(char c)
{
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';
}
private ExpressionParsedData ParseExpression(string expression)
{
ExpressionParsedData expressionParsedData = new ExpressionParsedData();
if (expression.Length == 0)
{
return expressionParsedData;
}
bool hasSeperator = Contains(expression, ',');
char leftSymbol = default(char);
char rightSymbol = default(char);
int begin = 0;
int end = expression.Length - 1;
if (!IsCharDigit(expression[0]))
{
leftSymbol = expression[0];
if (!Contains(k_LeftValidSymbols, leftSymbol))
{
throw new ExpressionNotValidException($"Invalid character {leftSymbol}", expression);
}
begin++;
}
var lastChar = expression[end];
if (!IsCharDigit(lastChar) && !IsCharLetter(lastChar))
{
rightSymbol = lastChar;
if (!Contains(k_RightValidSymbols, rightSymbol))
{
throw new ExpressionNotValidException($"Invalid character {rightSymbol}", 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 nextSemVer;
string leftSemVerString = PopSemanticVersion(expression, begin, end, out nextSemVer);
var hasLeftSemVer = !string.IsNullOrEmpty(leftSemVerString);
if (hasLeftSemVer)
{
expressionParsedData.leftSemVersion = SemVersionParser.Parse(leftSemVerString);
}
int notNeeded;
string rightSemVerString = PopSemanticVersion(expression, nextSemVer, end, out notNeeded);
var hasRightSemVer = !string.IsNullOrEmpty(rightSemVerString);
if (hasRightSemVer)
{
expressionParsedData.rightSemVersion = SemVersionParser.Parse(rightSemVerString);
}
expressionParsedData.GenerateExpressionTypeKey = new ExpressionTypeKey(leftSymbol: leftSymbol, rightSymbol: rightSymbol, hasSeparator: hasSeperator, hasLeftSemVer: hasLeftSemVer, hasRightSemVer: hasRightSemVer);
return expressionParsedData;
}
private static string PopSemanticVersion(string expression, int begin, int end, out int newBegin)
{
newBegin = begin;
if (begin >= end)
{
return null;
}
int count = 0;
while (newBegin <= end)
{
if (expression[newBegin] == ',')
{
newBegin++;
break;
}
var value = expression[newBegin];
if (!IsCharDigit(value) && value != '.'
&& value != '-' && !IsCharLetter(value) && value != '*')
{
throw new ExpressionNotValidException($"'{value}' is not valid in the expression");
}
count++;
newBegin++;
}
return expression.Substring(begin, count);
}
private struct ExpressionParsedData
{
public SemVersion leftSemVersion;
public SemVersion rightSemVersion;
public ExpressionTypeKey GenerateExpressionTypeKey;
}
}
}