Skip to content

Commit b1d9071

Browse files
committed
Deduplicate group parser
1 parent c89a02f commit b1d9071

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/parse.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -219,24 +219,21 @@ fn token_kind(input: Cursor) -> PResult<TokenTree> {
219219
}
220220

221221
fn group(input: Cursor) -> PResult<Group> {
222-
if let Ok(input) = input.expect("(") {
223-
let (input, ts) = token_stream(input)?;
224-
let input = skip_whitespace(input);
225-
let input = input.expect(")")?;
226-
Ok((input, Group::new(Delimiter::Parenthesis, ts)))
227-
} else if let Ok(input) = input.expect("[") {
228-
let (input, ts) = token_stream(input)?;
229-
let input = skip_whitespace(input);
230-
let input = input.expect("]")?;
231-
Ok((input, Group::new(Delimiter::Bracket, ts)))
232-
} else if let Ok(input) = input.expect("{") {
233-
let (input, ts) = token_stream(input)?;
234-
let input = skip_whitespace(input);
235-
let input = input.expect("}")?;
236-
Ok((input, Group::new(Delimiter::Brace, ts)))
222+
let (delimiter, close) = if input.starts_with("(") {
223+
(Delimiter::Parenthesis, ")")
224+
} else if input.starts_with("[") {
225+
(Delimiter::Bracket, "]")
226+
} else if input.starts_with("{") {
227+
(Delimiter::Brace, "}")
237228
} else {
238-
Err(LexError)
239-
}
229+
return Err(LexError);
230+
};
231+
232+
let input = input.advance(1);
233+
let (input, ts) = token_stream(input)?;
234+
let input = skip_whitespace(input);
235+
let input = input.expect(close)?;
236+
Ok((input, Group::new(delimiter, ts)))
240237
}
241238

242239
fn symbol(input: Cursor) -> PResult<TokenTree> {

0 commit comments

Comments
 (0)