Make range the first field, change conditional compilation, use assert_eq_size`

This commit is contained in:
Micha Reiser 2023-05-14 17:04:11 +02:00
parent be93244938
commit 028c04466d
No known key found for this signature in database
9 changed files with 134 additions and 164 deletions

View file

@ -17,7 +17,7 @@ members = [
[workspace.dependencies]
rustpython-ast = { path = "ast", default-features = false }
rustpython-parser-core = { path = "core" }
rustpython-parser-core = { path = "core", features = [] }
rustpython-literal = { path = "literal" }
ahash = "0.7.6"

View file

@ -280,10 +280,7 @@ class StructVisitor(EmitVisitor):
if has_attributes:
self.emit("pub range: R,", depth + 1)
else:
self.emit('#[cfg(feature = "all-nodes-with-ranges")]', depth + 1)
self.emit("pub range: R,", depth + 1)
self.emit('#[cfg(not(feature = "all-nodes-with-ranges"))]', depth + 1)
self.emit("pub range: crate::EmptyRange<R>,", depth + 1)
self.emit("pub range: crate::ranged::OptionalRange<R>,", depth + 1)
def simple_sum(self, sum, name, depth):
rust_name = rust_type_name(name)
@ -322,13 +319,13 @@ class StructVisitor(EmitVisitor):
self.emit_attrs(depth)
payload_name = f"{rust_name}{t.name}"
self.emit(f"pub struct {payload_name}<R = TextRange> {{", depth)
self.emit_range(sum_type_info.has_attributes, depth)
for f in t.fields:
self.visit(f, sum_type_info, "pub ", depth + 1, t.name)
assert sum_type_info.has_attributes == self.type_info[t.name].no_cfg(
self.type_info
)
self.emit_range(sum_type_info.has_attributes, depth)
self.emit("}", depth)
self.emit(

View file

@ -3,12 +3,9 @@
use crate::text_size::TextRange;
#[derive(Clone, Debug, PartialEq)]
pub struct ModModule<R = TextRange> {
pub range: crate::ranged::OptionalRange<R>,
pub body: Vec<Stmt<R>>,
pub type_ignores: Vec<TypeIgnore<R>>,
#[cfg(feature = "all-nodes-with-ranges")]
pub range: R,
#[cfg(not(feature = "all-nodes-with-ranges"))]
pub range: crate::EmptyRange<R>,
}
impl<R> From<ModModule<R>> for Mod<R> {
@ -19,11 +16,8 @@ impl<R> From<ModModule<R>> for Mod<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ModInteractive<R = TextRange> {
pub range: crate::ranged::OptionalRange<R>,
pub body: Vec<Stmt<R>>,
#[cfg(feature = "all-nodes-with-ranges")]
pub range: R,
#[cfg(not(feature = "all-nodes-with-ranges"))]
pub range: crate::EmptyRange<R>,
}
impl<R> From<ModInteractive<R>> for Mod<R> {
@ -34,11 +28,8 @@ impl<R> From<ModInteractive<R>> for Mod<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ModExpression<R = TextRange> {
pub range: crate::ranged::OptionalRange<R>,
pub body: Box<Expr<R>>,
#[cfg(feature = "all-nodes-with-ranges")]
pub range: R,
#[cfg(not(feature = "all-nodes-with-ranges"))]
pub range: crate::EmptyRange<R>,
}
impl<R> From<ModExpression<R>> for Mod<R> {
@ -49,12 +40,9 @@ impl<R> From<ModExpression<R>> for Mod<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ModFunctionType<R = TextRange> {
pub range: crate::ranged::OptionalRange<R>,
pub argtypes: Vec<Expr<R>>,
pub returns: Box<Expr<R>>,
#[cfg(feature = "all-nodes-with-ranges")]
pub range: R,
#[cfg(not(feature = "all-nodes-with-ranges"))]
pub range: crate::EmptyRange<R>,
}
impl<R> From<ModFunctionType<R>> for Mod<R> {
@ -73,13 +61,13 @@ pub enum Mod<R = TextRange> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtFunctionDef<R = TextRange> {
pub range: R,
pub name: Identifier,
pub args: Box<Arguments<R>>,
pub body: Vec<Stmt<R>>,
pub decorator_list: Vec<Expr<R>>,
pub returns: Option<Box<Expr<R>>>,
pub type_comment: Option<String>,
pub range: R,
}
impl<R> From<StmtFunctionDef<R>> for Stmt<R> {
@ -90,13 +78,13 @@ impl<R> From<StmtFunctionDef<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtAsyncFunctionDef<R = TextRange> {
pub range: R,
pub name: Identifier,
pub args: Box<Arguments<R>>,
pub body: Vec<Stmt<R>>,
pub decorator_list: Vec<Expr<R>>,
pub returns: Option<Box<Expr<R>>>,
pub type_comment: Option<String>,
pub range: R,
}
impl<R> From<StmtAsyncFunctionDef<R>> for Stmt<R> {
@ -107,12 +95,12 @@ impl<R> From<StmtAsyncFunctionDef<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtClassDef<R = TextRange> {
pub range: R,
pub name: Identifier,
pub bases: Vec<Expr<R>>,
pub keywords: Vec<Keyword<R>>,
pub body: Vec<Stmt<R>>,
pub decorator_list: Vec<Expr<R>>,
pub range: R,
}
impl<R> From<StmtClassDef<R>> for Stmt<R> {
@ -123,8 +111,8 @@ impl<R> From<StmtClassDef<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtReturn<R = TextRange> {
pub value: Option<Box<Expr<R>>>,
pub range: R,
pub value: Option<Box<Expr<R>>>,
}
impl<R> From<StmtReturn<R>> for Stmt<R> {
@ -135,8 +123,8 @@ impl<R> From<StmtReturn<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtDelete<R = TextRange> {
pub targets: Vec<Expr<R>>,
pub range: R,
pub targets: Vec<Expr<R>>,
}
impl<R> From<StmtDelete<R>> for Stmt<R> {
@ -147,10 +135,10 @@ impl<R> From<StmtDelete<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtAssign<R = TextRange> {
pub range: R,
pub targets: Vec<Expr<R>>,
pub value: Box<Expr<R>>,
pub type_comment: Option<String>,
pub range: R,
}
impl<R> From<StmtAssign<R>> for Stmt<R> {
@ -161,10 +149,10 @@ impl<R> From<StmtAssign<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtAugAssign<R = TextRange> {
pub range: R,
pub target: Box<Expr<R>>,
pub op: Operator,
pub value: Box<Expr<R>>,
pub range: R,
}
impl<R> From<StmtAugAssign<R>> for Stmt<R> {
@ -175,11 +163,11 @@ impl<R> From<StmtAugAssign<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtAnnAssign<R = TextRange> {
pub range: R,
pub target: Box<Expr<R>>,
pub annotation: Box<Expr<R>>,
pub value: Option<Box<Expr<R>>>,
pub simple: bool,
pub range: R,
}
impl<R> From<StmtAnnAssign<R>> for Stmt<R> {
@ -190,12 +178,12 @@ impl<R> From<StmtAnnAssign<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtFor<R = TextRange> {
pub range: R,
pub target: Box<Expr<R>>,
pub iter: Box<Expr<R>>,
pub body: Vec<Stmt<R>>,
pub orelse: Vec<Stmt<R>>,
pub type_comment: Option<String>,
pub range: R,
}
impl<R> From<StmtFor<R>> for Stmt<R> {
@ -206,12 +194,12 @@ impl<R> From<StmtFor<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtAsyncFor<R = TextRange> {
pub range: R,
pub target: Box<Expr<R>>,
pub iter: Box<Expr<R>>,
pub body: Vec<Stmt<R>>,
pub orelse: Vec<Stmt<R>>,
pub type_comment: Option<String>,
pub range: R,
}
impl<R> From<StmtAsyncFor<R>> for Stmt<R> {
@ -222,10 +210,10 @@ impl<R> From<StmtAsyncFor<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtWhile<R = TextRange> {
pub range: R,
pub test: Box<Expr<R>>,
pub body: Vec<Stmt<R>>,
pub orelse: Vec<Stmt<R>>,
pub range: R,
}
impl<R> From<StmtWhile<R>> for Stmt<R> {
@ -236,10 +224,10 @@ impl<R> From<StmtWhile<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtIf<R = TextRange> {
pub range: R,
pub test: Box<Expr<R>>,
pub body: Vec<Stmt<R>>,
pub orelse: Vec<Stmt<R>>,
pub range: R,
}
impl<R> From<StmtIf<R>> for Stmt<R> {
@ -250,10 +238,10 @@ impl<R> From<StmtIf<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtWith<R = TextRange> {
pub range: R,
pub items: Vec<Withitem<R>>,
pub body: Vec<Stmt<R>>,
pub type_comment: Option<String>,
pub range: R,
}
impl<R> From<StmtWith<R>> for Stmt<R> {
@ -264,10 +252,10 @@ impl<R> From<StmtWith<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtAsyncWith<R = TextRange> {
pub range: R,
pub items: Vec<Withitem<R>>,
pub body: Vec<Stmt<R>>,
pub type_comment: Option<String>,
pub range: R,
}
impl<R> From<StmtAsyncWith<R>> for Stmt<R> {
@ -278,9 +266,9 @@ impl<R> From<StmtAsyncWith<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtMatch<R = TextRange> {
pub range: R,
pub subject: Box<Expr<R>>,
pub cases: Vec<MatchCase<R>>,
pub range: R,
}
impl<R> From<StmtMatch<R>> for Stmt<R> {
@ -291,9 +279,9 @@ impl<R> From<StmtMatch<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtRaise<R = TextRange> {
pub range: R,
pub exc: Option<Box<Expr<R>>>,
pub cause: Option<Box<Expr<R>>>,
pub range: R,
}
impl<R> From<StmtRaise<R>> for Stmt<R> {
@ -304,11 +292,11 @@ impl<R> From<StmtRaise<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtTry<R = TextRange> {
pub range: R,
pub body: Vec<Stmt<R>>,
pub handlers: Vec<Excepthandler<R>>,
pub orelse: Vec<Stmt<R>>,
pub finalbody: Vec<Stmt<R>>,
pub range: R,
}
impl<R> From<StmtTry<R>> for Stmt<R> {
@ -319,11 +307,11 @@ impl<R> From<StmtTry<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtTryStar<R = TextRange> {
pub range: R,
pub body: Vec<Stmt<R>>,
pub handlers: Vec<Excepthandler<R>>,
pub orelse: Vec<Stmt<R>>,
pub finalbody: Vec<Stmt<R>>,
pub range: R,
}
impl<R> From<StmtTryStar<R>> for Stmt<R> {
@ -334,9 +322,9 @@ impl<R> From<StmtTryStar<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtAssert<R = TextRange> {
pub range: R,
pub test: Box<Expr<R>>,
pub msg: Option<Box<Expr<R>>>,
pub range: R,
}
impl<R> From<StmtAssert<R>> for Stmt<R> {
@ -347,8 +335,8 @@ impl<R> From<StmtAssert<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtImport<R = TextRange> {
pub names: Vec<Alias<R>>,
pub range: R,
pub names: Vec<Alias<R>>,
}
impl<R> From<StmtImport<R>> for Stmt<R> {
@ -359,10 +347,10 @@ impl<R> From<StmtImport<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtImportFrom<R = TextRange> {
pub range: R,
pub module: Option<Identifier>,
pub names: Vec<Alias<R>>,
pub level: Option<Int>,
pub range: R,
}
impl<R> From<StmtImportFrom<R>> for Stmt<R> {
@ -373,8 +361,8 @@ impl<R> From<StmtImportFrom<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtGlobal<R = TextRange> {
pub names: Vec<Identifier>,
pub range: R,
pub names: Vec<Identifier>,
}
impl<R> From<StmtGlobal<R>> for Stmt<R> {
@ -385,8 +373,8 @@ impl<R> From<StmtGlobal<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtNonlocal<R = TextRange> {
pub names: Vec<Identifier>,
pub range: R,
pub names: Vec<Identifier>,
}
impl<R> From<StmtNonlocal<R>> for Stmt<R> {
@ -397,8 +385,8 @@ impl<R> From<StmtNonlocal<R>> for Stmt<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct StmtExpr<R = TextRange> {
pub value: Box<Expr<R>>,
pub range: R,
pub value: Box<Expr<R>>,
}
impl<R> From<StmtExpr<R>> for Stmt<R> {
@ -500,9 +488,9 @@ pub enum Stmt<R = TextRange> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprBoolOp<R = TextRange> {
pub range: R,
pub op: Boolop,
pub values: Vec<Expr<R>>,
pub range: R,
}
impl<R> From<ExprBoolOp<R>> for Expr<R> {
@ -513,9 +501,9 @@ impl<R> From<ExprBoolOp<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprNamedExpr<R = TextRange> {
pub range: R,
pub target: Box<Expr<R>>,
pub value: Box<Expr<R>>,
pub range: R,
}
impl<R> From<ExprNamedExpr<R>> for Expr<R> {
@ -526,10 +514,10 @@ impl<R> From<ExprNamedExpr<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprBinOp<R = TextRange> {
pub range: R,
pub left: Box<Expr<R>>,
pub op: Operator,
pub right: Box<Expr<R>>,
pub range: R,
}
impl<R> From<ExprBinOp<R>> for Expr<R> {
@ -540,9 +528,9 @@ impl<R> From<ExprBinOp<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprUnaryOp<R = TextRange> {
pub range: R,
pub op: Unaryop,
pub operand: Box<Expr<R>>,
pub range: R,
}
impl<R> From<ExprUnaryOp<R>> for Expr<R> {
@ -553,9 +541,9 @@ impl<R> From<ExprUnaryOp<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprLambda<R = TextRange> {
pub range: R,
pub args: Box<Arguments<R>>,
pub body: Box<Expr<R>>,
pub range: R,
}
impl<R> From<ExprLambda<R>> for Expr<R> {
@ -566,10 +554,10 @@ impl<R> From<ExprLambda<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprIfExp<R = TextRange> {
pub range: R,
pub test: Box<Expr<R>>,
pub body: Box<Expr<R>>,
pub orelse: Box<Expr<R>>,
pub range: R,
}
impl<R> From<ExprIfExp<R>> for Expr<R> {
@ -580,9 +568,9 @@ impl<R> From<ExprIfExp<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprDict<R = TextRange> {
pub range: R,
pub keys: Vec<Option<Expr<R>>>,
pub values: Vec<Expr<R>>,
pub range: R,
}
impl<R> From<ExprDict<R>> for Expr<R> {
@ -593,8 +581,8 @@ impl<R> From<ExprDict<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprSet<R = TextRange> {
pub elts: Vec<Expr<R>>,
pub range: R,
pub elts: Vec<Expr<R>>,
}
impl<R> From<ExprSet<R>> for Expr<R> {
@ -605,9 +593,9 @@ impl<R> From<ExprSet<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprListComp<R = TextRange> {
pub range: R,
pub elt: Box<Expr<R>>,
pub generators: Vec<Comprehension<R>>,
pub range: R,
}
impl<R> From<ExprListComp<R>> for Expr<R> {
@ -618,9 +606,9 @@ impl<R> From<ExprListComp<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprSetComp<R = TextRange> {
pub range: R,
pub elt: Box<Expr<R>>,
pub generators: Vec<Comprehension<R>>,
pub range: R,
}
impl<R> From<ExprSetComp<R>> for Expr<R> {
@ -631,10 +619,10 @@ impl<R> From<ExprSetComp<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprDictComp<R = TextRange> {
pub range: R,
pub key: Box<Expr<R>>,
pub value: Box<Expr<R>>,
pub generators: Vec<Comprehension<R>>,
pub range: R,
}
impl<R> From<ExprDictComp<R>> for Expr<R> {
@ -645,9 +633,9 @@ impl<R> From<ExprDictComp<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprGeneratorExp<R = TextRange> {
pub range: R,
pub elt: Box<Expr<R>>,
pub generators: Vec<Comprehension<R>>,
pub range: R,
}
impl<R> From<ExprGeneratorExp<R>> for Expr<R> {
@ -658,8 +646,8 @@ impl<R> From<ExprGeneratorExp<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprAwait<R = TextRange> {
pub value: Box<Expr<R>>,
pub range: R,
pub value: Box<Expr<R>>,
}
impl<R> From<ExprAwait<R>> for Expr<R> {
@ -670,8 +658,8 @@ impl<R> From<ExprAwait<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprYield<R = TextRange> {
pub value: Option<Box<Expr<R>>>,
pub range: R,
pub value: Option<Box<Expr<R>>>,
}
impl<R> From<ExprYield<R>> for Expr<R> {
@ -682,8 +670,8 @@ impl<R> From<ExprYield<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprYieldFrom<R = TextRange> {
pub value: Box<Expr<R>>,
pub range: R,
pub value: Box<Expr<R>>,
}
impl<R> From<ExprYieldFrom<R>> for Expr<R> {
@ -694,10 +682,10 @@ impl<R> From<ExprYieldFrom<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprCompare<R = TextRange> {
pub range: R,
pub left: Box<Expr<R>>,
pub ops: Vec<Cmpop>,
pub comparators: Vec<Expr<R>>,
pub range: R,
}
impl<R> From<ExprCompare<R>> for Expr<R> {
@ -708,10 +696,10 @@ impl<R> From<ExprCompare<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprCall<R = TextRange> {
pub range: R,
pub func: Box<Expr<R>>,
pub args: Vec<Expr<R>>,
pub keywords: Vec<Keyword<R>>,
pub range: R,
}
impl<R> From<ExprCall<R>> for Expr<R> {
@ -722,10 +710,10 @@ impl<R> From<ExprCall<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprFormattedValue<R = TextRange> {
pub range: R,
pub value: Box<Expr<R>>,
pub conversion: Int,
pub format_spec: Option<Box<Expr<R>>>,
pub range: R,
}
impl<R> From<ExprFormattedValue<R>> for Expr<R> {
@ -736,8 +724,8 @@ impl<R> From<ExprFormattedValue<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprJoinedStr<R = TextRange> {
pub values: Vec<Expr<R>>,
pub range: R,
pub values: Vec<Expr<R>>,
}
impl<R> From<ExprJoinedStr<R>> for Expr<R> {
@ -748,9 +736,9 @@ impl<R> From<ExprJoinedStr<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprConstant<R = TextRange> {
pub range: R,
pub value: Constant,
pub kind: Option<String>,
pub range: R,
}
impl<R> From<ExprConstant<R>> for Expr<R> {
@ -761,10 +749,10 @@ impl<R> From<ExprConstant<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprAttribute<R = TextRange> {
pub range: R,
pub value: Box<Expr<R>>,
pub attr: Identifier,
pub ctx: ExprContext,
pub range: R,
}
impl<R> From<ExprAttribute<R>> for Expr<R> {
@ -775,10 +763,10 @@ impl<R> From<ExprAttribute<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprSubscript<R = TextRange> {
pub range: R,
pub value: Box<Expr<R>>,
pub slice: Box<Expr<R>>,
pub ctx: ExprContext,
pub range: R,
}
impl<R> From<ExprSubscript<R>> for Expr<R> {
@ -789,9 +777,9 @@ impl<R> From<ExprSubscript<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprStarred<R = TextRange> {
pub range: R,
pub value: Box<Expr<R>>,
pub ctx: ExprContext,
pub range: R,
}
impl<R> From<ExprStarred<R>> for Expr<R> {
@ -802,9 +790,9 @@ impl<R> From<ExprStarred<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprName<R = TextRange> {
pub range: R,
pub id: Identifier,
pub ctx: ExprContext,
pub range: R,
}
impl<R> From<ExprName<R>> for Expr<R> {
@ -815,9 +803,9 @@ impl<R> From<ExprName<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprList<R = TextRange> {
pub range: R,
pub elts: Vec<Expr<R>>,
pub ctx: ExprContext,
pub range: R,
}
impl<R> From<ExprList<R>> for Expr<R> {
@ -828,9 +816,9 @@ impl<R> From<ExprList<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprTuple<R = TextRange> {
pub range: R,
pub elts: Vec<Expr<R>>,
pub ctx: ExprContext,
pub range: R,
}
impl<R> From<ExprTuple<R>> for Expr<R> {
@ -841,10 +829,10 @@ impl<R> From<ExprTuple<R>> for Expr<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct ExprSlice<R = TextRange> {
pub range: R,
pub lower: Option<Box<Expr<R>>>,
pub upper: Option<Box<Expr<R>>>,
pub step: Option<Box<Expr<R>>>,
pub range: R,
}
impl<R> From<ExprSlice<R>> for Expr<R> {
@ -969,18 +957,15 @@ pub struct Comprehension<R = TextRange> {
pub iter: Expr<R>,
pub ifs: Vec<Expr<R>>,
pub is_async: bool,
#[cfg(feature = "all-nodes-with-ranges")]
pub range: R,
#[cfg(not(feature = "all-nodes-with-ranges"))]
pub range: crate::EmptyRange<R>,
pub range: crate::ranged::OptionalRange<R>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ExcepthandlerExceptHandler<R = TextRange> {
pub range: R,
pub type_: Option<Box<Expr<R>>>,
pub name: Option<Identifier>,
pub body: Vec<Stmt<R>>,
pub range: R,
}
impl<R> From<ExcepthandlerExceptHandler<R>> for Excepthandler<R> {
@ -1003,10 +988,7 @@ pub struct Arguments<R = TextRange> {
pub kw_defaults: Vec<Expr<R>>,
pub kwarg: Option<Box<Arg<R>>>,
pub defaults: Vec<Expr<R>>,
#[cfg(feature = "all-nodes-with-ranges")]
pub range: R,
#[cfg(not(feature = "all-nodes-with-ranges"))]
pub range: crate::EmptyRange<R>,
pub range: crate::ranged::OptionalRange<R>,
}
#[derive(Clone, Debug, PartialEq)]
@ -1035,10 +1017,7 @@ pub struct Alias<R = TextRange> {
pub struct Withitem<R = TextRange> {
pub context_expr: Expr<R>,
pub optional_vars: Option<Box<Expr<R>>>,
#[cfg(feature = "all-nodes-with-ranges")]
pub range: R,
#[cfg(not(feature = "all-nodes-with-ranges"))]
pub range: crate::EmptyRange<R>,
pub range: crate::ranged::OptionalRange<R>,
}
#[derive(Clone, Debug, PartialEq)]
@ -1046,16 +1025,13 @@ pub struct MatchCase<R = TextRange> {
pub pattern: Pattern<R>,
pub guard: Option<Box<Expr<R>>>,
pub body: Vec<Stmt<R>>,
#[cfg(feature = "all-nodes-with-ranges")]
pub range: R,
#[cfg(not(feature = "all-nodes-with-ranges"))]
pub range: crate::EmptyRange<R>,
pub range: crate::ranged::OptionalRange<R>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct PatternMatchValue<R = TextRange> {
pub value: Box<Expr<R>>,
pub range: R,
pub value: Box<Expr<R>>,
}
impl<R> From<PatternMatchValue<R>> for Pattern<R> {
@ -1066,8 +1042,8 @@ impl<R> From<PatternMatchValue<R>> for Pattern<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct PatternMatchSingleton<R = TextRange> {
pub value: Constant,
pub range: R,
pub value: Constant,
}
impl<R> From<PatternMatchSingleton<R>> for Pattern<R> {
@ -1078,8 +1054,8 @@ impl<R> From<PatternMatchSingleton<R>> for Pattern<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct PatternMatchSequence<R = TextRange> {
pub patterns: Vec<Pattern<R>>,
pub range: R,
pub patterns: Vec<Pattern<R>>,
}
impl<R> From<PatternMatchSequence<R>> for Pattern<R> {
@ -1090,10 +1066,10 @@ impl<R> From<PatternMatchSequence<R>> for Pattern<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct PatternMatchMapping<R = TextRange> {
pub range: R,
pub keys: Vec<Expr<R>>,
pub patterns: Vec<Pattern<R>>,
pub rest: Option<Identifier>,
pub range: R,
}
impl<R> From<PatternMatchMapping<R>> for Pattern<R> {
@ -1104,11 +1080,11 @@ impl<R> From<PatternMatchMapping<R>> for Pattern<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct PatternMatchClass<R = TextRange> {
pub range: R,
pub cls: Box<Expr<R>>,
pub patterns: Vec<Pattern<R>>,
pub kwd_attrs: Vec<Identifier>,
pub kwd_patterns: Vec<Pattern<R>>,
pub range: R,
}
impl<R> From<PatternMatchClass<R>> for Pattern<R> {
@ -1119,8 +1095,8 @@ impl<R> From<PatternMatchClass<R>> for Pattern<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct PatternMatchStar<R = TextRange> {
pub name: Option<Identifier>,
pub range: R,
pub name: Option<Identifier>,
}
impl<R> From<PatternMatchStar<R>> for Pattern<R> {
@ -1131,9 +1107,9 @@ impl<R> From<PatternMatchStar<R>> for Pattern<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct PatternMatchAs<R = TextRange> {
pub range: R,
pub pattern: Option<Box<Pattern<R>>>,
pub name: Option<Identifier>,
pub range: R,
}
impl<R> From<PatternMatchAs<R>> for Pattern<R> {
@ -1144,8 +1120,8 @@ impl<R> From<PatternMatchAs<R>> for Pattern<R> {
#[derive(Clone, Debug, PartialEq)]
pub struct PatternMatchOr<R = TextRange> {
pub patterns: Vec<Pattern<R>>,
pub range: R,
pub patterns: Vec<Pattern<R>>,
}
impl<R> From<PatternMatchOr<R>> for Pattern<R> {
@ -1168,12 +1144,9 @@ pub enum Pattern<R = TextRange> {
#[derive(Clone, Debug, PartialEq)]
pub struct TypeIgnoreTypeIgnore<R = TextRange> {
pub range: crate::ranged::OptionalRange<R>,
pub lineno: Int,
pub tag: String,
#[cfg(feature = "all-nodes-with-ranges")]
pub range: R,
#[cfg(not(feature = "all-nodes-with-ranges"))]
pub range: crate::EmptyRange<R>,
}
impl<R> From<TypeIgnoreTypeIgnore<R>> for TypeIgnore<R> {

View file

@ -1,5 +1,4 @@
use crate::{Constant, Excepthandler, Expr, Pattern, Stmt};
use static_assertions::const_assert_eq;
impl<R> Expr<R> {
/// Returns a short name for the node suitable for use in error messages.
@ -56,10 +55,10 @@ impl<R> Expr<R> {
}
#[cfg(target_arch = "x86_64")]
const_assert_eq!(std::mem::size_of::<Expr>(), 72);
static_assertions::assert_eq_size!(Expr, [u8; 72]);
#[cfg(target_arch = "x86_64")]
const_assert_eq!(std::mem::size_of::<Stmt>(), 136);
static_assertions::assert_eq_size!(Stmt, [u8; 136]);
#[cfg(target_arch = "x86_64")]
const_assert_eq!(std::mem::size_of::<Pattern>(), 96);
static_assertions::assert_eq_size!(Pattern, [u8; 96]);
#[cfg(target_arch = "x86_64")]
const_assert_eq!(std::mem::size_of::<Excepthandler>(), 64);
static_assertions::assert_eq_size!(Excepthandler, [u8; 64]);

View file

@ -16,7 +16,7 @@ mod unparse;
pub use builtin::*;
pub use generic::*;
pub use ranged::{EmptyRange, Ranged};
pub use ranged::{EmptyRange, OptionalRange, Ranged};
pub use rustpython_parser_core::{text_size, ConversionFlag};
pub type Suite<R = TextRange> = Vec<Stmt<R>>;

View file

@ -16,11 +16,26 @@ pub trait Ranged {
}
}
#[cfg(feature = "all-nodes-with-ranges")]
pub type OptionalRange<R> = R;
#[cfg(not(feature = "all-nodes-with-ranges"))]
pub type OptionalRange<R> = EmptyRange<R>;
#[derive(Eq, PartialEq, Hash, Copy, Clone)]
pub struct EmptyRange<R> {
phantom: PhantomData<R>,
}
impl<R> EmptyRange<R> {
#[inline(always)]
pub fn new(_start: TextSize, _end: TextSize) -> Self {
Self {
phantom: PhantomData,
}
}
}
impl<R> Display for EmptyRange<R> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str("()")

View file

@ -318,20 +318,6 @@ impl ParseErrorType {
}
}
#[cfg(feature = "all-nodes-with-ranges")]
#[inline(always)]
pub(super) fn range_or_empty<T: Into<crate::text_size::TextRange>>(
range: T,
) -> crate::text_size::TextRange {
range.into()
}
#[cfg(not(feature = "all-nodes-with-ranges"))]
#[inline(always)]
pub(super) fn range_or_empty<T, R>(_: T) -> rustpython_ast::EmptyRange<R> {
rustpython_ast::EmptyRange::default()
}
#[cfg(test)]
mod tests {
use super::*;

View file

@ -4,11 +4,11 @@
// See also: https://greentreesnakes.readthedocs.io/en/latest/nodes.html#keyword
use crate::{
ast::{self as ast, Ranged},
ast::{self as ast, Ranged, OptionalRange},
lexer::{LexicalError, LexicalErrorType},
function::{ArgumentList, parse_args, parse_params, validate_arguments},
context::set_context,
string::parse_strings, parser::range_or_empty,
string::parse_strings,
token::{self, StringKind},
text_size::TextSize,
};
@ -20,9 +20,9 @@ grammar;
// For each public entry point, a full parse table is generated.
// By having only a single pub function, we reduce this to one.
pub Top: ast::Mod = {
<start:@L> StartModule <body:Program> <end:@R> => ast::ModModule { body, type_ignores: vec![], range: range_or_empty(start..end) }.into(),
<start:@L> StartInteractive <body:Program> <end:@R> => ast::ModInteractive { body, range: range_or_empty(start..end) }.into(),
<start:@L> StartExpression <body:TestList> ("\n")* <end:@R> => ast::ModExpression { body: Box::new(body), range: range_or_empty(start..end) }.into()
<start:@L> StartModule <body:Program> <end:@R> => ast::ModModule { body, type_ignores: vec![], range: OptionalRange::new(start, end) }.into(),
<start:@L> StartInteractive <body:Program> <end:@R> => ast::ModInteractive { body, range: OptionalRange::new(start, end) }.into(),
<start:@L> StartExpression <body:TestList> ("\n")* <end:@R> => ast::ModExpression { body: Box::new(body), range: OptionalRange::new(start, end) }.into()
};
Program: ast::Suite = {
@ -371,7 +371,7 @@ MatchCase: ast::MatchCase = {
pattern,
guard: guard.map(Box::new),
body,
range: range_or_empty(start..end)
range: OptionalRange::new(start, end)
}
},
}
@ -929,15 +929,15 @@ WithItems: Vec<ast::Withitem> = {
#[inline]
WithItemsNoAs: Vec<ast::Withitem> = {
<location:@L> <all:OneOrMore<Test<"all">>> <end_location:@R> => {
all.into_iter().map(|context_expr| ast::Withitem { context_expr, optional_vars: None, range: range_or_empty(location..end_location) }).collect()
all.into_iter().map(|context_expr| ast::Withitem { context_expr, optional_vars: None, range: OptionalRange::new(location, end_location) }).collect()
},
}
WithItem<Goal>: ast::Withitem = {
<location:@L> <context_expr: Test<Goal>> <end_location:@R> if Goal != "as" => ast::Withitem { context_expr, optional_vars: None, range: range_or_empty(location..end_location) },
<location:@L> <context_expr: Test<Goal>> <end_location:@R> if Goal != "as" => ast::Withitem { context_expr, optional_vars: None, range: OptionalRange::new(location, end_location) },
<location:@L> <context_expr:Test<"all">> "as" <vars:Expression<"all">> <end_location:@R> => {
let optional_vars = Some(Box::new(set_context(vars, ast::ExprContext::Store)));
ast::Withitem { context_expr, optional_vars, range: range_or_empty(location..end_location) }
ast::Withitem { context_expr, optional_vars, range: OptionalRange::new(location, end_location) }
},
};
@ -966,7 +966,7 @@ Parameters: ast::Arguments = {
kw_defaults: vec![],
kwarg: None,
defaults: vec![],
range: range_or_empty(location..end_location)
range: OptionalRange::new(location, end_location)
})
)?;
@ -991,7 +991,7 @@ ParameterList<ArgType, StarArgType>: ast::Arguments = {
kwarg,
defaults,
kw_defaults,
range: range_or_empty(location..end_location)
range: OptionalRange::new(location, end_location)
})
},
<location:@L> <param1:ParameterDefs<ArgType>> <kw:("," KwargParameter<ArgType>)> ","? <end_location:@R> =>? {
@ -1011,7 +1011,7 @@ ParameterList<ArgType, StarArgType>: ast::Arguments = {
kwarg,
defaults,
kw_defaults,
range: range_or_empty(location..end_location)
range: OptionalRange::new(location, end_location)
})
},
<location:@L> <params:ParameterListStarArgs<ArgType, StarArgType>> ","? <end_location:@R> => {
@ -1024,7 +1024,7 @@ ParameterList<ArgType, StarArgType>: ast::Arguments = {
kwarg,
defaults: vec![],
kw_defaults,
range: range_or_empty(location..end_location)
range: OptionalRange::new(location, end_location)
}
},
<location:@L> <kwarg:KwargParameter<ArgType>> ","? <end_location:@R> => {
@ -1036,7 +1036,7 @@ ParameterList<ArgType, StarArgType>: ast::Arguments = {
kwarg,
defaults: vec![],
kw_defaults: vec![],
range: range_or_empty(location..end_location)
range: OptionalRange::new(location, end_location)
}
},
};
@ -1194,7 +1194,7 @@ LambdaDef: ast::Expr = {
kw_defaults: vec![],
kwarg: None,
defaults: vec![],
range: range_or_empty(location..end_location)
range: OptionalRange::new(location, end_location)
}
}
))?;
@ -1564,7 +1564,7 @@ SingleForComprehension: ast::Comprehension = {
iter,
ifs,
is_async,
range: range_or_empty(location..end_location)
range: OptionalRange::new(location, end_location)
}
}
};

52
parser/src/python.rs generated
View file

@ -1,11 +1,11 @@
// auto-generated: "lalrpop 0.20.0"
// sha3: 402d166f44e2679fb2f083fb2e4d621c7725b86d3c93474c2730b9b240a8794a
// sha3: dcd27eae3d0dc16a3ba0744e12b96ef7e802b7fe3eff03cbd5e86f6bb02cc325
use crate::{
ast::{self as ast, Ranged},
ast::{self as ast, Ranged, OptionalRange},
lexer::{LexicalError, LexicalErrorType},
function::{ArgumentList, parse_args, parse_params, validate_arguments},
context::set_context,
string::parse_strings, parser::range_or_empty,
string::parse_strings,
token::{self, StringKind},
text_size::TextSize,
};
@ -22,11 +22,11 @@ extern crate alloc;
mod __parse__Top {
use crate::{
ast::{self as ast, Ranged},
ast::{self as ast, Ranged, OptionalRange},
lexer::{LexicalError, LexicalErrorType},
function::{ArgumentList, parse_args, parse_params, validate_arguments},
context::set_context,
string::parse_strings, parser::range_or_empty,
string::parse_strings,
token::{self, StringKind},
text_size::TextSize,
};
@ -36623,7 +36623,7 @@ fn __action1<
(_, end, _): (TextSize, TextSize, TextSize),
) -> ast::Mod
{
ast::ModModule { body, type_ignores: vec![], range: range_or_empty(start..end) }.into()
ast::ModModule { body, type_ignores: vec![], range: OptionalRange::new(start, end) }.into()
}
#[allow(clippy::too_many_arguments)]
@ -36635,7 +36635,7 @@ fn __action2<
(_, end, _): (TextSize, TextSize, TextSize),
) -> ast::Mod
{
ast::ModInteractive { body, range: range_or_empty(start..end) }.into()
ast::ModInteractive { body, range: OptionalRange::new(start, end) }.into()
}
#[allow(clippy::too_many_arguments)]
@ -36648,7 +36648,7 @@ fn __action3<
(_, end, _): (TextSize, TextSize, TextSize),
) -> ast::Mod
{
ast::ModExpression { body: Box::new(body), range: range_or_empty(start..end) }.into()
ast::ModExpression { body: Box::new(body), range: OptionalRange::new(start, end) }.into()
}
#[allow(clippy::too_many_arguments)]
@ -37615,7 +37615,7 @@ fn __action80<
pattern,
guard: guard.map(Box::new),
body,
range: range_or_empty(start..end)
range: OptionalRange::new(start, end)
}
}
}
@ -38887,7 +38887,7 @@ fn __action153<
) -> Vec<ast::Withitem>
{
{
all.into_iter().map(|context_expr| ast::Withitem { context_expr, optional_vars: None, range: range_or_empty(location..end_location) }).collect()
all.into_iter().map(|context_expr| ast::Withitem { context_expr, optional_vars: None, range: OptionalRange::new(location, end_location) }).collect()
}
}
@ -38938,7 +38938,7 @@ fn __action155<
kw_defaults: vec![],
kwarg: None,
defaults: vec![],
range: range_or_empty(location..end_location)
range: OptionalRange::new(location, end_location)
})
)?;
@ -39124,7 +39124,7 @@ fn __action166<
kw_defaults: vec![],
kwarg: None,
defaults: vec![],
range: range_or_empty(location..end_location)
range: OptionalRange::new(location, end_location)
}
}
))?;
@ -39555,7 +39555,7 @@ fn __action206<
iter,
ifs,
is_async,
range: range_or_empty(location..end_location)
range: OptionalRange::new(location, end_location)
}
}
}
@ -39974,7 +39974,7 @@ fn __action240<
kwarg,
defaults,
kw_defaults,
range: range_or_empty(location..end_location)
range: OptionalRange::new(location, end_location)
})
}
}
@ -40006,7 +40006,7 @@ fn __action241<
kwarg,
defaults,
kw_defaults,
range: range_or_empty(location..end_location)
range: OptionalRange::new(location, end_location)
})
}
}
@ -40030,7 +40030,7 @@ fn __action242<
kwarg,
defaults: vec![],
kw_defaults,
range: range_or_empty(location..end_location)
range: OptionalRange::new(location, end_location)
}
}
}
@ -40053,7 +40053,7 @@ fn __action243<
kwarg,
defaults: vec![],
kw_defaults: vec![],
range: range_or_empty(location..end_location)
range: OptionalRange::new(location, end_location)
}
}
}
@ -40198,7 +40198,7 @@ fn __action256<
kwarg,
defaults,
kw_defaults,
range: range_or_empty(location..end_location)
range: OptionalRange::new(location, end_location)
})
}
}
@ -40230,7 +40230,7 @@ fn __action257<
kwarg,
defaults,
kw_defaults,
range: range_or_empty(location..end_location)
range: OptionalRange::new(location, end_location)
})
}
}
@ -40254,7 +40254,7 @@ fn __action258<
kwarg,
defaults: vec![],
kw_defaults,
range: range_or_empty(location..end_location)
range: OptionalRange::new(location, end_location)
}
}
}
@ -40277,7 +40277,7 @@ fn __action259<
kwarg,
defaults: vec![],
kw_defaults: vec![],
range: range_or_empty(location..end_location)
range: OptionalRange::new(location, end_location)
}
}
}
@ -40371,7 +40371,7 @@ fn __action268<
(_, end_location, _): (TextSize, TextSize, TextSize),
) -> ast::Withitem
{
ast::Withitem { context_expr, optional_vars: None, range: range_or_empty(location..end_location) }
ast::Withitem { context_expr, optional_vars: None, range: OptionalRange::new(location, end_location) }
}
#[allow(clippy::too_many_arguments)]
@ -40386,7 +40386,7 @@ fn __action269<
{
{
let optional_vars = Some(Box::new(set_context(vars, ast::ExprContext::Store)));
ast::Withitem { context_expr, optional_vars, range: range_or_empty(location..end_location) }
ast::Withitem { context_expr, optional_vars, range: OptionalRange::new(location, end_location) }
}
}
@ -40427,7 +40427,7 @@ fn __action273<
(_, end_location, _): (TextSize, TextSize, TextSize),
) -> ast::Withitem
{
ast::Withitem { context_expr, optional_vars: None, range: range_or_empty(location..end_location) }
ast::Withitem { context_expr, optional_vars: None, range: OptionalRange::new(location, end_location) }
}
#[allow(clippy::too_many_arguments)]
@ -40442,7 +40442,7 @@ fn __action274<
{
{
let optional_vars = Some(Box::new(set_context(vars, ast::ExprContext::Store)));
ast::Withitem { context_expr, optional_vars, range: range_or_empty(location..end_location) }
ast::Withitem { context_expr, optional_vars, range: OptionalRange::new(location, end_location) }
}
}
@ -40458,7 +40458,7 @@ fn __action275<
{
{
let optional_vars = Some(Box::new(set_context(vars, ast::ExprContext::Store)));
ast::Withitem { context_expr, optional_vars, range: range_or_empty(location..end_location) }
ast::Withitem { context_expr, optional_vars, range: OptionalRange::new(location, end_location) }
}
}