mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-09 22:25:23 +00:00
Add with_col_offset and with_row_offset to Location
This commit is contained in:
parent
b6647b0171
commit
72185fecd5
4 changed files with 44 additions and 8 deletions
|
@ -168,7 +168,7 @@ impl SymbolTableError {
|
||||||
pub fn into_codegen_error(self, source_path: String) -> CodegenError {
|
pub fn into_codegen_error(self, source_path: String) -> CodegenError {
|
||||||
CodegenError {
|
CodegenError {
|
||||||
error: CodegenErrorType::SyntaxError(self.error),
|
error: CodegenErrorType::SyntaxError(self.error),
|
||||||
location: Location::new(self.location.row(), self.location.column() + 1),
|
location: self.location.with_col_offset(1),
|
||||||
source_path,
|
source_path,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,6 +58,34 @@ impl Location {
|
||||||
self.row += 1;
|
self.row += 1;
|
||||||
self.column = 0;
|
self.column = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn with_col_offset<T: TryInto<isize>>(&self, offset: T) -> Self
|
||||||
|
where
|
||||||
|
<T as TryInto<isize>>::Error: std::fmt::Debug,
|
||||||
|
{
|
||||||
|
let column = (self.column as isize
|
||||||
|
+ offset
|
||||||
|
.try_into()
|
||||||
|
.expect("offset should be able to convert to isize")) as u32;
|
||||||
|
Location {
|
||||||
|
row: self.row,
|
||||||
|
column,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_row_offset<T: TryInto<isize>>(&self, offset: T) -> Self
|
||||||
|
where
|
||||||
|
<T as TryInto<isize>>::Error: std::fmt::Debug,
|
||||||
|
{
|
||||||
|
let row = (self.row as isize
|
||||||
|
+ offset
|
||||||
|
.try_into()
|
||||||
|
.expect("offset should be able to convert to isize")) as u32;
|
||||||
|
Location {
|
||||||
|
row,
|
||||||
|
column: self.column,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -77,4 +105,16 @@ mod tests {
|
||||||
assert!(Location::new(1, 1) < Location::new(2, 1));
|
assert!(Location::new(1, 1) < Location::new(2, 1));
|
||||||
assert!(Location::new(1, 2) < Location::new(2, 1));
|
assert!(Location::new(1, 2) < Location::new(2, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_col_offset() {
|
||||||
|
assert_eq!(Location::new(1, 1).with_col_offset(1), Location::new(1, 2));
|
||||||
|
assert_eq!(Location::new(1, 1).with_col_offset(-1), Location::new(1, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_row_offset() {
|
||||||
|
assert_eq!(Location::new(1, 1).with_row_offset(1), Location::new(2, 1));
|
||||||
|
assert_eq!(Location::new(1, 1).with_row_offset(-1), Location::new(0, 1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -213,7 +213,7 @@ pub(crate) fn parse_error_from_lalrpop(
|
||||||
let expected = (expected.len() == 1).then(|| expected[0].clone());
|
let expected = (expected.len() == 1).then(|| expected[0].clone());
|
||||||
ParseError {
|
ParseError {
|
||||||
error: ParseErrorType::UnrecognizedToken(token.1, expected),
|
error: ParseErrorType::UnrecognizedToken(token.1, expected),
|
||||||
location: Location::new(token.0.row(), token.0.column() + 1),
|
location: token.0.with_col_offset(1),
|
||||||
source_path,
|
source_path,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ impl<'a> StringParser<'a> {
|
||||||
kind,
|
kind,
|
||||||
str_start,
|
str_start,
|
||||||
str_end,
|
str_end,
|
||||||
location: Location::new(str_start.row(), str_start.column() + offset),
|
location: str_start.with_col_offset(offset),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -523,11 +523,7 @@ impl<'a> StringParser<'a> {
|
||||||
|
|
||||||
fn parse_fstring_expr(source: &str, location: Location) -> Result<Expr, ParseError> {
|
fn parse_fstring_expr(source: &str, location: Location) -> Result<Expr, ParseError> {
|
||||||
let fstring_body = format!("({source})");
|
let fstring_body = format!("({source})");
|
||||||
parse_expression_located(
|
parse_expression_located(&fstring_body, "<fstring>", location.with_col_offset(-1))
|
||||||
&fstring_body,
|
|
||||||
"<fstring>",
|
|
||||||
Location::new(location.row(), location.column() - 1),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_string(
|
pub fn parse_string(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue