Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax: cleanup param, method, and misc parsing #64910

Merged
merged 21 commits into from
Oct 2, 2019
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 30 additions & 44 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,40 +547,38 @@ impl<'a> Parser<'a> {
}
}

crate fn check_ident(&mut self) -> bool {
if self.token.is_ident() {
fn check_or_expected(&mut self, ok: bool, mk_type: impl FnOnce() -> TokenType) -> bool {
Centril marked this conversation as resolved.
Show resolved Hide resolved
if ok {
true
} else {
self.expected_tokens.push(TokenType::Ident);
self.expected_tokens.push(mk_type());
false
}
}

crate fn check_ident(&mut self) -> bool {
self.check_or_expected(self.token.is_ident(), || TokenType::Ident)
}

fn check_path(&mut self) -> bool {
if self.token.is_path_start() {
true
} else {
self.expected_tokens.push(TokenType::Path);
false
}
self.check_or_expected(self.token.is_path_start(), || TokenType::Path)
}

fn check_type(&mut self) -> bool {
if self.token.can_begin_type() {
true
} else {
self.expected_tokens.push(TokenType::Type);
false
}
self.check_or_expected(self.token.can_begin_type(), || TokenType::Type)
}

fn check_const_arg(&mut self) -> bool {
if self.token.can_begin_const_arg() {
true
} else {
self.expected_tokens.push(TokenType::Const);
false
}
self.check_or_expected(self.token.can_begin_const_arg(), || TokenType::Const)
}

/// Checks to see if the next token is either `+` or `+=`.
/// Otherwise returns `false`.
fn check_plus(&mut self) -> bool {
self.check_or_expected(
self.token.is_like_plus(),
|| TokenType::Token(token::BinOp(token::Plus)),
)
}

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

/// Checks to see if the next token is either `+` or `+=`.
/// Otherwise returns `false`.
fn check_plus(&mut self) -> bool {
if self.token.is_like_plus() {
true
}
else {
self.expected_tokens.push(TokenType::Token(token::BinOp(token::Plus)));
false
}
}

/// Expects and consumes an `&`. If `&&` is seen, replaces it with a single
/// `&` and continues. If an `&` is not seen, signals an error.
fn expect_and(&mut self) -> PResult<'a, ()> {
Expand Down Expand Up @@ -910,15 +896,13 @@ impl<'a> Parser<'a> {
self.expected_tokens.clear();
}

pub fn look_ahead<R, F>(&self, dist: usize, f: F) -> R where
F: FnOnce(&Token) -> R,
{
pub fn look_ahead<R>(&self, dist: usize, looker: impl FnOnce(&Token) -> R) -> R {
if dist == 0 {
return f(&self.token);
return looker(&self.token);
}

let frame = &self.token_cursor.frame;
f(&match frame.tree_cursor.look_ahead(dist - 1) {
looker(&match frame.tree_cursor.look_ahead(dist - 1) {
Some(tree) => match tree {
TokenTree::Token(token) => token,
TokenTree::Delimited(dspan, delim, _) =>
Expand Down Expand Up @@ -1008,9 +992,10 @@ impl<'a> Parser<'a> {
Ok((delim, tts.into()))
}

fn parse_or_use_outer_attributes(&mut self,
already_parsed_attrs: Option<ThinVec<Attribute>>)
-> PResult<'a, ThinVec<Attribute>> {
fn parse_or_use_outer_attributes(
&mut self,
already_parsed_attrs: Option<ThinVec<Attribute>>,
) -> PResult<'a, ThinVec<Attribute>> {
if let Some(attrs) = already_parsed_attrs {
Ok(attrs)
} else {
Expand Down Expand Up @@ -1539,9 +1524,10 @@ impl<'a> Parser<'a> {
}
}

fn collect_tokens<F, R>(&mut self, f: F) -> PResult<'a, (R, TokenStream)>
where F: FnOnce(&mut Self) -> PResult<'a, R>
{
fn collect_tokens<R>(
&mut self,
f: impl FnOnce(&mut Self) -> PResult<'a, R>,
) -> PResult<'a, (R, TokenStream)> {
// Record all tokens we parse when parsing this item.
let mut tokens = Vec::new();
let prev_collecting = match self.token_cursor.frame.last_token {
Expand Down