-
Notifications
You must be signed in to change notification settings - Fork 559
/
parse.rs
263 lines (216 loc) · 7.35 KB
/
parse.rs
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use super::{Command, CommandGroup, Configuration};
use crate::client::Context;
use crate::model::channel::Message;
use uwl::{StrExt, StringStream};
#[derive(Debug, Clone, PartialEq)]
pub enum Prefix<'a> {
Punct(&'a str),
Mention(&'a str),
None,
#[doc(hidden)]
__Nonexhaustive,
}
pub fn parse_prefix<'a>(
ctx: &mut Context,
msg: &'a Message,
config: &Configuration,
) -> (Prefix<'a>, &'a str) {
let mut stream = StringStream::new(&msg.content);
stream.take_while(|s| s.is_whitespace());
if let Some(ref mention) = config.on_mention {
if let Ok(id) = stream.parse("<@(!){}>") {
if id.is_numeric() && mention == id {
stream.take_while(|s| s.is_whitespace());
return (Prefix::Mention(id), stream.rest());
}
}
}
let mut prefix = None;
if !config.prefixes.is_empty() || !config.dynamic_prefixes.is_empty() {
for f in &config.dynamic_prefixes {
if let Some(p) = f(ctx, msg) {
let pp = stream.peek_for(p.chars().count());
if p == pp {
prefix = Some(pp);
break;
}
}
}
for p in &config.prefixes {
// If `dynamic_prefixes` succeeded, don't iterate through the normal prefixes.
if prefix.is_some() {
break;
}
let pp = stream.peek_for(p.chars().count());
if p == pp {
prefix = Some(pp);
break;
}
}
}
if let Some(prefix) = prefix {
stream.increment(prefix.len());
if config.with_whitespace.prefixes {
stream.take_while(|s| s.is_whitespace());
}
let args = stream.rest();
return (Prefix::Punct(prefix), args.trim());
}
if config.with_whitespace.prefixes {
stream.take_while(|s| s.is_whitespace());
}
let args = stream.rest();
(Prefix::None, args.trim())
}
#[derive(Debug, Clone, Copy, PartialEq)]
enum ParseMode {
BySpace,
ByLength,
}
struct CommandParser<'msg, 'groups, 'config> {
stream: StringStream<'msg>,
groups: &'groups [&'static CommandGroup],
config: &'config Configuration,
mode: ParseMode,
unrecognised: Option<&'msg str>,
}
impl<'msg, 'groups, 'config> CommandParser<'msg, 'groups, 'config> {
fn new(stream: StringStream<'msg>, groups: &'groups [&'static CommandGroup], config: &'config Configuration) -> Self {
CommandParser {
stream,
groups,
config,
mode: if config.by_space { ParseMode::BySpace } else { ParseMode::ByLength },
unrecognised: None,
}
}
fn next_text(&self, length: impl FnOnce() -> usize) -> &'msg str {
match self.mode {
ParseMode::ByLength => self.stream.peek_for(length()),
ParseMode::BySpace => self.stream.peek_until(|s| s.is_whitespace()),
}
}
fn as_lowercase(&self, s: &str, f: impl FnOnce(&str) -> bool) -> bool {
if self.config.case_insensitive {
let s = s.to_lowercase();
f(&s)
} else {
f(s)
}
}
fn command(&mut self, command: &'static Command) -> Option<&'static Command> {
for name in command.options.names {
// FIXME: If `by_space` option is set true, we shouldn't be retrieving the block of text
// again and again for the command name.
let n = self.next_text(|| name.chars().count());
let equals = self.as_lowercase(n, |n| n == *name && !self.config.disabled_commands.contains(n));
if equals {
self.stream.increment(n.len());
if self.config.with_whitespace.commands {
self.stream.take_while(|s| s.is_whitespace());
}
for sub in command.options.sub_commands {
if let Some(cmd) = self.command(sub) {
self.unrecognised = None;
return Some(cmd);
}
}
self.unrecognised = None;
return Some(command);
}
self.unrecognised = Some(n);
}
None
}
fn group(&mut self, group: &'static CommandGroup) -> (Option<&'msg str>, &'static CommandGroup) {
for p in group.options.prefixes {
let pp = self.next_text(|| p.chars().count());
if *p == pp {
self.stream.increment(pp.len());
if self.config.with_whitespace.groups {
self.stream.take_while(|s| s.is_whitespace());
}
for sub_group in group.sub_groups {
let x = self.group(*sub_group);
if x.0.is_some() {
return x;
}
}
return (Some(pp), group);
}
}
(None, group)
}
fn parse(mut self, prefix: Prefix<'msg>) -> Result<Invoke<'msg>, Option<&'msg str>> {
let pos = self.stream.offset();
for group in self.groups {
let (gprefix, group) = self.group(*group);
if gprefix.is_none() && !group.options.prefixes.is_empty() {
unsafe { self.stream.set_unchecked(pos) };
continue;
}
for command in group.commands {
if let Some(command) = self.command(command) {
return Ok(Invoke::Command {
prefix,
group,
gprefix,
command,
args: self.stream.rest(),
});
}
}
// Only execute the default command if a group prefix is present.
if let Some(command) = group.options.default_command {
if gprefix.is_some() {
return Ok(Invoke::Command {
prefix,
group,
gprefix,
command,
args: self.stream.rest(),
});
}
}
unsafe { self.stream.set_unchecked(pos) };
}
Err(self.unrecognised)
}
}
pub(crate) fn parse_command<'a>(
msg: &'a str,
prefix: Prefix<'a>,
groups: &[&'static CommandGroup],
config: &Configuration,
help_was_set: Option<&[&'static str]>,
) -> Result<Invoke<'a>, Option<&'a str>> {
let mut stream = StringStream::new(msg);
stream.take_while(|s| s.is_whitespace());
// We take precedence over commands named help command's name.
if let Some(names) = help_was_set {
for name in names {
if stream.eat(name) {
stream.take_while(|s| s.is_whitespace());
let args = stream.rest();
return Ok(Invoke::Help { prefix, name, args });
}
}
}
CommandParser::new(stream, groups, config).parse(prefix)
}
#[derive(Debug)]
pub enum Invoke<'a> {
Command {
prefix: Prefix<'a>,
// Group prefix
gprefix: Option<&'a str>,
group: &'static CommandGroup,
command: &'static Command,
args: &'a str,
},
Help {
prefix: Prefix<'a>,
name: &'static str,
args: &'a str,
},
}