-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCommandMapNode.cs
More file actions
236 lines (206 loc) · 8.07 KB
/
CommandMapNode.cs
File metadata and controls
236 lines (206 loc) · 8.07 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System;
using System.Collections.Generic;
using Ultz.Extensions.Commands.Built;
namespace Ultz.Extensions.Commands.Mapping
{
internal sealed class CommandMapNode
{
private readonly Dictionary<string, List<Command>> _commands;
private readonly bool _isSeparatorSingleWhitespace;
private readonly Dictionary<string, CommandMapNode> _nodes;
private readonly CommandService _service;
public CommandMapNode(CommandService service)
{
_service = service;
_commands = new Dictionary<string, List<Command>>(_service.StringComparer);
_nodes = new Dictionary<string, CommandMapNode>(_service.StringComparer);
_isSeparatorSingleWhitespace = _service.Separator.Length == 1 && char.IsWhiteSpace(_service.Separator[0]);
}
public void FindCommands(ref List<CommandMatch> matches, List<string> path, ReadOnlySpan<char> span)
{
if (path.Count == 0)
{
span = span.TrimStart();
}
if (span.IsEmpty)
{
return;
}
var segment = FindNextSegment(span, out var encounteredWhitespace, out var encounteredSeparator,
out var remaining);
if (encounteredSeparator && remaining.IsEmpty)
{
return;
}
var stringSegment = segment.ToString();
if (!encounteredSeparator && !(encounteredWhitespace && remaining.IsEmpty) &&
_commands.TryGetValue(stringSegment, out var commands))
{
path.Add(stringSegment);
var stringRemaining = remaining.IsEmpty
? string.Empty
: remaining.ToString();
if (matches == null)
{
matches = new List<CommandMatch>(commands.Count);
}
for (var i = 0; i < commands.Count; i++)
{
matches.Add(new CommandMatch(commands[i], stringSegment, path, stringRemaining));
}
path.RemoveAt(path.Count - 1);
}
bool hasSeparator;
switch (_service.SeparatorRequirement)
{
case SeparatorRequirement.Separator when _isSeparatorSingleWhitespace:
{
hasSeparator = encounteredWhitespace;
break;
}
case SeparatorRequirement.Separator:
{
hasSeparator = encounteredSeparator;
break;
}
case SeparatorRequirement.SeparatorOrWhitespace:
{
hasSeparator = encounteredWhitespace || encounteredSeparator;
break;
}
default:
throw new InvalidOperationException("Invalid separator requirement.");
}
if (hasSeparator && _nodes.TryGetValue(stringSegment, out var node))
{
path.Add(stringSegment);
node.FindCommands(ref matches, path, remaining);
path.RemoveAt(path.Count - 1);
}
}
private ReadOnlySpan<char> FindNextSegment(ReadOnlySpan<char> span,
out bool encounteredWhitespace, out bool encounteredSeparator, out ReadOnlySpan<char> remaining)
{
encounteredWhitespace = false;
encounteredSeparator = false;
var segmentIndex = 0;
var nextSegmentIndex = 0;
var separator = _service.Separator;
var separatorIndex = _isSeparatorSingleWhitespace
? -1
: span.IndexOf(separator.AsSpan(), _service.StringComparison);
for (var i = 0; i < span.Length; i++)
{
if (segmentIndex != 0)
{
if (i == separatorIndex)
{
encounteredSeparator = true;
if (!_isSeparatorSingleWhitespace)
{
encounteredWhitespace = false;
}
i += separator.Length - 1;
nextSegmentIndex += separator.Length;
continue;
}
if (char.IsWhiteSpace(span[i]))
{
encounteredWhitespace = true;
nextSegmentIndex++;
continue;
}
}
if (segmentIndex != nextSegmentIndex)
{
break;
}
segmentIndex++;
nextSegmentIndex++;
}
remaining = span.Slice(nextSegmentIndex);
return span.Slice(0, segmentIndex);
}
private void ValidateCommand(Command command, string segment, List<Command> commands)
{
for (var i = 0; i < commands.Count; i++)
{
var otherCommand = commands[i];
var signature = command.SignatureIdentifier;
var otherSignature = otherCommand.SignatureIdentifier;
if (signature.Identifier != otherSignature.Identifier)
{
continue;
}
if (signature.HasRemainder == otherSignature.HasRemainder)
{
throw new CommandMappingException(command, segment,
"Cannot map multiple overloads with the same signature.");
}
if (!signature.HasRemainder && command.IgnoresExtraArguments ||
!otherSignature.HasRemainder && otherCommand.IgnoresExtraArguments)
{
throw new CommandMappingException(command, segment,
"Cannot map multiple overloads with the same argument types, with one of them being a remainder, if the other one ignores extra arguments.");
}
}
}
public void AddCommand(Command command, IReadOnlyList<string> segments, int startIndex)
{
if (segments.Count == 0)
{
throw new CommandMappingException(command, null,
"Cannot map commands without aliases to the root node.");
}
var segment = segments[startIndex];
if (startIndex == segments.Count - 1)
{
if (_commands.TryGetValue(segment, out var commands))
{
ValidateCommand(command, segment, commands);
commands.Add(command);
}
else
{
_commands.Add(segment, new List<Command> {command});
}
}
else
{
if (!_nodes.TryGetValue(segment, out var node))
{
node = new CommandMapNode(_service);
_nodes.Add(segment, node);
}
node.AddCommand(command, segments, startIndex + 1);
}
}
public void RemoveCommand(Command command, IReadOnlyList<string> segments, int startIndex)
{
if (segments.Count == 0)
{
return;
}
var segment = segments[startIndex];
if (startIndex == segments.Count - 1)
{
if (_commands.TryGetValue(segment, out var commands))
{
commands.Remove(command);
if (commands.Count == 0)
{
_commands.Remove(segment);
}
}
}
else if (_nodes.TryGetValue(segment, out var node))
{
node.RemoveCommand(command, segments, startIndex + 1);
if (node._commands.Count == 0)
{
_nodes.Remove(segment);
}
}
}
}
}