Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve spans of Expr::Field parsed from a float Literal #1436

Merged
merged 1 commit into from Apr 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/expr.rs
Expand Up @@ -2716,22 +2716,36 @@ pub(crate) mod parsing {
}

fn multi_index(e: &mut Expr, dot_token: &mut Token![.], float: LitFloat) -> Result<bool> {
let mut float_repr = float.to_string();
let float_token = float.token();
let float_span = float_token.span();
let mut float_repr = float_token.to_string();
let trailing_dot = float_repr.ends_with('.');
if trailing_dot {
float_repr.truncate(float_repr.len() - 1);
}

let mut offset = 0;
for part in float_repr.split('.') {
let index = crate::parse_str(part).map_err(|err| Error::new(float.span(), err))?;
let mut index: Index =
crate::parse_str(part).map_err(|err| Error::new(float_span, err))?;
let part_end = offset + part.len();
index.span = float_token.subspan(offset..part_end).unwrap_or(float_span);

let base = mem::replace(e, Expr::DUMMY);
*e = Expr::Field(ExprField {
attrs: Vec::new(),
base: Box::new(base),
dot_token: Token![.](dot_token.span),
member: Member::Unnamed(index),
});
*dot_token = Token![.](float.span());

let dot_span = float_token
.subspan(part_end..part_end + 1)
.unwrap_or(float_span);
*dot_token = Token![.](dot_span);
offset = part_end + 1;
}

Ok(!trailing_dot)
}

Expand Down