-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDefaultArgumentParser.cs
More file actions
215 lines (191 loc) · 8.39 KB
/
DefaultArgumentParser.cs
File metadata and controls
215 lines (191 loc) · 8.39 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
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Ultz.Extensions.Commands.Built;
using Ultz.Extensions.Commands.Context;
using Ultz.Extensions.Commands.Results.User;
namespace Ultz.Extensions.Commands.Parsing.ArgumentParsers.Default
{
/// <summary>
/// Represents the default argument parser used by the <see cref="CommandService" />.
/// </summary>
public sealed class DefaultArgumentParser : IArgumentParser
{
/// <summary>
/// Gets the singleton instance of the <see cref="DefaultArgumentParser" />.
/// </summary>
public static readonly DefaultArgumentParser Instance = new DefaultArgumentParser();
private DefaultArgumentParser()
{
}
/// <summary>
/// Attempts to parse raw arguments for the given <see cref="CommandContext" />.
/// </summary>
/// <param name="context"> The <see cref="CommandContext" /> to parse raw arguments for. </param>
/// <returns>
/// A <see cref="DefaultArgumentParserResult" />.
/// </returns>
public ValueTask<ArgumentParserResult> ParseAsync(CommandContext context)
{
var command = context.Command;
var rawArguments = context.RawArguments.AsSpan();
Parameter currentParameter = null;
Parameter multipleParameter = null;
var argumentBuilder = new StringBuilder();
Dictionary<Parameter, object> arguments = null;
var currentQuote = '\0';
var expectedQuote = '\0';
var whitespaceSeparated = false;
var isEscaping = false;
for (var currentPosition = 0; currentPosition < rawArguments.Length; currentPosition++)
{
char character;
if (currentParameter == null)
{
character = rawArguments[currentPosition];
if (char.IsWhiteSpace(character))
{
whitespaceSeparated = true;
continue;
}
if (currentPosition != 0 && !whitespaceSeparated)
{
return new DefaultArgumentParserResult(command, null, arguments,
command.Service.QuotationMarkMap.TryGetValue(character, out expectedQuote)
&& rawArguments.Slice(currentPosition + 1).IndexOf(expectedQuote) == -1
? DefaultArgumentParserFailure.UnexpectedQuote
: DefaultArgumentParserFailure.NoWhitespaceBetweenArguments, currentPosition);
}
currentParameter = (arguments == null || arguments.Count < command.Parameters.Count)
&& command.Parameters.Count > 0
? command.Parameters[arguments?.Count ?? 0]
: multipleParameter;
if (currentParameter == null)
{
if (command.IgnoresExtraArguments)
{
break;
}
return new DefaultArgumentParserResult(command, null, arguments,
DefaultArgumentParserFailure.TooManyArguments, currentPosition);
}
if (currentParameter.IsMultiple)
{
multipleParameter = currentParameter;
}
}
if (currentParameter.IsRemainder)
{
argumentBuilder.Append(rawArguments.Slice(currentPosition).ToString());
break;
}
character = rawArguments[currentPosition];
if (isEscaping)
{
argumentBuilder.Append(character);
isEscaping = false;
continue;
}
if (currentQuote == '\0')
{
if (char.IsWhiteSpace(character))
{
whitespaceSeparated = true;
NextParameter(command, ref currentParameter, argumentBuilder, ref arguments);
continue;
}
if (character == '\\' && currentPosition + 1 < rawArguments.Length &&
command.Service.QuotationMarkMap.ContainsKey(rawArguments[currentPosition + 1]))
{
isEscaping = true;
continue;
}
if (command.Service.QuotationMarkMap.TryGetValue(character, out expectedQuote))
{
if (currentPosition != 0 && !whitespaceSeparated)
{
return new DefaultArgumentParserResult(command, currentParameter, arguments,
rawArguments.Slice(currentPosition + 1).IndexOf(expectedQuote) == -1
? DefaultArgumentParserFailure.UnexpectedQuote
: DefaultArgumentParserFailure.NoWhitespaceBetweenArguments, currentPosition);
}
currentQuote = character;
whitespaceSeparated = false;
continue;
}
}
else
{
if (character == '\\' && currentPosition + 1 < rawArguments.Length &&
rawArguments[currentPosition + 1] == expectedQuote)
{
isEscaping = true;
continue;
}
if (character == expectedQuote)
{
currentQuote = '\0';
expectedQuote = '\0';
NextParameter(command, ref currentParameter, argumentBuilder, ref arguments);
continue;
}
}
argumentBuilder.Append(character);
whitespaceSeparated = false;
}
if (currentQuote != '\0')
{
return new DefaultArgumentParserResult(command, currentParameter, arguments,
DefaultArgumentParserFailure.UnclosedQuote, rawArguments.LastIndexOf(currentQuote));
}
if (currentParameter != null)
{
NextParameter(command, ref currentParameter, argumentBuilder, ref arguments);
}
if (arguments == null && command.Parameters.Count > 0)
{
arguments = new Dictionary<Parameter, object>(command.Parameters.Count);
}
if (arguments != null && arguments.Count != command.Parameters.Count)
{
for (var i = arguments.Count; i < command.Parameters.Count; i++)
{
var parameter = command.Parameters[i];
if (!parameter.IsOptional)
{
return new DefaultArgumentParserResult(command, parameter, arguments,
DefaultArgumentParserFailure.TooFewArguments, null);
}
}
}
return new DefaultArgumentParserResult(command, arguments);
}
private static void NextParameter(Command command, ref Parameter currentParameter,
StringBuilder argumentBuilder,
ref Dictionary<Parameter, object> arguments)
{
if (arguments == null)
{
arguments = new Dictionary<Parameter, object>(command.Parameters.Count);
}
if (!currentParameter.IsMultiple)
{
arguments.Add(currentParameter, argumentBuilder.ToString());
}
else
{
if (arguments.TryGetValue(currentParameter, out var list))
{
(list as List<object>).Add(argumentBuilder.ToString());
}
else
{
arguments.Add(currentParameter, new List<object> {argumentBuilder.ToString()});
}
}
argumentBuilder.Clear();
currentParameter = null;
}
}
}