diff --git a/Cargo.toml b/Cargo.toml index a8ce804..4ed152b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ anyhow = "1.0.45" cfg-if = "1.0" insta = "1.14.0" itertools = "0.10.3" +is-macro = "0.2.2" log = "0.4.16" num-complex = "0.4.0" num-bigint = "0.4.3" diff --git a/ast/Cargo.toml b/ast/Cargo.toml index 78e90e1..82827c7 100644 --- a/ast/Cargo.toml +++ b/ast/Cargo.toml @@ -19,4 +19,5 @@ visitor = [] rustpython-parser-core = { workspace = true } rustpython-literal = { workspace = true, optional = true } +is-macro = { workspace = true } num-bigint = { workspace = true } diff --git a/ast/asdl_rs.py b/ast/asdl_rs.py index a6f4f86..3aeecfe 100755 --- a/ast/asdl_rs.py +++ b/ast/asdl_rs.py @@ -271,6 +271,7 @@ class StructVisitor(EmitVisitor): def simple_sum(self, sum, name, depth): rust_name = rust_type_name(name) self.emit_attrs(depth) + self.emit("#[derive(is_macro::Is)]", depth) self.emit(f"pub enum {rust_name} {{", depth) for variant in sum.types: self.emit(f"{variant.name},", depth + 1) @@ -291,6 +292,7 @@ class StructVisitor(EmitVisitor): generics, generics_applied = self.apply_generics(name, "U = ()", "U") self.emit_attrs(depth) + self.emit("#[derive(is_macro::Is)]", depth) self.emit(f"pub enum {rust_name}{suffix}{generics} {{", depth) for t in sum.types: if t.fields: diff --git a/ast/src/builtin.rs b/ast/src/builtin.rs index 3f8f1b0..dabc620 100644 --- a/ast/src/builtin.rs +++ b/ast/src/builtin.rs @@ -114,7 +114,7 @@ impl std::cmp::PartialEq for Int { } } -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, is_macro::Is)] pub enum Constant { None, Bool(bool), @@ -127,6 +127,21 @@ pub enum Constant { Ellipsis, } +impl Constant { + pub fn is_true(self) -> bool { + self.bool().map_or(false, |b| b) + } + pub fn is_false(self) -> bool { + self.bool().map_or(false, |b| !b) + } + pub fn complex(self) -> Option<(f64, f64)> { + match self { + Constant::Complex { real, imag } => Some((real, imag)), + _ => None, + } + } +} + impl From for Constant { fn from(s: String) -> Constant { Self::Str(s) @@ -247,3 +262,14 @@ impl std::ops::Deref for Attributed { &self.node } } + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_is_macro() { + let none = Constant::None; + assert!(none.is_none()); + assert!(!none.is_bool()); + } +} \ No newline at end of file diff --git a/ast/src/gen/generic.rs b/ast/src/gen/generic.rs index 908d317..a167d6e 100644 --- a/ast/src/gen/generic.rs +++ b/ast/src/gen/generic.rs @@ -47,6 +47,7 @@ impl From> for Mod { } #[derive(Clone, Debug, PartialEq)] +#[cfg_attr(feature = "is_macro", derive(is_macro::Is))] pub enum Mod { Module(ModModule), Interactive(ModInteractive), @@ -367,6 +368,7 @@ impl From> for StmtKind { } #[derive(Clone, Debug, PartialEq)] +#[cfg_attr(feature = "is_macro", derive(is_macro::Is))] pub enum StmtKind { FunctionDef(StmtFunctionDef), AsyncFunctionDef(StmtAsyncFunctionDef), @@ -727,6 +729,7 @@ impl From> for ExprKind { } #[derive(Clone, Debug, PartialEq)] +#[cfg_attr(feature = "is_macro", derive(is_macro::Is))] pub enum ExprKind { BoolOp(ExprBoolOp), NamedExpr(ExprNamedExpr), @@ -759,6 +762,7 @@ pub enum ExprKind { pub type Expr = Attributed, U>; #[derive(Clone, Debug, PartialEq)] +#[cfg_attr(feature = "is_macro", derive(is_macro::Is))] pub enum ExprContext { Load, Store, @@ -766,12 +770,14 @@ pub enum ExprContext { } #[derive(Clone, Debug, PartialEq)] +#[cfg_attr(feature = "is_macro", derive(is_macro::Is))] pub enum Boolop { And, Or, } #[derive(Clone, Debug, PartialEq)] +#[cfg_attr(feature = "is_macro", derive(is_macro::Is))] pub enum Operator { Add, Sub, @@ -789,6 +795,7 @@ pub enum Operator { } #[derive(Clone, Debug, PartialEq)] +#[cfg_attr(feature = "is_macro", derive(is_macro::Is))] pub enum Unaryop { Invert, Not, @@ -797,6 +804,7 @@ pub enum Unaryop { } #[derive(Clone, Debug, PartialEq)] +#[cfg_attr(feature = "is_macro", derive(is_macro::Is))] pub enum Cmpop { Eq, NotEq, @@ -832,6 +840,7 @@ impl From> for ExcepthandlerKind { } #[derive(Clone, Debug, PartialEq)] +#[cfg_attr(feature = "is_macro", derive(is_macro::Is))] pub enum ExcepthandlerKind { ExceptHandler(ExcepthandlerExceptHandler), } @@ -978,6 +987,7 @@ impl From> for PatternKind { } #[derive(Clone, Debug, PartialEq)] +#[cfg_attr(feature = "is_macro", derive(is_macro::Is))] pub enum PatternKind { MatchValue(PatternMatchValue), MatchSingleton(PatternMatchSingleton), @@ -1003,6 +1013,7 @@ impl From for TypeIgnore { } #[derive(Clone, Debug, PartialEq)] +#[cfg_attr(feature = "is_macro", derive(is_macro::Is))] pub enum TypeIgnore { TypeIgnore(TypeIgnoreTypeIgnore), }