From 028c04466df90a778568f7899409deac3342ab4e Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Sun, 14 May 2023 17:04:11 +0200 Subject: [PATCH] Make `range` the first field`, change conditional compilation, use `assert_eq_size` --- Cargo.toml | 2 +- ast/asdl_rs.py | 7 +- ast/src/gen/generic.rs | 165 ++++++++++++++++---------------------- ast/src/impls.rs | 9 +-- ast/src/lib.rs | 2 +- ast/src/ranged.rs | 15 ++++ parser/src/parser.rs | 14 ---- parser/src/python.lalrpop | 32 ++++---- parser/src/python.rs | 52 ++++++------ 9 files changed, 134 insertions(+), 164 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 223470d..38cedc5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/ast/asdl_rs.py b/ast/asdl_rs.py index 480ff7f..a08efd8 100755 --- a/ast/asdl_rs.py +++ b/ast/asdl_rs.py @@ -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,", depth + 1) + self.emit("pub range: crate::ranged::OptionalRange,", 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} {{", 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( diff --git a/ast/src/gen/generic.rs b/ast/src/gen/generic.rs index e58e7b4..6d819b1 100644 --- a/ast/src/gen/generic.rs +++ b/ast/src/gen/generic.rs @@ -3,12 +3,9 @@ use crate::text_size::TextRange; #[derive(Clone, Debug, PartialEq)] pub struct ModModule { + pub range: crate::ranged::OptionalRange, pub body: Vec>, pub type_ignores: Vec>, - #[cfg(feature = "all-nodes-with-ranges")] - pub range: R, - #[cfg(not(feature = "all-nodes-with-ranges"))] - pub range: crate::EmptyRange, } impl From> for Mod { @@ -19,11 +16,8 @@ impl From> for Mod { #[derive(Clone, Debug, PartialEq)] pub struct ModInteractive { + pub range: crate::ranged::OptionalRange, pub body: Vec>, - #[cfg(feature = "all-nodes-with-ranges")] - pub range: R, - #[cfg(not(feature = "all-nodes-with-ranges"))] - pub range: crate::EmptyRange, } impl From> for Mod { @@ -34,11 +28,8 @@ impl From> for Mod { #[derive(Clone, Debug, PartialEq)] pub struct ModExpression { + pub range: crate::ranged::OptionalRange, pub body: Box>, - #[cfg(feature = "all-nodes-with-ranges")] - pub range: R, - #[cfg(not(feature = "all-nodes-with-ranges"))] - pub range: crate::EmptyRange, } impl From> for Mod { @@ -49,12 +40,9 @@ impl From> for Mod { #[derive(Clone, Debug, PartialEq)] pub struct ModFunctionType { + pub range: crate::ranged::OptionalRange, pub argtypes: Vec>, pub returns: Box>, - #[cfg(feature = "all-nodes-with-ranges")] - pub range: R, - #[cfg(not(feature = "all-nodes-with-ranges"))] - pub range: crate::EmptyRange, } impl From> for Mod { @@ -73,13 +61,13 @@ pub enum Mod { #[derive(Clone, Debug, PartialEq)] pub struct StmtFunctionDef { + pub range: R, pub name: Identifier, pub args: Box>, pub body: Vec>, pub decorator_list: Vec>, pub returns: Option>>, pub type_comment: Option, - pub range: R, } impl From> for Stmt { @@ -90,13 +78,13 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtAsyncFunctionDef { + pub range: R, pub name: Identifier, pub args: Box>, pub body: Vec>, pub decorator_list: Vec>, pub returns: Option>>, pub type_comment: Option, - pub range: R, } impl From> for Stmt { @@ -107,12 +95,12 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtClassDef { + pub range: R, pub name: Identifier, pub bases: Vec>, pub keywords: Vec>, pub body: Vec>, pub decorator_list: Vec>, - pub range: R, } impl From> for Stmt { @@ -123,8 +111,8 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtReturn { - pub value: Option>>, pub range: R, + pub value: Option>>, } impl From> for Stmt { @@ -135,8 +123,8 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtDelete { - pub targets: Vec>, pub range: R, + pub targets: Vec>, } impl From> for Stmt { @@ -147,10 +135,10 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtAssign { + pub range: R, pub targets: Vec>, pub value: Box>, pub type_comment: Option, - pub range: R, } impl From> for Stmt { @@ -161,10 +149,10 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtAugAssign { + pub range: R, pub target: Box>, pub op: Operator, pub value: Box>, - pub range: R, } impl From> for Stmt { @@ -175,11 +163,11 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtAnnAssign { + pub range: R, pub target: Box>, pub annotation: Box>, pub value: Option>>, pub simple: bool, - pub range: R, } impl From> for Stmt { @@ -190,12 +178,12 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtFor { + pub range: R, pub target: Box>, pub iter: Box>, pub body: Vec>, pub orelse: Vec>, pub type_comment: Option, - pub range: R, } impl From> for Stmt { @@ -206,12 +194,12 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtAsyncFor { + pub range: R, pub target: Box>, pub iter: Box>, pub body: Vec>, pub orelse: Vec>, pub type_comment: Option, - pub range: R, } impl From> for Stmt { @@ -222,10 +210,10 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtWhile { + pub range: R, pub test: Box>, pub body: Vec>, pub orelse: Vec>, - pub range: R, } impl From> for Stmt { @@ -236,10 +224,10 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtIf { + pub range: R, pub test: Box>, pub body: Vec>, pub orelse: Vec>, - pub range: R, } impl From> for Stmt { @@ -250,10 +238,10 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtWith { + pub range: R, pub items: Vec>, pub body: Vec>, pub type_comment: Option, - pub range: R, } impl From> for Stmt { @@ -264,10 +252,10 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtAsyncWith { + pub range: R, pub items: Vec>, pub body: Vec>, pub type_comment: Option, - pub range: R, } impl From> for Stmt { @@ -278,9 +266,9 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtMatch { + pub range: R, pub subject: Box>, pub cases: Vec>, - pub range: R, } impl From> for Stmt { @@ -291,9 +279,9 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtRaise { + pub range: R, pub exc: Option>>, pub cause: Option>>, - pub range: R, } impl From> for Stmt { @@ -304,11 +292,11 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtTry { + pub range: R, pub body: Vec>, pub handlers: Vec>, pub orelse: Vec>, pub finalbody: Vec>, - pub range: R, } impl From> for Stmt { @@ -319,11 +307,11 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtTryStar { + pub range: R, pub body: Vec>, pub handlers: Vec>, pub orelse: Vec>, pub finalbody: Vec>, - pub range: R, } impl From> for Stmt { @@ -334,9 +322,9 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtAssert { + pub range: R, pub test: Box>, pub msg: Option>>, - pub range: R, } impl From> for Stmt { @@ -347,8 +335,8 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtImport { - pub names: Vec>, pub range: R, + pub names: Vec>, } impl From> for Stmt { @@ -359,10 +347,10 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtImportFrom { + pub range: R, pub module: Option, pub names: Vec>, pub level: Option, - pub range: R, } impl From> for Stmt { @@ -373,8 +361,8 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtGlobal { - pub names: Vec, pub range: R, + pub names: Vec, } impl From> for Stmt { @@ -385,8 +373,8 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtNonlocal { - pub names: Vec, pub range: R, + pub names: Vec, } impl From> for Stmt { @@ -397,8 +385,8 @@ impl From> for Stmt { #[derive(Clone, Debug, PartialEq)] pub struct StmtExpr { - pub value: Box>, pub range: R, + pub value: Box>, } impl From> for Stmt { @@ -500,9 +488,9 @@ pub enum Stmt { #[derive(Clone, Debug, PartialEq)] pub struct ExprBoolOp { + pub range: R, pub op: Boolop, pub values: Vec>, - pub range: R, } impl From> for Expr { @@ -513,9 +501,9 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprNamedExpr { + pub range: R, pub target: Box>, pub value: Box>, - pub range: R, } impl From> for Expr { @@ -526,10 +514,10 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprBinOp { + pub range: R, pub left: Box>, pub op: Operator, pub right: Box>, - pub range: R, } impl From> for Expr { @@ -540,9 +528,9 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprUnaryOp { + pub range: R, pub op: Unaryop, pub operand: Box>, - pub range: R, } impl From> for Expr { @@ -553,9 +541,9 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprLambda { + pub range: R, pub args: Box>, pub body: Box>, - pub range: R, } impl From> for Expr { @@ -566,10 +554,10 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprIfExp { + pub range: R, pub test: Box>, pub body: Box>, pub orelse: Box>, - pub range: R, } impl From> for Expr { @@ -580,9 +568,9 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprDict { + pub range: R, pub keys: Vec>>, pub values: Vec>, - pub range: R, } impl From> for Expr { @@ -593,8 +581,8 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprSet { - pub elts: Vec>, pub range: R, + pub elts: Vec>, } impl From> for Expr { @@ -605,9 +593,9 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprListComp { + pub range: R, pub elt: Box>, pub generators: Vec>, - pub range: R, } impl From> for Expr { @@ -618,9 +606,9 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprSetComp { + pub range: R, pub elt: Box>, pub generators: Vec>, - pub range: R, } impl From> for Expr { @@ -631,10 +619,10 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprDictComp { + pub range: R, pub key: Box>, pub value: Box>, pub generators: Vec>, - pub range: R, } impl From> for Expr { @@ -645,9 +633,9 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprGeneratorExp { + pub range: R, pub elt: Box>, pub generators: Vec>, - pub range: R, } impl From> for Expr { @@ -658,8 +646,8 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprAwait { - pub value: Box>, pub range: R, + pub value: Box>, } impl From> for Expr { @@ -670,8 +658,8 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprYield { - pub value: Option>>, pub range: R, + pub value: Option>>, } impl From> for Expr { @@ -682,8 +670,8 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprYieldFrom { - pub value: Box>, pub range: R, + pub value: Box>, } impl From> for Expr { @@ -694,10 +682,10 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprCompare { + pub range: R, pub left: Box>, pub ops: Vec, pub comparators: Vec>, - pub range: R, } impl From> for Expr { @@ -708,10 +696,10 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprCall { + pub range: R, pub func: Box>, pub args: Vec>, pub keywords: Vec>, - pub range: R, } impl From> for Expr { @@ -722,10 +710,10 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprFormattedValue { + pub range: R, pub value: Box>, pub conversion: Int, pub format_spec: Option>>, - pub range: R, } impl From> for Expr { @@ -736,8 +724,8 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprJoinedStr { - pub values: Vec>, pub range: R, + pub values: Vec>, } impl From> for Expr { @@ -748,9 +736,9 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprConstant { + pub range: R, pub value: Constant, pub kind: Option, - pub range: R, } impl From> for Expr { @@ -761,10 +749,10 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprAttribute { + pub range: R, pub value: Box>, pub attr: Identifier, pub ctx: ExprContext, - pub range: R, } impl From> for Expr { @@ -775,10 +763,10 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprSubscript { + pub range: R, pub value: Box>, pub slice: Box>, pub ctx: ExprContext, - pub range: R, } impl From> for Expr { @@ -789,9 +777,9 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprStarred { + pub range: R, pub value: Box>, pub ctx: ExprContext, - pub range: R, } impl From> for Expr { @@ -802,9 +790,9 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprName { + pub range: R, pub id: Identifier, pub ctx: ExprContext, - pub range: R, } impl From> for Expr { @@ -815,9 +803,9 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprList { + pub range: R, pub elts: Vec>, pub ctx: ExprContext, - pub range: R, } impl From> for Expr { @@ -828,9 +816,9 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprTuple { + pub range: R, pub elts: Vec>, pub ctx: ExprContext, - pub range: R, } impl From> for Expr { @@ -841,10 +829,10 @@ impl From> for Expr { #[derive(Clone, Debug, PartialEq)] pub struct ExprSlice { + pub range: R, pub lower: Option>>, pub upper: Option>>, pub step: Option>>, - pub range: R, } impl From> for Expr { @@ -969,18 +957,15 @@ pub struct Comprehension { pub iter: Expr, pub ifs: Vec>, pub is_async: bool, - #[cfg(feature = "all-nodes-with-ranges")] - pub range: R, - #[cfg(not(feature = "all-nodes-with-ranges"))] - pub range: crate::EmptyRange, + pub range: crate::ranged::OptionalRange, } #[derive(Clone, Debug, PartialEq)] pub struct ExcepthandlerExceptHandler { + pub range: R, pub type_: Option>>, pub name: Option, pub body: Vec>, - pub range: R, } impl From> for Excepthandler { @@ -1003,10 +988,7 @@ pub struct Arguments { pub kw_defaults: Vec>, pub kwarg: Option>>, pub defaults: Vec>, - #[cfg(feature = "all-nodes-with-ranges")] - pub range: R, - #[cfg(not(feature = "all-nodes-with-ranges"))] - pub range: crate::EmptyRange, + pub range: crate::ranged::OptionalRange, } #[derive(Clone, Debug, PartialEq)] @@ -1035,10 +1017,7 @@ pub struct Alias { pub struct Withitem { pub context_expr: Expr, pub optional_vars: Option>>, - #[cfg(feature = "all-nodes-with-ranges")] - pub range: R, - #[cfg(not(feature = "all-nodes-with-ranges"))] - pub range: crate::EmptyRange, + pub range: crate::ranged::OptionalRange, } #[derive(Clone, Debug, PartialEq)] @@ -1046,16 +1025,13 @@ pub struct MatchCase { pub pattern: Pattern, pub guard: Option>>, pub body: Vec>, - #[cfg(feature = "all-nodes-with-ranges")] - pub range: R, - #[cfg(not(feature = "all-nodes-with-ranges"))] - pub range: crate::EmptyRange, + pub range: crate::ranged::OptionalRange, } #[derive(Clone, Debug, PartialEq)] pub struct PatternMatchValue { - pub value: Box>, pub range: R, + pub value: Box>, } impl From> for Pattern { @@ -1066,8 +1042,8 @@ impl From> for Pattern { #[derive(Clone, Debug, PartialEq)] pub struct PatternMatchSingleton { - pub value: Constant, pub range: R, + pub value: Constant, } impl From> for Pattern { @@ -1078,8 +1054,8 @@ impl From> for Pattern { #[derive(Clone, Debug, PartialEq)] pub struct PatternMatchSequence { - pub patterns: Vec>, pub range: R, + pub patterns: Vec>, } impl From> for Pattern { @@ -1090,10 +1066,10 @@ impl From> for Pattern { #[derive(Clone, Debug, PartialEq)] pub struct PatternMatchMapping { + pub range: R, pub keys: Vec>, pub patterns: Vec>, pub rest: Option, - pub range: R, } impl From> for Pattern { @@ -1104,11 +1080,11 @@ impl From> for Pattern { #[derive(Clone, Debug, PartialEq)] pub struct PatternMatchClass { + pub range: R, pub cls: Box>, pub patterns: Vec>, pub kwd_attrs: Vec, pub kwd_patterns: Vec>, - pub range: R, } impl From> for Pattern { @@ -1119,8 +1095,8 @@ impl From> for Pattern { #[derive(Clone, Debug, PartialEq)] pub struct PatternMatchStar { - pub name: Option, pub range: R, + pub name: Option, } impl From> for Pattern { @@ -1131,9 +1107,9 @@ impl From> for Pattern { #[derive(Clone, Debug, PartialEq)] pub struct PatternMatchAs { + pub range: R, pub pattern: Option>>, pub name: Option, - pub range: R, } impl From> for Pattern { @@ -1144,8 +1120,8 @@ impl From> for Pattern { #[derive(Clone, Debug, PartialEq)] pub struct PatternMatchOr { - pub patterns: Vec>, pub range: R, + pub patterns: Vec>, } impl From> for Pattern { @@ -1168,12 +1144,9 @@ pub enum Pattern { #[derive(Clone, Debug, PartialEq)] pub struct TypeIgnoreTypeIgnore { + pub range: crate::ranged::OptionalRange, 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, } impl From> for TypeIgnore { diff --git a/ast/src/impls.rs b/ast/src/impls.rs index 53bed2d..a8cb340 100644 --- a/ast/src/impls.rs +++ b/ast/src/impls.rs @@ -1,5 +1,4 @@ use crate::{Constant, Excepthandler, Expr, Pattern, Stmt}; -use static_assertions::const_assert_eq; impl Expr { /// Returns a short name for the node suitable for use in error messages. @@ -56,10 +55,10 @@ impl Expr { } #[cfg(target_arch = "x86_64")] -const_assert_eq!(std::mem::size_of::(), 72); +static_assertions::assert_eq_size!(Expr, [u8; 72]); #[cfg(target_arch = "x86_64")] -const_assert_eq!(std::mem::size_of::(), 136); +static_assertions::assert_eq_size!(Stmt, [u8; 136]); #[cfg(target_arch = "x86_64")] -const_assert_eq!(std::mem::size_of::(), 96); +static_assertions::assert_eq_size!(Pattern, [u8; 96]); #[cfg(target_arch = "x86_64")] -const_assert_eq!(std::mem::size_of::(), 64); +static_assertions::assert_eq_size!(Excepthandler, [u8; 64]); diff --git a/ast/src/lib.rs b/ast/src/lib.rs index e233533..4055623 100644 --- a/ast/src/lib.rs +++ b/ast/src/lib.rs @@ -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 = Vec>; diff --git a/ast/src/ranged.rs b/ast/src/ranged.rs index b766b93..d260271 100644 --- a/ast/src/ranged.rs +++ b/ast/src/ranged.rs @@ -16,11 +16,26 @@ pub trait Ranged { } } +#[cfg(feature = "all-nodes-with-ranges")] +pub type OptionalRange = R; + +#[cfg(not(feature = "all-nodes-with-ranges"))] +pub type OptionalRange = EmptyRange; + #[derive(Eq, PartialEq, Hash, Copy, Clone)] pub struct EmptyRange { phantom: PhantomData, } +impl EmptyRange { + #[inline(always)] + pub fn new(_start: TextSize, _end: TextSize) -> Self { + Self { + phantom: PhantomData, + } + } +} + impl Display for EmptyRange { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { f.write_str("()") diff --git a/parser/src/parser.rs b/parser/src/parser.rs index fb878a3..141dc2a 100644 --- a/parser/src/parser.rs +++ b/parser/src/parser.rs @@ -318,20 +318,6 @@ impl ParseErrorType { } } -#[cfg(feature = "all-nodes-with-ranges")] -#[inline(always)] -pub(super) fn range_or_empty>( - range: T, -) -> crate::text_size::TextRange { - range.into() -} - -#[cfg(not(feature = "all-nodes-with-ranges"))] -#[inline(always)] -pub(super) fn range_or_empty(_: T) -> rustpython_ast::EmptyRange { - rustpython_ast::EmptyRange::default() -} - #[cfg(test)] mod tests { use super::*; diff --git a/parser/src/python.lalrpop b/parser/src/python.lalrpop index 137a161..2e0061d 100644 --- a/parser/src/python.lalrpop +++ b/parser/src/python.lalrpop @@ -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 = { - StartModule => ast::ModModule { body, type_ignores: vec![], range: range_or_empty(start..end) }.into(), - StartInteractive => ast::ModInteractive { body, range: range_or_empty(start..end) }.into(), - StartExpression ("\n")* => ast::ModExpression { body: Box::new(body), range: range_or_empty(start..end) }.into() + StartModule => ast::ModModule { body, type_ignores: vec![], range: OptionalRange::new(start, end) }.into(), + StartInteractive => ast::ModInteractive { body, range: OptionalRange::new(start, end) }.into(), + StartExpression ("\n")* => 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 = { #[inline] WithItemsNoAs: Vec = { >> => { - 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: ast::Withitem = { - > if Goal != "as" => ast::Withitem { context_expr, optional_vars: None, range: range_or_empty(location..end_location) }, + > if Goal != "as" => ast::Withitem { context_expr, optional_vars: None, range: OptionalRange::new(location, end_location) }, > "as" > => { 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: ast::Arguments = { kwarg, defaults, kw_defaults, - range: range_or_empty(location..end_location) + range: OptionalRange::new(location, end_location) }) }, > )> ","? =>? { @@ -1011,7 +1011,7 @@ ParameterList: ast::Arguments = { kwarg, defaults, kw_defaults, - range: range_or_empty(location..end_location) + range: OptionalRange::new(location, end_location) }) }, > ","? => { @@ -1024,7 +1024,7 @@ ParameterList: ast::Arguments = { kwarg, defaults: vec![], kw_defaults, - range: range_or_empty(location..end_location) + range: OptionalRange::new(location, end_location) } }, > ","? => { @@ -1036,7 +1036,7 @@ ParameterList: 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) } } }; diff --git a/parser/src/python.rs b/parser/src/python.rs index 9d2bcc3..b648da7 100644 --- a/parser/src/python.rs +++ b/parser/src/python.rs @@ -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 { { - 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) } } }