mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-30 15:18:02 +00:00
Add end locations to all nodes (#4192)
This commit is contained in:
parent
ca62bd1593
commit
8a32bab00a
61 changed files with 3157 additions and 144 deletions
|
@ -304,7 +304,7 @@ class FoldImplVisitor(TypeInfoEmitVisitor):
|
|||
depth,
|
||||
)
|
||||
self.emit(
|
||||
"Ok(Located { custom: folder.map_user(node.custom)?, location: node.location, node: f(folder, node.node)? })",
|
||||
"Ok(Located { custom: folder.map_user(node.custom)?, location: node.location, end_location: node.end_location, node: f(folder, node.node)? })",
|
||||
depth + 1,
|
||||
)
|
||||
self.emit("}", depth)
|
||||
|
@ -617,9 +617,6 @@ class TraitImplVisitor(EmitVisitor):
|
|||
column = self.decode_field(asdl.Field("int", "col_offset"), typename)
|
||||
self.emit(f"let _location = ast::Location::new({row}, {column});", depth)
|
||||
|
||||
def wrap_located_node(self, depth):
|
||||
self.emit(f"let node = ast::Located::new(_location, node);", depth)
|
||||
|
||||
def decode_field(self, field, typename):
|
||||
name = json.dumps(field.name)
|
||||
if field.opt and not field.seq:
|
||||
|
@ -658,13 +655,14 @@ def write_ast_def(mod, typeinfo, f):
|
|||
"""
|
||||
pub struct Located<T, U = ()> {
|
||||
pub location: Location,
|
||||
pub end_location: Option<Location>,
|
||||
pub custom: U,
|
||||
pub node: T,
|
||||
}
|
||||
|
||||
impl<T> Located<T> {
|
||||
pub fn new(location: Location, node: T) -> Self {
|
||||
Self { location, custom: (), node }
|
||||
pub fn new(location: Location, end_location: Location, node: T) -> Self {
|
||||
Self { location, end_location: Some(end_location), custom: (), node }
|
||||
}
|
||||
}
|
||||
\n
|
||||
|
|
|
@ -10,14 +10,16 @@ type Ident = String;
|
|||
#[derive(Debug, PartialEq)]
|
||||
pub struct Located<T, U = ()> {
|
||||
pub location: Location,
|
||||
pub end_location: Option<Location>,
|
||||
pub custom: U,
|
||||
pub node: T,
|
||||
}
|
||||
|
||||
impl<T> Located<T> {
|
||||
pub fn new(location: Location, node: T) -> Self {
|
||||
pub fn new(location: Location, end_location: Location, node: T) -> Self {
|
||||
Self {
|
||||
location,
|
||||
end_location: Some(end_location),
|
||||
custom: (),
|
||||
node,
|
||||
}
|
||||
|
@ -529,6 +531,7 @@ pub mod fold {
|
|||
Ok(Located {
|
||||
custom: folder.map_user(node.custom)?,
|
||||
location: node.location,
|
||||
end_location: node.end_location,
|
||||
node: f(folder, node.node)?,
|
||||
})
|
||||
}
|
||||
|
|
|
@ -121,6 +121,7 @@ impl<U> crate::fold::Fold<U> for ConstantOptimizer {
|
|||
node: expr,
|
||||
custom: node.custom,
|
||||
location: node.location,
|
||||
end_location: node.end_location,
|
||||
})
|
||||
}
|
||||
_ => crate::fold::fold_expr(self, node),
|
||||
|
@ -137,16 +138,19 @@ mod tests {
|
|||
use crate::fold::Fold;
|
||||
use crate::*;
|
||||
|
||||
let location = Location::new(0, 0);
|
||||
let start = Default::default();
|
||||
let end = None;
|
||||
let custom = ();
|
||||
let ast = Located {
|
||||
location,
|
||||
location: start,
|
||||
end_location: end,
|
||||
custom,
|
||||
node: ExprKind::Tuple {
|
||||
ctx: ExprContext::Load,
|
||||
elts: vec![
|
||||
Located {
|
||||
location,
|
||||
location: start,
|
||||
end_location: end,
|
||||
custom,
|
||||
node: ExprKind::Constant {
|
||||
value: BigInt::from(1).into(),
|
||||
|
@ -154,7 +158,8 @@ mod tests {
|
|||
},
|
||||
},
|
||||
Located {
|
||||
location,
|
||||
location: start,
|
||||
end_location: end,
|
||||
custom,
|
||||
node: ExprKind::Constant {
|
||||
value: BigInt::from(2).into(),
|
||||
|
@ -162,13 +167,15 @@ mod tests {
|
|||
},
|
||||
},
|
||||
Located {
|
||||
location,
|
||||
location: start,
|
||||
end_location: end,
|
||||
custom,
|
||||
node: ExprKind::Tuple {
|
||||
ctx: ExprContext::Load,
|
||||
elts: vec![
|
||||
Located {
|
||||
location,
|
||||
location: start,
|
||||
end_location: end,
|
||||
custom,
|
||||
node: ExprKind::Constant {
|
||||
value: BigInt::from(3).into(),
|
||||
|
@ -176,7 +183,8 @@ mod tests {
|
|||
},
|
||||
},
|
||||
Located {
|
||||
location,
|
||||
location: start,
|
||||
end_location: end,
|
||||
custom,
|
||||
node: ExprKind::Constant {
|
||||
value: BigInt::from(4).into(),
|
||||
|
@ -184,7 +192,8 @@ mod tests {
|
|||
},
|
||||
},
|
||||
Located {
|
||||
location,
|
||||
location: start,
|
||||
end_location: end,
|
||||
custom,
|
||||
node: ExprKind::Constant {
|
||||
value: BigInt::from(5).into(),
|
||||
|
@ -203,7 +212,8 @@ mod tests {
|
|||
assert_eq!(
|
||||
new_ast,
|
||||
Located {
|
||||
location,
|
||||
location: start,
|
||||
end_location: end,
|
||||
custom,
|
||||
node: ExprKind::Constant {
|
||||
value: Constant::Tuple(vec![
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue