Skip to content

Commit 5b2af30

Browse files
authored
Merge pull request #17 from youknowone/ruff
ruff integration support
2 parents d6b6df5 + cbe4e8c commit 5b2af30

File tree

14 files changed

+306
-253
lines changed

14 files changed

+306
-253
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ jobs:
3737

3838
- uses: Swatinem/rust-cache@v2
3939

40+
- name: run tests with default features
41+
run: cargo test --all
4042
- name: run tests with embedded parser
4143
run: cargo test --all --no-default-features
4244
- name: run tests with generated parser

Cargo.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ members = [
1616
]
1717

1818
[workspace.dependencies]
19-
rustpython-ast = { path = "ast", version = "0.2.0" }
20-
rustpython-parser-core = { path = "core", version = "0.2.0" }
19+
rustpython-ast = { path = "ast", version = "0.2.0", default-features = false }
20+
rustpython-parser-core = { path = "core", version = "0.2.0", default-features = false }
2121
rustpython-literal = { path = "literal", version = "0.2.0" }
22-
ruff_text_size = { path = "ruff_text_size" }
23-
ruff_source_location = { path = "ruff_source_location" }
2422

2523
ahash = "0.7.6"
2624
anyhow = "1.0.45"

ast/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ repository = "https://github.com/RustPython/RustPython"
88
license = "MIT"
99

1010
[features]
11-
default = ["constant-optimization", "fold", "source-code"]
11+
default = ["location"]
1212
constant-optimization = ["fold"]
13-
source-code = ["fold"]
13+
location = ["fold", "rustpython-parser-core/location"]
1414
fold = []
1515
unparse = ["rustpython-literal"]
1616
visitor = []

ast/src/builtin.rs

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,33 @@ impl Identifier {
1313
pub fn new(s: impl Into<String>) -> Self {
1414
Self(s.into())
1515
}
16+
}
17+
18+
impl std::cmp::PartialEq<str> for Identifier {
1619
#[inline]
17-
pub fn as_str(&self) -> &str {
18-
self.0.as_str()
20+
fn eq(&self, other: &str) -> bool {
21+
self.0 == other
1922
}
2023
}
2124

22-
impl std::string::ToString for Identifier {
25+
impl std::cmp::PartialEq<String> for Identifier {
2326
#[inline]
24-
fn to_string(&self) -> String {
25-
self.0.clone()
27+
fn eq(&self, other: &String) -> bool {
28+
&self.0 == other
29+
}
30+
}
31+
32+
impl std::ops::Deref for Identifier {
33+
type Target = String;
34+
#[inline]
35+
fn deref(&self) -> &Self::Target {
36+
&self.0
37+
}
38+
}
39+
40+
impl std::fmt::Display for Identifier {
41+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42+
self.0.fmt(f)
2643
}
2744
}
2845

@@ -33,24 +50,46 @@ impl From<Identifier> for String {
3350
}
3451
}
3552

36-
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
53+
impl From<String> for Identifier {
54+
#[inline]
55+
fn from(id: String) -> Self {
56+
Self(id)
57+
}
58+
}
59+
60+
impl<'a> From<&'a str> for Identifier {
61+
#[inline]
62+
fn from(id: &'a str) -> Identifier {
63+
id.to_owned().into()
64+
}
65+
}
66+
67+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
3768
pub struct Int(u32);
3869

3970
impl Int {
4071
pub fn new(i: u32) -> Self {
4172
Self(i)
4273
}
43-
pub fn new_bool(i: bool) -> Self {
44-
Self(i as u32)
45-
}
4674
pub fn to_u32(&self) -> u32 {
4775
self.0
4876
}
4977
pub fn to_usize(&self) -> usize {
5078
self.0 as _
5179
}
52-
pub fn to_bool(&self) -> bool {
53-
self.0 > 0
80+
}
81+
82+
impl std::cmp::PartialEq<u32> for Int {
83+
#[inline]
84+
fn eq(&self, other: &u32) -> bool {
85+
self.0 == *other
86+
}
87+
}
88+
89+
impl std::cmp::PartialEq<usize> for Int {
90+
#[inline]
91+
fn eq(&self, other: &usize) -> bool {
92+
self.0 as usize == *other
5493
}
5594
}
5695

@@ -165,11 +204,11 @@ impl<T, U> Attributed<T, U> {
165204

166205
impl<T> Attributed<T, ()> {
167206
/// Creates a new node that spans the position specified by `range`.
168-
pub fn new(range: impl Into<TextRange>, node: T) -> Self {
207+
pub fn new(range: impl Into<TextRange>, node: impl Into<T>) -> Self {
169208
Self {
170209
range: range.into(),
171210
custom: (),
172-
node,
211+
node: node.into(),
173212
}
174213
}
175214

ast/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod generic {
88
include!("gen/generic.rs");
99
}
1010
mod impls;
11-
#[cfg(feature = "source-code")]
11+
#[cfg(feature = "location")]
1212
mod source_locator;
1313
#[cfg(feature = "unparse")]
1414
mod unparse;
@@ -32,11 +32,12 @@ mod visitor {
3232
include!("gen/visitor.rs");
3333
}
3434

35-
#[cfg(feature = "source-code")]
35+
#[cfg(feature = "location")]
3636
pub mod located {
3737
include!("gen/located.rs");
3838
}
3939

40+
#[cfg(feature = "location")]
4041
pub use rustpython_parser_core::source_code;
4142
#[cfg(feature = "visitor")]
4243
pub use visitor::Visitor;

core/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ serde = { version = "1.0.133", optional = true, default-features = false, featur
2020
lz4_flex = "0.9.2"
2121

2222
[features]
23-
default = ["source-code"]
24-
source-code = ["ruff_source_location"]
23+
default = []
24+
location = ["ruff_source_location"]

core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
mod error;
55
mod format;
66
pub mod mode;
7-
#[cfg(feature = "source-code")]
7+
#[cfg(feature = "location")]
88
pub mod source_code;
99

1010
pub use error::BaseError;

literal/src/escape.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[derive(Debug, Clone, Copy)]
1+
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
22
pub enum Quote {
33
Single,
44
Double,
@@ -80,15 +80,21 @@ pub struct UnicodeEscape<'a> {
8080
}
8181

8282
impl<'a> UnicodeEscape<'a> {
83+
#[inline]
8384
pub fn with_forced_quote(source: &'a str, quote: Quote) -> Self {
8485
let layout = EscapeLayout { quote, len: None };
8586
Self { source, layout }
8687
}
87-
pub fn new_repr(source: &'a str) -> Self {
88-
let layout = Self::repr_layout(source, Quote::Single);
88+
#[inline]
89+
pub fn with_preferred_quote(source: &'a str, quote: Quote) -> Self {
90+
let layout = Self::repr_layout(source, quote);
8991
Self { source, layout }
9092
}
91-
93+
#[inline]
94+
pub fn new_repr(source: &'a str) -> Self {
95+
Self::with_preferred_quote(source, Quote::Single)
96+
}
97+
#[inline]
9298
pub fn str_repr<'r>(&'a self) -> StrRepr<'r, 'a> {
9399
StrRepr(self)
94100
}
@@ -265,18 +271,25 @@ pub struct AsciiEscape<'a> {
265271
}
266272

267273
impl<'a> AsciiEscape<'a> {
274+
#[inline]
268275
pub fn new(source: &'a [u8], layout: EscapeLayout) -> Self {
269276
Self { source, layout }
270277
}
278+
#[inline]
271279
pub fn with_forced_quote(source: &'a [u8], quote: Quote) -> Self {
272280
let layout = EscapeLayout { quote, len: None };
273281
Self { source, layout }
274282
}
275-
pub fn new_repr(source: &'a [u8]) -> Self {
276-
let layout = Self::repr_layout(source, Quote::Single);
283+
#[inline]
284+
pub fn with_preferred_quote(source: &'a [u8], quote: Quote) -> Self {
285+
let layout = Self::repr_layout(source, quote);
277286
Self { source, layout }
278287
}
279-
288+
#[inline]
289+
pub fn new_repr(source: &'a [u8]) -> Self {
290+
Self::with_preferred_quote(source, Quote::Single)
291+
}
292+
#[inline]
280293
pub fn bytes_repr<'r>(&'a self) -> BytesRepr<'r, 'a> {
281294
BytesRepr(self)
282295
}

parser/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ license = "MIT"
99
edition = "2021"
1010

1111
[features]
12-
default = []
12+
default = ["location"]
13+
location = ["rustpython-ast/location"]
1314
serde = ["dep:serde", "rustpython-parser-core/serde"]
1415

1516
[build-dependencies]

parser/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@
113113
#![doc(html_root_url = "https://docs.rs/rustpython-parser/")]
114114

115115
pub use rustpython_ast as ast;
116-
pub use rustpython_parser_core::{source_code, text_size, Mode};
116+
#[cfg(feature = "location")]
117+
pub use rustpython_parser_core::source_code;
118+
pub use rustpython_parser_core::{text_size, Mode};
117119

118120
mod function;
119121
// Skip flattening lexer to distinguish from full parser

0 commit comments

Comments
 (0)