Skip to content

Python location transform and related refactoring #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adapt SourceLocation
  • Loading branch information
youknowone committed May 9, 2023
commit 09a6afdd04587ccc8841a88e34890c9409d8649d
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ rand = "0.8.5"
serde = "1.0"
static_assertions = "1.1"
unicode_names2 = { version = "0.6.0", git = "https://github.com/youknowone/unicode_names2.git", rev = "4ce16aa85cbcdd9cc830410f1a72ef9a235f2fde" }
ruff_python_ast = { git = "https://github.com/youknowone/ruff.git", rev = "583df5c1fa43b2732896219f8ab425116c140c80" }
# ruff_python_ast = { path = "../ruff/crates/ruff_python_ast" }

[profile.dev.package."*"]
opt-level = 3
Expand Down
15 changes: 8 additions & 7 deletions ast/asdl_rs.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,11 @@ def gen_construction(self, cons_path, cons, name, depth):
def extract_location(self, typename, depth):
row = self.decode_field(asdl.Field("int", "lineno"), typename)
column = self.decode_field(asdl.Field("int", "col_offset"), typename)
self.emit(f"let _location = Location::new({row}, {column});", depth)
self.emit(f"""let _location = {{
let row = try_location_field({row}, _vm)?;
let column = try_location_field({column}, _vm)?;
SourceLocation {{ row, column }}
}};""", depth)

def decode_field(self, field, typename):
name = json.dumps(field.name)
Expand All @@ -785,10 +789,7 @@ def write_generic_def(mod, typeinfo, f):
f.write(
textwrap.dedent(
"""
#![allow(clippy::derive_partial_eq_without_eq)]

pub use crate::{Attributed, constant::*};
use rustpython_compiler_core::{text_size::{TextSize, TextRange}};

type Ident = String;
\n
Expand All @@ -804,15 +805,15 @@ def write_located_def(typeinfo, f):
f.write(
textwrap.dedent(
"""
use rustpython_compiler_core::LocationRange;
use crate::location::SourceRange;

pub type Located<T> = super::generic::Attributed<T, LocationRange>;
pub type Located<T> = super::generic::Attributed<T, SourceRange>;
"""
)
)
for info in typeinfo.values():
if info.has_userdata:
generics = "::<LocationRange>"
generics = "::<SourceRange>"
else:
generics = ""
f.write(
Expand Down
12 changes: 5 additions & 7 deletions ast/src/attributed.rs
Original file line number Diff line number Diff line change
@@ -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 = ()> {
Expand Down Expand Up @@ -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
}
}
Expand Down
3 changes: 0 additions & 3 deletions ast/src/generic.rs → ast/src/gen/generic.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
95 changes: 95 additions & 0 deletions ast/src/gen/located.rs
Original file line number Diff line number Diff line change
@@ -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;
46 changes: 42 additions & 4 deletions ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,58 @@ 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;

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),
}
}
}
}
95 changes: 0 additions & 95 deletions ast/src/located.rs

This file was deleted.

Loading