MCPcopy Index your code
hub / github.com/RustPython/RustPython / unparse_expr

Method unparse_expr

crates/codegen/src/unparse.rs:59–477  ·  view source on GitHub ↗
(&mut self, ast: &ast::Expr, level: u8)

Source from the content-addressed store, hash-verified

57 }
58
59 fn unparse_expr(&mut self, ast: &ast::Expr, level: u8) -> fmt::Result {
60 macro_rules! op_prec {
61 ($op_ty:ident, $x:expr, $enu:path, $($var:ident($op:literal, $prec:ident)),*$(,)?) => {
62 match $x {
63 $(<$enu>::$var => (op_prec!(@space $op_ty, $op), precedence::$prec),)*
64 }
65 };
66 (@space bin, $op:literal) => {
67 concat!(" ", $op, " ")
68 };
69 (@space un, $op:literal) => {
70 $op
71 };
72 }
73 macro_rules! group_if {
74 ($lvl:expr, $body:block) => {{
75 let group = level > $lvl;
76 self.p_if(group, "(")?;
77 let ret = $body;
78 self.p_if(group, ")")?;
79 ret
80 }};
81 }
82 match &ast {
83 ast::Expr::BoolOp(ast::ExprBoolOp {
84 op,
85 values,
86 node_index: _,
87 range: _range,
88 }) => {
89 let (op, prec) = op_prec!(bin, op, ast::BoolOp, And("and", AND), Or("or", OR));
90 group_if!(prec, {
91 let mut first = true;
92 for val in values {
93 self.p_delim(&mut first, op)?;
94 self.unparse_expr(val, prec + 1)?;
95 }
96 })
97 }
98 ast::Expr::Named(ast::ExprNamed {
99 target,
100 value,
101 node_index: _,
102 range: _range,
103 }) => {
104 group_if!(precedence::TUPLE, {
105 self.unparse_expr(target, precedence::ATOM)?;
106 self.p(" := ")?;
107 self.unparse_expr(value, precedence::ATOM)?;
108 })
109 }
110 ast::Expr::BinOp(ast::ExprBinOp {
111 left,
112 op,
113 right,
114 node_index: _,
115 range: _range,
116 }) => {

Callers 3

unparse_compMethod · 0.80
unparse_formattedMethod · 0.80
fmtMethod · 0.80

Calls 15

p_delimMethod · 0.80
unparse_compMethod · 0.80
p_idMethod · 0.80
unparse_fstringMethod · 0.80
unparse_tstringMethod · 0.80
str_reprMethod · 0.80
to_strMethod · 0.80
bytes_reprMethod · 0.80
to_stringFunction · 0.50
pMethod · 0.45
fmtMethod · 0.45
as_refMethod · 0.45

Tested by

no test coverage detected