Skip to content

Commit 5640be2

Browse files
authored
Merge pull request #186 from alexcrichton/yield
Support for `yield` expressions
2 parents 5aae682 + fe11046 commit 5640be2

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

src/expr.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,14 @@ ast_enum_of_structs! {
346346
pub catch_token: tokens::Catch,
347347
pub block: Block,
348348
}),
349+
350+
/// A yield expression.
351+
///
352+
/// E.g. `yield expr`
353+
pub Yield(ExprYield #full {
354+
pub yield_token: tokens::Yield,
355+
pub expr: Option<Box<Expr>>,
356+
}),
349357
}
350358
}
351359

@@ -1164,6 +1172,8 @@ pub mod parsing {
11641172
|
11651173
syn!(ExprCatch) => { ExprKind::Catch }
11661174
|
1175+
syn!(ExprYield) => { ExprKind::Yield }
1176+
|
11671177
call!(expr_closure, allow_struct)
11681178
|
11691179
cond_reduce!(allow_block, map!(syn!(ExprBlock), ExprKind::Block))
@@ -1208,6 +1218,8 @@ pub mod parsing {
12081218
|
12091219
syn!(ExprCatch) => { ExprKind::Catch }
12101220
|
1221+
syn!(ExprYield) => { ExprKind::Yield }
1222+
|
12111223
syn!(ExprBlock) => { ExprKind::Block }
12121224
), Expr::from));
12131225

@@ -1472,6 +1484,18 @@ pub mod parsing {
14721484
));
14731485
}
14741486

1487+
#[cfg(feature = "full")]
1488+
impl Synom for ExprYield {
1489+
named!(parse -> Self, do_parse!(
1490+
yield_: syn!(Yield) >>
1491+
expr: option!(syn!(Expr)) >>
1492+
(ExprYield {
1493+
yield_token: yield_,
1494+
expr: expr.map(Box::new),
1495+
})
1496+
));
1497+
}
1498+
14751499
#[cfg(feature = "full")]
14761500
fn arm_requires_comma(arm: &Arm) -> bool {
14771501
if let ExprKind::Block(ExprBlock { unsafety: Unsafety::Normal, .. }) = arm.body.node {
@@ -2448,6 +2472,14 @@ mod printing {
24482472
}
24492473
}
24502474

2475+
#[cfg(feature = "full")]
2476+
impl ToTokens for ExprYield {
2477+
fn to_tokens(&self, tokens: &mut Tokens) {
2478+
self.yield_token.to_tokens(tokens);
2479+
self.expr.to_tokens(tokens);
2480+
}
2481+
}
2482+
24512483
#[cfg(feature = "full")]
24522484
impl ToTokens for ExprClosure {
24532485
fn to_tokens(&self, tokens: &mut Tokens) {

src/fold.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,13 @@ pub fn noop_fold_expr<F: ?Sized + Folder>(folder: &mut F, Expr { node, attrs }:
892892
})
893893
}
894894
#[cfg(feature = "full")]
895+
Yield(e) => {
896+
Yield(ExprYield {
897+
expr: e.expr.map(|e| e.lift(|e| folder.fold_expr(e))),
898+
..e
899+
})
900+
}
901+
#[cfg(feature = "full")]
895902
Closure(e) => {
896903
Closure(ExprClosure {
897904
decl: e.decl.lift(|v| folder.fold_fn_decl(v)),

src/visit.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,12 @@ pub fn walk_expr<V: Visitor>(visitor: &mut V, expr: &Expr) {
591591
visitor.visit_expr(amt);
592592
}
593593
#[cfg(feature = "full")]
594+
Yield(ExprYield { ref expr, .. }) => {
595+
if let Some(ref expr) = *expr {
596+
visitor.visit_expr(expr);
597+
}
598+
}
599+
#[cfg(feature = "full")]
594600
TupField(ExprTupField { ref expr, .. }) |
595601
Unary(ExprUnary { ref expr, .. }) |
596602
Box(ExprBox { ref expr, .. }) |

synom/src/tokens.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ tokens! {
190190
(pub struct Use => "use"),
191191
(pub struct Where => "where"),
192192
(pub struct While => "while"),
193+
(pub struct Yield => "yield"),
193194
}
194195
}
195196

0 commit comments

Comments
 (0)