fn syntex_brackets(syntex_expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
    use syntex_syntax::ast::{Expr, ExprKind, Mac, Stmt, StmtKind, Pat, Ty, Field};
    use syntex_syntax::fold::{self, Folder};
    use syntex_syntax::util::ThinVec;
    use syntex_syntax::util::small_vector::SmallVector;
    use syntex_syntax::ext::quote::rt::DUMMY_SP;
    use syntex_syntax::codemap;

    fn expr(node: ExprKind) -> P<Expr> {
        P(Expr {
            id: ast::DUMMY_NODE_ID,
            node: node,
            span: DUMMY_SP,
            attrs: ThinVec::new(),
        })
    }

    struct BracketsFolder {
        failed: bool,
    };
    impl Folder for BracketsFolder {
        fn fold_expr(&mut self, e: P<Expr>) -> P<Expr> {
            e.map(|e| {
                Expr {
                    node: match e.node {
                        ExprKind::Paren(inner) => {
                            ExprKind::Paren(inner.map(|e| {
                                fold::noop_fold_expr(e, self)
                            }))
                        }
                        ExprKind::If(..) |
                        ExprKind::Block(..) |
                        ExprKind::IfLet(..) => {
                            return fold::noop_fold_expr(e, self);
                        }
                        node => {
                            ExprKind::Paren(expr(node).map(|e| {
                                fold::noop_fold_expr(e, self)
                            }))
                        }
                    },
                    ..e
                }
            })
        }

        fn fold_field(&mut self, f: Field) -> Field {
            Field {
                ident: codemap::respan(f.ident.span, self.fold_ident(f.ident.node)),
                expr: if f.is_shorthand {
                    f.expr.map(|e| fold::noop_fold_expr(e, self))
                } else {
                    self.fold_expr(f.expr)
                },
                span: self.new_span(f.span),
                is_shorthand: f.is_shorthand,
                attrs: fold::fold_thin_attrs(f.attrs, self),
            }
        }

        // We don't want to look at expressions that might appear in patterns or
        // types yet. We'll look into comparing those in the future. For now
        // focus on expressions appearing in other places.
        fn fold_pat(&mut self, pat: P<Pat>) -> P<Pat> {
            pat
        }

        fn fold_ty(&mut self, ty: P<Ty>) -> P<Ty> {
            ty
        }

        fn fold_stmt(&mut self, stmt: Stmt) -> SmallVector<Stmt> {
            let node = match stmt.node {
                // Don't wrap toplevel expressions in statements.
                StmtKind::Expr(e) => {
                    StmtKind::Expr(e.map(|e| fold::noop_fold_expr(e, self)))
                }
                StmtKind::Semi(e) => {
                    StmtKind::Semi(e.map(|e| fold::noop_fold_expr(e, self)))
                }
                s => s,
            };

            SmallVector::one(Stmt {
                node: node,
                ..stmt
            })
        }

        fn fold_mac(&mut self, mac: Mac) -> Mac {
            // By default when folding over macros, syntex panics. This is
            // because it's usually not what you want, you want to run after
            // macro expansion. We do want to do that (syn doesn't do macro
            // expansion), so we implement fold_mac to just return the macro
            // unchanged.
            mac
        }
    }

    let mut folder = BracketsFolder {
        failed: false,
    };
    let e = folder.fold_expr(syntex_expr);
    if folder.failed {
        None
    } else {
        Some(e)
    }
}
/// Wrap every expression which is not already wrapped in parens with parens, to
/// reveal the precidence of the parsed expressions, and produce a stringified form
/// of the resulting expression.
///
/// This method operates on syntex objects.
Wrap every expression which is not already wrapped in parens with parens, to
reveal the precidence of the parsed expressions, and produce a stringified form
of the resulting expression.

This method operates on syntex objects.
Wrap every expression which is not already wrapped in parens with parens to reveal the precidence of the parsed expressions and produce a stringified form of the resulting expression . This method operates on syntex objects .
fn syntex_brackets(syntex_expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
    use syntex_syntax::ast::{Expr, ExprKind, Mac, Stmt, StmtKind, Pat, Ty, Field};
    use syntex_syntax::fold::{self, Folder};
    use syntex_syntax::util::ThinVec;
    use syntex_syntax::util::small_vector::SmallVector;
    use syntex_syntax::ext::quote::rt::DUMMY_SP;
    use syntex_syntax::codemap;
    fn expr(node: ExprKind) -> P<Expr> {
        P(Expr {
            id: ast::DUMMY_NODE_ID,
            node: node,
            span: DUMMY_SP,
            attrs: ThinVec::new(),
        })
    }
    struct BracketsFolder {
        failed: bool,
    };
    impl Folder for BracketsFolder {
        fn fold_expr(&mut self, e: P<Expr>) -> P<Expr> {
            e.map(|e| {
                Expr {
                    node: match e.node {
                        ExprKind::Paren(inner) => {
                            ExprKind::Paren(inner.map(|e| {
                                fold::noop_fold_expr(e, self)
                            }))
                        }
                        ExprKind::If(..) |
                        ExprKind::Block(..) |
                        ExprKind::IfLet(..) => {
                            return fold::noop_fold_expr(e, self);
                        }
                        node => {
                            ExprKind::Paren(expr(node).map(|e| {
                                fold::noop_fold_expr(e, self)
                            }))
                        }
                    },
                    ..e
                }
            })
        }
        fn fold_field(&mut self, f: Field) -> Field {
            Field {
                ident: codemap::respan(f.ident.span, self.fold_ident(f.ident.node)),
                expr: if f.is_shorthand {
                    f.expr.map(|e| fold::noop_fold_expr(e, self))
                } else {
                    self.fold_expr(f.expr)
                },
                span: self.new_span(f.span),
                is_shorthand: f.is_shorthand,
                attrs: fold::fold_thin_attrs(f.attrs, self),
            }
        }
        // We don't want to look at expressions that might appear in patterns or
        // types yet. We'll look into comparing those in the future. For now
        // focus on expressions appearing in other places.
        fn fold_pat(&mut self, pat: P<Pat>) -> P<Pat> {
            pat
        }
        fn fold_ty(&mut self, ty: P<Ty>) -> P<Ty> {
            ty
        }
        fn fold_stmt(&mut self, stmt: Stmt) -> SmallVector<Stmt> {
            let node = match stmt.node {
                // Don't wrap toplevel expressions in statements.
                StmtKind::Expr(e) => {
                    StmtKind::Expr(e.map(|e| fold::noop_fold_expr(e, self)))
                }
                StmtKind::Semi(e) => {
                    StmtKind::Semi(e.map(|e| fold::noop_fold_expr(e, self)))
                }
                s => s,
            };
            SmallVector::one(Stmt {
                node: node,
                ..stmt
            })
        }
        fn fold_mac(&mut self, mac: Mac) -> Mac {
            // By default when folding over macros, syntex panics. This is
            // because it's usually not what you want, you want to run after
            // macro expansion. We do want to do that (syn doesn't do macro
            // expansion), so we implement fold_mac to just return the macro
            // unchanged.
            mac
        }
    }
    let mut folder = BracketsFolder {
        failed: false,
    };
    let e = folder.fold_expr(syntex_expr);
    if folder.failed {
        None
    } else {
        Some(e)
    }
}
fn syntex_brackets ( syntex_expr : P < ast :: Expr > ) -> Option < P < ast :: Expr > > { use syntex_syntax :: ast :: { Expr , ExprKind , Mac , Stmt , StmtKind , Pat , Ty , Field } ; use syntex_syntax :: fold :: { self , Folder } ; use syntex_syntax :: util :: ThinVec ; use syntex_syntax :: util :: small_vector :: SmallVector ; use syntex_syntax :: ext :: quote :: rt :: DUMMY_SP ; use syntex_syntax :: codemap ; fn expr ( node : ExprKind ) -> P < Expr > { P ( Expr { id : ast :: DUMMY_NODE_ID , node : node , span : DUMMY_SP , attrs : ThinVec :: new ( ) , } ) } struct BracketsFolder { failed : bool , } ; impl Folder for BracketsFolder { fn fold_expr ( & mut self , e : P < Expr > ) -> P < Expr > { e . map ( | e | { Expr { node : match e . node { ExprKind :: Paren ( inner ) => { ExprKind :: Paren ( inner . map ( | e | { fold :: noop_fold_expr ( e , self ) } ) ) } ExprKind :: If ( .. ) | ExprKind :: Block ( .. ) | ExprKind :: IfLet ( .. ) => { return fold :: noop_fold_expr ( e , self ) ; } node => { ExprKind :: Paren ( expr ( node ) . map ( | e | { fold :: noop_fold_expr ( e , self ) } ) ) } } , .. e } } ) } fn fold_field ( & mut self , f : Field ) -> Field { Field { ident : codemap :: respan ( f . ident . span , self . fold_ident ( f . ident . node ) ) , expr : if f . is_shorthand { f . expr . map ( | e | fold :: noop_fold_expr ( e , self ) ) } else { self . fold_expr ( f . expr ) } , span : self . new_span ( f . span ) , is_shorthand : f . is_shorthand , attrs : fold :: fold_thin_attrs ( f . attrs , self ) , } } // We don't want to look at expressions that might appear in patterns or // types yet. We'll look into comparing those in the future. For now // focus on expressions appearing in other places. fn fold_pat ( & mut self , pat : P < Pat > ) -> P < Pat > { pat } fn fold_ty ( & mut self , ty : P < Ty > ) -> P < Ty > { ty } fn fold_stmt ( & mut self , stmt : Stmt ) -> SmallVector < Stmt > { let node = match stmt . node { // Don't wrap toplevel expressions in statements. StmtKind :: Expr ( e ) => { StmtKind :: Expr ( e . map ( | e | fold :: noop_fold_expr ( e , self ) ) ) } StmtKind :: Semi ( e ) => { StmtKind :: Semi ( e . map ( | e | fold :: noop_fold_expr ( e , self ) ) ) } s => s , } ; SmallVector :: one ( Stmt { node : node , .. stmt } ) } fn fold_mac ( & mut self , mac : Mac ) -> Mac { // By default when folding over macros, syntex panics. This is // because it's usually not what you want, you want to run after // macro expansion. We do want to do that (syn doesn't do macro // expansion), so we implement fold_mac to just return the macro // unchanged. mac } } let mut folder = BracketsFolder { failed : false , } ; let e = folder . fold_expr ( syntex_expr ) ; if folder . failed { None } else { Some ( e ) } }
Wrap every expression which is not already wrapped in parens with parens, to
reveal the precidence of the parsed expressions, and produce a stringified form
of the resulting expression.
Wrap every expression which is not already wrapped in parens with parens to reveal the precidence of the parsed expressions and produce a stringified form of the resulting expression .
syntex_expr
P<ast::Expr>
syntex_expr
P<ast::Expr>