Skip to content

Commit 09a6afd

Browse files
committed
Adapt SourceLocation
1 parent a14e43e commit 09a6afd

File tree

117 files changed

+1606
-1676
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+1606
-1676
lines changed

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ rand = "0.8.5"
3232
serde = "1.0"
3333
static_assertions = "1.1"
3434
unicode_names2 = { version = "0.6.0", git = "https://github.com/youknowone/unicode_names2.git", rev = "4ce16aa85cbcdd9cc830410f1a72ef9a235f2fde" }
35+
ruff_python_ast = { git = "https://github.com/youknowone/ruff.git", rev = "583df5c1fa43b2732896219f8ab425116c140c80" }
36+
# ruff_python_ast = { path = "../ruff/crates/ruff_python_ast" }
3537

3638
[profile.dev.package."*"]
3739
opt-level = 3

ast/asdl_rs.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,11 @@ def gen_construction(self, cons_path, cons, name, depth):
761761
def extract_location(self, typename, depth):
762762
row = self.decode_field(asdl.Field("int", "lineno"), typename)
763763
column = self.decode_field(asdl.Field("int", "col_offset"), typename)
764-
self.emit(f"let _location = Location::new({row}, {column});", depth)
764+
self.emit(f"""let _location = {{
765+
let row = try_location_field({row}, _vm)?;
766+
let column = try_location_field({column}, _vm)?;
767+
SourceLocation {{ row, column }}
768+
}};""", depth)
765769

766770
def decode_field(self, field, typename):
767771
name = json.dumps(field.name)
@@ -785,10 +789,7 @@ def write_generic_def(mod, typeinfo, f):
785789
f.write(
786790
textwrap.dedent(
787791
"""
788-
#![allow(clippy::derive_partial_eq_without_eq)]
789-
790792
pub use crate::{Attributed, constant::*};
791-
use rustpython_compiler_core::{text_size::{TextSize, TextRange}};
792793
793794
type Ident = String;
794795
\n
@@ -804,15 +805,15 @@ def write_located_def(typeinfo, f):
804805
f.write(
805806
textwrap.dedent(
806807
"""
807-
use rustpython_compiler_core::LocationRange;
808+
use crate::location::SourceRange;
808809
809-
pub type Located<T> = super::generic::Attributed<T, LocationRange>;
810+
pub type Located<T> = super::generic::Attributed<T, SourceRange>;
810811
"""
811812
)
812813
)
813814
for info in typeinfo.values():
814815
if info.has_userdata:
815-
generics = "::<LocationRange>"
816+
generics = "::<SourceRange>"
816817
else:
817818
generics = ""
818819
f.write(

ast/src/attributed.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
use rustpython_compiler_core::{
2-
text_size::{TextRange, TextSize},
3-
Location, LocationRange,
4-
};
1+
use crate::location::{SourceLocation, SourceRange};
2+
use rustpython_compiler_core::text_size::{TextRange, TextSize};
53

64
#[derive(Clone, Debug, PartialEq)]
75
pub struct Attributed<T, U = ()> {
@@ -53,16 +51,16 @@ impl<T> Attributed<T, ()> {
5351
}
5452
}
5553

56-
impl<T> Attributed<T, LocationRange> {
54+
impl<T> Attributed<T, SourceRange> {
5755
/// Returns the absolute start position of the node from the beginning of the document.
5856
#[inline]
59-
pub const fn location(&self) -> Location {
57+
pub const fn location(&self) -> SourceLocation {
6058
self.custom.start
6159
}
6260

6361
/// Returns the absolute position at which the node ends in the source document.
6462
#[inline]
65-
pub const fn end_location(&self) -> Location {
63+
pub const fn end_location(&self) -> Option<SourceLocation> {
6664
self.custom.end
6765
}
6866
}

ast/src/generic.rs renamed to ast/src/gen/generic.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// File automatically generated by ast/asdl_rs.py.
22

3-
#![allow(clippy::derive_partial_eq_without_eq)]
4-
53
pub use crate::{constant::*, Attributed};
6-
use rustpython_compiler_core::text_size::{TextRange, TextSize};
74

85
type Ident = String;
96

ast/src/gen/located.rs

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// File automatically generated by ast/asdl_rs.py.
2+
3+
use crate::location::SourceRange;
4+
5+
pub type Located<T> = super::generic::Attributed<T, SourceRange>;
6+
pub type Mod = super::generic::Mod<SourceRange>;
7+
pub type ModModule = super::generic::ModModule<SourceRange>;
8+
pub type ModInteractive = super::generic::ModInteractive<SourceRange>;
9+
pub type ModExpression = super::generic::ModExpression<SourceRange>;
10+
pub type ModFunctionType = super::generic::ModFunctionType<SourceRange>;
11+
pub type Stmt = super::generic::Stmt<SourceRange>;
12+
pub type StmtKind = super::generic::StmtKind<SourceRange>;
13+
pub type StmtFunctionDef = super::generic::StmtFunctionDef<SourceRange>;
14+
pub type StmtAsyncFunctionDef = super::generic::StmtAsyncFunctionDef<SourceRange>;
15+
pub type StmtClassDef = super::generic::StmtClassDef<SourceRange>;
16+
pub type StmtReturn = super::generic::StmtReturn<SourceRange>;
17+
pub type StmtDelete = super::generic::StmtDelete<SourceRange>;
18+
pub type StmtAssign = super::generic::StmtAssign<SourceRange>;
19+
pub type StmtAugAssign = super::generic::StmtAugAssign<SourceRange>;
20+
pub type StmtAnnAssign = super::generic::StmtAnnAssign<SourceRange>;
21+
pub type StmtFor = super::generic::StmtFor<SourceRange>;
22+
pub type StmtAsyncFor = super::generic::StmtAsyncFor<SourceRange>;
23+
pub type StmtWhile = super::generic::StmtWhile<SourceRange>;
24+
pub type StmtIf = super::generic::StmtIf<SourceRange>;
25+
pub type StmtWith = super::generic::StmtWith<SourceRange>;
26+
pub type StmtAsyncWith = super::generic::StmtAsyncWith<SourceRange>;
27+
pub type StmtMatch = super::generic::StmtMatch<SourceRange>;
28+
pub type StmtRaise = super::generic::StmtRaise<SourceRange>;
29+
pub type StmtTry = super::generic::StmtTry<SourceRange>;
30+
pub type StmtTryStar = super::generic::StmtTryStar<SourceRange>;
31+
pub type StmtAssert = super::generic::StmtAssert<SourceRange>;
32+
pub type StmtImport = super::generic::StmtImport<SourceRange>;
33+
pub type StmtImportFrom = super::generic::StmtImportFrom<SourceRange>;
34+
pub type StmtGlobal = super::generic::StmtGlobal;
35+
pub type StmtNonlocal = super::generic::StmtNonlocal;
36+
pub type StmtExpr = super::generic::StmtExpr<SourceRange>;
37+
pub type Expr = super::generic::Expr<SourceRange>;
38+
pub type ExprKind = super::generic::ExprKind<SourceRange>;
39+
pub type ExprBoolOp = super::generic::ExprBoolOp<SourceRange>;
40+
pub type ExprNamedExpr = super::generic::ExprNamedExpr<SourceRange>;
41+
pub type ExprBinOp = super::generic::ExprBinOp<SourceRange>;
42+
pub type ExprUnaryOp = super::generic::ExprUnaryOp<SourceRange>;
43+
pub type ExprLambda = super::generic::ExprLambda<SourceRange>;
44+
pub type ExprIfExp = super::generic::ExprIfExp<SourceRange>;
45+
pub type ExprDict = super::generic::ExprDict<SourceRange>;
46+
pub type ExprSet = super::generic::ExprSet<SourceRange>;
47+
pub type ExprListComp = super::generic::ExprListComp<SourceRange>;
48+
pub type ExprSetComp = super::generic::ExprSetComp<SourceRange>;
49+
pub type ExprDictComp = super::generic::ExprDictComp<SourceRange>;
50+
pub type ExprGeneratorExp = super::generic::ExprGeneratorExp<SourceRange>;
51+
pub type ExprAwait = super::generic::ExprAwait<SourceRange>;
52+
pub type ExprYield = super::generic::ExprYield<SourceRange>;
53+
pub type ExprYieldFrom = super::generic::ExprYieldFrom<SourceRange>;
54+
pub type ExprCompare = super::generic::ExprCompare<SourceRange>;
55+
pub type ExprCall = super::generic::ExprCall<SourceRange>;
56+
pub type ExprFormattedValue = super::generic::ExprFormattedValue<SourceRange>;
57+
pub type ExprJoinedStr = super::generic::ExprJoinedStr<SourceRange>;
58+
pub type ExprConstant = super::generic::ExprConstant;
59+
pub type ExprAttribute = super::generic::ExprAttribute<SourceRange>;
60+
pub type ExprSubscript = super::generic::ExprSubscript<SourceRange>;
61+
pub type ExprStarred = super::generic::ExprStarred<SourceRange>;
62+
pub type ExprName = super::generic::ExprName;
63+
pub type ExprList = super::generic::ExprList<SourceRange>;
64+
pub type ExprTuple = super::generic::ExprTuple<SourceRange>;
65+
pub type ExprSlice = super::generic::ExprSlice<SourceRange>;
66+
pub type ExprContext = super::generic::ExprContext;
67+
pub type Boolop = super::generic::Boolop;
68+
pub type Operator = super::generic::Operator;
69+
pub type Unaryop = super::generic::Unaryop;
70+
pub type Cmpop = super::generic::Cmpop;
71+
pub type Comprehension = super::generic::Comprehension<SourceRange>;
72+
pub type Excepthandler = super::generic::Excepthandler<SourceRange>;
73+
pub type ExcepthandlerKind = super::generic::ExcepthandlerKind<SourceRange>;
74+
pub type ExcepthandlerExceptHandler = super::generic::ExcepthandlerExceptHandler<SourceRange>;
75+
pub type Arguments = super::generic::Arguments<SourceRange>;
76+
pub type Arg = super::generic::Arg<SourceRange>;
77+
pub type ArgData = super::generic::ArgData<SourceRange>;
78+
pub type Keyword = super::generic::Keyword<SourceRange>;
79+
pub type KeywordData = super::generic::KeywordData<SourceRange>;
80+
pub type Alias = super::generic::Alias<SourceRange>;
81+
pub type AliasData = super::generic::AliasData;
82+
pub type Withitem = super::generic::Withitem<SourceRange>;
83+
pub type MatchCase = super::generic::MatchCase<SourceRange>;
84+
pub type Pattern = super::generic::Pattern<SourceRange>;
85+
pub type PatternKind = super::generic::PatternKind<SourceRange>;
86+
pub type PatternMatchValue = super::generic::PatternMatchValue<SourceRange>;
87+
pub type PatternMatchSingleton = super::generic::PatternMatchSingleton;
88+
pub type PatternMatchSequence = super::generic::PatternMatchSequence<SourceRange>;
89+
pub type PatternMatchMapping = super::generic::PatternMatchMapping<SourceRange>;
90+
pub type PatternMatchClass = super::generic::PatternMatchClass<SourceRange>;
91+
pub type PatternMatchStar = super::generic::PatternMatchStar;
92+
pub type PatternMatchAs = super::generic::PatternMatchAs<SourceRange>;
93+
pub type PatternMatchOr = super::generic::PatternMatchOr<SourceRange>;
94+
pub type TypeIgnore = super::generic::TypeIgnore;
95+
pub type TypeIgnoreTypeIgnore = super::generic::TypeIgnoreTypeIgnore;

ast/src/lib.rs

+42-4
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,58 @@ mod attributed;
22
mod constant;
33
#[cfg(feature = "fold")]
44
mod fold_helpers;
5-
mod generic;
5+
mod generic {
6+
#![allow(clippy::derive_partial_eq_without_eq)]
7+
include!("gen/generic.rs");
8+
}
69
mod impls;
710
#[cfg(feature = "location")]
8-
pub mod located;
11+
pub mod located {
12+
include!("gen/located.rs");
13+
}
914
#[cfg(feature = "location")]
1015
mod locator;
16+
#[cfg(feature = "location")]
17+
pub use crate::locator::locate;
18+
#[cfg(feature = "location")]
19+
pub use rustpython_compiler_core::SourceLocator;
1120

1221
#[cfg(feature = "unparse")]
1322
mod unparse;
1423

1524
pub use attributed::Attributed;
1625
pub use constant::{Constant, ConversionFlag};
1726
pub use generic::*;
18-
#[cfg(feature = "location")]
19-
pub use locator::Locator;
2027

2128
pub type Suite<U = ()> = Vec<Stmt<U>>;
29+
30+
pub mod location {
31+
pub use rustpython_compiler_core::source_code::{OneIndexed, SourceLocation};
32+
33+
#[derive(Debug)]
34+
pub struct SourceRange {
35+
pub start: SourceLocation,
36+
pub end: Option<SourceLocation>,
37+
}
38+
39+
impl SourceRange {
40+
pub fn new(start: SourceLocation, end: SourceLocation) -> Self {
41+
Self {
42+
start,
43+
end: Some(end),
44+
}
45+
}
46+
pub fn unwrap_end(&self) -> SourceLocation {
47+
self.end.unwrap()
48+
}
49+
}
50+
51+
impl From<std::ops::Range<SourceLocation>> for SourceRange {
52+
fn from(value: std::ops::Range<SourceLocation>) -> Self {
53+
Self {
54+
start: value.start,
55+
end: Some(value.end),
56+
}
57+
}
58+
}
59+
}

ast/src/located.rs

-95
This file was deleted.

0 commit comments

Comments
 (0)