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

Commit a23c23b

Browse files
committed
Add TextRange to Identifier
1 parent 9fdaab3 commit a23c23b

File tree

77 files changed

+7997
-15344
lines changed

Some content is hidden

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

77 files changed

+7997
-15344
lines changed

ast/asdl_rs.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,12 @@ def visitField(self, field, parent, vis, depth, constructor=None):
514514
if typ == "Int":
515515
typ = BUILTIN_INT_NAMES.get(field.name, typ)
516516
name = rust_field(field.name)
517+
518+
# Use a String, rather than an Identifier, for the `id` field of `Expr::Name`.
519+
# Names already include a range, so there's no need to duplicate the span.
520+
if name == "id":
521+
typ = "String"
522+
517523
self.emit(f"{vis}{name}: {typ},", depth)
518524

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

ast/src/builtin.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,85 +2,86 @@
22
33
use num_bigint::BigInt;
44

5+
use crate::Ranged;
6+
use rustpython_parser_core::text_size::TextRange;
7+
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
@@ -1447,7 +1447,7 @@ impl<R> From<ExprStarred<R>> for Ast<R> {
14471447
#[derive(Clone, Debug, PartialEq)]
14481448
pub struct ExprName<R = TextRange> {
14491449
pub range: R,
1450-
pub id: Identifier,
1450+
pub id: String,
14511451
pub ctx: ExprContext,
14521452
}
14531453

parser/src/function.rs

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

121121
keywords.push(ast::Keyword {
122-
arg: name.map(ast::Identifier::new),
122+
arg: name.map(|name| ast::Identifier::new(name, TextRange::new(start, end))),
123123
value,
124124
range: TextRange::new(start, end),
125125
});

parser/src/python.lalrpop

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

@@ -279,14 +279,14 @@ ImportAsAlias<I>: ast::Alias = {
279279

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

@@ -564,8 +564,8 @@ CapturePattern: ast::Pattern = {
564564
}
565565

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

@@ -1200,7 +1200,7 @@ NamedExpression: ast::Expr = {
12001200
ast::Expr::NamedExpr(
12011201
ast::ExprNamedExpr {
12021202
target: Box::new(ast::Expr::Name(
1203-
ast::ExprName { id, ctx: ast::ExprContext::Store, range: (location..end_location).into() },
1203+
ast::ExprName { id: id.into(), ctx: ast::ExprContext::Store, range: (location..end_location).into() },
12041204
)),
12051205
range: (location..value.end()).into(),
12061206
value: Box::new(value),
@@ -1427,8 +1427,8 @@ Atom<Goal>: ast::Expr = {
14271427
<location:@L> <value:Constant> <end_location:@R> => ast::Expr::Constant(
14281428
ast::ExprConstant { value, kind: None, range: (location..end_location).into() }
14291429
),
1430-
<location:@L> <name:Identifier> <end_location:@R> => ast::Expr::Name(
1431-
ast::ExprName { id: name, ctx: ast::ExprContext::Load, range: (location..end_location).into() }
1430+
<location:@L> <id:Identifier> <end_location:@R> => ast::Expr::Name(
1431+
ast::ExprName { id: id.into(), ctx: ast::ExprContext::Load, range: (location..end_location).into() }
14321432
),
14331433
<location:@L> "[" <e:ListLiteralValues?> "]"<end_location:@R> => {
14341434
let elts = e.unwrap_or_default();
@@ -1663,7 +1663,7 @@ Constant: ast::Constant = {
16631663
};
16641664

16651665
Identifier: ast::Identifier = {
1666-
<s:name> => ast::Identifier::new(s)
1666+
<location:@L> <s:name> <end_location:@R> => ast::Identifier::new(s, (location..end_location).into())
16671667
};
16681668

16691669
// Hook external lexer:

0 commit comments

Comments
 (0)