Skip to content

Commit

Permalink
Merge pull request #378 from dtolnay/literalspan
Browse files Browse the repository at this point in the history
Create meaningful span for Literal in FromStr
  • Loading branch information
dtolnay authored Apr 1, 2023
2 parents 287979f + da4c83d commit e163e79
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
24 changes: 17 additions & 7 deletions src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,20 +1020,30 @@ impl Literal {
impl FromStr for Literal {
type Err = LexError;

fn from_str(mut repr: &str) -> Result<Self, Self::Err> {
let negative = repr.starts_with('-');
fn from_str(repr: &str) -> Result<Self, Self::Err> {
let mut cursor = get_cursor(repr);
#[cfg(span_locations)]
let lo = cursor.off;

let negative = cursor.starts_with_char('-');
if negative {
repr = &repr[1..];
if !repr.starts_with(|ch: char| ch.is_ascii_digit()) {
cursor = cursor.advance(1);
if !cursor.starts_with_fn(|ch| ch.is_ascii_digit()) {
return Err(LexError::call_site());
}
}
let cursor = get_cursor(repr);
if let Ok((_rest, mut literal)) = parse::literal(cursor) {
if literal.repr.len() == repr.len() {

if let Ok((rest, mut literal)) = parse::literal(cursor) {
if rest.is_empty() {
if negative {
literal.repr.insert(0, '-');
}
literal.span = Span {
#[cfg(span_locations)]
lo,
#[cfg(span_locations)]
hi: rest.off,
};
return Ok(literal);
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,18 @@ impl<'a> Cursor<'a> {
self.rest.starts_with(s)
}

fn starts_with_char(&self, ch: char) -> bool {
pub fn starts_with_char(&self, ch: char) -> bool {
self.rest.starts_with(ch)
}

fn is_empty(&self) -> bool {
pub fn starts_with_fn<Pattern>(&self, f: Pattern) -> bool
where
Pattern: FnMut(char) -> bool,
{
self.rest.starts_with(f)
}

pub fn is_empty(&self) -> bool {
self.rest.is_empty()
}

Expand Down
20 changes: 20 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,26 @@ fn literal_parse() {
assert!("-\"\"".parse::<Literal>().is_err());
}

#[test]
fn literal_span() {
let positive = "0.1".parse::<Literal>().unwrap();
let negative = "-0.1".parse::<Literal>().unwrap();

#[cfg(not(span_locations))]
{
let _ = positive;
let _ = negative;
}

#[cfg(span_locations)]
{
assert_eq!(positive.span().start().column, 0);
assert_eq!(positive.span().end().column, 3);
assert_eq!(negative.span().start().column, 0);
assert_eq!(negative.span().end().column, 4);
}
}

#[test]
fn roundtrip() {
fn roundtrip(p: &str) {
Expand Down

0 comments on commit e163e79

Please sign in to comment.