Skip to content

Commit 9af9c16

Browse files
authored
Merge pull request RustPython#4417 from harupy/add-with-offset-methods
Add `with_col_offset` and `with_row_offset` to `Location` for conveniece
2 parents 36ec2e4 + 9e0d31d commit 9af9c16

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

compiler/codegen/src/symboltable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl SymbolTableError {
168168
pub fn into_codegen_error(self, source_path: String) -> CodegenError {
169169
CodegenError {
170170
error: CodegenErrorType::SyntaxError(self.error),
171-
location: Location::new(self.location.row(), self.location.column() + 1),
171+
location: self.location.with_col_offset(1),
172172
source_path,
173173
}
174174
}

compiler/core/src/location.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,34 @@ impl Location {
5858
self.row += 1;
5959
self.column = 0;
6060
}
61+
62+
pub fn with_col_offset<T: TryInto<isize>>(&self, offset: T) -> Self
63+
where
64+
<T as TryInto<isize>>::Error: std::fmt::Debug,
65+
{
66+
let column = (self.column as isize
67+
+ offset
68+
.try_into()
69+
.expect("offset should be able to convert to isize")) as u32;
70+
Self {
71+
row: self.row,
72+
column,
73+
}
74+
}
75+
76+
pub fn with_row_offset<T: TryInto<isize>>(&self, offset: T) -> Self
77+
where
78+
<T as TryInto<isize>>::Error: std::fmt::Debug,
79+
{
80+
let row = (self.row as isize
81+
+ offset
82+
.try_into()
83+
.expect("offset should be able to convert to isize")) as u32;
84+
Self {
85+
row,
86+
column: self.column,
87+
}
88+
}
6189
}
6290

6391
#[cfg(test)]
@@ -77,4 +105,16 @@ mod tests {
77105
assert!(Location::new(1, 1) < Location::new(2, 1));
78106
assert!(Location::new(1, 2) < Location::new(2, 1));
79107
}
108+
109+
#[test]
110+
fn test_with_col_offset() {
111+
assert_eq!(Location::new(1, 1).with_col_offset(1), Location::new(1, 2));
112+
assert_eq!(Location::new(1, 1).with_col_offset(-1), Location::new(1, 0));
113+
}
114+
115+
#[test]
116+
fn test_with_row_offset() {
117+
assert_eq!(Location::new(1, 1).with_row_offset(1), Location::new(2, 1));
118+
assert_eq!(Location::new(1, 1).with_row_offset(-1), Location::new(0, 1));
119+
}
80120
}

compiler/parser/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ pub(crate) fn parse_error_from_lalrpop(
213213
let expected = (expected.len() == 1).then(|| expected[0].clone());
214214
ParseError {
215215
error: ParseErrorType::UnrecognizedToken(token.1, expected),
216-
location: Location::new(token.0.row(), token.0.column() + 1),
216+
location: token.0.with_col_offset(1),
217217
source_path,
218218
}
219219
}

compiler/parser/src/string_parser.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<'a> StringParser<'a> {
3232
kind,
3333
str_start,
3434
str_end,
35-
location: Location::new(str_start.row(), str_start.column() + offset),
35+
location: str_start.with_col_offset(offset),
3636
}
3737
}
3838

@@ -519,11 +519,7 @@ impl<'a> StringParser<'a> {
519519

520520
fn parse_fstring_expr(source: &str, location: Location) -> Result<Expr, ParseError> {
521521
let fstring_body = format!("({source})");
522-
parse_expression_located(
523-
&fstring_body,
524-
"<fstring>",
525-
Location::new(location.row(), location.column() - 1),
526-
)
522+
parse_expression_located(&fstring_body, "<fstring>", location.with_col_offset(-1))
527523
}
528524

529525
pub fn parse_string(

0 commit comments

Comments
 (0)