mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-04 02:39:22 +00:00
Replace row/column based Location with byte-offsets.
This commit is contained in:
parent
7b8844bd3e
commit
58c35ab458
131 changed files with 12120 additions and 23198 deletions
|
@ -16,5 +16,6 @@ unparse = ["rustpython-literal"]
|
|||
[dependencies]
|
||||
rustpython-compiler-core = { path = "../core", version = "0.2.0" }
|
||||
rustpython-literal = { path = "../literal", version = "0.2.0", optional = true }
|
||||
ruff_text_size = { path = "../ruff_text_size" }
|
||||
|
||||
num-bigint = { workspace = true }
|
||||
|
|
|
@ -352,7 +352,7 @@ class FoldImplVisitor(TypeInfoEmitVisitor):
|
|||
depth,
|
||||
)
|
||||
self.emit(
|
||||
"Ok(Located { custom: folder.map_user(node.custom)?, location: node.location, end_location: node.end_location, node: f(folder, node.node)? })",
|
||||
"Ok(Located { custom: folder.map_user(node.custom)?, range: node.range, node: f(folder, node.node)? })",
|
||||
depth + 1,
|
||||
)
|
||||
self.emit("}", depth)
|
||||
|
@ -718,7 +718,7 @@ def write_ast_def(mod, typeinfo, f):
|
|||
#![allow(clippy::derive_partial_eq_without_eq)]
|
||||
|
||||
pub use crate::constant::*;
|
||||
pub use crate::Location;
|
||||
pub use ruff_text_size::{TextSize, TextRange};
|
||||
|
||||
type Ident = String;
|
||||
\n
|
||||
|
@ -730,26 +730,54 @@ def write_ast_def(mod, typeinfo, f):
|
|||
textwrap.dedent(
|
||||
"""
|
||||
pub struct Located<T, U = ()> {
|
||||
pub location: Location,
|
||||
pub end_location: Option<Location>,
|
||||
pub range: TextRange,
|
||||
pub custom: U,
|
||||
pub node: T,
|
||||
}
|
||||
|
||||
impl<T> Located<T> {
|
||||
pub fn new(location: Location, end_location: Location, node: T) -> Self {
|
||||
Self { location, end_location: Some(end_location), custom: (), node }
|
||||
pub fn new(start: TextSize, end: TextSize, node: T) -> Self {
|
||||
Self { range: TextRange::new(start, end), custom: (), node }
|
||||
}
|
||||
|
||||
pub const fn start(&self) -> Location {
|
||||
self.location
|
||||
/// Creates a new node that spans the position specified by `range`.
|
||||
pub fn with_range(node: T, range: TextRange) -> Self {
|
||||
Self {
|
||||
range,
|
||||
custom: (),
|
||||
node,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the absolute start position of the node from the beginning of the document.
|
||||
#[inline]
|
||||
pub const fn start(&self) -> TextSize {
|
||||
self.range.start()
|
||||
}
|
||||
|
||||
/// Returns the node
|
||||
#[inline]
|
||||
pub fn node(&self) -> &T {
|
||||
&self.node
|
||||
}
|
||||
|
||||
/// Consumes self and returns the node.
|
||||
#[inline]
|
||||
pub fn into_node(self) -> T {
|
||||
self.node
|
||||
}
|
||||
|
||||
/// Returns the `range` of the node. The range offsets are absolute to the start of the document.
|
||||
#[inline]
|
||||
pub const fn range(&self) -> TextRange {
|
||||
self.range
|
||||
}
|
||||
|
||||
/// Returns the absolute position at which the node ends in the source document.
|
||||
#[inline]
|
||||
pub const fn end(&self) -> TextSize {
|
||||
self.range.end()
|
||||
}
|
||||
|
||||
/// Returns the node's [`end_location`](Located::end_location) or [`location`](Located::start) if
|
||||
/// [`end_location`](Located::end_location) is `None`.
|
||||
pub fn end(&self) -> Location {
|
||||
self.end_location.unwrap_or(self.location)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, U> std::ops::Deref for Located<T, U> {
|
||||
|
|
|
@ -3,36 +3,63 @@
|
|||
#![allow(clippy::derive_partial_eq_without_eq)]
|
||||
|
||||
pub use crate::constant::*;
|
||||
pub use crate::Location;
|
||||
pub use ruff_text_size::{TextRange, TextSize};
|
||||
|
||||
type Ident = String;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Located<T, U = ()> {
|
||||
pub location: Location,
|
||||
pub end_location: Option<Location>,
|
||||
pub range: TextRange,
|
||||
pub custom: U,
|
||||
pub node: T,
|
||||
}
|
||||
|
||||
impl<T> Located<T> {
|
||||
pub fn new(location: Location, end_location: Location, node: T) -> Self {
|
||||
pub fn new(start: TextSize, end: TextSize, node: T) -> Self {
|
||||
Self {
|
||||
location,
|
||||
end_location: Some(end_location),
|
||||
range: TextRange::new(start, end),
|
||||
custom: (),
|
||||
node,
|
||||
}
|
||||
}
|
||||
|
||||
pub const fn start(&self) -> Location {
|
||||
self.location
|
||||
/// Creates a new node that spans the position specified by `range`.
|
||||
pub fn with_range(node: T, range: TextRange) -> Self {
|
||||
Self {
|
||||
range,
|
||||
custom: (),
|
||||
node,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the node's [`end_location`](Located::end_location) or [`location`](Located::start) if
|
||||
/// [`end_location`](Located::end_location) is `None`.
|
||||
pub fn end(&self) -> Location {
|
||||
self.end_location.unwrap_or(self.location)
|
||||
/// Returns the absolute start position of the node from the beginning of the document.
|
||||
#[inline]
|
||||
pub const fn start(&self) -> TextSize {
|
||||
self.range.start()
|
||||
}
|
||||
|
||||
/// Returns the node
|
||||
#[inline]
|
||||
pub fn node(&self) -> &T {
|
||||
&self.node
|
||||
}
|
||||
|
||||
/// Consumes self and returns the node.
|
||||
#[inline]
|
||||
pub fn into_node(self) -> T {
|
||||
self.node
|
||||
}
|
||||
|
||||
/// Returns the `range` of the node. The range offsets are absolute to the start of the document.
|
||||
#[inline]
|
||||
pub const fn range(&self) -> TextRange {
|
||||
self.range
|
||||
}
|
||||
|
||||
/// Returns the absolute position at which the node ends in the source document.
|
||||
#[inline]
|
||||
pub const fn end(&self) -> TextSize {
|
||||
self.range.end()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1142,8 +1169,7 @@ pub mod fold {
|
|||
) -> Result<Located<MT, F::TargetU>, F::Error> {
|
||||
Ok(Located {
|
||||
custom: folder.map_user(node.custom)?,
|
||||
location: node.location,
|
||||
end_location: node.end_location,
|
||||
range: node.range,
|
||||
node: f(folder, node.node)?,
|
||||
})
|
||||
}
|
||||
|
|
|
@ -126,8 +126,7 @@ impl<U> crate::fold::Fold<U> for ConstantOptimizer {
|
|||
Ok(crate::Expr {
|
||||
node: expr,
|
||||
custom: node.custom,
|
||||
location: node.location,
|
||||
end_location: node.end_location,
|
||||
range: node.range,
|
||||
})
|
||||
}
|
||||
_ => crate::fold::fold_expr(self, node),
|
||||
|
@ -144,19 +143,17 @@ mod tests {
|
|||
fn test_constant_opt() {
|
||||
use crate::{fold::Fold, *};
|
||||
|
||||
let start = Default::default();
|
||||
let end = None;
|
||||
let range = TextRange::default();
|
||||
#[allow(clippy::let_unit_value)]
|
||||
let custom = ();
|
||||
let ast = Located {
|
||||
location: start,
|
||||
end_location: end,
|
||||
range,
|
||||
custom,
|
||||
node: ExprTuple {
|
||||
ctx: ExprContext::Load,
|
||||
elts: vec![
|
||||
Located {
|
||||
location: start,
|
||||
end_location: end,
|
||||
range,
|
||||
custom,
|
||||
node: ExprConstant {
|
||||
value: BigInt::from(1).into(),
|
||||
|
@ -165,8 +162,7 @@ mod tests {
|
|||
.into(),
|
||||
},
|
||||
Located {
|
||||
location: start,
|
||||
end_location: end,
|
||||
range,
|
||||
custom,
|
||||
node: ExprConstant {
|
||||
value: BigInt::from(2).into(),
|
||||
|
@ -175,15 +171,13 @@ mod tests {
|
|||
.into(),
|
||||
},
|
||||
Located {
|
||||
location: start,
|
||||
end_location: end,
|
||||
range,
|
||||
custom,
|
||||
node: ExprTuple {
|
||||
ctx: ExprContext::Load,
|
||||
elts: vec![
|
||||
Located {
|
||||
location: start,
|
||||
end_location: end,
|
||||
range,
|
||||
custom,
|
||||
node: ExprConstant {
|
||||
value: BigInt::from(3).into(),
|
||||
|
@ -192,8 +186,7 @@ mod tests {
|
|||
.into(),
|
||||
},
|
||||
Located {
|
||||
location: start,
|
||||
end_location: end,
|
||||
range,
|
||||
custom,
|
||||
node: ExprConstant {
|
||||
value: BigInt::from(4).into(),
|
||||
|
@ -202,8 +195,7 @@ mod tests {
|
|||
.into(),
|
||||
},
|
||||
Located {
|
||||
location: start,
|
||||
end_location: end,
|
||||
range,
|
||||
custom,
|
||||
node: ExprConstant {
|
||||
value: BigInt::from(5).into(),
|
||||
|
@ -225,8 +217,7 @@ mod tests {
|
|||
assert_eq!(
|
||||
new_ast,
|
||||
Located {
|
||||
location: start,
|
||||
end_location: end,
|
||||
range,
|
||||
custom,
|
||||
node: ExprConstant {
|
||||
value: Constant::Tuple(vec![
|
||||
|
|
|
@ -7,6 +7,5 @@ mod impls;
|
|||
mod unparse;
|
||||
|
||||
pub use ast_gen::*;
|
||||
pub use rustpython_compiler_core::Location;
|
||||
|
||||
pub type Suite<U = ()> = Vec<Stmt<U>>;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue