(&'a self, v: &'a Expr<T>)
| 1207 | } |
| 1208 | |
| 1209 | pub fn doc_expr<'a, T: AstInfo>(&'a self, v: &'a Expr<T>) -> RcDoc<'a> { |
| 1210 | match v { |
| 1211 | Expr::Op { op, expr1, expr2 } => { |
| 1212 | if let Some(expr2) = expr2 { |
| 1213 | RcDoc::concat([ |
| 1214 | self.doc_expr(expr1), |
| 1215 | RcDoc::line(), |
| 1216 | RcDoc::text(format!("{} ", op)), |
| 1217 | self.doc_expr(expr2).nest(TAB), |
| 1218 | ]) |
| 1219 | } else { |
| 1220 | // See the AstDisplay `Expr::Op` comment (`prefix_operand_needs_parens`): |
| 1221 | // a prefix op binds tighter than `COLLATE`/the binary ops but |
| 1222 | // looser than the postfix `::`/`[…]`, and `- <number>` folds, so |
| 1223 | // peel the tight postfixes and parenthesize when the chain |
| 1224 | // bottoms out at a numeric literal or a non-self-delimiting / |
| 1225 | // `COLLATE` operand. |
| 1226 | let needs_parens = { |
| 1227 | let mut e = expr1.as_ref(); |
| 1228 | let mut saw_postfix = false; |
| 1229 | loop { |
| 1230 | match e { |
| 1231 | Expr::Cast { expr, .. } | Expr::Subscript { expr, .. } => { |
| 1232 | saw_postfix = true; |
| 1233 | e = expr.as_ref(); |
| 1234 | } |
| 1235 | Expr::Value(Value::Number(_)) => break saw_postfix, |
| 1236 | // Another prefix operator stacks directly (no |
| 1237 | // re-association, no `- <number>` fold) — safe, |
| 1238 | // and avoids exploding deep unary chains. |
| 1239 | Expr::Op { expr2: None, .. } | Expr::Not { .. } => break false, |
| 1240 | Expr::Value(_) |
| 1241 | | Expr::Identifier(_) |
| 1242 | | Expr::QualifiedWildcard(_) |
| 1243 | | Expr::Parameter(_) |
| 1244 | | Expr::Function(_) |
| 1245 | | Expr::HomogenizingFunction { .. } |
| 1246 | | Expr::NullIf { .. } |
| 1247 | | Expr::Subquery(_) |
| 1248 | | Expr::Exists(_) |
| 1249 | | Expr::Nested(_) |
| 1250 | | Expr::Array(_) |
| 1251 | | Expr::ArraySubquery(_) |
| 1252 | | Expr::List(_) |
| 1253 | | Expr::ListSubquery(_) |
| 1254 | | Expr::Map(_) |
| 1255 | | Expr::MapSubquery(_) |
| 1256 | | Expr::Case { .. } |
| 1257 | | Expr::Row { .. } => break false, |
| 1258 | _ => break true, |
| 1259 | } |
| 1260 | } |
| 1261 | }; |
| 1262 | let operand = if needs_parens { |
| 1263 | bracket("(", self.doc_expr(expr1), ")") |
| 1264 | } else { |
| 1265 | self.doc_expr(expr1) |
| 1266 | }; |
no test coverage detected