Skip to content

Commit 5b80ead

Browse files
committed
syntax: misc cleanup
1 parent d9d0e5d commit 5b80ead

File tree

1 file changed

+30
-44
lines changed

1 file changed

+30
-44
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -547,40 +547,38 @@ impl<'a> Parser<'a> {
547547
}
548548
}
549549

550-
crate fn check_ident(&mut self) -> bool {
551-
if self.token.is_ident() {
550+
fn check_or_expected(&mut self, ok: bool, mk_type: impl FnOnce() -> TokenType) -> bool {
551+
if ok {
552552
true
553553
} else {
554-
self.expected_tokens.push(TokenType::Ident);
554+
self.expected_tokens.push(mk_type());
555555
false
556556
}
557557
}
558558

559+
crate fn check_ident(&mut self) -> bool {
560+
self.check_or_expected(self.token.is_ident(), || TokenType::Ident)
561+
}
562+
559563
fn check_path(&mut self) -> bool {
560-
if self.token.is_path_start() {
561-
true
562-
} else {
563-
self.expected_tokens.push(TokenType::Path);
564-
false
565-
}
564+
self.check_or_expected(self.token.is_path_start(), || TokenType::Path)
566565
}
567566

568567
fn check_type(&mut self) -> bool {
569-
if self.token.can_begin_type() {
570-
true
571-
} else {
572-
self.expected_tokens.push(TokenType::Type);
573-
false
574-
}
568+
self.check_or_expected(self.token.can_begin_type(), || TokenType::Type)
575569
}
576570

577571
fn check_const_arg(&mut self) -> bool {
578-
if self.token.can_begin_const_arg() {
579-
true
580-
} else {
581-
self.expected_tokens.push(TokenType::Const);
582-
false
583-
}
572+
self.check_or_expected(self.token.can_begin_const_arg(), || TokenType::Const)
573+
}
574+
575+
/// Checks to see if the next token is either `+` or `+=`.
576+
/// Otherwise returns `false`.
577+
fn check_plus(&mut self) -> bool {
578+
self.check_or_expected(
579+
self.token.is_like_plus(),
580+
|| TokenType::Token(token::BinOp(token::Plus)),
581+
)
584582
}
585583

586584
/// Expects and consumes a `+`. if `+=` is seen, replaces it with a `=`
@@ -604,18 +602,6 @@ impl<'a> Parser<'a> {
604602
}
605603
}
606604

607-
/// Checks to see if the next token is either `+` or `+=`.
608-
/// Otherwise returns `false`.
609-
fn check_plus(&mut self) -> bool {
610-
if self.token.is_like_plus() {
611-
true
612-
}
613-
else {
614-
self.expected_tokens.push(TokenType::Token(token::BinOp(token::Plus)));
615-
false
616-
}
617-
}
618-
619605
/// Expects and consumes an `&`. If `&&` is seen, replaces it with a single
620606
/// `&` and continues. If an `&` is not seen, signals an error.
621607
fn expect_and(&mut self) -> PResult<'a, ()> {
@@ -910,15 +896,13 @@ impl<'a> Parser<'a> {
910896
self.expected_tokens.clear();
911897
}
912898

913-
pub fn look_ahead<R, F>(&self, dist: usize, f: F) -> R where
914-
F: FnOnce(&Token) -> R,
915-
{
899+
pub fn look_ahead<R>(&self, dist: usize, looker: impl FnOnce(&Token) -> R) -> R {
916900
if dist == 0 {
917-
return f(&self.token);
901+
return looker(&self.token);
918902
}
919903

920904
let frame = &self.token_cursor.frame;
921-
f(&match frame.tree_cursor.look_ahead(dist - 1) {
905+
looker(&match frame.tree_cursor.look_ahead(dist - 1) {
922906
Some(tree) => match tree {
923907
TokenTree::Token(token) => token,
924908
TokenTree::Delimited(dspan, delim, _) =>
@@ -1008,9 +992,10 @@ impl<'a> Parser<'a> {
1008992
Ok((delim, tts.into()))
1009993
}
1010994

1011-
fn parse_or_use_outer_attributes(&mut self,
1012-
already_parsed_attrs: Option<ThinVec<Attribute>>)
1013-
-> PResult<'a, ThinVec<Attribute>> {
995+
fn parse_or_use_outer_attributes(
996+
&mut self,
997+
already_parsed_attrs: Option<ThinVec<Attribute>>,
998+
) -> PResult<'a, ThinVec<Attribute>> {
1014999
if let Some(attrs) = already_parsed_attrs {
10151000
Ok(attrs)
10161001
} else {
@@ -1539,9 +1524,10 @@ impl<'a> Parser<'a> {
15391524
}
15401525
}
15411526

1542-
fn collect_tokens<F, R>(&mut self, f: F) -> PResult<'a, (R, TokenStream)>
1543-
where F: FnOnce(&mut Self) -> PResult<'a, R>
1544-
{
1527+
fn collect_tokens<R>(
1528+
&mut self,
1529+
f: impl FnOnce(&mut Self) -> PResult<'a, R>,
1530+
) -> PResult<'a, (R, TokenStream)> {
15451531
// Record all tokens we parse when parsing this item.
15461532
let mut tokens = Vec::new();
15471533
let prev_collecting = match self.token_cursor.frame.last_token {

0 commit comments

Comments
 (0)