Skip to content
This repository was archived by the owner on Jul 27, 2023. It is now read-only.

Commit ea1106a

Browse files
committed
Add TextRange to Identifier
1 parent f0d200c commit ea1106a

File tree

48 files changed

+3188
-3213
lines changed

Some content is hidden

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

48 files changed

+3188
-3213
lines changed

ast/asdl_rs.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,12 @@ def visitField(self, field, parent, vis, depth, constructor=None):
630630
if typ == "Int":
631631
typ = BUILTIN_INT_NAMES.get(field.name, typ)
632632
name = rust_field(field.name)
633+
634+
# Use a String, rather than an Identifier, for the `id` field of `Expr::Name`.
635+
# Names already include a range, so there's no need to duplicate the span.
636+
if name == "id":
637+
typ = "String"
638+
633639
self.emit(f"{vis}{name}: {typ},", depth)
634640

635641
def visitProduct(self, product, type, depth):

ast/src/builtin.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,87 @@
11
//! `builtin_types` in asdl.py and Attributed
22
3+
use rustpython_parser_core::text_size::TextRange;
4+
35
use crate::bigint::BigInt;
6+
use crate::Ranged;
47

58
pub type String = std::string::String;
69

710
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
8-
pub struct Identifier(String);
11+
pub struct Identifier {
12+
id: String,
13+
range: TextRange,
14+
}
915

1016
impl Identifier {
1117
#[inline]
12-
pub fn new(s: impl Into<String>) -> Self {
13-
Self(s.into())
18+
pub fn new(id: impl Into<String>, range: TextRange) -> Self {
19+
Self {
20+
id: id.into(),
21+
range,
22+
}
1423
}
1524
}
1625

1726
impl Identifier {
1827
#[inline]
1928
pub fn as_str(&self) -> &str {
20-
self.0.as_str()
29+
self.id.as_str()
2130
}
2231
}
2332

24-
impl std::cmp::PartialEq<str> for Identifier {
33+
impl PartialEq<str> for Identifier {
2534
#[inline]
2635
fn eq(&self, other: &str) -> bool {
27-
self.0 == other
36+
self.id == other
2837
}
2938
}
3039

31-
impl std::cmp::PartialEq<String> for Identifier {
40+
impl PartialEq<String> for Identifier {
3241
#[inline]
3342
fn eq(&self, other: &String) -> bool {
34-
&self.0 == other
43+
&self.id == other
3544
}
3645
}
3746

3847
impl std::ops::Deref for Identifier {
3948
type Target = str;
4049
#[inline]
4150
fn deref(&self) -> &Self::Target {
42-
self.0.as_str()
51+
self.id.as_str()
4352
}
4453
}
4554

4655
impl AsRef<str> for Identifier {
4756
#[inline]
4857
fn as_ref(&self) -> &str {
49-
self.0.as_str()
58+
self.id.as_str()
5059
}
5160
}
5261

5362
impl AsRef<String> for Identifier {
5463
#[inline]
5564
fn as_ref(&self) -> &String {
56-
&self.0
65+
&self.id
5766
}
5867
}
5968

6069
impl std::fmt::Display for Identifier {
6170
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62-
self.0.fmt(f)
71+
self.id.fmt(f)
6372
}
6473
}
6574

6675
impl From<Identifier> for String {
6776
#[inline]
68-
fn from(id: Identifier) -> String {
69-
id.0
77+
fn from(identifier: Identifier) -> String {
78+
identifier.id
7079
}
7180
}
7281

73-
impl From<String> for Identifier {
74-
#[inline]
75-
fn from(id: String) -> Self {
76-
Self(id)
77-
}
78-
}
79-
80-
impl<'a> From<&'a str> for Identifier {
81-
#[inline]
82-
fn from(id: &'a str) -> Identifier {
83-
id.to_owned().into()
82+
impl Ranged for Identifier {
83+
fn range(&self) -> TextRange {
84+
self.range
8485
}
8586
}
8687

@@ -207,6 +208,7 @@ impl std::fmt::Display for Constant {
207208
#[cfg(test)]
208209
mod tests {
209210
use super::*;
211+
210212
#[test]
211213
fn test_is_macro() {
212214
let none = Constant::None;

ast/src/gen/generic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,7 @@ impl<R> From<ExprStarred<R>> for Ast<R> {
15631563
#[derive(Clone, Debug, PartialEq)]
15641564
pub struct ExprName<R = TextRange> {
15651565
pub range: R,
1566-
pub id: Identifier,
1566+
pub id: String,
15671567
pub ctx: ExprContext,
15681568
}
15691569

ast/src/impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ impl<R> Expr<R> {
5757
#[cfg(target_arch = "x86_64")]
5858
static_assertions::assert_eq_size!(crate::Expr, [u8; 72]);
5959
#[cfg(target_arch = "x86_64")]
60-
static_assertions::assert_eq_size!(crate::Stmt, [u8; 136]);
60+
static_assertions::assert_eq_size!(crate::Stmt, [u8; 144]);
6161
#[cfg(target_arch = "x86_64")]
6262
static_assertions::assert_eq_size!(crate::Pattern, [u8; 96]);
6363
#[cfg(target_arch = "x86_64")]
64-
static_assertions::assert_eq_size!(crate::ExceptHandler, [u8; 64]);
64+
static_assertions::assert_eq_size!(crate::ExceptHandler, [u8; 72]);

parser/src/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub(crate) fn parse_args(func_args: Vec<FunctionArgument>) -> Result<ArgumentLis
103103
}
104104

105105
keywords.push(ast::Keyword {
106-
arg: name.map(ast::Identifier::new),
106+
arg: name.map(|name| ast::Identifier::new(name, TextRange::new(start, end))),
107107
value,
108108
range: TextRange::new(start, end),
109109
});

parser/src/parser.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,10 @@ impl Parse for ast::Identifier {
213213
) -> Result<Self, ParseError> {
214214
let expr = ast::Expr::parse_tokens(lxr, source_path)?;
215215
match expr {
216-
ast::Expr::Name(name) => Ok(name.id),
216+
ast::Expr::Name(name) => {
217+
let range = name.range();
218+
Ok(ast::Identifier::new(name.id, range))
219+
}
217220
expr => Err(ParseError {
218221
error: ParseErrorType::InvalidToken,
219222
offset: expr.range().start(),

parser/src/python.lalrpop

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ ImportAsNames: Vec<ast::Alias> = {
266266
<location:@L> "(" <i:OneOrMore<ImportAsAlias<Identifier>>> ","? ")" <end_location:@R> => i,
267267
<location:@L> "*" <end_location:@R> => {
268268
// Star import all
269-
vec![ast::Alias { name: ast::Identifier::new("*"), asname: None, range: (location..end_location).into() }]
269+
vec![ast::Alias { name: ast::Identifier::new("*", (location..end_location).into()), asname: None, range: (location..end_location).into() }]
270270
},
271271
};
272272

@@ -278,14 +278,14 @@ ImportAsAlias<I>: ast::Alias = {
278278

279279
// A name like abc or abc.def.ghi
280280
DottedName: ast::Identifier = {
281-
<n:name> => ast::Identifier::new(n),
282-
<n:name> <n2: ("." Identifier)+> => {
281+
<location:@L> <n:name> <end_location:@R> => ast::Identifier::new(n, (location..end_location).into()),
282+
<location:@L> <n:name> <n2: ("." Identifier)+> <end_location:@R> => {
283283
let mut r = n.to_string();
284284
for x in n2 {
285285
r.push('.');
286286
r.push_str(x.1.as_str());
287287
}
288-
ast::Identifier::new(r)
288+
ast::Identifier::new(r, (location..end_location).into())
289289
},
290290
};
291291

@@ -563,8 +563,8 @@ CapturePattern: ast::Pattern = {
563563
}
564564

565565
MatchName: ast::Expr = {
566-
<location:@L> <name:Identifier> <end_location:@R> => ast::Expr::Name(
567-
ast::ExprName { id: name, ctx: ast::ExprContext::Load, range: (location..end_location).into() },
566+
<location:@L> <id:Identifier> <end_location:@R> => ast::Expr::Name(
567+
ast::ExprName { id: id.into(), ctx: ast::ExprContext::Load, range: (location..end_location).into() },
568568
),
569569
}
570570

@@ -1189,7 +1189,7 @@ NamedExpression: ast::Expr = {
11891189
ast::Expr::NamedExpr(
11901190
ast::ExprNamedExpr {
11911191
target: Box::new(ast::Expr::Name(
1192-
ast::ExprName { id, ctx: ast::ExprContext::Store, range: (location..end_location).into() },
1192+
ast::ExprName { id: id.into(), ctx: ast::ExprContext::Store, range: (location..end_location).into() },
11931193
)),
11941194
range: (location..value.end()).into(),
11951195
value: Box::new(value),
@@ -1405,8 +1405,8 @@ Atom<Goal>: ast::Expr = {
14051405
<location:@L> <value:Constant> <end_location:@R> => ast::Expr::Constant(
14061406
ast::ExprConstant { value, kind: None, range: (location..end_location).into() }
14071407
),
1408-
<location:@L> <name:Identifier> <end_location:@R> => ast::Expr::Name(
1409-
ast::ExprName { id: name, ctx: ast::ExprContext::Load, range: (location..end_location).into() }
1408+
<location:@L> <id:Identifier> <end_location:@R> => ast::Expr::Name(
1409+
ast::ExprName { id: id.into(), ctx: ast::ExprContext::Load, range: (location..end_location).into() }
14101410
),
14111411
<location:@L> "[" <e:ListLiteralValues?> "]"<end_location:@R> => {
14121412
let elts = e.unwrap_or_default();
@@ -1641,7 +1641,7 @@ Constant: ast::Constant = {
16411641
};
16421642

16431643
Identifier: ast::Identifier = {
1644-
<s:name> => ast::Identifier::new(s)
1644+
<location:@L> <s:name> <end_location:@R> => ast::Identifier::new(s, (location..end_location).into())
16451645
};
16461646

16471647
// Hook external lexer:

0 commit comments

Comments
 (0)