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

add yeet syntax support #1164

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
61 changes: 61 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ ast_enum_of_structs! {
/// A while loop: `while expr { ... }`.
While(ExprWhile),

/// A `yeet`, with an optional value to be yote.
Yeet(ExprYeet),

/// A yield expression: `yield expr`.
Yield(ExprYield),

Expand Down Expand Up @@ -768,6 +771,19 @@ ast_struct! {
}
}

ast_struct! {
/// A `do yeet`, with an optional value to be yote.
///
/// *This type is available only if Syn is built with the `"full"` feature.*
#[cfg_attr(doc_cfg, doc(cfg(feature = "full")))]
pub struct ExprYeet #full {
pub attrs: Vec<Attribute>,
pub do_token: Token![do],
pub yeet_token: Token![yeet],
pub expr: Option<Box<Expr>>,
}
}

ast_struct! {
/// A yield expression: `yield expr`.
///
Expand Down Expand Up @@ -832,6 +848,7 @@ impl Expr {
| Expr::Async(ExprAsync { attrs, .. })
| Expr::Await(ExprAwait { attrs, .. })
| Expr::TryBlock(ExprTryBlock { attrs, .. })
| Expr::Yeet(ExprYeet { attrs, .. })
| Expr::Yield(ExprYield { attrs, .. }) => mem::replace(attrs, new),
Expr::Verbatim(_) => Vec::new(),

Expand Down Expand Up @@ -1742,6 +1759,8 @@ pub(crate) mod parsing {
input.parse().map(Expr::Continue)
} else if input.peek(Token![return]) {
expr_ret(input, allow_struct).map(Expr::Return)
} else if input.peek(Token![do]) && input.peek2(Token![yeet]) {
expr_yeet(input, allow_struct).map(Expr::Yeet)
} else if input.peek(token::Bracket) {
array_or_repeat(input)
} else if input.peek(Token![let]) {
Expand Down Expand Up @@ -2391,6 +2410,15 @@ pub(crate) mod parsing {
}
}

#[cfg(feature = "full")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
impl Parse for ExprYeet {
fn parse(input: ParseStream) -> Result<Self> {
let allow_struct = AllowStruct(true);
expr_yeet(input, allow_struct)
}
}

#[cfg(feature = "full")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
impl Parse for ExprTryBlock {
Expand Down Expand Up @@ -2625,6 +2653,28 @@ pub(crate) mod parsing {
})
}

#[cfg(feature = "full")]
fn expr_yeet(input: ParseStream, allow_struct: AllowStruct) -> Result<ExprYeet> {
Ok(ExprYeet {
attrs: Vec::new(),
do_token: input.parse()?,
yeet_token: input.parse()?,
expr: {
if input.is_empty() || input.peek(Token![,]) || input.peek(Token![;]) {
None
} else {
// NOTE: yeet is greedy and eats blocks after it even when in a
// position where structs are not allowed, such as in if statement
// conditions. For example:
//
// if yeet { println!("A") } {} // Prints "A"
let expr = ambiguous_expr(input, allow_struct)?;
Some(Box::new(expr))
}
},
})
}

#[cfg(feature = "full")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
impl Parse for FieldValue {
Expand Down Expand Up @@ -3428,6 +3478,17 @@ pub(crate) mod printing {
}
}

#[cfg(feature = "full")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
impl ToTokens for ExprYeet {
fn to_tokens(&self, tokens: &mut TokenStream) {
outer_attrs_to_tokens(&self.attrs, tokens);
self.do_token.to_tokens(tokens);
self.yeet_token.to_tokens(tokens);
self.expr.to_tokens(tokens);
}
}

#[cfg(feature = "full")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
impl ToTokens for ExprMacro {
Expand Down
14 changes: 14 additions & 0 deletions src/gen/clone.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions src/gen/debug.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/gen/eq.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions src/gen/fold.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion src/gen/hash.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions src/gen/visit.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions src/gen/visit_mut.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn accept_as_ident(ident: &Ident) -> bool {
"mod" | "move" | "mut" | "override" | "priv" | "pub" | "ref" |
"return" | "Self" | "self" | "static" | "struct" | "super" | "trait" |
"true" | "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" |
"where" | "while" | "yield" => false,
"where" | "while" | "yeet" | "yield" => false,
_ => true,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ pub use crate::expr::{
ExprBox, ExprBreak, ExprCall, ExprCast, ExprClosure, ExprContinue, ExprField, ExprForLoop,
ExprGroup, ExprIf, ExprIndex, ExprLet, ExprLit, ExprLoop, ExprMacro, ExprMatch, ExprMethodCall,
ExprParen, ExprPath, ExprRange, ExprReference, ExprRepeat, ExprReturn, ExprStruct, ExprTry,
ExprTryBlock, ExprTuple, ExprType, ExprUnary, ExprUnsafe, ExprWhile, ExprYield, Index, Member,
ExprTryBlock, ExprTuple, ExprType, ExprUnary, ExprUnsafe, ExprWhile, ExprYeet, ExprYield, Index, Member,
};

#[cfg(any(feature = "full", feature = "derive"))]
Expand Down
2 changes: 2 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ define_keywords! {
"virtual" pub struct Virtual /// `virtual`
"where" pub struct Where /// `where`
"while" pub struct While /// `while`
"yeet" pub struct Yeet /// `yeet`
"yield" pub struct Yield /// `yield`
}

Expand Down Expand Up @@ -807,6 +808,7 @@ macro_rules! export_token_macro {
[pub] => { $crate::token::Pub };
[ref] => { $crate::token::Ref };
[return] => { $crate::token::Return };
[yeet] => { $crate::token::Yeet };
[Self] => { $crate::token::SelfType };
[self] => { $crate::token::SelfValue };
[static] => { $crate::token::Static };
Expand Down
Loading