mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-27 05:44:52 +00:00
Move range
from Attributed
to Node
s (#22)
* Move `range` from `Attributed` to `Node`s * No Attributed + custom for Range PoC * Generate all located variants, generate enum implementations * Implement `Copy` on simple enums * Move `Suite` to `ranged` and `located` * Update tests --------- Co-authored-by: Jeong YunWon <jeong@youknowone.org>
This commit is contained in:
parent
a983f4383f
commit
192379cede
126 changed files with 29410 additions and 30670 deletions
|
@ -20,35 +20,29 @@ impl<U> crate::fold::Fold<U> for ConstantOptimizer {
|
|||
Ok(user)
|
||||
}
|
||||
fn fold_expr(&mut self, node: crate::Expr<U>) -> Result<crate::Expr<U>, Self::Error> {
|
||||
match node.node {
|
||||
crate::ExprKind::Tuple(crate::ExprTuple { elts, ctx }) => {
|
||||
match node {
|
||||
crate::Expr::Tuple(crate::ExprTuple { elts, ctx, range }) => {
|
||||
let elts = elts
|
||||
.into_iter()
|
||||
.map(|x| self.fold_expr(x))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let expr = if elts
|
||||
.iter()
|
||||
.all(|e| matches!(e.node, crate::ExprKind::Constant { .. }))
|
||||
{
|
||||
let expr = if elts.iter().all(|e| e.is_constant_expr()) {
|
||||
let tuple = elts
|
||||
.into_iter()
|
||||
.map(|e| match e.node {
|
||||
crate::ExprKind::Constant(crate::ExprConstant { value, .. }) => value,
|
||||
.map(|e| match e {
|
||||
crate::Expr::Constant(crate::ExprConstant { value, .. }) => value,
|
||||
_ => unreachable!(),
|
||||
})
|
||||
.collect();
|
||||
crate::ExprKind::Constant(crate::ExprConstant {
|
||||
crate::Expr::Constant(crate::ExprConstant {
|
||||
value: Constant::Tuple(tuple),
|
||||
kind: None,
|
||||
range,
|
||||
})
|
||||
} else {
|
||||
crate::ExprKind::Tuple(crate::ExprTuple { elts, ctx })
|
||||
crate::Expr::Tuple(crate::ExprTuple { elts, ctx, range })
|
||||
};
|
||||
Ok(crate::Expr {
|
||||
node: expr,
|
||||
custom: node.custom,
|
||||
range: node.range,
|
||||
})
|
||||
Ok(expr)
|
||||
}
|
||||
_ => crate::fold::fold_expr(self, node),
|
||||
}
|
||||
|
@ -66,95 +60,68 @@ mod tests {
|
|||
use crate::{fold::Fold, *};
|
||||
|
||||
let range = TextRange::default();
|
||||
#[allow(clippy::let_unit_value)]
|
||||
let custom = ();
|
||||
let ast = Attributed {
|
||||
let ast = ExprTuple {
|
||||
ctx: ExprContext::Load,
|
||||
elts: vec![
|
||||
ExprConstant {
|
||||
value: BigInt::from(1).into(),
|
||||
kind: None,
|
||||
range,
|
||||
}
|
||||
.into(),
|
||||
ExprConstant {
|
||||
value: BigInt::from(2).into(),
|
||||
kind: None,
|
||||
range,
|
||||
}
|
||||
.into(),
|
||||
ExprTuple {
|
||||
ctx: ExprContext::Load,
|
||||
elts: vec![
|
||||
ExprConstant {
|
||||
value: BigInt::from(3).into(),
|
||||
kind: None,
|
||||
range,
|
||||
}
|
||||
.into(),
|
||||
ExprConstant {
|
||||
value: BigInt::from(4).into(),
|
||||
kind: None,
|
||||
range,
|
||||
}
|
||||
.into(),
|
||||
ExprConstant {
|
||||
value: BigInt::from(5).into(),
|
||||
kind: None,
|
||||
range,
|
||||
}
|
||||
.into(),
|
||||
],
|
||||
range,
|
||||
}
|
||||
.into(),
|
||||
],
|
||||
range,
|
||||
custom,
|
||||
node: ExprTuple {
|
||||
ctx: ExprContext::Load,
|
||||
elts: vec![
|
||||
Attributed {
|
||||
range,
|
||||
custom,
|
||||
node: ExprConstant {
|
||||
value: BigInt::from(1).into(),
|
||||
kind: None,
|
||||
}
|
||||
.into(),
|
||||
},
|
||||
Attributed {
|
||||
range,
|
||||
custom,
|
||||
node: ExprConstant {
|
||||
value: BigInt::from(2).into(),
|
||||
kind: None,
|
||||
}
|
||||
.into(),
|
||||
},
|
||||
Attributed {
|
||||
range,
|
||||
custom,
|
||||
node: ExprTuple {
|
||||
ctx: ExprContext::Load,
|
||||
elts: vec![
|
||||
Attributed {
|
||||
range,
|
||||
custom,
|
||||
node: ExprConstant {
|
||||
value: BigInt::from(3).into(),
|
||||
kind: None,
|
||||
}
|
||||
.into(),
|
||||
},
|
||||
Attributed {
|
||||
range,
|
||||
custom,
|
||||
node: ExprConstant {
|
||||
value: BigInt::from(4).into(),
|
||||
kind: None,
|
||||
}
|
||||
.into(),
|
||||
},
|
||||
Attributed {
|
||||
range,
|
||||
custom,
|
||||
node: ExprConstant {
|
||||
value: BigInt::from(5).into(),
|
||||
kind: None,
|
||||
}
|
||||
.into(),
|
||||
},
|
||||
],
|
||||
}
|
||||
.into(),
|
||||
},
|
||||
],
|
||||
}
|
||||
.into(),
|
||||
};
|
||||
let new_ast = ConstantOptimizer::new()
|
||||
.fold_expr(ast)
|
||||
.fold_expr(ast.into())
|
||||
.unwrap_or_else(|e| match e {});
|
||||
assert_eq!(
|
||||
new_ast,
|
||||
Attributed {
|
||||
ExprConstant {
|
||||
value: Constant::Tuple(vec![
|
||||
BigInt::from(1).into(),
|
||||
BigInt::from(2).into(),
|
||||
Constant::Tuple(vec![
|
||||
BigInt::from(3).into(),
|
||||
BigInt::from(4).into(),
|
||||
BigInt::from(5).into(),
|
||||
])
|
||||
]),
|
||||
kind: None,
|
||||
range,
|
||||
custom,
|
||||
node: ExprConstant {
|
||||
value: Constant::Tuple(vec![
|
||||
BigInt::from(1).into(),
|
||||
BigInt::from(2).into(),
|
||||
Constant::Tuple(vec![
|
||||
BigInt::from(3).into(),
|
||||
BigInt::from(4).into(),
|
||||
BigInt::from(5).into(),
|
||||
])
|
||||
]),
|
||||
kind: None
|
||||
}
|
||||
.into(),
|
||||
}
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue