mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-08 05:35:22 +00:00
basic field accessors
This commit is contained in:
parent
2baad9ead6
commit
706ac3364c
5 changed files with 42 additions and 1 deletions
|
@ -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"
|
||||
|
|
|
@ -19,4 +19,5 @@ visitor = []
|
|||
rustpython-parser-core = { workspace = true }
|
||||
rustpython-literal = { workspace = true, optional = true }
|
||||
|
||||
is-macro = { workspace = true }
|
||||
num-bigint = { workspace = true }
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -114,7 +114,7 @@ impl std::cmp::PartialEq<usize> 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<String> for Constant {
|
||||
fn from(s: String) -> Constant {
|
||||
Self::Str(s)
|
||||
|
@ -247,3 +262,14 @@ impl<T, U> std::ops::Deref for Attributed<T, U> {
|
|||
&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());
|
||||
}
|
||||
}
|
|
@ -47,6 +47,7 @@ impl<U> From<ModFunctionType<U>> for Mod<U> {
|
|||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "is_macro", derive(is_macro::Is))]
|
||||
pub enum Mod<U = ()> {
|
||||
Module(ModModule<U>),
|
||||
Interactive(ModInteractive<U>),
|
||||
|
@ -367,6 +368,7 @@ impl<U> From<StmtExpr<U>> for StmtKind<U> {
|
|||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "is_macro", derive(is_macro::Is))]
|
||||
pub enum StmtKind<U = ()> {
|
||||
FunctionDef(StmtFunctionDef<U>),
|
||||
AsyncFunctionDef(StmtAsyncFunctionDef<U>),
|
||||
|
@ -727,6 +729,7 @@ impl<U> From<ExprSlice<U>> for ExprKind<U> {
|
|||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "is_macro", derive(is_macro::Is))]
|
||||
pub enum ExprKind<U = ()> {
|
||||
BoolOp(ExprBoolOp<U>),
|
||||
NamedExpr(ExprNamedExpr<U>),
|
||||
|
@ -759,6 +762,7 @@ pub enum ExprKind<U = ()> {
|
|||
pub type Expr<U = ()> = Attributed<ExprKind<U>, 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<U> From<ExcepthandlerExceptHandler<U>> for ExcepthandlerKind<U> {
|
|||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "is_macro", derive(is_macro::Is))]
|
||||
pub enum ExcepthandlerKind<U = ()> {
|
||||
ExceptHandler(ExcepthandlerExceptHandler<U>),
|
||||
}
|
||||
|
@ -978,6 +987,7 @@ impl<U> From<PatternMatchOr<U>> for PatternKind<U> {
|
|||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "is_macro", derive(is_macro::Is))]
|
||||
pub enum PatternKind<U = ()> {
|
||||
MatchValue(PatternMatchValue<U>),
|
||||
MatchSingleton(PatternMatchSingleton),
|
||||
|
@ -1003,6 +1013,7 @@ impl From<TypeIgnoreTypeIgnore> for TypeIgnore {
|
|||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "is_macro", derive(is_macro::Is))]
|
||||
pub enum TypeIgnore {
|
||||
TypeIgnore(TypeIgnoreTypeIgnore),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue