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

Using parse_parens, parse_braces, parse_brackets instead of macros. #1519

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 10 additions & 7 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,21 +628,24 @@ pub(crate) mod parsing {
}

pub(crate) fn single_parse_inner(input: ParseStream) -> Result<Attribute> {
let content;
let pound_token = input.parse()?;
let style = AttrStyle::Inner(input.parse()?);
let Brackets { token: bracket_token, content } = parse_brackets(input)?;
Ok(Attribute {
pound_token: input.parse()?,
style: AttrStyle::Inner(input.parse()?),
bracket_token: bracketed!(content in input),
pound_token,
style,
bracket_token,
meta: content.parse()?,
})
}

pub(crate) fn single_parse_outer(input: ParseStream) -> Result<Attribute> {
let content;
let pound_token = input.parse()?;
let Brackets { token: bracket_token, content } = parse_brackets(input)?;
Ok(Attribute {
pound_token: input.parse()?,
pound_token,
style: AttrStyle::Outer,
bracket_token: bracketed!(content in input),
bracket_token,
meta: content.parse()?,
})
}
Expand Down
9 changes: 5 additions & 4 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ ast_struct! {
pub(crate) mod parsing {
use super::*;
use crate::ext::IdentExt;
use crate::group::{Parens, parse_parens};
use crate::parse::{Parse, ParseStream, Result};

#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
Expand Down Expand Up @@ -191,9 +192,9 @@ pub(crate) mod parsing {
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
impl Parse for FieldsNamed {
fn parse(input: ParseStream) -> Result<Self> {
let content;
let Braces { token: brace_token, content } = parse_braces(input)?;
Ok(FieldsNamed {
brace_token: braced!(content in input),
brace_token,
named: content.parse_terminated(Field::parse_named, Token![,])?,
})
}
Expand All @@ -202,9 +203,9 @@ pub(crate) mod parsing {
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
impl Parse for FieldsUnnamed {
fn parse(input: ParseStream) -> Result<Self> {
let content;
let Parens { token: paren_token, content } = parse_parens(input)?;
Ok(FieldsUnnamed {
paren_token: parenthesized!(content in input),
paren_token,
unnamed: content.parse_terminated(Field::parse_unnamed, Token![,])?,
})
}
Expand Down
3 changes: 1 addition & 2 deletions src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ pub(crate) mod parsing {
)> {
let where_clause = input.parse()?;

let content;
let brace = braced!(content in input);
let Braces { token: brace, content } = parse_braces(input)?;
let variants = content.parse_terminated(Variant::parse, Token![,])?;

Ok((where_clause, brace, variants))
Expand Down
69 changes: 28 additions & 41 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ pub(crate) mod parsing {
use crate::parse::{Parse, ParseStream, Result};
use crate::path;
use std::cmp::Ordering;

use crate::group::{Parens, parse_parens};
mod kw {
crate::custom_keyword!(builtin);
crate::custom_keyword!(raw);
Expand Down Expand Up @@ -1437,11 +1437,11 @@ pub(crate) mod parsing {
fn trailer_helper(input: ParseStream, mut e: Expr) -> Result<Expr> {
loop {
if input.peek(token::Paren) {
let content;
let Parens { token: paren_token, content } = parse_parens(input)?;
e = Expr::Call(ExprCall {
attrs: Vec::new(),
func: Box::new(e),
paren_token: parenthesized!(content in input),
paren_token,
args: content.parse_terminated(Expr::parse, Token![,])?,
});
} else if input.peek(Token![.])
Expand Down Expand Up @@ -1480,14 +1480,14 @@ pub(crate) mod parsing {

if turbofish.is_some() || input.peek(token::Paren) {
if let Member::Named(method) = member {
let content;
let Parens { token: paren_token, content } = parse_parens(input)?;
e = Expr::MethodCall(ExprMethodCall {
attrs: Vec::new(),
receiver: Box::new(e),
dot_token,
method,
turbofish,
paren_token: parenthesized!(content in input),
paren_token,
args: content.parse_terminated(Expr::parse, Token![,])?,
});
continue;
Expand All @@ -1501,11 +1501,11 @@ pub(crate) mod parsing {
member,
});
} else if input.peek(token::Bracket) {
let content;
let Brackets { token: bracket_token, content } = parse_brackets(input)?;
e = Expr::Index(ExprIndex {
attrs: Vec::new(),
expr: Box::new(e),
bracket_token: bracketed!(content in input),
bracket_token,
index: content.parse()?,
});
} else if input.peek(Token![?]) {
Expand All @@ -1527,11 +1527,11 @@ pub(crate) mod parsing {

loop {
if input.peek(token::Paren) {
let content;
let Parens { token: paren_token, content } = parse_parens(input)?;
e = Expr::Call(ExprCall {
attrs: Vec::new(),
func: Box::new(e),
paren_token: parenthesized!(content in input),
paren_token,
args: content.parse_terminated(Expr::parse, Token![,])?,
});
} else if input.peek(Token![.])
Expand All @@ -1552,11 +1552,11 @@ pub(crate) mod parsing {
member: input.parse()?,
});
} else if input.peek(token::Bracket) {
let content;
let Brackets { token: bracket_token, content } = parse_brackets(input)?;
e = Expr::Index(ExprIndex {
attrs: Vec::new(),
expr: Box::new(e),
bracket_token: bracketed!(content in input),
bracket_token,
index: content.parse()?,
});
} else {
Expand Down Expand Up @@ -1693,8 +1693,7 @@ pub(crate) mod parsing {
} else {
if input.peek(token::Brace) {
let scan = input.fork();
let content;
braced!(content in scan);
let Braces { token: _, content } = parse_braces(&scan)?;
if content.parse::<Expr>().is_ok() && content.is_empty() {
let expr_block = verbatim::between(input, &scan);
input.advance_to(&scan);
Expand All @@ -1713,8 +1712,7 @@ pub(crate) mod parsing {
input.parse::<Token![#]>()?;
input.parse::<Ident>()?;

let args;
parenthesized!(args in input);
let Parens { token: _, content: args } = parse_parens(input)?;
args.parse::<TokenStream>()?;

Ok(Expr::Verbatim(verbatim::between(&begin, input)))
Expand Down Expand Up @@ -1768,8 +1766,7 @@ pub(crate) mod parsing {

#[cfg(feature = "full")]
fn paren_or_tuple(input: ParseStream) -> Result<Expr> {
let content;
let paren_token = parenthesized!(content in input);
let Parens { token: paren_token, content } = parse_parens(input)?;
if content.is_empty() {
return Ok(Expr::Tuple(ExprTuple {
attrs: Vec::new(),
Expand Down Expand Up @@ -1807,8 +1804,7 @@ pub(crate) mod parsing {

#[cfg(feature = "full")]
fn array_or_repeat(input: ParseStream) -> Result<Expr> {
let content;
let bracket_token = bracketed!(content in input);
let Brackets { token: bracket_token, content } = parse_brackets(input)?;
if content.is_empty() {
return Ok(Expr::Array(ExprArray {
attrs: Vec::new(),
Expand Down Expand Up @@ -1854,8 +1850,7 @@ pub(crate) mod parsing {
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
impl Parse for ExprArray {
fn parse(input: ParseStream) -> Result<Self> {
let content;
let bracket_token = bracketed!(content in input);
let Brackets { token: bracket_token, content } = parse_brackets(input)?;
let mut elems = Punctuated::new();

while !content.is_empty() {
Expand All @@ -1880,9 +1875,9 @@ pub(crate) mod parsing {
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
impl Parse for ExprRepeat {
fn parse(input: ParseStream) -> Result<Self> {
let content;
let Brackets { token: bracket_token, content } = parse_brackets(input)?;
Ok(ExprRepeat {
bracket_token: bracketed!(content in input),
bracket_token,
attrs: Vec::new(),
expr: content.parse()?,
semi_token: content.parse()?,
Expand Down Expand Up @@ -1967,10 +1962,10 @@ pub(crate) mod parsing {
}

fn expr_paren(input: ParseStream) -> Result<ExprParen> {
let content;
let Parens { token: paren_token, content } = parse_parens(input)?;
Ok(ExprParen {
attrs: Vec::new(),
paren_token: parenthesized!(content in input),
paren_token,
expr: content.parse()?,
})
}
Expand Down Expand Up @@ -2058,8 +2053,7 @@ pub(crate) mod parsing {
let in_token: Token![in] = input.parse()?;
let expr: Expr = input.call(Expr::parse_without_eager_brace)?;

let content;
let brace_token = braced!(content in input);
let Braces { token: brace_token, content } = parse_braces(input)?;
attr::parsing::parse_inner(&content, &mut attrs)?;
let stmts = content.call(Block::parse_within)?;

Expand All @@ -2083,8 +2077,7 @@ pub(crate) mod parsing {
let label: Option<Label> = input.parse()?;
let loop_token: Token![loop] = input.parse()?;

let content;
let brace_token = braced!(content in input);
let Braces { token: brace_token, content } = parse_braces(input)?;
attr::parsing::parse_inner(&content, &mut attrs)?;
let stmts = content.call(Block::parse_within)?;

Expand All @@ -2105,8 +2098,7 @@ pub(crate) mod parsing {
let match_token: Token![match] = input.parse()?;
let expr = Expr::parse_without_eager_brace(input)?;

let content;
let brace_token = braced!(content in input);
let Braces { token: brace_token, content } = parse_braces(input)?;
attr::parsing::parse_inner(&content, &mut attrs)?;

let mut arms = Vec::new();
Expand Down Expand Up @@ -2371,8 +2363,7 @@ pub(crate) mod parsing {
let while_token: Token![while] = input.parse()?;
let cond = Expr::parse_without_eager_brace(input)?;

let content;
let brace_token = braced!(content in input);
let Braces { token: brace_token, content } = parse_braces(input)?;
attr::parsing::parse_inner(&content, &mut attrs)?;
let stmts = content.call(Block::parse_within)?;

Expand All @@ -2392,8 +2383,7 @@ pub(crate) mod parsing {
fn parse(input: ParseStream) -> Result<Self> {
let const_token: Token![const] = input.parse()?;

let content;
let brace_token = braced!(content in input);
let Braces { token: brace_token, content } = parse_braces(input)?;
let inner_attrs = content.call(Attribute::parse_inner)?;
let stmts = content.call(Block::parse_within)?;

Expand Down Expand Up @@ -2527,8 +2517,7 @@ pub(crate) mod parsing {
qself: Option<QSelf>,
path: Path,
) -> Result<ExprStruct> {
let content;
let brace_token = braced!(content in input);
let Braces { token: brace_token, content } = parse_braces(input)?;

let mut fields = Punctuated::new();
while !content.is_empty() {
Expand Down Expand Up @@ -2573,8 +2562,7 @@ pub(crate) mod parsing {
fn parse(input: ParseStream) -> Result<Self> {
let unsafe_token: Token![unsafe] = input.parse()?;

let content;
let brace_token = braced!(content in input);
let Braces { token: brace_token, content } = parse_braces(input)?;
let inner_attrs = content.call(Attribute::parse_inner)?;
let stmts = content.call(Block::parse_within)?;

Expand All @@ -2593,8 +2581,7 @@ pub(crate) mod parsing {
let mut attrs = input.call(Attribute::parse_outer)?;
let label: Option<Label> = input.parse()?;

let content;
let brace_token = braced!(content in input);
let Braces { token: brace_token, content } = parse_braces(input)?;
attr::parsing::parse_inner(&content, &mut attrs)?;
let stmts = content.call(Block::parse_within)?;

Expand Down
5 changes: 4 additions & 1 deletion src/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ ast_struct! {
pub(crate) mod parsing {
use super::*;
use crate::ext::IdentExt;
use crate::group::{Parens, parse_parens};
use crate::parse::{Parse, ParseStream, Result};

#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
Expand Down Expand Up @@ -755,7 +756,9 @@ pub(crate) mod parsing {

let content;
let (paren_token, content) = if input.peek(token::Paren) {
(Some(parenthesized!(content in input)), &content)
let Parens { token, content: parens_content } = parse_parens(input)?;
content = parens_content;
(Some(token), &content)
} else {
(None, input)
};
Expand Down
6 changes: 0 additions & 6 deletions src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::token;
use proc_macro2::extra::DelimSpan;
use proc_macro2::Delimiter;

// Not public API.
#[doc(hidden)]
pub struct Parens<'a> {
#[doc(hidden)]
Expand All @@ -13,7 +12,6 @@ pub struct Parens<'a> {
pub content: ParseBuffer<'a>,
}

// Not public API.
#[doc(hidden)]
pub struct Braces<'a> {
#[doc(hidden)]
Expand All @@ -22,7 +20,6 @@ pub struct Braces<'a> {
pub content: ParseBuffer<'a>,
}

// Not public API.
#[doc(hidden)]
pub struct Brackets<'a> {
#[doc(hidden)]
Expand All @@ -41,7 +38,6 @@ pub struct Group<'a> {
pub content: ParseBuffer<'a>,
}

// Not public API.
#[doc(hidden)]
pub fn parse_parens<'a>(input: &ParseBuffer<'a>) -> Result<Parens<'a>> {
parse_delimited(input, Delimiter::Parenthesis).map(|(span, content)| Parens {
Expand All @@ -50,7 +46,6 @@ pub fn parse_parens<'a>(input: &ParseBuffer<'a>) -> Result<Parens<'a>> {
})
}

// Not public API.
#[doc(hidden)]
pub fn parse_braces<'a>(input: &ParseBuffer<'a>) -> Result<Braces<'a>> {
parse_delimited(input, Delimiter::Brace).map(|(span, content)| Braces {
Expand All @@ -59,7 +54,6 @@ pub fn parse_braces<'a>(input: &ParseBuffer<'a>) -> Result<Braces<'a>> {
})
}

// Not public API.
#[doc(hidden)]
pub fn parse_brackets<'a>(input: &ParseBuffer<'a>) -> Result<Brackets<'a>> {
parse_delimited(input, Delimiter::Bracket).map(|(span, content)| Brackets {
Expand Down
Loading