mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-28 06:14:56 +00:00
Adapt SourceLocation
This commit is contained in:
parent
a14e43e03a
commit
09a6afdd04
117 changed files with 1606 additions and 1676 deletions
|
@ -1,7 +1,5 @@
|
|||
use rustpython_compiler_core::{
|
||||
text_size::{TextRange, TextSize},
|
||||
Location, LocationRange,
|
||||
};
|
||||
use crate::location::{SourceLocation, SourceRange};
|
||||
use rustpython_compiler_core::text_size::{TextRange, TextSize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Attributed<T, U = ()> {
|
||||
|
@ -53,16 +51,16 @@ impl<T> Attributed<T, ()> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T> Attributed<T, LocationRange> {
|
||||
impl<T> Attributed<T, SourceRange> {
|
||||
/// Returns the absolute start position of the node from the beginning of the document.
|
||||
#[inline]
|
||||
pub const fn location(&self) -> Location {
|
||||
pub const fn location(&self) -> SourceLocation {
|
||||
self.custom.start
|
||||
}
|
||||
|
||||
/// Returns the absolute position at which the node ends in the source document.
|
||||
#[inline]
|
||||
pub const fn end_location(&self) -> Location {
|
||||
pub const fn end_location(&self) -> Option<SourceLocation> {
|
||||
self.custom.end
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
// File automatically generated by ast/asdl_rs.py.
|
||||
|
||||
#![allow(clippy::derive_partial_eq_without_eq)]
|
||||
|
||||
pub use crate::{constant::*, Attributed};
|
||||
use rustpython_compiler_core::text_size::{TextRange, TextSize};
|
||||
|
||||
type Ident = String;
|
||||
|
95
ast/src/gen/located.rs
Normal file
95
ast/src/gen/located.rs
Normal file
|
@ -0,0 +1,95 @@
|
|||
// File automatically generated by ast/asdl_rs.py.
|
||||
|
||||
use crate::location::SourceRange;
|
||||
|
||||
pub type Located<T> = super::generic::Attributed<T, SourceRange>;
|
||||
pub type Mod = super::generic::Mod<SourceRange>;
|
||||
pub type ModModule = super::generic::ModModule<SourceRange>;
|
||||
pub type ModInteractive = super::generic::ModInteractive<SourceRange>;
|
||||
pub type ModExpression = super::generic::ModExpression<SourceRange>;
|
||||
pub type ModFunctionType = super::generic::ModFunctionType<SourceRange>;
|
||||
pub type Stmt = super::generic::Stmt<SourceRange>;
|
||||
pub type StmtKind = super::generic::StmtKind<SourceRange>;
|
||||
pub type StmtFunctionDef = super::generic::StmtFunctionDef<SourceRange>;
|
||||
pub type StmtAsyncFunctionDef = super::generic::StmtAsyncFunctionDef<SourceRange>;
|
||||
pub type StmtClassDef = super::generic::StmtClassDef<SourceRange>;
|
||||
pub type StmtReturn = super::generic::StmtReturn<SourceRange>;
|
||||
pub type StmtDelete = super::generic::StmtDelete<SourceRange>;
|
||||
pub type StmtAssign = super::generic::StmtAssign<SourceRange>;
|
||||
pub type StmtAugAssign = super::generic::StmtAugAssign<SourceRange>;
|
||||
pub type StmtAnnAssign = super::generic::StmtAnnAssign<SourceRange>;
|
||||
pub type StmtFor = super::generic::StmtFor<SourceRange>;
|
||||
pub type StmtAsyncFor = super::generic::StmtAsyncFor<SourceRange>;
|
||||
pub type StmtWhile = super::generic::StmtWhile<SourceRange>;
|
||||
pub type StmtIf = super::generic::StmtIf<SourceRange>;
|
||||
pub type StmtWith = super::generic::StmtWith<SourceRange>;
|
||||
pub type StmtAsyncWith = super::generic::StmtAsyncWith<SourceRange>;
|
||||
pub type StmtMatch = super::generic::StmtMatch<SourceRange>;
|
||||
pub type StmtRaise = super::generic::StmtRaise<SourceRange>;
|
||||
pub type StmtTry = super::generic::StmtTry<SourceRange>;
|
||||
pub type StmtTryStar = super::generic::StmtTryStar<SourceRange>;
|
||||
pub type StmtAssert = super::generic::StmtAssert<SourceRange>;
|
||||
pub type StmtImport = super::generic::StmtImport<SourceRange>;
|
||||
pub type StmtImportFrom = super::generic::StmtImportFrom<SourceRange>;
|
||||
pub type StmtGlobal = super::generic::StmtGlobal;
|
||||
pub type StmtNonlocal = super::generic::StmtNonlocal;
|
||||
pub type StmtExpr = super::generic::StmtExpr<SourceRange>;
|
||||
pub type Expr = super::generic::Expr<SourceRange>;
|
||||
pub type ExprKind = super::generic::ExprKind<SourceRange>;
|
||||
pub type ExprBoolOp = super::generic::ExprBoolOp<SourceRange>;
|
||||
pub type ExprNamedExpr = super::generic::ExprNamedExpr<SourceRange>;
|
||||
pub type ExprBinOp = super::generic::ExprBinOp<SourceRange>;
|
||||
pub type ExprUnaryOp = super::generic::ExprUnaryOp<SourceRange>;
|
||||
pub type ExprLambda = super::generic::ExprLambda<SourceRange>;
|
||||
pub type ExprIfExp = super::generic::ExprIfExp<SourceRange>;
|
||||
pub type ExprDict = super::generic::ExprDict<SourceRange>;
|
||||
pub type ExprSet = super::generic::ExprSet<SourceRange>;
|
||||
pub type ExprListComp = super::generic::ExprListComp<SourceRange>;
|
||||
pub type ExprSetComp = super::generic::ExprSetComp<SourceRange>;
|
||||
pub type ExprDictComp = super::generic::ExprDictComp<SourceRange>;
|
||||
pub type ExprGeneratorExp = super::generic::ExprGeneratorExp<SourceRange>;
|
||||
pub type ExprAwait = super::generic::ExprAwait<SourceRange>;
|
||||
pub type ExprYield = super::generic::ExprYield<SourceRange>;
|
||||
pub type ExprYieldFrom = super::generic::ExprYieldFrom<SourceRange>;
|
||||
pub type ExprCompare = super::generic::ExprCompare<SourceRange>;
|
||||
pub type ExprCall = super::generic::ExprCall<SourceRange>;
|
||||
pub type ExprFormattedValue = super::generic::ExprFormattedValue<SourceRange>;
|
||||
pub type ExprJoinedStr = super::generic::ExprJoinedStr<SourceRange>;
|
||||
pub type ExprConstant = super::generic::ExprConstant;
|
||||
pub type ExprAttribute = super::generic::ExprAttribute<SourceRange>;
|
||||
pub type ExprSubscript = super::generic::ExprSubscript<SourceRange>;
|
||||
pub type ExprStarred = super::generic::ExprStarred<SourceRange>;
|
||||
pub type ExprName = super::generic::ExprName;
|
||||
pub type ExprList = super::generic::ExprList<SourceRange>;
|
||||
pub type ExprTuple = super::generic::ExprTuple<SourceRange>;
|
||||
pub type ExprSlice = super::generic::ExprSlice<SourceRange>;
|
||||
pub type ExprContext = super::generic::ExprContext;
|
||||
pub type Boolop = super::generic::Boolop;
|
||||
pub type Operator = super::generic::Operator;
|
||||
pub type Unaryop = super::generic::Unaryop;
|
||||
pub type Cmpop = super::generic::Cmpop;
|
||||
pub type Comprehension = super::generic::Comprehension<SourceRange>;
|
||||
pub type Excepthandler = super::generic::Excepthandler<SourceRange>;
|
||||
pub type ExcepthandlerKind = super::generic::ExcepthandlerKind<SourceRange>;
|
||||
pub type ExcepthandlerExceptHandler = super::generic::ExcepthandlerExceptHandler<SourceRange>;
|
||||
pub type Arguments = super::generic::Arguments<SourceRange>;
|
||||
pub type Arg = super::generic::Arg<SourceRange>;
|
||||
pub type ArgData = super::generic::ArgData<SourceRange>;
|
||||
pub type Keyword = super::generic::Keyword<SourceRange>;
|
||||
pub type KeywordData = super::generic::KeywordData<SourceRange>;
|
||||
pub type Alias = super::generic::Alias<SourceRange>;
|
||||
pub type AliasData = super::generic::AliasData;
|
||||
pub type Withitem = super::generic::Withitem<SourceRange>;
|
||||
pub type MatchCase = super::generic::MatchCase<SourceRange>;
|
||||
pub type Pattern = super::generic::Pattern<SourceRange>;
|
||||
pub type PatternKind = super::generic::PatternKind<SourceRange>;
|
||||
pub type PatternMatchValue = super::generic::PatternMatchValue<SourceRange>;
|
||||
pub type PatternMatchSingleton = super::generic::PatternMatchSingleton;
|
||||
pub type PatternMatchSequence = super::generic::PatternMatchSequence<SourceRange>;
|
||||
pub type PatternMatchMapping = super::generic::PatternMatchMapping<SourceRange>;
|
||||
pub type PatternMatchClass = super::generic::PatternMatchClass<SourceRange>;
|
||||
pub type PatternMatchStar = super::generic::PatternMatchStar;
|
||||
pub type PatternMatchAs = super::generic::PatternMatchAs<SourceRange>;
|
||||
pub type PatternMatchOr = super::generic::PatternMatchOr<SourceRange>;
|
||||
pub type TypeIgnore = super::generic::TypeIgnore;
|
||||
pub type TypeIgnoreTypeIgnore = super::generic::TypeIgnoreTypeIgnore;
|
|
@ -2,12 +2,21 @@ mod attributed;
|
|||
mod constant;
|
||||
#[cfg(feature = "fold")]
|
||||
mod fold_helpers;
|
||||
mod generic;
|
||||
mod generic {
|
||||
#![allow(clippy::derive_partial_eq_without_eq)]
|
||||
include!("gen/generic.rs");
|
||||
}
|
||||
mod impls;
|
||||
#[cfg(feature = "location")]
|
||||
pub mod located;
|
||||
pub mod located {
|
||||
include!("gen/located.rs");
|
||||
}
|
||||
#[cfg(feature = "location")]
|
||||
mod locator;
|
||||
#[cfg(feature = "location")]
|
||||
pub use crate::locator::locate;
|
||||
#[cfg(feature = "location")]
|
||||
pub use rustpython_compiler_core::SourceLocator;
|
||||
|
||||
#[cfg(feature = "unparse")]
|
||||
mod unparse;
|
||||
|
@ -15,7 +24,36 @@ mod unparse;
|
|||
pub use attributed::Attributed;
|
||||
pub use constant::{Constant, ConversionFlag};
|
||||
pub use generic::*;
|
||||
#[cfg(feature = "location")]
|
||||
pub use locator::Locator;
|
||||
|
||||
pub type Suite<U = ()> = Vec<Stmt<U>>;
|
||||
|
||||
pub mod location {
|
||||
pub use rustpython_compiler_core::source_code::{OneIndexed, SourceLocation};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SourceRange {
|
||||
pub start: SourceLocation,
|
||||
pub end: Option<SourceLocation>,
|
||||
}
|
||||
|
||||
impl SourceRange {
|
||||
pub fn new(start: SourceLocation, end: SourceLocation) -> Self {
|
||||
Self {
|
||||
start,
|
||||
end: Some(end),
|
||||
}
|
||||
}
|
||||
pub fn unwrap_end(&self) -> SourceLocation {
|
||||
self.end.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::ops::Range<SourceLocation>> for SourceRange {
|
||||
fn from(value: std::ops::Range<SourceLocation>) -> Self {
|
||||
Self {
|
||||
start: value.start,
|
||||
end: Some(value.end),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,95 +0,0 @@
|
|||
// File automatically generated by ast/asdl_rs.py.
|
||||
|
||||
use rustpython_compiler_core::LocationRange;
|
||||
|
||||
pub type Located<T> = super::generic::Attributed<T, LocationRange>;
|
||||
pub type Mod = super::generic::Mod<LocationRange>;
|
||||
pub type ModModule = super::generic::ModModule<LocationRange>;
|
||||
pub type ModInteractive = super::generic::ModInteractive<LocationRange>;
|
||||
pub type ModExpression = super::generic::ModExpression<LocationRange>;
|
||||
pub type ModFunctionType = super::generic::ModFunctionType<LocationRange>;
|
||||
pub type Stmt = super::generic::Stmt<LocationRange>;
|
||||
pub type StmtKind = super::generic::StmtKind<LocationRange>;
|
||||
pub type StmtFunctionDef = super::generic::StmtFunctionDef<LocationRange>;
|
||||
pub type StmtAsyncFunctionDef = super::generic::StmtAsyncFunctionDef<LocationRange>;
|
||||
pub type StmtClassDef = super::generic::StmtClassDef<LocationRange>;
|
||||
pub type StmtReturn = super::generic::StmtReturn<LocationRange>;
|
||||
pub type StmtDelete = super::generic::StmtDelete<LocationRange>;
|
||||
pub type StmtAssign = super::generic::StmtAssign<LocationRange>;
|
||||
pub type StmtAugAssign = super::generic::StmtAugAssign<LocationRange>;
|
||||
pub type StmtAnnAssign = super::generic::StmtAnnAssign<LocationRange>;
|
||||
pub type StmtFor = super::generic::StmtFor<LocationRange>;
|
||||
pub type StmtAsyncFor = super::generic::StmtAsyncFor<LocationRange>;
|
||||
pub type StmtWhile = super::generic::StmtWhile<LocationRange>;
|
||||
pub type StmtIf = super::generic::StmtIf<LocationRange>;
|
||||
pub type StmtWith = super::generic::StmtWith<LocationRange>;
|
||||
pub type StmtAsyncWith = super::generic::StmtAsyncWith<LocationRange>;
|
||||
pub type StmtMatch = super::generic::StmtMatch<LocationRange>;
|
||||
pub type StmtRaise = super::generic::StmtRaise<LocationRange>;
|
||||
pub type StmtTry = super::generic::StmtTry<LocationRange>;
|
||||
pub type StmtTryStar = super::generic::StmtTryStar<LocationRange>;
|
||||
pub type StmtAssert = super::generic::StmtAssert<LocationRange>;
|
||||
pub type StmtImport = super::generic::StmtImport<LocationRange>;
|
||||
pub type StmtImportFrom = super::generic::StmtImportFrom<LocationRange>;
|
||||
pub type StmtGlobal = super::generic::StmtGlobal;
|
||||
pub type StmtNonlocal = super::generic::StmtNonlocal;
|
||||
pub type StmtExpr = super::generic::StmtExpr<LocationRange>;
|
||||
pub type Expr = super::generic::Expr<LocationRange>;
|
||||
pub type ExprKind = super::generic::ExprKind<LocationRange>;
|
||||
pub type ExprBoolOp = super::generic::ExprBoolOp<LocationRange>;
|
||||
pub type ExprNamedExpr = super::generic::ExprNamedExpr<LocationRange>;
|
||||
pub type ExprBinOp = super::generic::ExprBinOp<LocationRange>;
|
||||
pub type ExprUnaryOp = super::generic::ExprUnaryOp<LocationRange>;
|
||||
pub type ExprLambda = super::generic::ExprLambda<LocationRange>;
|
||||
pub type ExprIfExp = super::generic::ExprIfExp<LocationRange>;
|
||||
pub type ExprDict = super::generic::ExprDict<LocationRange>;
|
||||
pub type ExprSet = super::generic::ExprSet<LocationRange>;
|
||||
pub type ExprListComp = super::generic::ExprListComp<LocationRange>;
|
||||
pub type ExprSetComp = super::generic::ExprSetComp<LocationRange>;
|
||||
pub type ExprDictComp = super::generic::ExprDictComp<LocationRange>;
|
||||
pub type ExprGeneratorExp = super::generic::ExprGeneratorExp<LocationRange>;
|
||||
pub type ExprAwait = super::generic::ExprAwait<LocationRange>;
|
||||
pub type ExprYield = super::generic::ExprYield<LocationRange>;
|
||||
pub type ExprYieldFrom = super::generic::ExprYieldFrom<LocationRange>;
|
||||
pub type ExprCompare = super::generic::ExprCompare<LocationRange>;
|
||||
pub type ExprCall = super::generic::ExprCall<LocationRange>;
|
||||
pub type ExprFormattedValue = super::generic::ExprFormattedValue<LocationRange>;
|
||||
pub type ExprJoinedStr = super::generic::ExprJoinedStr<LocationRange>;
|
||||
pub type ExprConstant = super::generic::ExprConstant;
|
||||
pub type ExprAttribute = super::generic::ExprAttribute<LocationRange>;
|
||||
pub type ExprSubscript = super::generic::ExprSubscript<LocationRange>;
|
||||
pub type ExprStarred = super::generic::ExprStarred<LocationRange>;
|
||||
pub type ExprName = super::generic::ExprName;
|
||||
pub type ExprList = super::generic::ExprList<LocationRange>;
|
||||
pub type ExprTuple = super::generic::ExprTuple<LocationRange>;
|
||||
pub type ExprSlice = super::generic::ExprSlice<LocationRange>;
|
||||
pub type ExprContext = super::generic::ExprContext;
|
||||
pub type Boolop = super::generic::Boolop;
|
||||
pub type Operator = super::generic::Operator;
|
||||
pub type Unaryop = super::generic::Unaryop;
|
||||
pub type Cmpop = super::generic::Cmpop;
|
||||
pub type Comprehension = super::generic::Comprehension<LocationRange>;
|
||||
pub type Excepthandler = super::generic::Excepthandler<LocationRange>;
|
||||
pub type ExcepthandlerKind = super::generic::ExcepthandlerKind<LocationRange>;
|
||||
pub type ExcepthandlerExceptHandler = super::generic::ExcepthandlerExceptHandler<LocationRange>;
|
||||
pub type Arguments = super::generic::Arguments<LocationRange>;
|
||||
pub type Arg = super::generic::Arg<LocationRange>;
|
||||
pub type ArgData = super::generic::ArgData<LocationRange>;
|
||||
pub type Keyword = super::generic::Keyword<LocationRange>;
|
||||
pub type KeywordData = super::generic::KeywordData<LocationRange>;
|
||||
pub type Alias = super::generic::Alias<LocationRange>;
|
||||
pub type AliasData = super::generic::AliasData;
|
||||
pub type Withitem = super::generic::Withitem<LocationRange>;
|
||||
pub type MatchCase = super::generic::MatchCase<LocationRange>;
|
||||
pub type Pattern = super::generic::Pattern<LocationRange>;
|
||||
pub type PatternKind = super::generic::PatternKind<LocationRange>;
|
||||
pub type PatternMatchValue = super::generic::PatternMatchValue<LocationRange>;
|
||||
pub type PatternMatchSingleton = super::generic::PatternMatchSingleton;
|
||||
pub type PatternMatchSequence = super::generic::PatternMatchSequence<LocationRange>;
|
||||
pub type PatternMatchMapping = super::generic::PatternMatchMapping<LocationRange>;
|
||||
pub type PatternMatchClass = super::generic::PatternMatchClass<LocationRange>;
|
||||
pub type PatternMatchStar = super::generic::PatternMatchStar;
|
||||
pub type PatternMatchAs = super::generic::PatternMatchAs<LocationRange>;
|
||||
pub type PatternMatchOr = super::generic::PatternMatchOr<LocationRange>;
|
||||
pub type TypeIgnore = super::generic::TypeIgnore;
|
||||
pub type TypeIgnoreTypeIgnore = super::generic::TypeIgnoreTypeIgnore;
|
|
@ -1,41 +1,14 @@
|
|||
use crate::attributed::Attributed;
|
||||
use crate::fold_helpers::Foldable;
|
||||
use rustpython_compiler_core::{
|
||||
text_size::{TextRange, TextSize},
|
||||
Location, LocationRange,
|
||||
};
|
||||
use crate::location::SourceRange;
|
||||
use rustpython_compiler_core::SourceLocator;
|
||||
|
||||
/// Converts source code byte-offset to Python convention line and column numbers.
|
||||
#[derive(Default)]
|
||||
pub struct Locator<'a> {
|
||||
source: &'a str,
|
||||
pub fn locate<X: Foldable<(), SourceRange>>(locator: &mut SourceLocator, ast: X) -> X::Mapped {
|
||||
ast.fold(locator).unwrap()
|
||||
}
|
||||
|
||||
impl<'a> Locator<'a> {
|
||||
#[inline]
|
||||
pub fn new(source: &'a str) -> Self {
|
||||
Self { source }
|
||||
}
|
||||
|
||||
pub fn source(&'a self) -> &'a str {
|
||||
self.source
|
||||
}
|
||||
|
||||
pub fn locate(&mut self, offset: TextSize) -> Location {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn locate_range(&mut self, range: TextRange) -> LocationRange {
|
||||
self.locate(range.start())..self.locate(range.end())
|
||||
}
|
||||
|
||||
pub fn locate_ast<X: Foldable<(), LocationRange>>(&mut self, ast: X) -> X::Mapped {
|
||||
ast.fold(self).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::fold::Fold<()> for Locator<'_> {
|
||||
type TargetU = LocationRange;
|
||||
impl crate::fold::Fold<()> for SourceLocator<'_> {
|
||||
type TargetU = SourceRange;
|
||||
type Error = std::convert::Infallible;
|
||||
|
||||
#[cold]
|
||||
|
@ -47,10 +20,11 @@ impl crate::fold::Fold<()> for Locator<'_> {
|
|||
&mut self,
|
||||
node: Attributed<T, ()>,
|
||||
) -> Result<Attributed<T, Self::TargetU>, Self::Error> {
|
||||
let location = self.locate_range(node.range);
|
||||
let start = self.locate(node.range.start());
|
||||
let end = self.locate(node.range.end());
|
||||
Ok(Attributed {
|
||||
range: node.range,
|
||||
custom: location,
|
||||
custom: (start..end).into(),
|
||||
node: node.node,
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue