rustpython_ast/
impls.rs

1use crate::{Constant, Expr};
2
3impl<R> Expr<R> {
4    /// Returns a short name for the node suitable for use in error messages.
5    pub fn python_name(&self) -> &'static str {
6        match self {
7            Expr::BoolOp { .. } | Expr::BinOp { .. } | Expr::UnaryOp { .. } => "operator",
8            Expr::Subscript { .. } => "subscript",
9            Expr::Await { .. } => "await expression",
10            Expr::Yield { .. } | Expr::YieldFrom { .. } => "yield expression",
11            Expr::Compare { .. } => "comparison",
12            Expr::Attribute { .. } => "attribute",
13            Expr::Call { .. } => "function call",
14            Expr::Constant(crate::ExprConstant { value, .. }) => match value {
15                Constant::Str(_)
16                | Constant::Int(_)
17                | Constant::Float(_)
18                | Constant::Complex { .. }
19                | Constant::Bytes(_) => "literal",
20                Constant::Tuple(_) => "tuple",
21                Constant::Bool(b) => {
22                    if *b {
23                        "True"
24                    } else {
25                        "False"
26                    }
27                }
28                Constant::None => "None",
29                Constant::Ellipsis => "ellipsis",
30            },
31            Expr::List { .. } => "list",
32            Expr::Tuple { .. } => "tuple",
33            Expr::Dict { .. } => "dict display",
34            Expr::Set { .. } => "set display",
35            Expr::ListComp { .. } => "list comprehension",
36            Expr::DictComp { .. } => "dict comprehension",
37            Expr::SetComp { .. } => "set comprehension",
38            Expr::GeneratorExp { .. } => "generator expression",
39            Expr::Starred { .. } => "starred",
40            Expr::Slice { .. } => "slice",
41            Expr::JoinedStr(crate::ExprJoinedStr { values, .. }) => {
42                if values.iter().any(|e| e.is_joined_str_expr()) {
43                    "f-string expression"
44                } else {
45                    "literal"
46                }
47            }
48            Expr::FormattedValue { .. } => "f-string expression",
49            Expr::Name { .. } => "name",
50            Expr::Lambda { .. } => "lambda",
51            Expr::IfExp { .. } => "conditional expression",
52            Expr::NamedExpr { .. } => "named expression",
53        }
54    }
55}
56
57// TODO: make this a #[test] to avoid eq comparison
58// #[cfg(target_arch = "x86_64")]
59// static_assertions::assert_eq_size!(crate::Expr, [u8; 72]);
60// #[cfg(target_arch = "x86_64")]
61// static_assertions::assert_eq_size!(crate::Stmt, [u8; 160]);
62// #[cfg(target_arch = "x86_64")]
63// static_assertions::assert_eq_size!(crate::Pattern, [u8; 96]);
64// #[cfg(target_arch = "x86_64")]
65// static_assertions::assert_eq_size!(crate::ExceptHandler, [u8; 64]);