Skip to content

Commit f23308a

Browse files
authored
Relax parsing of labels (#1591)
1 parent 83f7a8d commit f23308a

File tree

2 files changed

+33
-50
lines changed

2 files changed

+33
-50
lines changed

parser/src/command.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,14 @@ impl<'a> Command<'a> {
210210
#[test]
211211
fn errors_outside_command_are_fine() {
212212
let input =
213-
"haha\" unterminated quotes @bot modify labels: +bug. Terminating after the command";
213+
"haha\" unterminated quotes @bot labels +bug. Terminating after the command";
214214
let mut input = Input::new(input, vec!["bot"]);
215215
assert!(input.next().unwrap().is_ok());
216216
}
217217

218218
#[test]
219219
fn code_1() {
220-
let input = "`@bot modify labels: +bug.`";
220+
let input = "`@bot modify label: +bug.`";
221221
let mut input = Input::new(input, vec!["bot"]);
222222
assert!(input.next().is_none());
223223
}
@@ -235,23 +235,23 @@ fn code_2() {
235235
fn edit_1() {
236236
let input_old = "@bot modify labels: +bug.";
237237
let mut input_old = Input::new(input_old, vec!["bot"]);
238-
let input_new = "Adding labels: @bot modify labels: +bug. some other text";
238+
let input_new = "Adding labels: @bot modify label +bug. some other text";
239239
let mut input_new = Input::new(input_new, vec!["bot"]);
240240
assert_eq!(input_old.next(), input_new.next());
241241
}
242242

243243
#[test]
244244
fn edit_2() {
245-
let input_old = "@bot modify label: +bug.";
245+
let input_old = "@bot label bug.";
246246
let mut input_old = Input::new(input_old, vec!["bot"]);
247-
let input_new = "@bot modify labels: +bug.";
247+
let input_new = "@bot modify labels to: +bug.";
248248
let mut input_new = Input::new(input_new, vec!["bot"]);
249-
assert_ne!(input_old.next(), input_new.next());
249+
assert_eq!(input_old.next(), input_new.next());
250250
}
251251

252252
#[test]
253253
fn move_input_along() {
254-
let input = "@bot modify labels: +bug. Afterwards, delete the world.";
254+
let input = "@bot labels: +bug. Afterwards, delete the world.";
255255
let mut input = Input::new(input, vec!["bot"]);
256256
assert!(input.next().unwrap().is_ok());
257257
assert_eq!(&input.all[input.parsed..], " Afterwards, delete the world.");
@@ -268,7 +268,7 @@ fn move_input_along_1() {
268268

269269
#[test]
270270
fn multiname() {
271-
let input = "@rustbot modify labels: +bug. Afterwards, delete the world. @triagebot prioritize";
271+
let input = "@rustbot label to: +bug. Afterwards, delete the world. @triagebot prioritize";
272272
let mut input = Input::new(input, vec!["triagebot", "rustbot"]);
273273
assert!(dbg!(input.next().unwrap()).is_ok());
274274
assert_eq!(

parser/src/command/relabel.rs

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
//! The grammar is as follows:
66
//!
77
//! ```text
8-
//! Command: `@bot modify labels:? to? <label-list>.` or `@bot label:? <label-list>.`
8+
//! Command: `@bot modify? <label-w> to? :? <label-list>.`
9+
//!
10+
//! <label-w>:
11+
//! - label
12+
//! - labels
913
//!
1014
//! <label-list>:
1115
//! - <label-delta>
@@ -46,7 +50,6 @@ pub enum ParseError {
4650
EmptyLabel,
4751
ExpectedLabelDelta,
4852
MisleadingTo,
49-
NoSeparator,
5053
}
5154

5255
impl std::error::Error for ParseError {}
@@ -57,7 +60,6 @@ impl fmt::Display for ParseError {
5760
ParseError::EmptyLabel => write!(f, "empty label"),
5861
ParseError::ExpectedLabelDelta => write!(f, "a label delta"),
5962
ParseError::MisleadingTo => write!(f, "forbidden `to`, use `+to`"),
60-
ParseError::NoSeparator => write!(f, "must have `:` or `to` as label starter"),
6163
}
6264
}
6365
}
@@ -128,28 +130,13 @@ impl RelabelCommand {
128130
pub fn parse<'a>(input: &mut Tokenizer<'a>) -> Result<Option<Self>, Error<'a>> {
129131
let mut toks = input.clone();
130132

131-
if toks.eat_token(Token::Word("modify"))? {
132-
if toks.eat_token(Token::Word("labels"))? {
133-
if toks.eat_token(Token::Colon)? {
134-
// ate the colon
135-
} else if toks.eat_token(Token::Word("to"))? {
136-
// optionally eat the colon after to, e.g.:
137-
// @rustbot modify labels to: -S-waiting-on-author, +S-waiting-on-review
138-
toks.eat_token(Token::Colon)?;
139-
} else {
140-
// It's okay if there's no to or colon, we can just eat labels
141-
// afterwards.
142-
}
143-
// continue
144-
{} // FIXME(rustfmt#4506): this is needed to get rustfmt to indent the comment correctly
145-
} else {
146-
return Ok(None);
147-
}
148-
} else if toks.eat_token(Token::Word("label"))? {
149-
// optionally eat a colon
133+
toks.eat_token(Token::Word("modify"))?;
134+
135+
if toks.eat_token(Token::Word("labels"))? || toks.eat_token(Token::Word("label"))? {
136+
toks.eat_token(Token::Word("to"))?;
150137
toks.eat_token(Token::Colon)?;
138+
151139
// continue
152-
{} // FIXME(rustfmt#4506): this is needed to get rustfmt to indent the comment correctly
153140
} else {
154141
return Ok(None);
155142
}
@@ -163,12 +150,8 @@ impl RelabelCommand {
163150
deltas.push(LabelDelta::parse(&mut toks)?);
164151

165152
// optional `, and` separator
166-
if let Some(Token::Comma) = toks.peek_token()? {
167-
toks.next_token()?;
168-
}
169-
if let Some(Token::Word("and")) = toks.peek_token()? {
170-
toks.next_token()?;
171-
}
153+
toks.eat_token(Token::Comma)?;
154+
toks.eat_token(Token::Word("and"))?;
172155

173156
if let Some(Token::Semi) | Some(Token::Dot) | Some(Token::EndOfLine) =
174157
toks.peek_token()?
@@ -258,7 +241,7 @@ fn parse_shorter_command() {
258241
#[test]
259242
fn parse_shorter_command_with_colon() {
260243
assert_eq!(
261-
parse("label: +T-compiler -T-lang bug"),
244+
parse("labels: +T-compiler -T-lang bug"),
262245
Ok(Some(vec![
263246
LabelDelta::Add(Label("T-compiler".into())),
264247
LabelDelta::Remove(Label("T-lang".into())),
@@ -270,23 +253,23 @@ fn parse_shorter_command_with_colon() {
270253
#[test]
271254
fn parse_shorter_command_with_to() {
272255
assert_eq!(
273-
parse("label to +T-compiler -T-lang bug")
274-
.unwrap_err()
275-
.source()
276-
.unwrap()
277-
.downcast_ref(),
278-
Some(&ParseError::MisleadingTo)
256+
parse("label to +T-compiler -T-lang bug"),
257+
Ok(Some(vec![
258+
LabelDelta::Add(Label("T-compiler".into())),
259+
LabelDelta::Remove(Label("T-lang".into())),
260+
LabelDelta::Add(Label("bug".into())),
261+
]))
279262
);
280263
}
281264

282265
#[test]
283266
fn parse_shorter_command_with_to_colon() {
284267
assert_eq!(
285-
parse("label to: +T-compiler -T-lang bug")
286-
.unwrap_err()
287-
.source()
288-
.unwrap()
289-
.downcast_ref(),
290-
Some(&ParseError::MisleadingTo)
268+
parse("label to: +T-compiler -T-lang bug"),
269+
Ok(Some(vec![
270+
LabelDelta::Add(Label("T-compiler".into())),
271+
LabelDelta::Remove(Label("T-lang".into())),
272+
LabelDelta::Add(Label("bug".into())),
273+
]))
291274
);
292275
}

0 commit comments

Comments
 (0)